Skip to content

Commit f46da0d

Browse files
committed
Add support for setting environment variables.
Specified as a list of NAME=VALUE lines in the pgfutils.environment configuration option.
1 parent f8115a7 commit f46da0d

File tree

16 files changed

+240
-7
lines changed

16 files changed

+240
-7
lines changed

doc/config.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,48 @@ This is a comma-separated list of extra libraries to install file tracking on.
203203
See the [file tracking](file_tracking.md) documentation for more details.
204204

205205

206+
### Environment variables
207+
208+
Environment variables to set can be specified using the multi-line option
209+
``environment``. Each line represents one environment variable in the format
210+
``name = value``:
211+
212+
```INI
213+
[pgfutils]
214+
environment =
215+
name1 = value1
216+
name2 = value2
217+
```
218+
219+
All names and values are processed and set as strings; any leading or trailing
220+
spaces around the names or values will be stripped. Existing environment
221+
variables of the same name will be overwritten. This means that if you specify
222+
the same variable multiple times the last value will be used. For example, the
223+
configuration
224+
225+
```INI
226+
[pgfutils]
227+
environment =
228+
name1 = value1
229+
name2 = value2
230+
name1 = value3
231+
```
232+
233+
will result in ``name1`` being set to ``value3``. The variables are set during
234+
the call to ``setup_figure()``, so any libraries that read the environment
235+
variables must be imported after this call.
236+
237+
Note that the ``PGFUTILS_TRACK_FILES`` variable described in the [file tracking
238+
documentation](file_tracking.md) can be configured through this option, for
239+
example to output tracked files to stdout:
240+
241+
```INI
242+
[pgfutils]
243+
environment =
244+
PGFUTILS_TRACK_FILES = 1
245+
```
246+
247+
206248
Path settings
207249
-------------
208250

extras/pgfutils.cfg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ preamble_substitute = false
114114
# Extra library-specific file trackers to install.
115115
extra_tracking =
116116

117+
# Environment variables to set. This should be a multi-line value; place one or
118+
# more spaces between each subsequenct line (these spaces will be removed when
119+
# parsing the configuration). Each line should be in the format name = value,
120+
# with the value being read as a string, and leading and trailing spaces being
121+
# removed from both the name and the value.
122+
environment =
123+
117124

118125

119126
# Path setup.

pgfutils.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
3737
"""
3838

39-
__version__ = "1.4.0"
39+
__version__ = "1.5.0"
4040

4141
# We don't import Matplotlib here as this brings in NumPy. In turn, NumPy
4242
# caches a reference to the io.open() method as part of its data loading
@@ -410,6 +410,7 @@ def _config_reset():
410410
'figure_background': '',
411411
'axes_background': 'white',
412412
'extra_tracking': '',
413+
'environment': '',
413414
},
414415

415416
'paths': {
@@ -705,24 +706,39 @@ def setup_figure(width=1.0, height=1.0, columns=None, margin=False,
705706
"""
706707
global _config, _interactive
707708

708-
# Need to install our file trackers (if desired)
709-
# before we import Matplotlib.
710-
if 'PGFUTILS_TRACK_FILES' in os.environ:
711-
_install_standard_file_trackers()
712-
713709
# Reset the configuration.
714710
_config_reset()
715711

716712
# Load configuration from a local file if one exists.
717713
if os.path.exists('pgfutils.cfg'):
718714
_config.read('pgfutils.cfg')
715+
_file_tracker.filenames.add(("r", "pgfutils.cfg"))
719716

720717
# And anything given in the function call.
721718
if kwargs:
722719
_config.read_kwargs(**kwargs)
723720

724-
# Now we can add any extra trackers specified in the config.
721+
# Set environment variables specified in the configuration.
722+
for line in _config['pgfutils']['environment'].splitlines():
723+
line = line.strip()
724+
if not line:
725+
continue
726+
727+
# Check the variables are formatted correctly.
728+
if '=' not in line:
729+
raise ValueError(
730+
"Environment variables should be in the form NAME=VALUE. "
731+
"The line '{}' does not match this.".format(line)
732+
)
733+
734+
# And set them.
735+
key, value = line.split("=", 1)
736+
os.environ[key.strip()] = value.strip()
737+
738+
# Install file trackers if desired. This must be done before anything which
739+
# imports Matplotlib.
725740
if 'PGFUTILS_TRACK_FILES' in os.environ:
741+
_install_standard_file_trackers()
726742
extra = _config['pgfutils']['extra_tracking'].strip()
727743
if extra:
728744
_install_extra_file_trackers(extra.split(","))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from pgfutils import setup_figure, save
2+
setup_figure()
3+
4+
import os
5+
6+
name1 = os.environ.get("name1", "not set")
7+
name2 = os.environ.get("name2", "not set")
8+
9+
if name1 != "value1":
10+
raise ValueError("Incorrect value ({}) for environment variable name1".format(name1))
11+
if name2 != "value2":
12+
raise ValueError("Incorrect value ({}) for environment variable name2".format(name2))
13+
14+
save()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
os.environ["name1"] = "original value"
3+
4+
from pgfutils import setup_figure, save
5+
setup_figure()
6+
7+
import os
8+
9+
name1 = os.environ.get("name1", "not set")
10+
name2 = os.environ.get("name2", "not set")
11+
12+
if name1 != "value1":
13+
raise ValueError("Incorrect value ({}) for environment variable name1".format(name1))
14+
if name2 != "value2":
15+
raise ValueError("Incorrect value ({}) for environment variable name2".format(name2))
16+
17+
save()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[pgfutils]
2+
environment=
3+
name1=value1
4+
name2 = value2
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from pgfutils import setup_figure, save
2+
setup_figure()
3+
save()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[pgfutils]
2+
environment=
3+
line1=ok
4+
line2 should fail
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from pgfutils import setup_figure, save
2+
setup_figure()
3+
4+
import os
5+
6+
name1 = os.environ.get("name1", "not set")
7+
name2 = os.environ.get("name2", "not set")
8+
9+
if name1 != "value3":
10+
raise ValueError("Incorrect value ({}) for environment variable name1".format(name1))
11+
if name2 != "value2":
12+
raise ValueError("Incorrect value ({}) for environment variable name2".format(name2))
13+
14+
save()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pgfutils]
2+
environment=
3+
name1=value1
4+
name2 = value2
5+
name1=value3

0 commit comments

Comments
 (0)