예제 #1
0
def confgen(query):
    results = vendor_menu()
    temp = frdb("SELECT template FROM device_templates where uid = '%s'" %
                query)
    header = frdb("SELECT * FROM device_templates where uid = '%s'" % query)
    template = temp[0][0]
    temp = template
    env = Environment()
    ast = env.parse(template)
    vars = meta.find_undeclared_variables(ast)
    vars = list(vars)
    stringvars = ';'.join(vars)
    tform = DataForm()
    tformdata = []
    output = ' '
    tempresult = False
    nlines = False
    stringtem = ''
    paramlist = []
    dicis = []
    temout = []
    line_length = 45
    if tform.validate():
        tformdata = tform.body.data
        tformdata = tformdata.replace('\r\n', '')
        tformdata = tformdata.encode('utf-8')
        tformdata = tformdata.split('#')
        template = Template(template, trim_blocks=True, lstrip_blocks=True)
        for single in tformdata:
            paramlist.append(single.split(';'))
        for singleparam in paramlist:
            dicis.append(dict(zip(vars, singleparam)))
        for dic in dicis:
            temout.append(template.render(dic))
        stringtem = '\n\n'.join(temout)
        nlines = stringtem.count('\n') + 4
        for lines in stringtem.split('\n'):
            if line_length < len(lines):
                line_length = len(lines)
    return render_template('template.html',
                           temout=temout,
                           tform=tform,
                           tformdata=tformdata,
                           output=output,
                           temp=temp,
                           results=results,
                           vars=vars,
                           stringvars=stringvars,
                           nlines=nlines,
                           line_length=line_length,
                           stringtem=stringtem,
                           header=header)
예제 #2
0
def combo(checkbox_data):
    results = vendor_menu()
    tform = DataForm()
    buffer = TempTemplate()
    headers = TempHeader()
    longstring = TempString()
    template = ''
    i = 0
    paramlist = []
    temp_header_string = ''
    nlines = False
    line_length = False
    temout = []
    tformdata = False
    stringtem = ''
    dicis = []
    header = []
    head = []
    header_string = ''
    line_length = 45
    temp = False
    stringvars = False
    wrong_list = ["u", "[", "]", "'"]
    checkbox_data = checkbox_data.encode('utf-8')
    for sym in wrong_list:
        checkbox_data = checkbox_data.replace(sym, '')
    checkbox_data = checkbox_data.split(', ')
    checkbox_value = map(int, checkbox_data)
    for check in checkbox_value:
        one_header = frdb("SELECT * FROM device_templates where uid = '%d'" %
                          check)
        header.append(one_header[0][1:4])
    if header:
        for head in header:
            if i == (len(header)) - 1:
                for h in head:
                    header_string = header_string + str(h) + ' '
                header_string = header_string + ' '
            else:
                for h in head:
                    header_string = header_string + str(h) + ' '
                header_string = header_string + '& '
            i += 1
        header_string = header_string + 'configuration'
        headers.header_list.data = header
    if headers.header_list.data:
        temp_header = headers.header_list.data
    if header_string:
        longstring.string_list.data = header_string
    if longstring.string_list.data:
        header_string = longstring.string_list.data
    for check in checkbox_value:
        one_template = frdb(
            "SELECT template FROM device_templates where uid = '%s'" % check)
        one_template = str(one_template[0][0])
        template = template + '\n' + '!' * 40 + '\n' + one_template
    if template:
        buffer.temp_template.data = template
    if buffer.temp_template.data:  #buffer.validate():
        temp = buffer.temp_template.data
        env = Environment()
        ast = env.parse(buffer.temp_template.data)
        vars = meta.find_undeclared_variables(ast)
        vars = list(vars)
        stringvars = ';'.join(vars)
        temout = []
        if tform.validate():
            tformdata = tform.body.data
            tformdata = tformdata.replace('\r\n', '')
            tformdata = tformdata.encode('utf-8')
            tformdata = tformdata.split('#')
            template = buffer.temp_template.data
            template = Template(template, trim_blocks=True, lstrip_blocks=True)
            for single in tformdata:
                paramlist.append(single.split(';'))
            for singleparam in paramlist:
                dicis.append(dict(zip(vars, singleparam)))
            for dic in dicis:
                temout.append(template.render(dic))
            stringtem = '\n\n'.join(temout)
            nlines = stringtem.count('\n') + 4
            for lines in stringtem.split('\n'):
                if line_length < len(lines):
                    line_length = len(lines)
    return render_template(
        'combo.html',
        header_string=header_string,
        tform=tform,
        stringvars=stringvars,
        buffer=buffer,
        headers=headers,
        longstring=longstring,
        temout=temout,
        nlines=nlines,
        line_length=line_length,
        stringtem=stringtem,
        temp=temp,
        results=results,
        checkbox_data=checkbox_data,
    )