# Path: xclib/utf8.py
# def unutf8(u, opts='strict'):
#     if opts == 'illegal':
#         try:
#             return u.decode('utf-8', 'strict')
#         except UnicodeError:
#             dec = u.decode('utf-8', 'ignore')
#             logging.error('Illegal UTF-8 sequence: %r' % dec)
#             sys.stderr.write('Illegal UTF-8 sequence: %r\n' % dec)
#             traceback.print_exc()
#             return 'illegal-utf8-sequence-' + dec
#     else:
#         return u.decode('utf-8', opts)


import bsddb3
import sqlite3
import os
import time
import logging
from datetime import datetime
from xclib.utf8 import unutf8
            self.db_create_cache(self.conn,
                    olddb = args.cache_db, upgrade = True)
            self.db_upgrade_roster(args.shared_roster_db)

    def db_upgrade_domain(self, olddb):
        logging.debug('Upgrading domain from %s' % olddb)
        try:
            self.conn.execute('''CREATE TABLE domains
                     (xmppdomain TEXT PRIMARY KEY,
                      authsecret TEXT,
                      authurl    TEXT,
                      authdomain TEXT,
                      regcontact TEXT,
                      regfirst   TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                      reglatest  TIMESTAMP DEFAULT CURRENT_TIMESTAMP)''')
        except sqlite3.OperationalError as e:
            logging.warning('Cannot create `domains` table; maybe multiple processes started in parallel? %s' % str(e))
            # Try to get out of the way of a parallel updater
            time.sleep(1)
            # Someone else already created the table; he probably also
            # migrated it
            return
        try:
            if olddb is None:
                return
            elif isinstance(olddb, str):
                db = bsddb3.hashopen(olddb, 'r')
            else: # dict
                db = olddb
            for k,v in db.items():

# Path: xclib/utf8.py
# def unutf8(u, opts='strict'):
#     if opts == 'illegal':
#         try:
#             return u.decode('utf-8', 'strict')
#         except UnicodeError:
#             dec = u.decode('utf-8', 'ignore')
#             logging.error('Illegal UTF-8 sequence: %r' % dec)
#             sys.stderr.write('Illegal UTF-8 sequence: %r\n' % dec)
#             traceback.print_exc()
#             return 'illegal-utf8-sequence-' + dec
#     else:
#         return u.decode('utf-8', opts)


# Path: xclib/db.py
import bsddb3
import sqlite3
import os
import time
import logging
from datetime import datetime
from xclib.utf8 import unutf8

            self.db_create_cache(self.conn,
                    olddb = args.cache_db, upgrade = True)
            self.db_upgrade_roster(args.shared_roster_db)

    def db_upgrade_domain(self, olddb):
        logging.debug('Upgrading domain from %s' % olddb)
        try:
            self.conn.execute('''CREATE TABLE domains
                     (xmppdomain TEXT PRIMARY KEY,
                      authsecret TEXT,
                      authurl    TEXT,
                      authdomain TEXT,
                      regcontact TEXT,
                      regfirst   TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                      reglatest  TIMESTAMP DEFAULT CURRENT_TIMESTAMP)''')
        except sqlite3.OperationalError as e:
            logging.warning('Cannot create `domains` table; maybe multiple processes started in parallel? %s' % str(e))
            # Try to get out of the way of a parallel updater
            time.sleep(1)
            # Someone else already created the table; he probably also
            # migrated it
            return
        try:
            if olddb is None:
                return
            elif isinstance(olddb, str):
                db = bsddb3.hashopen(olddb, 'r')
            else: # dict
                db = olddb
            for k,v in db.items():

                k = unutf8(k, 'illegal')