def pathify(self): """ rfc2616 says in 5.1.2: "all HTTP/1.1 servers MUST accept the absoluteURI form in requests" so if we get one, transform it to abs_path. Raises HttpReqError if the request is malformed, else returns (path, query, fragment) """ try: path, query, fragment = urisup.uri_help_split(self.path)[2:] if not path: path = "/" if path[0] != "/": raise urisup.InvalidURIError, 'path %r does not start with "/"' % path path = re.sub("^/+", "/", path, 1) if query: query = self.querylist_to_dict(query) return path, query, fragment except urisup.InvalidURIError, e: log.error("invalid URI: %s", e) raise HttpReqError(400, str(e))
def connect_by_uri(uri): """General URI syntax: postgresql://user:passwd@host:port/db NOTE: the authority and the path parts of the URI have precedence over the query part, if an argument is given in both. conv,quote_conv,cursorclass are not (yet?) allowed as complex Python objects are needed, hard to transmit within an URI... """ puri = urisup.uri_help_split(uri) #params = __dict_from_query(puri[QUERY]) params = {} if puri[AUTHORITY]: user, passwd, host, port = puri[AUTHORITY] if user: params['user'] = user if passwd: params['password'] = passwd if host: params['host'] = host if port: params['port'] = port if puri[PATH]: params['database'] = puri[PATH] if params['database'] and params['database'][0] == '/': params['database'] = params['database'][1:] #__apply_types(params, __typemap) return psycopg2.connect(**params)
def pathify(self): """ rfc2616 says in 5.1.2: "all HTTP/1.1 servers MUST accept the absoluteURI form in requests" so if we get one, transform it to abs_path. Raises HttpReqError if the request is malformed, else returns (path, query, fragment) """ try: path, query, fragment = urisup.uri_help_split(self.path)[2:] if not path: path = "/" if path[0] != "/": raise urisup.InvalidURIError( 'path %r does not start with "/"' % path) path = re.sub("^/+", "/", path, 1) if query: query = self.querylist_to_dict(query) return path, query, fragment except urisup.InvalidURIError as e: log.error("invalid URI: %s", e) raise HttpReqError(400, str(e))
def connect_by_uri(uri): puri = urisup.uri_help_split(uri) opts = __dict_from_query(puri[QUERY]) con = sqlite.connect(puri[PATH], client_encoding='utf8') if "timeout_ms" in opts: con.db.sqlite_busy_timeout(int(opts["timeout_ms"])) return con
def connect_by_uri(uri): """General URI syntax: postgres://user:password@host:port/database?opt1=val1&opt2=val2... where opt_n is in the list of options supported by PostGreSQL: host,user,password,port,database,client_encoding,unicode_results NOTE: the authority and the path parts of the URI have precedence over the query part, if an argument is given in both. Descriptions of options: file:///usr/lib/python?.?/site-packages/pyPgSQL/PgSQL.py """ puri = urisup.uri_help_split(uri) params = __dict_from_query(puri[QUERY]) if puri[AUTHORITY]: user, password, host, port = puri[AUTHORITY] if user: params['user'] = user if password: params['password'] = password if host: params['host'] = host if port: params['port'] = port if puri[PATH]: params['database'] = puri[PATH] if params['database'] and params['database'][0] == '/': params['database'] = params['database'][1:] __apply_types(params, __typemap) return PgSQL.connect(**params)
def connect_by_uri(uri): puri = urisup.uri_help_split(uri) opts = __dict_from_query(puri[QUERY]) con = None if "timeout_ms" in opts: con = sqlite3.connect(puri[PATH],float(opts["timeout_ms"])) else: con = sqlite3.connect(puri[PATH]) return con
def c14n_uri(uri): puri = list(urisup.uri_help_split(uri)) puri[PATH] = os.path.abspath(puri[PATH]) return uri_help_unsplit(tuple(puri))
def __init__(self, iuri): self.iuri = iuri self.ldapopj = None self.uri = None self.dbname = None try: log.info('LDAP URI requested: %r', iuri) if isinstance(iuri, unicode): ldapuri = urisup.uri_help_split(iuri.encode('utf8')) else: ldapuri = urisup.uri_help_split(iuri) uri_scheme = ldapuri[0] if uri_scheme not in ('ldap', 'ldaps'): raise NotImplementedError, 'Unknown URI scheme: %r' % uri_scheme # user, pass, host, port if ldapuri[1][0] is None: ldapuser = '' else: ldapuser = ldapuri[1][0] if ldapuri[1][1] is None: ldappass = '' else: ldappass = ldapuri[1][1] if ldapuri[1][2] is None: ldaphost = 'localhost' else: ldaphost = ldapuri[1][2] if ldapuri[1][3] is None: if uri_scheme == 'ldaps': ldapport = '636' else: ldapport = '389' else: ldapport = ldapuri[1][3] # dbname if ldapuri[2] is not None: if ldapuri[2].startswith('/'): self.dbname = ldapuri[2][1:] else: self.dbname = ldapuri[2] # bypassing the result made by urisup, since it gives bad results, i.e. for : # ldap://user:pass@host:333/dbname???(&(mail=*.com)(telephoneNumber=*)(displayName=*%Q*)) # the tuple result is : # (('??(', None), ('(mail', '*.com)(telephoneNumber=*)(displayName=*%Q*))')) # while we should be able to extract : # - attributes = '' # - scope = '' # - filter = '(&(mail=*.com)(telephoneNumber=*)(displayName=*%Q*))' # - extensions = '' dbpos = iuri.rfind(self.dbname) # ?attributes?scope?filter?extensions = 'asfe' asfe = iuri[dbpos + len(self.dbname):].split('?', 4) while len(asfe) < 5: asfe.append('') (self.base_attributes, self.base_scope, self.base_filter, self.base_extensions) = asfe[1:] self.uri = "%s://%s:%s" % (uri_scheme, ldaphost, ldapport) debuglevel = 0 self.ldapobj = ldap.initialize(self.uri, debuglevel) log.info('LDAP URI understood: %r / filter: %r', self.uri, self.base_filter) # actual cases where these options are useful and used would be # to be investigated further, since it does not look like the proper # way to add such options to the LDAP URI : # ldapquery = dict(ldapuri[3]) # if ldapquery.has_key('protocol_version'): # self.ldapobj.set_option(ldap.OPT_PROTOCOL_VERSION, # int(ldapquery.get('protocol_version'))) # if ldapquery.has_key('network_timeout'): # self.ldapobj.set_option(ldap.OPT_NETWORK_TIMEOUT, # float(ldapquery.get('network_timeout'))) self.ldapobj.set_option(ldap.OPT_TIMEOUT , 1) self.ldapobj.set_option(ldap.OPT_NETWORK_TIMEOUT, 1) self.ldapobj.simple_bind_s(ldapuser, ldappass) log.info('LDAP : simple bind done on %r %r', ldapuser, ldappass) # XXX how should we handle the starttls option ? # if uri_scheme == 'ldap' and int(ldapquery.get('tls', 0)): # self.ldapobj.start_tls_s() usetls = False # looks like you need to set this to True in some AD2008 configs if usetls: self.ldapobj.set_option(ldap.OPT_X_TLS, 1) except ldap.LDAPError, exc: log.exception('__init__: ldap.LDAPError (%r, %r, %r)', self.ldapobj, iuri, exc) self.ldapobj = None
def _get_methods_by_uri(sqluri): uri_scheme = urisup.uri_help_split(sqluri)[0] if uri_scheme not in __uri_create_methods: raise NotImplementedError('Unknown URI scheme "%s"' % str(uri_scheme)) return __uri_create_methods[uri_scheme]
def _get_methods_by_uri(sqluri): uri_scheme = urisup.uri_help_split(sqluri)[0] if uri_scheme not in __uri_create_methods: raise NotImplementedError, 'Unknown URI scheme "%s"' % str(uri_scheme) return __uri_create_methods[uri_scheme]
def set_db_backends(args, options): # pylint: disable-msg=W0613 """ POST /set_db_backends """ if 'xivo' not in args: raise HttpReqError(415, "missing option 'xivo'") else: xivodburi = list(urisup.uri_help_split(args['xivo'])) if xivodburi[0] is None or xivodburi[0].lower() not in WIZARD_XIVO_DB_ENGINES: raise HttpReqError(415, "invalid option 'xivo'") else: xivodburi[0] = xivodburi[0].lower() if WIZARD_XIVO_DB_ENGINES[xivodburi[0]]: xivodbparams = WIZARD_XIVO_DB_ENGINES[xivodburi[0]]['params'] else: xivodbparams = {} if xivodburi[3]: xivodbparams.update(dict(xivodburi[3])) if xivodbparams: xivodburi[3] = zip(xivodbparams.keys(), xivodbparams.values()) else: xivodburi[3] = None args['xivo'] = urisup.uri_help_unsplit(xivodburi) if 'ql' not in args: args['ql'] = args['xivo'] if 'ipbxengine' not in args: raise HttpReqError(415, "missing option 'ipbxengine'") elif args['ipbxengine'] not in WIZARD_IPBX_ENGINES: raise HttpReqError(415, "invalid ipbxengine: %r" % args['ipbxengine']) ipbxdbinfo = WIZARD_IPBX_ENGINES[args['ipbxengine']]['database'] if 'ipbx' not in args: raise HttpReqError(415, "missing option 'ipbx'") else: ipbxdburi = list(urisup.uri_help_split(args['ipbx'])) if ipbxdburi[0] is None or ipbxdburi[0].lower() not in ipbxdbinfo: raise HttpReqError(415, "invalid option 'ipbx'") else: ipbxdburi[0] = ipbxdburi[0].lower() if ipbxdbinfo[ipbxdburi[0]]['params']: ipbxdbparams = ipbxdbinfo[ipbxdburi[0]]['params'] else: ipbxdbparams = {} if ipbxdburi[3]: ipbxdbparams.update(dict(ipbxdburi[3])) if ipbxdbparams: ipbxdburi[3] = zip(ipbxdbparams.keys(), ipbxdbparams.values()) else: ipbxdburi[3] = None args['ipbx'] = urisup.uri_help_unsplit(ipbxdburi) if not WIZARDLOCK.acquire_read(Wdc['lock_timeout']): raise HttpReqError(503, "unable to take WIZARDLOCK for reading after %s seconds" % Wdc['lock_timeout']) try: connect = xivo_helpers.db_connect(args['xivo']) if not connect: raise HttpReqError(415, "unable to connect to 'xivo' database") connect = xivo_helpers.db_connect(args['ipbx']) if not connect: raise HttpReqError(415, "unable to connect to 'ipbx' database") merge_config_file(Wdc['agid_config_tpl_file'], Wdc['agid_config_custom_tpl_file'], Wdc['agid_config_file'], {'db': {'db_uri': args['ipbx']}}) merge_config_file(Wdc['provisioning_config_tpl_file'], Wdc['provisioning_config_custom_tpl_file'], Wdc['provisioning_config_file'], {'general': {'database_uri': args['ipbx']}}) merge_config_file(Wdc['webinterface_xivo_tpl_file'], Wdc['webinterface_xivo_custom_tpl_file'], Wdc['webinterface_xivo_file'], {'general': {'datastorage': '"%s"' % args['xivo']}}) merge_config_file(Wdc['webinterface_cti_tpl_file'], Wdc['webinterface_cti_custom_tpl_file'], Wdc['webinterface_cti_file'], {'general': {'datastorage': '"%s"' % args['xivo']}, 'queuelogger': {'datastorage': '"%s"' % args['ql']}, }) merge_config_file("%s.%s" % (Wdc['webinterface_ipbx_tpl_file'], args['ipbxengine']), "%s.%s" % (Wdc['webinterface_ipbx_custom_tpl_file'], args['ipbxengine']), Wdc['webinterface_ipbx_file'], {'general': {'datastorage': '"%s"' % args['ipbx']}}) if args['ipbxengine'] == 'asterisk': asterisk_configuration(ipbxdburi, ipbxdbinfo[ipbxdburi[0]], ipbxdbparams) finally: WIZARDLOCK.release()
def connect_by_uri(uri): """General URI syntax: mysql://user:passwd@host:port/db?opt1=val1&opt2=val2&... where opt_n is in the list of options supported by MySQLdb: host,user,passwd,db,compress,connect_timeout,read_default_file, read_default_group,unix_socket,port NOTE: the authority and the path parts of the URI have precedence over the query part, if an argument is given in both. conv,quote_conv,cursorclass are not (yet?) allowed as complex Python objects are needed, hard to transmit within an URI... See for description of options: http://dustman.net/andy/python/MySQLdb_obsolete/doc/MySQLdb-3.html#ss3.1 http://mysql-python.svn.sourceforge.net/viewvc/mysql-python/trunk/MySQLdb/doc/MySQLdb.txt?revision=438&view=markup&pathrev=438 """ puri = urisup.uri_help_split(uri) params = __dict_from_query(puri[QUERY]) if puri[AUTHORITY]: user, passwd, host, port = puri[AUTHORITY] if user: params['user'] = user if passwd: params['passwd'] = passwd if host: params['host'] = host if port: params['port'] = port if puri[PATH]: params['db'] = puri[PATH] if params['db'] and params['db'][0] == '/': params['db'] = params['db'][1:] __apply_types(params, __typemap) # The next affectation work around a bug in python-mysqldb which # happens when using an unicode charset: the conv parameter is # defaulted to the common dictionary MySQLdb.converters.conversions # when not explicitly given to the __init__() of # MySQLdb.connections.Connection, the _mysql module just store it in # the .converter member in the __init__() method of the base class # _mysql.connection, and later, back in the __init__() of # MySQLdb.connections.Connection, some children of .converter, which # are lists, are prepended by dynamically generated functions. The net # result is that every times a new Mysql connection is asked for with # no individualised conversion dictionary passed to the conv parameter, # a bunch of new functions and tuples are created, on which the process # will keep references forever, effectively leaking some memory as some # won't be used anymore after the termination of any connection. # This work around is believed to be effective because the only # references to the dynamically created conversion functions generated # by MySQLdb.connections will be in this instance-specific copy of # MySQLdb.converters.conversions. A unique reference to this copy will # be held by the connection instance, so when the latter is garbage # collected, the copied conversion dictionary is freed, and eventually # the now orphan tuples and generated functions are too. params['conv'] = CST_CONVERSIONS.copy() params['cursorclass'] = SSCursor return MySQLdb.connect(**params)