All pastes #495574 Raw Edit

Wolever

public python v1 · immutable
·published 2007-05-18 20:35 UTC
rendered paste body
# Copyright 2007 University of Toronto## Please view LICENSE for additional licensing information.from drproject.core import Componentfrom elixir import metadata, objectstore, create_allclass DatabaseManager(Component):    def create_tables(self):        create_all()    def shutdown(self):        pass    def connect(self, dbString=None):        # Create and populate the database        if not dbString:            dbString = self.config.get('trac', 'database')            # for sqlite, if it's a relative path, make it relative            # to the path of to the environment folder            if dbString.startswith('sqlite://'):                if not dbString.startswith('sqlite:////'): # not absolute                    firstPart = 'sqlite:///'                    dbString = '%s%s/%s' % (firstPart, self.env.path, dbString[len(firstPart):])        metadata.connect(dbString)        #~ metadata.engine.echo = True    def begin_transaction(self, debug=False):        metadata.engine.echo = debug        return objectstore.create_transaction()    # FIXME: possibly some of this old code, the part dealing with sqlite paths,    # should be preserved since our new version above is untested (but perhaps    # so is _get_connector?) and doesn't explicitly deal with :memory:    #~ def _get_connector(self):        #~ scheme, args = _parse_db_str(self.env.config.get('trac', 'database'))        #~ if scheme == 'sqlite':            #~ # Special case for SQLite to support a path relative to the            #~ # environment directory            #~ if args['path'] != ':memory:' and \                   #~ not args['path'].startswith('/'):                #~ args['path'] = os.path.join(self.env.path,                                            #~ args['path'].lstrip('/'))        #~ return connector, args#~ def _parse_db_str(db_str):    #~ scheme, rest = db_str.split(':', 1)    #~ if not rest.startswith('/'):        #~ if scheme == 'sqlite':            #~ # Support for relative and in-memory SQLite connection strings            #~ host = None            #~ path = rest        #~ else:            #~ raise DrProjectError('Database connection string %s must start '                                 #~ 'with scheme:/' % db_str)    #~ else:        #~ path = None        #~ if rest.startswith('/') and not rest.startswith('//'):            #~ host = None            #~ rest = rest[1:]        #~ elif rest.startswith('///'):            #~ host = None            #~ rest = rest[3:]            #~ if scheme == 'sqlite':                #~ path = rest        #~ else:            #~ rest = rest[2:]            #~ if rest.find('/') == -1:                #~ host = rest                #~ rest = ''            #~ else:                #~ host, rest = rest.split('/', 1)    #~ if host and host.find('@') != -1:        #~ user, host = host.split('@', 1)        #~ if user.find(':') != -1:            #~ user, password = user.split(':', 1)        #~ else:            #~ password = None    #~ else:        #~ user = password = None    #~ if host and host.find(':') != -1:        #~ host, port = host.split(':')        #~ port = int(port)    #~ else:        #~ port = None    #~ if not path:        #~ path = '/' + rest    #~ if os.name == 'nt':        #~ # Support local paths containing drive letters on Win32        #~ if len(rest) > 1 and rest[1] == '|':            #~ path = "%s:%s" % (rest[0], rest[2:])    #~ params = {}    #~ if path.find('?') != -1:        #~ path, qs = path.split('?', 1)        #~ qs = qs.split('&')        #~ for param in qs:            #~ name, value = param.split('=', 1)            #~ value = urllib.unquote(value)            #~ params[name] = value    #~ args = zip(('user', 'password', 'host', 'port', 'path', 'params'),               #~ (user, password, host, port, path, params))    #~ return scheme, dict([(key, value) for key, value in args if value])