forked from psyplot/psyplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrcsetup.py
More file actions
executable file
·1254 lines (1049 loc) · 41.3 KB
/
rcsetup.py
File metadata and controls
executable file
·1254 lines (1049 loc) · 41.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Default management of the psyplot package
This module defines the necessary classes, data and functions for the default
configuration of the module.
The structure is motivated and to larger parts taken from the matplotlib_
package.
.. _matplotlib: http://matplotlib.org/api/"""
# Disclaimer
# ----------
#
# Copyright (C) 2021 Helmholtz-Zentrum Hereon
# Copyright (C) 2020-2021 Helmholtz-Zentrum Geesthacht
# Copyright (C) 2016-2021 University of Lausanne
#
# This file is part of psyplot and is released under the GNU LGPL-3.O license.
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3.0 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU LGPL-3.0 license for more details.
#
# You should have received a copy of the GNU LGPL-3.0 license
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import sys
import six
import logging
import re
import inspect
import yaml
import contextlib
from itertools import chain
from collections import defaultdict
from psyplot.warning import warn
from psyplot.compat.pycompat import (
UserDict, DictMethods, getcwd, zip, isstring, map)
from psyplot.docstring import docstrings, dedent, safe_modulo
from psyplot.config.logsetup import _get_home
@docstrings.get_sections(base='safe_list')
@dedent
def safe_list(l):
"""Function to create a list
Parameters
----------
l: iterable or anything else
Parameter that shall be converted to a list.
- If string or any non-iterable, it will be put into a list
- if iterable, it will be converted to a list
Returns
-------
list
`l` put (or converted) into a list"""
if isstring(l):
return [l]
try:
return list(l)
except TypeError:
return [l]
class SubDict(UserDict, dict):
"""Class that keeps week reference to the base dictionary
This class is used by the :meth:`RcParams.find_and_replace` method
to provide an easy handable instance that keeps reference to the
base rcParams dictionary."""
@property
def data(self):
"""Dictionary representing this :class:`SubDict` instance
See Also
--------
iteritems
"""
return dict(self.iteritems())
@property
def replace(self):
""":class:`bool`. If True, matching strings in the :attr:`base_str`
attribute are replaced with an empty string."""
return self._replace
@replace.setter
def replace(self, value):
def replace_base(key):
for pattern in self.patterns:
try:
return pattern.match(key).group('key')
except AttributeError: # if match is None
pass
raise KeyError(
"Could not find any matching key for %s in the base "
"dictionary!" % key)
value = bool(value)
if hasattr(self, '_replace') and value == self._replace:
return
if not hasattr(self, '_replace'):
self._replace = value
return
# if the value has changed, we change the key in the SubDict instance
# to match the ones in the base dictionary (if they exist)
for key, val in DictMethods.iteritems(self):
try:
if value:
new_key = replace_base(key)
else:
new_key = self._get_val_and_base(key)[0]
except KeyError:
continue
else:
dict.__setitem__(self, new_key, dict.pop(self, key))
self._replace = value
#: :class:`dict`. Reference dictionary
base = {}
#: list of strings. The strings that are used to set and get a specific key
#: from the :attr:`base` dictionary
base_str = []
#: list of compiled patterns from the :attr:`base_str` attribute, that
#: are used to look for the matching keys in :attr:`base`
patterns = []
#: :class:`bool`. If True, changes are traced back to the :attr:`base` dict
trace = False
@docstrings.get_sections(base='SubDict.add_base_str')
@dedent
def add_base_str(self, base_str, pattern='.+', pattern_base=None,
append=True):
"""
Add further base string to this instance
Parameters
----------
base_str: str or list of str
Strings that are used as to look for keys to get and set keys in
the :attr:`base` dictionary. If a string does not contain
``'%(key)s'``, it will be appended at the end. ``'%(key)s'`` will
be replaced by the specific key for getting and setting an item.
pattern: str
Default: ``'.+'``. This is the pattern that is inserted for
``%(key)s`` in a base string to look for matches (using the
:mod:`re` module) in the `base` dictionary. The default `pattern`
matches everything without white spaces.
pattern_base: str or list or str
If None, the whatever is given in the `base_str` is used.
Those strings will be used for generating the final search
patterns. You can specify this parameter by yourself to avoid the
misinterpretation of patterns. For example for a `base_str` like
``'my.str'`` it is recommended to additionally provide the
`pattern_base` keyword with ``'my\.str'``.
Like for `base_str`, the ``%(key)s`` is appended if not already in
the string.
append: bool
If True, the given `base_str` are appended (i.e. it is first
looked for them in the :attr:`base` dictionary), otherwise they are
put at the beginning"""
base_str = safe_list(base_str)
pattern_base = safe_list(pattern_base or [])
for i, s in enumerate(base_str):
if '%(key)s' not in s:
base_str[i] += '%(key)s'
if pattern_base:
for i, s in enumerate(pattern_base):
if '%(key)s' not in s:
pattern_base[i] += '%(key)s'
else:
pattern_base = base_str
self.base_str = base_str + self.base_str
self.patterns = list(map(lambda s: re.compile(s.replace(
'%(key)s', '(?P<key>%s)' % pattern)), pattern_base)) + \
self.patterns
docstrings.delete_params('SubDict.add_base_str.parameters', 'append')
@docstrings.get_sections(base='SubDict')
@docstrings.dedent
def __init__(self, base, base_str, pattern='.+', pattern_base=None,
trace=False, replace=True):
"""
Parameters
----------
base: dict
base dictionary
%(SubDict.add_base_str.parameters.no_append)s
trace: bool
Default: False. If True, changes in the SubDict are traced back to
the `base` dictionary. You can change this behaviour also
afterwards by changing the :attr:`trace` attribute
replace: bool
Default: True. If True, everything but the '%%(key)s' part in a
base string is replaced (see examples below)
Notes
-----
- If a key of matches multiple strings in `base_str`, the first
matching one is used.
- the SubDict class is (of course) not that efficient as the
:attr:`base` dictionary, since we loop multiple times through it's
keys
Examples
--------
Initialization example::
>>> from psyplot import rcParams
>>> d = rcParams.find_and_replace(['plotter.baseplotter.',
... 'plotter.vector.'])
>>> print d['title']
>>> print d['arrowsize']
1.0
To convert it to a usual dictionary, simply use the :attr:`data`
attribute::
>>> d.data
{'title': None, 'arrowsize': 1.0, ...}
Note that changing one keyword of your :class:`SubDict` will not change
the :attr:`base` dictionary, unless you set the :attr:`trace` attribute
to ``True``::
>>> d['title'] = 'my title'
>>> print(d['title'])
my title
>>> print(rcParams['plotter.baseplotter.title'])
>>> d.trace = True
>>> d['title'] = 'my second title'
>>> print(d['title'])
my second title
>>> print(rcParams['plotter.baseplotter.title'])
my second title
Furthermore, changing the :attr:`replace` attribute will change how you
can access the keys::
>>> d.replace = False
# now setting d['title'] = 'anything' would raise an error (since
# d.trace is set to True and 'title' is not a key in the rcParams
# dictionary. Instead we need
>>> d['plotter.baseplotter.title'] = 'anything'
See Also
--------
RcParams.find_and_replace"""
self.base = base
self.base_str = []
self.patterns = []
self.replace = bool(replace)
self.trace = bool(trace)
self.add_base_str(base_str, pattern=pattern, pattern_base=pattern_base,
append=False)
def __getitem__(self, key):
if key in DictMethods.iterkeys(self):
return dict.__getitem__(self, key)
if not self.replace:
return self.base[key]
return self._get_val_and_base(key)[1]
def __setitem__(self, key, val):
# set it in the SubDict instance if trace is False
if not self.trace:
dict.__setitem__(self, key, val)
return
base = self.base
# set it with the given key, if trace is True
if not self.replace:
base[key] = val
dict.pop(self, key, None)
return
# first look if the key already exists in the base dictionary
for s, patt in self._iter_base_and_pattern(key):
m = patt.match(s)
if m and s in base:
base[m.group()] = val
return
# if the key does not exist, we set it
self.base[key] = val
def _get_val_and_base(self, key):
found = False
e = None
for s, patt in self._iter_base_and_pattern(key):
found = True
try:
m = patt.match(s)
if m:
return m.group(), self.base[m.group()]
else:
raise KeyError(
"{0} does not match the specified pattern!".format(
s))
except KeyError as e:
pass
if not found:
if e is not None:
raise
raise KeyError("{0} does not match the specified pattern!".format(
key))
def _iter_base_and_pattern(self, key):
return zip(
map(lambda s: safe_modulo(s, {'key': key}), self.base_str),
self.patterns)
def iterkeys(self):
"""Unsorted iterator over keys"""
patterns = self.patterns
replace = self.replace
seen = set()
for key in six.iterkeys(self.base):
for pattern in patterns:
m = pattern.match(key)
if m:
ret = m.group('key') if replace else m.group()
if ret not in seen:
seen.add(ret)
yield ret
break
for key in DictMethods.iterkeys(self):
if key not in seen:
yield key
def iteritems(self):
"""Unsorted iterator over items"""
return ((key, self[key]) for key in self.iterkeys())
def itervalues(self):
"""Unsorted iterator over values"""
return (val for key, val in self.iteritems())
def update(self, *args, **kwargs):
"""Update the dictionary"""
for k, v in six.iteritems(dict(*args, **kwargs)):
self[k] = v
docstrings.delete_params('SubDict.parameters', 'base')
class RcParams(dict):
"""A dictionary object including validation
validating functions are defined and associated with rc parameters in
:data:`defaultParams`
This class is essentially the same as in maplotlibs
:class:`~matplotlib.RcParams` but has the additional
:meth:`find_and_replace` method."""
@property
def validate(self):
"""Dictionary with validation methods as values"""
depr = self._all_deprecated
return dict((key, val[1]) for key, val in
six.iteritems(self.defaultParams)
if key not in depr)
@property
def descriptions(self):
"""The description of each keyword in the rcParams dictionary"""
return {key: val[2] for key, val in six.iteritems(self.defaultParams)
if len(val) >= 3}
HEADER = """Configuration parameters of the psyplot module
You can copy this file (or parts of it) to another path and save it as
psyplotrc.yml. The directory should then be stored in the PSYPLOTCONFIGDIR
environment variable."""
msg_depr = "%s is deprecated and replaced with %s; please use the latter."
msg_depr_ignore = "%s is deprecated and ignored. Use %s"
#: possible connections that shall be called if the rcParams value change
_connections = defaultdict(list)
#: the names of the entry points that are loaded during the
#: :meth:`load_plugins` method
_plugins = []
@property
def _all_deprecated(self):
return set(chain(self._deprecated_ignore_map, self._deprecated_map))
@property
def defaultParams(self):
return getattr(self, '_defaultParams', defaultParams)
@defaultParams.setter
def defaultParams(self, value):
self._defaultParams = value
@defaultParams.deleter
def defaultParams(self):
del self._defaultParams
# validate values on the way in
def __init__(self, *args, **kwargs):
"""
Parameters
----------
defaultParams: dict
The defaultParams to use (see the :attr:`defaultParams` attribute).
By default, the :attr:`psyplot.config.rcsetup.defaultParams`
dictionary is used
Other Parameters
----------------
*args, **kwargs
Any key-value pair for the initialization of the dictionary
"""
defaultParams = kwargs.pop('defaultParams', None)
if defaultParams is not None:
self.defaultParams = defaultParams
self._deprecated_map = {}
self._deprecated_ignore_map = {}
for k, v in six.iteritems(dict(*args, **kwargs)):
try:
self[k] = v
except (ValueError, RuntimeError):
# force the issue
warn(_rcparam_warn_str.format(key=repr(k), value=repr(v),
func='__init__'))
dict.__setitem__(self, k, v)
def __setitem__(self, key, val):
key, val = self._get_depreceated(key, val)
if key is None:
return
try:
cval = self.validate[key](val)
except ValueError as ve:
raise ValueError("Key %s: %s" % (key, str(ve)))
dict.__setitem__(self, key, cval)
for func in self._connections.get(key, []):
func(cval)
def _get_depreceated(self, key, *args):
if key in self._deprecated_map:
alt_key, alt_val = self._deprecated_map[key]
warn(self.msg_depr % (key, alt_key))
key = alt_key
return key, alt_val(args[0]) if args else None
elif key in self._deprecated_ignore_map:
alt = self._deprecated_ignore_map[key]
warn(self.msg_depr_ignore % (key, alt))
return None, None
elif key not in self.defaultParams:
raise KeyError(
'%s is not a valid rc parameter. See rcParams.keys() for a '
'list of valid parameters.' % (key,))
return key, args[0] if args else None
def __getitem__(self, key):
key = self._get_depreceated(key)[0]
if key is not None:
return dict.__getitem__(self, key)
def connect(self, key, func):
"""Connect a function to the given formatoption
Parameters
----------
key: str
The rcParams key
func: function
The function that shall be called when the rcParams key changes.
It must accept a single value that is the new value of the
key."""
key = self._get_depreceated(key)[0]
if key is not None:
self._connections[key].append(func)
def disconnect(self, key=None, func=None):
"""Disconnect the connections to the an rcParams key
Parameters
----------
key: str
The rcParams key. If None, all keys are used
func: function
The function that is connected. If None, all functions are
connected
"""
if key is None:
for key, connections in self._connections.items():
for conn in connections[:]:
if func is None or conn is func:
connections.remove(conn)
else:
connections = self._connections[key]
for conn in connections[:]:
if func is None or conn is func:
connections.remove(conn)
def remove(self, key, func):
key = self._get_depreceated(key)[0]
if key is not None:
self._connections[key].remove(func)
# the default dict `update` does not use __setitem__
# so rcParams.update(...) (such as in seaborn) side-steps
# all of the validation over-ride update to force
# through __setitem__
def update(self, *args, **kwargs):
for k, v in six.iteritems(dict(*args, **kwargs)):
try:
self[k] = v
except (ValueError, RuntimeError):
# force the issue
warn(_rcparam_warn_str.format(key=repr(k), value=repr(v),
func='update'))
dict.__setitem__(self, k, v)
def update_from_defaultParams(self, defaultParams=None,
plotters=True):
"""Update from the a dictionary like the :attr:`defaultParams`
Parameters
----------
defaultParams: dict
The :attr:`defaultParams` like dictionary. If None, the
:attr:`defaultParams` attribute will be updated
plotters: bool
If True, ``'project.plotters'`` will be updated too"""
if defaultParams is None:
defaultParams = self.defaultParams
self.update({key: val[0] for key, val in defaultParams.items()
if plotters or key != 'project.plotters'})
def __repr__(self):
import pprint
class_name = self.__class__.__name__
indent = len(class_name) + 1
repr_split = pprint.pformat(dict(self), indent=1,
width=80 - indent).split('\n')
repr_indented = ('\n' + ' ' * indent).join(repr_split)
return '{0}({1})'.format(class_name, repr_indented)
def __str__(self):
return '\n'.join('{0}: {1}'.format(k, v)
for k, v in sorted(self.items()))
def keys(self):
"""
Return sorted list of keys.
"""
k = list(dict.keys(self))
k.sort()
return k
def values(self):
"""
Return values in order of sorted keys.
"""
return [self[k] for k in self.keys()]
def find_all(self, pattern):
"""
Return the subset of this RcParams dictionary whose keys match,
using :func:`re.search`, the given ``pattern``.
Parameters
----------
pattern: str
pattern as suitable for re.compile
Returns
-------
RcParams
RcParams instance with entries that match the given `pattern`
Notes
-----
Changes to the returned dictionary are (different from
:meth:`find_and_replace` are *not* propagated to the parent RcParams
dictionary.
See Also
--------
find_and_replace"""
pattern_re = re.compile(pattern)
ret = RcParams()
ret.defaultParams = self.defaultParams
ret.update((key, value) for key, value in self.items()
if pattern_re.search(key))
return ret
@docstrings.dedent
def find_and_replace(self, *args, **kwargs):
"""
Like :meth:`find_all` but the given strings are replaced
This method returns a dictionary-like object that keeps weak reference
to this rcParams instance. The resulting `SubDict` instance takes the
keys from this rcParams instance but leaves away what is found in
`base_str`.
``*args`` and ``**kwargs`` are determined by the :class:`SubDict`
class, where the `base` dictionary is this one.
Parameters
----------
%(SubDict.parameters.no_base)s
Returns
-------
SubDict
SubDict with this rcParams instance as reference.
Examples
--------
The syntax is the same as for the initialization of the
:class:`SubDict` class::
>>> from psyplot import rcParams
>>> d = rcParams.find_and_replace(['plotter.baseplotter.',
... 'plotter.vector.'])
>>> print(d['title'])
None
>>> print(d['arrowsize'])
1.0
See Also
--------
find_all
SubDict"""
return SubDict(self, *args, **kwargs)
def load_from_file(self, fname=None):
"""Update rcParams from user-defined settings
This function updates the instance with what is found in `fname`
Parameters
----------
fname: str
Path to the yaml configuration file. Possible keys of the
dictionary are defined by :data:`config.rcsetup.defaultParams`.
If None, the :func:`config.rcsetup.psyplot_fname` function is used.
See Also
--------
dump_to_file, psyplot_fname"""
fname = fname or psyplot_fname()
if fname and os.path.exists(fname):
with open(fname) as f:
d = yaml.load(f, Loader=yaml.SafeLoader)
self.update(d)
if (d.get('project.plotters.user') and
'project.plotters' in self):
self['project.plotters'].update(d['project.plotters.user'])
def dump(self, fname=None, overwrite=True, include_keys=None,
exclude_keys=['project.plotters'], include_descriptions=True,
**kwargs):
"""Dump this instance to a yaml file
Parameters
----------
fname: str or None
file name to write to. If None, the string that would be written
to a file is returned
overwrite: bool
If True and `fname` already exists, it will be overwritten
include_keys: None or list of str
Keys in the dictionary to be included. If None, all keys are
included
exclude_keys: list of str
Keys from the :class:`RcParams` instance to be excluded
Other Parameters
----------------
``**kwargs``
Any other parameter for the :func:`yaml.dump` function
Returns
-------
str or None
if fname is ``None``, the string is returned. Otherwise, ``None``
is returned
Raises
------
IOError
If `fname` already exists and `overwrite` is False
See Also
--------
load_from_file"""
if fname is not None and not overwrite and os.path.exists(fname):
raise IOError(
'%s already exists! Set overwrite=True to overwrite it!' % (
fname))
if six.PY2:
kwargs.setdefault('encoding', 'utf-8')
d = {key: val for key, val in six.iteritems(self) if (
include_keys is None or key in include_keys) and
key not in exclude_keys}
kwargs['default_flow_style'] = False
if include_descriptions:
s = yaml.dump(d, **kwargs)
desc = self.descriptions
i = 2
header = self.HEADER.splitlines() + [
'', 'Created with python', ''] + sys.version.splitlines() + [
'', '']
lines = ['# ' + l for l in header] + s.splitlines()
for l in lines[2:]:
key = l.split(':')[0]
if key in desc:
lines.insert(i, '# ' + '\n# '.join(desc[key].splitlines()))
i += 1
i += 1
s = '\n'.join(lines)
if fname is None:
return s
else:
with open(fname, 'w') as f:
f.write(s)
else:
if fname is None:
return yaml.dump(d, **kwargs)
with open(fname, 'w') as f:
yaml.dump(d, f, **kwargs)
return None
def _load_plugin_entrypoints(self):
"""Load the modules for the psyplot plugins
Yields
------
importlib.metadata.EntryPoint
The entry point for the psyplot plugin module"""
from psyplot.utils import plugin_entrypoints
def load_plugin(ep):
try:
ep.module
except AttributeError: # python<3.10
try:
ep.module = ep.pattern.match(ep.value).group("module")
except AttributeError: # python<3.8
ep.module = ep.module_name
if plugins_env == ['no']:
return False
elif ep.module in exclude_plugins:
return False
elif include_plugins and ep.module not in include_plugins:
return False
return True
self._plugins = self._plugins or []
plugins_env = os.getenv('PSYPLOT_PLUGINS', '').split('::')
include_plugins = [s[4:] for s in plugins_env if s.startswith('yes:')]
exclude_plugins = [s[3:] for s in plugins_env if s.startswith('no:')]
logger = logging.getLogger(__name__)
eps = plugin_entrypoints("psyplot", "plugin")
for ep in eps:
if not load_plugin(ep):
logger.debug('Skipping entrypoint %s', ep)
continue
self._plugins.append(str(ep))
logger.debug('Loading entrypoint %s', ep)
yield ep
def load_plugins(self, raise_error=False):
"""
Load the plotters and defaultParams from the plugins
This method loads the `plotters` attribute and `defaultParams`
attribute from the plugins that use the entry point specified by
`group`. Entry points must be objects (or modules) that have a
`defaultParams` and a `plotters` attribute.
Parameters
----------
raise_error: bool
If True, an error is raised when multiple plugins define the same
plotter or rcParams key. Otherwise only a warning is raised"""
pm_env = os.getenv('PSYPLOT_PLOTMETHODS', '').split('::')
include_pms = [s[4:] for s in pm_env if s.startswith('yes:')]
exclude_pms = [s[3:] for s in pm_env if s.startswith('no:')]
logger = logging.getLogger(__name__)
plotters = self['project.plotters']
def_plots = {'default': list(plotters)}
defaultParams = self.defaultParams
def_keys = {'default': defaultParams}
def register_pm(ep, name):
full_name = '%s:%s' % (ep.module, name)
ret = True
if pm_env == ['no']:
ret = False
elif name in exclude_pms or full_name in exclude_pms:
ret = False
elif include_pms and (name not in include_pms and
full_name not in include_pms):
ret = False
if not ret:
logger.debug('Skipping plot method %s', full_name)
return ret
for ep in self._load_plugin_entrypoints():
try:
plugin_mod = ep.load()
except (ModuleNotFoundError, ImportError):
logger.debug("Failed to import %s!" % (ep, ), exc_info=True)
logger.warning("Failed to import %s!" % (ep, ))
continue
rc = plugin_mod.rcParams
# load the plotters
plugin_plotters = {
key: val for key, val in rc.get('project.plotters', {}).items()
if register_pm(ep, key)}
already_defined = set(plotters).intersection(plugin_plotters)
if already_defined:
msg = ("Error while loading psyplot plugin %s! The "
"following plotters have already been "
"defined") % ep
msg += 'and will be overwritten:' if not raise_error else ':'
msg += '\n' + '\n'.join(chain.from_iterable(
(('%s by %s' % (key, plugin)
for plugin, keys in def_plots.items() if key in keys)
for key in already_defined)))
if raise_error:
raise ImportError(msg)
else:
warn(msg)
for d in plugin_plotters.values():
d['plugin'] = ep.module
plotters.update(plugin_plotters)
def_plots[ep] = list(plugin_plotters)
# load the defaultParams keys
plugin_defaultParams = rc.defaultParams
already_defined = set(defaultParams).intersection(
plugin_defaultParams) - {'project.plotters'}
if already_defined:
msg = ("Error while loading psyplot plugin %s! The "
"following default keys have already been "
"defined:") % ep
msg += '\n' + '\n'.join(chain.from_iterable(
(('%s by %s' % (key, plugin)
for plugin, keys in def_keys.items() if key in keys)
for key in already_defined)))
if raise_error:
raise ImportError(msg)
else:
warn(msg)
update_keys = set(plugin_defaultParams) - {'project.plotters'}
def_keys[ep] = update_keys
self.defaultParams.update(
{key: plugin_defaultParams[key] for key in update_keys})
# load the rcParams (without validation)
super(RcParams, self).update({key: rc[key] for key in update_keys})
# add the deprecated keys
self._deprecated_ignore_map.update(rc._deprecated_ignore_map)
self._deprecated_map.update(rc._deprecated_map)
def copy(self):
"""Make sure, the right class is retained"""
return RcParams(self)
@contextlib.contextmanager
def catch(self):
"""Context manager to reset the rcParams afterwards
Usage::
rcParams['some_key'] = 0
with rcParams.catch():
rcParams['some_key'] = 1
assert rcParams['some_key'] == 1
assert rcParams['some_key'] == 0
"""
save = dict(self)
yield
super().update(save) # reset settings
def psyplot_fname(env_key='PSYPLOTRC', fname='psyplotrc.yml',
if_exists=True):
"""
Get the location of the config file.
The file location is determined in the following order
- `$PWD/psyplotrc.yml`
- environment variable `PSYPLOTRC` (pointing to the file location or a
directory containing the file `psyplotrc.yml`)
- `$PSYPLOTCONFIGDIR/psyplot`
- On Linux and osx,
- `$HOME/.config/psyplot/psyplotrc.yml`
- On other platforms,
- `$HOME/.psyplot/psyplotrc.yml` if `$HOME` is defined.
- Lastly, it looks in `$PSYPLOTDATA/psyplotrc.yml` for a
system-defined copy.
Parameters
----------
env_key: str
The environment variable that can be used for the configuration
directory
fname: str
The name of the configuration file
if_exists: bool
If True, the path is only returned if the file exists
Returns
-------
None or str
None, if no file could be found and `if_exists` is True, else the path
to the psyplot configuration file
Notes
-----
This function is motivated by the :func:`matplotlib.matplotlib_fname`
function"""
cwd = getcwd()
full_fname = os.path.join(cwd, fname)
if os.path.exists(full_fname):
return full_fname
if env_key in os.environ:
path = os.environ[env_key]
if os.path.exists(path):
if os.path.isdir(path):
full_fname = os.path.join(path, fname)
if os.path.exists(full_fname):
return full_fname
else:
return path
configdir = get_configdir()
if configdir is not None:
full_fname = os.path.join(configdir, fname)
if os.path.exists(full_fname) or not if_exists:
return full_fname
return None
def get_configdir(name='psyplot', env_key='PSYPLOTCONFIGDIR'):
"""
Return the string representing the configuration directory.
The directory is chosen as follows:
1. If the `env_key` environment variable is supplied, choose that.
2a. On Linux and osx, choose ``'$HOME/.config/' + name``.
2b. On other platforms, choose ``'$HOME/.' + name``.
3. If the chosen directory exists, use that as the
configuration directory.