--- ZSQLExtend/importFMPXML.py 2007/04/02 09:48:13 1.9 +++ ZSQLExtend/importFMPXML.py 2007/05/25 15:49:40 1.13 @@ -229,10 +229,13 @@ class xml_handler: # check fields to update if self.update_fields is None: if self.keep_fields: - # update existing fields - self.update_fields = self.sql_fields - - + # update all existing fields from sql (when they are in the xml file) + 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: # update all fields if self.lc_names: @@ -450,8 +453,48 @@ class xml_handler: 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) + if __name__ == "__main__": from optparse import OptionParser @@ -512,43 +555,6 @@ if __name__ == "__main__": importFMPXML(options) -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 - """ - update_fields = None - - if getattr(options,'update_fields',None): - uf = {} - for f in options.update_fields.split(','): - (n,t) = f.split(':') - 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) - +