| 35 | |
| 36 | |
| 37 | class Make: |
| 38 | __keyword_indicator = "k#:" |
| 39 | __keyword_end_indicator = ":#" |
| 40 | __exp_dir = EXP_PATH |
| 41 | |
| 42 | def __init__(self, path: Path): |
| 43 | with open(path) as f: |
| 44 | self.config: collections.abc.Mapping = yaml.safe_load(f) |
| 45 | if not isinstance(self.config, collections.abc.Mapping): |
| 46 | raise Exception("Invalid config file") |
| 47 | self.__keywords() |
| 48 | self.targets = { |
| 49 | k: Target.from_dict(k, v) |
| 50 | for k, v in self.config["targets"].items() |
| 51 | } |
| 52 | self.__exp_dir.mkdir(exist_ok=True) |
| 53 | |
| 54 | def has_keyword(self, src: str) -> bool: |
| 55 | return self.__keyword_indicator in src |
| 56 | |
| 57 | def resolve_keyword(self, src: str, visit: Set[str] = None) -> str: |
| 58 | if visit is None: |
| 59 | visit = set() |
| 60 | |
| 61 | chunks = self.__keyword_chunks(src) |
| 62 | replace = dict() |
| 63 | for chunk in chunks: |
| 64 | keyword = self.__keyword_in_chunk(chunk) |
| 65 | content = self.keywords[keyword] |
| 66 | |
| 67 | if not self.has_keyword(content): |
| 68 | replace[chunk] = content |
| 69 | continue |
| 70 | |
| 71 | if keyword in visit: |
| 72 | raise RuntimeError(f"Circlular reference [{keyword}]") |
| 73 | |
| 74 | visit.add(keyword) |
| 75 | content = self.resolve_keyword(self.keywords[keyword], visit) |
| 76 | visit.remove(keyword) |
| 77 | replace[chunk] = content |
| 78 | |
| 79 | reptargets = list(replace.keys()) |
| 80 | for reptarget in reptargets: |
| 81 | src = src.replace(reptarget, replace[reptarget]) |
| 82 | |
| 83 | if self.has_keyword(src): |
| 84 | raise RuntimeError(f"Error: {src}") |
| 85 | |
| 86 | return src |
| 87 | |
| 88 | def download(self, name: str): |
| 89 | |
| 90 | check_key(name, self.targets) |
| 91 | target = self.targets[name] |
| 92 | |
| 93 | dirname = target.name |
| 94 | |