99
1010COMMON_OPTIONS = {
1111 'R' : '''\
12- R or region : str or list
12+ R : str or list
1313 *Required if this is the first plot command*.
1414 ``'xmin/xmax/ymin/ymax[+r][+uunit]'``.
1515 Specify the region of interest.''' ,
1616 'J' : '''\
17- J or projection : str
17+ J : str
1818 *Required if this is the first plot command*.
1919 Select map projection.''' ,
2020 'B' : '''\
3131 File name of a CPT file or ``C='color1,color2[,color3,...]'`` to
3232 build a linear continuous CPT from those colors automatically.''' ,
3333 'G' : '''\
34- G : str
35- Select color or pattern for filling of symbols or polygons. Default is
36- no fill.''' ,
34+ G : str
35+ Select color or pattern for filling of symbols or polygons. Default
36+ is no fill.''' ,
3737}
3838
3939
@@ -82,11 +82,11 @@ def fmt_docstring(module_func):
8282 <BLANKLINE>
8383 Parameters
8484 ----------
85- R or region : str or list
85+ R : str or list
8686 *Required if this is the first plot command*.
8787 ``'xmin/xmax/ymin/ymax[+r][+uunit]'``.
8888 Specify the region of interest.
89- J or projection : str
89+ J : str
9090 *Required if this is the first plot command*.
9191 Select map projection.
9292 <BLANKLINE>
@@ -142,6 +142,56 @@ def kwargs2string(kwargs):
142142 return arg_str
143143
144144
145+ def use_alias (** aliases ):
146+ """
147+ Decorator to add aliases to keyword arguments of a function.
148+
149+ Replaces the aliases with their desired names before passing them along to
150+ the module function.
151+
152+ Keywords passed to this decorator are the desired argument name and their
153+ value is the alias.
154+
155+ Use this as the last decorator applied to a function (the furthest from the
156+ ``def`` statement).
157+
158+ Examples
159+ --------
160+
161+ >>> @use_alias(R='region', J='projection')
162+ ... def my_module(**kwargs):
163+ ... print('R =', kwargs['R'], 'J =', kwargs['J'])
164+ >>> my_module(R='bla', J='meh')
165+ R = bla J = meh
166+ >>> my_module(region='bla', J='meh')
167+ R = bla J = meh
168+ >>> my_module(R='bla', projection='meh')
169+ R = bla J = meh
170+ >>> my_module(region='bla', projection='meh')
171+ R = bla J = meh
172+
173+ """
174+
175+ def alias_decorator (module_func ):
176+ """
177+ Decorator that replaces the aliases for arguments.
178+ """
179+
180+ @functools .wraps (module_func )
181+ def new_module (* args , ** kwargs ):
182+ """
183+ New module that parses and replaces the registered aliases.
184+ """
185+ for arg , alias in aliases .items ():
186+ if alias in kwargs :
187+ kwargs [arg ] = kwargs .pop (alias )
188+ return module_func (* args , ** kwargs )
189+
190+ return new_module
191+
192+ return alias_decorator
193+
194+
145195def parse_bools (module_func ):
146196 """
147197 Parse boolean arguments and transform them into option strings.
@@ -228,10 +278,6 @@ def parse_region(module_func):
228278 {'R': '1/2/3/4'}
229279 >>> my_module(R=[1, 2, 3, 4])
230280 {'R': '1/2/3/4'}
231- >>> my_module(region=[1, 2, 3, 4])
232- {'R': '1/2/3/4'}
233- >>> my_module(region='1/2/3/4')
234- {'R': '1/2/3/4'}
235281
236282 """
237283
@@ -240,8 +286,6 @@ def new_module(*args, **kwargs):
240286 """
241287 New function that parses R before executing the module.
242288 """
243- if 'region' in kwargs :
244- kwargs ['R' ] = kwargs .pop ('region' )
245289 if 'R' in kwargs :
246290 value = kwargs ['R' ]
247291 if is_nonstr_iter (value ):
0 commit comments