How to use pykern - 10 common examples

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_supervisor_tornado.py View on Github external
async def process_server_request(self, request, send_to_server):
        pkdlog(f'Processing server request. Request: {request}')
        reply_sent = tornado.locks.Event()
        request.request_id = str(uuid.uuid4())
        work_to_do = pkcollections.Dict(request)
        self._server_responses[request.request_id] = pkcollections.Dict({
            'send': send_to_server,
            'reply_sent': reply_sent,
        })
        await self._driver_work_q.put(work_to_do)
        await reply_sent.wait()
        del self._server_responses[request.request_id]
github radiasoft / sirepo / sirepo / template / elegant.py View on Github external
def parse_elegant_log(run_dir):
    path = run_dir.join(ELEGANT_LOG_FILE)
    if not path.exists():
        return '', 0
    res = ''
    last_element = None
    text = pkio.read_text(str(path))
    want_next_line = False
    prev_line = ''
    prev_err = ''
    for line in text.split('\n'):
        if line == prev_line:
            continue
        match = re.search('^Starting (\S+) at s\=', line)
        if match:
            name = match.group(1)
            if not re.search('^M\d+\#', name):
                last_element = name
        if want_next_line:
            res += line + '\n'
            want_next_line = False
        elif _is_ignore_error_text(line):
            pass
github radiasoft / sirepo / sirepo / template / synergia.py View on Github external
def parse_synergia_log(run_dir):
    if not run_dir.join(template_common.RUN_LOG).exists():
        return None
    text = pkio.read_text(run_dir.join(template_common.RUN_LOG))
    errors = []
    current = ''
    for line in text.split("\n"):
        if not line:
            if current:
                errors.append(current)
                current = ''
            continue
        m = re.match('\*\*\* (WARR?NING|ERROR) \*\*\*(.*)', line)
        if m:
            if not current:
                error_type = m.group(1)
                if error_type == 'WARRNING':
                    error_type = 'WARNING'
                current = '{}: '.format(error_type)
            extra = m.group(2)
github radiasoft / sirepo / sirepo / template / flash.py View on Github external
def _generate_parameters_file(data):
    res = ''
    names = {}
    for line in pkio.read_text(_FLASH_UNITS_PATH[data.models.simulation.flashType]).split('\n'):
        name = ''
        #TODO(pjm): share with setup_params parser
        for part in line.split('/'):
            if not re.search('Main$', part):
                name += (':' if len(name) else '') + part
        names[name] = line
    for m in sorted(data.models):
        if m in names:
            if m not in _SCHEMA.model:
                # old model which was removed from schema
                continue
            schema = _SCHEMA.model[m]
            heading = '# {}\n'.format(names[m])
            has_heading = False
            for f in sorted(data.models[m]):
                if f not in schema:
github radiasoft / sirepo / sirepo / auth / guest.py View on Github external
def init_apis(*args, **kwargs):
    global cfg
    cfg = pkconfig.init(
        expiry_days=(None, _cfg_login_days, 'when auth login expires'),
    )
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()