def process_converter(filename, form_xml, debug, cl_path):
    if not filename:
        return None

    if form_xml:
        form = form_xml.xpath('/CALC/FORMSET/FORM/@val')[0].upper()

    preprocessed = preprocess_calc_file(filename, debug=debug, cl_path=cl_path)

    input_stream = InputStream(preprocessed)
    tree = antrl_parse(input_stream, debug)
    xml = tree2xml(tree)
    if debug:
        print "##### parse converter: %s #####" % filename
        pretty_print(xml)
    withforms = xml.xpath('//WithForms[ID[1][@val="%s"]]' % form)
    if not withforms:
        return

    withforms = withforms[0]
    fedform = withforms.xpath("ID[2]/@val")[0].upper()
    for each in withforms.xpath(".//Assign"):
        each.set('fed', '1')
        id = each.getchildren()[0]
        id.set('val', "%s.%s" % (form, id.get('val').upper()))

        for id in each.getchildren()[1].xpath('.//ID'):
            id.set('val', "%s.%s" % (fedform, id.get('val').upper()))
            id.set('fed','1')


    #Cleanup boolean\literal assigns
    for each in withforms.xpath(".//IfStruct/Assign[Boolean|Literal]"):
        each.getparent().remove(each)

    if debug:
        print "##### withforms #####"
        pretty_print(withforms)

    form_xml.append(withforms)
Пример #2
0
    parser.add_argument('--pref', action='store', dest='pref_file',
                        help='The preferences file to use.',
                        required=False,
                        default='calc2xref.pref')

    parser.add_argument("--debug", help="Show parsing steps", default=False)
    arg = parser.parse_args()
    debug = arg.debug
    print "\n....Loading preferences from: %s" %  arg.pref_file
    with open(arg.pref_file) as settings_file:
        settings = json.load(settings_file)
    print "\n\n....Settings:"
    print json.dumps(settings,indent=4)
    print "\n\n"

    preprocessed = preprocess_calc_file(settings['calc_filename'], debug=debug, cl_path=settings['cl_path'])

    print "\n\n\n"

    input_stream = InputStream(preprocessed)

    tree = antrl_parse(input_stream, debug)

    root = tree2xml(tree)

    if debug:
        print "##ParseTreeWalker##"
        pretty_print(root)

    process_converter(settings['cvt_filename'], form_xml=root, debug=debug, cl_path=settings['cl_path'])