def _stringify_dictionary_values(self, data_dict): if not data_dict: raise EmptyParameterException() return [ str(value) if type(value) is not str else value for value in list(data_dict.values()) ]
def _not_expired(self, item): if not item: raise EmptyParameterException() if item.get("expiry_time") == 0: return True else: return get_unix_millis(current_utctime()) < item.get("expiry_time")
def _filter_and(self, key, filter_list): if not key or not filter_list: raise EmptyParameterException() if not self._resource_is_available(key): raise ResourceNotFoundException() if not self._filters_are_valid(filter_list, key): raise InvalidFilterFormatException() is_start = True result = [] for filter_item in filter_list: if is_start: filter_result = filter( lambda item: item.get(filter_item.get("field")) == filter_item.get("value"), self.get_resource(key)) filter_result = list(filter_result) [result.append(item) for item in filter_result] is_start = False else: if len(result) == 0: return [] filter_result = filter( lambda item: item.get(filter_item.get("field")) == filter_item.get("value"), result) result = list(filter_result) return result
def _field_existed(self, key, field): if not key or not field: raise EmptyParameterException() if not self._resource_is_available(key): raise ResourceNotFoundException() result = filter(lambda item: fnmatch.filter(item.keys(), field), self.get_resource(key)) return len(list(result)) > 0
def _member_id_existed(self, key, id_value): if not key and not id_value: raise EmptyParameterException() if not self._resource_is_available(key): raise ResourceNotFoundException() result = filter(lambda item: item.get("id") == id_value, self.get_resource(key)) return len(list(result)) > 0
def _member_key_is_available(self, key, field): if not key and not field: raise EmptyParameterException() if not self._resource_is_available(key): raise ResourceNotFoundException() result = filter(lambda item: fnmatch.filter(item.keys(), field), self._memory_data.get(key)) return len(list(result)) > 0
def _fetch_matching_resource(self, key): if not key: raise EmptyParameterException() matched_keys = fnmatch.filter(self._memory_data.keys(), key) data = [] for matched_key in matched_keys: [data.append(item) for item in self._memory_data.get(matched_key)] return data
def delete_resource(self, key): if not key: raise EmptyParameterException() if not self._resource_is_available(key): raise ResourceNotFoundException() self._memory_data.pop(key, None) self._persist_data() return "Resource deleted"
def _fetch_matching_member_by_value(self, key, value): if not key and not value: raise EmptyParameterException() if not self._resource_is_available(key): raise ResourceNotFoundException() result = filter( lambda item: fnmatch.filter( self._stringify_dictionary_values(item), value), self.get_resource(key)) return list(result)
def put_member(self, key, member_value, expiry=0): if not key or not member_value: raise EmptyParameterException() if "id" not in member_value.keys(): raise IdNotFoundException() if not self._resource_is_available(key): self.create_resource(key) if self._member_id_existed(key, member_value.get("id")): return self._update_member(key, member_value, expiry) else: return self._create_member(key, member_value, expiry)
def delete_member(self, key, id_value): if not key or not id_value: raise EmptyParameterException() if not self._resource_is_available(key): raise ResourceNotFoundException() for idx, member in enumerate(self._memory_data.get(key)): if member.get("id") == id_value: del self._memory_data[key][idx] self._persist_data() return "Delete successful" return "Member not found"
def _fetch_matching_member_by_field(self, key, value, field="id"): if not key or not value: raise EmptyParameterException() if not self._resource_is_available(key): raise ResourceNotFoundException() if not self._field_existed(key, field): raise FieldNotFoundException() result = filter( lambda item: fnmatch.fnmatch(item.get(field), value) and self. _not_expired(item), self.get_resource(key)) return list(result)
def _filter_or(self, key, filter_list): if not key or not filter_list: raise EmptyParameterException() if not self._resource_is_available(key): raise ResourceNotFoundException() if not self._filters_are_valid(filter_list, key): raise InvalidFilterFormatException() result = [] for filter_item in filter_list: filter_result = filter( lambda item: item.get(filter_item.get("field")) == filter_item. get("value"), self.get_resource(key)) [ result.append(item) for item in filter_result if item not in result ] return result
def _update_member(self, key, member_value, expiry=0): if not key or not member_value: raise EmptyParameterException() if not self._resource_is_available(key): raise ResourceNotFoundException() current_member = self.get_member(key, member_value.get("id")) member_value["created_at"] = current_member.get("created_at") creation_time = get_timestamp_from_millis( current_member.get("created_at")) member_value["expiry_time"] = get_expiry_time( expiry, creation_time) if expiry > 0 else 0 for idx, member in enumerate(self._memory_data.get(key)): if member.get("id") == member_value.get("id"): for field_key in member_value.keys(): member[field_key] = member_value[field_key] self._memory_data[key][idx] = member self._persist_data() return self._memory_data[key][idx]
def create_resource(self, key): if not key: raise EmptyParameterException() self._memory_data[key] = [] self._persist_data()
def _resource_is_available(self, key): if not key: raise EmptyParameterException() return key in self._memory_data.keys()
def put_value(self, key, value): if not key or not value: raise EmptyParameterException() self._memory_data[key] = value self._persist_data() return value