Module licenseware.cli.report_creator.report_creator

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



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




class ReportCreator(BaseCreator):

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


    def add_report_import(self):

        app_init_path = "./app/__init__.py"

        import_report_str = f'from app.reports.{self.entity_underscore} import {self.entity_underscore}_report'
        register_report_str = f'App.register_report({self.entity_underscore}_report)'

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

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

        # Registering report
        data.insert(data.index(')\n') + 1, register_report_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):

        files = ['__init__.py', 'report.py']
        filepath = os.path.join(paths.reports, self.entity_underscore)

        if not os.path.exists(filepath):
            self.add_report_import()
        
        for file in files: 
            self.create_file(
                filename=file,
                filepath=filepath,
                template_resource=templates
            )

Classes

class ReportCreator (report_id: 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 ReportCreator(BaseCreator):

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


    def add_report_import(self):

        app_init_path = "./app/__init__.py"

        import_report_str = f'from app.reports.{self.entity_underscore} import {self.entity_underscore}_report'
        register_report_str = f'App.register_report({self.entity_underscore}_report)'

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

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

        # Registering report
        data.insert(data.index(')\n') + 1, register_report_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):

        files = ['__init__.py', 'report.py']
        filepath = os.path.join(paths.reports, self.entity_underscore)

        if not os.path.exists(filepath):
            self.add_report_import()
        
        for file in files: 
            self.create_file(
                filename=file,
                filepath=filepath,
                template_resource=templates
            )

Ancestors

Methods

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

    app_init_path = "./app/__init__.py"

    import_report_str = f'from app.reports.{self.entity_underscore} import {self.entity_underscore}_report'
    register_report_str = f'App.register_report({self.entity_underscore}_report)'

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

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

    # Registering report
    data.insert(data.index(')\n') + 1, register_report_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):

    files = ['__init__.py', 'report.py']
    filepath = os.path.join(paths.reports, self.entity_underscore)

    if not os.path.exists(filepath):
        self.add_report_import()
    
    for file in files: 
        self.create_file(
            filename=file,
            filepath=filepath,
            template_resource=templates
        )

Inherited members

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

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

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

Class variables

var reports : str
var root : str