def headers_handler(response): # retrieves the various information from # the response object to be used as diagnostics # information in the observer object status_code = response.status_code status_message = response.status_message content_length = response.headers_map.get("Content-Length") or 0 content_type = response.headers_map.get("Content-Type") or "N/A" content_length_integer = int(content_length) content_length_string = colony.size_round_unit(content_length_integer, space=True) # prints a series of message to the observer object colony.message(handlers_map, "Request sent, awaiting response... %d %s" % (status_code, status_message)) colony.message(handlers_map, "Received headers [%s] [%s]" % (content_length_string, content_type)) colony.message(handlers_map, "Receiving data... ") # updates the message length in the current context context["message_length"] = content_length_integer
def download_package(self, address, target_directory=None, handlers_map={}): """ Downloads a package from the given url address to a target directory. @type address: String @param address: The url address of the package to download. @type target_directory: String @param target_directory: The target directory of the download. @type handlers_map: Dictionary @param handlers_map: The map of handlers for the execution events. """ try: # retrieves the current time for the initial time initial_time = time.time() # sets the target directory target_directory = target_directory or self._get_default_target_directory() # retrieves the client http plugin client_http_plugin = self.plugin.client_http_plugin # notifies the handlers about the message colony.message(handlers_map, "Get %s" % address) # creates a new set of handlers map (for http client) for # the current context _handlers_map = self._create_handlers_map(handlers_map) # retrieves the file name from the url path file_name = self.get_file_name_url(address) # creates the http client http_client = client_http_plugin.create_client({}) # opens the http client http_client.open({}) try: # fetches the url retrieving the http response http_response = http_client.fetch_url(address, handlers_map=_handlers_map) # retrieves the status code from the http response status_code = http_response.status_code # in case the status code is not valid if not status_code in VALID_STATUS_CODES: # retrieves the status message from the http response status_message = http_response.status_message # raises the invalid status code exception raise exceptions.InvalidStatusCodeException("%i - %s" % (status_code, status_message)) # retrieves the file contents from the http response file_contents = http_response.received_message finally: # closes the http client http_client.close({}) # in case there is no directory if not os.path.isdir(target_directory): # creates the directory and intermediate directories os.makedirs(target_directory) # creates the file path by joining the target directory (path) # and the file name target_path = os.path.join(target_directory, file_name) # opens a new file and creates it if necessary file = open(target_path, "wb") try: # writes the contents to the file file.write(file_contents) finally: # closes the file file.close() # retrieves the current time for the final time # and calculates the delta time final_time = time.time() delta_time = final_time - initial_time # calculates the file contents length and then # uses it to calculate the speed of the download # measured in bytes file_contents_length = len(file_contents) speed = int(float(file_contents_length) / delta_time) # converts the speed value into a size string (speed string) # for simpler scale values speed_string = colony.size_round_unit(speed, space=True) # notifies the handlers about the message colony.message(handlers_map, "Saved data as %s [%s/s]. " % (file_name, speed_string)) except Exception as exception: # prints an info message self.plugin.info( "Problem while downloading file: " + address + ", error: " + colony.legacy.UNICODE(exception) )
def handle_directory_list(self, request, directory_list): # sets the request content type request.content_type = HTML_MIME_TYPE # retrieves the plugin manager plugin_manager = self.plugin.manager # retrieves the template engine plugin template_engine_plugin = self.plugin.template_engine_plugin # retrieves the plugin path plugin_path = plugin_manager.get_plugin_path_by_id(self.plugin.id) # creates the template file path template_file_path = plugin_path +\ "/" + TEMPLATE_DIRECTORY_RESOURCES_PATH + "/" + DIRECTORY_HTML_TEMPLATE_FILE_NAME # parses the template file path template_file = template_engine_plugin.parse_template( template_file_path, encoding = DEFAULT_TEMPLATE_ENCODING ) # retrieves the directory entries directory_entries = directory_list["entries"] # iterates over all the directory entries in the directory # entries (list) to process their values for the template for directory_entry in directory_entries: # retrieves the directory entry type directory_entry_type = directory_entry["type"] # retrieves the directory entry size directory_entry_size = directory_entry["size"] # in case the directory entry type is file must round # the size of the file according to the predefined rules # otherwise a slice must be used to indicate the size if directory_entry_type == "file": directory_entry_size_string = colony.size_round_unit(directory_entry_size) else: directory_entry_size_string = "-" # sets the directory entry size string value directory_entry["size_string"] = directory_entry_size_string # retrieves the requested resource base path resource_base_path = request.get_resource_base_path_decoded() # splits the resource base path into various values resource_path_values = resource_base_path.strip("/").split("/") # retrieves the prefix resource path values prefix_resource_path_values = resource_path_values[:-1] # retrieves the suffix resource path value suffix_resouces_path_value = resource_path_values[-1] # creates the directory list directory_list = [] # starts the index value with the length of the # prefix resource path values index = len(prefix_resource_path_values) for prefix_resource_path_value in prefix_resource_path_values: # creates the resource item resource_item = {} # sets the resource item value resource_item["name"] = prefix_resource_path_value resource_item["link"] = "../" * index # decrements the index value index -= 1 # adds the resources item to the directory list directory_list.append(resource_item) # retrieves the format attribute from the request format = request.get_attribute("format") or "table" # creates the format file path format_file = "formats/" + format + ".html.tpl" # creates a new formats map from the original one formats_map = copy.copy(FORMATS_MAP) # retrieves the initial time start_time = request.request_time # retrieves the end time end_time = time.time() # calculates the delta time delta_time = end_time - start_time # rounds the delta time delta_time_rounded = round(delta_time, 2) # sets the current format as active formats_map[format] = "active" # assigns the template variables template_file.assign("directory_list", directory_list) template_file.assign("directory_final_item", suffix_resouces_path_value) template_file.assign("directory_entries", directory_entries) template_file.assign("format", format) template_file.assign("format_file", format_file) template_file.assign("formats_map", formats_map) template_file.assign("delta_time", delta_time_rounded) # processes the template file processed_template_file = template_file.process() # encodes the processed template file using the default encoding processed_template_file_encoded = processed_template_file.encode(DEFAULT_ENCODING) # writes the processed template file encoded to the request request.write(processed_template_file_encoded)