def _analyze_resources_directory(self, directory_path, verified_resource_paths_list): # in case the directory path does not exists if not os.path.exists(directory_path): # returns immediately return # retrieves the resources manager plugin resources_manager_plugin = self.plugin.resources_manager_plugin # retrieves the file path resources list map from the resources manager file_path_resources_list_map = resources_manager_plugin.get_file_path_resources_list_map() # retrieves the resources path directory contents resources_path_directory_contents = os.listdir(directory_path) # iterates over the resources path directory contents # to load them in the resources manager for resources_path_item in resources_path_directory_contents: # creates the resources full path item resources_full_path_item = os.path.join(directory_path, resources_path_item) # in case the current the resource path item refers a real # resource name (resource found in path) if resources_manager_plugin.is_resource_name(resources_path_item): # normalizes the resources full path (for file verification) resources_full_path_item_normalized = colony.normalize_path(resources_full_path_item) # adds the resource path to the verified resource paths list # because the resource has been verified as existent verified_resource_paths_list.append(resources_full_path_item_normalized) # retrieves the current modified time from the resource and the # modified time currently stored in the internal structure # for later comparison current_modified_time = os.path.getmtime(resources_full_path_item_normalized) modified_time = self.file_path_modified_time_map.get(resources_full_path_item_normalized, None) # in case the modified time hasn't changed # the file is considered to be the same if current_modified_time == modified_time: # continues the loop (nothing changed) continue # in case there's no current modified time defined (the file # did not already existed) if modified_time == None: # prints an info message (about the loading) self.plugin.info("Loading resource file '%s' in resources manager" % resources_full_path_item_normalized) # otherwise the file already existed but the modified time has # changed (reload case) else: # prints an info message (about the reloading) self.plugin.info("Reloading resource file '%s' in resources manager" % resources_full_path_item_normalized) # retrieves the resources list for the resources path and then uses it to # unregister the resources in the resources manager resources_list = file_path_resources_list_map[resources_full_path_item_normalized] resources_manager_plugin.unregister_resources(resources_list, resources_full_path_item, directory_path) # parses the resources description file resources_manager_plugin.parse_file(resources_full_path_item, directory_path) # sets the "new" modified time in the file path modified # time map (updates the modified time) self.file_path_modified_time_map[resources_full_path_item_normalized] = current_modified_time # otherwise in case the resources full path is a directory # path a descent must be done elif os.path.isdir(resources_full_path_item): # analyzes the resources for the directory self._analyze_resources_directory(resources_full_path_item, verified_resource_paths_list)
def _process_directory(self, request, complete_path): """ Processes a directory request for the given complete path and request. :type request: HttpRequest :param request: The http request to be handled. :type complete_path: String :param complete_path: The complete path to the directory. """ # retrieves the requested resource base path resource_base_path = request.get_resource_base_path_decoded() # in case the resource base path does not end with a slash if not resource_base_path.endswith("/"): # adds the extra slash to the resource base path # in order to avoid file redirection problems # and then redirect the user agent to the new page, # returning the control flow immediately to caller resource_path = resource_base_path.encode(DEFAULT_CHARSET) + "/".encode(DEFAULT_CHARSET) self._redirect(request, resource_path) return # retrieves the directory names for the complete path directory_names = os.listdir(complete_path) # creates a list for the directory entries directory_entries = [] # iterates over all the directory names for directory_name in directory_names: # creates the complete file path file_path = complete_path + "/" + directory_name # normalizes the file path file_path = colony.normalize_path(file_path) # retrieves the file stat file_stat = os.stat(file_path) # retrieves the file properties file_size = file_stat[stat.ST_SIZE] file_modified_date = datetime.datetime.fromtimestamp(file_stat[stat.ST_MTIME]) # retrieves the file mode file_mode = file_stat[stat.ST_MODE] # in case the file is of type directory or link if stat.S_ISDIR(file_mode) or stat.S_ISLNK(file_mode): file_type = FOLDER_TYPE file_size = 0 # in case the file is of type register elif stat.S_ISREG(file_mode): file_type = FILE_TYPE # otherwise else: file_type = UNKNOWN_TYPE # creates the file entry file_entry = {} # sets the file entry values file_entry["name"] = directory_name file_entry["size"] = file_size file_entry["modified_date"] = file_modified_date file_entry["type"] = file_type # adds the file entry to the directory entries directory_entries.append(file_entry) # retrieves the comparator attribute from the request comparator = request.get_attribute("comparator") or "name" # generates a new comparator method using the comparator string # note that this is a key based comparator method comparator_method = self.generate_comparator(comparator) # sorts the directory entries directory_entries.sort(key = comparator_method) directory_entries.sort(key = self.type_comparator) # creates the directory list structure directory_list = {} # sets the entries in the directory list structure directory_list["entries"] = directory_entries directory_list["comparator"] = comparator # handles the directory list self._handle_directory_list(self.handler_configuration, request, directory_list)