Here are the examples of the python api sqlalchemy.engine.url.URL taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
20 Examples
3
Example 1
Project: aiohttp-devtools Source File: main.py
def pg_dsn(db_settings: dict) -> str:
"""
:param db_settings: dict of connection settings, see SETTINGS_STRUCTURE for definition
:return: DSN url suitable for sqlalchemy and aiopg.
"""
return str(URL(
database=db_settings['name'],
password=db_settings['password'],
host=db_settings['host'],
port=db_settings['port'],
username=db_settings['user'],
drivername='postgres',
))
3
Example 2
def create_url(self, superuser=False):
kwargs = {}
if superuser:
kwargs['username'] = self.superuser
else:
kwargs['username'] = self.user
kwargs['database'] = self.name
if self.host is not None:
kwargs['host'] = self.host
if self.port is not None:
kwargs['port'] = self.port
if self.password is not None:
kwargs['password'] = self.password
return URL('postgresql', **kwargs)
3
Example 3
def get_url():
"""
Return the URL to be used with engine creation based on configuration
"""
used = Profile.get_used()
return URL(**dict(used))
3
Example 4
def get_session(args):
if args.db_path is None:
data_dir = xdg.BaseDirectory.save_data_path(APP_NAME)
args.db_path = os.path.join(data_dir, 'db.sqlite')
url = sqlalchemy.engine.url.URL('sqlite', database=args.db_path)
engine = sqlalchemy.engine.create_engine(
url, echo=args.verbose_sql, poolclass=SingletonThreadPool)
sqlalchemy.event.listen(engine, 'connect', sql_setup)
upgrade_schema(engine)
Session = sessionmaker(bind=engine)
sess = Session()
return sess
3
Example 5
def get_db_connection(self):
url = sqlalchemy.engine.url.URL(drivername=self.drivername, username=self.username or None,
password=self.password or None, host=self.host or None, port=self.port or None, database=self.database)
#Sqlalchemy doesn't seem to let us specify dialect in URL, I guess we have to hack it in??
s_url = str(url)
if self.dialect:
drivername,the_rest = str(url).split('://')
s_url = drivername + '+' + self.dialect + '://' + the_rest
engine = sqlalchemy.create_engine(s_url)
return engine.connect()
3
Example 6
def application(environ, start_response):
""" Handler for WSGI appications. Assume that it does not run threaded."""
dba = URL('postgresql', username=config.defaults.DB_USER,
database=config.defaults.DB_NAME,
password=config.defaults.DB_PASSWORD)
cherrypy.thread_data.conn = create_engine(dba, echo=False).connect()
setup_site(environ['WMT_CONFIG'], script_name=environ['SCRIPT_NAME'], debug=False)
cherrypy.config.update({'log.wsgi' : True, 'log.screen' : False})
globals()['application'] = cherrypy.tree
return cherrypy.tree(environ, start_response)
3
Example 7
def prepare(options):
dba = URL('postgresql', username=options.username,
password=options.password, database=options.database)
engine = create_engine(dba, echo=options.echo_sql)
""" Creates the necessary indices on a new DB."""
engine.execute("CREATE INDEX idx_relation_member ON relation_members USING btree (member_id, member_type)")
#engine.execute("idx_nodes_tags ON nodes USING GIN(tags)")
#engine.execute("idx_ways_tags ON ways USING GIN(tags)")
#engine.execute("idx_relations_tags ON relations USING GIN(tags)")
engine.execute("ANALYSE")
engine.execute("CREATE EXTENSION pg_trgm")
0
Example 8
Project: ripozo-oasis Source File: cli_commands.py
@click.command()
@click.argument('database_uri', required=False)
@click.option('-p', '--port', type=int, help='The port of the database that you wish to expose')
@click.option('-h', '--host', type=str, help='The data base host e.g. "localhost"')
@click.option('-d', '--dialect', type=str, help='The database dialect e.g. "mysql" or "postgres"')
@click.option('--driver', type=str, help='The database driver to use e.g. "psycopg2" or "pg8000"')
@click.option('-n', '--name', type=str, help='The database name')
@click.option('-u', '--user', type=str, help='The database user')
@click.option('-p', '--password', type=str, help='The database user\'s password if necessary')
@click.option('--debug', is_flag=True, help='A flag to run the application in debug mode')
@click.option('--app-port', type=int, help='The port to run the application on')
def auto_ripozo_db(app_port, debug, password, user, name, driver, dialect, host, port, database_uri):
"""
Creates and starts a ReSTful API from a database. Full CRUD+L (Create,
Retrieve, Update, Delete, and List) is available for every model in the
database. Additionally, the application is completely HATEOAS with
full urls pointing to related objects.
See the SQLAlchemy docuementation on
`Engine Configuration <http://docs.sqlalchemy.org/en/rel_1_0/core/engines.html>`_
for more details on constructing a datbase uri.
Either the database_uri argument is required or host, port, name and dialect are
required at a minimum.
"""
if not database_uri:
dialect = '{0}+{1}'.format(dialect, driver) if driver else dialect
database_uri = URL(dialect, username=user, password=password, host=host,
port=port, database=name)
try:
app = create_app(database_uri)
except ImportError:
traceback.print_exc()
print()
print("It appears there was an import error. Typically,"
" this is because you are missing the driver. Simply "
"pip install the driver you prefer for your database and "
"try again.")
print("For example, for MySQL `pip install MySQL-python` or "
"for PostGreSQL: `pip install psycopg2`. ")
print("Check out this link for more details: "
"http://docs.sqlalchemy.org/en/rel_1_0/core/engines.html")
print()
else:
app.run(debug=debug, port=app_port)
0
Example 9
Project: VisTrails Source File: init.py
def compute(self):
url = URL(drivername=self.get_input('protocol'),
username=self.force_get_input('user', None),
password=self.force_get_input('password', None),
host=self.force_get_input('host', None),
port=self.force_get_input('port', None),
database=self.get_input('db_name'))
try:
engine = create_engine(url)
except ImportError, e:
driver = url.drivername
installed = False
if driver == 'sqlite':
raise ModuleError(self,
"Python was built without sqlite3 support")
elif (driver == 'mysql' or
driver == 'drizzle'): # drizzle is a variant of MySQL
installed = install({
'pip': 'mysql-python',
'linux-debian': 'python-mysqldb',
'linux-ubuntu': 'python-mysqldb',
'linux-fedora': 'MySQL-python'})
elif (driver == 'postgresql' or
driver == 'postgre'): # deprecated alias
installed = install({
'pip': 'psycopg2',
'linux-debian':'python-psycopg2',
'linux-ubuntu':'python-psycopg2',
'linux-fedora':'python-psycopg2'})
elif driver == 'firebird':
installed = install({
'pip': 'fdb',
'linux-fedora':'python-fdb'})
elif driver == 'mssql' or driver == 'sybase':
installed = install({
'pip': 'pyodbc',
'linux-debian':'python-pyodbc',
'linux-ubuntu':'python-pyodbc',
'linux-fedora':'pyodbc'})
elif driver == 'oracle':
installed = install({
'pip': 'cx_Oracle'})
else:
raise ModuleError(self,
"SQLAlchemy couldn't connect: %s" %
debug.format_exception(e))
if not installed:
raise ModuleError(self,
"Failed to install required driver")
try:
engine = create_engine(url)
except Exception, e:
raise ModuleError(self,
"Couldn't connect to the database: %s" %
debug.format_exception(e))
except SQLAlchemyError:
# This is NoSuchModuleError in newer versions of SQLAlchemy but we
# want compatibility here
raise ModuleError(
self,
"SQLAlchemy has no support for protocol %r -- are you "
"sure you spelled that correctly?" % url.drivername)
self.set_output('connection', engine.connect())
0
Example 10
Project: query Source File: core.py
def __init__(self, drivername=None, database=None,
host=None, port=None,
password=None, username=None,
use_env_vars=True, demo=False):
"""
Initialize and test the connection.
Kwargs:
drivername (str): Drivername passed to sqlalchemy.
database (str): Name of the database.
host (str): IP address for the host to connect to.
port (int): Port to connect on.
username (str): Username for the database.
password (str): Optionally specify a password. Defaults to None,
which prompts the user for a password using getpass.
use_env_vars (bool): Use environmental variables if specified?
Returns:
engine: The sqlalchemy database engine.
Raises:
OperationalError
"""
# Demo mode w/ included dummy database
if demo:
drivername = "sqlite"
database = os.path.join(
os.path.split(os.path.abspath(query.__file__))[0],
"sample_data/Chinook_Sqlite.sqlite")
use_env_vars = False
# Check if the host, port. or database name options are overwritten
# by environmental variables
environ_driver = os.environ.get('QUERY_DB_DRIVER')
environ_host = os.environ.get('QUERY_DB_HOST')
environ_port = os.environ.get('QUERY_DB_PORT')
environ_name = os.environ.get('QUERY_DB_NAME')
if environ_driver is not None and use_env_vars:
drivername = environ_driver
if environ_host is not None and use_env_vars:
host = environ_host
if environ_port is not None and use_env_vars:
port = environ_port
if environ_name is not None and use_env_vars:
database = environ_name
# Note: This will require the user's terminal to be open. In the
# case of IPython QtConsole or Notebook, this will be the terminal
# from which the kernel was launched
if password is None and drivername != "sqlite": # sqlite does not support pwds
password = os.environ.get('QUERY_DB_PASS')
if password is None:
if pd.core.common.in_ipnb():
# Display a somewhat obnoxious warning to the user
try:
from IPython.display import display, HTML
display(HTML(GETPASS_USE_WARNING))
except ImportError:
pass
password = getpass.getpass(
"Please enter the %s server password:" % drivername)
# Connection
url = sqlalchemy.engine.url.URL(
drivername=drivername,
username=username,
password=password,
host=host,
port=port,
database=database)
engine = sqlalchemy.create_engine(url)
# Tests the connection
with engine.begin():
pass
# Set the engine ane metadata
self._engine = engine
self._summary_info = []
self._set_metadata()
# Finally, set some pretty printing params
# (schema diagram setup to go here)
self._db_name = database.split("/")[-1].split(":")[0]
self._summary_info = pd.DataFrame(self._summary_info,
columns=["Table", "Primary Key(s)",
"# of Columns", "# of Column Types"])
self._html = df_to_html(self._summary_info, "%s Database Summary" % self._db_name,
bold=True)
0
Example 11
Project: ipython-sql Source File: parse.py
def parse(cell, config):
parts = [part.strip() for part in cell.split(None, 1)]
if not parts:
return {'connection': '', 'sql': ''}
if parts[0].startswith('[') and parts[0].endswith(']'):
section = parts[0].lstrip('[').rstrip(']')
parser = CP.ConfigParser()
parser.read(config.dsn_filename)
cfg_dict = dict(parser.items(section))
connection = str(URL(**cfg_dict))
sql = parts[1] if len(parts) > 1 else ''
elif '@' in parts[0] or '://' in parts[0]:
connection = parts[0]
if len(parts) > 1:
sql = parts[1]
else:
sql = ''
else:
connection = ''
sql = cell
return {'connection': connection.strip(),
'sql': sql.strip()}
0
Example 12
Project: alchemist Source File: _engine.py
@utils.memoize
def __getitem__(self, name):
if 'DATABASES' not in settings:
raise exceptions.ImproperlyConfigured(
'DATABASES not configured in project settings.')
if name not in settings['DATABASES']:
raise exceptions.ImproperlyConfigured(
'%r not present in DATABASES configuration.' % name)
config = settings['DATABASES'][name]
if isinstance(config, six.string_types):
url = make_url(config)
options = {}
else:
config = dict(map(lambda i: (i[0].lower(), i[1]), config.items()))
options = config.get('options', {})
url = URL(
config['engine'],
username=config.get('username', config.get('user')),
password=config.get('password', config.get('pass')),
host=config.get('hostname', config.get('host')),
port=config.get('port'),
database=config.get('name', config.get('database')))
# If alchemist is invoked by a test runner we should switch to using
# testing databases.
if settings['TESTING']:
if url.drivername.startswith('sqlite'):
# Switch to using an in-memory database for sqlite.
url.database = ':memory:'
else:
# Switch to using a named testing database for other dialects.
ident = threading.current_thread().ident
url.database = 'test_%s_%s' % (url.database, ident)
# Apply MySQL hacks to make MySQL play nice.
pool_size = None
pool_recycle = None
if url.drivername.startswith('mysql'):
pool_size = 10
pool_recycle = 7200
# Get "global" options for the database engine.
pool_size = settings.get('DATABASE_POOL_SIZE', pool_size)
if pool_size:
options.setdefault('pool_size', pool_size)
pool_recycle = settings.get('DATABASE_POOL_RECYCLE', pool_recycle)
if pool_recycle:
options.setdefault('pool_recycle', pool_recycle)
pool_timeout = settings.get('DATABASE_POOL_TIMEOUT')
if pool_timeout:
options.setdefault('pool_timeout', pool_timeout)
# Forward configuration to sqlalchemy and create the engine.
engine = sa.create_engine(url, **options)
if settings["DEBUG"]:
# Create a no-op listener if we're in debug mode.
from sqlalchemy.event import listen
listen(engine, "after_cursor_execute", lambda *a, **kw: None)
# Return the created engine.
return engine
0
Example 13
Project: PyDev.Debugger Source File: sql.py
def __init__(self, url, creator = None):
"""
Connect to the database using the given connection URL.
The current implementation uses SQLAlchemy and so it will support
whatever database said module supports.
@type url: str
@param url:
URL that specifies the database to connect to.
Some examples:
- Opening an SQLite file:
C{dao = CrashDAO("sqlite:///C:\\some\\path\\database.sqlite")}
- Connecting to a locally installed SQL Express database:
C{dao = CrashDAO("mssql://.\\SQLEXPRESS/Crashes?trusted_connection=yes")}
- Connecting to a MySQL database running locally, using the
C{oursql} library, authenticating as the "winappdbg" user with
no password:
C{dao = CrashDAO("mysql+oursql://winappdbg@localhost/Crashes")}
- Connecting to a Postgres database running locally,
authenticating with user and password:
C{dao = CrashDAO("postgresql://winappdbg:winappdbg@localhost/Crashes")}
For more information see the C{SQLAlchemy} docuementation online:
U{http://docs.sqlalchemy.org/en/latest/core/engines.html}
Note that in all dialects except for SQLite the database
must already exist. The tables schema, however, is created
automatically when connecting for the first time.
To create the database in MSSQL, you can use the
U{SQLCMD<http://msdn.microsoft.com/en-us/library/ms180944.aspx>}
command::
sqlcmd -Q "CREATE DATABASE Crashes"
In MySQL you can use something like the following::
mysql -u root -e "CREATE DATABASE Crashes;"
And in Postgres::
createdb Crashes -h localhost -U winappdbg -p winappdbg -O winappdbg
Some small changes to the schema may be tolerated (for example,
increasing the maximum length of string columns, or adding new
columns with default values). Of course, it's best to test it
first before making changes in a live database. This all depends
very much on the SQLAlchemy version you're using, but it's best
to use the latest version always.
@type creator: callable
@param creator: (Optional) Callback function that creates the SQL
database connection.
Normally it's not necessary to use this argument. However in some
odd cases you may need to customize the database connection.
"""
# Parse the connection URL.
parsed_url = URL(url)
schema = parsed_url.drivername
if '+' in schema:
dialect, driver = schema.split('+')
else:
dialect, driver = schema, 'base'
dialect = dialect.strip().lower()
driver = driver.strip()
# Prepare the database engine arguments.
arguments = {'echo' : self._echo}
if dialect == 'sqlite':
arguments['module'] = sqlite3.dbapi2
arguments['listeners'] = [_SQLitePatch()]
if creator is not None:
arguments['creator'] = creator
# Load the database engine.
engine = create_engine(url, **arguments)
# Create a new session.
session = self._new_session(bind = engine)
# Create the required tables if they don't exist.
BaseDTO.metadata.create_all(engine)
# TODO: create a dialect specific index on the "signature" column.
# Set the instance properties.
self._url = parsed_url
self._driver = driver
self._dialect = dialect
self._session = session
0
Example 14
def start(self):
dba = URL('postgresql', **self.db_params)
self.sa_engine = sa.create_engine(dba, echo=False)
cherrypy.engine.subscribe('start_thread', self.db_connect)
0
Example 15
def __init__(self, driver, user, password, dbname, host,
url_list, report_filename, make_report, be_quiet,
force, web_user, web_password, compressed_dir=None,
backend=None, offset=0):
# If no "--compressed-dir" parameter is set, use default
if compressed_dir is None:
compressed_dir = COMPRESSED_DIR
self.mail_parser = MailArchiveAnalyzer()
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.WARN)
drv = url.URL(driver, user, password, host, database=dbname)
engine = create_engine(drv, encoding='utf8', convert_unicode=True)
Database.create_tables(engine, checkfirst=True)
Session = sessionmaker()
Session.configure(bind=engine)
session = Session()
self.db = Database()
self.db.set_session(session)
# User and password to make login in case the archives
# are set to private
self.web_user = web_user
self.web_password = web_password
# Don't show messages when retrieveing and analyzing files
self.be_quiet = be_quiet
# Force to download and parse any link found in the given URL
self.force = force
# URLs or local files to be analyzed
self.url_list = url_list
self.backend = backend
self.offset = offset
self.__check_mlstats_dirs(compressed_dir)
total_messages = 0
stored_messages = 0
non_parsed = 0
for url_ml in url_list:
t, s, np = self.__analyze_mailing_list(url_ml, compressed_dir)
total_messages += t
stored_messages += s
non_parsed += np
self.__print_output("%d messages analyzed" % total_messages)
self.__print_output("%d messages stored in database %s" %
(stored_messages, dbname))
self.__print_output("%d messages ignored by the parser" % non_parsed)
difference = total_messages - stored_messages
if difference == 0 and non_parsed == 0:
self.__print_output("INFO: Everything seems to be ok.")
if difference > 0:
self.__print_output("WARNING: Some messages were parsed but "
"not stored")
if non_parsed > 0:
self.__print_output("WARNING: Some messages were ignored by "
"the parser (probably because they were "
"ill formed messages)")
if make_report:
report = Report()
report.set_session(session)
report.print_brief_report(report_filename=report_filename)
session.close()
0
Example 16
def setUp(self):
self.driver = os.getenv('DB') or 'sqlite'
dbname = ':memory:' if self.driver == 'sqlite' else DB_NAME
# Ask for DBUSER and DBPASSWORD in case of running the unit test
# in a different setting (local). 'None' is enough for Travis,
# which is what os.getenv() returns if the variable is undefined.
user = os.getenv('DBUSER')
password = os.getenv('DBPASSWORD')
host = None
drv = URL(self.driver, user, password, host, database=dbname)
self.engine = create_engine(drv, encoding='utf8', convert_unicode=True)
Base.metadata.create_all(self.engine, checkfirst=True)
Session = sessionmaker()
Session.configure(bind=self.engine)
self.connection = self.engine.connect()
# begin a non-ORM transaction
self.transaction = self.connection.begin()
self.session = Session(bind=self.connection)
self.setup_mailing_lists()
self.setup_compressed_files()
0
Example 17
Project: zine Source File: database.py
def create_engine(uri, relative_to=None, debug=False):
"""Create a new engine. This works a bit like SQLAlchemy's
`create_engine` with the difference that it automaticaly set's MySQL
engines to 'utf-8', and paths for SQLite are relative to the path
provided as `relative_to`.
Furthermore the engine is created with `convert_unicode` by default.
"""
# special case sqlite. We want nicer urls for that one.
if uri.startswith('sqlite:'):
match = _sqlite_re.match(uri)
if match is None:
raise ArgumentError('Could not parse rfc1738 URL')
database, query = match.groups()
if database is None:
database = ':memory:'
elif relative_to is not None:
database = path.join(relative_to, database)
if query:
query = url_decode(query).to_dict()
else:
query = {}
info = URL('sqlite', database=database, query=query)
else:
info = make_url(uri)
# if mysql is the database engine and no connection encoding is
# provided we set it to utf-8
if info.drivername == 'mysql':
info.query.setdefault('charset', 'utf8')
options = {'convert_unicode': True}
# alternative pool sizes / recycle settings and more. These are
# interpreter wide and not from the config for the following reasons:
#
# - system administrators can set it independently from the webserver
# configuration via SetEnv and friends.
# - this setting is deployment dependent should not affect a development
# server for the same instance or a development shell
for key in 'pool_size', 'pool_recycle', 'pool_timeout':
value = os.environ.get('ZINE_DATABASE_' + key.upper())
if value is not None:
options[key] = int(value)
# if debugging is enabled, hook the ConnectionDebugProxy in
if debug:
options['proxy'] = ConnectionDebugProxy()
return sqlalchemy.create_engine(info, **options)
0
Example 18
Project: torngas Source File: dbalchemy.py
@classmethod
def parser_engines(cls):
connections = settings.DATABASE_CONNECTION
engines = {}
for connection_name, connection_item in connections.items():
if connection_name in engines:
raise ConfigError('conn:%s ,has already exist.' % connection_name)
engines[connection_name] = {
'kwargs': connection_item.get('kwargs', {}),
'master': [],
'slaves': [],
}
connections_str = connection_item['connections']
for conn in connections_str:
dburl = url.URL(drivername=conn['DRIVER']
, username=conn['UID']
, password=conn['PASSWD']
, host=conn['HOST']
, port=conn['PORT']
, database=conn['DATABASE']
, query=conn['QUERY'])
if conn['ROLE'] == _CONNECTION_TYPE[0]:
engines[connection_name]['master'].append(dburl)
elif conn['ROLE'] == _CONNECTION_TYPE[1]:
engines[connection_name]['slaves'].append(dburl)
else:
raise ConfigError('role %s not allowed.' % conn['ROLE'])
if not len(engines[connection_name]['master']):
raise ConfigError('conn:%s ,master connection not found.' % connection_name)
return engines
0
Example 19
Project: masakari Source File: api.py
def get_engine(rc_config):
# Connect db
conf_db_dic = rc_config.get_value('db')
"""
Possible values for db drivername is,
'drizzle',
'firebird',
'informix',
'mssql',
'mysql',
'postgresql',
'sqlite',
'oracle',
'sybase'
sqlite is only for testing..
"""
# get the drivername of the db.
# default is sqlite
drivername = conf_db_dic.get("drivername", "sqlite")
charset = conf_db_dic.get("charset")
# Todo(sampath): currently query string is only support for
# mysql and postgresql.
# need to extend the support for other dbs
if drivername is "postgresql":
query = {'client_encoding': charset}
elif drivername is "mysql":
query = {'charset': charset}
else:
query = {}
if drivername != 'sqlite':
dburl = engine.url.URL(
drivername=drivername,
database=conf_db_dic.get("name"),
username=conf_db_dic.get("user"),
password=conf_db_dic.get("passwd"),
host=conf_db_dic.get("host"),
port=conf_db_dic.get("port", None),
query=query
)
eng = create_engine(dburl)
else:
eng = create_engine('sqlite:////tmp/msakari.db', echo=True)
return eng
0
Example 20
Project: monasca-api Source File: sql_repository.py
def __init__(self):
try:
super(SQLRepository, self).__init__()
self.conf = cfg.CONF
url = None
if self.conf.mysql.database_name is not None:
settings_db = (self.conf.mysql.username,
self.conf.mysql.password,
self.conf.mysql.hostname,
self.conf.mysql.database_name)
url = make_url("mysql+pymysql://%s:%s@%s/%s" % settings_db)
else:
if self.conf.database.url is not None:
url = make_url(self.conf.database.url)
else:
database_conf = dict(self.conf.database)
if 'url' in database_conf:
del database_conf['url']
url = URL(**database_conf)
from sqlalchemy import create_engine
self._db_engine = create_engine(url)
self.metadata = MetaData()
except Exception as ex:
LOG.exception(ex)
raise exceptions.RepositoryException(ex)