async def model(name: str, table: str): """ \b Generate a new ORM model schematic... \b USAGE: Models should be lower_understore and SINGULAR While their table should be lower_understore and PLURAL \b ./uvicore gen model user users ./uvicore gen model user_detail user_details ./uvicore gen model post posts ./uvicore gen model post_tag post_tags """ stub = os.path.dirname(__file__) + '/stubs/model.py' dest = uvicore.config('app.paths.models') + '/' + name + '.py' Schematic( type='model', stub=stub, dest=dest, replace = [ ('xx_modelname', name), ('xx_ModelName', str.studly(name)), ('xx_tablename', table), ('xx_TableName', str.studly(table)), ] ).generate() uvicore.log.nl() uvicore.log.notice('Be sure to add this model to your ./models/__init__.py')
def __init__(self, *, type: str, stub: str, dest: str, replace: List): self.type = type self.package = uvicore.app.package(main=True) self.stub = os.path.realpath(stub) self.dest = os.path.realpath(dest) # Replacements (order is important) self.replacements = replace self.replacements.extend([ ("xx_vendor", self.package.vendor), ("xx_Vendor", str.studly(self.package.vendor)), ("xx_appname", self.package.short_name), ("xx_AppName", str.studly(self.package.short_name)), ])
async def command(name: str): """Generate a new CLI Command""" stub = os.path.dirname(__file__) + '/stubs/command.py' dest = uvicore.config('app.paths.commands') + '/' + name + '.py' Schematic(type='command', stub=stub, dest=dest, replace=[('xx_name', name)]).generate() # Get running package package = uvicore.app.package(main=True) log.nl() log.header('Add this to your Service Provider commands List') print("'{}': '{}.commands.{}.cli',".format(name, package.name, name)) log.nl() log.notice( 'IF you do NOT have a self.commands() already in your Service Provider, add this' ) print("""self.commands( group='{}', help='{} Commands', commands={{ '{}': '{}.commands.{}.cli', }} )""".format( package.short_name, str.studly(package.short_name), name, package.name, name, ))
async def seeder(name: str): """ \b Generate a new Database table seeder schematic... \b USAGE: Seeder should match the lower_underscore PLURAL tablenames \b ./uvicore gen seeder users ./uvicore gen seeder user_details ./uvicore gen seeder posts ./uvicore gen seeder post_tags """ stub = os.path.dirname(__file__) + '/stubs/seeder.py' dest = uvicore.config('app.paths.seeders') + '/' + name + '.py' Schematic(type='seeder', stub=stub, dest=dest, replace=[ ('xx_modelname', name), ('xx_ModelName', str.studly(name)), ]).generate() uvicore.log.nl() uvicore.log.notice( 'Be sure to add this seeder to your ./database/seeders/__init__.py')
async def table(name: str): """ \b Generate a new Database table schematic... \b USAGE: Tables should be lower_underscore and PLURAL If a table is awkward as plural, ok to make a few singluar (user_info) \b ./uvicore gen table users ./uvicore gen table user_details ./uvicore gen table posts ./uvicore gen table post_tags """ stub = os.path.dirname(__file__) + '/stubs/table.py' dest = uvicore.config('app.paths.tables') + '/' + name + '.py' Schematic(type='table', stub=stub, dest=dest, replace=[ ('xx_tablename', name), ('xx_TableName', str.studly(name)), ]).generate() uvicore.log.nl() uvicore.log.notice( 'Be sure to add this table to your ./database/tables/__init__.py')
async def seeder(name: str): """Generate a new Database table seeder schematic""" stub = os.path.dirname(__file__) + '/stubs/seeder.py' dest = uvicore.config('app.paths.seeders') + '/' + name + '.py' Schematic(type='seeder', stub=stub, dest=dest, replace=[ ('xx_modelname', name), ('xx_ModelName', str.studly(name)), ]).generate()
async def table(name: str): """Generate a new Database table schematic""" stub = os.path.dirname(__file__) + '/stubs/table.py' dest = uvicore.config('app.paths.tables') + '/' + name + '.py' Schematic(type='table', stub=stub, dest=dest, replace=[ ('xx_tablename', name), ('xx_TableName', str.studly(name)), ]).generate()
async def command(name: str): """ \b Generate a new CLI command... \b USAGE: Commands should be lower_underscore and SINGULAR (plural is OK) Remember to manually add the command to your service provider! \b ./uvicore gen command welcome ./uvicore gen command process ./uvicore gen command scan_files """ stub = os.path.dirname(__file__) + '/stubs/command.py' dest = uvicore.config('app.paths.commands') + '/' + name + '.py' Schematic(type='command', stub=stub, dest=dest, replace=[('xx_name', name)]).generate() # Get running package package = uvicore.app.package(main=True) log.nl() log.header('Add this to your Service Provider commands List') print("'{}': '{}.commands.{}.cli',".format(str.kebab(name), package.name, name)) log.nl() log.notice( 'IF you do NOT have a self.commands() already in your Service Provider, add this' ) print("""self.commands( group='{}', help='{} Commands', commands={{ '{}': '{}.commands.{}.cli', }} )""".format( package.short_name, str.studly(package.short_name), str.kebab(name), package.name, name, ))
async def controller(name: str): """ \b Generate a new HTTP Web controller schematic... \b USAGE: Web Controllers should be lower_understore and SINGULAR \b ./uvicore gen controller home ./uvicore gen controller about ./uvicore gen controller contact_us """ stub = os.path.dirname(__file__) + '/stubs/controller.py' dest = uvicore.config('app.paths.controllers') + '/' + name + '.py' Schematic(type='controller', stub=stub, dest=dest, replace=[ ('xx_controllername', name), ('xx_ControllerName', str.studly(name)), ]).generate()
async def api_controller(name: str): """ \b Generate a new HTTP API controller schematic... \b USAGE: API Controllers should be lower_understore and SINGULAR \b ./uvicore gen controller user ./uvicore gen controller user_detail ./uvicore gen controller post ./uvicore gen controller post_tag """ stub = os.path.dirname(__file__) + '/stubs/api_controller.py' dest = uvicore.config('app.paths.api') + '/' + name + '.py' Schematic(type='api_controller', stub=stub, dest=dest, replace=[ ('xx_controllername', name), ('xx_ControllerName', str.studly(name)), ]).generate()
def controller(self, module: Union[str, Callable], *, prefix: str = '', name: str = '', tags: Optional[List[str]] = None, options: Dict = {}) -> List: """Include a Route Controller""" if prefix: if prefix[-1] == '/': prefix = prefix[0:-1] # Remove trailing / if prefix[0] != '/': prefix = '/' + prefix # Add beginning / # Get name if not name: name = prefix # Clean name if name: name = name.replace('/', '.') if name[-1] == '.': name = name[0:-1] # Remove trailing . if name[0] == '.': name = name[1:] # Remove beginning . # Import controller module from string cls = module if type(module) == str: if self.controllers: if '.' not in module: # We are defining just 'home', so we add .Home class module = self.controllers + '.' + module + '.' + string.studly( module) elif module[0] == '.': # We are appending the path to self.controllers # Must have class too, ex: .folder.stuff.Stuff module = self.controllers + module # elif module.count('.') == 1: # NO, we'll just add a . before, like .home.Home and it does the same thing # # We are defining the file and the class (home.Home) # # Only works with ONE dot. If you want to append use .folder.stuff.Stuff # module = self.controllers + '.' + module else: # We are defining the FULL module path even though we have defined a self.controller path # Must have class too, ex: acme.appstub.http.api.stuff.Stuff pass # Dynamically import the calculated module cls = load(module).object if str(type(cls)) == "<class 'module'>": # Trying to load a module, but we want the class inside the module, auto add module = module + '.' + string.studly(module.split('.')[-1]) cls = load(module).object # Instantiate controller file controller: Routes = cls(self.package, **options) # New self (Web or Api) router instance router = self.__class__(self.package, self.prefix + prefix, self.name + '.' + name, self.controllers) # Register controllers routes and return new updated router router = controller.register(router) # Add contoller class level attributes as middleware to each route on this controller controller_middlewares = controller._middleware() if controller_middlewares: for route in router.routes.values(): (route.middleware, route.endpoint) = self._merge_route_middleware( controller_middlewares, route.middleware, route.endpoint) # Merge controllers routes into this main (parent of recursion) router #dump(router.routes) self.routes.merge(router.routes) # Return just this controllers routes as a list routes = [] for route in router.routes.keys(): # Append .controller(tags=[xyz]) if exists if tags: if router.routes[route].tags is None: router.routes[route].tags = [] router.routes[route].tags.extend(tags) routes.append(router.routes[route]) return routes
def controller(self, module: Union[str, Callable], *, prefix: str = '', name: str = '', tags: Optional[List[str]] = None, options: Dict = {}) -> List: if prefix: if prefix[-1] == '/': prefix = prefix[0:-1] # Remove trailing / if prefix[0] != '/': prefix = '/' + prefix # Add beginning / # Get name if not name: name = prefix # Clean name if name: name = name.replace('/', '.') if name[-1] == '.': name = name[0:-1] # Remove trailing . if name[0] == '.': name = name[1:] # Remove beginning . # Import controller module from string if type(module) == str: if self.controllers: if '.' not in module: # We are defining just 'home', so we add .Home class module = self.controllers + '.' + module + '.' + string.studly( module) elif module.count('.') == 1: # We are defining the file and the class (home.Home) module = self.controllers + '.' + module else: # We are defining the FULL module path even though we have defined a self.controller path pass module = load(module).object # Instantiate controller file controller: Routes = module(self.package, **options) # New self (Web or Api) router instance router = self.__class__(self.package, self.prefix + prefix, self.name + '.' + name, self.controllers) # Register controllers routes and return new updated router router = controller.register(router) # Add contoller class level attributes as middleware to each route on this controller controller_middlewares = controller._middleware() if controller_middlewares: for route in router.routes.values(): (route.middleware, route.endpoint) = self._merge_route_middleware( controller_middlewares, route.middleware, route.endpoint) # Merge controllers routes into this main (parent of recursion) router #dump(router.routes) self.routes.merge(router.routes) # Return just this controllers routes as a list routes = [] for route in router.routes.keys(): routes.append(router.routes[route]) return routes