def create_patch(self): """ Creates a patch without changing anything on the state of the current repository :return: patch, a name for the patch and it's hash """ # orig_branch = self.repo.active_branch.name # # # push untracked changes to the stash) # files = list( # set([item.a_path for item in self.repo.index.diff('HEAD')]) | # set([item.a_path for item in self.repo.index.diff(None)]) | # set(self.repo.untracked_files)) # # files = [item.a_path for item in untracked_files] # try: # for f in files: # self.repo.git.add(f) # self.repo.git.stash('push', '--keep-index') # # # generate the diff patch # patch = self.repo.git.stash('show', '-p') # diff_hash = persistent_hash(patch) # finally: # # Remove temporary tracked files # for f in files: # self.repo.git.reset(f) patch = self.repo.git.diff('HEAD') diff_hash = persistent_hash(patch) return patch, diff_hash
def get_library_descriptor(obj) -> LibraryModel: """ Try to extract the defining package of this class. :return: """ # TODO extract reference to self package try: name = obj.__module__.split(".")[0] from pypads.utils.util import find_package_version version = find_package_version(name) return LibraryModel(name=name, version=version, uid=persistent_hash((name, version)), extracted=True) except Exception: return LibraryModel(name="__unkown__", version="0.0", uid=persistent_hash(("__unkown__", "0.0")), extracted=True)
def __init__(self, path, name=None): with open(path, encoding='utf-8') as f: if name is None: name = os.path.basename(f.name) data = f.read() self.path = path super().__init__(name, data) # computing the hash of the mapping file self._hash = persistent_hash(data) self.uid = self._hash
def store_hash(*args): """ Store a hash of an ontology obj :param args: Arguments to convert to a id hash :return: True if not existing else False """ obj_hash = persistent_hash(tuple(args)) if obj_hash not in storage_hash_set: storage_hash_set.add(obj_hash) return True return False
def store_lib(self): from pypads.app.pypads import get_current_pads lib_repo = get_current_pads().library_repository # TODO get hash uid for logger lib_hash = persistent_hash( (self._defined_in.name, self._defined_in.version)) if not lib_repo.has_object(uid=lib_hash): lib_obj = lib_repo.get_object(uid=lib_hash) lib_obj.log_json(self._defined_in) else: lib_obj = lib_repo.get_object(uid=lib_hash) self.defined_in = lib_obj.get_reference()
def __init__(self, key, version, library, author=None, **kwargs): """ Object holding a set of mappings related to a library :param key: Name / key of the collection :param version: Version of the collection :param library: Library information including library version constraint and name """ self._mappings = {} self._name = key self._author = author self._version = version self._lib = LibSelector.from_dict(library) self._hash = persistent_hash( (self._name, self._version, hash(self._lib))) self.uid = self._hash super().__init__()
def store_schema(cls): if not cls._schema_reference: from pypads.app.pypads import get_current_pads pads = get_current_pads() schema_repo = pads.schema_repository schema = cls.schema() schema_hash = persistent_hash(str(schema)) if not schema_repo.has_object(uid=schema_hash): schema_obj = schema_repo.get_object(uid=schema_hash) # TODO store schema as string for now because $ is reserved in MongoDB schema_wrapper = { "category": "LoggerOutputSchema", "schema": str(jsonable_encoder(schema)) } schema_obj.log_json(schema_wrapper) else: schema_obj = schema_repo.get_object(uid=schema_hash) cls._schema_reference = schema_obj.get_reference() return cls._schema_reference
def get_default_ctx_path(): """ Function to persist the default context and get it's location. :return: """ try: global default_ctx_path from pypads.app.pypads import get_current_pads pads = get_current_pads() if not default_ctx_path: obj = pads.schema_repository.get_object( uid=persistent_hash(str(DEFAULT_CONTEXT))) default_ctx_path = obj.log_mem_artifact( "pypads_context_default", DEFAULT_CONTEXT, write_format=FileFormats.json, write_meta=False) obj.set_tag("pypads.schema_name", "pypads_context_default") return os.path.join(default_ctx_path) except Exception as e: # Return context itself instead return DEFAULT_CONTEXT['@context']
def default_ts_modified(cls, v, *, values, **kwargs): return v or str(persistent_hash( (values["backend_uri"], values["uid"])))
def __hash__(self): return persistent_hash((self.backend_uri, self.uid))
def __post__(self, ctx, *args, _pypads_env: InjectionLoggerEnv, _logger_call: InjectionLoggerCallModel, _logger_output, _pypads_result, **kwargs): """ This function is used to extract estimator information from the code and the related mapping file. This is run after the hooked function is executed. Pypads injects a set of default parameters. :param ctx: A reference to the context on which the original function was called :param args: Args given to the original function :param _pypads_env: A logging environment object storing information about the used mappings, original_call etc. :param _logger_call: A information object storing additonal information about the logger call itself :param _logger_output: A prepared result object of the class defined in output_schema_class(cls) :param _pypads_result: The return value of the __pre__ function :param kwargs: Kwargs given to the original function :return: """ # Get data from mapping file mapping_data = _pypads_env.data estimator_data = data_str(mapping_data, "estimator", "@schema", default={}) # Create repository object ero = EstimatorRepositoryObject( name=data_str(estimator_data, "rdfs:label", default=ctx.__class__.__name__, warning=f"No name given for {ctx.__class__}. " f"Extracting name from class."), description=data_str(estimator_data, "rdfs:description", default="Some unknown estimator."), documentation=data_str( estimator_data, "padre:documentation", default=ctx.__class__.__doc__, warning= f"No documentation defined on the mapping file for {ctx.__class__}. " f"Taking code documentation instead."), parameter_schema=data_path( estimator_data, "padre:parameters", default="unkown", warning= f"No parameters are defined on the mapping file for {ctx.__class__}. " f"Logging estimator without parameters."), location=_logger_call.original_call.call_id.context.reference, additional_data=estimator_data) # Compile identifying hash hash_id = persistent_hash(ero.json()) # Add to repo if needed if not _pypads_env.pypads.estimator_repository.has_object(uid=hash_id): repo_obj = _pypads_env.pypads.estimator_repository.get_object( uid=hash_id) repo_obj.log_json(ero) # Create referencing object eto = EstimatorTO( repository_reference=hash_id, repository_type=_pypads_env.pypads.estimator_repository.name, parent=_logger_output, additional_data=mapping_data) # Store object _logger_output.estimator = eto.store()
def _persistent_hash(cls): # TODO include package? version? git hash? content with inspect? Something else? return persistent_hash(inspect.getsource(cls))