stored licenses to filename mapping in json file

This commit is contained in:
alexzvk 2022-11-28 16:22:03 -05:00
parent c91a5d2ce3
commit 9a39937b0c
3 changed files with 9 additions and 6 deletions

View File

@ -14,8 +14,7 @@ import os
import random import random
import shutil import shutil
import string import string
os.chdir('../scripts/') import json
from update_licenses import titles_dict
try: try:
# Inspired by # Inspired by
@ -332,6 +331,8 @@ def handle_licenses():
with open(os.path.join("licenses", "-temporary-placeholder.txt")) as f: with open(os.path.join("licenses", "-temporary-placeholder.txt")) as f:
selected_title = f.readline() selected_title = f.readline()
with open('../licenses.json', 'r') as f:
titles_dict = json.load(f)
# access the title to filename dictionary to find the correct file # access the title to filename dictionary to find the correct file
# using a dictionary instead of looping reduces time complexity # using a dictionary instead of looping reduces time complexity
with open(os.path.join("licenses", titles_dict[selected_title])) as f: with open(os.path.join("licenses", titles_dict[selected_title])) as f:

1
licenses.json Normal file
View File

@ -0,0 +1 @@
{"BSD Zero Clause License": "0bsd.txt", "Academic Free License v3.0": "afl-3.0.txt", "GNU Affero General Public License v3.0": "agpl-3.0.txt", "Apache License 2.0": "apache-2.0.txt", "Artistic License 2.0": "artistic-2.0.txt", "BSD 2-Clause \"Simplified\" License": "bsd-2-clause.txt", "BSD 3-Clause Clear License": "bsd-3-clause-clear.txt", "BSD 3-Clause \"New\" or \"Revised\" License": "bsd-3-clause.txt", "BSD 4-Clause \"Original\" or \"Old\" License": "bsd-4-clause.txt", "Boost Software License 1.0": "bsl-1.0.txt", "Creative Commons Attribution 4.0 International": "cc-by-4.0.txt", "Creative Commons Attribution Share Alike 4.0 International": "cc-by-sa-4.0.txt", "Creative Commons Zero v1.0 Universal": "cc0-1.0.txt", "CeCILL Free Software License Agreement v2.1": "cecill-2.1.txt", "CERN Open Hardware Licence Version 2 - Permissive": "cern-ohl-p-2.0.txt", "CERN Open Hardware Licence Version 2 - Strongly Reciprocal": "cern-ohl-s-2.0.txt", "CERN Open Hardware Licence Version 2 - Weakly Reciprocal": "cern-ohl-w-2.0.txt", "Educational Community License v2.0": "ecl-2.0.txt", "Eclipse Public License 1.0": "epl-1.0.txt", "Eclipse Public License 2.0": "epl-2.0.txt", "European Union Public License 1.1": "eupl-1.1.txt", "European Union Public License 1.2": "eupl-1.2.txt", "GNU Free Documentation License v1.3": "gfdl-1.3.txt", "GNU General Public License v2.0": "gpl-2.0.txt", "GNU General Public License v3.0": "gpl-3.0.txt", "ISC License": "isc.txt", "GNU Lesser General Public License v2.1": "lgpl-2.1.txt", "GNU Lesser General Public License v3.0": "lgpl-3.0.txt", "LaTeX Project Public License v1.3c": "lppl-1.3c.txt", "MIT No Attribution": "mit-0.txt", "MIT License": "mit.txt", "Mozilla Public License 2.0": "mpl-2.0.txt", "Microsoft Public License": "ms-pl.txt", "Microsoft Reciprocal License": "ms-rl.txt", "Mulan Permissive Software License, Version 2": "mulanpsl-2.0.txt", "University of Illinois/NCSA Open Source License": "ncsa.txt", "Open Data Commons Open Database License v1.0": "odbl-1.0.txt", "SIL Open Font License 1.1": "ofl-1.1.txt", "Open Software License 3.0": "osl-3.0.txt", "PostgreSQL License": "postgresql.txt", "The Unlicense": "unlicense.txt", "Universal Permissive License v1.0": "upl-1.0.txt", "Vim License": "vim.txt", "\"Do What The F*ck You Want To Public License\"": "wtfpl.txt", "zlib License": "zlib.txt"}

View File

@ -8,9 +8,7 @@ from github import Github
CURRENT_FILE = Path(__file__) CURRENT_FILE = Path(__file__)
ROOT = CURRENT_FILE.parents[1] ROOT = CURRENT_FILE.parents[1]
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", None) GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", None)
GITHUB_TOKEN = "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"
# dictionary declared here to be exported to post_gen_project, for faster license access
titles_dict = {}
def main() -> None: def main() -> None:
""" """
@ -19,7 +17,6 @@ def main() -> None:
repo = Github(login_or_token=GITHUB_TOKEN).get_repo("github/choosealicense.com") repo = Github(login_or_token=GITHUB_TOKEN).get_repo("github/choosealicense.com")
license_dir = ROOT / "{{cookiecutter.project_slug}}" / "licenses" license_dir = ROOT / "{{cookiecutter.project_slug}}" / "licenses"
license_dir.mkdir(exist_ok=True) license_dir.mkdir(exist_ok=True)
global titles_dict
for file in repo.get_contents("_licenses", "gh-pages"): for file in repo.get_contents("_licenses", "gh-pages"):
content = codecs.decode(file.decoded_content) content = codecs.decode(file.decoded_content)
# make below line into a dictionary mapping to filename # make below line into a dictionary mapping to filename
@ -29,6 +26,10 @@ def main() -> None:
if not path.is_file(): if not path.is_file():
path.touch() path.touch()
(license_dir / file.name).write_text(replace_content_options(content)) (license_dir / file.name).write_text(replace_content_options(content))
# write the titles dictionary to a json file and put it in workflows so it can be accessed by other files
with open('licenses.json', 'w') as licenses_dict:
licenses_dict.write(json.dumps(titles_dict))
# Put "Not open source" at front so people know it's an option # Put "Not open source" at front so people know it's an option
front_options = [ front_options = [
"Not open source", "Not open source",