def get_refesh_list(wordlist, cat_name): try: client = dbu.get_client() db = client[DC.DB_NAME] list_coll = db[DC.LISTS_COLL] list = set() search_query = {} inner_search_query = {} inner_search_query[JC.IN] = wordlist search_query['list.' + cat_name + '.name'] = inner_search_query fetch_query = {} fetch_query[JC.NAME] = 1 fetch_query[JC.ID] = 0 results = list_coll.find(search_query, fetch_query) for result in results: list.add(result[JC.NAME]) if list is not None: return list return [] except Exception as exception: traceback.print_exc() return False finally: client.close()
def add_words_to_list(list_name, word_list): try: in_valid_word_list = [] for word in word_list: is_valid = su.search(word) if not is_valid: in_valid_word_list.append(word) word_list.remove(word) if len(in_valid_word_list) > 0: print('Unable to find following words: {0}'.format( in_valid_word_list)) client = dbu.get_client() db = client[DC.DB_NAME] list_coll = db[DC.LISTS_COLL] list_update_query = {} list_update_query[JC.NAME] = list_name set_query = {} set_query[JC.PUSH] = {JC.LIST: {JC.EACH: word_list}} list_coll.update_one(list_update_query, set_query) except: traceback.print_exc() finally: client.close()
def export_list(listname): try: word_list = wu.get_list(listname) # for word in self.list: # is_valid = su.search(word) # if not is_valid: # in_valid_word_list.append(word) # self.list.remove(word) # # if len(in_valid_word_list) > 0: # print('Unable to find following words: {0}'.format(in_valid_word_list)) client = dbu.get_client() db = client[DC.DB_NAME] words_coll = db[DC.WORDS_COLL] list_search_query = {} list_search_query[JC.ID] = {JC.IN: word_list} result = words_coll.find(list_search_query) for words in result: print(words['shortdef']) except: traceback.print_exc() finally: client.close()
def delete_category(category_name): try: client = dbu.get_client() db = client[DC.DB_NAME] category_coll = db[DC.CATEGORY_COLL] # words_coll = db[DC.WORDS_COLL] # # word_update_query = {} # word_update_query[JC.CATEGORY] = category_name # # new_values = {} # new_values[JC.CATEGORY] = "" # # set_query = {} # set_query[JC.SET] = new_values # words_coll.update_one(word_update_query,set_query) delete_query = {} delete_query[JC.NAME] = category_name cat = get_category(category_name) catlist = cat[JC.LIST] category_coll.delete_one(delete_query) listnames = wlu.get_refesh_list(catlist, category_name) for listname in listnames: wlu.refesh(listname) except: traceback.print_exc() finally: client.close()
def create(name, list): try: in_valid_word_list = [] for word in list: is_valid = su.search(word) if not is_valid: in_valid_word_list.append(word) list.remove(word) if len(in_valid_word_list) > 0: print('Unable to find following words: {0}'.format( in_valid_word_list)) client = dbu.get_client() db = client[DC.DB_NAME] list_coll = db[DC.LISTS_COLL] list_create_query = {} list_create_query[JC.NAME] = name list_create_query[JC.LIST] = get_compl_list(list) list_coll.insert_one(list_create_query) return True except: traceback.print_exc() return False finally: client.close()
def get_word_from_mw(wordname): print('Fetching word {0} from MW db.'.format(wordname)) search_url = get_url(wordname) resp = requests.get(search_url) resp_data = resp.json() if len(resp_data) is not 0: if JC.META not in resp_data[0]: return None else: return None word = resp.json()[0] word[JC.ID] = wordname try: client = dbu.get_client() db = client[DC.DB_NAME] words_schema = db[DC.WORDS_COLL] words_schema.insert_one(word) return word except Exception as exception: traceback.print_exc() return None finally: client.close()
def get_selected_word_from_db(wordname,filter=""): if filter == None or len(filter) == 0: return get_word_from_db(wordname) try: client = dbu.get_client() db = client[DC.DB_NAME] words_schema = db[DC.WORDS_COLL] search_data = {} search_data[JC.STEMS] = wordname filter_query={} filter_query[JC.ID] = 0 for field in filter: filter_query[field]=1 if words_schema.count_documents(search_data) == 0: return None result = words_schema.find(search_data,filter_query) return result[0] except Exception as exception: traceback.print_exc() return None finally: client.close()
def update_category(category_name, new_category_name, new_word_list): if not is_category_present(category_name): print("No category with name '{0}' is present in DB.".format( category_name)) return False if category_name != new_category_name and is_category_present( new_category_name): print("Category with name '{0}' is already present in DB.".format( new_category_name)) return False try: in_valid_word_list = [] for word in new_word_list: is_valid = su.search(word) if not is_valid: in_valid_word_list.append(word) new_word_list.remove(word) if len(in_valid_word_list) > 0: print('Unable to find following words: {0}'.format( in_valid_word_list)) client = dbu.get_client() db = client[DC.DB_NAME] category_coll = db[DC.CATEGORY_COLL] category_update_query = {} category_update_query[JC.NAME] = category_name new_data = {} # new_data[JC.PUSH] = {JC.LIST: {JC.EACH: word_list}} new_data[JC.NAME] = new_category_name new_data[JC.LIST] = new_word_list set_query = {} set_query[JC.SET] = new_data check_query = {} check_query[JC.UPSERT] = 'True' category_coll.update_one(category_update_query, set_query) listnames = wlu.get_refesh_list(new_word_list, category_name) for listname in listnames: wlu.refesh(listname) return True except: traceback.print_exc() return False finally: client.close()
def delete_list(list_name): try: client = dbu.get_client() db = client[DC.DB_NAME] list_coll = db[DC.LISTS_COLL] delete_query = {} delete_query[JC.NAME] = list_name list_coll.delete_one(delete_query) except: traceback.print_exc() finally: client.close()
def is_word_present(wordname): try: client = dbu.get_client() db = client[DC.DB_NAME] cat_coll = db[DC.CATEGORY_COLL] search_data = {} search_data[JC.LIST] = wordname if cat_coll.count_documents(search_data) == 0: return False return True except Exception as exception: traceback.print_exc() return False finally: client.close()
def remove_words_from_list(list_name, word_list): try: client = dbu.get_client() db = client[DC.DB_NAME] list_coll = db[DC.LISTS_COLL] list_update_query = {} list_update_query[JC.NAME] = list_name set_query = {} set_query[JC.PULL] = {JC.LIST: {JC.IN: word_list}} list_coll.update_one(list_update_query, set_query) except: traceback.print_exc() finally: client.close()
def get_category(wordname): try: client = dbu.get_client() db = client[DC.DB_NAME] cat_coll = db[DC.CATEGORY_COLL] search_data = {} search_data[JC.LIST] = wordname if cat_coll.count_documents(search_data) == 0: return None result = cat_coll.find(search_data) return result[0][JC.NAME] except Exception as exception: traceback.print_exc() return None finally: client.close()
def get_word_from_db(wordname): try: client = dbu.get_client() db = client[DC.DB_NAME] words_schema = db[DC.WORDS_COLL] search_data = {} search_data[JC.STEMS] = wordname if words_schema.count_documents(search_data) == 0: return False, None result = words_schema.find(search_data) return True, result[0] except Exception as exception: traceback.print_exc() return False, None finally: client.close()
def is_category_present(category_name): try: client = dbu.get_client() db = client[DC.DB_NAME] category_coll = db[DC.CATEGORY_COLL] category_search_query = {} category_search_query[JC.NAME] = category_name result = category_coll.count_documents(category_search_query) if result == 0: return False return True except: traceback.print_exc() finally: client.close()
def get_lists(): try: client = dbu.get_client() db = client[DC.DB_NAME] list_coll = db[DC.LISTS_COLL] list = [] result = list_coll.find() if result is not None: for cat in result: list.append(cat[JC.NAME]) return list return [] except Exception as exception: traceback.print_exc() return False finally: client.close()
def is_list_present(list_name): try: client = dbu.get_client() db = client[DC.DB_NAME] list_coll = db[DC.LISTS_COLL] list_search_query = {} list_search_query[JC.NAME] = list_name result = list_coll.count_documents(list_search_query) if result == 0: return False return True except: traceback.print_exc() finally: client.close()
def get_list(listname): try: client = dbu.get_client() db = client[DC.DB_NAME] words_schema = db[DC.LISTS_COLL] search_data = {} search_data[JC.NAME] = listname if words_schema.count_documents(search_data) == 0: return None result = words_schema.find(search_data) print(result[0]) return result[0] except Exception as exception: traceback.print_exc() return None finally: client.close()
def get_category_names(): try: client = dbu.get_client() db = client[DC.DB_NAME] cat_coll = db[DC.CATEGORY_COLL] cat_list = [] result = cat_coll.find() if result is not None: for cat in result: cat_list.append(cat[JC.NAME]) return cat_list return [] except Exception as exception: traceback.print_exc() return False finally: client.close()
def get_category(category_name): try: client = dbu.get_client() db = client[DC.DB_NAME] cat_coll = db[DC.CATEGORY_COLL] search_data = {} filter_data = {} filter_data[JC.ID] = 0 search_data[JC.NAME] = category_name result = cat_coll.find(search_data, filter_data) if result is not None: return result[0] return {} except Exception as exception: traceback.print_exc() return False finally: client.close()
def create(self): result = self.add_word() try: client = dbu.get_client() db = client[DC.DB_NAME] category_coll = db[DC.CATEGORY_COLL] search_query = {} search_query[JC.NAME] = self.name category_data = {} # category_data[JC.ID] = self.name category_data[JC.NAME] = self.name category_data[JC.LIST] = self.list if category_coll.count_documents(search_query) == 0: category_coll.insert_one(category_data) # listnames = wlu.get_refesh_list(self.list) # for listname in listnames: # wlu.refesh(listname) # return True, result else: category_coll.replace_one(search_query, category_data) listnames = wlu.get_refesh_list(self.list, 'None') for listname in listnames: wlu.refesh(listname) return True, result except Exception as exception: traceback.print_exc() return False, result finally: client.close() # cat=Category('testcat2', ['abate','axiom', 'ggg','dictum']) # print(cat.create())
def remove_words_from_category(category_name, word_list): if not is_category_present(category_name): print("No category with name '{0}' is present in DB.".format( category_name)) return False try: client = dbu.get_client() db = client[DC.DB_NAME] category_coll = db[DC.CATEGORY_COLL] category_update_query = {} category_update_query[JC.NAME] = category_name set_query = {} set_query[JC.PULL] = {JC.LIST: {JC.IN: word_list}} category_coll.update_one(category_update_query, set_query) except: traceback.print_exc() finally: client.close()
def update_category_name(from_name, to_name): if not is_category_present(from_name): print( "No category with name '{0}' is present in DB.".format(from_name)) return False try: client = dbu.get_client() db = client[DC.DB_NAME] category_coll = db[DC.CATEGORY_COLL] # words_coll = db[DC.WORDS_COLL] # # word_update_query = {} # word_update_query[JC.CATEGORY] = from_name # # new_values_for_words = {} # new_values_for_words[JC.CATEGORY] = to_name # # set_query = {} # set_query[JC.SET] = new_values_for_words # words_coll.update_many(word_update_query,set_query) category_update_query = {} category_update_query[JC.ID] = from_name new_values_for_category = {} new_values_for_category[JC.ID] = to_name new_values_for_category[JC.NAME] = to_name set_query = {} set_query[JC.SET] = new_values_for_category category_coll.update_one(category_update_query, set_query) except: traceback.print_exc() finally: client.close()
def get_list_names(listname): try: word_list = [] client = dbu.get_client() db = client[DC.DB_NAME] list_coll = db[DC.LISTS_COLL] list_create_query = {} list_create_query[JC.NAME] = listname results = list_coll.find(list_create_query) if results[0] is not None: for cat in results[0][JC.LIST]: for word in results[0][JC.LIST][cat]: word_list.append(word[JC.NAME]) # word_list = results[0][JC.LIST] return word_list except: traceback.print_exc() finally: client.close()
def __init__(): dbu.initialize_db()