How to use the pykern.pkconfig function in pykern

To help you get started, we’ve selected a few pykern examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github radiasoft / sirepo / sirepo / pkcli / job_agent.py View on Github external
def default_command():
#TODO(robnagler) commands need their own init hook like the server has
    job.init()
    global cfg

    cfg = pkconfig.init(
        agent_id=pkconfig.Required(str, 'id of this agent'),
        supervisor_uri=pkconfig.Required(
            str,
            'how to connect to the supervisor',
        ),
    )
    pkdlog('{}', cfg)
    i = tornado.ioloop.IOLoop.current()
    d = _Dispatcher()
    def s(n, x):
        return i.add_callback_from_signal(d.terminate)
    signal.signal(signal.SIGTERM, s)
    signal.signal(signal.SIGINT, s)
    i.spawn_callback(d.loop)
    i.start()
github radiasoft / sirepo / sirepo / auth / bluesky.py View on Github external
def init_apis(*args, **kwargs):
    global cfg
    cfg = pkconfig.init(
        secret=pkconfig.Required(
            str,
            'Shared secret between Sirepo and BlueSky server',
        ),
github radiasoft / sirepo / sirepo / srtime.py View on Github external
def _init():
    if pkconfig.channel_in_internal_test():
        from sirepo import uri_router
        uri_router.register_api_module()
    else:
        global utc_now_as_float, utc_now
        utc_now_as_float = time.time
        utc_now = datetime.datetime.utcnow
github radiasoft / sirepo / sirepo / srunit.py View on Github external
"""
    global server, app

    a = 'srunit_flask_client'
    if not cfg:
        cfg = PKDict()
    t = sim_types or CONFTEST_ALL_CODES
    if t:
        if isinstance(t, (tuple, list)):
            t = ':'.join(t)
        cfg['SIREPO_FEATURE_CONFIG_SIM_TYPES'] = t
    if not (server and hasattr(app, a)):
        from pykern import pkconfig

        # initialize pkdebug with correct values
        pkconfig.reset_state_for_testing(cfg)

        from pykern import pkunit
        with pkunit.save_chdir_work() as wd:
            from pykern import pkio
            cfg['SIREPO_SRDB_ROOT'] = str(pkio.mkdir_parent(wd.join('db')))
            pkconfig.reset_state_for_testing(cfg)
            from sirepo import server as s

            server = s
            app = server.init()
            app.config['TESTING'] = True
            app.test_client_class = _TestClient
            setattr(app, a, app.test_client())
    return getattr(app, a)
github radiasoft / sirepo / sirepo / simulation_db.py View on Github external
def _init():
    global SCHEMA_COMMON, cfg
    cfg = pkconfig.init(
        nfs_tries=(10, int, 'How many times to poll in hack_nfs_write_status'),
        nfs_sleep=(0.5, float, 'Seconds sleep per hack_nfs_write_status poll'),
        sbatch_display=(None, str, 'how to display sbatch cluster to user'),
    )
    fn = STATIC_FOLDER.join('json/schema-common{}'.format(JSON_SUFFIX))
    with open(str(fn)) as f:
        SCHEMA_COMMON = json_load(f)
    # In development, you can touch schema-common to get a new version
    SCHEMA_COMMON.version = _timestamp(fn.mtime()) if pkconfig.channel_in('dev') else sirepo.__version__
    SCHEMA_COMMON.common.enum.JobRunMode = _init_JobRunMode()
github radiasoft / sirepo / sirepo / runner / docker.py View on Github external
def __volumes(self):
        res = []

        def _res(src, tgt):
            res.append('--volume={}:{}'.format(src, tgt))

        if pkconfig.channel_in('dev'):
            for v in '~/src', '~/.pyenv':
                v = pkio.py_path(v)
                # pyenv and src shouldn't be writable, only rundir
                _res(v, v + ':ro')
        _res(self.run_dir, self.run_dir)
        return tuple(res)
github radiasoft / sirepo / sirepo / auth / email.py View on Github external
def _send_login_email(user, url):
    if not _smtp:
        assert pkconfig.channel_in('dev')
        pkdlog('{}', url)
        return http_reply.gen_json_ok({'url': url})
    login_text = u'sign in to' if user.user_name else \
        u'confirm your email and finish creating'
    msg = flask_mail.Message(
        subject='Sign in to Sirepo',
        sender=(cfg.from_name, cfg.from_email),
        recipients=[user.unverified_email],
        body=u'''
Click the link below to {} your Sirepo account.

This link will expire in {} hours and can only be used once.

{}
'''.format(login_text, _EXPIRES_MINUTES / 60, url)
    )
github radiasoft / sirepo / sirepo / srschema.py View on Github external
Validations performed:
        Values of default data (if any)
        Existence of dynamic modules
        Enums keyed by string value

    Args:
        schema (pkcollections.Dict): app schema
    """
    sch_models = schema.model
    sch_enums = schema.enum
    sch_ntfy = schema.notifications
    sch_cookies = schema.cookies
    for name in sch_enums:
        for values in sch_enums[name]:
            if not isinstance(values[0], pkconfig.STRING_TYPES):
                raise AssertionError(util.err(name, 'enum values must be keyed by a string value: {}', type(values[0])))
    for model_name in sch_models:
        sch_model = sch_models[model_name]
        for field_name in sch_model:
            sch_field_info = sch_model[field_name]
            if len(sch_field_info) <= 2:
                continue
            field_default = sch_field_info[2]
            if field_default == '' or field_default is None:
                continue
            _validate_enum(field_default, sch_field_info, sch_enums)
            _validate_number(field_default, sch_field_info)
    for n in sch_ntfy:
        if 'cookie' not in sch_ntfy[n] or sch_ntfy[n].cookie not in sch_cookies:
            raise AssertionError(util.err(sch_ntfy[n], 'notification must reference a cookie in the schema'))
    for sc in sch_cookies: