示例#1
0
def register_fast_module(location: str, module: str):
    file_name = os.path.abspath(os.path.join(location, 'main.py'))
    lines = generator.read_lines(file_name)
    # look for last line with 'import' prefix
    import_found = False
    insert_index = 0
    for index, line in enumerate(lines):
        if line.startswith('import '):
            import_found = True
        elif import_found:
            insert_index = index
            break
    lines.insert(insert_index, 'import {}'.format(module))
    lines.append(init_module_template.format(module=module))
    generator.write_lines(file_name, lines)
示例#2
0
def create_route(location: str, module: str, entity: str, entity_caption: str):
    file_name = os.path.abspath(os.path.join(location, module, 'route.py'))
    lines = generator.read_lines(file_name)
    # look for line with 'def init(' prefix
    insert_index = -1
    for index, line in enumerate(lines):
        if line.startswith('def init('):
            insert_index = index + 1
            break
    if insert_index == -1:
        raise Exception('init function not found in {}'.format(file_name))
    lines.insert(
        insert_index,
        route_template.format(entity=entity, entity_caption=entity_caption))
    generator.write_lines(file_name, lines)
def create_fast_event_handler(location: str, module: str, event: str):
    file_name = os.path.abspath(os.path.join(location, module, 'event.py'))
    lines = generator.read_lines(file_name)
    # look for line with 'def init(' prefix
    insert_index = -1
    for index, line in enumerate(lines):
        if line.startswith('def init('):
            insert_index = index + 1
            break
    if insert_index == -1:
        raise Exception('init function not found in {}'.format(file_name))
    lines.insert(
        insert_index,
        handle_event_template.format(event=event,
                                     handler='handle_event_{}'.format(
                                         re.sub(r'[^A-Za-z0-9_]+', '_',
                                                event).lower())))
    generator.write_lines(file_name, lines)