def test01(self): f = dbfreader.DBFFile(os.path.join(dataPath, "NAT.DBF"), codepage="cp850") ae = self.assertEqual ae(f.get_version(), "dBASE III") ae(f.has_memo(), False) ae(str(f.lastUpdate), "2003-01-22") ae(f.get_record_count(), 14) ae(f.get_record_len(), 94) #print get_fields(f) ae( get_fields(f), """\ IDNAT: Character (3) NAME: Character (40) INTRA: Character (3) IDLNG: Character (1) IDTLF: Character (1) TELPREFIX: Character (6) TVAPREFIX: Character (3) IDDEV: Character (3) TVAPICT: Character (25) ATTRIB: Character (5) IDREG: Character (1) ISOCODE: Character (2)""") s = "\n".join([rec["NAME"].strip() for rec in f.fetchall()]) #print s ae( s, u"""\ Belgien Luxemburg Deutschland Niederlande Frankreich Großbritannien Italien Irland Spanien Portugal Schweiz Österreich Dänemark United States of America""")
def load_dbf(tableName, load): fn = os.path.join(settings.SITE.legacy_data_path, '%s.DBF' % tableName) f = dbfreader.DBFFile(fn, codepage="cp850") logger.info("Loading %d records from %s...", len(f), fn) f.open() for dbfrow in f: i = load(dbfrow) if i is not None: i = settings.TIM2LINO_LOCAL(tableName, i) if i is not None: try_full_clean(i) yield i #~ try: #~ i.save() #~ except Exception,e: #~ dblogger.warning("Failed to save %s from %s : %s",obj2str(i),dbfrow,e) #~ dblogger.exception(e) f.close()
def load_dbf(self, tableName, row2obj=None): if row2obj is None: row2obj = getattr(self, 'load_' + tableName[-3:].lower()) fn = self.dbpath if self.archive_name is not None: if tableName in self.archived_tables: fn = os.path.join(fn, self.archive_name) fn = os.path.join(fn, tableName) fn += dd.plugins.tim2lino.dbf_table_ext if dd.plugins.tim2lino.use_dbf_py: dd.logger.info("Loading %s...", fn) import dbf # http://pypi.python.org/pypi/dbf/ # table = dbf.Table(fn) table = dbf.Table(fn, codepage=self.codepage) # table.use_deleted = False table.open() # print table.structure() dd.logger.info("Loading %d records from %s (%s)...", len(table), fn, table.codepage) for record in table: if not dbf.is_deleted(record): try: yield row2obj(record) except Exception as e: dd.logger.warning( "Failed to load record %s from %s : %s", record, tableName, e) # i = row2obj(record) # if i is not None: # yield settings.TIM2LINO_LOCAL(tableName, i) table.close() elif dd.plugins.tim2lino.use_dbfread: dd.logger.info("Loading readonly %s...", fn) from dbfread import DBF dbf = DBF(fn) for record in dbf: d = { f.name.lower() : record[f.name] for f in dbf.fields} d = AttrDict(d) try: yield row2obj(d) except Exception as e: dd.logger.warning( "Failed to load record %s from %s : %s", record, tableName, e) else: f = dbfreader.DBFFile(fn, codepage="cp850") dd.logger.info("Loading %d records from %s...", len(f), fn) f.open(deleted=True) # must set deleted=True and then filter them out myself # because big tables can raise # RuntimeError: maximum recursion depth exceeded in cmp for dbfrow in f: if not dbfrow.deleted(): try: i = row2obj(dbfrow) if i is not None: yield settings.TIM2LINO_LOCAL(tableName, i) except Exception as e: traceback.print_exc(e) dd.logger.warning("Failed to load record %s : %s", dbfrow, e) f.close() self.after_load(tableName)
def test02(self): f = dbfreader.DBFFile(os.path.join(dataPath, "PAR.DBF"), codepage="cp850") ae = self.assertEqual ae(f.has_memo(), True) ae(f.get_version(), "dBASE III+ with memo") ae(f.get_record_count(), 48) ae(f.get_record_len(), 879) s = get_fields(f) ae( s, """\ IDPAR: Character (6) IDGEN: Character (6) FIRME: Character (35) NAME2: Character (35) RUE: Character (35) CP: Character (8) IDPRT: Character (1) PAYS: Character (3) TEL: Character (18) FAX: Character (18) COMPTE1: Character (47) NOTVA: Character (18) COMPTE3: Character (47) IDPGP: Character (2) DEBIT: Character (10) CREDIT: Character (10) ATTRIB: Character (5) IDMFC: Character (3) LANGUE: Character (1) PROF: Character (4) CODE1: Character (12) CODE2: Character (12) CODE3: Character (12) DATCREA: Date (8) IDREG: Character (1) ALLO: Character (35) NB1: Character (60) NB2: Character (60) IDDEV: Character (3) MEMO: Memo field (10) COMPTE2: Character (47) RUENUM: Character (4) RUEBTE: Character (6) MVPDATE: Date (8) VORNAME: Character (20) EMAIL: Character (250) GSM: Character (18)""") fmt = "|%(IDPAR)s|%(FIRME)s|" rows = f.fetchall() s = "\n".join([fmt % rec for rec in rows[10:15]]) #print s ae( s, u"""\ |000008|Ausdemwald| |000012|M�ller AG| |000013|Bodard| |000014|Mendelssohn GmbH| |000015|INTERMOBIL s.a.|""") norbert = rows[10] ae(norbert['FIRME'].strip(), "Ausdemwald") ae(norbert['IDPAR'], "000008") ae( norbert['MEMO'], u"""\ Das ist der Memotext zu Norbert Ausdemwald (IdPar 000008). Hier ist eine zweite Zeile. Auf der vierten Zeile kommen weiche Zeilensprünge (ASCII 141) hinzu, die von memoedit() automatisch eingefügt werden, wenn eine Zeile länger als der Texteditor ist. Hinter "von" und "der" müsste jeweils ein ASCII 141 sein. Und jetzt ist Schluss. Ohne Leerzeile hinter dem Ausrufezeichen!""")