Beispiel #1
0
def generate_genserver(directive, functions, records, folder):
    """
    Generate genserver files from functions and records lists.
    """
    d = directive
    params = dict(module_name=d.value, author=get_directive_option(d, 'author'),\
                      author_email=get_directive_option(d, 'author-email'), \
                      description=get_directive_option(d, 'doc'), \
                      date=datetime.now().strftime('%d.%m.%Y'), \
                      export_functions=get_export_functions(functions), \
                      proxy_functions=get_proxy_functions(functions), \
                      handle_call_functions=get_handle_call_functions(functions))
    params.update(ERLANG_MODULE_OPTIONS)
    if records:                 # make hrl file
        header_file = '%s.hrl' % d.value
        params['header_files'] = '-include("%s").' % header_file
        header_file = os.path.join(folder, header_file)
        records_string = u''
        for r in records:
            if len(r.params) == 0:
                print 'Warning: record %s has no fields, skip it...' % make_erlang_func(r.name)
                continue

            records_string += FUNC_DOC_HL + u'%% ' + r.doc + u'\n'
            for p in r.params:
                records_string += u'%%%% %s : %s\n' % (make_erlang_var(p.name), p.doc)
            records_string += FUNC_DOC_HL
            records_string += u'-record(%s, {%s}).\n\n' % \
                (make_erlang_func(r.name), u', '.join([make_erlang_var(x.name) for x in r.params]))

        if records_string:
            f = open(header_file, 'wb')
            f.write(records_string.encode('utf-8'))
            f.close()
            print 'Create file %s' % header_file
        else:
            print 'Header file %s was not prodused. ' % header_file
            del params['header_files']

    f_name = os.path.join(folder, '%s.erl' % d.value)
    f = open(f_name, 'wb')
    unicode_string = substitute(GEN_SERVER_TEMPLATE, params)
    f.write(unicode_string.encode('utf-8'))
    f.close()
    print 'Create file %s' % f_name
    if get_directive_option(d, 'supervisor'): # make supervisor for genserver
        params['supervisor_name'] = '%s_sup' % d.value
        f_name = os.path.join(folder, '%s_sup.erl' % d.value)
        f = open(f_name, 'wb')
        unicode_string = substitute(SUPERVISOR_TEMPLATE, params)
        f.write(unicode_string.encode('utf-8'))
        f.close()
        print 'Create file %s' % f_name
Beispiel #2
0
def get_records(module_directive, folder):
    """
    Return list of :class: `Struct` objects from module_directive.
    """
    records = []
    for r in module_directive.childs:
        if r.directive == 'struct':
            s = Struct(r.value, doc=get_directive_option(r, 'doc'))
            for param in get_params(r):
                s.add_param(param)
            records.append(s)
    return records
Beispiel #3
0
def get_module_functions(module_directive):
    """
    Get list of :class: `Func` objects, from module_directive.
    """
    functions = []
    for f in module_directive.childs:
        if f.directive == 'func':
            fun = Func(f.value, doc=get_directive_option(f, 'doc'), \
                           store_optional_params_in_list=True)
            for fp in get_params(f):
                fun.add_param(fp)
            fun.returns = get_return_param(f)
            functions.append(fun)
    return functions
Beispiel #4
0
def generate(directives, filename):
    """
    Function, that generates code in Erlang
    """
    print 'Erlang generator [START]'
    options = find_options(directives, ERLANG_MODULE_OPTIONS)
    if not can_generate(options):
        raise ErlangOptionsException("Erlang can't generate template for file %s " % filename)
    e_dir = options.get_option('out_dir')
    if not options.get_option('make_lang_dir').strip().lower() in ['false', 'nil']:
        e_dir = os.path.join(e_dir, 'erlang')

    e_dir = os.path.abspath(e_dir)
    if not os.path.exists(e_dir):
        os.makedirs(e_dir)
    mod_name = ''
    for d in directives:
        if d.directive == 'module':
            if not d.value:
                raise EmptyModuleNameException('module name can\'t be empty [%s]' % filename)

            functions = get_module_functions(d)

            # create hrl file, if needed
            records = get_records(d, e_dir)

            # read module and write it down
            if get_directive_option(d, 'genserver'): # make genserver
                generate_genserver(d, functions=functions, records=records, folder=e_dir)


            elif get_directive_option(d, 'fsmserver'): # make fsm server
                pass
            else:               # just make erlang module
                pass
    print 'Erlang generator [DONE]'