0

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

NorrinRadd
  • 545
  • 6
  • 21

1 Answers1

1

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Thanks for the clarification!! I am indeed using python > 3.6 and the older code is on python < 3.6. – NorrinRadd Oct 25 '22 at 12:11
  • Upon closer inspection according to the docs sting is a path like object? – NorrinRadd Oct 25 '22 at 12:20
  • @NorrinRadd Yes, you can use a string to represent a path. Specifically "A path-like object is either a str or bytes object representing a path..." [source](https://docs.python.org/3/glossary.html#term-path-like-object). I suspect that braces in a "filename" makes it invalid as a "path-like object". At the very least, it is unusual to have braces, brackets, commas, quotes, and spaces in a file path, all of which you have in this JSON. – Code-Apprentice Oct 26 '22 at 17:49
  • Once again, thanks for the clarification. I am still puzzled why this was not a problem on another mashine. But i guess there is sometihing at play what i have not recognized yet. – NorrinRadd Oct 27 '22 at 11:25
  • @NorrinRadd Your original question doesn't mention anything about running your code on a different machine, so I can't even begin to guess. – Code-Apprentice Oct 27 '22 at 18:48
  • Indeed that was something i should have mentined but forgott. I actually just wanted to port some code but my logger failed.. Thanky you for taking some of your to personal time to help me. It is really apreciated! – NorrinRadd Oct 28 '22 at 08:43