Project

General

Profile

« Previous | Next » 

Revision 89

Added support for name XML function. Added error handling for empty rows.

View differences:

scripts/lib/xml_func.py
19 19

  
20 20
def date(items):
21 21
    items = dict(items)
22
    return '-'.join([items['year'], items.get('month', '1'),
22
    return ' '.join([items['year'], items.get('month', '1'),
23 23
        items.get('day', '1')])
24 24

  
25
def name(items):
26
    items = dict(items)
27
    return ' '.join([items['first'], items['last']])
28

  
25 29
# Function names must start with _ to avoid collisions with real tags
26 30
# Functions have take arguments (doc, node)
27
funcs = {'_range': range_, '_avg': avg, '_date': date}
31
funcs = {'_range': range_, '_avg': avg, '_date': date, '_name': name}
28 32

  
29 33
def process(doc, node=None):
30 34
    if node == None: node = doc.documentElement
scripts/lib/sql.py
24 24

  
25 25
class NullValueException(ExceptionWithColumn): pass
26 26

  
27
class EmptyRowException(DbException): pass
28

  
27 29
def check_name(name):
28 30
    if re.search(r'\W', name) != None: raise NameException('Name "'+name
29 31
        +'" may contain only alphanumeric characters and _')
......
64 66
        else: cond_ += '='
65 67
        cond_ += ' %s'
66 68
        return cond_
67
    return run_query(db, 'SELECT '+', '.join(fields)+' FROM '+table+' WHERE '
68
        +' AND '.join(map(cond, conds.iteritems())), conds.values())
69
    query = 'SELECT '+', '.join(fields)+' FROM '+table
70
    if conds != {}:
71
        query += ' WHERE '+' AND '.join(map(cond, conds.iteritems()))
72
    return run_query(db, query, conds.values())
69 73

  
70 74
def insert(db, table, row):
71 75
    check_name(table)
72 76
    cols = row.keys()
73 77
    map(check_name, cols)
74
    return run_query(db, 'INSERT INTO '+table+' ('+', '.join(cols)
75
        +') VALUES ('+', '.join(['%s']*len(cols))+')', row.values())
78
    query = 'INSERT INTO '+table
79
    if row != {}: query += ' ('+', '.join(cols)+') VALUES ('\
80
        +', '.join(['%s']*len(cols))+')'
81
    else: query += ' DEFAULT VALUES'
82
    return run_query(db, query, row.values())
76 83

  
77 84
def last_insert_id(db): return value(run_query(db, 'SELECT lastval()'))
78 85

  
......
93 100
    return col(run_query(db, 'SELECT * FROM '+table+' LIMIT 0'), 0)
94 101

  
95 102
def get(db, table, row, pkey, create=False, row_ct_ref=None):
103
    if row == []: raise EmptyRowException(table) # nothing to insert/filter by
96 104
    try: return value(select(db, table, [pkey], row))
97 105
    except StopIteration:
98 106
        if not create: raise
mappings/VegX-VegBank.organisms.csv
26 26
"/*ID->/*s/individualOrganism/*sID->/*s/taxonNameUsageConcept->/*s/taxonDetermination[*ID]/partyWithRole[role=identifier]/*ID->/parties/party/individualName/{givenName,surName}","/taxonImportance/stemCount/stemLocation/taxonInterpretation[PLANTCONCEPT_ID=]/*_ID/party/{givenName,middleName,surName}"
27 27
"/*ID->/*s/individualOrganism/*sID->/*s/taxonNameUsageConcept/authorCode","/taxonImportance/stemCount/stemLocation/taxonInterpretation[PLANTCONCEPT_ID=]/collectionNumber"
28 28
"/*ID->/*s/individualOrganism/*sID->/*s/taxonNameUsageConcept/note/text","/taxonImportance/stemCount/stemLocation/taxonInterpretation[PLANTCONCEPT_ID=]/notes"
29
"/*ID->/*s/individualOrganism/*sID->/*s/taxonNameUsageConcept/partyWithRole/*ID->/parties/party/organizationName/_name/firstName","/taxonImportance/stemCount/stemLocation/taxonInterpretation[PLANTCONCEPT_ID=]/museum_ID/party/OrganizationName/_name/firstName"
30
"/*ID->/*s/individualOrganism/*sID->/*s/taxonNameUsageConcept/partyWithRole/*ID->/parties/party/organizationName/_name/lastName","/taxonImportance/stemCount/stemLocation/taxonInterpretation[PLANTCONCEPT_ID=]/museum_ID/party/OrganizationName/_name/lastName"
29
"/*ID->/*s/individualOrganism/*sID->/*s/taxonNameUsageConcept/partyWithRole/*ID->/parties/party/organizationName/_name/first","/taxonImportance/stemCount/stemLocation/taxonInterpretation[PLANTCONCEPT_ID=]/museum_ID/party/OrganizationName/_name/first"
30
"/*ID->/*s/individualOrganism/*sID->/*s/taxonNameUsageConcept/partyWithRole/*ID->/parties/party/organizationName/_name/last","/taxonImportance/stemCount/stemLocation/taxonInterpretation[PLANTCONCEPT_ID=]/museum_ID/party/OrganizationName/_name/last"
31 31
"/*ID->/*s/individualOrganism/*sID->/*s/taxonNameUsageConcept/voucher","/{taxonImportance/stemCount/stemLocation/taxonInterpretation[PLANTCONCEPT_ID=]/museumAccessionNumber,*_ID/observation/*_ID/plot/authorPlotCode}"
32 32
"/*ID->/*s/individualOrganism/identificationLabel","/taxonImportance/stemCount/stemLocation/stemCode"
33 33
"/*ID->/*s/individualOrganism/simpleUserdefined[name=sex]/value","/taxonImportance/stemCount/stemLocation/taxonInterpretation[PLANTCONCEPT_ID=]/definedValue[@fkey=tableRecord_ID,userDefined[tableName=taxonInterpretation,userDefinedName=sex]]/definedValue"
mappings/NYBG-VegX.organisms.csv
3 3
":1","/simpleUserdefined[name=count]/value"
4 4
"key",
5 5
"DateLastModified",
6
"InstitutionCode","/*ID->/*s/individualOrganism/*sID->/*s/taxonNameUsageConcept/partyWithRole/*ID->/parties/party/organizationName/_name/firstName"
7
"CollectionCode","/*ID->/*s/individualOrganism/*sID->/*s/taxonNameUsageConcept/partyWithRole/*ID->/parties/party/organizationName/_name/lastName"
6
"InstitutionCode","/*ID->/*s/individualOrganism/*sID->/*s/taxonNameUsageConcept/partyWithRole/*ID->/parties/party/organizationName/_name/first"
7
"CollectionCode","/*ID->/*s/individualOrganism/*sID->/*s/taxonNameUsageConcept/partyWithRole/*ID->/parties/party/organizationName/_name/last"
8 8
"CatalogNumber",
9 9
"ScientificName","/*ID->/*s/individualOrganism/*sID->/*s/taxonNameUsageConcept->/*s/taxonDetermination[*ID]/*s/taxonRelationshipAssertion(/assertion/*ID->/*s/taxonName)/Simple"
10 10
"BasisOfRecord",
mappings/joins/NYBG-VegBank.organisms.csv
1 1
NYBG,VegBank:/taxonObservation
2 2
:0,/*_ID/observation/*_ID/plot/confidentialityStatus
3 3
:1,/taxonImportance/*/stemCount
4
InstitutionCode,/taxonImportance/stemCount/stemLocation/taxonInterpretation[PLANTCONCEPT_ID=]/museum_ID/party/OrganizationName/_name/firstName
5
CollectionCode,/taxonImportance/stemCount/stemLocation/taxonInterpretation[PLANTCONCEPT_ID=]/museum_ID/party/OrganizationName/_name/lastName
4
InstitutionCode,/taxonImportance/stemCount/stemLocation/taxonInterpretation[PLANTCONCEPT_ID=]/museum_ID/party/OrganizationName/_name/first
5
CollectionCode,/taxonImportance/stemCount/stemLocation/taxonInterpretation[PLANTCONCEPT_ID=]/museum_ID/party/OrganizationName/_name/last
6 6
ScientificName,/taxonImportance/stemCount/stemLocation/taxonInterpretation(/*_ID/plantConcept[plantStatus/plantLevel=Species])/*_ID/*/plantName
7 7
Kingdom,/taxonImportance/stemCount/stemLocation/taxonInterpretation(/*_ID/plantConcept[plantStatus/plantLevel=Kingdom])/*_ID/*/plantName
8 8
Phylum,/taxonImportance/stemCount/stemLocation/taxonInterpretation(/*_ID/plantConcept[plantStatus/plantLevel=Subkingdom])/*_ID/*/plantName

Also available in: Unified diff