Exemplo n.º 1
0
 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()
Exemplo n.º 2
0
 def execute(self):
     try:
         app_module = os.environ['APP_MODULE']
         test_runner = None
         cli_args = ''
         sys.argv = [sys.argv.pop(0)]
         try:
             import pytest
             test_runner = 'pytest'
             cli_args = '--cov {0}'.format(app_module)
         except:
             with ignored(ImportError):
                 import nose
                 test_runner = 'nose'
                 cli_args = '--cover-package={0}'.format(app_module)
         if test_runner:
             sys.modules[test_runner].main(cli_args.split(' '))
         else:
             raise ConsoleError(
                 "You must install either 'nose' or 'py.test' to run the unit tests."
             )
     except:
         _no_application_error()
Exemplo n.º 3
0
 def execute(self):
     raise ConsoleError('Something went wrong')
Exemplo n.º 4
0
 def test_instance(self):
     exc = ConsoleError()
     assert isinstance(exc, KeyError)
Exemplo n.º 5
0
 def execute(self):
     if not self.parsed_args.project_name:
         raise ConsoleError('No project name specified')
     if not self.parsed_args.app_name:
         raise ConsoleError('No app name specified')
     project_name = self.parsed_args.project_name
     app_name = self.parsed_args.app_name
     if self.parsed_args.dir:
         root = os.path.abspath(self.parsed_args.dir)
     else:
         root = os.getcwd()
     basepath = os.path.join(root, project_name)
     paths = [
         basepath,
         os.path.join(basepath, app_name),
         os.path.join(basepath, app_name, 'config'),
         os.path.join(basepath, app_name, 'controllers'),
         os.path.join(basepath, app_name, 'views'),
         os.path.join(basepath, app_name, 'views', 'index'),
         os.path.join(basepath, 'data'),
         os.path.join(basepath, 'data', 'cache'),
         os.path.join(basepath, 'data', 'logs'),
         os.path.join(basepath, 'data', 'uploads'),
         os.path.join(basepath, 'public'),
         os.path.join(basepath, 'public', 'css'),
         os.path.join(basepath, 'public', 'img'),
         os.path.join(basepath, 'public', 'js'),
         os.path.join(basepath, 'tests'),
         os.path.join(basepath, 'tests', app_name),
         os.path.join(basepath, 'tests', app_name, 'controllers'),
     ]
     files = [
         (os.path.join(basepath, app_name,
                       '__init__.py'), BLANK_PY_TEMPLATE),
         (os.path.join(basepath, app_name, 'app.py'), APP_PY_TEMPLATE),
         (os.path.join(basepath, app_name, 'config',
                       '__init__.py'), BLANK_PY_TEMPLATE),
         (os.path.join(basepath, app_name, 'config',
                       'prod.py.dist'), PROD_CONFIG_PY_TEMPLATE),
         (os.path.join(basepath, app_name, 'config',
                       'dev.py.dist'), DEV_CONFIG_PY_TEMPLATE),
         (os.path.join(basepath, app_name, 'config',
                       'config.py'), DEV_CONFIG_PY_TEMPLATE),
         (os.path.join(basepath, app_name, 'config',
                       'routes.py'), ROUTES_PY_TEMPLATE),
         (os.path.join(basepath, app_name, 'controllers',
                       '__init__.py'), SAMPLE_CONTROLLER_INIT_TEMPLATE),
         (os.path.join(basepath, app_name, 'controllers',
                       'index.py'), SAMPLE_CONTROLLER_TEMPLATE),
         (os.path.join(basepath, app_name, 'views', 'index',
                       'get.html'), SAMPLE_VIEW_TEMPLATE),
         (os.path.join(basepath, 'tests',
                       '__init__.py'), BLANK_PY_TEMPLATE),
         (os.path.join(basepath, 'tests', app_name,
                       '__init__.py'), BLANK_PY_TEMPLATE),
         (os.path.join(basepath, 'tests', app_name, 'controllers',
                       '__init__.py'), BLANK_PY_TEMPLATE),
         (os.path.join(basepath, 'tests', app_name, 'controllers',
                       'test_index.py'), SAMPLE_TEST_SUITE),
         (os.path.join(basepath, 'console.py'), CONSOLE_TEMPLATE),
     ]
     for path in paths:
         try:
             os.mkdir(path)
         except:
             if not self.parsed_args.override:
                 raise ConsoleError(
                     'Project already exists at {0}'.format(basepath))
     for filename, contents in files:
         try:
             with open(filename, 'w', encoding='utf-8') as file:
                 file.write(
                     Template(contents).safe_substitute(app_name=app_name))
         except:
             if not self.parsed_args.override:
                 raise ConsoleError(
                     'File {0} already exists.'.format(filename))
     st = os.stat(files[-1][0])
     os.chmod(files[-1][0], st.st_mode | stat.S_IEXEC)
Exemplo n.º 6
0
def _no_application_error():
    raise ConsoleError(
        'No watson application can be found, are you sure you\'re in the correct directory?'
    )