<s> import functools <EOL> import inspect <EOL> import os <EOL> from initpy . templates import blank , falcon , flask , tornado_web <EOL> from initpy . exceptions import RootPathDoesNotExists <EOL> def name_validator ( func ) : <EOL> @ functools . wraps ( func ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> func_args = inspect . getcallargs ( func , * args , ** kwargs ) <EOL> if func_args . get ( '<STR_LIT>' ) : <EOL> _filter = "<STR_LIT>" <EOL> name = func_args . get ( '<STR_LIT:name>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if ( len ( list ( set ( list ( name ) ) . intersection ( list ( _filter ) ) ) ) > <NUM_LIT:0> <EOL> or name [ <NUM_LIT:0> ] . isdigit ( ) ) : <EOL> title = func . __name__ . split ( '<STR_LIT:_>' ) [ <NUM_LIT:1> ] . title ( ) <EOL> raise globals ( ) [ '<STR_LIT>' + title + '<STR_LIT:Name>' ] <EOL> return func ( * args , ** kwargs ) <EOL> return wrapper <EOL> class Creator ( object ) : <EOL> errors = [ ] <EOL> root_path = None <EOL> def __init__ ( self , root_path ) : <EOL> if not os . path . isdir ( root_path ) : <EOL> raise RootPathDoesNotExists <EOL> self . root_path = root_path <EOL> @ name_validator <EOL> def create_file ( self , _path , name , template , validate = True ) : <EOL> file_path = os . path . join ( _path , name ) <EOL> with open ( file_path , '<STR_LIT:w>' ) as _file : <EOL> _file . write ( template ) <EOL> @ name_validator <EOL> def create_folder ( self , _path , name , validate = True ) : <EOL> try : <EOL> folder_path = os . path . join ( _path , name ) <EOL> os . mkdir ( folder_path ) <EOL> except OSError : <EOL> self . errors . append ( '<STR_LIT>' + name + '<STR_LIT>' ) <EOL> def create_module ( self , _path , name , template = blank . python ) : <EOL> self . create_folder ( _path , name ) <EOL> module_path = os . path . join ( _path , name ) <EOL> self . create_file ( module_path , '<STR_LIT>' , template , False ) <EOL> class FlaskCreator ( Creator ) : <EOL> def create_app ( self , _path , module ) : <EOL> self . create_module ( _path , "<STR_LIT>" , <EOL> flask . app_init . substitute ( module = module ) ) <EOL> app_path = os . path . join ( _path , "<STR_LIT>" ) <EOL> self . create_folder ( app_path , "<STR_LIT>" ) <EOL> self . create_templates ( app_path , module ) <EOL> self . create_app_module ( app_path , module ) <EOL> def create_app_module ( self , _path , name ) : <EOL> self . create_folder ( _path , name ) <EOL> module_path = os . path . join ( _path , name ) <EOL> self . create_file ( module_path , "<STR_LIT>" , <EOL> flask . module_init . substitute ( module = name ) , False ) <EOL> self . create_file ( module_path , "<STR_LIT>" , <EOL> flask . module_views . substitute ( module = name ) ) <EOL> self . create_file ( module_path , "<STR_LIT>" , blank . python ) <EOL> def create_templates ( self , _path , module ) : <EOL> self . create_folder ( _path , "<STR_LIT>" ) <EOL> template_path = os . path . join ( _path , "<STR_LIT>" ) <EOL> self . create_file ( template_path , "<STR_LIT>" , flask . base_html ) <EOL> self . create_folder ( template_path , module ) <EOL> self . create_file ( os . path . join ( template_path , module ) , "<STR_LIT>" , <EOL> flask . module_html ) <EOL> def create_requirements ( self , _path ) : <EOL> self . create_folder ( _path , "<STR_LIT>" ) <EOL> self . create_file ( os . path . join ( _path , "<STR_LIT>" ) , "<STR_LIT>" , <EOL> flask . requirements ) <EOL> def create_project ( self , name , module ) : <EOL> self . create_folder ( self . root_path , name ) <EOL> project_path = os . path . join ( self . root_path , name ) <EOL> self . create_file ( project_path , "<STR_LIT>" , flask . manager ) <EOL> self . create_app ( project_path , module ) <EOL> self . create_requirements ( project_path ) <EOL> class TornadoCreator ( Creator ) : <EOL> def create_handlers ( self , _path , name ) : <EOL> self . create_module ( _path , "<STR_LIT>" ) <EOL> handlers_path = os . path . join ( _path , "<STR_LIT>" ) <EOL> self . create_file ( handlers_path , name + "<STR_LIT>" , tornado_web . tornado_handler ) <EOL> def create_requirements ( self , _path ) : <EOL> self . create_folder ( _path , "<STR_LIT>" ) <EOL> self . create_file ( os . path . join ( _path , "<STR_LIT>" ) , "<STR_LIT>" , <EOL> tornado_web . requirements ) <EOL> def create_project ( self , name , module ) : <EOL> self . create_folder ( self . root_path , name ) <EOL> project_path = os . path . join ( self . root_path , name ) <EOL> self . create_file ( project_path , "<STR_LIT>" , tornado_web . tornado_app ) <EOL> self . create_file ( project_path , "<STR_LIT>" , <EOL> tornado_web . tornado_urls . substitute ( module = module ) ) <EOL> self . create_handlers ( project_path , module ) <EOL> self . create_requirements ( project_path ) <EOL> class FalconCreator ( Creator ) : <EOL> def create_app ( self , _path , module ) : <EOL> args = dict ( module = module , module_title = module . title ( ) ) <EOL> self . create_module ( _path , "<STR_LIT>" , <EOL> falcon . app_init . safe_substitute ( args ) ) <EOL> app_path = os . path . join ( _path , "<STR_LIT>" ) <EOL> self . create_middleware ( app_path ) <EOL> self . create_models ( app_path , module ) <EOL> self . create_app_module ( app_path , module ) <EOL> def create_app_module ( self , _path , name ) : <EOL> args = dict ( module = name , module_title = name . title ( ) ) <EOL> self . create_folder ( _path , '<STR_LIT>' ) <EOL> module_path = os . path . join ( _path , '<STR_LIT>' ) <EOL> self . create_file ( module_path , "<STR_LIT>" , <EOL> falcon . resource_init . safe_substitute ( args ) , False ) <EOL> self . create_file ( module_path , "<STR_LIT>" . format ( name ) , <EOL> falcon . resource_controller . safe_substitute ( args ) ) <EOL> def create_models ( self , _path , name ) : <EOL> self . create_module ( _path , "<STR_LIT>" ) <EOL> models_path = os . path . join ( _path , "<STR_LIT>" ) <EOL> self . create_file ( models_path , "<STR_LIT>" , blank . python ) <EOL> self . create_file ( models_path , "<STR_LIT>" . format ( name ) , blank . python ) <EOL> def create_middleware ( self , _path ) : <EOL> self . create_module ( _path , "<STR_LIT>" ) <EOL> middleware_path = os . path . join ( _path , "<STR_LIT>" ) <EOL> self . create_file ( middleware_path , "<STR_LIT>" , blank . python ) <EOL> def create_requirements ( self , _path ) : <EOL> self . create_folder ( _path , "<STR_LIT>" ) <EOL> self . create_file ( os . path . join ( _path , "<STR_LIT>" ) , "<STR_LIT>" , <EOL> falcon . requirements ) <EOL> def create_project ( self , name , module ) : <EOL> self . create_folder ( self . root_path , name ) <EOL> project_path = os . path . join ( self . root_path , name ) <EOL> self . create_file ( project_path , "<STR_LIT>" , falcon . manager ) <EOL> self . create_app ( project_path , module ) <EOL> self . create_requirements ( project_path ) <EOL> def downloader ( args ) : <EOL> url = args . download <EOL> from urllib2 import urlopen , HTTPError <EOL> try : <EOL> res = urlopen ( url ) <EOL> except HTTPError : <EOL> from initpy . prompt import color_print <EOL> color_print ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return <EOL> from initpy . compact import StringIO <EOL> from zipfile import ZipFile , BadZipfile <EOL> try : <EOL> template_zip = ZipFile ( StringIO ( res . read ( ) ) ) <EOL> except BadZipfile : <EOL> from initpy . prompt import color_print <EOL> color_print ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return <EOL> from os import path , getcwd , mkdir <EOL> proj_path = path . join ( getcwd ( ) , args . name ) <EOL> try : <EOL> mkdir ( proj_path ) <EOL> except OSError : <EOL> pass <EOL> zip_root = template_zip . namelist ( ) [ <NUM_LIT:0> ] <EOL> for fn in template_zip . namelist ( ) [ <NUM_LIT:1> : ] : <EOL> file_name = fn . replace ( zip_root , '<STR_LIT>' ) <EOL> file_path = path . join ( proj_path , file_name ) <EOL> if file_path . endswith ( '<STR_LIT:/>' ) : <EOL> try : <EOL> mkdir ( file_path ) <EOL> except OSError : <EOL> pass <EOL> else : <EOL> _file = open ( file_path , '<STR_LIT:w>' ) <EOL> _file . write ( template_zip . read ( fn ) ) <EOL> _file . close ( ) </s>
