def __init__(self, document): if isinstance(document, dict): self._document = document else: self._document = json.loads(document) def list_helper(ls): out = [] for item in ls: if isinstance(item, dict): out.append(JsonDocument(item)) elif isinstance(item, list): out.append(list_helper(item)) else: out.append(item) return out for key, value in self._document.items(): key = make_identifier(key) if isinstance(value, list): list_values = list_helper(value) setattr(self, key, list_values) elif isinstance(value, dict): sub_document = JsonDocument(value) setattr(self, key, sub_document) else: setattr(self, key, value)
def __init__(self, field_name, mapping, parent=None, search_model=None): self._is_parent = const.PROPERTIES in mapping self._is_multi_field = (const.PROPERTY_TYPE in mapping and mapping[const.PROPERTY_TYPE] == const.MAPPING_MULTI_FIELD) self._search_model = search_model self._parent = parent self._mapping = mapping self._field_name = make_identifier(field_name) self._mapping_name = field_name
def __init__(self, id=None, hash=None, url=None): if id: self.id = id self.url = self.read_from('url_by_id', self.id) elif hash: self.hash = hash self.id = self.read_from('id_by_hash', self.hash) self.url = self.read_from('url_by_id', self.id) elif url: self.url = url if urlparse(url).scheme else 'http://' + url try: self.id = self.read_from('id_by_hash', self.hash) except: while True: # Keep doing this until we find an unoccupied slot... self.id = make_identifier() if not os.path.exists(path_for('u', 'url_by_id', self.id + '.txt')): break else: raise Exception("Can't create an URL without an ID, a Hash or an URL")
def _has_field(cls, field_name): field_name = make_identifier(field_name) return hasattr(cls, field_name)
def __getattr__(self, key): key = make_identifier(key) val = self.__dict__.get(key) return val