|
| 1 | +import json |
| 2 | +import re |
| 3 | +from argparse import ArgumentParser, Namespace |
| 4 | +from collections import OrderedDict |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | + |
| 8 | +def get_args() -> Namespace: |
| 9 | + parser = ArgumentParser(prog='update-version') |
| 10 | + parser.add_argument('-v', '--version', required=True) |
| 11 | + return parser.parse_args() |
| 12 | + |
| 13 | + |
| 14 | +def get_template_data() -> dict: |
| 15 | + pattern = re.compile(r"localize\s*?\(\s*?(?P<q1>[\'\"])(?P<key>[\w\.]+?)\s*?(?P=q1)" |
| 16 | + r",\s*?(?P<q2>[\'\"])(?P<val>.+?)(?<!\\\\)(?P=q2)\s*?.*?\)") |
| 17 | + |
| 18 | + template_path = Path(__file__).parent.parent.parent / 'Submitty' / 'site' / 'app' / 'templates' |
| 19 | + if not template_path.is_dir(): |
| 20 | + raise NotADirectoryError('Could not locate template directory.') |
| 21 | + |
| 22 | + data = dict() |
| 23 | + |
| 24 | + # Loop through template files |
| 25 | + for child in template_path.iterdir(): |
| 26 | + if not child.is_file() or child.suffix != '.twig': |
| 27 | + continue |
| 28 | + |
| 29 | + # Split into template blocks {{ }} |
| 30 | + body = child.read_text() |
| 31 | + parts = [part.split('}}')[0] for part in body.split('{{')[1:]] |
| 32 | + |
| 33 | + for part in parts: |
| 34 | + for match in re.finditer(pattern, part): |
| 35 | + group = match.groupdict() |
| 36 | + tree = group.get('key').split('.') |
| 37 | + val = group.get('val') |
| 38 | + |
| 39 | + last_key = tree.pop() |
| 40 | + |
| 41 | + loc = data # Current location in tree (should always be dict) |
| 42 | + for key in tree: |
| 43 | + if key in loc: |
| 44 | + loc = loc[key] |
| 45 | + if not isinstance(loc, dict): |
| 46 | + raise KeyError('Duplicate template key found: ' + key) |
| 47 | + else: |
| 48 | + loc[key] = dict() |
| 49 | + loc = loc[key] |
| 50 | + |
| 51 | + if not isinstance(loc, dict): |
| 52 | + raise KeyError('Duplicate template key found: ' + key) |
| 53 | + loc[last_key] = val |
| 54 | + |
| 55 | + return data |
| 56 | + |
| 57 | + |
| 58 | +def update_data(original: OrderedDict, updated: dict) -> OrderedDict: |
| 59 | + result = OrderedDict() |
| 60 | + |
| 61 | + # Update existing keys |
| 62 | + for key, val in original.items(): |
| 63 | + if key not in updated: |
| 64 | + continue |
| 65 | + |
| 66 | + if isinstance(val, OrderedDict) and isinstance(updated[key], dict): |
| 67 | + result[key] = update_data(val, updated[key]) |
| 68 | + else: |
| 69 | + result[key] = updated[key] |
| 70 | + |
| 71 | + # Add new keys |
| 72 | + for key, val in updated.items(): |
| 73 | + if key not in original: |
| 74 | + if isinstance(val, dict): |
| 75 | + result[key] = OrderedDict(val) |
| 76 | + else: |
| 77 | + result[key] = val |
| 78 | + |
| 79 | + return result |
| 80 | + |
| 81 | + |
| 82 | +def main(): |
| 83 | + args = get_args() |
| 84 | + |
| 85 | + repo_path = Path(__file__).parent.parent |
| 86 | + if not repo_path.is_dir(): |
| 87 | + raise NotADirectoryError('Could not locate repository.') |
| 88 | + |
| 89 | + # Update version in JSON file |
| 90 | + with (repo_path / 'config.json').open() as file: |
| 91 | + data = json.load(file, object_pairs_hook=OrderedDict) |
| 92 | + data['submitty_version'] = args.version |
| 93 | + with (repo_path / 'config.json').open('w') as file: |
| 94 | + json.dump(data, file, indent=2) |
| 95 | + |
| 96 | + # Update default lang data |
| 97 | + with (repo_path / 'lang' / 'en_US.json').open() as file: |
| 98 | + json_data = json.load(file, object_pairs_hook=OrderedDict) |
| 99 | + json_data = update_data(json_data, get_template_data()) |
| 100 | + with (repo_path / 'lang' / 'en_US.json').open('w') as file: |
| 101 | + json.dump(json_data, file, indent=4) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == '__main__': |
| 105 | + main() |
0 commit comments