示例#1
0
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)
示例#2
0
文件: server.py 项目: gokulaol/g3
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)
示例#3
0
    def handle_post(self):

        if self.req_params['d_type'] == 'KNOWLEDGE_INPUT_SAVE':
            return self.handle_knowledge_save()
        elif self.req_params['d_type'] == 'KNOWLEDGE_SEARCH':
            debug_print(5, "POST", self.req_params)
            return self.handle_knowledge_search('JSON')
        else:
            return None
示例#4
0
    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'] + '&nbsp &nbsp'
                    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)
示例#5
0
def serve_normal_files(n_file_name):

    # get the name of the actual file
    file_name = os.path.basename(n_file_name)

    debug_print(5, "normal files: file_name = ", file_name)

    # search in the path
    containing_dir = find_containing_dir(file_name, global_args.base_dir)

    # static_file takes care of returning correct STATUS code
    # even if the file is not found.
    return static_file(file_name, root=containing_dir)
示例#6
0
文件: server.py 项目: gokulaol/g3
def serve_normal_files(n_file_name):

    # get the name of the actual file 
    file_name = os.path.basename(n_file_name)
        
    debug_print(5, "normal files: file_name = ", file_name)

    # search in the path
    containing_dir = find_containing_dir(file_name, global_args.base_dir)
    
    # static_file takes care of returning correct STATUS code
    # even if the file is not found.
    return static_file(file_name, root=containing_dir)
示例#7
0
    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)
示例#8
0
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)
示例#9
0
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)
示例#10
0
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)
示例#11
0
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)
示例#12
0
def serve_image_files(file_name):
    debug_print(5, "image files: file_name = ", file_name)
    return static_file(file_name, root=global_args.base_dir + 'client/images')
示例#13
0
文件: server.py 项目: gokulaol/g3
def serve_image_files(file_name):
    debug_print(5, "image files: file_name = ", file_name)
    return static_file(file_name, root=global_args.base_dir + 'client/images')