Exemplo n.º 1
0
Arquivo: gen.py Projeto: IATI/CSV2IATI
def element_loop(element, indent='', doc=False, top=False):
    if not doc:
        if not top:
            attribute_loop(element, indent)

        ext = element.find("./xsd:complexType/xsd:simpleContent/xsd:extension", namespaces=namespaces)
        if ext is not None:
            if ext.attrib['base'] in ['xsd:anyURI', 'xsd:string', 'xsd:decimal']:
                print_column_info('text', indent)
            else: raise Exception, ext.attrib['base']

    def print_info(name):
        print indent+"'{0}':".format(name)
        print indent+"  datatype: 'compound'"
        print indent+"  label: '{0}'".format(string.capwords(name.replace('-', ' ')))
        print indent+"  fields:"
    children = ( element.findall('xsd:complexType/xsd:choice/xsd:element', namespaces=namespaces)
        + element.findall("xsd:complexType/xsd:all/xsd:element", namespaces=namespaces) )
    if top:
        children = sorted(children, key=lambda x: order.key(x.attrib['ref']))
    for child in children:
        a = child.attrib
        if 'name' in a:
            if not doc:
                print_info(a['name'])
            element_loop(child, indent+'    ',doc=doc)
        else:
            if not a['ref'] in blacklisted_elements:
                if not doc:
                    print_info(a['ref'])
                get_element(a['ref'], indent+'    ',doc=doc)
Exemplo n.º 2
0
def get_csv_data(m, o, character_encoding, csvdata, dir, multiple_fields=[]):
    # m contains the mapping data
    # o contains the organisation data

    ordered_m = [ x[0] for x in
        sorted(m.items(),
               key=lambda x: order.key(x[1]['iati-field']))
        ]

    #iatidata will contain all of the data from the CSV file plus the mapping
    iatidata = []
    if (multiple_fields):
        # for each unique iati identifier...
        for csvdata_group, csvdata_items in csvdata.items():
            # for each group, create a line of data
            linedata = []
            already_got_project_data = False
            """
            # csvdata_group is the name of the identifier
            # csvdata_items is the name of all the items within that identifier (each row in the individual spreadsheet)
            
            # loop through the items.
            # take all of the properties from the first item. Add the properties for each row for the multiple_field field.
            """
                        
            for i, line in enumerate(csvdata_items):
                # for each row in the bundle of activities...
                # this is equivalent to "for line in csvdata"
                #
                # send to get_field_data, and therefore add to fielddata, if:
                # a) it's the first row in this group, or
                # b) the iati field is equal to the multiple fields field
                try:
                    # for each dimension in the mapping file...
                    for field in ordered_m:
                        #field = the dimension in the JSON mapping file. This can be anything as long as it's unique within the JSON.
                        try:
                            iati_field = m[field]["iati-field"]
                        except KeyError:
                            type, value, tb = sys.exc_info()
                            raise Error("%s" % value.message)
                        #iati_field contains the name of the IATI field this dimension should output.
                        if ((not already_got_project_data) or (iati_field in multiple_fields)):
                            fielddata = get_field_data(iati_field, field, m, line, character_encoding)
                            if already_got_project_data:
                                keys = [ x.keys()[0] for x in linedata ]
                                keys.reverse()
                                index = len(keys) - keys.index(iati_field)
                                linedata.insert(index, fielddata)
                            elif (fielddata):
                                linedata.append(fielddata)
                except KeyError, e:
                    type, value, tb = sys.exc_info()
                    raise Error("ERROR: No such field: %s" % value.message)
                # End of this row within the activity group... got the first row
                already_got_project_data = True
            # Finished this activity group, so write the activity
            iatidata.append(linedata)
        """ 
Exemplo n.º 3
0
def element_loop(element, indent='', doc=False, top=False):
    if not doc:
        if not top:
            attribute_loop(element, indent)

        ext = element.find("./xsd:complexType/xsd:simpleContent/xsd:extension",
                           namespaces=namespaces)
        if ext is not None:
            if ext.attrib['base'] in [
                    'xsd:anyURI', 'xsd:string', 'xsd:decimal'
            ]:
                print_column_info('text', indent)
            else:
                raise Exception, ext.attrib['base']

    def print_info(name):
        print indent + "'{0}':".format(name)
        print indent + "  datatype: 'compound'"
        print indent + "  label: '{0}'".format(
            string.capwords(name.replace('-', ' ')))
        print indent + "  fields:"

    children = (element.findall('xsd:complexType/xsd:choice/xsd:element',
                                namespaces=namespaces) +
                element.findall("xsd:complexType/xsd:all/xsd:element",
                                namespaces=namespaces))
    if top:
        children = sorted(children, key=lambda x: order.key(x.attrib['ref']))
    for child in children:
        a = child.attrib
        if 'name' in a:
            if not doc:
                print_info(a['name'])
            element_loop(child, indent + '    ', doc=doc)
        else:
            if not a['ref'] in blacklisted_elements:
                if not doc:
                    print_info(a['ref'])
                get_element(a['ref'], indent + '    ', doc=doc)