Module licenseware.cli.restx_controller_creator.restx_controller_creator

Expand source code
import os
from dataclasses import dataclass
from licenseware.cli.base_creator import BaseCreator
from . import templates



@dataclass
class paths:
    controllers: str = './app/controllers'
    root: str = "./"


class RestxControllerCreator(BaseCreator):

    def __init__(self, controller_name: str):
        super().__init__(controller_name)

    
    def add_namespace_import(self):

        app_init_path = "./app/__init__.py"

        import_controller_str = f'from app.controllers.{self.entity_underscore}_controller import ns as {self.entity_underscore}_ns'
        register_controller_str = f'App.register_namespace({self.entity_underscore}_ns)'

        with open(app_init_path, 'r') as f:
            data = f.readlines()

        # Importing controller 
        data.insert(data.index('from licenseware.app_builder import AppBuilder\n') + 1, import_controller_str)
        data.insert(data.index('from licenseware.app_builder import AppBuilder\n') + 2, '\n')

        # Registering controller
        data.insert(data.index(')\n') + 1, register_controller_str)
        data.insert(data.index(')\n') + 2, '\n')

        data = "".join(data)

        with open(app_init_path, 'w') as f:
            f.write(data)


    def create(self):

        filepath = os.path.join(paths.controllers, self.entity_underscore + "_controller.py")

        if not os.path.exists(filepath):
            self.add_namespace_import()

        self.create_file(
            filename = self.entity_underscore + "_controller.py",
            filepath = paths.controllers,
            template_filename="restx_controller.py.jinja",
            template_resource = templates
        )

Classes

class RestxControllerCreator (controller_name: str)

This class uses the given entity_id which can be: uploader_id, report_id, app_id etc The entity_id will have multiple derivate shapes (see bellow transformations in the init method) which will be passed to jinja templates as template variables

Each file creation class should inherit (if possible) from this class.

As an example you can see licenseware/cli/uploader_creator package.

Expand source code
class RestxControllerCreator(BaseCreator):

    def __init__(self, controller_name: str):
        super().__init__(controller_name)

    
    def add_namespace_import(self):

        app_init_path = "./app/__init__.py"

        import_controller_str = f'from app.controllers.{self.entity_underscore}_controller import ns as {self.entity_underscore}_ns'
        register_controller_str = f'App.register_namespace({self.entity_underscore}_ns)'

        with open(app_init_path, 'r') as f:
            data = f.readlines()

        # Importing controller 
        data.insert(data.index('from licenseware.app_builder import AppBuilder\n') + 1, import_controller_str)
        data.insert(data.index('from licenseware.app_builder import AppBuilder\n') + 2, '\n')

        # Registering controller
        data.insert(data.index(')\n') + 1, register_controller_str)
        data.insert(data.index(')\n') + 2, '\n')

        data = "".join(data)

        with open(app_init_path, 'w') as f:
            f.write(data)


    def create(self):

        filepath = os.path.join(paths.controllers, self.entity_underscore + "_controller.py")

        if not os.path.exists(filepath):
            self.add_namespace_import()

        self.create_file(
            filename = self.entity_underscore + "_controller.py",
            filepath = paths.controllers,
            template_filename="restx_controller.py.jinja",
            template_resource = templates
        )

Ancestors

Methods

def add_namespace_import(self)
Expand source code
def add_namespace_import(self):

    app_init_path = "./app/__init__.py"

    import_controller_str = f'from app.controllers.{self.entity_underscore}_controller import ns as {self.entity_underscore}_ns'
    register_controller_str = f'App.register_namespace({self.entity_underscore}_ns)'

    with open(app_init_path, 'r') as f:
        data = f.readlines()

    # Importing controller 
    data.insert(data.index('from licenseware.app_builder import AppBuilder\n') + 1, import_controller_str)
    data.insert(data.index('from licenseware.app_builder import AppBuilder\n') + 2, '\n')

    # Registering controller
    data.insert(data.index(')\n') + 1, register_controller_str)
    data.insert(data.index(')\n') + 2, '\n')

    data = "".join(data)

    with open(app_init_path, 'w') as f:
        f.write(data)
def create(self)
Expand source code
def create(self):

    filepath = os.path.join(paths.controllers, self.entity_underscore + "_controller.py")

    if not os.path.exists(filepath):
        self.add_namespace_import()

    self.create_file(
        filename = self.entity_underscore + "_controller.py",
        filepath = paths.controllers,
        template_filename="restx_controller.py.jinja",
        template_resource = templates
    )

Inherited members

class paths (controllers: str = './app/controllers', root: str = './')

paths(controllers: str = './app/controllers', root: str = './')

Expand source code
class paths:
    controllers: str = './app/controllers'
    root: str = "./"

Class variables

var controllers : str
var root : str