0

I am using mkdocs system which is configured using yaml file, parsed via PyYml. I want to be able to specificy environment variable in its mkdocs.yml which is not possible in mkdocs ecosystem, but workaround exists using PyYml special !!python/object/apply:os.getenv syntax which works great for yaml keys of type string.

However, this doesn't work:

plugins:
    - search
    - pdf-export:
        combined: !!python/object/apply:os.getenv ["PDF_EXPORT_COMBINED"]

The build complains that string is not bool value:

PS> $Env:PDF_EXPORT_COMBINED='true'
PS> mkdocs build
...
ERROR - Config value: 'plugins'. Error: Plugin value: 'combined'. 
        Expected type: <class 'bool'> but received: <class 'str'>

I looked into PyYAML section YAML tags and Python types^1 to no avail. I guess form:

!python/object/apply:module.f | value of f(...)

could be used as I need pythons distutils.util.strtobool( os.getenv("PDF_EXPORT_COMBINED") ) but not sure if that is possible using that syntax.

majkinetor
  • 8,730
  • 9
  • 54
  • 72
  • You should make your own module `x` with a function `y` that does both the getenv and strtobool and then run `!python/object/apply:x.y ["PDF_EXPORT_COMBINED"] instead of trying to nest function calls and hope apply takes care of things. – Anthon Jan 10 '20 at 20:08
  • Thx, not sure how to do that in the context of external tool tho, but I guess it could be done. – majkinetor Jan 11 '20 at 09:34
  • 1
    If you have installed mkdocs via your package manager look at the header of the command, it should show you which python it uses (or use `which` etc) it used. If you have installed mkdocs with pip you know which virtualenv you used. In either case just store your module in `lib/pythonXY/site-packages` next to the `bin` directory that holds the python executable. – Anthon Jan 11 '20 at 13:41

0 Answers0