Python sqlalchemy.orm.scoping.scoped_session() Examples

The following are code examples for showing how to use sqlalchemy.orm.scoping.scoped_session(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: hydrus   Author: HTTP-APIs   File: crud.py    MIT License 8 votes vote down vote up
def insert_single(object_: Dict[str, Any], session: scoped_session) -> Any:
    """Insert instance of classes with single objects.
    :param object_: object to be inserted
    :param session: sqlalchemy scoped session
    :return:

    Raises:
        ClassNotFound: If `type_` does not represt a valid/defined RDFClass.
        Instance: If an Instance of type `type_` already exists.

    """
    try:
        rdf_class = session.query(RDFClass).filter(
            RDFClass.name == object_["@type"]).one()
    except NoResultFound:
        raise ClassNotFound(type_=object_["@type"])

    try:
        session.query(Instance).filter(
            Instance.type_ == rdf_class.id).all()[-1]
    except (NoResultFound, IndexError, ValueError):
        return insert(object_, session=session)

    raise InstanceExists(type_=rdf_class.name) 
Example 2
Project: hydrus   Author: HTTP-APIs   File: crud_helpers.py    MIT License 7 votes vote down vote up
def apply_filter(object_id: str, search_props: Dict[str, Any],
                 triples: Graph, session: scoped_session) -> bool:
    """Check whether objects has properties with query values or not.
    :param object_id: Id of the instance.
    :param search_props: Dictionary of query parameters with property id and values.
    :param triples: All triples.
    :param session: sqlalchemy scoped session.
    :return: True if the instance has properties with given values, False otherwise.
    """
    for prop in search_props:
        # For nested properties
        if isinstance(search_props[prop], dict):
            data = session.query(triples).filter(
                triples.GraphIII.subject == object_id, triples.GraphIII.predicate == prop).one()
            if apply_filter(data.object_, search_props[prop], triples, session) is False:
                return False
        else:
            data = session.query(triples).filter(
                triples.GraphIIT.subject == object_id, triples.GraphIIT.predicate == prop).one()
            terminal = session.query(Terminal).filter(
                Terminal.id == data.object_).one()
            if terminal.value != search_props[prop]:
                return False
    return True 
Example 3
Project: hydrus   Author: HTTP-APIs   File: crud.py    MIT License 6 votes vote down vote up
def delete_single(type_: str, session: scoped_session) -> None:
    """Delete instance of classes with single objects.
    :param type_: type of object to be deleted
    :param session: sqlalchemy scoped session
    :return: None

    Raises:
        ClassNotFound: If `type_` does not represt a valid/defined RDFClass.
        InstanceNotFound: If no Instance of the class exists.

    """
    try:
        rdf_class = session.query(RDFClass).filter(
            RDFClass.name == type_).one()
    except NoResultFound:
        raise ClassNotFound(type_=type_)

    try:
        instance = session.query(Instance).filter(
            Instance.type_ == rdf_class.id).all()[-1]
    except (NoResultFound, IndexError, ValueError):
        raise InstanceNotFound(type_=rdf_class.name)

    return delete(instance.id, type_, session=session) 
Example 4
Project: falcon-http-hydra   Author: HTTP-APIs   File: crud.py    MIT License 6 votes vote down vote up
def update(id_: int, type_: str, object_: Dict[str,str], session: scoped_session, api_name:str) -> int:
    """Update an object properties based on the given object [PUT]."""
    # Keep the object as fail safe
    instance = get(id_=id_, type_=type_, session=session, api_name=api_name)
    instance.pop("@id")

    # Delete the old object
    delete(id_=id_, type_=type_, session=session)
    # Try inserting new object
    try:
        insert(object_=object_, id_=id_, session=session)
    except Exception as e:
        # Put old object back
        insert(object_=instance, id_=id_, session=session)
        raise e

    get(id_=id_, type_=type_, session=session, api_name=api_name)
    return id_ 
Example 5
Project: falcon-http-hydra   Author: HTTP-APIs   File: crud.py    MIT License 6 votes vote down vote up
def get_single(type_: str, api_name: str, session: scoped_session) -> Dict[str, Any]:
    """Get instance of classes with single objects."""
    try:
        rdf_class = session.query(RDFClass).filter(RDFClass.name == type_).one()
    except NoResultFound:
        raise ClassNotFound(type_=type_)

    try:
        instance = session.query(Instance).filter(Instance.type_ == rdf_class.id).all()[-1]
    except (NoResultFound, IndexError, ValueError):
        raise InstanceNotFound(type_=rdf_class.name)
    object_ = get(instance.id, rdf_class.name, session=session, api_name=api_name)

    object_["@id"] = "/"+api_name+"/"+type_

    return object_ 
Example 6
Project: sgx-kms   Author: cloud-security-research   File: pkcs11_kek_rewrap.py    Apache License 2.0 6 votes vote down vote up
def __init__(self, conf):
        self.dry_run = False
        self.db_engine = sqlalchemy.create_engine(conf.sql_connection)
        self._session_creator = scoping.scoped_session(
            orm.sessionmaker(
                bind=self.db_engine,
                autocommit=True
            )
        )
        self.crypto_plugin = p11_crypto.P11CryptoPlugin(conf)
        self.pkcs11 = self.crypto_plugin.pkcs11
        self.plugin_name = utils.generate_fullname_for(self.crypto_plugin)
        self.hsm_session = self.pkcs11.get_session()
        self.new_mkek_label = self.crypto_plugin.mkek_label
        self.new_hmac_label = self.crypto_plugin.hmac_label
        self.new_mkek = self.crypto_plugin._get_master_key(self.new_mkek_label)
        self.new_mkhk = self.crypto_plugin._get_master_key(self.new_hmac_label) 
Example 7
Project: hydrus   Author: HTTP-APIs   File: crud_helpers.py    MIT License 5 votes vote down vote up
def parse_search_params(search_params: Dict[str, Any],
                        properties: BaseProperty,
                        session: scoped_session) -> Dict[str, Any]:
    """Parse search parameters and create a dict with id of parameters as keys.
    :param search_params: Dictionary having input search parameters.
    :param properties: All properties.
    :param session: sqlalchemy session.
    :return: A dictionary having property ids as keys.
    """
    search_props = dict()
    pagination_parameters = ["page", "pageIndex", "limit", "offset"]
    for param in search_params:
        # Skip if the parameter is a pagination parameter
        if param in pagination_parameters:
            continue
        # For one level deep nested parameters
        if "[" in param and "]" in param:
            prop_name = param.split('[')[0]
            try:
                prop_id = session.query(properties).filter(
                    properties.name == prop_name).one().id
                if prop_id not in search_props:
                    search_props[prop_id] = {}
                nested_prop_id = session.query(properties).filter(
                    properties.name == param[param.find('[') + 1:param.find(']')]).one().id
                search_props[prop_id][nested_prop_id] = search_params[param]
            except NoResultFound:
                raise InvalidSearchParameter(param)
        # For normal parameters
        else:
            try:
                prop_id = session.query(properties).filter(
                    properties.name == param).one().id
                search_props[prop_id] = search_params[param]
            except NoResultFound:
                raise InvalidSearchParameter(param)
    return search_props 
Example 8
Project: hydrus   Author: HTTP-APIs   File: crud.py    MIT License 5 votes vote down vote up
def update(id_: str,
           type_: str,
           object_: Dict[str,
                         str],
           session: scoped_session,
           api_name: str,
           link_props: Dict[str, Any]={},
           path: str = None) -> str:
    """Update an object properties based on the given object [PUT].
    :param id_: if of object to be updated
    :param type_: type of object to be updated
    :param object_: object that has to be inserted
    :param session: sqlalchemy scoped session
    :param api_name: api name specified while starting server
    :param link_props: Link properties of the object being updated.
    :param path: endpoint
    :return: id of updated object
    """
    # Keep the object as fail safe
    instance = get(id_=id_, type_=type_, session=session, api_name=api_name)
    instance.pop("@id")
    # Delete the old object
    delete(id_=id_, type_=type_, session=session)
    # Try inserting new object
    try:
        insert(object_=object_, id_=id_, link_props=link_props, session=session)
    except (ClassNotFound, InstanceExists, PropertyNotFound) as e:
        # Put old object back
        insert(object_=instance, id_=id_, link_props=link_props, session=session)
        raise e

    get(id_=id_, type_=type_, session=session, api_name=api_name, path=path)
    return id_ 
Example 9
Project: hydrus   Author: HTTP-APIs   File: crud.py    MIT License 5 votes vote down vote up
def update_single(object_: Dict[str,
                                Any],
                  session: scoped_session,
                  api_name: str,
                  link_props: Dict[str, Any],
                  path: str = None) -> int:
    """Update instance of classes with single objects.
    :param object_: new object
    :param session: sqlalchemy scoped session
    :param api_name: api name specified while starting server
    :param link_props: Link properties of the object being updated
    :param path: endpoint
    :return: id of the updated object

    Raises:
        ClassNotFound: If `object['@type']` does not represt a valid/defined RDFClass.
        InstanceNotFound: If no Instance of the class exists.

    """
    try:
        rdf_class = session.query(RDFClass).filter(
            RDFClass.name == object_["@type"]).one()
    except NoResultFound:
        raise ClassNotFound(type_=object_["@type"])

    try:
        instance = session.query(Instance).filter(
            Instance.type_ == rdf_class.id).all()[-1]
    except (NoResultFound, IndexError, ValueError):
        raise InstanceNotFound(type_=rdf_class.name)

    return update(
        id_=instance.id,
        type_=object_["@type"],
        object_=object_,
        session=session,
        api_name=api_name,
        link_props=link_props,
        path=path) 
Example 10
Project: hydrus   Author: HTTP-APIs   File: crud.py    MIT License 5 votes vote down vote up
def insert_modification_record(method: str, resource_url: str,
                               session: scoped_session) -> int:
    """
    Insert a modification record into the database.
    :param method: HTTP method type of related operation.
    :param resource_url: URL of resource modified.
    :param session: sqlalchemy session.
    :return: ID of new modification record.
    """
    modification = Modification(method=method, resource_url=resource_url)
    session.add(modification)
    session.commit()
    return modification.job_id 
Example 11
Project: hydrus   Author: HTTP-APIs   File: crud.py    MIT License 5 votes vote down vote up
def get_last_modification_job_id(session: scoped_session) -> str:
    """
    Get job id of most recent modification record stored in the db.
    :param session: sqlalchemy session
    :return: job id of recent modification.
    """
    last_modification = session.query(Modification).order_by(Modification.job_id.desc()).first()
    if last_modification is None:
        last_job_id = ""
    else:
        last_job_id = last_modification.job_id
    return last_job_id 
Example 12
Project: hydrus   Author: HTTP-APIs   File: crud.py    MIT License 5 votes vote down vote up
def insert_iii(object_: Dict[str, Any], prop_name: str,
               instance: Instance, property_: BaseProperty,
               session: scoped_session) -> GraphIII:
    """
    Insert a GraphIII triple in the database.
    :param object_:  Object body.
    :param prop_name: Property name.
    :param instance: Instance for the newly added object.
    :param property_: Predicate in the new triple being inserted.
    :param session: sqlalchemy session.

    :return: GraphIII triple.

    :raises: NotInstanceProperty
    """
    instance_id = insert(object_[prop_name], session=session)
    instance_object = session.query(Instance).filter(
        Instance.id == instance_id).one()
    if property_.type_ == "PROPERTY" or property_.type_ == "INSTANCE":
        property_.type_ = "INSTANCE"
        session.add(property_)
        triple = GraphIII(
            subject=instance.id,
            predicate=property_.id,
            object_=instance_object.id)
        session.add(triple)
        return triple
    else:
        session.close()
        raise NotInstanceProperty(type_=prop_name) 
Example 13
Project: hydrus   Author: HTTP-APIs   File: crud.py    MIT License 5 votes vote down vote up
def insert_iac(object_: Dict[str, Any], prop_name: str,
               instance: Instance, property_: BaseProperty,
               session: scoped_session) -> GraphIAC:
    """
    Insert a GraphIAC triple in the database.
    :param object_:  Object body.
    :param prop_name: Property name.
    :param instance: Instance for the newly added object.
    :param property_: Predicate in the new triple being inserted.
    :param session: sqlalchemy session.

    :return: GraphIAC triple.
    """
    if property_.type_ == "PROPERTY" or property_.type_ == "ABSTRACT":
        property_.type_ = "ABSTRACT"
        session.add(property_)
        class_ = session.query(RDFClass).filter(
            RDFClass.name == object_[prop_name]).one()
        triple = GraphIAC(
            subject=instance.id,
            predicate=property_.id,
            object_=class_.id)
        session.add(triple)
        return triple
    else:
        session.close()
        raise NotAbstractProperty(type_=prop_name) 
Example 14
Project: hydrus   Author: HTTP-APIs   File: crud.py    MIT License 5 votes vote down vote up
def insert_iit(object_: Dict[str, Any], prop_name: str,
               instance: Instance, property_: BaseProperty,
               session: scoped_session) -> GraphIIT:
    """
    Insert a GraphIIT triple in the database.
    :param object_:  Object body.
    :param prop_name: Property name.
    :param instance: Instance for the newly added object.
    :param property_: Predicate in the new triple being inserted.
    :param session: sqlalchemy session.

    :return: GraphIIT triple.

    :raises: NotInstanceProperty
    """
    terminal = Terminal(value=object_[prop_name])
    session.add(terminal)
    session.flush()  # Assigns ID without committing

    if property_.type_ == "PROPERTY" or property_.type_ == "INSTANCE":
        property_.type_ = "INSTANCE"
        session.add(property_)
        triple = GraphIIT(
            subject=instance.id,
            predicate=property_.id,
            object_=terminal.id)
        # Add things directly to session, if anything fails whole
        # transaction is aborted
        session.add(triple)
        return triple
    else:
        session.close()
        raise NotInstanceProperty(type_=prop_name) 
Example 15
Project: telethon-session-sqlalchemy   Author: tulir   File: sqlalchemy.py    MIT License 5 votes vote down vote up
def __init__(self, engine: Union[sql.engine.Engine, str] = None,
                 session: Optional[Union[orm.Session, scoped_session, bool]] = None,
                 table_prefix: str = "", table_base: Optional[declarative_base] = None,
                 manage_tables: bool = True) -> None:
        if isinstance(engine, str):
            engine = sql.create_engine(engine)

        self.db_engine = engine
        if session is None:
            db_factory = orm.sessionmaker(bind=self.db_engine)
            self.db = orm.scoping.scoped_session(db_factory)
        elif not session:
            self.db = None
        else:
            self.db = session

        table_base = table_base or declarative_base()
        (self.Version, self.Session, self.Entity,
         self.SentFile, self.UpdateState) = self.create_table_classes(self.db, table_prefix,
                                                                      table_base)
        self.alchemy_session_class = AlchemySession
        if not self.db:
            # Implicit core mode if there's no ORM session.
            self.core_mode = True

        if manage_tables:
            if not self.db:
                raise ValueError("Can't manage tables without an ORM session.")
            table_base.metadata.bind = self.db_engine
            if not self.db_engine.dialect.has_table(self.db_engine,
                                                    self.Version.__tablename__):
                table_base.metadata.create_all()
                self.db.add(self.Version(version=LATEST_VERSION))
                self.db.commit()
            else:
                self.check_and_upgrade_database() 
Example 16
Project: falcon-http-hydra   Author: HTTP-APIs   File: crud.py    MIT License 5 votes vote down vote up
def delete(id_:int, type_:str, session:scoped_session) -> None:
    """Delete an Instance and all its relations from DB given id [DELETE]."""
    try:
        rdf_class = session.query(RDFClass).filter(RDFClass.name == type_).one()
    except NoResultFound:
        print(type_)
        raise ClassNotFound(type_=type_)
    try:
        instance = session.query(Instance).filter(
            Instance.id == id_ and type_ == rdf_class.id).one()
    except NoResultFound:
        raise InstanceNotFound(type_=rdf_class.name, id_=id_)

    data_IIT = session.query(triples).filter(triples.GraphIIT.subject == id_).all()
    data_IAC = session.query(triples).filter(triples.GraphIAC.subject == id_).all()
    data_III = session.query(triples).filter(triples.GraphIII.subject == id_).all()

    data = data_III + data_IIT + data_IAC
    for item in data:
        session.delete(item)

    for data in data_IIT:
        terminal = session.query(Terminal).filter(Terminal.id == data.object_).one()
        session.delete(terminal)

    for data in data_III:
        III_instance = session.query(Instance).filter(Instance.id == data.object_).one()
        III_instance_type = session.query(RDFClass).filter(RDFClass.id == III_instance.type_).one()
        # Get the III object type_
        delete(III_instance.id, III_instance_type.name, session=session)

    session.delete(instance)
    session.commit() 
Example 17
Project: falcon-http-hydra   Author: HTTP-APIs   File: crud.py    MIT License 5 votes vote down vote up
def get_collection(API_NAME: str, type_: str, session: scoped_session) -> Dict[str, Any]:
    """Retrieve a type of collection from the database."""
    collection_template = {
        "@id": "/"+API_NAME+"/" + type_ + "Collection/",
        "@context": None,
        "@type": type_ + "Collection",
        "members": list()
    } # type: Dict[str, Any]
    try:
        rdf_class = session.query(RDFClass).filter(
            RDFClass.name == type_).one()
    except NoResultFound:
        raise ClassNotFound(type_=type_)

    try:
        instances = session.query(Instance).filter(Instance.type_ == rdf_class.id).all()
    except NoResultFound:
        instances = list()

    for instance_ in instances:
        object_template = {
            "@id": "/"+API_NAME+"/" + type_ + "Collection/" + str(instance_.id),
            "@type": type_
        }
        collection_template["members"].append(object_template)
    return collection_template 
Example 18
Project: falcon-http-hydra   Author: HTTP-APIs   File: crud.py    MIT License 5 votes vote down vote up
def update_single(object_: Dict[str, Any], session: scoped_session, api_name: str) -> int:
    """Update instance of classes with single objects."""
    try:
        rdf_class = session.query(RDFClass).filter(RDFClass.name == object_["@type"]).one()
    except NoResultFound:
        print("Class not found in update_single")
        raise ClassNotFound(type_=object_["@type"])

    try:
        instance = session.query(Instance).filter(Instance.type_ == rdf_class.id).all()[-1]
    except (NoResultFound, IndexError, ValueError):
        raise InstanceNotFound(type_=rdf_class.name)

    return update(id_=instance.id, type_=object_["@type"], object_=object_, session=session, api_name=api_name) 
Example 19
Project: falcon-http-hydra   Author: HTTP-APIs   File: crud.py    MIT License 5 votes vote down vote up
def delete_single(type_: str, session: scoped_session) -> None:
    """Delete instance of classes with single objects."""
    try:
        rdf_class = session.query(RDFClass).filter(RDFClass.name == type_).one()
    except NoResultFound:
        raise ClassNotFound(type_=type_)

    try:
        instance = session.query(Instance).filter(Instance.type_ == rdf_class.id).all()[-1]
    except (NoResultFound, IndexError, ValueError):
        raise InstanceNotFound(type_=rdf_class.name)

    return delete(instance.id, type_, session=session) 
Example 20
Project: falcon-http-hydra   Author: HTTP-APIs   File: doc_parse.py    MIT License 5 votes vote down vote up
def insert_classes(classes: List[Dict[str, Any]], session: scoped_session) -> Optional[Any]:
    """Insert all the classes as defined in the APIDocumentation into DB."""
    # print(session.query(exists().where(RDFClass.name == "Datastream")).scalar())
    class_list = [RDFClass(name=class_["label"].strip('.')) for class_ in classes
                  if "label" in class_ and
                  not session.query(exists().where(RDFClass.name == class_["label"].strip('.'))).scalar()]

    class_list = class_list + [RDFClass(name=class_["title"].strip('.')) for class_ in classes
                               if "title" in class_ and
                               not session.query(exists().where(RDFClass.name == class_["title"].strip('.'))).scalar()]
    # print(class_list)
    session.add_all(class_list)
    session.commit()
    return None 
Example 21
Project: falcon-http-hydra   Author: HTTP-APIs   File: doc_parse.py    MIT License 5 votes vote down vote up
def insert_properties(properties: Set[str], session: scoped_session) -> Optional[Any]:
    """Insert all the properties as defined in the APIDocumentation into DB."""
    prop_list = [BaseProperty(name=prop) for prop in properties
                 if not session.query(exists().where(BaseProperty.name == prop)).scalar()]
    session.add_all(prop_list)
    session.commit()
    return None


# if __name__ == "__main__":
#     Session = sessionmaker(bind=engine)
#     session = Session()
#
#     doc = doc_gen("test", "test")
#     # Extract all classes with supportedProperty from both
#     classes = get_classes(doc.generate())
#
#     # Extract all properties from both
#     # import pdb; pdb.set_trace()
#     properties = get_all_properties(classes)
#     # Add all the classes
#     insert_classes(classes, session)
#     print("Classes inserted successfully")
#     # Add all the properties
#     insert_properties(properties, session)
#     print("Properties inserted successfully") 
Example 22
Project: Dallinger   Author: Dallinger   File: test_experiment_server.py    MIT License 5 votes vote down vote up
def standard_args(experiment):
    from dallinger.models import Participant
    from sqlalchemy.orm.scoping import scoped_session

    return {
        "participant": mock.Mock(spec_set=Participant, status="working"),
        "assignment_id": "some assignment id",
        "experiment": experiment,
        "session": mock.Mock(spec_set=scoped_session),
        "config": {},
        "now": end_time,
    }.copy() 
Example 23
Project: gamification-engine   Author: ActiDoo   File: base.py    MIT License 5 votes vote down vote up
def setUp(self):
        from gengine.app.cache import clear_all_caches
        clear_all_caches()
        self.db = db.db()
        dsn = self.db.dsn()
        self.engine =  create_engine(
            "postgresql://%(user)s@%(host)s:%(port)s/%(database)s" % {
                "user": dsn["user"],
                "host": dsn["host"],
                "port": dsn["port"],
                "database": dsn["database"],
            }
        )
        init_session(override_session=scoped_session(get_sessionmaker(bind=self.engine)), replace=True)
        from gengine.metadata import Base
        Base.metadata.bind = self.engine

        Base.metadata.drop_all(self.engine)
        self.engine.execute("DROP SCHEMA IF EXISTS public CASCADE")
        self.engine.execute("CREATE SCHEMA IF NOT EXISTS public")

        from alembic.config import Config
        from alembic import command

        alembic_cfg = Config(attributes={
            'engine': self.engine,
            'schema': 'public'
        })
        script_location = os.path.join(
            os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
            'app/alembic'
        )
        alembic_cfg.set_main_option("script_location", script_location)

        from gengine.app import model

        tables = [t for name, t in model.__dict__.items() if isinstance(t, Table)]
        Base.metadata.create_all(self.engine, tables=tables)

        command.stamp(alembic_cfg, "head") 
Example 24
Project: gamification-engine   Author: ActiDoo   File: base.py    MIT License 5 votes vote down vote up
def setUp(self):
        from gengine.app.cache import clear_all_caches
        clear_all_caches()
        self.db = db.db()
        dsn = self.db.dsn()
        self.engine =  create_engine(
            "postgresql://%(user)s@%(host)s:%(port)s/%(database)s" % {
                "user": dsn["user"],
                "host": dsn["host"],
                "port": dsn["port"],
                "database": dsn["database"],
            }
        )
        init_session(override_session=scoped_session(get_sessionmaker(bind=self.engine)), replace=True)
        from gengine.metadata import Base
        Base.metadata.bind = self.engine

        Base.metadata.drop_all(self.engine)
        self.engine.execute("DROP SCHEMA IF EXISTS public CASCADE")
        self.engine.execute("CREATE SCHEMA IF NOT EXISTS public")

        from alembic.config import Config
        from alembic import command

        alembic_cfg = Config(attributes={
            'engine': self.engine,
            'schema': 'public'
        })
        script_location = os.path.join(
            os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
            'app/alembic'
        )
        alembic_cfg.set_main_option("script_location", script_location)

        from gengine.app import model

        tables = [t for name, t in model.__dict__.items() if isinstance(t, Table)]
        Base.metadata.create_all(self.engine, tables=tables)

        command.stamp(alembic_cfg, "head") 
Example 25
Project: gamification-engine   Author: ActiDoo   File: metadata.py    MIT License 5 votes vote down vote up
def init_session(override_session=None, replace=False):
    global DBSession
    if DBSession.target and not replace:
        return
    if override_session:
        DBSession.target = override_session
    else:
        DBSession.target = scoped_session(get_sessionmaker()) 
Example 26
Project: freqtrade   Author: Hooker41   File: persistence.py    GNU General Public License v3.0 5 votes vote down vote up
def init(config: dict, engine: Optional[Engine] = None) -> None:
    """
    Initializes this module with the given config,
    registers all known command handlers
    and starts polling for message updates
    :param config: config to use
    :param engine: database engine for sqlalchemy (Optional)
    :return: None
    """
    _CONF.update(config)
    if not engine:
        if _CONF.get('dry_run', False):
            # the user wants dry run to use a DB
            if _CONF.get('dry_run_db', False):
                engine = create_engine('sqlite:///tradesv3.dry_run.sqlite')
            # Otherwise dry run will store in memory
            else:
                engine = create_engine('sqlite://',
                                       connect_args={'check_same_thread': False},
                                       poolclass=StaticPool,
                                       echo=False)
        else:
            engine = create_engine('sqlite:///tradesv3.sqlite')

    session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True))
    Trade.session = session()
    Trade.query = session.query_property()
    _DECL_BASE.metadata.create_all(engine)

    # Clean dry_run DB
    if _CONF.get('dry_run', False) and _CONF.get('dry_run_db', False):
        clean_dry_run_db() 
Example 27
Project: sgx-kms   Author: cloud-security-research   File: pkcs11_migrate_kek_signatures.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, db_connection, library_path, login, slot_id):
        self.dry_run = False
        self.db_engine = sqlalchemy.create_engine(db_connection)
        self._session_creator = scoping.scoped_session(
            orm.sessionmaker(
                bind=self.db_engine,
                autocommit=True
            )
        )
        self.crypto_plugin = p11_crypto.P11CryptoPlugin(CONF)
        self.plugin_name = utils.generate_fullname_for(self.crypto_plugin)
        self.pkcs11 = self.crypto_plugin.pkcs11
        self.session = self.pkcs11.get_session() 
Example 28
Project: AI-Scalpel-Trading-Bot   Author: hackobi   File: persistence.py    GNU General Public License v3.0 5 votes vote down vote up
def init(db_url: str, clean_open_orders: bool = False) -> None:
    """
    Initializes this module with the given config,
    registers all known command handlers
    and starts polling for message updates
    :param db_url: Database to use
    :param clean_open_orders: Remove open orders from the database.
        Useful for dry-run or if all orders have been reset on the exchange.
    :return: None
    """
    kwargs = {}

    # Take care of thread ownership if in-memory db
    if db_url == 'sqlite://':
        kwargs.update({
            'connect_args': {'check_same_thread': False},
            'poolclass': StaticPool,
            'echo': False,
        })

    try:
        engine = create_engine(db_url, **kwargs)
    except NoSuchModuleError:
        raise OperationalException(f"Given value for db_url: '{db_url}' "
                                   f"is no valid database URL! (See {_SQL_DOCS_URL})")

    session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True))
    Trade.session = session()
    Trade.query = session.query_property()
    _DECL_BASE.metadata.create_all(engine)
    check_migrate(engine)

    # Clean dry_run DB if the db is not in-memory
    if clean_open_orders and db_url != 'sqlite://':
        clean_dry_run_db() 
Example 29
Project: legion   Author: GoVanguard   File: database.py    GNU General Public License v3.0 5 votes vote down vote up
def establishSqliteConnection(self, dbFileName: str):
        self.name = dbFileName
        self.dbsemaphore = QSemaphore(1)  # to control concurrent write access to db
        self.engine = create_engine(
            'sqlite:///{dbFileName}'.format(dbFileName=dbFileName))
        self.session = scoped_session(sessionmaker())
        self.session.configure(bind=self.engine, autoflush=False)
        self.metadata = Base.metadata
        self.metadata.create_all(self.engine)
        self.metadata.echo = True
        self.metadata.bind = self.engine 
Example 30
Project: saas-api-boilerplate   Author: rgant   File: test_db.py    GNU General Public License v3.0 5 votes vote down vote up
def test_factory():
    """ create and drop tables commands need ENGINE exposed """
    assert isinstance(db.FACTORY, scoped_session) 
Example 31
Project: hydrus   Author: HTTP-APIs   File: crud.py    MIT License 4 votes vote down vote up
def delete(id_: str, type_: str, session: scoped_session) -> None:
    """Delete an Instance and all its relations from DB given id [DELETE].
    :param id_: id of object to be deleted
    :param type_: type of object to be deleted
    :param session: sqlalchemy scoped session

    Raises:
        ClassNotFound: If `type_` does not represent a valid/defined RDFClass.
        InstanceNotFound: If no instace of type `type_` with id `id_` exists.

    """
    try:
        rdf_class = session.query(RDFClass).filter(
            RDFClass.name == type_).one()
    except NoResultFound:
        raise ClassNotFound(type_=type_)
    try:
        instance = session.query(Instance).filter(
            Instance.id == id_ and type_ == rdf_class.id).one()
    except NoResultFound:
        raise InstanceNotFound(type_=rdf_class.name, id_=id_)

    data_IIT = session.query(triples).filter(
        triples.GraphIIT.subject == id_).all()
    data_IAC = session.query(triples).filter(
        triples.GraphIAC.subject == id_).all()
    data_III = session.query(triples).filter(
        triples.GraphIII.subject == id_).all()

    data = data_III + data_IIT + data_IAC
    for item in data:
        session.delete(item)

    for data in data_IIT:
        terminal = session.query(Terminal).filter(
            Terminal.id == data.object_).one()
        session.delete(terminal)

    for data in data_III:
        III_instance = session.query(Instance).filter(
            Instance.id == data.object_).one()
        III_instance_type = session.query(RDFClass).filter(
            RDFClass.id == III_instance.type_).one()
        # Get the III object type_
        delete(III_instance.id, III_instance_type.name, session=session)

    session.delete(instance)
    session.commit() 
Example 32
Project: hydrus   Author: HTTP-APIs   File: crud.py    MIT License 4 votes vote down vote up
def delete_multiple(
        id_: List[int],
        type_: str,
        session: scoped_session) -> None:
    """
    To delete multiple rows in a single request
    :param id_: list of ids for objects to be deleted\
    :param type_: type of object to be deleted
    :param session: sqlalchemy scoped session

    Raises:
        ClassNotFound: If `type_` does not represent a valid/defined RDFClass.
        InstanceNotFound: If any instance with type 'type_' and any id in 'id_' list
            does not exist.

    """
    id_ = id_.split(',')
    try:
        rdf_class = session.query(RDFClass).filter(
            RDFClass.name == type_).one()
    except NoResultFound:
        raise ClassNotFound(type_=type_)

    instances = list()
    data_III = list()
    data_IAC = list()
    data_IIT = list()

    for index in id_:
        try:
            instance = session.query(Instance).filter(
                Instance.id == index and type_ == rdf_class.id).one()
            instances.append(instance)
        except NoResultFound:
            raise InstanceNotFound(type_=rdf_class.name, id_=index)
        data_IIT += session.query(triples).filter(
            triples.GraphIIT.subject == index).all()
        data_IAC += session.query(triples).filter(
            triples.GraphIAC.subject == index).all()
        data_III += session.query(triples).filter(
            triples.GraphIII.subject == index).all()

    data = data_III + data_IIT + data_IAC
    for item in data:
        session.delete(item)

    for data in data_IIT:
        terminal = session.query(Terminal).filter(
            Terminal.id == data.object_).one()
        session.delete(terminal)

    for data in data_III:
        III_instance = session.query(Instance).filter(
            Instance.id == data.object_).one()
        III_instance_type = session.query(RDFClass).filter(
            RDFClass.id == III_instance.type_).one()
        # Get the III object type_
        delete(III_instance.id, III_instance_type.name, session=session)
    for instance in instances:
        session.delete(instance)
    session.commit() 
Example 33
Project: hydrus   Author: HTTP-APIs   File: crud.py    MIT License 4 votes vote down vote up
def insert_iii_with_link(instance_id: str, property_: BaseProperty,
                         property_value: str, session: scoped_session):
    """
    Inserts GraphIII triple to store a relation defined with hydra:Link.
    :param instance_id:  Id of the instance being inserted
    :param property_: Property being used as predicate in the new triple.
    :param property_value: Value of the property being inserted.
    :param session: sqlalchemy session
    :return:
    """
    if property_.type_ == "PROPERTY" or property_.type_ == "INSTANCE":
        property_.type_ = "INSTANCE"
        # If value matches with the regex then value is an id and link is to an
        # instance of a collection class otherwise value is a class_type and link
        # is to a non collection class.
        regex = r'[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}'
        matchObj = re.match(regex, property_value)
        # Link is to an instance of a collection class
        if matchObj:
            try:
                nested_instance = session.query(Instance).filter(
                    Instance.id == property_value).one()
            except NoResultFound:
                raise InstanceNotFound(id_=property_value, type_="")
            triple = GraphIII(
                subject=instance_id,
                predicate=property_.id,
                object_=nested_instance.id)
            session.add(triple)
            return triple
        # Link is to a non collection, single instance class
        else:
            try:
                nested_rdf_class = session.query(RDFClass).filter(
                    RDFClass.name == property_value).one()
            except NoResultFound:
                raise ClassNotFound(type_=property_value)
            try:
                nested_instance = session.query(Instance).filter(
                    Instance.type_ == nested_rdf_class.id).all()[-1]
            except (NoResultFound, IndexError, ValueError):
                raise InstanceNotFound(type_=nested_rdf_class.name)
            triple = GraphIII(
                subject=instance_id,
                predicate=property_.id,
                object_=nested_instance.id)
            session.add(triple)
            return triple
    else:
        session.close()
        raise NotInstanceProperty(type_=property_.name) 
Example 34
Project: falcon-http-hydra   Author: HTTP-APIs   File: crud.py    MIT License 4 votes vote down vote up
def get(id_: int, type_: str, api_name: str, session: scoped_session, recursive: bool = False) -> Dict[str, str]:
    """Retrieve an Instance with given ID from the database [GET]."""
    object_template = {
        "@type": "",
    } # type: Dict[str, Any]
    try:
        rdf_class = session.query(RDFClass).filter(
            RDFClass.name == type_).one()
    except NoResultFound:
        raise ClassNotFound(type_=type_)

    try:
        instance = session.query(Instance).filter(
            Instance.id == id_, Instance.type_ == rdf_class.id).one()
    except NoResultFound:
        raise InstanceNotFound(type_=rdf_class.name, id_=id_)

    data_IAC = session.query(triples).filter(triples.GraphIAC.subject == id_).all()

    data_III = session.query(triples).filter(triples.GraphIII.subject == id_).all()

    data_IIT = session.query(triples).filter(triples.GraphIIT.subject == id_).all()

    for data in data_IAC:
        prop_name = session.query(properties).filter(properties.id == data.predicate).one().name
        class_name = session.query(RDFClass).filter(RDFClass.id == data.object_).one().name
        object_template[prop_name] = class_name

    for data in data_III:
        prop_name = session.query(properties).filter(properties.id == data.predicate).one().name
        instance = session.query(Instance).filter(Instance.id == data.object_).one()
        # Get class name for instance object
        inst_class_name = session.query(RDFClass).filter(RDFClass.id == instance.type_).one().name
        # Recursive call should get the instance needed
        object_ = get(id_=instance.id, type_=inst_class_name, session=session, recursive=True, api_name=api_name)
        object_template[prop_name] = object_

    for data in data_IIT:
        prop_name = session.query(properties).filter(properties.id == data.predicate).one().name
        terminal = session.query(Terminal).filter(Terminal.id == data.object_).one()
        try:
            object_template[prop_name] = terminal.value
        except:
            # If terminal is none
            object_template[prop_name] = ""
    object_template["@type"] = rdf_class.name
    if not recursive:
        object_template["@id"] = "/"+api_name+"/"+type_+"Collection/"+str(id_)

    return object_template 
Example 35
Project: edmunds-python   Author: LowieHuyghe   File: testdatabase.py    Apache License 2.0 4 votes vote down vote up
def test_loading_and_database_and_session(self):
        """
        Test loading and database and session function
        :return:    void
        """

        # Write config
        self.write_config([
            "from edmunds.database.drivers.mysql import MySql \n",
            "APP = { \n",
            "   'database': { \n",
            "       'enabled': True, \n",
            "       'instances': [ \n",
            "           { \n",
            "               'name': 'mysql',\n",
            "               'driver': MySql,\n",
            "               'user': 'root',\n",
            "               'pass': 'root',\n",
            "               'host': 'localhost',\n",
            "               'database': 'edmunds',\n",
            "           }, \n",
            "       ], \n",
            "   }, \n",
            "} \n",
        ])

        # Create app
        app = self.create_application()

        # Test database
        self.assert_is_not_none(app.database_engine())
        self.assert_is_instance(app.database_engine(), Engine)
        self.assert_is_not_none(app.database_engine('mysql'))
        self.assert_is_instance(app.database_engine('mysql'), Engine)
        with self.assert_raises_regexp(RuntimeError, '[Nn]o instance'):
            app.database_engine('mysql2')
        self.assert_is_none(app.database_engine('mysql2', no_instance_error=True))

        self.assert_equal_deep(app.database_engine(), app.database_engine())
        self.assert_equal_deep(app.database_engine(), app.database_engine('mysql'))

        # Test session
        with app.app_context():
            self.assert_is_not_none(app.database_session())
            self.assert_is_instance(app.database_session(), scoped_session)
            self.assert_is_not_none(app.database_session('mysql'))
            self.assert_is_instance(app.database_session('mysql'), scoped_session)
            with self.assert_raises_regexp(RuntimeError, '[Nn]o instance'):
                app.database_session('mysql2')
            self.assert_is_none(app.database_session('mysql2', no_instance_error=True))

            self.assert_equal_deep(app.database_session(), app.database_session())
            self.assert_equal_deep(app.database_session(), app.database_session('mysql'))