version 1.8, 2007/03/29 18:31:32
|
version 1.13, 2007/05/25 15:49:40
|
Line 85 class TableColumn:
|
Line 85 class TableColumn:
|
|
|
|
|
class xml_handler: |
class xml_handler: |
|
|
def __init__(self,options): |
def __init__(self,options): |
''' |
"""SAX handler to import FileMaker XML file (FMPXMLRESULT format) into the table. |
SAX handler to import FileMaker XML file (FMPXMLRESULT format) into the table. |
|
@param options: dict of options |
@param options: dict of options |
@param options.dsn: database connection string |
@param options.dsn: database connection string |
@param options.table: name of the table the xml shall be imported into |
@param options.table: name of the table the xml shall be imported into |
Line 100 class xml_handler:
|
Line 98 class xml_handler:
|
@param options.keep_fields: (optional) don't add fields to SQL database |
@param options.keep_fields: (optional) don't add fields to SQL database |
@param options.ascii_db: (optional) assume ascii encoding in db |
@param options.ascii_db: (optional) assume ascii encoding in db |
@param options.replace_table: (optional) delete and re-insert data |
@param options.replace_table: (optional) delete and re-insert data |
''' |
""" |
|
|
# set up parser |
# set up parser |
self.event = None |
self.event = None |
self.top_dispatcher = { |
self.top_dispatcher = { |
Line 115 class xml_handler:
|
Line 114 class xml_handler:
|
self.db = self.dbCon.cursor() |
self.db = self.dbCon.cursor() |
assert self.db, "AIIEE no db cursor for %s!!"%options.dsn |
assert self.db, "AIIEE no db cursor for %s!!"%options.dsn |
|
|
self.table = options.table |
self.table = getattr(options,"table",None) |
self.update_fields = options.update_fields |
self.update_fields = getattr(options,"update_fields",None) |
self.id_field = options.id_field |
self.id_field = getattr(options,"id_field",None) |
self.sync_mode = options.sync_mode |
self.sync_mode = getattr(options,"sync_mode",None) |
self.lc_names = options.lc_names |
self.lc_names = getattr(options,"lc_names",None) |
self.keep_fields = options.keep_fields |
self.keep_fields = getattr(options,"keep_fields",None) |
self.ascii_db = options.ascii_db |
self.ascii_db = getattr(options,"ascii_db",None) |
self.replace_table = options.replace_table |
self.replace_table = getattr(options,"replace_table",None) |
self.backup_table = options.backup_table |
self.backup_table = getattr(options,"backup_table",None) |
|
|
logging.debug("dsn: "+repr(options.dsn)) |
logging.debug("dsn: "+repr(getattr(options,"dsn",None))) |
logging.debug("table: "+repr(self.table)) |
logging.debug("table: "+repr(self.table)) |
logging.debug("update_fields: "+repr(self.update_fields)) |
logging.debug("update_fields: "+repr(self.update_fields)) |
logging.debug("id_field: "+repr(self.id_field)) |
logging.debug("id_field: "+repr(self.id_field)) |
Line 230 class xml_handler:
|
Line 229 class xml_handler:
|
# check fields to update |
# check fields to update |
if self.update_fields is None: |
if self.update_fields is None: |
if self.keep_fields: |
if self.keep_fields: |
# update existing fields |
# update all existing fields from sql (when they are in the xml file) |
self.update_fields = self.sql_fields |
self.update_fields = {} |
|
for f in self.sql_fields.keys(): |
|
if self.sql_field_map.has_key(f): |
|
xf = self.sql_field_map[f] |
|
self.update_fields[f] = self.xml_field_map[xf] |
|
|
else: |
else: |
# update all fields |
# update all fields |
Line 450 class xml_handler:
|
Line 453 class xml_handler:
|
return |
return |
|
|
|
|
|
def importFMPXML(options): |
|
"""SAX handler to import FileMaker XML file (FMPXMLRESULT format) into the table. |
|
@param options: dict of options |
|
@param options.dsn: database connection string |
|
@param options.table: name of the table the xml shall be imported into |
|
@param options.filename: xmlfile filename |
|
@param options.update_fields: (optional) list of fields to update; default is to create all fields |
|
@param options.id_field: (optional) field which uniquely identifies an entry for updating purposes. |
|
@param options.sync_mode: (optional) really synchronise, i.e. delete entries not in XML file |
|
@param options.lc_names: (optional) lower case and clean up field names from XML |
|
@param options.keep_fields: (optional) don't add fields to SQL database |
|
@param options.ascii_db: (optional) assume ascii encoding in db |
|
@param options.replace_table: (optional) delete and re-insert data |
|
""" |
|
|
|
|
|
if getattr(options,'update_fields',None): |
|
uf = {} |
|
for f in options.update_fields.split(','): |
|
if f.find(':') > 0: |
|
(n,t) = f.split(':') |
|
else: |
|
n = f |
|
t = None |
|
uf[n] = TableColumn(n,t) |
|
|
|
options.update_fields = uf |
|
|
|
if getattr(options,'id_field',None) and getattr(options,'replace_table',None): |
|
logging.error("ABORT: sorry, you can't do both sync (id_field) and replace") |
|
sys.exit(1) |
|
|
|
parser = sax.make_parser() |
|
#The "consumer" is our own handler |
|
consumer = xml_handler(options) |
|
#Initialize Tenorsax with handler |
|
handler = saxtools.tenorsax(consumer) |
|
#Resulting tenorsax instance is the SAX handler |
|
parser.setContentHandler(handler) |
|
parser.setFeature(sax.handler.feature_namespaces, 1) |
|
parser.parse(options.filename) |
|
|
## |
|
## public static int main() |
|
## |
|
|
|
|
if __name__ == "__main__": |
from optparse import OptionParser |
from optparse import OptionParser |
|
|
opars = OptionParser() |
opars = OptionParser() |
Line 513 logging.basicConfig(level=loglevel,
|
Line 553 logging.basicConfig(level=loglevel,
|
format='%(asctime)s %(levelname)s %(message)s', |
format='%(asctime)s %(levelname)s %(message)s', |
datefmt='%H:%M:%S') |
datefmt='%H:%M:%S') |
|
|
update_fields = None |
importFMPXML(options) |
|
|
if options.update_fields: |
|
uf = {} |
|
for f in options.update_fields.split(','): |
|
(n,t) = f.split(':') |
|
uf[n] = TableColumn(n,t) |
|
|
|
options.update_fields = uf |
|
|
|
if options.id_field and options.replace_table: |
|
logging.error("ABORT: sorry, you can't do both sync (id_field) and replace") |
|
sys.exit(1) |
|
|
|
parser = sax.make_parser() |
|
#The "consumer" is our own handler |
|
consumer = xml_handler(options) |
|
#Initialize Tenorsax with handler |
|
handler = saxtools.tenorsax(consumer) |
|
#Resulting tenorsax instance is the SAX handler |
|
parser.setContentHandler(handler) |
|
parser.setFeature(sax.handler.feature_namespaces, 1) |
|
parser.parse(options.filename) |
|
|
|
|
|
print "DONE!" |
|