|
| 1 | +import io |
| 2 | +from collections import defaultdict |
| 3 | +from collections.abc import Iterable, Sequence |
| 4 | +from functools import cached_property, reduce |
| 5 | +from itertools import chain |
| 6 | +from pathlib import Path |
| 7 | +from types import MappingProxyType |
| 8 | + |
| 9 | +from .make_ver import LANGUAGES, Language, Version, VersionModel |
| 10 | + |
| 11 | + |
| 12 | +class LanguageVersions(): |
| 13 | + r""" |
| 14 | + >>> from .make_ver import _testfiles |
| 15 | + >>> with _testfiles() as files: |
| 16 | + ... l = LanguageVersions(files) |
| 17 | +
|
| 18 | + `hello_world` is in the VERSION_ORDER and so will come first, then the rest are then ordered |
| 19 | + >>> l.all_versions |
| 20 | + ('hello_world', 'test1', 'test2', 'test4') |
| 21 | + >>> l.languages['js']['test4'] |
| 22 | + 'console.log("Hello Test")' |
| 23 | + """ |
| 24 | + |
| 25 | + VERSION_ORDER = [ # TODO: these should be moved eventually |
| 26 | + 'title', |
| 27 | + 'download', |
| 28 | + 'help', |
| 29 | + 'run', |
| 30 | + 'hello_world', |
| 31 | + 'read_line_from_console', |
| 32 | + 'comment', |
| 33 | + 'define_variables', |
| 34 | + 'define_constants', |
| 35 | + 'arithmetic', |
| 36 | + 'if_statement', |
| 37 | + 'if_statement_more', |
| 38 | + 'while_loop', |
| 39 | + 'until_loop', |
| 40 | + 'for_loop', |
| 41 | + 'for_each_loop', |
| 42 | + 'file_write', |
| 43 | + 'file_read', |
| 44 | + 'string_concatenation', |
| 45 | + 'split_strings', |
| 46 | + 'convert_string_to_integer_and_back', |
| 47 | + 'convert_double_to_string_and_back', |
| 48 | + 'function', |
| 49 | + 'function_with_return_value', |
| 50 | + 'function_with_params_by_reference', |
| 51 | + 'function_with_params_by_value', |
| 52 | + 'function_with_param_function', |
| 53 | + 'define_fixed_array', |
| 54 | + 'define_list', |
| 55 | + 'define_2d_arrays_with_nested_arrays', |
| 56 | + 'define_2d_arrays_with_1d_array_with_lookup_function', |
| 57 | + 'define_2d_arrays_with_dictionary', |
| 58 | + 'define_map', |
| 59 | + 'define_set', |
| 60 | + 'error_handling', |
| 61 | + 'join_strings', |
| 62 | + 'random_number', |
| 63 | + 'class', |
| 64 | + 'read_csv_into_array_of_classs', |
| 65 | + 'sleep', |
| 66 | + 'list_comprehension', |
| 67 | + 'dict_comprehension', |
| 68 | + ] |
| 69 | + |
| 70 | + def __init__(self, files: Iterable[Path]): |
| 71 | + """ |
| 72 | + Concept: |
| 73 | + `/java/main_stuff.java` |
| 74 | + `/java/graphics_stuff.java` |
| 75 | + `/java/network_stuff.java` |
| 76 | + are amalgamated/concatenated into `self.files['java']` |
| 77 | + This means that we can have |
| 78 | + `hello_world` |
| 79 | + `draw_sqaure` |
| 80 | + `get_http` |
| 81 | + defined in different files but still available as a version |
| 82 | + """ |
| 83 | + def _amalgamate_files_with_same_extension(acc, path): |
| 84 | + acc[''.join(path.suffixes).strip('.')].append(path.read_text('utf-8')) |
| 85 | + return acc |
| 86 | + self.files = MappingProxyType({ |
| 87 | + ext: "\n".join(file_content_list) |
| 88 | + for ext, file_content_list in reduce( |
| 89 | + _amalgamate_files_with_same_extension, |
| 90 | + files, |
| 91 | + defaultdict(list), |
| 92 | + ).items() |
| 93 | + }) |
| 94 | + |
| 95 | + @property |
| 96 | + def all_versions(self) -> Sequence[Version]: |
| 97 | + versions = frozenset(chain.from_iterable(version_data.keys() for version_data in self.languages.values())) |
| 98 | + return tuple(v for v in self.VERSION_ORDER if v in versions) + tuple(sorted(v for v in versions if v not in self.VERSION_ORDER)) |
| 99 | + |
| 100 | + @cached_property |
| 101 | + def languages(self) -> MappingProxyType[str, MappingProxyType[Version, str]]: |
| 102 | + return MappingProxyType({ |
| 103 | + language: self._build_versions(io.StringIO(self.files.get(language)), LANGUAGES[language]) |
| 104 | + for language in LANGUAGES.keys() |
| 105 | + }) |
| 106 | + |
| 107 | + @classmethod |
| 108 | + def _build_versions(cls, source: io.IOBase, language: Language) -> MappingProxyType[Version, str]: |
| 109 | + r""" |
| 110 | + >>> from textwrap import dedent |
| 111 | + >>> java = io.StringIO(dedent(''' |
| 112 | + ... import java.util.stream.Collectors; // VER: list_comprehension,dict_comprehension |
| 113 | + ... import static java.util.Map.entry; // VER: dict_comprehension |
| 114 | + ... public class Java { |
| 115 | + ... public static void main(String[] args) {new Java();} |
| 116 | + ... public Java() { |
| 117 | + ... hello_world(); |
| 118 | + ... arithmetic(); |
| 119 | + ... } |
| 120 | + ... void hello_world() { |
| 121 | + ... // // Must be in file named `HelloWorld.java` // VER: hello_world |
| 122 | + ... //public class HelloWorld { // VER: hello_world |
| 123 | + ... //public static void main(String[] args) {new HelloWorld();} // VER: hello_world |
| 124 | + ... //public HelloWorld() { // VER: hello_world |
| 125 | + ... System.out.println("Hello World"); // VER: hello_world |
| 126 | + ... //} // VER: hello_world |
| 127 | + ... //} // VER: hello_world |
| 128 | + ... } |
| 129 | + ... void list_comprehension() { |
| 130 | + ... List<Integer> data1 = new ArrayList<>(Arrays.asList(new Integer[]{1,2,3,4,5,6})); // VER: list_comprehension |
| 131 | + ... } |
| 132 | + ... void dict_comprehension() { |
| 133 | + ... Map<String,Integer> data3 = Map.ofEntries( // VER: dict_comprehension |
| 134 | + ... entry("a", 1), // VER: dict_comprehension |
| 135 | + ... entry("b", 2) // VER: dict_comprehension |
| 136 | + ... ); // VER: dict_comprehension |
| 137 | + ... } |
| 138 | + ... } |
| 139 | + ... ''')) |
| 140 | + >>> versions = LanguageVersions._build_versions(java, LANGUAGES['java']) |
| 141 | +
|
| 142 | + >>> sorted(versions.keys()) |
| 143 | + ['dict_comprehension', 'hello_world', 'list_comprehension'] |
| 144 | +
|
| 145 | + >>> print(versions['hello_world']) |
| 146 | + // Must be in file named `HelloWorld.java` |
| 147 | + public class HelloWorld { |
| 148 | + public static void main(String[] args) {new HelloWorld();} |
| 149 | + public HelloWorld() { |
| 150 | + System.out.println("Hello World"); |
| 151 | + } |
| 152 | + } |
| 153 | +
|
| 154 | + >>> print(versions['list_comprehension']) |
| 155 | + import java.util.stream.Collectors; |
| 156 | + List<Integer> data1 = new ArrayList<>(Arrays.asList(new Integer[]{1,2,3,4,5,6})); |
| 157 | +
|
| 158 | + >>> print(versions['dict_comprehension']) |
| 159 | + import java.util.stream.Collectors; |
| 160 | + import static java.util.Map.entry; |
| 161 | + Map<String,Integer> data3 = Map.ofEntries( |
| 162 | + entry("a", 1), |
| 163 | + entry("b", 2) |
| 164 | + ); |
| 165 | + """ |
| 166 | + lines = VersionModel(source, language).lines |
| 167 | + # We build a dict incrementally with the versions from each line. |
| 168 | + # This is not the way ProjectVersions works. |
| 169 | + # Perhaps we can get a list of all versions and then run the version evaluator for each line to include it? |
| 170 | + # The process below is definitely efficient to build, but I wonder if a single code path for versions would be neater and cleaner |
| 171 | + # For now LanguageVersions feels like it's own case, but it feels weird because `VER:` lines are used for different things in different ways |
| 172 | + def _reducer(acc: defaultdict[Version, list[str]], line: VersionModel.Line) -> defaultdict[Version, list[str]]: |
| 173 | + for version in line.version_evaluator.versions: |
| 174 | + if not version: # Lines with not explicitly tagged with a version are not considered in LanguageVersions |
| 175 | + continue |
| 176 | + acc[version].append(line.line_without_ver) |
| 177 | + return acc |
| 178 | + return MappingProxyType({ |
| 179 | + version: "\n".join(lines) |
| 180 | + for version, lines in reduce(_reducer, lines, defaultdict(list[str])).items() |
| 181 | + }) |
| 182 | + |
| 183 | + # @classmethod |
| 184 | + # def build_versions_from_path(cls: Self, path: str | Path) -> MappingProxyType[Version, str]: |
| 185 | + # path = Path(path) |
| 186 | + # language = LANGUAGES.get(path.suffix.strip('.')) |
| 187 | + # assert language, f'Language unknown: {path.suffix}. Valid languages are {LANGUAGES.keys()}' |
| 188 | + # with path.open() as source: |
| 189 | + # return cls._build_versions(source, language) |
0 commit comments