Beispiel #1
0
def test_get_default_values_for_api() -> None:
    """Check if default values saved in unit test configuration is properly defined."""
    config = AppConfiguration()

    assert config.get('api', 'host') == 'localhost'
    assert config.getint('api', 'rest_port') == 51000
    assert config.getint('api', 'websocket_port') == 51001
    assert not config.getboolean('api', 'debug')
    assert config.get('api', 'secret_key') == 'SECRET_KEY'
    assert config.get('db', 'database_uri') == 'sqlite:///:memory:'
Beispiel #2
0
    _modified = Column(DateTime, nullable=False, server_default=func.now(), default=datetime.utcnow,
                       onupdate=datetime.utcnow)

    def __repr__(self) -> str:
        """Return string representation."""
        return '<{}: {}>'.format(self.__class__.__name__, getattr(self, 'id', '-'))

    def save(self) -> None:
        """Save the model into the database after changes."""
        with db_session() as _session:
            _session.add(self)


configuration = AppConfiguration()
db_uri = configuration.get('db', 'database_uri')
db_pool_size = configuration.getint('db', 'connection_pool_size')
engine = create_engine(db_uri, pool_size=db_pool_size, convert_unicode=True)
session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))

convention = {
    "ix": 'ix_%(column_0_label)s',
    "uq": "uq_%(table_name)s_%(column_0_name)s",
    "ck": "ck_%(table_name)s_%(constraint_name)s",
    "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
    "pk": "pk_%(table_name)s",
}

metadata = MetaData(naming_convention=convention)

Base = declarative_base(cls=MedTaggerBase, metadata=metadata)
Base.query = session.query_property()
Beispiel #3
0
from medtagger.storage import create_connection  # noqa

# Import all REST services
from medtagger.api.core.service import core_ns as core_rest_ns  # noqa
from medtagger.api.labels.service import labels_ns  # noqa
from medtagger.api.scans.service_rest import scans_ns as scans_rest_ns  # noqa
from medtagger.api.users.service import users_ns  # noqa
from medtagger.api.auth.service import auth_ns  # noqa

logger = logging.getLogger(__name__)

# Load configuration
logger.info('Loading configuration file...')
configuration = AppConfiguration()
host = configuration.get('api', 'host', fallback='localhost')
port = configuration.getint('api', 'rest_port', fallback=51000)
debug = configuration.getboolean('api', 'debug', fallback=True)

# Definition of application
app = Flask(__name__)
CORS(app)
app.secret_key = configuration.get('api', 'secret_key', fallback='')
app.register_blueprint(blueprint)

# Application config
app.config['RESTPLUS_MASK_SWAGGER'] = False
app.config['SWAGGER_UI_DOC_EXPANSION'] = 'list'
app.config['RESTPLUS_VALIDATE'] = True

try:
    # This will raise ModuleNotFoundError if app was not run inside uWSGI server
Beispiel #4
0
from medtagger.api import blueprint, web_socket  # noqa
from medtagger.config import AppConfiguration  # noqa
from medtagger.database import session  # noqa
from medtagger.database.models import User, Role  # noqa
from medtagger.storage import create_connection  # noqa

# Import all WebSocket services
from medtagger.api.scans.service_web_socket import Slices as slices_websocket_ns  # noqa

logger = logging.getLogger(__name__)

# Load configuration
logger.info('Loading configuration file...')
configuration = AppConfiguration()
host = configuration.get('api', 'host', fallback='localhost')
port = configuration.getint('api', 'websocket_port', fallback=51001)
debug = configuration.getboolean('api', 'debug', fallback=True)

# Definition of application
app = Flask(__name__)
CORS(app)
app.secret_key = configuration.get('api', 'secret_key', fallback='')
web_socket.init_app(app)

create_connection(use_gevent=True)


@app.teardown_appcontext
def shutdown_session(exception: Any = None) -> None:  # pylint: disable=unused-argument
    """Remove Session on each Request end."""
    session.remove()
Beispiel #5
0
"""Definition of storage for MedTagger."""
from cassandra.cluster import Cluster, Session, NoHostAvailable  # pylint: disable=no-name-in-module
from cassandra.policies import RoundRobinPolicy
from cassandra.cqlengine import connection
from cassandra.io.asyncorereactor import AsyncoreConnection
from cassandra.io.geventreactor import GeventConnection

from medtagger.config import AppConfiguration

MEDTAGGER_KEYSPACE = 'medtagger'

configuration = AppConfiguration()
addresses = configuration.get('cassandra', 'addresses', 'localhost').split(',')
port = configuration.getint('cassandra', 'port', 9042)
default_timeout = configuration.getint('cassandra', 'default_timeout', 20)
connect_timeout = configuration.getint('cassandra', 'connect_timeout', 20)


def create_session(use_gevent: bool = False) -> Session:
    """Create a Session object for above Cluster."""
    connection_class = GeventConnection if use_gevent else AsyncoreConnection
    cluster = Cluster(addresses,
                      port=port,
                      load_balancing_policy=RoundRobinPolicy(),
                      connection_class=connection_class,
                      connect_timeout=connect_timeout)
    session = cluster.connect()
    session.default_timeout = default_timeout
    return session