def execute(self): try: router = self.container.get('router') if not router.routes: raise ConsoleError('There are no routes associated with the application.') if self.parsed_args.url: environ = {} util.setup_testing_defaults(environ) environ.update({ 'REQUEST_METHOD': self.parsed_args.method or 'GET', 'HTTP_ACCEPT': self.parsed_args.format or 'text/html', 'PATH_INFO': self.parsed_args.url, 'SERVER_NAME': self.parsed_args.server or '127.0.0.1' }) request = create_request_from_environ(environ) matches = router.matches(request) if matches: sys.stdout.write(colors.header('Displaying {0} matching routes for the application:\n'.format(len(matches)))) for match in matches: sys.stdout.write('{0}\t\t\{1}\t\t{2}\n'.format(colors.ok_green(match.route.name), match.route.path, match.route.regex.pattern)) else: raise ConsoleError('There are no matching routes.') else: sys.stdout.write(colors.header('Displaying {0} routes for the application:\n'.format(len(router)))) for name, route in router: sys.stdout.write('{0}\t\t{1}\n'.format(name, route.path)) except ConsoleError: raise except: _no_application_error()
def routes(self, path, method, format, server): """Aids in the debugging of routes associated. Args: path: Validate the specified path against the router method: The http request method format: The http request format server: The hostname of the request """ try: router = self.router if not router.routes: raise ConsoleError( 'There are no routes associated with the application.') if path: environ = {} util.setup_testing_defaults(environ) server = server or '127.0.0.1' environ.update({ 'REQUEST_METHOD': method or 'GET', 'HTTP_ACCEPT': format or 'text/html', 'PATH_INFO': path, 'SERVER_NAME': server, 'HTTP_HOST': server }) request = Request.from_environ(environ) matches = [match for match in router.matches(request)] if matches: longest_route = max([match.route.name for match in matches], key=len) self.write( colors.header('Displaying {} matching routes for {}:\n'.format( len(matches), request.url))) for match in matches: route = match.route self.write('{0}\t{1}\n'.format( route.name.rjust(len(longest_route)), route.path)) else: raise ConsoleError('There are no matching routes.') else: self.write(colors.header('Displaying {0} routes for the application:\n'.format(len(router)))) longest_route = max(router, key=len) for name, route in router: self.write('{0}\t{1}\n'.format( name.rjust(len(longest_route[0])), route.path)) except ConsoleError: raise except Exception as e: raise e _no_application_error()
def usage(self): """Returns the usage text. This is used when the -h or --help command is invoked. """ help = colors.header("""{name} [command], or append -h (--help) for additional help. """) return help.format(name=self.name)
def get_command_usage(self, command): """Returns the usage string for an individual command. """ usage = [] for arguments in command.arguments: if isinstance(arguments, tuple): usage.append(''.join(('[', arguments[0][0], ' VALUE]'))) else: usage.append(arguments.get('name', arguments.get('dest'))) return colors.header(' '.join((command.name, ' '.join(usage))))
def execute(self): try: router = self.container.get('router') if not router.routes: raise ConsoleError( 'There are no routes associated with the application.') if self.parsed_args.url: environ = {} util.setup_testing_defaults(environ) environ.update({ 'REQUEST_METHOD': self.parsed_args.method or 'GET', 'HTTP_ACCEPT': self.parsed_args.format or 'text/html', 'PATH_INFO': self.parsed_args.url, 'SERVER_NAME': self.parsed_args.server or '127.0.0.1' }) request = Request.from_environ(environ) matches = router.matches(request) if matches: sys.stdout.write( colors.header( 'Displaying {0} matching routes for the application:\n' .format(len(matches)))) for match in matches: sys.stdout.write('{0}\t\t\{1}\t\t{2}\n'.format( colors.ok_green(match.route.name), match.route.path, match.route.regex.pattern)) else: raise ConsoleError('There are no matching routes.') else: sys.stdout.write( colors.header( 'Displaying {0} routes for the application:\n'.format( len(router)))) for name, route in router: sys.stdout.write('{0}\t\t{1}\n'.format(name, route.path)) except ConsoleError: raise except: _no_application_error()
def update_usage(self, parser, namespace, is_subparser=False): """Updates the usage for the relevant parser. Forces the usage message to include the namespace and color. """ usage = parser.format_usage() r = USAGE_REGEX.search(usage) if not r: return parts = [s for s in r.groups()] if namespace: if is_subparser: method_part = parts[1].split(' ') method_part.insert(-2, namespace) parts[1] = ' '.join(method_part) else: parts.insert(2, '{} '.format(namespace)) parser.usage = colors.header(''.join(parts[1:]).strip())
def test_header(self): assert header('test') == '\033[95mtest\033[0m' assert header('test', terminate=False) == '\033[95mtest'