Beispiel #1
0
def main():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("src", nargs="?", default=None)
    parser.add_argument("--package", default="autogenerated")
    args = parser.parse_args()

    loading.setup()
    data = loading.loadfile(args.src)
    m = go.Module()
    m.package(args.package)
    emit(data, m)
    print(m)
Beispiel #2
0
def generate_write_sheet(sheet, sheetname=None, m=None):
    m = m or go.Module()
    funcname = sheet.get('func') or 'Write{sheetname}'.format(
        sheetname=go.titlize(sheetname))

    f = sheet['attributes'][0][0]
    m.stmt("// {funcname} :".format(funcname=funcname))
    with m.func(funcname,
                *[' '.join(pair) for pair in sheet['attributes']],
                return_=sheet.get('returns') or 'error'):
        for i, row in enumerate(sheet['sheet']):
            m.stmt('row{i} := {sheet}.AddRow()'.format(i=i, sheet=f))
            for j, cell in enumerate(row):
                m.stmt('cell{i}{j} := row{i}.AddCell()'.format(i=i, j=j))

                action, value, theme = None, None, None
                if len(cell) == 1:
                    value, = cell
                elif len(cell) == 2:
                    action, value = cell
                else:
                    action, value, theme, *_ = cell

                raw_action, typ = split_action(action)
                if raw_action == C:
                    if typ == str:
                        m.stmt('cell{i}{j}.SetString("{value}")'.format(
                            i=i, j=j, value=value))
                    elif typ == float:
                        m.stmt('cell{i}{j}.SetFloat({value})'.format(
                            i=i, j=j, value=value))
                    else:
                        raise ValueError(raw_action, typ)
                elif raw_action == A:
                    if typ == str:
                        m.stmt('cell{i}{j}.SetString({value})'.format(
                            i=i, j=j, value=value))
                    elif typ == float:
                        m.stmt('cell{i}{j}.SetFloat({value})'.format(
                            i=i, j=j, value=value))
                    else:
                        raise ValueError(raw_action, typ)
                else:
                    raise ValueError('invalid action: {} in {}'.format(
                        action, cell))
            m.sep()
        # todo: return value
        m.return_('nil')
    return m
Beispiel #3
0
def generate_all(filename, workbook, m=None):
    m = m or go.Module()
    m.package("main")
    with m.import_group() as im:
        im("fmt")
        im("github.com/tealeg/xlsx")
    m.sep()

    with m.func("main"):
        m.stmt('err := WriteFile("{filename}")'.format(filename=filename))
        with m.if_('err != nil'):
            m.stmt('panic(err)')

    generate_write_workbook('WriteFile', workbook, m=m)
    for i, sheet in enumerate(workbook):
        sheetname = sheet.get('name') or 'sheet{i}'.format(i=i)
        generate_write_sheet(sheet, sheetname=sheetname, m=m)
    return m
Beispiel #4
0
def main():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("src",
                        nargs="?",
                        default=sys.stdin,
                        type=argparse.FileType("r"))
    parser.add_argument("--dst",
                        default=sys.stdout,
                        type=argparse.FileType("w"))
    parser.add_argument("--ref", required=True)
    parser.add_argument("--package", default=None)
    args = parser.parse_args()

    doc = loading.load(args.src)
    m = go.Module()
    if args.package:
        m.package(args.package)
    emitter = Emitter(doc, m=m)
    emitter.emit_ref(args.ref)
    print(m)
Beispiel #5
0
def generate_write_workbook(funcname, workbook, m=None):
    m = m or go.Module()
    m.stmt("// {funcname} :".format(funcname=funcname))
    with m.func(funcname, 'name string', return_='error'):
        m.stmt("// todo: set font")
        m.stmt("file := xlsx.NewFile()")
        for i, sheet in enumerate(workbook):
            sheetname = sheet.get('name') or 'sheet{i}'.format(i=i)
            m.stmt('sheet{i}, err := file.AddSheet("{sheetname}")'.format(
                i=i, sheetname=sheetname))
            with m.if_('err != nil'):
                m.return_('err')

            writefunc = sheet.get('func') or 'Write{sheetname}'.format(
                sheetname=go.titlize(sheetname))
            args = ['sheet{i}'.format(i=i)]
            m.stmt('// todo: arguments')
            with m.if_('err := {writefunc}({args}); err != nil'.format(
                    writefunc=writefunc, args=', '.join(args))):
                m.return_('err')
            m.sep()
        m.return_('file.Save(name)')
    return m
Beispiel #6
0
 def __init__(self, doc, m=None, mapping=None):
     self.doc = doc
     self.m = m or go.Module()
     self.defined = {}  # ref -> name, todo: conflict
     self.nullable = {}  # ref
     self.type_mapping = mapping or self.DEFAULT_MAPPING