Ejemplo n.º 1
0
 def sout(self):
     rc = self.rc
     list_search = []
     collection = self.gtx["contacts"]
     if rc.name:
         list_search.extend(["name", rc.name])
     if rc.inst:
         list_search.extend(["institution", rc.inst])
     if rc.notes:
         list_search.extend(["notes", rc.notes])
     if rc.filter:
         list_search.extend(rc.filter)
     filtered_contacts_id = (search_collection(collection, list_search)).strip('    \n')
     filtered_contacts_id = list(filtered_contacts_id.split('    \n'))
     if rc.date:
         date_list = []
         temp_dat = date_parser.parse(rc.date).date()
         temp_dict = {"begin_date": (temp_dat - dateutil.relativedelta.relativedelta(
                                     months=int(rc.range))).isoformat(),
                      "end_date": (temp_dat + dateutil.relativedelta.relativedelta(
                                  months=int(rc.range))).isoformat()}
         for contact in collection:
             curr_d = get_dates(contact)['date']
             if is_current(temp_dict, now=curr_d):
                 date_list.append(contact.get('_id'))
             filtered_contacts_id = [value for value in filtered_contacts_id if value in date_list]
     filtered_contacts = []
     string_contacts = ''
     for contact in collection:
         if contact.get('_id') in filtered_contacts_id:
             filtered_contacts.append(contact)
             institution = contact.get('institution')
             institution_name = fuzzy_retrieval(self.gtx['institutions'],
                                                ['name', '_id', 'aka'], institution)
             if institution_name:
                 contact['institution'] = institution_name.get('name')
             if rc.verbose:
                 contact_str = f"{contact.get('name')}\n"
                 for k in ['_id', 'email', 'institution', 'department', 'notes', 'aka']:
                     if contact.get(k):
                         if isinstance(contact.get(k), list):
                             lst_expanded = '\n        -'.join(map(str, contact.get(k)))
                             contact_str += f"    {k}:\n        -{lst_expanded}\n"
                         else:
                             contact_str += f"    {k}: {contact.get(k)}\n"
                 string_contacts += contact_str
             else:
                 string_contacts += f"{contact.get('name')}  |  {contact.get('_id')}  |" \
                                    f"  institution: {contact.get('institution')}  |" \
                                    f"  email: {contact.get('email', 'missing')}\n"
     print(string_contacts.strip('\n'))
     return
Ejemplo n.º 2
0
 def sout(self):
     rc = self.rc
     coll = self.gtx[rc.coll]
     keys = sorted(set([i for k in coll for i in k.keys()]))
     list_keys = []
     if rc.kv_filter:
         list_keys.extend([
             rc.kv_filter[n] for n in range(len(rc.kv_filter)) if n % 2 == 0
         ])
     if rc.return_fields:
         list_keys.extend(rc.return_fields)
     for i in list_keys:
         if i not in keys:
             raise ValueError(
                 f"{i} is not a valid key. Please choose a valid key from: {keys}"
             )
     if len(coll) == 0:
         raise RuntimeError(
             'This collection is empty or does not exist. Please inform a valid collection.'
         )
     if rc.kv_filter:
         filter_result = search_collection(coll, rc.kv_filter).strip()
         if filter_result == '':
             print("There are no results that match your search.")
         else:
             if rc.return_fields:
                 print(
                     "Results of your search:\n"
                     f"{(search_collection(coll, rc.kv_filter, rc.return_fields).strip())}"
                 )
             else:
                 print(f"Results of your search:\n{filter_result}")
     if rc.return_fields and not rc.kv_filter:
         print(
             f"Results of your search:\n{collection_str(coll, rc.return_fields).strip()}"
         )
     if rc.keys:
         print(f"Available keys:\n{keys}")
         return
     if not rc.kv_filter and not rc.return_fields:
         print(f"Results of your search:\n{collection_str(coll).strip()}")
     return
Ejemplo n.º 3
0
def test_search_collection(input, expected):
    assert (search_collection(input[0], input[1], input[2]) == expected)