Skip to content
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions js/Licenses.js
Original file line number Diff line number Diff line change
@@ -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 },
];
79 changes: 51 additions & 28 deletions le_utils/constants/licenses.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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),
]
65 changes: 0 additions & 65 deletions le_utils/resources/licenselookup.json

This file was deleted.

12 changes: 10 additions & 2 deletions scripts/generate_from_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
119 changes: 119 additions & 0 deletions spec/constants-licenses.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
21 changes: 21 additions & 0 deletions tests/test_generate_from_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down
Loading