def load_file(session: Session, path: PurePath, content: str): data = deserialize_file(path.suffix, content) if "_type" in data: cls = Model._decl_class_registry[data["_type"]] # type: ignore else: for model_class in Model._decl_class_registry.values(): if hasattr(model_class, "babu_paths"): for path_glob in model_class.babu_paths: if path.match(path_glob): cls = model_class break inst = cls() inst.id = path.with_name(path.stem).as_posix() for k, v in data.items(): if k == "_type": continue if k == CONTENT: inspector = inspect(cls) for col in inspector.columns: if isinstance(col.type, Content): k = col.name break else: raise ValueError(f"Class {cls} has no content column") setattr(inst, k, v) session.add(inst) session.commit()
# 純粋パスを扱う(PurePath)------------------------------------------------- from pathlib import PurePath p = PurePath('/hoge/fuga/piyo.txt') pprint(p.drive) pprint(p.root) pprint(p.anchor) pprint(list(p.parents)) pprint(p.parent) pprint(p.name) pprint(p.suffix) pprint(p.stem) pprint(p.is_absolute()) pprint(p.joinpath('foo', 'bar', 'baz')) pprint(p.match('piyo.*')) # 具象パスを扱う(Path)-------------------------------------------------------- from pathlib import Path p = Path.cwd() / 'newfile.txt' pprint(p) pprint(p.exists()) f = p.open('w+') pprint(p.exists()) pprint(p.resolve()) # ディレクトリ探索-------------------------------------------------------- from pathlib import Path
def ignores(self, path): ''' Test if this module ignores the file at "path" ''' test_path = PurePath(path) for exp in self.ignore_patterns: if test_path.match(exp): logger.debug('"%s" ignored' % path) return True return False