def GET(self, profile_key, owner_guid=None): # todo key = {'profile:key': profile_key} t = None if not owner_guid: t = self.dhtf.pull_local(key) else: guid_bin = guid_hex_to_bin(owner_guid) if guid_bin == self.dhtf.ert.rsa_guid_bin: t = self.dhtf.pull_local(key) else: t = self.dhtf.pull_remote(key, guid_bin) if t: d = bson.loads(t[-1]) if 'e' in d: ll = d['e'] if len(ll) >= 1: dd = ll[1] if 'v' in dd: # sanitize vvv = dd['v'] js = json.dumps(vvv) js_cl = bleach.clean(js) js_b = js_cl.encode() return js_b return b'null'
def index_field( self, container_hk_hex, specifier, text_data, prefixes=True, ert_tokens_major=0, ert_tokens_minor=0, q1: int = 0, q2: int = 0, ): try: pk_bin = guid_hex_to_bin(container_hk_hex) self.idx.index_bag_of_spec_text(container_hash=pk_bin, specifier=specifier, text_data=text_data, prefixes=prefixes, ert_tokens_major=ert_tokens_major, ert_tokens_minor=ert_tokens_minor, q1=q1, q2=q2) self.logger('INDEXED', container_hk_hex, specifier) except Exception as e: self.logger('ERR IDX FAILED', specifier, text_data, str(e))
def get2(self, profile_key, owner_guid=None): if len(owner_guid) != 64: return b'null' # key = {'profile:key': profile_key} t = None if not owner_guid: t = self.dhf.pull_local(key) else: guid_bin = cdx.guid_hex_to_bin(owner_guid) if guid_bin == self.dhf.ert.rsa_guid_bin: t = self.dhf.pull_local(key) else: t = self.dhf.pull_remote(key, guid_bin) if t: d = bson.loads(t[-1]) if 'e' in d: ll = d['e'] if len(ll) >= 1: dd = ll[1] if 'v' in dd: # sanitize vvv = dd['v'] return vvv
def index_gig_document(self, hk_hex: str, doc: dict): text = '' if 'title' in doc: text += doc['title'] + ' ' if 'description' in doc: text += doc['description'] if text.strip() != '': # pass self.index_field(guid_hex_to_bin(hk_hex), 'text', text_data=text) if 'tags' in doc: self.index_field(guid_hex_to_bin(hk_hex), 'tags', text_data=' '.join(doc['tags']), prefixes=False) if 'owner_guid' in doc: self.index_field(guid_hex_to_bin(hk_hex), 'owner_guid', text_data=doc['owner_guid'], prefixes=False)
def DELETE(self, q): try: pk_hash_bin = guid_hex_to_bin(q) bin_rs, content_type, content_encoding = self.api.delete( pk_hash=pk_hash_bin) self.cherrypy.response.status = 200 return bin_rs except: self.cherrypy.response.status = 400 traceback.print_exc() return b'null'
def render_profile_key(self, key): for ip4 in self.peers: data = self.peers[ip4] if 'guid' in data: bin_guid = guid_hex_to_bin(data['guid']) key_proto = {'profile:key': key} if 'profile' not in data: data['profile'] = dict() t = self._dhf.pull_remote(key=key_proto, guid=bin_guid) if t: val = decode_val(t) if val: data['profile'][key] = val
def index_gig_document(self, hk_hex: str, doc: dict): text = '' q1 = 0 q2 = 0 if 'price' in doc: q1 = int(doc['price']) if 'title' in doc: text += doc['title'] + ' ' if 'description' in doc: text += doc['description'] if text.strip() != '': # pass self.index_field(guid_hex_to_bin(hk_hex), 'text', text_data=text, q1=q1, q2=q2) if 'tags' in doc: self.index_field(guid_hex_to_bin(hk_hex), 'tags', text_data=' '.join(doc['tags']), prefixes=False, q1=q1, q2=q2) if 'owner_guid' in doc: self.index_field(guid_hex_to_bin(hk_hex), 'owner_guid', text_data=doc['owner_guid'], prefixes=False, q1=q1, q2=q2) if 'category' in doc: self.index_field(guid_hex_to_bin(hk_hex), 'category', text_data=doc['category'], prefixes=False, q1=q1, q2=q2)
def GET(self, q: str): try: pk_hash_bin = guid_hex_to_bin(q) bin_rs, content_type, content_encoding = self.api.query( pk_hash=pk_hash_bin) self.cherrypy.response.status = 200 if bin_rs: return bin_rs else: self.cherrypy.response.status = 404 return b'null' except: self.cherrypy.response.status = 400 traceback.print_exc() return b'null'
def get(self, profile_key, guid_hex=None, local=False): if not guid_hex: guid_hex = self.own_guid_hex guid_bin = cdx.guid_hex_to_bin(guid_hex) key_proto = {'profile:key': profile_key} t = None if local: t = self.dhf.pull_local(key_proto, guid=guid_bin) if not t: t = self.dhf.pull_remote(key_proto, guid=guid_bin) else: t = self.dhf.pull_remote(key_proto, guid=guid_bin) if t: data = cdx.decode_profile_key_val(t) return data
def read(self, pk_hex_hash: str): pk_hash = guid_hex_to_bin(pk_hex_hash) print(pk_hex_hash, pk_hash) c = self.data_store.read_resource(pk_hash) if c: t = c.fetchone() if t: pk_hash, owner_hash, resource_hash, resource_own_sig, \ content_type_bin, content_encoding_bin, resource_data_bin = t res = SignedBinResource() res.pk_bin = pk_hash res.owner_bin = owner_hash res.content_type = content_type_bin.decode() res.content_encoding = content_encoding_bin.decode() res.data_bin = resource_data_bin res.orig_res_hash = resource_hash return res
def index(self, body: bytes): body_str = body.decode('utf-8') d = json.loads(body_str) if FIELD_NAME not in d: if CONTAINER_HASH_KEY not in d: if FIELD_TEXT not in d: raise Exception( 'Please supply an JSON object with all fields set %s %s %s' % ALL_FIELDS) ctx_bin_hash = guid_hex_to_bin(d[CONTAINER_HASH_KEY]) specifier = d[FIELD_NAME] text_data = d[FIELD_TEXT] self.idx_engine.index_bag_of_spec_text(container_hash=ctx_bin_hash, specifier=specifier, text_data=text_data)
def DELETE(self, q): try: pk_hash_bin = guid_hex_to_bin(q) bin_rs, content_type, content_encoding = self.api.delete( pk_hash=pk_hash_bin) if bin_rs: self.cherrypy.response.headers['Content-Type'] = content_type self.cherrypy.response.headers[ 'Content-Encoding'] = content_encoding self.cherrypy.response.status = 200 return bin_rs else: self.cherrypy.response.status = 400 return b'null' except: self.cherrypy.response.status = 400 traceback.print_exc() return b'null'
def bin(self): return guid_hex_to_bin(self.str_guid_hex)
def unindex(self, hk_hex): try: pk_bin = guid_hex_to_bin(hk_hex) self.idx.unindex(pk_bin) except Exception as e: self.logger('UNINDEX FAILED', e)
def unindex(self, hk_hex): try: pk_bin = guid_hex_to_bin(hk_hex) self.idx.unindex(pk_bin) except: print('UNINDEX FAILED')
def unindex_on(self, hk_hex): try: self.unindex(guid_hex_to_bin(hk_hex)) except: print('UNINDEX FAILED')
def __call__(self, *args, **kwargs): return guid_hex_to_bin(self.str_guid_hex)
def unindex(self, container_hash_hex: str): ctx_bin_hash = guid_hex_to_bin(container_hash_hex) self.idx_engine.unindex(ctx_bin_hash) pass