def handle(self): if not self.app: raise NotConfigured('Please load the configuration first.') if len(sys.argv) == 1: self.show_console_usage() raise TerminationSignal('List command.') command_id = sys.argv[1] command = None commands = services.find_by_tag('command:{}'.format(command_id)) if commands: command = commands[0] if not command: self.show_console_usage() raise CommandNotFound(command_id) # Reconfigure the parser. parser = argparse.ArgumentParser(description='Console') subparsers = parser.add_subparsers() command_parser = subparsers.add_parser(command_id, description=command.__class__.__doc__.strip()) command.define_arguments(command_parser) command.execute(parser.parse_args())
def get(self, path=None): """ Get a particular resource. :param path: blocks of path used to composite an actual path. .. note:: This method requires refactoring. """ resource = self._retrieve_resource_entity() if isinstance(resource, str): self.set_header('Content-Type', 'image/vnd.microsoft.icon') return self.finish(resource) elif resource.exists and resource.cacheable: self._cache_objects[self.request.uri] = resource elif not resource.exists: # Return HTTP 404 if the content is not found. self._logger.error('%s could not be found.' % resource.path) raise HTTPError(404) # Get the content type. self.set_header("Content-Type", resource.kind or 'text/plain') # Retrieve the plugins if registered. if not ResourceService._plugins and not ResourceService._plugins_registered: ResourceService._plugins = services.find_by_tag( ResourceService._plugins_tag_name ) # Apply the plugin. for plugin in ResourceService._plugins: if plugin.expect(resource): resource = plugin.execute(resource) # End the iteration # Return the content. try: self.finish(resource.content) except Exception as e: raise HTTPError(500)
def get(self, path=None): """ Get a particular resource. :param path: blocks of path used to composite an actual path. .. note:: This method requires refactoring. """ resource = self._retrieve_resource_entity() if isinstance(resource, str): self.set_header('Content-Type', 'image/vnd.microsoft.icon') return self.finish(resource) if isinstance(resource, bytes): self.set_header('Content-Type', 'image/vnd.microsoft.icon') return self.finish(resource) if isinstance(resource, ResourceEntity): if resource.exists and resource.cacheable: self._cache_objects[self.request.uri] = resource if not resource.exists: # Return HTTP 404 if the content is not found. self._logger.error('{} could not be found.'.format(resource.path)) raise HTTPError(404) else: self._logger.error('Unable to process resource {}'.format(resource)) raise HTTPError(403) # Get the content type. self.set_header("Content-Type", resource.kind or 'text/plain') # Retrieve the plugins if registered. if not ResourceService._plugins and not ResourceService._plugins_registered: ResourceService._plugins = services.find_by_tag( ResourceService._plugins_tag_name ) # Apply the plugin. for plugin in ResourceService._plugins: if plugin.expect(resource): resource = plugin.execute(resource) # End the iteration if resource.is_originally_dir: alternative_destination = path[:-1] if path[-1] == '/' else path alternative_destination = '/{}/{}'.format(alternative_destination, ResourceEntity.DEFAULT_INDEX_FILE) self._logger.error('Assuming "{}" is {}.'.format(path, alternative_destination)) return self.redirect(alternative_destination) # Return the content. try: self.finish(resource.content) except Exception as e: raise HTTPError(500)