sirepo package

sirepo package

copyright:

Copyright (c) 2018-2024 RadiaSoft LLC.. All Rights Reserved.

license:

https://www.apache.org/licenses/LICENSE-2.0.html

Subpackages

Submodules

sirepo.agent_supervisor_api module

Utilities for agent to supervisor api requests

For example, sim_db_file and global_resources.

copyright:

Copyright (c) 2023 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

class sirepo.agent_supervisor_api.ReqBase(application: Application, request: HTTPServerRequest, **kwargs: Any)[source]

Bases: AuthHeaderRequestHandler

classmethod token_for_user(uid)[source]
write_error(status_code, *args, **kwargs)[source]

Override to implement custom error pages.

write_error may call write, render, set_header, etc to produce output as usual.

If this error was caused by an uncaught exception (including HTTPError), an exc_info triple will be available as kwargs["exc_info"]. Note that this exception may not be the “current” exception for purposes of methods like sys.exc_info() or traceback.format_exc.

sirepo.agent_supervisor_api.request(method, uri, token, data=None, json=None)[source]

sirepo.api_auth module

authentication and authorization routines

copyright:

Copyright (c) 2018 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.api_auth.assert_api_def(func)[source]
sirepo.api_auth.check_api_call(qcall, func)[source]
sirepo.api_auth.maybe_sim_type_required_for_api(func)[source]

sirepo.api_perm module

decorators for API permissions and the permissions themselves

copyright:

Copyright (c) 2018 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

class sirepo.api_perm.APIPerm(*values)[source]

Bases: Flag

An enumeration.

ALLOW_COOKIELESS_REQUIRE_USER = 1

A user is required but there might not be a cookie yet

ALLOW_COOKIELESS_SET_USER = 2

cookie set_user can be called even if a cookie wasn’t received

ALLOW_SIM_TYPELESS_REQUIRE_EMAIL_USER = 8

a logged in email user is required but they don’t have to have a role for the sim type

ALLOW_VISITOR = 4

anybody can view this page, even without cookies

INTERNAL_TEST = 256

only usable on internal test systems

REQUIRE_ADM = 16

only users with role adm

REQUIRE_AUTH_BASIC = 32

use basic auth authentication (only)

a cookie has to have been returned, which might contain a user

REQUIRE_PLAN = 512

a user with an active plan (any type) is required

REQUIRE_PREMIUM = 1024

a user with a premium plan is required

REQUIRE_USER = 128

a user will be created if necessary and auth may be necessary

sirepo.api_perm.ATTR = 'api_perm'

decorator sets this attribute with an APIPerm

sirepo.api_perm.SIM_TYPELESS_PERMS = {<APIPerm.ALLOW_COOKIELESS_SET_USER: 2>, <APIPerm.ALLOW_SIM_TYPELESS_REQUIRE_EMAIL_USER: 8>, <APIPerm.ALLOW_VISITOR: 4>, <APIPerm.REQUIRE_COOKIE_SENTINEL: 64>}

A user can access APIs decorated with these permissions even if they don’t have the role

sirepo.api_perm.allow_cookieless_require_user(func)
sirepo.api_perm.allow_cookieless_set_user(func)
sirepo.api_perm.allow_sim_typeless_require_email_user(func)
sirepo.api_perm.allow_visitor(func)
sirepo.api_perm.internal_test(func)
sirepo.api_perm.require_adm(func)
sirepo.api_perm.require_auth_basic(func)
sirepo.api_perm.require_plan(func)
sirepo.api_perm.require_premium(func)
sirepo.api_perm.require_user(func)

sirepo.auth_api module

apis for auth

copyright:

Copyright (c) 2022 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

class sirepo.auth_api.API(in_pkcli=False)[source]

Bases: API

api_authCompleteRegistration(**kwargs)
api_authLogout(**kwargs)
api_authState(**kwargs)
sirepo.auth_api.init_apis(*args, **kwargs)[source]

sirepo.auth_role module

User roles

copyright:

Copyright (c) 2021 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

class sirepo.auth_role.ModerationStatus(name, value=None, doc=None)[source]

Bases: NamedConstant

States used by auth_role_moderation and UserRoleModeration

APPROVE = <ModerationStatus.APPROVE: 'approve'>
CLARIFY = <ModerationStatus.CLARIFY: 'clarify'>
DENY = <ModerationStatus.DENY: 'deny'>
PENDING = <ModerationStatus.PENDING: 'pending'>
VALID_SET = frozenset({'approve', 'clarify', 'deny', 'pending'})
classmethod check(value)[source]
sirepo.auth_role.check(role)[source]
sirepo.auth_role.for_moderated_sim_types()[source]
sirepo.auth_role.for_new_user(auth_method)[source]
sirepo.auth_role.for_proprietary_oauth_sim_types()[source]
sirepo.auth_role.for_sim_type(sim_type)[source]
sirepo.auth_role.sim_type(role)[source]

sirepo.auth_role_moderation module

sirepo.const module

Constant values

copyright:

Copyright (c) 2021 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.const.DEV_SRC_RADIASOFT_DIR = '~/src/radiasoft/'

hardwired root of development src tree; Not a py.path, because must defer tilde evaluation

sirepo.const.LIB_DIR = 'lib'

where template resources and template non-sim user files live

sirepo.const.SCHEMA_COMMON = {'websocketMsg': {'kind': {'asyncMsg': 4, 'httpReply': 2, 'httpRequest': 1, 'srException': 3}, 'method': {'setCookies': 'setCookies'}, 'version': 1}}

These values will be injected into simulation_db.SCHEMA_COMMON

sirepo.const.SIM_DATA_BASENAME = 'sirepo-data.json'

Simulation file name saved both in sim db and run directory

sirepo.const.SIM_RUN_INPUT_BASENAME = 'in.json'

Simulation file name saved both in sim db and run directory

sirepo.cookie module

User state management via an HTTP cookie

copyright:

Copyright (c) 2015 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.cookie.init_module()[source]
sirepo.cookie.init_quest(qcall)[source]
sirepo.cookie.unchecked_http_name()[source]

http_name used in the cookie header

Should only be called by sirepo.auth.

Returns:

http_name or None

Return type:

str

sirepo.cron module

periodic callbacks

copyright:

Copyright (c) 2025 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

class sirepo.cron.CronTask(period, coro_func, params)[source]

Bases: object

Creates a task that runs a coroutine function periodically

coro_func is called at startup and then periodically.

coro_func must yield_to_event_loop to release processor

For global tasks, not associated with quest.

Parameters:
  • period (int or float) – how often to run job, must be greater than 60

  • coro_func (func) – async function

  • params (object) – passed verbatim to coro_func

destroy()[source]

Stop the polling process

classmethod init_class(loop)[source]

Initialized by service

If loop is None, then CronTasks do nothing (multi-server case).

Parameters:

loop (asyncio.EventLoop) – event loop or None

sirepo.crystal module

Get polarizability from X0h server (http://x-server.gmca.aps.anl.gov/x0h.html). For details see http://x-server.gmca.aps.anl.gov/pub/Stepanov_CR_1991_08.pdf.

sirepo.crystal.calc_bragg_angle(d, energy_eV, n=1)[source]

Calculate Bragg angle from the provided energy and d-spacing.

Parameters:
  • d (float) – interplanar spacing (d-spacing) [A].

  • energy_eV (float) – photon energy [eV].

  • n (int) – number of diffraction peak.

Returns:

the resulted dictionary with:

lamda (float): wavelength [nm]. bragg_angle (float): Bragg angle [rad]. bragg_angle_deg (float): Bragg angle [deg].

Return type:

dict

sirepo.crystal.get_crystal_parameters(material, energy_eV, h, k, l)[source]

Obtain parameters for the specified crystal and energy.

Parameters:
  • material (str) – material full name (e.g., ‘Silicon’).

  • energy_eV (float) – photon energy [eV].

  • h (int) – Miller’s index h.

  • k (int) – Miller’s index k.

  • l (int) – Miller’s index l.

Returns:

crystal parameters:

d (float): interplanar spacing (d-spacing) [A]. xr0 (float): real part of the 0-th Fourier component of crystal’s polarizability. xi0 (float): imaginary part of the 0-th Fourier component of crystal’s polarizability. xrh (float): real part of the H-th Fourier component of crystal’s polarizability (Sigma polarization). xih (float): imaginary part of the H-th Fourier component of crystal’s polarizability (Sigma polarization). bragg_angle_deg (float): Bragg angle [deg].

Return type:

dict

sirepo.csv module

wrappers to Python csv

copyright:

Copyright (c) 2024 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.csv.open_csv(path, encoding='utf-8')[source]
sirepo.csv.read_as_list(path, data_type=<class 'str'>)[source]
sirepo.csv.read_as_number_list(path)[source]

sirepo.db_upgrade module

sirepo.events module

Reigster callbacks for events and call callbacks when events are emitted.

Using events allows disparate areas of the code base to perform some task on an event without muddling the code that triggered the event. In addition events can be registered by configuration. This allows areas of the code to register/emit events when they are configured on and doesn’t require if statements throughout the code.

For example, when a user logs out the AUTH_LOGOUT event is emitted. Other areas of the code can register a callback for this event (ex to clear jupyterhub cookies so the user is logged out of jupyterhub too).

The events: - ‘auth_logout’ emitted when a user logs out, before the cookie is cleared.

kwargs contains uid.

  • ‘end_api_call’ emitted at the end of call_api. kwargs contains response object.

copyright:

Copyright (c) 2020 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.events.emit(qcall, event, kwargs=None)[source]

Call the handlers for event with kwargs

Handlers will be called in registration order (FIFO).

Parameters:
  • event (str) – one of the names in _MAP

  • kwargs (PKDict) – optional arguments to pass to event

sirepo.events.register(registrants)[source]

Register callback(s) for event(s)

Parameters:

registrants (PKDict) – Key is the event and value is the callback

sirepo.exporter module

sirepo.feature_config module

List of features available

To add a code that is not in the default list:

export SIREPO_FEATURE_CONFIG_SIM_TYPES=raydata:DEFAULT

copyright:

Copyright (c) 2016-2025 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.feature_config.auth_controlled_sim_types()[source]

All sim types that require granted authentication to access

Returns:

enabled sim types that require role

Return type:

frozenset

sirepo.feature_config.cfg()[source]

global configuration

Returns:

configurated features

Return type:

dict

sirepo.feature_config.for_sim_type(sim_type)[source]

Get cfg for simulation type

Parameters:

sim_type (str) – srw, elegent, etc.

Returns:

application specific config

Return type:

dict

sirepo.feature_config.have_payments()[source]
sirepo.feature_config.proprietary_sim_types()[source]

All sim types that have proprietary information and require granted access to use

Granted access can be through oauth or manual management of the role

Returns:

enabled sim types that require role

Return type:

frozenset

sirepo.file_lock module

file locking

copyright:

Copyright (c) 2023 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

class sirepo.file_lock.AsyncFileLock(path, qcall)[source]

Bases: _Base

Lock a file for global mutex

Parameters:
  • path (py.path.local) – base name for lock

  • qcall (sirepo.quest.API) – to ensure not re-entrant

class sirepo.file_lock.FileLock(path)[source]

Bases: _Base

sirepo.html module

html template

copyright:

Copyright (c) 2020 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.html.render(path)[source]

sirepo.http_request module

request input parsing

copyright:

Copyright (c) 2018 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.http_request.init_module(**imports)[source]
sirepo.http_request.parse_post(qcall, kwargs)[source]

Parse a post augmented by inline args

Arguments are either bool or another object. If a bool and True, the value is parsed from req_data. If another object, the value is parsed as is, setting on req_data.

The names of the args are the keys of the return value.

Parameters:
  • req_data (PKDict) – input values [body_as_dict]

  • type (object) – assert_sim_type

  • file_type (object) – sirepo.util.secure_filename

  • filename (object) – sirepo.util.secure_filename

  • folder (object) – parse_folder

  • id (object) – parse_sid

  • model (object) – parse_model

  • name (object) – parse_name

  • template (object) – sirepo.template.import_module

Returns:

with arg names set to parsed values

Return type:

PKDict

sirepo.http_util module

Support routines for http requests/responses

copyright:

Copyright (c) 2024 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.http_util.auth_header(token)[source]

Construct RFC6750 auth header compatible with requests.

Parameters:

token (str) – Secret to be included in the header

Returns

PKDict: auth header

sirepo.http_util.parse_auth_header(headers)[source]

Parse and retrieve RFC6750 bearer token.

Parameters:

headers (object) – Object containing a get method to retrieve headers by name.

Returns:

bearer token from header or None if invalid syntax

Return type:

str

sirepo.http_util.remote_ip(request)[source]

IP address of client from request.

Tornado covers ‘X-Real-Ip’ and ‘X-Forwared-For’. This adds addition headers to check.

Parameters:

request (tornado.httputil.HTTPServerRequest) – Incoming request

Returns:

IP address of client

Return type:

str

sirepo.hundli_console module

Hundli: a dog height and weight simulation engine

This is not technically part of Sirepo. It demonstrates a arbitrary 3rd party code, which Sirepo calls as an independent program.

copyright:

Copyright (c) 2018 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.hundli_console.main()[source]

Read the input yaml and write the output csv

sirepo.importer module

sirepo.job module

Common functionality that is shared between the server, supervisor, and driver.

copyright:

Copyright (c) 2019-2023 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.job.ACTIVE_STATUSES = frozenset({'pending', 'running'})

Queued or running

sirepo.job.AGENT_URI = '/job-agent-websocket'

path supervisor registers to receive messages from agent

sirepo.job.CANCELED = 'canceled'

status values

sirepo.job.CMD_COMPUTE = 'compute'

job_cmds

sirepo.job.CPU_SLOT_OPS = frozenset({'analysis', 'run'})

Types of slots required by op types

sirepo.job.DATA_FILE_ROOT = None

where user data files come in (job_supervisor)

sirepo.job.DATA_FILE_URI = '/job-cmd-data-file'

path supervisor registers to receive requests from job_process for file PUTs

sirepo.job.DEFAULT_IP = '127.0.0.1'

address where supervisor binds to

sirepo.job.DEFAULT_PORT = 8001

port supervisor listens on

sirepo.job.DEFAULT_SUPERVISOR_URI_DECL = ('http://127.0.0.1:8001', <class 'str'>, 'how to reach supervisor')

_cfg declaration for supervisor_uri for drivers

sirepo.job.EXIT_STATUSES = frozenset({'canceled', 'completed', 'error', 'job_run_purged', 'missing'})

When the job is inactive

sirepo.job.GLOBAL_RESOURCES_URI = '/global-resources'

path supervisor registers to receive requests from job_process for global resources

sirepo.job.JOB_CMD_STATE_SBATCH_RUN_STATUS_STOP = 'sbatch_run_status_stop'

For communication between job_agent and job_cmd

sirepo.job.KINDS = frozenset({'parallel', 'sequential'})

categories of jobs

sirepo.job.NERSC_QUEUE_MAX = {'cores': {'debug': 34816, 'premium': 56704, 'regular': 61824}, 'hours': {'debug': 0.5, 'premium': 48, 'regular': 48}}

Max values for each nersc queue

sirepo.job.OPS_WITHOUT_SLOTS = frozenset({'alive', 'begin_session', 'cancel', 'error', 'kill', 'ok', 'run_status', 'sbatch_agent_ready', 'sbatch_login'})

Ops which don’t need slot allocations or supervisor does not send

sirepo.job.OP_ALIVE = 'alive'

Agent indicates it is ready

sirepo.job.QUASI_SID_RE = re.compile('^_1_')

Allow sids for different kinds of jobs (not simulations)

sirepo.job.RUN_MODES = frozenset({'parallel', 'sbatch', 'sequential'})

valid jobRunMode values

sirepo.job.SEQUENTIAL = 'sequential'

jobRunMode and kinds; should come from schema

sirepo.job.SERVER_PING_URI = '/job-api-ping'

path supervisor registers to receive pings from server

sirepo.job.SERVER_SRTIME_URI = '/job-api-srtime'

path supervisor registers to receive srtime adjustments from server

sirepo.job.SERVER_URI = '/job-api-request'

path supervisor registers to receive requests from server

sirepo.job.SIM_DB_FILE_URI = '/sim-db-file'

path supervisor registers to receive requests from job_process for simulation file GETs/PUTs

sirepo.job.SLOT_OPS = frozenset({'analysis', 'io', 'run'})

All ops that have slots (see job_driver.DriverBase._slots_ready)

sirepo.job.STATE_OK = 'ok'

state value (other states are implicit statuses)

sirepo.job.STATUSES = frozenset({'canceled', 'completed', 'error', 'job_run_purged', 'missing', 'pending', 'running'})

Valid values for job status

sirepo.job.SUPERVISOR_SRV_ROOT = None

how jobs request files (absolute)

sirepo.job.SUPERVISOR_SRV_SUBDIR = 'supervisor-srv'

how jobs request files (relative to srdb.root)

sirepo.job.agent_cmd_stdin_env(cmd, env, uid, cwd='.', source_bashrc='')[source]

Convert cmd with env to script and cmd

Uses tempfile so the file can be closed after the subprocess gets the handle. You have to close stdin after calling tornado.process.Subprocess, which calls subprocess.Popen inline, since it’ not async.

Parameters:
  • cmd (iter) – list of words to be quoted

  • env (str) – empty or result of agent_env

  • uid (str) – which user should be logged in

  • cwd (str) – directory for the agent to run in (will be created if it doesn’t exist)

Returns:

new cmd (tuple), stdin (file), env (PKDict or None)

Return type:

tuple

sirepo.job.agent_env(uid, env=None)[source]

Convert to bash environment

Parameters:
  • uid (str) – which user is running this agent process

  • env (str) – empty or base environment

Returns:

bash environment export commands

Return type:

str

sirepo.job.cfg()[source]
sirepo.job.init_module()[source]
sirepo.job.is_ok_reply(value)[source]
sirepo.job.join_jid(uid, sid, compute_model)[source]

A Job is a tuple of user, sid, and compute_model.

A jid is words and dashes.

Parameters:
  • uid (str) – user id

  • sid (str) – simulation id

  • compute_model (str) – model name

Returns:

unique name (treat opaquely)

Return type:

str

sirepo.job.ok_reply()[source]
sirepo.job.quasi_jid(uid, op_key, method)[source]

Creates an id for a non-simulation job

Parameters:
  • uid (str) – user id

  • op_key (str) – “stful” or “stlss”

  • method (str) – statelessCompute or statefulCompute method

sirepo.job.sbatch_login_ok()[source]

Response for sbatchLogin API

Returns:

success response

Return type:

PKDict

sirepo.job.split_jid(jid)[source]

Split jid into named parts

Parameters:

jid (str) – properly formed job identifier

Returns:

parts named uid, sid, compute_model.

Return type:

PKDict

sirepo.job.supervisor_file_uri(supervisor_uri, *args)[source]

sirepo.job_api module

sirepo.job_supervisor module

sirepo.jupyterhub module

sirepo.lib module

Public functions from sirepo

Use this to call sirepo from other packages or Python notebooks.

copyright:

Copyright (c) 2020 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

class sirepo.lib.GenerateBase[source]

Bases: object

Common functionality between code specific Generate implementations.

property util
class sirepo.lib.Importer(sim_type, ignore_files=None, update_filenames=False)[source]

Bases: object

Imports a code’s native files into Sirepo representation

Parameters:
  • sim_type (str) – type of simulation (eg. ‘elegant’ or ‘madx’)

  • ignore_files (list) – files ignored during verification and symlink routines [None]

parse_file(path)[source]
class sirepo.lib.LibAdapterBase(ignore_files=None, update_filenames=False)[source]

Bases: object

Common functionality between code specific LibAdapter implementations.

input_filenames(data)[source]
class sirepo.lib.SimData(data, source, adapter)[source]

Bases: PKDict

Represents data of simulation

copy()[source]

Allows copy.deepcopy

input_filenames()[source]
write_files(dest_dir)[source]

Writes files for simulation state

Parameters:

dest_dir (str or py.path) – where to write files

Returns:

files written (debugging only)

Return type:

PKDict

sirepo.modules module

initialize modules based on mode

copyright:

Copyright (c) 2022 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.modules.import_and_init(name)[source]

sirepo.mpi module

Run Python processes in background

copyright:

Copyright (c) 2016 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.mpi.abort_on_signal_code()[source]
sirepo.mpi.cfg()[source]
sirepo.mpi.get_cmd()[source]
sirepo.mpi.restrict_op_to_first_rank(op)[source]

If the process has rank FIRST_RANK, call a function. Otherwise do nothing.

Use this to call a function that will cause conflicts if called by multiple processes, such as writing results to a file

Parameters:

op (function) – function to call

Returns:

result of op

Return type:

object

sirepo.mpi.restrict_ops_to_first_node(ops)[source]

Run ops in parallel on available cores on the first node

This functions uses multiprocessing to run ops. If run under MPI, will run instantiate the multiprocessing.Pool in the first rank only, and the other MPI ranks (processes) will remain idle until all ops have been run.

Note that processes originate from the first rank MPI process so they share the first rank’s memory up until multiprocessing spawns each op.

Parameters:

ops (iterable) – produces callables that will be run in parallel

sirepo.mpi.run_program(cmd, output='mpi_run.log', env=None)[source]

Execute python script with mpi.

Parameters:
  • cmd (list) – cmd to run

  • output (str) – where to write stdout and stderr

  • env (dict) – what to pass as env

sirepo.mpi.run_script(script)[source]

Execute python script with mpi.

Parameters:

script (str) – python text

sirepo.nersc module

Methods related to running on NERSC

copyright:

Copyright (c) 2023 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.nersc.sbatch_project_option(project)[source]

sirepo.numpy module

Wrappers for numpy

copyright:

Copyright (c) 2022 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.numpy.ndarray_from_csv(path, skip_header, **kwargs)[source]
sirepo.numpy.ndarray_from_generator(generator, skip_header, **kwargs)[source]

sirepo.oauth module

oauth for authentication and role moderation

copyright:

Copyright (c) 2022 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.oauth.check_authorized_callback(qcall)[source]
sirepo.oauth.raise_authorize_redirect(qcall, sim_type)[source]

sirepo.payments module

Payment handling with Stripe

copyright:

Copyright (c) 2025 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

class sirepo.payments.API(in_pkcli=False)[source]

Bases: API

api_paymentCheckoutSessionStatus(**kwargs)
api_paymentCreateCheckoutSession(**kwargs)
api_paymentPlanRedirect(**kwargs)
api_stripeWebhook(**kwargs)
sirepo.payments.cfg()[source]
sirepo.payments.init_apis(*args, **kwargs)[source]

sirepo.quest module

Requests hold context for API calls

copyright:

Copyright (c) 2019-2022 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

class sirepo.quest.API(in_pkcli=False)[source]

Bases: API

Holds request context for all API calls.

absolute_uri(uri)[source]

Convert to an absolute uri

Parameters:

uri (str) – must begin with “/”

Returns:

absolute uri

Return type:

str

attr_set(name, obj)[source]

Assign an object to qcall

body_as_dict()[source]
bucket_get(name)[source]
bucket_get_or_default(name, default)[source]

Get named value or pksetdefault

Parameters:
  • name (str) – key

  • default (object) – if callable, will be called

Returns:

value of name

Return type:

object

bucket_set(name, value)[source]
bucket_unchecked_get(name)[source]
async call_api(name, kwargs=None, body=None)[source]

Calls uri_router.call_api, which calls the API with permission checks.

Parameters:
  • name (object) – api name (without api_ prefix)

  • kwargs (PKDict) – to be passed to API [None]

  • body (PKDict) – will be returned self.body_as_dict [None]

Returns:

result

Return type:

Reply

call_api_sync(*args, **kwargs)[source]

Synchronous call_api

Only use in tests.

destroy(commit=False)[source]
headers_for_cache(resp, path=None)[source]
headers_for_no_cache(resp)[source]
parent_set(parent)[source]

Links parent qcall to self and copies Attrs

Parameters:

parent (API) – qcall to link as parent

parse_params(**kwargs)[source]
parse_post(**kwargs)[source]
reply(**kwargs)[source]
reply_as_proxy(content, content_type)[source]
reply_attachment(content_or_path, filename=None)[source]
reply_dict(value)[source]
reply_error(error)[source]
reply_file(path, filename=None)[source]
reply_html(path)[source]
reply_list_deprecated(value)[source]

Always reply_dict, not with a list

reply_ok(value=None)[source]
reply_redirect(uri)[source]
reply_redirect_for_app_root(sim_type=None)[source]
reply_redirect_for_local_route(sim_type=None, route=None, params=None, query=None, **kwargs)[source]
reply_static_jinja(base, ext, j2_ctx)[source]
sim_type_set(sim_type)[source]

Set sim_type if there, else don’t set

sim_type_set_from_spec(func)[source]
sim_type_uget(value=None)[source]

Return value or reuqest’s sim_type

Parameters:

value (str) – will be validated if not None

Returns:

sim_type or possibly None

Return type:

str

uri_for_api(api_name, params=None)[source]

Generate uri for api method

Parameters:
  • api_name (str) – full name of api

  • params (PKDict) – paramters to pass to uri

Returns:

formmatted URI

Return type:

str

uri_for_app_root(sim_type=None)[source]

Return uri for sim_type

Parameters:

sim_type (str) – sim_type (must be defined)

Returns:

uri

Return type:

str

user_agent_headers()[source]
class sirepo.quest.Attr(qcall, init_quest_for_child=False, **kwargs)[source]

Bases: PKDict

detach_from_quest()[source]

Useful only for _SReply

Detaches from the quest so won’t be destroyed.

Returns:

object

Return type:

self

init_quest_for_child(child, parent)[source]

Create or copy state of self (parent) to child (return)

self is the Attr in parent

If sharing between parent and child, care should be taken.

Parameters:
  • child (API) – child quest that is being initialized

  • parent (API) – parent quest to initialize from

Returns:

instance to be assigned to child

Return type:

Attr

class sirepo.quest.Spec(perm, **kwargs)[source]

Bases: Spec

sirepo.quest.init_module(**imports)[source]
sirepo.quest.start(in_pkcli=False)[source]

Create a qcall after importing and initializing sirepo.auth

Parameters:

in_pkcli (bool) – if is inside a pkcli

Yields:

API – newly created qcall

sirepo.reply module

Reply hold the response to API calls.

Replies are independent of the web platform (tornado http or websocket). They are converted to the native format by the platform dispatcher at the time. Internal call_api returns an _SReply object.

copyright:

Copyright (c) 2023 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.reply.CACHE_MAX_AGE = 43200

default Max-Age header

sirepo.reply.SR_EXCEPTION_STATE = 'srException'

data.state for srException

sirepo.reply.init_module(**imports)[source]
sirepo.reply.init_quest(qcall)[source]

sirepo.request module

Requests hold context for API calls

copyright:

Copyright (c) 2022 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.request.init_quest(qcall, internal_req=None)[source]

sirepo.resource module

Functions for accessing resource files

copyright:

Copyright (c) 2021 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.resource.file_path(*paths)[source]

Get absolute path to a resource file

Parameters:

paths (str) – Names of paths to get to resource file

Returns:

absolute path to resource file

Return type:

py.path

sirepo.resource.glob_paths(*paths)[source]

Get matching paths

Parameters:

paths (str) – Path components of file

Returns:

paths that match pattern

Return type:

[py.path]

sirepo.resource.render_jinja(*paths, target_dir=None, j2_ctx=None)[source]

Render a resource template file with Jinja into target_dir.

Parameters:
  • paths (str) – Path components of resource file without pykern.pkjinja.RESOURCE_SUFFIX

  • target_dir (py.path) – target directory for rendered file

  • j2_ctx (PKDict) – parameters to jinja file

Returns:

output path which is target_dir.join(paths[-1])

Return type:

py.path

sirepo.resource.root_modules()[source]

Get all root modules in package_path

Returns:

root modules

Return type:

[module]

sirepo.resource.static(*paths)[source]

Absolute or relative path to resource static file

Parameters:

paths (str) – Path components of file

Returns:

path to file

Return type:

py.path

sirepo.resource.static_files()[source]

Generate all, non-overlapping, non-dot static files

Yields:

(str, py.path) – relative path (including static) and absolute paths

sirepo.resource.static_paths_for_type(file_type)[source]

Get paths of static file of type

Parameters:

file_type (str) – The type of file (ex json)

Returns:

paths that match pattern

Return type:

[py.path]

sirepo.resource.static_url(*paths)[source]

Get url for static file

Parameters:

paths (str) – Path components of file

Returns:

url for file

Return type:

str

sirepo.server module

sirepo.sim_db_file module

Operating on simulation db files via the job agent

copyright:

Copyright (c) 2020 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

class sirepo.sim_db_file.SimDbClient(sim_data)[source]

Bases: object

Client to be used from the job agent.

An instance is created and is accessed via SimData.sim_db_client.

LIB_DIR = 'lib'
copy(src_uri, dst_uri)[source]

Copy src_uri to dst_uri

Parameters:
delete_glob(lib_sid_uri, path_prefix, sim_type=None)[source]

deletes files that begin with path_prefix

exists(lib_sid_uri, basename=None, sim_type=None)[source]

Tests if file exists

get(lib_sid_uri, basename=None, sim_type=None)[source]
move(src_uri, dst_uri)[source]

Rename src_uri to dst_uri

Parameters:
put(lib_sid_uri, basename, path_or_content, sim_type=None)[source]
read_sim(lib_sid_uri, sim_type=None)[source]
save_from_url(src_url, dst_uri)[source]
save_sim(sdata)[source]
size(lib_sid_uri, basename=None, sim_type=None)[source]
uri(lib_sid_uri, basename=None, sim_type=None)[source]

Create a SimDbUri

lib_sid_uri may be sirepo.const.LIB_DIR, a simulation id, or a SimDbUri. In the latter case, the uri must match sim_type (if supplied), and basename must be None (or match uri).

Parameters:
  • lib_sid_uri (object) – see above

  • basename (str) – name without directories (see above)

  • sim_type (str) – valid code [sim_data.sim_type]

Returns:

valid in any string context

Return type:

SimDbUri

class sirepo.sim_db_file.SimDbServer(application: Application, request: HTTPServerRequest, **kwargs: Any)[source]

Bases: ReqBase

class sirepo.sim_db_file.SimDbUri(sim_type, slu, basename)[source]

Bases: str

Identifies the relative path to a file in the simulation db.

The value is a URI comprised of parts: sim_type, sim_id or LIB_DIR and basename.

If slu is a SimDbUri, basename must match it or be None. sim_type must match the SimDbUri.

Parameters:
  • sim_type (str) – simulation code

  • slu (object) – a SimDbUri, LIB_DIR, or a sim_id

  • basename (str) – valid simple file name

sirepo.sim_db_file.in_job_agent()[source]

sirepo.sim_run module

sirepo.simulation_db module

sirepo.sirepo_console module

Front-end command line for sirepo.

See pykern.pkcli for how this module is used.

copyright:

Copyright (c) 2015-2024 RadiaSoft LLC. All Rights Reserved.

license:

https://www.apache.org/licenses/LICENSE-2.0.html

sirepo.sirepo_console.main()[source]

sirepo.smtp module

SMTP connection to send emails

copyright:

Copyright (c) 2018-2024 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.smtp.send(recipient, subject, body)[source]

sirepo.spa_session module

Manage user sessions

copyright:

Copyright (c) 2022 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.spa_session.init_module()[source]
async sirepo.spa_session.maybe_begin(qcall)[source]

sirepo.srdb module

db configuration

copyright:

Copyright (c) 2019 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.srdb.proprietary_code_dir(sim_type)[source]

Directory for proprietary code binaries

Parameters:

sim_type (str) – not validated code name

sirepo.srdb.root()[source]
sirepo.srdb.supervisor_dir()[source]

Directory for supervisor job db

sirepo.srschema module

Simulation schema

copyright:

Copyright (c) 2018 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.srschema.get_enums(schema, name)[source]
sirepo.srschema.parse_folder(folder)[source]

Verifies syntax of folder is correct

Parameters:

folder (str) – what to validate

Returns:

cleaned up folder name

Return type:

str

sirepo.srschema.parse_name(name)[source]

Verifies syntax of simulation is correct

Parameters:

folder (str) – what to validate

Returns:

cleaned up folder name

Return type:

str

sirepo.srschema.validate(schema)[source]

Validate the schema

Validations performed:

Values of default data (if any) Existence of dynamic modules Enums keyed by string value Model names containing special characters Method name for API calls with them are valid python function names and not too long

Parameters:

schema (PKDict) – app schema

sirepo.srschema.validate_fields(data, schema)[source]

Validate the values of the fields in model data

Validations performed:

enums (see _validate_enum) numeric values (see _validate_number)

Parameters:
  • data (PKDict) – model data

  • schema (PKDict) – schema which data inmplements

sirepo.srschema.validate_name(data, data_files, max_copies)[source]

Validate and if necessary uniquify name

Parameters:
  • data (dict) – what to validate

  • data_files (list) – simulation files already in the folder

sirepo.srtime module

time functions (artificial time)

copyright:

Copyright (c) 2019 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

class sirepo.srtime.API(in_pkcli=False)[source]

Bases: API

api_adjustTime(**kwargs)
sirepo.srtime.EPOCH = datetime.datetime(1970, 1, 1, 0, 0)

POSIX epoch as object

sirepo.srtime.adjust_time(days)[source]

Shift the system time by days

Parameters:

days (str) – must be integer. If None or 0, clear the adjustment.

sirepo.srtime.init_apis(*args, **kwargs)[source]
sirepo.srtime.init_module()[source]
sirepo.srtime.to_timestamp(dt)[source]

Convert datetime into float seconds from epoch

Parameters:

dt (datetime) – datetime object

Returns:

seconds since epoch

Return type:

float

sirepo.srtime.utc_now()[source]

Adjusted UTC time as object

Returns:

adjusted pykern.pkcompat.utcnow

Return type:

datetime.datetime

sirepo.srtime.utc_now_as_float()[source]

Adjusted POSIX time as a float

Returns:

adjusted time.time

Return type:

float

sirepo.srtime.utc_now_as_milliseconds()[source]

Adjusted POSIX time as milliseconds

Returns:

adjusted time.time as milliseconds

Return type:

int

sirepo.srunit module

Support for unit tests

copyright:

Copyright (c) 2016 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.srunit.CONFTEST_DEFAULT_CODES = None

set by conftest.py

exception sirepo.srunit.JobAgentError[source]

Bases: RuntimeError

sirepo.srunit.MYAPP = 'myapp'

Default “app”

exception sirepo.srunit.ServerRequestError[source]

Bases: RuntimeError

sirepo.srunit.http_client(env=None, sim_types=None, job_run_mode=None, empty_work_dir=True, port=None)[source]

Create an http_client that talks to server

sirepo.srunit.quest_start(want_user=False, cfg=None)[source]
sirepo.srunit.setup_srdb_root(cfg=None)[source]
sirepo.srunit.template_import_file(sim_type, path, arguments=None)[source]

Call stateful_compute_import_file

Parameters:
  • sim_type (str) – template name

  • path (object) – if path is a str, will be joined with data_dir,

  • arguments (object) – literal passed as import_file_arguments

Returns:

imported_data if successful; otherwise, error state

Return type:

PKDict

sirepo.srunit_api module

Support for sirepo.srunit tests

copyright:

Copyright (c) 2023 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

class sirepo.srunit_api.API(in_pkcli=False)[source]

Bases: API

api_srUnitCase(**kwargs)
sirepo.srunit_api.init_apis(*args, **kwargs)[source]

sirepo.srunit_servers module

start servers for unit tests

copyright:

Copyright (c) 2024 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.srunit_servers.api_and_supervisor(pytest_req, fc_args)[source]
sirepo.srunit_servers.sim_db_file(pytest_req)[source]

sirepo.status module

sirepo.tornado module

Wrappers for Tornado

copyright:

Copyright (c) 2020 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

class sirepo.tornado.AuthHeaderRequestHandler(application: Application, request: HTTPServerRequest, **kwargs: Any)[source]

Bases: RequestHandler

async get(*args, **kwargs)[source]
classmethod get_header(token)[source]
async post(*args, **kwargs)[source]
async put(*args, **kwargs)[source]
class sirepo.tornado.Event[source]

Bases: Event

Event with ordered waiters.

When the event is set the waiters are awoken in a FIFO order.

class sirepo.tornado.Queue(maxsize: int = 0)[source]

Bases: Queue

async get()[source]

Implements a cancelable Queue.get

See https://github.com/radiasoft/sirepo/issues/2375

sirepo.tornado.error_forbidden()[source]
sirepo.tornado.error_not_found()[source]

sirepo.uri module

uri formatting

copyright:

Copyright (c) 2019 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

sirepo.uri.PARAM_RE = '([\\?\\*]?)<{}>'

route parsing

sirepo.uri.PATH_INFO_CHAR = '*'

optional parameter that consumes rest of parameters

sirepo.uri.app_root(sim_type=None)[source]

Generate uri for application root

Parameters:

sim_type (str) – application name [None]

Returns:

formatted URI

Return type:

str

sirepo.uri.decode_to_str(encoded)[source]
sirepo.uri.default_local_route_name(schema)[source]
sirepo.uri.init_module(**imports)[source]
sirepo.uri.is_sr_exception_only(sim_type, route_name)[source]

local route has srExceptionOnly param

Parameters:
  • sim_type (str) – simulation type (must be valid)

  • route_name (str) – a local route

Returns:

True if srExceptionOnly, else False; None if route not found

Return type:

object

sirepo.uri.local_route(sim_type, route_name=None, params=None, query=None)[source]

Generate uri for local route with params

Parameters:
  • sim_type (str) – simulation type (must be valid)

  • route_name (str) – a local route [defaults to local default]

  • params (dict) – paramters to pass to route

  • query (dict) – query values (joined and escaped)

Returns:

formatted URI

Return type:

str

sirepo.uri.server_route(route_or_uri, params, query)[source]

Convert name to uri found in SCHEMA_COMMON

Parameters:
  • route_or_uri (str) – route or uri

  • params (dict) – parameters to apply to route

  • query (dict) – query string values

Returns:

URI

Return type:

str

sirepo.uri.unchecked_root_redirect(path)[source]

sirepo.uri_router module

Handles dispatching of uris to server.api_* functions

copyright:

Copyright (c) 2017 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

async sirepo.uri_router.call_api(qcall, name, kwargs=None, body=None)[source]

Should not be called outside of Base.call_api(). Use self.call_api() to call API.

Call another API with permission checks.

Parameters:
  • qcall (quest.API) – request object

  • route_or_name (object) – api function or name (without api_ prefix)

  • kwargs (PKDict) – to be passed to API [None]

  • body (PKDict) – will be returned qcall.body_as_dict

Returns:

result

Return type:

Response

sirepo.uri_router.init_module(want_apis, **imports)[source]

Convert route map to dispatchable callables

Initializes _uri_to_route

sirepo.uri_router.maybe_sim_type_required_for_api(qcall)[source]
sirepo.uri_router.register_api_module(module)[source]

Add caller_module to the list of modules which implements apis.

The module must have methods: api_XXX which do not collide with other apis. It may also have init_apis(), which will be called unless it is already registered.

Parameters:

module (module or str) – name of module or module

sirepo.uri_router.start_tornado(ip, port, debug, is_primary=True)[source]

Start tornado server, does not return

sirepo.uri_router.uri_for_api(api_name, params=None)[source]

Generate uri for api method

Parameters:
  • api_name (str) – full name of api

  • params (PKDict) – paramters to pass to uri

Returns:

formatted URI

Return type:

str

sirepo.util module

Support routines and classes, mostly around errors and I/O.

copyright:

Copyright (c) 2018 RadiaSoft LLC. All Rights Reserved.

license:

http://www.apache.org/licenses/LICENSE-2.0.html

exception sirepo.util.BadRequest(*args, **kwargs)[source]

Bases: ReplyExc

Raised for bad request

exception sirepo.util.ContentTooLarge(*args, **kwargs)[source]

Bases: ReplyExc

The content requested by the user was too large (ex large data file)

exception sirepo.util.Error(error, *args, **kwargs)[source]

Bases: ReplyExc

Raised to send an error response

Parameters:

error_msg (str) – just the error to output to user

exception sirepo.util.Forbidden(*args, **kwargs)[source]

Bases: ReplyExc

Raised for forbidden

exception sirepo.util.InvalidEmail(*args, **kwargs)[source]

Bases: ReplyExc

Email user is trying to register with is malformed or domain is on deny list.

exception sirepo.util.NotFound(*args, **kwargs)[source]

Bases: ReplyExc

Raised for not found

exception sirepo.util.OKReplyExc(*args, **kwargs)[source]

Bases: ReplyExc

When a ReplyExc exception is a successful response

exception sirepo.util.PlanExpired(*args, **kwargs)[source]

Bases: ReplyExc

API requires and active plan

exception sirepo.util.Redirect(uri, *args, **kwargs)[source]

Bases: OKReplyExc

Raised to redirect

Parameters:
  • uri (str) – where to redirect to

  • log_fmt (str) – server side log data

exception sirepo.util.ReplyExc(*args, **kwargs)[source]

Bases: Exception

Raised to end the request.

Parameters:
  • sr_args (dict) – exception args that Sirepo specific

  • log_fmt (str) – server side log data

exception sirepo.util.SPathNotFound(sim_type, uid, sid, *args, **kwargs)[source]

Bases: NotFound

Raised by simulation_db

Parameters:
  • sim_type (str) – simulation type

  • uid (str) – user

  • sid (str) – simulation id

exception sirepo.util.SRException(route_name, params, *args, **kwargs)[source]

Bases: ReplyExc

Raised to communicate a local redirect and log info

params may have simulationType, which will be used for routeName rendering. Otherwise, sim_type on qcall will be used.

Parameters:
  • route_name (str) – a local route

  • params (dict) – for route url or for srExceptionOnly case

  • log_fmt (str) – server side log data

exception sirepo.util.SReplyExc(sreply, *args, **kwargs)[source]

Bases: OKReplyExc

Raise with an SReply object

Parameters:
  • sreply (object) – what the reply should be

  • log_fmt (str) – server side log data

exception sirepo.util.ServerError(*args, **kwargs)[source]

Bases: ReplyExc

Raised for server error

sirepo.util.TOKEN_SIZE = 16

length of string returned by create_token

sirepo.util.UNIQUE_KEY_CHARS_RE = '\\w+'

Matches anything generated by unique_key

Type:

POSIT

sirepo.util.UNIQUE_KEY_RE = re.compile('^\\w+$')

A standalone unique key

exception sirepo.util.Unauthorized(*args, **kwargs)[source]

Bases: ReplyExc

Raised to generate 401 response

exception sirepo.util.UserAlert(display_text, *args, **kwargs)[source]

Bases: ReplyExc

Raised to display a user error and log info

Parameters:
  • display_text (str) – string that user will see

  • log_fmt (str) – server side log data

exception sirepo.util.UserDirNotFound(user_dir, uid, *args, **kwargs)[source]

Bases: NotFound

Raised by simulation_db

Parameters:
  • user_dir (py.path) – directory not found

  • uid (str) – user

exception sirepo.util.WWWAuthenticate(*args, **kwargs)[source]

Bases: ReplyExc

Raised to generate 401 response with WWWAuthenticate response

sirepo.util.assert_sim_type(sim_type)[source]

Validate simulation type

Parameters:

sim_type (str) – to check

Returns:

validated sim_type

Return type:

str

sirepo.util.create_token(value)[source]
sirepo.util.err(obj, fmt='', *args, **kwargs)[source]
sirepo.util.files_to_watch_for_reload(*extensions)[source]
sirepo.util.find_obj(arr, key, value)[source]

Return the first object in the array such that obj[key] == value

Parameters:
  • arr (list) – list of dict-like objects

  • key (str) – object key

  • value – value

Returns:

the object, or None if not found

Return type:

object

sirepo.util.first_sim_type()[source]

Returns the first configured sim_type

sirepo.util.import_submodule(submodule, type_or_data)[source]

Import fully qualified module that contains submodule for sim type

sirepo.feature_config.package_path will be searched for a match.

Parameters:
  • submodule (str) – the name of the submodule

  • type_or_data (str or dict) – simulation type or description

Returns:

simulation type module instance

Return type:

module

sirepo.util.is_jupyter_enabled()[source]
sirepo.util.is_python_identifier(name)[source]
sirepo.util.is_sim_type(sim_type)[source]

Validate simulation type

Parameters:

sim_type (str) – to check

Returns:

true if is a sim_type

Return type:

bool

sirepo.util.json_dump(obj, path=None, pretty=False, **kwargs)[source]

Formats as json as string, and writing atomically to disk

Parameters:
  • obj (object) – any Python object

  • path (py.path) – where to write (atomic) [None]

  • pretty (bool) – pretty print [False]

  • kwargs (object) – other arguments to json.dumps

Returns:

sorted and formatted JSON

Return type:

str

sirepo.util.json_path(path, run_dir=None)[source]

Append sirepo.const.JSON_SUFFIX if necessary and convert to str

Parameters:
  • path (py.path or str) – to convert

  • run_dir (py.path) – which directory to join (only if path is str)

Returns:

path.json

Return type:

py.path

sirepo.util.json_read(path)[source]

Read data from json file

Parameters:

path (py.path or str) – will append sirepo.const.JSON_SUFFIX if necessary

Returns:

json converted to python

Return type:

object

sirepo.util.numpy_to_py(obj)[source]

Convert numpy objects to Python objects

Use to avoid repr conversions.

Parameters:

obj (object) – source

Returns:

no numpy.floating objects

Return type:

object

sirepo.util.plan_role_expiration(role)[source]

Get expiration for an (asserted) plan

Parameters:

role (str) – plan to change to

Returns:

new expiration

Return type:

datetime

sirepo.util.random_base62(length=32, prefix=None)[source]

Returns a safe string of sufficient length to be a nonce

Parameters:

length (int) – how long to make the base62 string [32]

Returns:

random base62 characters

Return type:

str

sirepo.util.read_zip(path_or_bytes)[source]

Read the contents of a zip archive.

Protects against malicious filenames (ex ../../filename)

Parameters:

path_or_bytes (py.path or str or bytes) – The path to the archive or it’s contents

Returns:

The basename of the file, the contents of the file

Return type:

(py.path, bytes)

sirepo.util.sanitize_string(string)[source]

Remove special characters from string

This results in a string the is a valid python identifier. This string can also be used as a css id because valid python identifiers are also valid css ids.

Parameters:

string (str) – The string to sanatize

Returns:

A string with special characters replaced

Return type:

(str)

sirepo.util.secure_filename(path)[source]

Converts a user supplied path to a secure file

Parameters:

path (str) – contains anything

Returns:

does not contain special file system chars or path chars

Return type:

str

sirepo.util.setattr_imports(imports)[source]
sirepo.util.split_comma_delimited_string(s, f_type)[source]
sirepo.util.to_comma_delimited_string(arr)[source]
sirepo.util.unique_key()[source]
sirepo.util.url_safe_hash(value)[source]
sirepo.util.validate_path(uri)[source]

Ensures path component of uri is safe

Very strict. Doesn’t allow any dot files and few specials.

Parameters:

uri (str) – uncheck path

Returns:

validated path

Return type:

str

sirepo.util.write_zip(path)[source]
async sirepo.util.yield_to_event_loop()[source]

Documents and wraps asyncio.sleep(0)

If a server (api, supervisor, agent) is doing a lot of work, call this routine to release the processor to the event loop.