Python sqlalchemy.engine.url.URL Examples

The following are 30 code examples of sqlalchemy.engine.url.URL(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy.engine.url , or try the search function .
Example #1
Source File: test_model.py    From grimoirelab-sortinghat with GNU General Public License v3.0 8 votes vote down vote up
def __init__(self, user, password, database, host, port):
        driver = 'mysql+pymysql'

        self.url = URL(driver, user, password, host, port, database)

        # Hack to establish SSL connection (see #231)
        try:
            self._engine = create_engine(self.url, echo=True,
                                         connect_args={'ssl': {'activate': True}})
            self._engine.connect().close()
        except InternalError:
            self._engine = create_engine(self.url, echo=True)

        self._Session = sessionmaker(bind=self._engine)

        # Create the schema on the database.
        # It won't replace any existing schema
        ModelBase.metadata.create_all(self._engine) 
Example #2
Source File: strategies.py    From jbox with MIT License 8 votes vote down vote up
def create(self, name_or_url, executor, **kwargs):
        # create url.URL object
        u = url.make_url(name_or_url)

        dialect_cls = u.get_dialect()

        dialect_args = {}
        # consume dialect arguments from kwargs
        for k in util.get_cls_kwargs(dialect_cls):
            if k in kwargs:
                dialect_args[k] = kwargs.pop(k)

        # create dialect
        dialect = dialect_cls(**dialect_args)

        return MockEngineStrategy.MockConnection(dialect, executor) 
Example #3
Source File: sql_helper.py    From galaxia with Apache License 2.0 6 votes vote down vote up
def init_db(db_type, username, password, host, db, db_location):
    """
    Creates database engine
    :param db_type: database type
    :param username: database username
    :param password: database password
    :param host: database host
    :param db: database name
    :return:
    """
    global engine
    url = None
    if db_type == 'mysql':
        url = URL(db_type, username, password, host, database=db)
    elif db_type == 'sqlite':
        url = "sqlite:///" + db_location
    engine = create_engine(url) 
Example #4
Source File: new_tweet_parser.py    From hoaxy-backend with GNU General Public License v3.0 6 votes vote down vote up
def main_mulprocessing(min_id=None,
                       max_id=None,
                       window_size=5000,
                       number_of_processes=4,
                       drop_first=False):
    engine = create_engine(
        URL(**CONF['database']['connect_args']),
        pool_size=1,
        pool_recycle=CONF['database']['pool_recycle'],
        client_encoding='utf8')
    init_tables(engine, drop_first)
    try:
        manager = ParserManager(
            min_id=min_id,
            max_id=max_id,
            window_size=window_size,
            number_of_processes=number_of_processes)
        manager.start()
        manager.join()
    except (KeyboardInterrupt, SystemExit):
        logger.info('interrupt signal received')
        sys.exit(1)
    except Exception as e:
        raise e 
Example #5
Source File: strategies.py    From android_universal with MIT License 6 votes vote down vote up
def create(self, name_or_url, executor, **kwargs):
        # create url.URL object
        u = url.make_url(name_or_url)

        dialect_cls = u.get_dialect()

        dialect_args = {}
        # consume dialect arguments from kwargs
        for k in util.get_cls_kwargs(dialect_cls):
            if k in kwargs:
                dialect_args[k] = kwargs.pop(k)

        # create dialect
        dialect = dialect_cls(**dialect_args)

        return MockEngineStrategy.MockConnection(dialect, executor) 
Example #6
Source File: strategies.py    From moviegrabber with GNU General Public License v3.0 6 votes vote down vote up
def create(self, name_or_url, executor, **kwargs):
        # create url.URL object
        u = url.make_url(name_or_url)

        dialect_cls = u.get_dialect()

        dialect_args = {}
        # consume dialect arguments from kwargs
        for k in util.get_cls_kwargs(dialect_cls):
            if k in kwargs:
                dialect_args[k] = kwargs.pop(k)

        # create dialect
        dialect = dialect_cls(**dialect_args)

        return MockEngineStrategy.MockConnection(dialect, executor) 
Example #7
Source File: relational_db_api.py    From beam-nuggets with MIT License 6 votes vote down vote up
def __init__(
        self,
        drivername,
        host=None,
        port=None,
        database=None,
        username=None,
        password=None,
        create_if_missing=False,
    ):
        self.url = URL(
            drivername=drivername,
            username=username,
            password=password,
            host=host,
            port=port,
            database=database
        )
        self.create_if_missing = create_if_missing 
Example #8
Source File: strategies.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def create(self, name_or_url, executor, **kwargs):
        # create url.URL object
        u = url.make_url(name_or_url)

        dialect_cls = u.get_dialect()

        dialect_args = {}
        # consume dialect arguments from kwargs
        for k in util.get_cls_kwargs(dialect_cls):
            if k in kwargs:
                dialect_args[k] = kwargs.pop(k)

        # create dialect
        dialect = dialect_cls(**dialect_args)

        return MockEngineStrategy.MockConnection(dialect, executor) 
Example #9
Source File: strategies.py    From jarvis with GNU General Public License v2.0 6 votes vote down vote up
def create(self, name_or_url, executor, **kwargs):
        # create url.URL object
        u = url.make_url(name_or_url)

        dialect_cls = u.get_dialect()

        dialect_args = {}
        # consume dialect arguments from kwargs
        for k in util.get_cls_kwargs(dialect_cls):
            if k in kwargs:
                dialect_args[k] = kwargs.pop(k)

        # create dialect
        dialect = dialect_cls(**dialect_args)

        return MockEngineStrategy.MockConnection(dialect, executor) 
Example #10
Source File: database.py    From ATM with MIT License 6 votes vote down vote up
def __init__(self, dialect, database, username=None, password=None,
                 host=None, port=None, query=None):
        """
        Accepts configuration for a database connection, and defines SQLAlchemy
        ORM objects for all the tables in the database.
        """

        # Prepare environment for pymysql
        pymysql.install_as_MySQLdb()
        pymysql.converters.encoders[np.float64] = pymysql.converters.escape_float
        pymysql.converters.conversions = pymysql.converters.encoders.copy()
        pymysql.converters.conversions.update(pymysql.converters.decoders)

        db_url = URL(drivername=dialect, database=database, username=username,
                     password=password, host=host, port=port, query=query)
        self.engine = create_engine(db_url)
        self.session = None
        self.get_session = sessionmaker(bind=self.engine,
                                        expire_on_commit=False)

        # create ORM objects for the tables
        self._define_tables() 
Example #11
Source File: strategies.py    From planespotter with MIT License 6 votes vote down vote up
def create(self, name_or_url, executor, **kwargs):
        # create url.URL object
        u = url.make_url(name_or_url)

        dialect_cls = u.get_dialect()

        dialect_args = {}
        # consume dialect arguments from kwargs
        for k in util.get_cls_kwargs(dialect_cls):
            if k in kwargs:
                dialect_args[k] = kwargs.pop(k)

        # create dialect
        dialect = dialect_cls(**dialect_args)

        return MockEngineStrategy.MockConnection(dialect, executor) 
Example #12
Source File: strategies.py    From pyRevit with GNU General Public License v3.0 6 votes vote down vote up
def create(self, name_or_url, executor, **kwargs):
        # create url.URL object
        u = url.make_url(name_or_url)

        dialect_cls = u.get_dialect()

        dialect_args = {}
        # consume dialect arguments from kwargs
        for k in util.get_cls_kwargs(dialect_cls):
            if k in kwargs:
                dialect_args[k] = kwargs.pop(k)

        # create dialect
        dialect = dialect_cls(**dialect_args)

        return MockEngineStrategy.MockConnection(dialect, executor) 
Example #13
Source File: base.py    From incubator-superset with Apache License 2.0 6 votes vote down vote up
def adjust_database_uri(cls, uri: URL, selected_schema: Optional[str]) -> None:
        """
        Mutate the database component of the SQLAlchemy URI.

        The URI here represents the URI as entered when saving the database,
        ``selected_schema`` is the schema currently active presumably in
        the SQL Lab dropdown. Based on that, for some database engine,
        we can return a new altered URI that connects straight to the
        active schema, meaning the users won't have to prefix the object
        names by the schema name.

        Some databases engines have 2 level of namespacing: database and
        schema (postgres, oracle, mssql, ...)
        For those it's probably better to not alter the database
        component of the URI with the schema name, it won't work.

        Some database drivers like presto accept '{catalog}/{schema}' in
        the database component of the URL, that can be handled here.
        """ 
Example #14
Source File: middleware.py    From fastapi-sqlalchemy with MIT License 6 votes vote down vote up
def __init__(
        self,
        app: ASGIApp,
        db_url: Optional[Union[str, URL]] = None,
        custom_engine: Optional[Engine] = None,
        engine_args: Dict = None,
        session_args: Dict = None,
    ):
        super().__init__(app)
        global _Session
        engine_args = engine_args or {}

        session_args = session_args or {}
        if not custom_engine and not db_url:
            raise ValueError("You need to pass a db_url or a custom_engine parameter.")
        if not custom_engine:
            engine = create_engine(db_url, **engine_args)
        else:
            engine = custom_engine
        _Session = sessionmaker(bind=engine, **session_args) 
Example #15
Source File: strategies.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def create(self, name_or_url, executor, **kwargs):
        # create url.URL object
        u = url.make_url(name_or_url)

        dialect_cls = u.get_dialect()

        dialect_args = {}
        # consume dialect arguments from kwargs
        for k in util.get_cls_kwargs(dialect_cls):
            if k in kwargs:
                dialect_args[k] = kwargs.pop(k)

        # create dialect
        dialect = dialect_cls(**dialect_args)

        return MockEngineStrategy.MockConnection(dialect, executor) 
Example #16
Source File: assertsql.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def _compile_dialect(self, execute_observed):
        if self.dialect == 'default':
            return DefaultDialect()
        else:
            # ugh
            if self.dialect == 'postgresql':
                params = {'implicit_returning': True}
            else:
                params = {}
            return url.URL(self.dialect).get_dialect()(**params) 
Example #17
Source File: assertsql.py    From android_universal with MIT License 5 votes vote down vote up
def _compile_dialect(self, execute_observed):
        if self.dialect == 'default':
            return DefaultDialect()
        else:
            # ugh
            if self.dialect == 'postgresql':
                params = {'implicit_returning': True}
            else:
                params = {}
            return url.URL(self.dialect).get_dialect()(**params) 
Example #18
Source File: provision.py    From android_universal with MIT License 5 votes vote down vote up
def __call__(self, cfg, *arg):
        if isinstance(cfg, compat.string_types):
            url = sa_url.make_url(cfg)
        elif isinstance(cfg, sa_url.URL):
            url = cfg
        else:
            url = cfg.db.url
        backend = url.get_backend_name()
        if backend in self.fns:
            return self.fns[backend](cfg, *arg)
        else:
            return self.fns['*'](cfg, *arg) 
Example #19
Source File: mysql.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def adjust_database_uri(
        cls, uri: URL, selected_schema: Optional[str] = None
    ) -> None:
        if selected_schema:
            uri.database = parse.quote(selected_schema, safe="") 
Example #20
Source File: core.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def get_password_masked_url(
        cls, url: URL  # pylint: disable=redefined-outer-name
    ) -> URL:
        url_copy = deepcopy(url)
        if url_copy.password is not None:
            url_copy.password = PASSWORD_MASK
        return url_copy 
Example #21
Source File: new_tweet_parser.py    From hoaxy-backend with GNU General Public License v3.0 5 votes vote down vote up
def consumer_queue(q):
    engine = create_engine(
        URL(**CONF['database']['connect_args']),
        pool_size=1,
        pool_recycle=CONF['database']['pool_recycle'],
        client_encoding='utf8')
    Session = scoped_session(sessionmaker(bind=engine))
    session = Session()
    parser = Parser(
        session,
        platform_id=1,
        saved_tweet=True,
        file_save_null_byte_tweet='null_byte_tweet.txt')
    e_fp = open('exception_tweet.txt', 'w')
    while True:
        jd = q.get()
        if jd == 'STOP':
            logger.info('Task finished!')
            q.put('STOP')
            break
        try:
            parser.parse(jd)
        except Exception as e:
            logger.warning('Exception: %s, when parsing: %s', e, jd['id'])
            e_fp.write(jd['id'])
            e_fp.write('\n')
    e_fp.close() 
Example #22
Source File: core.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def get_password_masked_url_from_uri(  # pylint: disable=invalid-name
        cls, uri: str
    ) -> URL:
        sqlalchemy_url = make_url(uri)
        return cls.get_password_masked_url(sqlalchemy_url) 
Example #23
Source File: core.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def url_object(self) -> URL:
        return make_url(self.sqlalchemy_uri_decrypted) 
Example #24
Source File: sql_lab.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def sqlalchemy_uri(self) -> URL:
        return self.database.sqlalchemy_uri 
Example #25
Source File: analytics_db_safety.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def check_sqlalchemy_uri(uri: URL) -> None:
    if uri.startswith("sqlite"):
        # sqlite creates a local DB, which allows mapping server's filesystem
        raise DBSecurityException(
            "SQLite database cannot be used as a data source for security reasons."
        ) 
Example #26
Source File: test_reconnect.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_dialect_initialize_once(self):
        from sqlalchemy.engine.url import URL
        from sqlalchemy.engine.default import DefaultDialect

        dbapi = self.dbapi

        class MyURL(URL):
            def _get_entrypoint(self):
                return Dialect

            def get_dialect(self):
                return Dialect

        class Dialect(DefaultDialect):
            initialize = Mock()

        engine = create_engine(MyURL("foo://"), module=dbapi)
        engine.connect()

        # note that the dispose() call replaces the old pool with a new one;
        # this is to test that even though a single pool is using
        # dispatch.exec_once(), by replacing the pool with a new one, the event
        # would normally fire again onless once=True is set on the original
        # listen as well.

        engine.dispose()
        engine.connect()
        eq_(Dialect.initialize.call_count, 1) 
Example #27
Source File: test_parseconnect.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_password_custom_obj(self):
        class SecurePassword(str):
            def __init__(self, value):
                self.value = value

            def __str__(self):
                return self.value

        sp = SecurePassword("secured_password")
        u = url.URL("dbtype", username="x", password=sp, host="localhost")

        eq_(u.password, "secured_password")
        eq_(str(u), "dbtype://x:secured_password@localhost")

        # test in-place modification
        sp.value = "new_secured_password"
        eq_(u.password, "new_secured_password")
        eq_(str(u), "dbtype://x:new_secured_password@localhost")

        u.password = "hi"

        eq_(u.password, "hi")
        eq_(str(u), "dbtype://x:hi@localhost")

        u.password = None

        is_(u.password, None)
        eq_(str(u), "dbtype://x@localhost") 
Example #28
Source File: utils.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def _url_from_target(self, target):
        if isinstance(target, Connectable):
            return target.engine.url
        elif isinstance(target, str):
            if "://" not in target:
                target_url = sa_url.make_url("%s://" % target)
            else:
                target_url = sa_url.make_url(target)
            return target_url
        elif isinstance(target, sa_url.URL):
            return target
        else:
            raise ValueError("Invalid target type: %r" % target) 
Example #29
Source File: models.py    From tethys with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_url(self):
        """
        Returns a Persistent Store URL
        """
        from sqlalchemy.engine.url import URL
        url = URL(
            drivername=self.engine,
            host=self.host,
            port=self.port,
            username=self.username,
            password=self.password,
            database=self.database
        )
        return url 
Example #30
Source File: base.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def modify_url_for_impersonation(
        cls, url: URL, impersonate_user: bool, username: Optional[str]
    ) -> None:
        """
        Modify the SQL Alchemy URL object with the user to impersonate if applicable.
        :param url: SQLAlchemy URL object
        :param impersonate_user: Flag indicating if impersonation is enabled
        :param username: Effective username
        """
        if impersonate_user and username is not None:
            url.username = username