Repository: radiasoft/sirepo

Issue:
add values from proprietary_sim_types to sim_types
proprietary_sim_types should be unioned with sim_types so that any proprietary_sim_types is guaranteed in sim_types.  This makes configuration management easier.

default proprietary sim types can include flash for dev, but not alpha, etc.



Discussion:

Patch:
diff --git a/sirepo/feature_config.py b/sirepo/feature_config.py
index d424c8e4e..36cd39bb9 100644
--- a/sirepo/feature_config.py
+++ b/sirepo/feature_config.py
@@ -36,7 +36,8 @@ ALPHA_CODES = frozenset((
 ALL_CODES = NON_ALPHA_CODES.union(ALPHA_CODES)
 
 
-_DEFAULT_PROPRIETARY_CODES = ('flash',)
+#: codes which we include in dev for testing
+_DEV_PROPRIETARY_CODES = ('flash',)
 
 
 #: Configuration
@@ -76,32 +77,19 @@ def _init():
     from pykern import pkconfig
     global _cfg
 
-    @pkconfig.parse_none
-    def _cfg_sim_types(value):
-        res = pkconfig.parse_set(value)
-        if not res:
-            return tuple(_codes())
-        for c in res:
-            assert c in _codes(), \
-                'invalid sim_type={}, expected one of={}'.format(c, _codes())
-        if 'jspec' in res:
-            res = set(res)
-            res.add('elegant')
-        return tuple(res)
-
-    def _codes():
-        return ALL_CODES if pkconfig.channel_in_internal_test() \
-            else NON_ALPHA_CODES
-
     _cfg = pkconfig.init(
         api_modules=((), set, 'optional api modules, e.g. status'),
         jspec=dict(
             derbenevskrinsky_force_formula=(pkconfig.channel_in_internal_test(), bool, 'Include Derbenev-Skrinsky force forumla'),
         ),
-        proprietary_sim_types=(_DEFAULT_PROPRIETARY_CODES, set, 'codes that require authorization'),
+        proprietary_sim_types=(
+            _DEV_PROPRIETARY_CODES if pkconfig.channel_in('dev') else set(),
+            set,
+            'codes that require authorization',
+        ),
         #TODO(robnagler) make sim_type config
         rs4pi_dose_calc=(False, bool, 'run the real dose calculator'),
-        sim_types=(None, _cfg_sim_types, 'simulation types (codes) to be imported'),
+        sim_types=(set(), set, 'simulation types (codes) to be imported'),
         srw=dict(
             mask_in_toolbar=(pkconfig.channel_in_internal_test(), bool, 'Show the mask element in toolbar'),
             beamline3d=(pkconfig.channel_in_internal_test(), bool, 'Show 3D beamline plot'),
@@ -111,4 +99,16 @@ def _init():
             display_test_boxes=(pkconfig.channel_in_internal_test(), bool, 'Display test boxes to visualize 3D -> 2D projections'),
         ),
     )
+    a = ALL_CODES if pkconfig.channel_in_internal_test() else NON_ALPHA_CODES
+    s = set(_cfg.sim_types) if _cfg.sim_types else set(a)
+    s.update(_cfg.proprietary_sim_types)
+    # jspec imports elegant, but elegant won't work if it is not a valid
+    # sim_type so need to include here. Need a better model of
+    # dependencies between codes.
+    if 'jspec' in s and 'elegant' not in s:
+        s.add('elegant')
+    x = s.difference(a)
+    assert not x, \
+        'sim_type(s) invalid={} expected={}'.format(x, a)
+    _cfg.sim_types = frozenset(s)
     return _cfg
diff --git a/sirepo/pkcli/admin.py b/sirepo/pkcli/admin.py
index cc3ba3705..243d5098b 100644
--- a/sirepo/pkcli/admin.py
+++ b/sirepo/pkcli/admin.py
@@ -64,7 +64,7 @@ def audit_proprietary_lib_files(*uid):
                 pass
 
     server.init()
-    t = feature_config.cfg().proprietary_sim_types.intersection(feature_config.cfg().sim_types)
+    t = feature_config.cfg().proprietary_sim_types
     if not t:
         return
     for u in uid or auth_db.all_uids():
@@ -99,13 +99,12 @@ def setup_dev_proprietary_code(sim_type, rpm_url):
       sim_type (str): simulation type
       rpm_url (str): Url of the rpm (file:// or http://)
     """
-    import sirepo.pkcli.admin
     import urllib.request
 
     assert pkconfig.channel_in('dev'), \
         'Only to be used in dev. channel={}'.format(pkconfig.cfg.channel)
 
-    d = sirepo.pkcli.admin.proprietary_code_dir(sim_type)
+    d = proprietary_code_dir(sim_type)
     pkio.mkdir_parent(d)
     s = sirepo.sim_data.get_class(sim_type)
 
diff --git a/sirepo/simulation_db.py b/sirepo/simulation_db.py
index 639f50432..963120045 100644
--- a/sirepo/simulation_db.py
+++ b/sirepo/simulation_db.py
@@ -254,7 +254,7 @@ def get_schema(sim_type):
 
     """
     t = sirepo.template.assert_sim_type(sim_type) if sim_type is not None \
-        else feature_config.cfg().sim_types[0]
+        else list(feature_config.cfg().sim_types)[0]
     if t in _SCHEMA_CACHE:
         return _SCHEMA_CACHE[t]
     schema = read_json(


Test patch:
diff --git a/tests/feature_config_test.py b/tests/feature_config_test.py
new file mode 100644
index 000000000..af1d26de0
--- /dev/null
+++ b/tests/feature_config_test.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+u"""test sim_types
+
+:copyright: Copyright (c) 2020 RadiaSoft LLC.  All Rights Reserved.
+:license: http://www.apache.org/licenses/LICENSE-2.0.html
+"""
+from pykern.pkcollections import PKDict
+import os
+import pytest
+
+
+def setup_module(module):
+    os.environ.update(
+        SIREPO_FEATURE_CONFIG_PROPRIETARY_SIM_TYPES='xyz1',
+        SIREPO_FEATURE_CONFIG_SIM_TYPES='xyz2',
+    )
+
+
+def test_1():
+    from pykern import pkunit
+    import sirepo.feature_config
+
+    with pkunit.pkexcept('xyz.*xyz'):
+        sirepo.feature_config.cfg()