Home > Software design >  Expand environment variable: ox.expandvars() only working in Json dict not in string?
Expand environment variable: ox.expandvars() only working in Json dict not in string?

Time:10-25

I have an .env File with the following content:

SOME_PATH_VARIABLE = PATH_TO_FOLDER

I Want to read in a nested Json file ( Log file config ) and expand the SOME_PATH_VARIABLE via os.path.expandvars().

The Json file looks roughly like this:

{
    "version": 1,
    "disable_existing_loggers": false,
    "formatters": {
        "std_formatter": {
            ....
        },
        "MY_Custom_formatter": {
            ......
        }
    },
    "handlers": {
        "console": {
            ....
        },
        "MY_CUSTOM_HANDLER": {
            "class": "logging.handlers.TimedRotatingFileHandler",
            "when": "midnight",
            "level": "DEBUG",
            "formatter": "MY_Custom_formatter",
            "filename": "${SOME_PATH_VARIABLE}/DailyLog.log",
            "encoding": "UTF-8"
        }
    },
    "loggers": {
                ...
        }
    }
}

Now:

import os.path
from string import Template
from dotenv import find_dotenv, load_dotenv
load_dotenv(find_dotenv())

with open(PATH_TO_JSON_FILE, encoding="utf-8") as c_file:
        # expanding .env content
        # This works und windows (ie. load as dict )
        j_dict = json.loads(c_file.read())
        j_dict['handlers']['MY_CUSTOM_HANDLER']['filename'] = os.path.expandvars(j_dict['handlers']['MY_CUSTOM_HANDLER']['filename'])
        # This also works
         j_string = Template(c_file.read()).substitute(os.environ)

        # This used to work under Linux/ OSX (i.e. expand the json string)
        # But i doesnt work anymore und windows ( if that is the problem)
        # The string is returned without expanded enviroment variables.
        j_string= os.path.expandvars(c_file.read())

I do not understand what is going on in the lower case? Is the placeholder set to the literal value of the var? I read that expanding JSON string, with quotes escaped can be problematic. But that is not the case ?

any help would be appreciated.

Thank you in advance

CodePudding user response:

According to the docs:

Changed in version 3.6: Accepts a path-like object.

So I'm guessing that you updated to python 3.6 or later and a JSON string doesn't work with expandvars() since it isn't a path-like object.

On the other hand, the string value from j_dict['handlers']['MY_CUSTOM_HANDLER']['filename'] is a path-like object, so expandvars() works fine when you pull it out of the python dict.

  • Related