1
1
#!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
- from __future__ import unicode_literals , print_function
2
+ from __future__ import print_function , unicode_literals
4
3
5
4
import argparse
6
5
import os
7
6
import sys
8
7
from distutils .util import strtobool
9
8
9
+ from pyms .config import create_conf_file
10
10
from pyms .crypt .fernet import Crypt
11
11
from pyms .flask .services .swagger import merge_swagger_file
12
12
from pyms .utils import check_package_exists , import_from , utils
13
- from pyms .config import create_conf_file
14
13
15
14
16
15
class Command :
@@ -27,37 +26,40 @@ def __init__(self, *args, **kwargs):
27
26
if not arguments : # pragma: no cover
28
27
arguments = sys .argv [1 :]
29
28
30
- parser = argparse .ArgumentParser (description = ' Python Microservices' )
29
+ parser = argparse .ArgumentParser (description = " Python Microservices" )
31
30
32
- commands = parser .add_subparsers (title = "Commands" , description = ' Available commands' , dest = ' command_name' )
31
+ commands = parser .add_subparsers (title = "Commands" , description = " Available commands" , dest = " command_name" )
33
32
34
- parser_encrypt = commands .add_parser (' encrypt' , help = ' Encrypt a string' )
35
- parser_encrypt .add_argument ("encrypt" , default = '' , type = str , help = ' Encrypt a string' )
33
+ parser_encrypt = commands .add_parser (" encrypt" , help = " Encrypt a string" )
34
+ parser_encrypt .add_argument ("encrypt" , default = "" , type = str , help = " Encrypt a string" )
36
35
37
- parser_create_key = commands .add_parser ('create-key' , help = 'Generate a Key to encrypt strings in config' )
38
- parser_create_key .add_argument ("create_key" , action = 'store_true' ,
39
- help = 'Generate a Key to encrypt strings in config' )
36
+ parser_create_key = commands .add_parser ("create-key" , help = "Generate a Key to encrypt strings in config" )
37
+ parser_create_key .add_argument (
38
+ "create_key" , action = "store_true" , help = "Generate a Key to encrypt strings in config"
39
+ )
40
40
41
41
parser_startproject = commands .add_parser (
42
- 'startproject' ,
43
- help = 'Generate a project from https://github.com/python-microservices/microservices-template' )
42
+ "startproject" ,
43
+ help = "Generate a project from https://github.com/python-microservices/microservices-template" ,
44
+ )
44
45
parser_startproject .add_argument (
45
- "startproject" , action = 'store_true' ,
46
- help = 'Generate a project from https://github.com/python-microservices/microservices-template' )
46
+ "startproject" ,
47
+ action = "store_true" ,
48
+ help = "Generate a project from https://github.com/python-microservices/microservices-template" ,
49
+ )
47
50
48
51
parser_startproject .add_argument (
49
- "-b" , "--branch" ,
50
- help = 'Select a branch from https://github.com/python-microservices/microservices-template' )
52
+ "-b" , "--branch" , help = "Select a branch from https://github.com/python-microservices/microservices-template"
53
+ )
51
54
52
- parser_merge_swagger = commands .add_parser ('merge-swagger' , help = 'Merge swagger into a single file' )
53
- parser_merge_swagger .add_argument ("merge_swagger" , action = 'store_true' ,
54
- help = 'Merge swagger into a single file' )
55
+ parser_merge_swagger = commands .add_parser ("merge-swagger" , help = "Merge swagger into a single file" )
56
+ parser_merge_swagger .add_argument ("merge_swagger" , action = "store_true" , help = "Merge swagger into a single file" )
55
57
parser_merge_swagger .add_argument (
56
- "-f" , "--file" , default = os .path .join (' project' , ' swagger' , ' swagger.yaml' ),
57
- help = 'Swagger file path' )
58
+ "-f" , "--file" , default = os .path .join (" project" , " swagger" , " swagger.yaml" ), help = "Swagger file path"
59
+ )
58
60
59
- parser_create_config = commands .add_parser (' create-config' , help = ' Generate a config file' )
60
- parser_create_config .add_argument ("create_config" , action = ' store_true' , help = ' Generate a config file' )
61
+ parser_create_config = commands .add_parser (" create-config" , help = " Generate a config file" )
62
+ parser_create_config .add_argument ("create_config" , action = " store_true" , help = " Generate a config file" )
61
63
62
64
parser .add_argument ("-v" , "--verbose" , default = "" , type = str , help = "Verbose " )
63
65
@@ -100,10 +102,10 @@ def run(self):
100
102
crypt = Crypt ()
101
103
if self .create_key :
102
104
path = crypt ._loader .get_path_from_env () # pylint: disable=protected-access
103
- pwd = self .get_input (' Type a password to generate the key file: ' )
105
+ pwd = self .get_input (" Type a password to generate the key file: " )
104
106
# Should use yes_no_input insted of get input below
105
107
# the result should be validated for Yes (Y|y) rather allowing anything other than 'n'
106
- generate_file = self .get_input (' Do you want to generate a file in {}? [Y/n]' .format (path ))
108
+ generate_file = self .get_input (" Do you want to generate a file in {}? [Y/n]" .format (path ))
107
109
generate_file = generate_file .lower () != "n"
108
110
key = crypt .generate_key (pwd , generate_file )
109
111
if generate_file :
@@ -118,7 +120,7 @@ def run(self):
118
120
if self .startproject :
119
121
check_package_exists ("cookiecutter" )
120
122
cookiecutter = import_from ("cookiecutter.main" , "cookiecutter" )
121
- cookiecutter (' gh:python-microservices/cookiecutter-pyms' , checkout = self .branch )
123
+ cookiecutter (" gh:python-microservices/cookiecutter-pyms" , checkout = self .branch )
122
124
self .print_ok ("Created project OK" )
123
125
if self .merge_swagger :
124
126
try :
@@ -128,8 +130,8 @@ def run(self):
128
130
self .print_error (ex .__str__ ())
129
131
return False
130
132
if self .create_config :
131
- use_requests = self .yes_no_input (' Do you want to use request' )
132
- use_swagger = self .yes_no_input (' Do you want to use swagger' )
133
+ use_requests = self .yes_no_input (" Do you want to use request" )
134
+ use_swagger = self .yes_no_input (" Do you want to use swagger" )
133
135
try :
134
136
conf_file_path = create_conf_file (use_requests , use_swagger )
135
137
self .print_ok (f'Config file "{ conf_file_path } " created' )
@@ -140,7 +142,9 @@ def run(self):
140
142
return True
141
143
142
144
def yes_no_input (self , msg = "" ): # pragma: no cover
143
- answer = input (utils .colored_text (f'{ msg } { "?" if not msg .endswith ("?" ) else "" } [Y/n] :' , utils .Colors .BLUE , True )) # nosec
145
+ answer = input ( # nosec
146
+ utils .colored_text (f'{ msg } { "?" if not msg .endswith ("?" ) else "" } [Y/n] :' , utils .Colors .BLUE , True )
147
+ )
144
148
try :
145
149
return strtobool (answer )
146
150
except ValueError :
@@ -168,6 +172,6 @@ def exit_ok(self, msg=""): # pragma: no cover
168
172
sys .exit (0 )
169
173
170
174
171
- if __name__ == ' __main__' : # pragma: no cover
175
+ if __name__ == " __main__" : # pragma: no cover
172
176
cmd = Command (arguments = sys .argv [1 :], autorun = False )
173
177
cmd .run ()
0 commit comments