def from_json(self, json: Map) -> Maybe[Project]: ''' Try to instantiate a Project from the given json object. Convert the **type** key to **tpe** and its value to Maybe. Make sure **root** is a directory, fall back to resolution by **tpe/name**. Reinsert the root dir into the json dict, filter out all keys that aren't contained in Project's fields. Try to instantiate. ''' root = json.get('root')\ .map(mkpath)\ .or_else( json.get_all('type', 'name') .flat_smap(self.resolver.type_name)) valid_fields = root\ .map(lambda a: json ** Map(root=a, tpe=json.get('type')))\ .map(lambda a: a.at(*Project._pclass_fields)) return Maybe.from_call(lambda: valid_fields.ssmap(Project)) | Empty()
def main_dir(self) -> Maybe[Path]: return Maybe.from_call(Path.cwd, exc=IOError)
def setup(self): self.cwd = Maybe.from_call(Path.cwd, exc=IOError) super().setup()
def sub_path(base: Path, path: Path): check = lambda: path.relative_to(str(base)) return Maybe.from_call(check, exc=ValueError)\ .map(_.parts)\ .map(List.wrap)
def ref(self, name): return Maybe.from_call(lambda: self.repo.refs[name.encode()], exc=KeyError)