/
tasks.py
139 lines (121 loc) · 5.48 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright 2015-2018 Flavio Garcia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import sys
import os
from six import iteritems
import firenado.conf
from firenado.management import ManagementTask
from firenado.util import file as _file
logger = logging.getLogger(__name__)
class CreateProjectTask(ManagementTask):
"""
Creates a new project from scratch
"""
def run(self, namespace):
from tornado import template
if len(sys.argv) > 2:
module = namespace.module
component = module.replace(".", " ").title().replace(" ", "")
project_name = module.replace(".", "_").lower()
project_directory = _file.create_module(module, os.getcwd())
#TODO: Check if project exists
#TODO: If doesn't exists create project
#TODO: If exists throw an error
loader = template.Loader(os.path.join(firenado.conf.ROOT,
"management", "templates",
"project"))
project_init_content = loader.load("app.py.txt").generate(
project_name=project_name, module=module, component=component)
# Generating application firenado component and handlers
_file.write(os.path.join(project_directory, "__init__.py"), "")
_file.write(os.path.join(project_directory, "app.py"),
project_init_content.decode(sys.stdout.encoding))
handlers_file_name = os.path.join(project_directory, "handlers.py")
_file.touch(handlers_file_name)
project_handlers_content = loader.load("handlers.py.txt").generate(
handlers=["Index"])
_file.write(handlers_file_name,
project_handlers_content.decode(sys.stdout.encoding))
# Generating configuration
project_conf_directory = os.path.join(project_directory, "conf")
os.mkdir(project_conf_directory)
project_conf_file = os.path.join(project_conf_directory,
"firenado.yml")
_file.touch(project_conf_file)
project_init_content = loader.load("firenado.yml.txt").generate(
app_name=project_name, module=module, component=component)
_file.write(project_conf_file,
project_init_content.decode(sys.stdout.encoding))
else:
#TODO: This thing has to go. The parameter validation should be
#TODO: executed by the argument parser.
loader = template.Loader(os.path.join(
firenado.conf.ROOT, "management", "templates", "help"))
help_message = loader.load("init_command_help.txt").generate()
def add_arguments(self, parser):
"""
Add module argument to the command parser.
:param parser: The current parser validating the command holding this
task.
"""
parser.add_argument("module", help="The project module")
def get_error_message(self, parser, exception):
return exception.message
class InstallProjectTask(ManagementTask):
""" Triggers the install method of all components registered in the
application.
"""
def run(self, namespace):
# TODO: Resolve module if doesn't exists
if firenado.conf.app['pythonpath']:
sys.path.append(firenado.conf.app['pythonpath'])
# TODO This should consider the type of application being handled by
# Firenado.
from firenado.tornadoweb import TornadoApplication
application = TornadoApplication()
for key, component in iteritems(application.components):
component.install()
class RunApplicationTask(ManagementTask):
def add_arguments(self, parser):
parser.add_argument("-a", "--addresses", default=None)
parser.add_argument("-d", "--dir", default=None)
parser.add_argument("-P", "--port", type=int)
""" Runs a Firenado Tornado Application based
on the it's project configuration
"""
def run(self, namespace):
#TODO throw a custom error when type is not found
from firenado.config import get_class_from_config
parameters = {}
if namespace.addresses is not None:
parameters['addresses'] = namespace.addresses.split(",")
if namespace.dir is not None:
parameters['dir'] = namespace.dir
if namespace.port is not None:
parameters['port'] = namespace.port
app_type = firenado.conf.app['types'][firenado.conf.app['type']]
launcher = get_class_from_config(app_type['launcher'])(**parameters)
launcher.load()
launcher.launch()
class GenerateCookieSecretTask(ManagementTask):
""" Generates a radom string to serve as the cookie secret for an
application.
"""
def run(self, namespace):
from firenado.util import random_string
print(random_string(64))