diff --git a/CHANGELOG.md b/CHANGELOG.md index a6a851f..24fd9ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ Unreleased - `file_formats.getformat(id, default)` returns `default` for an unknown `id` - Removed `resources/formatlookup.json`; use `file_formats.FORMATLIST` - Added `kolibri-constants/FileFormats` + - Removed `resources/licenselookup.json`; use `licenses.LICENSELIST` + - Added `kolibri-constants/Licenses` + - `licenses.generate_list` no longer casts ids to `int` or fills `description` 0.2.17 Apr 13, 2026 diff --git a/js/Licenses.js b/js/Licenses.js new file mode 100644 index 0000000..4c6bebc --- /dev/null +++ b/js/Licenses.js @@ -0,0 +1,27 @@ +// -*- coding: utf-8 -*- +// Generated by scripts/generate_from_specs.py +// Licenses + +export default { + CC_BY: "CC BY", + CC_BY_SA: "CC BY-SA", + CC_BY_ND: "CC BY-ND", + CC_BY_NC: "CC BY-NC", + CC_BY_NC_SA: "CC BY-NC-SA", + CC_BY_NC_ND: "CC BY-NC-ND", + ALL_RIGHTS_RESERVED: "All Rights Reserved", + PUBLIC_DOMAIN: "Public Domain", + SPECIAL_PERMISSIONS: "Special Permissions", +}; + +export const LICENSELIST = [ + { id: 1, name: "CC BY", exists: true, url: "https://creativecommons.org/licenses/by/4.0/", description: "", custom: false, copyright_holder_required: true }, + { id: 2, name: "CC BY-SA", exists: true, url: "https://creativecommons.org/licenses/by-sa/4.0/", description: "", custom: false, copyright_holder_required: true }, + { id: 3, name: "CC BY-ND", exists: true, url: "https://creativecommons.org/licenses/by-nd/4.0/", description: "", custom: false, copyright_holder_required: true }, + { id: 4, name: "CC BY-NC", exists: true, url: "https://creativecommons.org/licenses/by-nc/4.0/", description: "", custom: false, copyright_holder_required: true }, + { id: 5, name: "CC BY-NC-SA", exists: true, url: "https://creativecommons.org/licenses/by-nc-sa/4.0/", description: "", custom: false, copyright_holder_required: true }, + { id: 6, name: "CC BY-NC-ND", exists: true, url: "https://creativecommons.org/licenses/by-nc-nd/4.0/", description: "", custom: false, copyright_holder_required: true }, + { id: 7, name: "All Rights Reserved", exists: true, url: "http://www.allrights-reserved.com/", description: "", custom: false, copyright_holder_required: true }, + { id: 8, name: "Public Domain", exists: true, url: "https://creativecommons.org/publicdomain/mark/1.0/", description: "", custom: false, copyright_holder_required: false }, + { id: 9, name: "Special Permissions", exists: false, url: "", description: "", custom: true, copyright_holder_required: true }, +]; diff --git a/le_utils/constants/licenses.py b/le_utils/constants/licenses.py index f2d01e1..bbf594f 100644 --- a/le_utils/constants/licenses.py +++ b/le_utils/constants/licenses.py @@ -1,10 +1,10 @@ -import json -import pkgutil -from collections import namedtuple +# -*- coding: utf-8 -*- +# Generated by scripts/generate_from_specs.py +from __future__ import unicode_literals -""" License Constants """ +from collections import namedtuple -# IMPORTANT: Keep these constants in sync with licenselookup.json +# Licenses CC_BY = "CC BY" CC_BY_SA = "CC BY-SA" @@ -29,32 +29,55 @@ ) -class License( - namedtuple( - "License", - [ - "id", - "name", - "exists", - "url", - "description", - "custom", - "copyright_holder_required", - ], - ) -): +class License(namedtuple("License", ["id", "name", "exists", "url", "description", "custom", "copyright_holder_required"])): pass def generate_list(constantlist): - for id, lang in constantlist.items(): - yield License(id=int(id), description="", **lang) - - -def _initialize_license_list(): - constantlist = json.loads(pkgutil.get_data("le_utils", "resources/licenselookup.json").decode("utf-8")) - - return generate_list(constantlist) + for id, record in constantlist.items(): + yield License(id=id, **record) -LICENSELIST = list(_initialize_license_list()) +LICENSELIST = [ + License(id=1, name="CC BY", exists=True, url="https://creativecommons.org/licenses/by/4.0/", description="", custom=False, copyright_holder_required=True), + License( + id=2, name="CC BY-SA", exists=True, url="https://creativecommons.org/licenses/by-sa/4.0/", description="", custom=False, copyright_holder_required=True + ), + License( + id=3, name="CC BY-ND", exists=True, url="https://creativecommons.org/licenses/by-nd/4.0/", description="", custom=False, copyright_holder_required=True + ), + License( + id=4, name="CC BY-NC", exists=True, url="https://creativecommons.org/licenses/by-nc/4.0/", description="", custom=False, copyright_holder_required=True + ), + License( + id=5, + name="CC BY-NC-SA", + exists=True, + url="https://creativecommons.org/licenses/by-nc-sa/4.0/", + description="", + custom=False, + copyright_holder_required=True, + ), + License( + id=6, + name="CC BY-NC-ND", + exists=True, + url="https://creativecommons.org/licenses/by-nc-nd/4.0/", + description="", + custom=False, + copyright_holder_required=True, + ), + License( + id=7, name="All Rights Reserved", exists=True, url="http://www.allrights-reserved.com/", description="", custom=False, copyright_holder_required=True + ), + License( + id=8, + name="Public Domain", + exists=True, + url="https://creativecommons.org/publicdomain/mark/1.0/", + description="", + custom=False, + copyright_holder_required=False, + ), + License(id=9, name="Special Permissions", exists=False, url="", description="", custom=True, copyright_holder_required=True), +] diff --git a/le_utils/resources/licenselookup.json b/le_utils/resources/licenselookup.json deleted file mode 100644 index 845f221..0000000 --- a/le_utils/resources/licenselookup.json +++ /dev/null @@ -1,65 +0,0 @@ -{ - "1": { - "name": "CC BY", - "exists": true, - "custom": false, - "copyright_holder_required": true, - "url": "https://creativecommons.org/licenses/by/4.0/" - }, - "2": { - "name": "CC BY-SA", - "exists": true, - "custom": false, - "copyright_holder_required": true, - "url": "https://creativecommons.org/licenses/by-sa/4.0/" - }, - "3": { - "name": "CC BY-ND", - "exists": true, - "custom": false, - "copyright_holder_required": true, - "url": "https://creativecommons.org/licenses/by-nd/4.0/" - }, - "4": { - "name": "CC BY-NC", - "exists": true, - "custom": false, - "copyright_holder_required": true, - "url": "https://creativecommons.org/licenses/by-nc/4.0/" - }, - "5": { - "name": "CC BY-NC-SA", - "exists": true, - "custom": false, - "copyright_holder_required": true, - "url": "https://creativecommons.org/licenses/by-nc-sa/4.0/" - }, - "6": { - "name": "CC BY-NC-ND", - "exists": true, - "custom": false, - "copyright_holder_required": true, - "url": "https://creativecommons.org/licenses/by-nc-nd/4.0/" - }, - "7": { - "name": "All Rights Reserved", - "exists": true, - "custom": false, - "copyright_holder_required": true, - "url": "http://www.allrights-reserved.com/" - }, - "8": { - "name": "Public Domain", - "exists": true, - "custom": false, - "copyright_holder_required": false, - "url": "https://creativecommons.org/publicdomain/mark/1.0/" - }, - "9": { - "name": "Special Permissions", - "exists": false, - "custom": true, - "copyright_holder_required": true, - "url": "" - } -} diff --git a/scripts/generate_from_specs.py b/scripts/generate_from_specs.py index ef834b9..eb659ab 100644 --- a/scripts/generate_from_specs.py +++ b/scripts/generate_from_specs.py @@ -240,14 +240,22 @@ def define_constant(constants, constant, value): def namedtuple_constants(spec): constants = OrderedDict() ids = set() + values = set() + fields = spec["namedtuple"]["fields"] + constant_field = spec["namedtuple"].get("constant_field", "id") + if constant_field not in fields: + raise ValueError("constant_field {} is not a field".format(constant_field)) for record in spec["records"]: - for field in spec["namedtuple"]["fields"]: + for field in fields: if field not in record: raise ValueError("Record {} has no field {}".format(record["constant"], field)) if record["id"] in ids: raise ValueError("Record id {} is defined twice".format(record["id"])) ids.add(record["id"]) - define_constant(constants, record["constant"], record["id"]) + if record[constant_field] in values: + raise ValueError("Record {} {} is defined twice".format(constant_field, record[constant_field])) + values.add(record[constant_field]) + define_constant(constants, record["constant"], record[constant_field]) for field in record.get("field_constants", []): define_constant(constants, "{}_{}".format(record["constant"], field.upper()), record[field]) for constant, value in spec.get("constants", {}).items(): diff --git a/spec/constants-licenses.json b/spec/constants-licenses.json new file mode 100644 index 0000000..e09d0ad --- /dev/null +++ b/spec/constants-licenses.json @@ -0,0 +1,119 @@ +{ + "namedtuple": { + "name": "License", + "fields": ["id", "name", "exists", "url", "description", "custom", "copyright_holder_required"], + "constant_field": "name" + }, + "records": [ + { + "constant": "CC_BY", + "label": "CC BY", + "id": 1, + "name": "CC BY", + "exists": true, + "url": "https://creativecommons.org/licenses/by/4.0/", + "description": "", + "custom": false, + "copyright_holder_required": true + }, + { + "constant": "CC_BY_SA", + "label": "CC BY-SA", + "id": 2, + "name": "CC BY-SA", + "exists": true, + "url": "https://creativecommons.org/licenses/by-sa/4.0/", + "description": "", + "custom": false, + "copyright_holder_required": true + }, + { + "constant": "CC_BY_ND", + "label": "CC BY-ND", + "id": 3, + "name": "CC BY-ND", + "exists": true, + "url": "https://creativecommons.org/licenses/by-nd/4.0/", + "description": "", + "custom": false, + "copyright_holder_required": true + }, + { + "constant": "CC_BY_NC", + "label": "CC BY-NC", + "id": 4, + "name": "CC BY-NC", + "exists": true, + "url": "https://creativecommons.org/licenses/by-nc/4.0/", + "description": "", + "custom": false, + "copyright_holder_required": true + }, + { + "constant": "CC_BY_NC_SA", + "label": "CC BY-NC-SA", + "id": 5, + "name": "CC BY-NC-SA", + "exists": true, + "url": "https://creativecommons.org/licenses/by-nc-sa/4.0/", + "description": "", + "custom": false, + "copyright_holder_required": true + }, + { + "constant": "CC_BY_NC_ND", + "label": "CC BY-NC-ND", + "id": 6, + "name": "CC BY-NC-ND", + "exists": true, + "url": "https://creativecommons.org/licenses/by-nc-nd/4.0/", + "description": "", + "custom": false, + "copyright_holder_required": true + }, + { + "constant": "ALL_RIGHTS_RESERVED", + "label": "All Rights Reserved", + "id": 7, + "name": "All Rights Reserved", + "exists": true, + "url": "http://www.allrights-reserved.com/", + "description": "", + "custom": false, + "copyright_holder_required": true + }, + { + "constant": "PUBLIC_DOMAIN", + "label": "Public Domain", + "id": 8, + "name": "Public Domain", + "exists": true, + "url": "https://creativecommons.org/publicdomain/mark/1.0/", + "description": "", + "custom": false, + "copyright_holder_required": false + }, + { + "constant": "SPECIAL_PERMISSIONS", + "label": "Special Permissions", + "id": 9, + "name": "Special Permissions", + "exists": false, + "url": "", + "description": "", + "custom": true, + "copyright_holder_required": true + } + ], + "choices_order": [ + "CC_BY", + "CC_BY_SA", + "CC_BY_ND", + "CC_BY_NC", + "CC_BY_NC_SA", + "CC_BY_NC_ND", + "ALL_RIGHTS_RESERVED", + "PUBLIC_DOMAIN", + "SPECIAL_PERMISSIONS" + ] +} diff --git a/tests/test_generate_from_specs.py b/tests/test_generate_from_specs.py index 7e519d2..2a5b635 100644 --- a/tests/test_generate_from_specs.py +++ b/tests/test_generate_from_specs.py @@ -96,6 +96,22 @@ def test_record_missing_field_raises(tmp_path): assert_spec_raises(tmp_path, spec(records=[record])) +def test_constant_field_not_in_fields_raises(tmp_path): + assert_spec_raises(tmp_path, spec(namedtuple=dict(spec()["namedtuple"], constant_field="label"))) + + +def test_constant_field_repeated_value_raises(tmp_path): + records = [mp4_record(field_constants=[]), mp4_record(constant="M4V", id="m4v", field_constants=[])] + assert_spec_raises( + tmp_path, + spec( + namedtuple=dict(spec()["namedtuple"], constant_field="mimetype"), + records=records, + choices_order=["MP4", "M4V"], + ), + ) + + def write_and_exec(tmp_path, valid_spec): output_file = str(tmp_path / "formats.py") generate_from_specs.write_namedtuple_python_file(output_file, "FileFormats", valid_spec) @@ -131,6 +147,11 @@ def test_spec_without_getter_omits_getter(tmp_path): assert namespace["FORMATLIST"] == [namespace["Format"](id="mp4", mimetype="video/mp4")] +def test_constant_field_sets_record_constant_value(tmp_path): + namespace = write_and_exec(tmp_path, spec(namedtuple=dict(spec()["namedtuple"], constant_field="mimetype"))) + assert namespace["MP4"] == "video/mp4" + + @pytest.mark.skipif(shutil.which("node") is None, reason="node is not installed") def test_js_exports_constants_and_records(tmp_path): output_file = tmp_path / "Formats.mjs"