Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:58:21

0001 #!/usr/bin/env python
0002 
0003 import json
0004 import re
0005 import os
0006 
0007 
0008 is_unicode_defined = True
0009 try:
0010     _ = unicode('test')
0011 except NameError:
0012     is_unicode_defined = False
0013 
0014 
0015 def is_string(value):
0016     if is_unicode_defined and type(value) in [str, unicode] or type(value) in [str]:   # noqa F821
0017         return True
0018     else:
0019         return False
0020 
0021 
0022 def as_parse_env(dct):
0023     # print(dct)
0024     for key in dct:
0025         value = dct[key]
0026         # print(value)
0027         # print(type(value))
0028         if is_string(value) and '$' in value:
0029             env_matchs = re.findall('\$\{*([^\}]+)\}*', value)     # noqa W605
0030             print("env_matchs")
0031             print(env_matchs)
0032             for env_name in env_matchs:
0033                 print(env_name)
0034                 if env_name not in os.environ:
0035                     print("Error: %s is defined in configmap but is not defined in environments" % env_name)
0036                 else:
0037                     # dct[key] = os.environ[env_name]
0038                     print('${%s}' % env_name)
0039                     print(os.environ.get(env_name))
0040                     env_name1 = r'${%s}' % env_name
0041                     env_name2 = r'$%s' % env_name
0042                     print(env_name1)
0043                     print(env_name2)
0044                     print(value.replace(env_name1, os.environ.get(env_name)))
0045                     value = value.replace(env_name1, os.environ.get(env_name)).replace(env_name2, os.environ.get(env_name))
0046             dct[key] = value
0047     return dct
0048 
0049 
0050 if __name__ == '__main__':
0051     json_string = """
0052 {    "/opt/idds/config/panda.cfg":
0053         {"panda":
0054             {"panda_url_ssl": "${PANDA_URL_SSL}",
0055              "panda_url": "${PANDA_URL}",
0056              "panda_monitor_url": "${PANDA_MONITOR_URL}",
0057              "# PANDA_AUTH_VO": "panda_dev",
0058              "panda_auth": "${PANDA_AUTH}",
0059              "panda_auth_vo": "${PANDA_AUTH_VO}",
0060              "panda_config_root": "${PANDA_CONFIG_ROOT}",
0061              "pandacache_url": "${PANDACACHE_URL}",
0062              "panda_verify_host": "${PANDA_VERIFY_HOST}",
0063              "test1": {"test2": "${TEST_ENV}"}
0064             },
0065          "database":
0066             {"default": "postgresql://${IDDS_DATABASE_USER}:${IDDS_DATABASE_PASSWORD}@${IDDS_DATABASE_HOST}/${IDDS_DATABASE_NAME}",
0067              "schema": "${IDDS_DATABASE_SCHEMA}",
0068              "pool_size": 20,
0069              "pool_recycle": 3600,
0070              "echo": 0,
0071              "pool_reset_on_return": "rollback"
0072             }
0073         }
0074 }
0075 """
0076     print("test1")
0077     test1 = json.loads(json_string)
0078     print(test1)
0079     print("test2")
0080     test1 = json.loads(json_string, object_hook=as_parse_env)
0081     print(test1)