def __save_catalog__(self, sub_catalog, namespace=None): """ @param::sub_catalog: the catelog object contains feature metadata @param::namespace: the string namespace of catalog return None """ # check edge case if sub_catalog == None: raise Exception('> save_catalog: (sub)catalog can not be None!') if not isinstance(sub_catalog, dict): raise Exception( '> save_catalog: (sub)catalog is not dictionary type!') # save (sub)catalog into flat file catalog = {} namespace = self.__build_canonical_namespace__(namespace) if file_exist(self.path, self.filename): bytes_obj = read_binary_file(self.path, self.filename) catalog = pl.loads(bytes_obj) if len(sub_catalog) == 0: # delete the namespace associate with empty sub catalog. del catalog[namespace] else: catalog[namespace] = sub_catalog else: if len(sub_catalog) > 0: catalog[namespace] = sub_catalog dumps = pl.dumps(catalog) write_binary_file(self.path, self.filename, dumps)
def namespaces(self, **kwargs): """ @param::kwargs: keyword parameter list return list of canonical namespace objects and the counts of features in that namespace """ catalog = {} if file_exist(self.path, self.filename): # read and deserialize catalog object bytes_obj = read_binary_file(self.path, self.filename) catalog = pl.loads(bytes_obj) return catalog.keys(), list(map(len, catalog.values()))
def __get_catalog__(self, namespace=None): """ @param::namespace: the string namespace of catalog return the catalog object or None """ catalog_rtn = {} namespace = self.__build_canonical_namespace__(namespace) if file_exist(self.path, self.filename): # read and deserialize catalog object bytes_obj = read_binary_file(self.path, self.filename) catalog = pl.loads(bytes_obj) if namespace in catalog: catalog_rtn = catalog[namespace] return catalog_rtn
def read(self, namespace='default', match_child=True, **kwargs): """ @param::namespace: the namespace in string @param::match_child: boolean value indicate whether match sub namespaces @param::kwargs: the keyword parameter list return a dictionary of {uid:feature meta data} """ feature_dict = {} if file_exist(self.path, self.filename): bytes_obj = read_binary_file(self.path, self.filename) catalog = pl.loads(bytes_obj) canonical_ns = self.__build_canonical_namespace__(namespace) if match_child: for key_ns in catalog: if self.__namespace_match__(canonical_ns, key_ns): feature_dict.update(catalog[key_ns]) else: if canonical_ns in catalog: feature_dict = catalog[canonical_ns] return feature_dict
def __check_feature_name_uniqueness__(self, feature): """ @param::feature: the feature metadata object. return boolean value of uniqueness of feature name in given namespace. """ if file_exist(self.path, self.filename): # read and deserialize catalog object bytes_obj = read_binary_file(self.path, self.filename) catalog = pl.loads(bytes_obj) # check uniquess feature_name = feature.name namespace = feature.namespace uid = feature.uid canonical_ns = self.__build_canonical_namespace__(namespace) if canonical_ns in catalog: for _, feature in catalog[canonical_ns].items(): if feature.name.lower() == feature_name.lower( ) and feature.uid != uid: return False return True
def read(self, uid, **kwargs): filename = '%s.dill' % (uid) if file_exist(self.path, filename): content = read_binary_file(self.path, filename) return content return None