def find_containing_dir(file_name, top_level_dir): found = False try: debug_print(5, "top_level_dir : ", top_level_dir) # use os.walk to identify the directory associated with the # file and then return that as the root dir # NOTE that the loop quits on first match. for root, dirs, files in os.walk(top_level_dir): if file_name in files: root_dir = root found = True break if found == False: print "Could not find the file <%s> in top level dir <%s>" % ( file_name, top_level_dir) # we return global_args.base_dir here because the call will serve # using static_file, which takes care of return code. return global_args.base_dir else: return root_dir except: print_exception(True)
def find_containing_dir(file_name, top_level_dir): found = False try: debug_print(5, "top_level_dir : ", top_level_dir) # use os.walk to identify the directory associated with the # file and then return that as the root dir # NOTE that the loop quits on first match. for root, dirs, files in os.walk(top_level_dir): if file_name in files: root_dir = root found = True break if found == False: print "Could not find the file <%s> in top level dir <%s>" %(file_name, top_level_dir) # we return global_args.base_dir here because the call will serve # using static_file, which takes care of return code. return global_args.base_dir else: return root_dir except: print_exception(True)
def handle_knowledge_search(self, return_mode): try: search_term = self.req_params['d_keyword'] # do the mongo_search here # for now, limit the items to 10 entries item_limit = 10 results = mongo_search(search_term, item_limit) if return_mode == 'HTML': # search results are in a list, with each # entry being a dictionary. tbg = TwitterBootstrap_HTMLGenerator() # use our bootstrap adapter to generate HTML # but also wrap it in a "jumbotron" class html = '<div class="jumbotron" id="search-result-jumbotron">' for item in results: # Let us make a panel_footer before calling to generate # the entire panel code. footer = '<b> Date: </b>' + item['d_date'] + '   ' footer += '<b> Place: </b>' + item['d_place'] html += tbg.generate_panel(n_columns=9, panel_color='light-blue', panel_body=item['d_knowledge'], panel_title=item['d_title'], panel_footer=footer) # in this case, we will add the date and place by ourselves. # hence panel_footer was passed as None. # of the 9 columns, have Date in 1-4 and Place in 6-9 (leave 5 empty) # 'Date: ' + item['d_date'] + ' Place: ' + item['d_place']) html += '</div>' debug_print(5,"HTML Code:", html) return html else: json_array = [] # return type now is JSON. for item in results: print type(item), item json_array.append(item) return_dict = item # return_dict['data'] = json.dump(item) return return_dict except: print_exception(True)
def mongo_init(): try: # get the db and the collection know_db, know_col = _get_db_col() # make sure there is an index know_col.ensure_index([(MONGO_KNOWLEDGE_FIELD, 'text')], name=MONGO_TEXT_INDEX_NAME) print color.red("Ensured MongoDB Indices.") except: print_exception(True)
def run(self, handler): try: from wsgiref.simple_server import make_server, WSGIRequestHandler import ssl if self.quiet: class QuietHandler(WSGIRequestHandler): def log_request(*args, **kw): pass self.options['handler_class'] = QuietHandler srv = make_server(self.host, self.port, handler, **self.options) srv.socket = ssl.wrap_socket ( srv.socket, certfile='server.pem', # path to certificate server_side=True) srv.serve_forever() except: print_exception(True)
def handle_knowledge_save(self): # Make a UUID using the following INPUTs # # Make a uuid_seed_string = str(date) + str(series) # Use the uuid_seed_string with uuid.NAMESPACE_DNS + string (see uuid5()) # try: # create a UUID and store it into the dictionary uuid_seed_string = self.req_params['d_series'] + self.req_params['d_date'] + self.req_params['d_sheetno'] cur_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, uuid_seed_string) debug_print(5, "cur_uuid: ", cur_uuid, uuid.NAMESPACE_DNS) self.req_params['d_uuid'] = str(cur_uuid) if global_args.debug_level >= 5: print_table_from_dict([ 'POST param', 'Value'], self.req_params) # make a file name # The filename is "SERIES_<series_string>_DATE_<date_string>_SEQ_<sheet_no>.json" file_name = '' file_name += 'SERIES_' file_name += self.req_params['d_series'].replace(' ','_') file_name += '_DATE_' file_name += self.req_params['d_date'].replace('/','_').replace('-','_') file_name += '_SEQ_' file_name += self.req_params['d_sheetno'] file_name += ".json" debug_print(5, "JSON file_name: ", file_name) # now write to a file full_file_name = global_args.base_dir + 'data/files/' + file_name with open(full_file_name, 'w') as outfile: json.dump(self.req_params, outfile, sort_keys = True, indent = 4, ensure_ascii=False) return "<p> HEE HEE: Your login information was correct.</p>" except: print_exception(True)
def mongo_search(search_term, return_limit): result = [] try: # get the db and the collection know_db, know_col = _get_db_col() # make sure there is an index # know_col.ensure_index([('tdata' , 'text')], name='knowledge_text_index') s_term = {"$text": {"$search": search_term}} debug_print(5, "search_term:", s_term) x = know_col.find(s_term).limit(return_limit) for item in x: debug_print(6, "Item: ", item) result.append(item) return result except: print_exception(True)
def _get_db_col(): try: # get a client connection mongo_client = MongoClient(global_args.mongo_host, global_args.mongo_port) debug_print(5, "MongoClient:", mongo_client) # our knowledge_text database (has a fixed name) know_db_name = MONGO_DB_NAME know_db = mongo_client[know_db_name] # get the collection that has ALL the Series sheets know_col_name = MONGO_COL_NAME know_col = know_db[know_col_name] debug_print(5, "db,col: ", know_db, know_col) return know_db, know_col except: print_exception(True)
def mongo_search(search_term, return_limit): result = [] try: # get the db and the collection know_db, know_col = _get_db_col() # make sure there is an index # know_col.ensure_index([('tdata' , 'text')], name='knowledge_text_index') s_term = { "$text" : { "$search" : search_term } } debug_print(5,"search_term:", s_term) x = know_col.find(s_term).limit(return_limit) for item in x : debug_print(6, "Item: ", item) result.append(item) return result except: print_exception(True)
def run(self, handler): try: from wsgiref.simple_server import make_server, WSGIRequestHandler import ssl if self.quiet: class QuietHandler(WSGIRequestHandler): def log_request(*args, **kw): pass self.options['handler_class'] = QuietHandler srv = make_server(self.host, self.port, handler, **self.options) srv.socket = ssl.wrap_socket( srv.socket, certfile='server.pem', # path to certificate server_side=True) srv.serve_forever() except: print_exception(True)