Exemple #1
0
def generate_query_resolver_function(qspec: GQLQuerySpec) -> str:

    j2env = jinja2.Environment()
    template_mgr = common.JinjaTemplateManager(j2env)
    template = j2env.from_string(QUERY_RESOLVER_FUNCTION_TEMPLATE)

    return template.render(query_spec=qspec)
Exemple #2
0
    def do_save(self, cmd_args):
        '''Saves all created Kinesis stream specs to the designated output .tf file.
        '''

        if not len(self.stream_specs):
            should_save = cli.InputPrompt(
                'No Kinesis streams defined. Are you sure (Y/n)?', 'y').show()
            if should_save != 'y':
                print('\nCancelling save.\n')
                return

        if os.path.isfile(self.output_file):
            print('designated output file already exists.')
            return

        if not len(self.stream_specs):
            output_data = '# Intentionally empty file. No Kinesis streams defined for project %s.' % self.project_name
        else:
            j2env = jinja2.Environment()
            template_mgr = common.JinjaTemplateManager(j2env)
            streamdef_template = j2env.from_string(KINESIS_STREAM_TEMPLATE)
            output_data = streamdef_template.render(streams=self.stream_specs)

        with open(self.output_file, 'w') as f:
            f.write(output_data)

        print('\nSaved Terraform resources to output file %s.\n' %
              self.output_file)
Exemple #3
0
    def do_show(self, cmd_args):

        if not len(self.datasource_specs):
            print(
                '\nCannot generate a datamap config without one or more lookup datasources.\n'
            )
            return

        if not len(self.map_specs):
            print(
                '\nCannot generate a datamap config without one or more maps.\n'
            )
            return

        global_tbl = named_tuple_array_to_dict(self.globals,
                                               key_name='name',
                                               value_name='value')
        template_data = {
            'project_home': global_tbl['project_home'],
            'datasource_module': global_tbl['datasource_module'],
            'service_module': global_tbl['service_module'],
            'datasources': self.datasource_specs,
            'map_specs': self.map_specs
        }

        j2env = jinja2.Environment()
        template_mgr = common.JinjaTemplateManager(j2env)
        map_template = j2env.from_string(MAP_CONFIG_TEMPLATE)
        output_data = map_template.render(**template_data)
        print('_______________________\n')
        print('YAML datamap config:\n')
        print(output_data)
        print('_______________________\n')
Exemple #4
0
def generate_mutation_resolver_function(mspec: GQLMutationSpec) -> str:

    j2env = jinja2.Environment()
    template_mgr = common.JinjaTemplateManager(j2env)
    template = j2env.from_string(MUTATION_RESOLVER_FUNCTION_TEMPLATE)

    return template.render(mutation_spec=mspec)
Exemple #5
0
def main(args):
    j2env = jinja2.Environment()
    template_mgr = common.JinjaTemplateManager(j2env)
    map_template = j2env.from_string(MAP_CONFIG_TEMPLATE)

    datasource_specs = []
    map_specs = []

    prompt = MakeMapPrompt()
    prompt.prompt = 'm2> '
    prompt.cmdloop('starting mkmap in interactive mode...')

    datasource_specs = []
    #datasource_specs.append(DatasourceSpec(name='amc', klass='SVODLookupSource'))

    source_datafile = args['<datafile>']
    mapspec = create_default_map_from_csv_file(source_datafile, 'foobar',
                                               'amc', '\t')
    map_specs.append(mapspec)

    template_data = {
        'project_home': '$APOLLO_HOME',
        'lookup_source_module': 'apollo_datasources',
        'service_module': 'apollo_services',
        'datasources': datasource_specs,
        'map_specs': map_specs
    }

    print(map_template.render(**template_data))
Exemple #6
0
def generate_handler_source(yaml_config: dict) -> str:

    j2env = jinja2.Environment()
    template_mgr = common.JinjaTemplateManager(j2env)
    template = j2env.from_string(HANDLER_MODULE_TEMPLATE)

    qspecs = load_query_specs(yaml_config)
    return template.render(query_specs=qspecs)
Exemple #7
0
def generate_app_source(schema_filename: str, yaml_config: dict) -> str:

    project_conf = GProjectBuilder.build_project(schema_filename, yaml_config)

    j2env = jinja2.Environment()
    template_mgr = common.JinjaTemplateManager(j2env)
    template = j2env.from_string(MAIN_APP_TEMPLATE)

    return template.render(project=project_conf)
Exemple #8
0
    def do_show(self, cmd_args):
        '''Shows the created Kinesis stream specs.
        '''

        j2env = jinja2.Environment()
        template_mgr = common.JinjaTemplateManager(j2env)
        streamdef_template = j2env.from_string(KINESIS_STREAM_TEMPLATE)
        output_data = streamdef_template.render(streams=self.stream_specs)
        print(output_data)
Exemple #9
0
def generate_resolver_source(yaml_config: dict) -> str:

    j2env = jinja2.Environment()
    template_mgr = common.JinjaTemplateManager(j2env)
    template = j2env.from_string(RESOLVER_MODULE_TEMPLATE)

    qspecs = load_query_specs(yaml_config)
    mspecs = load_mutation_specs(yaml_config)

    return template.render(query_specs=qspecs, mutation_specs=mspecs)
Exemple #10
0
def main(args):

    j2env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.getcwd()))
    template_mgr = common.JinjaTemplateManager(j2env)

    launcher_data = dict(tdx_home=os.getenv('METL_HOME'),
                         s3_bucket='bucket_name')

    launcher_template = template_mgr.get_template(TEMPLATE_FILE)
    output_data = launcher_template.render(launcher_data)

    print(output_data)
def main(args):
    yaml_config = common.read_config_file(args['<initfile>'])
    if args['--channel'] == False:
        print('\n'.join(yaml_config['channels'].keys()))
        return 0

    event_channel = args['<event_channel>']
    if not yaml_config['channels'].get(event_channel):
        raise eavesdroppr.NoSuchEventChannel(event_channel)

    channel_config = yaml_config['channels'][event_channel]

    operation = channel_config['db_operation']
    if not operation in SUPPORTED_DB_OPS:
        raise eavesdroppr.UnsupportedDBOperation(operation)

    table_name = channel_config['db_table_name']
    db_schema = channel_config.get('db_schema') or 'public'
    proc_name = channel_config.get(
        'db_proc_name') or '%s_%s_notify' % (table_name, operation.lower())
    trigger_name = channel_config.get(
        'db_trigger_name') or 'trg_%s_%s' % (table_name, operation.lower())
    source_fields = channel_config['payload_fields']

    j2env = jinja2.Environment()
    template_mgr = common.JinjaTemplateManager(j2env)
    json_func_template = j2env.from_string(JSON_BUILD_FUNC_TEMPLATE)
    json_func = json_func_template.render(payload_fields=source_fields)

    pk_field = channel_config['pk_field_name']
    pk_type = channel_config['pk_field_type']

    if args['--proc']:
        print(
            PROC_TEMPLATE.format(schema=db_schema,
                                 pk_field_name=pk_field,
                                 pk_field_type=pk_type,
                                 channel_name=event_channel,
                                 json_build_func=json_func))

    elif args['--trigger']:
        print(
            TRIGGER_TEMPLATE.format(schema=db_schema,
                                    table_name=table_name,
                                    trigger_name=trigger_name,
                                    db_proc_name=proc_name,
                                    db_op=operation))
Exemple #12
0
def create_gql_schema(yaml_config: dict) -> str:

    j2env = jinja2.Environment()
    template_mgr = common.JinjaTemplateManager(j2env)

    query_template = j2env.from_string(GQL_QUERY_TEMPLATE)
    query_schema = query_template.render(
        query_specs=load_query_specs(yaml_config))

    mutation_template = j2env.from_string(GQL_MUTATION_TEMPLATE)
    mutation_schema = mutation_template.render(
        mutation_specs=load_mutation_specs(yaml_config))

    types_template = j2env.from_string(GQL_TYPE_TEMPLATE)
    types_schema = types_template.render(
        type_specs=load_type_specs(yaml_config))

    return ''.join([query_schema, mutation_schema, types_schema])
Exemple #13
0
    def do_save(self, cmd_args):
        '''Saves the current configuration to the designated output file.
        '''

        if os.path.isfile(self.output_file):
            prompt_text = 'output file "%s" already exists. Overwrite (Y/n)?' % self.output_file
            should_overwrite = cli.InputPrompt(prompt_text, 'y').show()
            if should_overwrite == 'n':
                return

        j2env = jinja2.Environment()
        template_mgr = common.JinjaTemplateManager(j2env)
        var_template = j2env.from_string(PROJECT_VAR_TEMPLATE)
        output_data = var_template.render(project_vars=self.project_var_specs)

        with open(self.output_file, 'w') as f:
            f.write(output_data)

        print('\n+++ Terraform project vars written to %s.\n' %
              self.output_file)
Exemple #14
0
def main(args):

    j2env = jinja2.Environment()
    template_mgr = common.JinjaTemplateManager(j2env)
    map_template = j2env.from_string(MAP_CONFIG_TEMPLATE)

    source_filename = None
    output_filename = None

    if args['--from'] == True:
        source_filename = args['<datafile>']

    if args['--to'] == True:
        output_filename = args['<output_file>']

    prompt = MakeMapCLI('mkmap',
                        initial_sourcefile=source_filename,
                        output_file=output_filename)
    prompt.cmdloop('starting mkmap in interactive mode...')
    '''
Exemple #15
0
    def do_save(self, cmd_args):
        '''Save all datamaps to a file.
        '''

        if not len(self.map_specs):
            print(
                '\nNo datamaps registered. Create one or more datamaps first.\n'
            )
            return

        output_filename = cli.InputPrompt('output filename').show()
        global_tbl = named_tuple_array_to_dict(self.globals,
                                               key_name='name',
                                               value_name='value')
        template_data = {
            'project_home': global_tbl['project_home'],
            'datasource_module': global_tbl['datasource_module'],
            'service_module': global_tbl['service_module'],
            'datasources': self.datasource_specs,
            'map_specs': self.map_specs
        }

        j2env = jinja2.Environment()
        template_mgr = common.JinjaTemplateManager(j2env)
        map_template = j2env.from_string(MAP_CONFIG_TEMPLATE)

        output_data = map_template.render(**template_data)
        #print(yaml.dump(data))
        should_save = cli.InputPrompt(
            'Save this datamap configuration to %s (Y/n)?' % output_filename,
            'y').show()
        if should_save == 'y':
            with open(output_filename, 'w') as f:
                #f.write(map_template.render(**template_data))
                #yaml.dump(output_data, output_data, f, default_flow_style=False)
                f.write(output_data)
                print('\nSaved datamap config to output file %s.\n' %
                      output_filename)
Exemple #16
0
def generate_code(event_channel, channel_config, **kwargs):
    operation = channel_config['db_operation']
    if not operation in SUPPORTED_DB_OPS:
        raise UnsupportedDBOperation(operation)

    table_name = channel_config['db_table_name']
    db_schema = channel_config.get('db_schema') or 'public'
    procedure_name = channel_config.get('db_proc_name') or default_proc_name(
        table_name, operation)
    trigger_name = channel_config.get(
        'db_trigger_name') or default_trigger_name(table_name, operation)
    source_fields = channel_config['payload_fields']
    primary_key_field = channel_config['pk_field_name']
    primary_key_type = channel_config['pk_field_type']

    j2env = jinja2.Environment()
    template_mgr = common.JinjaTemplateManager(j2env)
    json_func_template = j2env.from_string(JSON_BUILD_FUNC_TEMPLATE)
    json_func = json_func_template.render(payload_fields=source_fields,
                                          pk_field=primary_key_field)

    if kwargs['procedure']:
        print(
            PROC_TEMPLATE.format(schema=db_schema,
                                 proc_name=procedure_name,
                                 pk_field_name=primary_key_field,
                                 pk_field_type=primary_key_type,
                                 channel_name=event_channel,
                                 json_build_func=json_func))

    elif kwargs['trigger']:
        print(
            TRIGGER_TEMPLATE.format(schema=db_schema,
                                    table_name=table_name,
                                    trigger_name=trigger_name,
                                    db_proc_name=procedure_name,
                                    db_op=operation))
def main(args):
    #print(common.jsonpretty(args))

    credential_type = 'unsupported'
    if args.get('ssh'):
        credential_type = 'ssh'
    elif args.get('aws'):
        credential_type = 'aws'

    j2env = jinja2.Environment()
    template_mgr = common.JinjaTemplateManager(j2env)
    keyring_varfile_template = j2env.from_string(KEYRING_TEMPLATE)

    keyfile_dir = os.path.expanduser(args['--dir'])
    keyfiles = find_keyfiles_in_dir(keyfile_dir)

    # TODO: add an interactive portion to allow users to label keys
    key_table = {}
    index = 0
    for kfile in keyfiles:
        label = kfile[0:-4]
        key_table[label] = kfile
        index += 1

    key_data = load_keys(key_table, keyfile_dir)
    keypair_records = []
    for label, key in key_data.items():
        keypair_records.append(KeypairRecord(name=label, public_key=key))

    print(keyring_varfile_template.render(keypairs=keypair_records))

    with open('keyfiles.json', 'w') as f:
        keymap = {}
        keymap['location'] = keyfile_dir
        keymap['public_keys'] = key_data
        f.write(json.dumps(keymap))
Exemple #18
0
    def render(self):
        j2env = jinja2.Environment()
        template_mgr = common.JinjaTemplateManager(j2env)
        j2template = j2env.from_string(self.template)

        return j2template.render(**self.params)
Exemple #19
0
 def render(self):
     j2env = jinja2.Environment()
     template_mgr = common.JinjaTemplateManager(j2env)
     j2template = j2env.from_string(self.template)
     return j2template.render(element=self.element, attrs=self._attributes)
Exemple #20
0
def generate_handler_function(name: str) -> str:

    j2env = jinja2.Environment()
    template_mgr = common.JinjaTemplateManager(j2env)
    template = j2env.from_string(HANDLER_FUNCTION_TEMPLATE)
    return template.render(handler_name=name)