Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use $schema and $id in the right way #283

Merged
merged 9 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions menuinst/_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@


log = getLogger(__name__)
SCHEMA_DIALECT = "http://json-schema.org/draft-07/schema#"
SCHEMA_VERSION = "1"
SCHEMA_URL = f"https://schemas.conda.org/menuinst-{SCHEMA_VERSION}.schema.json"


def _clean_description(description: str) -> str:
Expand Down Expand Up @@ -695,14 +698,21 @@ class MenuItem(BaseModel):
class MenuInstSchema(BaseModel):
"Metadata required to create menu items across operating systems with `menuinst`."

id_: Literal["https://schemas.conda.io/menuinst-1.schema.json"] = Field(
...,
description=("Version of the menuinst schema."),
class Config(BaseModel.Config):
schema_extra = {
"$schema": SCHEMA_DIALECT,
"$id": SCHEMA_URL,
}

id_: constr(min_length=1) = Field(
SCHEMA_URL,
description="DEPRECATED. Use ``$schema``.",
alias="$id",
deprecated=True,
)
schema_: Literal["https://json-schema.org/draft-07/schema"] = Field(
...,
description=("Standard of the JSON schema we adhere to."),
schema_: constr(min_length=1) = Field(
SCHEMA_URL,
description="Version of the menuinst schema.",
alias="$schema",
)
menu_name: constr(min_length=1) = Field(
Expand All @@ -716,14 +726,15 @@ class MenuInstSchema(BaseModel):


def dump_schema_to_json(write=True):
schema = MenuInstSchema.schema()
if write:
here = Path(__file__).parent
schema = MenuInstSchema.schema_json(indent=2)
print(schema)
with open(here / "data" / "menuinst.schema.json", "w") as f:
f.write(schema)
schema_str = json.dumps(schema, indent=2)
print(schema_str)
with open(here / "data" / f"menuinst-{SCHEMA_VERSION}.schema.json", "w") as f:
f.write(schema_str)
f.write("\n")
return MenuInstSchema.schema()
return schema


def dump_default_to_json(write=True):
Expand All @@ -736,21 +747,17 @@ def dump_default_to_json(write=True):
"osx": MacOS().dict(),
"linux": Linux().dict(),
}
default = MenuInstSchema(
menu_name="REQUIRED",
menu_items=[default_item],
**{
"$id": "https://schemas.conda.io/menuinst-1.schema.json",
"$schema": "https://json-schema.org/draft-07/schema",
},
).dict()
default = MenuInstSchema(menu_name="REQUIRED", menu_items=[default_item]).dict()
default["$schema"] = SCHEMA_URL
default.pop("id_", None)
default.pop("schema_", None)
for platform_value in default["menu_items"][0]["platforms"].values():
for key in list(platform_value.keys()):
if key in MenuItem.__fields__:
platform_value.pop(key)
if write:
pprint(default)
with open(here / "data" / "menuinst.default.json", "w") as f:
with open(here / "data" / f"menuinst-{SCHEMA_VERSION}.default.json", "w") as f:
json.dump(default, f, indent=2)
f.write("\n")
return default
Expand Down
68 changes: 68 additions & 0 deletions menuinst/data/menuinst-1.default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"menu_name": "REQUIRED",
"menu_items": [
{
"name": "REQUIRED",
"description": "REQUIRED",
"command": [
"REQUIRED"
],
"icon": null,
"precommand": null,
"precreate": null,
"working_dir": null,
"activate": true,
"terminal": false,
"platforms": {
"linux": {
"Categories": null,
"DBusActivatable": null,
"GenericName": null,
"Hidden": null,
"Implements": null,
"Keywords": null,
"MimeType": null,
"NoDisplay": null,
"NotShowIn": null,
"OnlyShowIn": null,
"PrefersNonDefaultGPU": null,
"SingleMainWindow": null,
"StartupNotify": null,
"StartupWMClass": null,
"TryExec": null,
"glob_patterns": null
},
"osx": {
"CFBundleDisplayName": null,
"CFBundleIdentifier": null,
"CFBundleName": null,
"CFBundleSpokenName": null,
"CFBundleVersion": null,
"CFBundleURLTypes": null,
"CFBundleDocumentTypes": null,
"LSApplicationCategoryType": null,
"LSBackgroundOnly": null,
"LSEnvironment": null,
"LSMinimumSystemVersion": null,
"LSMultipleInstancesProhibited": null,
"LSRequiresNativeExecution": null,
"NSSupportsAutomaticGraphicsSwitching": null,
"UTExportedTypeDeclarations": null,
"UTImportedTypeDeclarations": null,
"entitlements": null,
"link_in_bundle": null,
"event_handler": null
},
"win": {
"desktop": true,
"quicklaunch": false,
"terminal_profile": null,
"url_protocols": null,
"file_extensions": null,
"app_user_model_id": null
}
}
}
],
"$schema": "https://schemas.conda.org/menuinst-1.schema.json"
}
Loading
Loading