Skip to content

Commit 4382a1b

Browse files
committed
Add registered classes to namespace, validate cmap/color names
1 parent 6382cf9 commit 4382a1b

1 file changed

Lines changed: 56 additions & 15 deletions

File tree

proplot/__init__.py

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#!/usr/bin/env python3
22
"""
3-
A python package for making beautiful, publication-quality graphics.
3+
A comprehensive matplotlib wrapper for making beautiful, publication-quality graphics.
44
"""
5-
# Import everything to top-level
6-
# NOTE: In future will enable submodule access and stop importing classes to top-level
7-
import pkg_resources as _pkg
8-
9-
from .config import * # noqa: F401 F403
10-
from .internals import timers as _timers
5+
# SCM versioning
6+
import pkg_resources as pkg
7+
name = 'proplot'
8+
try:
9+
version = __version__ = pkg.get_distribution(__name__).version
10+
except pkg.DistributionNotFound:
11+
version = __version__ = 'unknown'
1112

12-
with _timers._benchmark('imports'):
13-
from .utils import * # noqa: F401 F403
13+
# Import everything to top level
14+
from .internals import benchmarks, docstring, rcsetup, warnings # noqa: F401
15+
with benchmarks._benchmark('imports'):
16+
from .config import * # noqa: F401 F403
1417
from .crs import * # noqa: F401 F403
18+
from .utils import * # noqa: F401 F403
1519
from .colors import * # noqa: F401 F403
1620
from .ticker import * # noqa: F401 F403
1721
from .scale import * # noqa: F401 F403
@@ -22,9 +26,46 @@
2226
from .ui import * # noqa: F401 F403
2327
from .demos import * # noqa: F401 F403
2428

25-
# SCM versioning
26-
name = 'proplot'
27-
try:
28-
version = __version__ = _pkg.get_distribution(__name__).version
29-
except _pkg.DistributionNotFound:
30-
version = __version__ = 'unknown'
29+
# Dynamically add registered classes to top-level namespace
30+
from .constructor import NORMS, LOCATORS, FORMATTERS, SCALES, PROJS
31+
_globals = globals()
32+
for _src in (NORMS, LOCATORS, FORMATTERS, SCALES, PROJS):
33+
for _key, _cls in _src.items():
34+
if isinstance(_cls, type): # i.e. not a scale preset
35+
_globals[_cls.__name__] = _cls # may overwrite proplot names
36+
37+
# Register objects
38+
from .config import register_cmaps, register_cycles, register_colors, register_fonts
39+
with benchmarks._benchmark('cmaps'):
40+
register_cmaps(default=True)
41+
with benchmarks._benchmark('cycles'):
42+
register_cycles(default=True)
43+
with benchmarks._benchmark('colors'):
44+
register_colors(default=True)
45+
with benchmarks._benchmark('fonts'):
46+
register_fonts(default=True)
47+
48+
# Validate colormap names and propagate 'cycle' to 'axes.prop_cycle'
49+
# NOTE: cmap.sequential also updates siblings 'cmap' and 'image.cmap'
50+
from .config import rc
51+
rcsetup.VALIDATE_REGISTERED_CMAPS = True
52+
for _key in ('cycle', 'cmap.sequential', 'cmap.diverging', 'cmap.cyclic', 'cmap.qualitative'): # noqa: E501
53+
try:
54+
rc[_key] = rc[_key]
55+
except ValueError as err:
56+
warnings._warn_proplot(f'Invalid user rc file setting: {err}')
57+
rc[_key] = 'Fire' # fill value
58+
59+
# Validate color names now that colors are registered
60+
from .config import rc_proplot, rc_matplotlib
61+
rcsetup.VALIDATE_REGISTERED_COLORS = True
62+
for _src in (rc_proplot, rc_matplotlib):
63+
for _key in _src: # loop through unsynced properties
64+
if 'color' not in _key:
65+
continue
66+
# Likely has a color validator or derivative thereof; if not, harmless
67+
try:
68+
_src[_key] = _src[_key]
69+
except ValueError as err:
70+
warnings._warn_proplot(f'Invalid user rc file setting: {err}')
71+
_src[_key] = 'black' # fill value

0 commit comments

Comments
 (0)