Exemple #1
0
def setup_config(config):
    app_abspath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    app_config = Config(app_abspath)
    app_config.from_object("superdesk.default_settings")
    cwd = Path.cwd()
    for p in [cwd] + list(cwd.parents):
        settings = p / "settings.py"
        if settings.is_file():
            logger.info(f"using local settings from {settings}")
            app_config.from_pyfile(settings)
            break
    else:
        logger.warning("Can't find local settings")

    update_config(app_config)

    app_config.update(
        config or {},
        **{
            "APP_ABSPATH": app_abspath,
            "DEBUG": True,
            "TESTING": True,
        },
    )

    logging.getLogger("apps").setLevel(logging.WARNING)
    logging.getLogger("elastic").setLevel(logging.WARNING)  # elastic datalayer
    logging.getLogger("urllib3").setLevel(logging.WARNING)
    logging.getLogger("celery").setLevel(logging.WARNING)
    logging.getLogger("superdesk").setLevel(logging.ERROR)
    logging.getLogger("elasticsearch").setLevel(logging.ERROR)
    logging.getLogger("superdesk.errors").setLevel(logging.CRITICAL)

    return {key: deepcopy(val) for key, val in app_config.items()}
Exemple #2
0
    def __init__(self) -> None:
        self.plugins = OrderedDict()  # type: OrderedDict[str, PluginBase]
        self.rules = None  # entry point

        self.config = Config('/')

        app.init_app()  # fake app for plugin config (deprecated)
Exemple #3
0
def test_config():
    " how can you read the config if do not create an app? " ""
    config = Config(".")
    print(config)
    config.from_object("cryptoadvance.specter.config.DevelopmentConfig")
    print(config)
    assert config["PORT"] == 25441
Exemple #4
0
def getcollection(cfgfile='config.default.py'):
    cfg = Config('')
    cfg.from_pyfile(cfgfile)
    client = pymongo.MongoClient(cfg['SIMDB_URI'])
    db = client[cfg['SIMDB_DATABASE']]
    collection = db[cfg['SIMDB_COLLECTION']]
    return collection
Exemple #5
0
    def __init__(self, args):
        self.env = args.env
        self.action = args.action
        self.kill_delay = args.kill_delay
        self.conn = None

        # Load settings                                                                                                                                       
        self.config = Config(os.path.dirname(os.path.realpath(__file__)))
        # There may be overiding settings specific to the server we are running on                                                                             
        servername = socket.gethostname().split('.')[0]
        if servername and os.path.isfile(f'settings/workers/{self.env}_{servername}.py'):
            self.config.from_object(f'settings.workers.{self.env}_{servername}.Config')
        else:
            self.config.from_object(f'settings.workers.{self.env}.Config')


        # Redis                                                                                                                                               
        if self.config['REDIS_USE_SENTINEL']:
            sentinel = redis.sentinel.Sentinel(
                self.config['REDIS_ADDRESS'],
                db=self.config['REDIS_DB'],
                password=self.config['REDIS_PASSWORD'],
                decode_responses=True
            )
            self.conn = sentinel.master_for(self.config['REDIS_SENTINEL_MASTER'])

        else:
            self.conn = redis.StrictRedis(
                host=self.config['REDIS_ADDRESS'][0],
                port=self.config['REDIS_ADDRESS'][1],
                db=self.config['REDIS_DB'],
                password=self.config['REDIS_PASSWORD'],
                decode_responses=True
            )
def loadConfig(argconfig):
    "Load the config file the same way Flask does which Chill uses."
    config_file = (argconfig if argconfig[0] == os.sep else os.path.join(
        os.getcwd(), argconfig))
    config = Config(os.getcwd())
    config.from_pyfile(config_file)
    return config
Exemple #7
0
    def get_config(
        self,
        bundle: Bundle,
        env: Union[DEV, PROD, STAGING, TEST],
    ) -> AttrDict:
        bundle_config_module = self.import_bundle_module(bundle)
        base_config = getattr(bundle_config_module, BASE_CONFIG, None)
        env_config = getattr(bundle_config_module, ENV_CONFIGS[env], None)

        if (isinstance(bundle, AppBundle) and
            (not base_config or not issubclass(base_config, AppConfig))):
            raise Exception("Could not find an AppConfig subclass in your app "
                            "bundle's config module.")

        merged = Config(None)
        for config in [base_config, env_config]:
            if config:
                merged.from_object(config)

        if isinstance(bundle, AppBundle) and 'SECRET_KEY' not in merged:
            raise Exception(
                "The `SECRET_KEY` config option is required. "
                "Please set it in your app bundle's base `Config` class.")

        return AttrDict(merged)
Exemple #8
0
def worker(verbose, queues, worker_ttl, job_monitoring_interval, config_file):
    """
    Run an RQ worker, with config loaded from CONFIG_FILE,
    to process commands on the QUEUES.
    """
    from sentry_sdk.integrations.rq import RqIntegration
    sentry_sdk.init(send_default_pii=False, integrations=[RqIntegration()])

    from rq import Connection, Worker
    import solarforecastarbiter  # NOQA preload
    from sfa_api.utils.queuing import make_redis_connection

    config = Config(Path.cwd())
    config.from_pyfile(config_file)

    worker_loglevel = _get_log_level(config, verbose, key='WORKER_LOG_LEVEL')
    _setup_logging(_get_log_level(config, verbose))

    if 'QUEUES' in config:  # pragma: no cover
        queues = config['QUEUES']

    # will likely want to add prometheus in here somewhere,
    # perhaps as custom worker class
    # if possible, get len report obj, time range
    red = make_redis_connection(config)
    with Connection(red):
        w = Worker(queues,
                   default_worker_ttl=worker_ttl,
                   job_monitoring_interval=job_monitoring_interval)
        w.work(logging_level=worker_loglevel)
Exemple #9
0
def setup(context=None, config=None, app_factory=get_app):

    app_abspath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    app_config = Config(app_abspath)
    app_config.from_object('superdesk.tests.test_settings')
    app_config['APP_ABSPATH'] = app_abspath

    app_config.update(get_test_settings())
    app_config.update(config or {})

    app_config.update({
        'DEBUG': True,
        'TESTING': True,
    })

    app = app_factory(app_config)
    logger = logging.getLogger('superdesk')
    logger.setLevel(logging.ERROR)
    logger = logging.getLogger('elasticsearch')
    logger.setLevel(logging.ERROR)
    logger = logging.getLogger('urllib3')
    logger.setLevel(logging.ERROR)
    drop_elastic(app)
    drop_mongo(app)

    # create index again after dropping it
    app.data.init_elastic(app)

    if context:
        context.app = app
        context.client = app.test_client()
Exemple #10
0
    def __init__(self,
                 app_name='enhanced-flask-app',
                 config_file=None,
                 js_filters=[],
                 css_filters=[],
                 **flask_kwargs):
        """
        
        """
        config = Config('.', Flask.default_config)
        if config_file:
            config.from_object(config_file)

        self.app = Flask(app_name, **flask_kwargs)
        self.app.config = config

        # Create webassets
        self.assets_env = Environment(self.app)
        self.assets_env.url_expire = True
        self.assets_env.url = '/static'

        # Initialize additional jinja stuff
        self.enhance_jinja(self.app.jinja_env)

        # Flask assets related
        self.js_filters = []
        self.css_filters = []
        self.depends_scss = []

        self.enhance_assets()
def setup_config(config):
    app_abspath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    app_config = Config(app_abspath)
    app_config.from_object("superdesk.default_settings")

    update_config(app_config)

    app_config.update(
        config or {},
        **{
            "APP_ABSPATH": app_abspath,
            "DEBUG": True,
            "TESTING": True,
        },
    )

    logging.getLogger("apps").setLevel(logging.WARNING)
    logging.getLogger("elastic").setLevel(logging.WARNING)  # elastic datalayer
    logging.getLogger("urllib3").setLevel(logging.WARNING)
    logging.getLogger("celery").setLevel(logging.WARNING)
    logging.getLogger("superdesk").setLevel(logging.ERROR)
    logging.getLogger("elasticsearch").setLevel(logging.ERROR)
    logging.getLogger("superdesk.errors").setLevel(logging.CRITICAL)

    return {key: deepcopy(val) for key, val in app_config.items()}
Exemple #12
0
def app():
    from flask import Config
    from newsroom import Newsroom

    cfg = Config(root)
    cfg.from_object('newsroom.default_settings')
    update_config(cfg)
    return Newsroom(config=cfg)
Exemple #13
0
def app():
    from flask import Config
    from newsroom.web import NewsroomWebApp

    cfg = Config(root)
    cfg.from_object('newsroom.default_settings')
    update_config(cfg)
    return NewsroomWebApp(config=cfg, testing=True)
Exemple #14
0
def app():
    from flask import Config
    from newsroom.news_api.app import NewsroomNewsAPI

    cfg = Config(root)
    cfg.from_object('newsroom.news_api.settings')
    update_config(cfg)
    return NewsroomNewsAPI(config=cfg, testing=True)
Exemple #15
0
def load_graph_from_db(time_limit):
    config = Config('./')
    config.from_pyfile('web_config.cfg')

    with NodeDB(config) as db:
        nodes = db.get_nodes(time_limit)
        edges = db.get_edges(nodes, 60 * 60 * 24 * 7)
        return (nodes, edges)
Exemple #16
0
def create_app(config_file=None, **kwargs):
    """
    Create a new eve app object and initialize everything.

    User configuration can be loaded in the following order:

    1. Use the `config_file` arg to specify a file
    2. If `config_file` is `None`, you set the environment variable
       `AMIVAPI_CONFIG` to the path of your config file
    3. If no environment variable is set either, `config.py` in the current
       working directory is used

    Args:
        config (path): Specify config file to use.
        kwargs: All other key-value arguments will be used to update the config
    Returns:
        (Eve): The Eve application
    """
    # Load config
    config = Config(getcwd())
    config.from_object("amivapi.settings")

    # Specified path > environment var > default path; abspath for better log
    user_config = abspath(config_file or getenv('AMIVAPI_CONFIG', 'config.py'))
    try:
        config.from_pyfile(user_config)
        config_status = "Config loaded: %s" % user_config
    except IOError:
        config_status = "No config found."

    config.update(kwargs)

    app = Eve(settings=config, validator=ValidatorAMIV)
    app.logger.info(config_status)

    # Set up error logging with sentry
    init_sentry(app)

    # Create LDAP connector
    ldap.init_app(app)

    # Initialize modules to register resources, validation, hooks, auth, etc.
    users.init_app(app)
    auth.init_app(app)
    events.init_app(app)
    groups.init_app(app)
    joboffers.init_app(app)
    beverages.init_app(app)
    studydocs.init_app(app)
    cascade.init_app(app)
    cron.init_app(app)
    documentation.init_app(app)

    # Fix that eve doesn't run hooks on embedded documents
    app.on_fetched_item += utils.run_embedded_hooks_fetched_item
    app.on_fetched_resource += utils.run_embedded_hooks_fetched_resource

    return app
Exemple #17
0
 def get(pipeline_name):
     config = Config(None)
     config.from_object('brainminer.settings')
     pipelines = config['PIPELINES']
     if pipeline_name not in pipelines.keys():
         return None
     pipeline_cls = getattr(importlib.import_module(
         pipelines[pipeline_name]['module_path']), pipelines[pipeline_name]['class_name'])
     pipeline_obj = pipeline_cls()
     return pipeline_obj
def get_config():
    """Get the configuration file and return a flask.Config instance."""
    config = Config("/")

    if os.path.exists("/etc/upload_rest_api.conf"):
        config.from_pyfile("/etc/upload_rest_api.conf")
    else:
        logging.error("/etc/upload_rest_api.conf not found!")

    return config
Exemple #19
0
def _init_config():
    import os
    import sys
    from flask import Config
    from . import defconfig

    config = Config(os.path.abspath(os.path.curdir))
    config.from_object(defconfig)
    del sys.modules[__name__]._init_config
    return config
Exemple #20
0
def create_app(config_file=None, **kwargs):
    """Create a new eve app object and initialize everything.

    User configuration can be loaded in the following order:

    1. Use the `config_file` arg to specify a file
    2. If `config_file` is `None`, you set the environment variable
       `PVK_CONFIG` to the path of your config file
    3. If no environment variable is set either, `config.py` in the current
       working directory is used

    Args:
        config (path): Specify config file to use.
        kwargs: All other key-value arguments will be used to update the config
    Returns:
        (Eve): The Eve application
    """
    # Load config
    config = Config(getcwd())
    config.from_object("backend.settings")

    # Specified path > environment var > default path; abspath for better log
    user_config = abspath(config_file or getenv('PVK_CONFIG', 'config.py'))
    try:
        config.from_pyfile(user_config)
        config_status = "Config loaded: %s" % user_config
    except IOError:
        config_status = "No config found."

    config.update(kwargs)

    # Create the app object
    application = Eve(auth=APIAuth, validator=APIValidator, settings=config)
    application.logger.info(config_status)

    # Eve provides hooks at several points of the request,
    # we use this do add dynamic filtering
    for resource in ['signups', 'selections']:
        for method in ['GET', 'PATCH', 'DELETE']:
            event = getattr(application, 'on_pre_%s_%s' % (method, resource))
            event += only_own_nethz

    # Also use hooks to add pre- and postprocessing to resources
    application.on_post_POST_signups += new_signups
    application.on_deleted_item_signups += deleted_signup
    application.on_updated_signups += patched_signup

    application.on_updated_courses += patched_course
    application.on_delete_item_courses += block_course_deletion

    application.on_insert_payments += create_payment
    application.on_inserted_payments += mark_as_paid
    application.on_deleted_item_payments += mark_as_unpaid

    return application
Exemple #21
0
def main(action, args):
    config = Config(dirname(dirname(__file__)))
    create_config(config, None)

    if action == 'create':
        create(config, args.allow_exists)
    elif action == 'start':
        start(config, args.i, args.allow_running)
    elif action == 'stop':
        stop(config)
    elif action == 'is-running':
        is_running(config)
    elif action == 'backup':
        dump(config, create_backup_file=True, file_name=args.output)
Exemple #22
0
def get_config(path=None, filename=None):
    """
    Used to make the config globally accessible for jobs
    Has to be accessed once with a path and config filename.
    After that it will return the same object everytime without the need to pass a path or a filename

    :param path: The path to the containing folder of the config file
    :param filename: The filename of the config file (Needs to be a .py file)
    :return: The loaded :class:`~flask.Config` instance
    """
    if get_config.cfg is None and path is not None and filename is not None:
        cfg = Config(path)
        cfg.from_pyfile(filename=filename)
        get_config.cfg = cfg
    return get_config.cfg
Exemple #23
0
    def __init__(self):
        conf_path = os.path.join(MALICE_ROOT, "conf", "av.conf")
        if not os.path.exists(conf_path):
            log.error("Configuration file av.conf not found".format(conf_path))
            self.av_options = False
            return

        self.av_options = Config(conf_path)

        conf_path = os.path.join(MALICE_ROOT, "conf", "intel.conf")
        if not os.path.exists(conf_path):
            log.error("Configuration file intel.conf not found".format(conf_path))
            self.intel_options = False
            return

        self.intel_options = Config(conf_path)

        conf_path = os.path.join(MALICE_ROOT, "conf", "file.conf")
        if not os.path.exists(conf_path):
            log.error("Configuration file file.conf not found".format(conf_path))
            self.file_options = False
            return

        self.file_options = Config(conf_path)
Exemple #24
0
def test():
    config = Config('application/config/')
    config.from_pyfile('testing.cfg')
    #db('createall', database=config['POSTGRESQL_DATABASE_DB'])

    print ' [START] Testing server'
    proc = subprocess.Popen(["python", "runserver.py", "testing"])
    time.sleep(5)

    call(["python", "application/tests/testsuite.py"])

    print 'KILLING THE TESTING SERVER...'
    time.sleep(5)
    os.kill(proc.pid, signal.SIGTERM)
    for pid in children_pid(proc.pid):
        os.kill(pid, signal.SIGTERM)
    def __init__(self, app, registry_namespace=None):
        super(ConfigurationRegistry, self).__init__(
            'config',
            registry_namespace=registry_namespace,
            with_setup=False,
        )

        # Create a new configuration module to collect configuration in.
        self.new_config = Config(app.config.root_path)

        # Auto-discover configuration in packages
        self.discover(app)

        # Overwrite default configuration with user specified configuration
        self.new_config.update(app.config)
        app.config.update(self.new_config)
def main():
    config = Config(__name__)

    config.from_envvar('COMPOWSR_SETTINGS', silent=True)

    # get the praw stuff
    reddit = praw.Reddit(config['PRAW_SITE_NAME'],
                         user_agent='flair_updater by /u/Witchtower')
    subreddit = reddit.subreddit(config['PRAW_SUBREDDIT_NAME'])

    # get reddit users with rank flair set
    print "getting all users with rank-flair from reddit"

    to_update = [(user['user'].name, user['flair_css_class'])
                 for user in subreddit.flair()
                 if user['flair_css_class'] in config['OW_RANKS'].keys()]

    print "got %i users with a rank flair" % len(to_update)

    # check who really needs to be updated
    db = connect_db(config)

    to_update_usernames = [i[0] for i in to_update]

    statement = "SELECT * FROM acc_links "  #\
    #% ','.join(['?']*len(to_update_usernames))
    # AND last_update > datetime('now', '+14 days')" \
    print statement
    print to_update_usernames
    cursor = db.execute(statement)  #, to_update_usernames)
    to_update2 = cursor.fetchall()
    print "%i users haven't been updated in the last 14 days" % len(to_update2)

    for row in to_update2:
        # pull sr and update in db
        new_rank = playoverwatch.CareerProfile('eu', row['bnet_name']).rank
        db.execute(
            "UPDATE acc_links SET \
                            last_rank = ?, \
                            last_update = datetime('now') \
                        WHERE bnet_name = ?", [new_rank, row['bnet_name']])
        subreddit.flair.set(row['reddit_name'], css_class=new_rank)
        print "flair '%s' set for '%s'" % (new_rank, row['reddit_name'])

    print "all done"
Exemple #27
0
def setup_config(config):
    app_abspath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    app_config = Config(app_abspath)
    app_config.from_object("superdesk.default_settings")
    cwd = Path.cwd()
    for p in [cwd] + list(cwd.parents):
        settings = p / "settings.py"
        if settings.is_file():
            logger.info(f"using local settings from {settings}")
            app_config.from_pyfile(settings)
            break
    else:
        logger.warning("Can't find local settings")

    update_config(app_config)

    app_config.setdefault("INSTALLED_APPS", [])

    # Extend the INSTALLED APPS with the list provided
    if config:
        config.setdefault("INSTALLED_APPS", [])
        app_config["INSTALLED_APPS"].extend(config.pop("INSTALLED_APPS", []))

    # Make sure there are no duplicate entries in INSTALLED_APPS
    app_config["INSTALLED_APPS"] = list(set(app_config["INSTALLED_APPS"]))

    app_config.update(
        config or {},
        **{
            "APP_ABSPATH": app_abspath,
            "DEBUG": True,
            "TESTING": True,
        },
    )

    logging.getLogger("apps").setLevel(logging.WARNING)
    logging.getLogger("elastic").setLevel(logging.WARNING)  # elastic datalayer
    logging.getLogger("urllib3").setLevel(logging.WARNING)
    logging.getLogger("celery").setLevel(logging.WARNING)
    logging.getLogger("superdesk").setLevel(logging.ERROR)
    logging.getLogger("elasticsearch").setLevel(logging.ERROR)
    logging.getLogger("superdesk.errors").setLevel(logging.CRITICAL)

    return {key: deepcopy(val) for key, val in app_config.items()}
Exemple #28
0
    def routing(self, alert: 'Alert') -> 'Tuple[Iterable[PluginBase], Config]':
        try:
            if self.plugins and self.rules:
                try:
                    r = self.rules(alert, self.plugins, config=self.config)
                except TypeError:
                    r = self.rules(alert, self.plugins)

                if isinstance(r, list):
                    return r, self.config
                else:
                    plugins, config = r
                    return plugins, Config('/', {**self.config, **config})

        except Exception as e:
            LOG.warning('Plugin routing rules failed: {}'.format(e))

        # default when no routing rules defined
        return self.plugins.values(), self.config
Exemple #29
0
def setup_config(config):
    app_abspath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    app_config = Config(app_abspath)
    app_config.from_object('superdesk.default_settings')

    update_config(app_config)
    app_config.update(config or {}, **{
        'APP_ABSPATH': app_abspath,
        'DEBUG': True,
        'TESTING': True,
    })

    logging.getLogger('apps').setLevel(logging.WARNING)
    logging.getLogger('elastic').setLevel(logging.WARNING)  # elastic datalayer
    logging.getLogger('urllib3').setLevel(logging.WARNING)
    logging.getLogger('celery').setLevel(logging.WARNING)
    logging.getLogger('superdesk').setLevel(logging.ERROR)
    logging.getLogger('elasticsearch').setLevel(logging.ERROR)
    return app_config
Exemple #30
0
def setup_config(config):
    app_abspath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    app_config = Config(app_abspath)
    app_config.from_object('superdesk.tests.test_settings')
    app_config['APP_ABSPATH'] = app_abspath

    app_config.update(get_test_settings())
    app_config.update(config or {})

    app_config.update({
        'DEBUG': True,
        'TESTING': True,
    })

    logging.getLogger('superdesk').setLevel(logging.WARNING)
    logging.getLogger('elastic').setLevel(logging.WARNING)  # elastic datalayer
    logging.getLogger('elasticsearch').setLevel(logging.WARNING)
    logging.getLogger('urllib3').setLevel(logging.WARNING)
    return app_config