forked from cameron-simpson/css
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeco.py
More file actions
1325 lines (1099 loc) · 41.9 KB
/
Copy pathdeco.py
File metadata and controls
1325 lines (1099 loc) · 41.9 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
#!/usr/bin/python
#
# Decorators.
# - Cameron Simpson <cs@cskk.id.au> 02jul2017
#
r'''
Assorted function decorators.
'''
# Add flake8 fixes -
# easy fixes, suppress E731 because I think it would hurt the context;
# suppress instance of E129 that seems like a bug because the visual
# indent was not the same as the next nested indent, even though that
# is what E129 indicates -
# Jonathan Hyry <jonathan.hyry@outlook.com> - 2026-04-05
import sys
import traceback
import typing
from collections import defaultdict
from contextlib import contextmanager
from inspect import Parameter, isgeneratorfunction, ismethod, signature
from cs.gimmicks import warning
from cs.typingutils import is_optional
__version__ = '20260719-post'
DISTINFO = {
'keywords': ["python3"],
'classifiers': [
"Programming Language :: Python",
"Programming Language :: Python :: 3",
],
'install_requires': ['cs.gimmicks', 'cs.typingutils'],
}
def ALL(func):
''' Include this function's name in its module's `__all__` list.
Example:
from cs.deco import ALL
__all__ = []
def obscure_function(...):
...
@ALL
def well_known_function(...):
...
'''
sys.modules[func.__module__].__all__.append(func.__name__)
return func
def fmtdoc(func):
''' A decorator to format a function's docstring.
This replaces the docstring with that string
formatted against the function's module `__dict__`
using `str.format_map`.
A quirk of `format_map` allows us to also support:
* `{`*name*`=}`: the f-string `{`*name*`=}` notation
* `{`*name*`==}`: the f-string `{`*name*`=}` notation
with the function module name prefixed
This supports simple formatted docstrings. Example:
FUNC_ENVVAR = 'FUNC_SETTING
FUNC_DEFAUlT = 12
@fmtdoc
def func():
"""
Do something with the environment variable `${FUNC_ENVVAR}`.
The default if no `${FUNC_ENVVAR}` comes from `{FUNC_DEFAUlT==}`.
"""
print(os.environ.get(FUNC_ENVVAR, FUNC_DEFAUlT))
This gives `func` this docstring:
Do something with the environment variable `$FUNC_SETTING`.
The default if no `$FUNC_SETTING` comes from `module.func.FUNC_DEFAUlT=12`.
*Warning*: this decorator is intended for wiring "constants"
into docstrings, not for dynamic values. Use for other types
of values should be considered with trepidation.
'''
fmtmap = dict(**sys.modules[func.__module__].__dict__)
# bodge in support for {name=}
for name, value in list(fmtmap.items()):
fmtmap[f'{name}='] = f'{name}={value!r}'
fmtmap[f'{name}=='] = f'{func.__module__}.{name}={value!r}'
func.__doc__ = func.__doc__.format_map(fmtmap)
return func
def decorator(deco=None, *, decorable=callable):
''' A decorator for decorator functions to support optional arguments
and also to allow specifying additional attributes to apply
to the decorated function.
The decorated function is updated by `functools.update_wrapper`
and also has its `__name__` and `__doc__` set to those from
before the decoration.
The return of such a decorator is _usually_ the conventional
decorated function, but may alternatively be a `(decorated,attrs)`
2-tuple where `attrs` is a mapping of additional attributes
to apply to `decorated`.
This allows overriding the restored `__name__` and `__doc__`
from above, and potentially other useful attributes.
The actual decorator function (eg `mydeco`) ends up being called as:
mydeco(func, *da, **dkw)
allowing `da` and `dkw` to affect the behaviour of the decorator `mydeco`.
`@decorator` itself accepts an optional `decorable=` keyword
parameter; this is used as the test of the first positional
argument to the subdecorator to see if it is the function it
decorates.
This defaults to `callable`, but may be specified; I have a
decorator which can accept classes as positional arguments
and classes are callable so I decorate it like this:
@decorator(decorable=lambda f: callable(f) and not isinstance(f, type))
so as not to mistake a class as the function being decoration.
Examples:
# define your decorator as if always called with func and args
@decorator
def mydeco(func, *da, arg2=None):
... decorate func subject to the values of da and arg2
# @mydeco called with defaults
@mydeco
def func1(...):
...
# @mydeco called with nondefault arguments
@mydeco('foo', arg2='bah')
def func2(...):
...
The `@mydeco` decorator itself is then written as though the
arguments were always supplied.
'''
if deco is None:
return lambda func, **kw: decorator(func, decorable=decorable, **kw)
def decorate(func, *dargs, **dkwargs):
''' Final decoration when we have the function and the decorator arguments.
'''
# First, collect the attributes of the function/class before deco() has at it.
func_doc = getattr(func, '__doc__', None) or ''
func_module = getattr(func, '__module__', None)
func_name = getattr(func, '__name__', str(func))
# Now decorate func.
decorated = deco(func, *dargs, **dkwargs)
# accept either the a function or a (function,attrmap) 2-tuple
try:
decorated, func_attrs = decorated
except TypeError:
func_attrs = {}
# Catch mucked decorators which forget to return the new function.
assert decorated is not None, f'@decorator @{deco=} of {func=} returned None'
if decorated is not func:
# We got a wrapper function back, pretty up the returned wrapper.
try:
from functools import update_wrapper
except ImportError:
pass
else:
try:
update_wrapper(decorated, func)
except AttributeError:
pass
try:
decorated.__name__ = func_name
except AttributeError:
pass
try:
decorated.__doc__ = func_doc
except AttributeError:
warning("cannot set __doc__ on %r", decorated)
try:
decorated.__module__ = func_module
except AttributeError:
pass
# apply any func_attrs
for attr, value in func_attrs.items():
setattr(decorated, attr, value)
return decorated
def metadeco(*da, **dkw):
''' Compute either the wrapper function for `func`
or a decorator expecting to get `func` when used.
If there is at least one positional parameter
and it is callable the it is presumed to be the function to decorate;
decorate it directly.
Otherwise return a decorator using the provided arguments,
ready for the subsequent function.
'''
if len(da) > 0 and decorable(da[0]):
# `func` is already supplied, pop it off and decorate it now.
func = da[0]
da = tuple(da[1:])
return decorate(func, *da, **dkw)
# `func` is not supplied, collect the arguments supplied and return a
# decorator which takes the subsequent callable and returns
# `deco(func, *da, **kw)`.
return lambda func: decorate(func, *da, **dkw)
metadeco.__name__ = getattr(deco, '__name__', repr(deco))
metadeco.__doc__ = getattr(deco, '__doc__', '')
metadeco.__module__ = getattr(deco, '__module__', None)
return metadeco
@decorator
def attr(func, **attrs):
''' A decorator to set attributes on a function.
Example:
@attr(hook_names=('hook1', 'hook2'))
def func():
.....
This is just a more overt and clear form of:
def func():
.....
func.hook_names=('hook1', 'hook2')
'''
if not attrs:
raise ValueError('@attr: no attributes upplied')
for attr, value in attrs.items():
setattr(func, attr, value)
return func
@decorator
def contextdecorator(cmgrfunc):
''' A decorator for a context manager function `cmgrfunc`
which turns it into a decorator for other functions.
This supports easy implementation of "setup" and "teardown"
code around other functions without the tedium of defining
the wrapper function itself. See the examples below.
The resulting context manager accepts an optional keyword
parameter `provide_context`, default `False`. If true, the
context returned from the context manager is provided as the
first argument to the call to the wrapped function.
Note that the context manager function `cmgrfunc`
has _not_ yet been wrapped with `@contextmanager`,
that is done by `@contextdecorator`.
This decorator supports both normal functions and generator functions.
With a normal function the process is:
* call the context manager with `(func,a,kw,*da,**dkw)`,
returning `ctxt`,
where `da` and `dkw` are the positional and keyword parameters
supplied when the decorator was defined.
* within the context
return the value of `func(ctxt,*a,**kw)` if `provide_context` is true
or the value of `func(*a,**kw)` if not (the default)
With a generator function the process is:
* obtain an iterator by calling `func(*a,**kw)`
* for iterate over the iterator, yielding its results,
by calling the context manager with `(func,a,kw,**da,**dkw)`,
around each `next()`
Note that it is an error to provide a true value for `provide_context`
if the decorated function is a generator function.
Some examples follow.
Trace the call and return of a specific function:
@contextdecorator
def tracecall(func, a, kw):
""" Trace the call and return from some function.
This can easily be adapted to purposes such as timing a
function call or logging use.
"""
print("call %s(*%r,**%r)" % (func, a, kw))
try:
yield
except Exception as e:
print("exception from %s(*%r,**%r): %s" % (func, a, kw, e))
raise
else:
print("return from %s(*%r,**%r)" % (func, a, kw))
@tracecall
def f():
""" Some function to trace.
"""
@tracecall(provide_context=True):
def f(ctxt, *a, **kw):
""" A function expecting the context object as its first argument,
ahead of whatever other arguments it would normally require.
"""
See who is making use of a generator's values,
when a generator might be invoked in one place and consumed elsewhere:
from cs.py.stack import caller
@contextdecorator
def genuser(genfunc, *a, **kw):
user = caller(-4)
print(f"iterate over {genfunc}(*{a!r},**{kw!r}) from {user}")
yield:
@genuser
def linesof(filename):
with open(filename) as f:
yield from f
# obtain a generator of lines here
lines = linesof(__file__)
# perhaps much later, or in another function
for lineno, line in enumerate(lines, 1):
print("line %d: %d words" % (lineno, len(line.split())))
Turn on "verbose mode" around a particular function:
import sys
import threading
from cs.context import stackattrs
class State(threading.local):
def __init__(self):
# verbose if stderr is on a terminal
self.verbose = sys.stderr.isatty()
# per thread global state
state = State()
@contextdecorator
def verbose(func):
with stackattrs(state, verbose=True) as old_attrs:
if not old_attrs['verbose']:
print(f"enabled verbose={state.verbose} for function {func}")
# yield the previous verbosity as the context
yield old_attrs['verbose']
# turn on verbose mode
@verbose
def func(x, y):
if state.verbose:
# print if verbose
print("x =", x, "y =", y)
# turn on verbose mode and also pass in the previous state
# as the first argument
@verbose(provide_context=True):
def func2(old_verbose, x, y):
if state.verbose:
# print if verbose
print("old_verbosity =", old_verbose, "x =", x, "y =", y)
'''
# turn the function into a context manager
cmgr = contextmanager(cmgrfunc)
# prepare a new decorator which wraps functions in a context
# manager using `cmgrfunc`
@decorator
def cmgrdeco(func, *da, provide_context=False, **dkw):
''' Decorator for functions which wraps calls to the function
in a context manager, optionally supplying the context
as the first argument to the called function.
'''
if isgeneratorfunction(func):
if provide_context:
raise ValueError(
"provide_context may not be true when func:%s is a generator" %
(func,)
)
def wrapped(*a, **kw):
''' Wrapper function:
* obtain an iterator by calling `func(*a,**kw)`
* iterate over the iterator, yielding its results,
by calling the context manager with `(func,a,kw,**da,**dkw)`,
around each `next()`
'''
it = func(*a, **kw)
while True:
with cmgr(func, a, kw, *da, **dkw):
try:
value = next(it)
except StopIteration:
break
yield value
else:
def wrapped(*a, **kw):
''' Wrapper function:
* call the context manager with `(func,a,kw,**da,**dkw)`,
returning `ctxt`
* within the context
return the value of `func(ctxt,*a,**kw)`
if `provide_context` is true
or the value of `func(*a,**kw)` if not (the default)
'''
with cmgr(func, a, kw, *da, **dkw) as ctxt:
if provide_context:
a = [ctxt] + list(a)
return func(*a, **kw)
return wrapped
return cmgrdeco
@decorator
def logging_wrapper(log_call, stacklevel_increment=1):
''' Decorator for logging call shims
which bumps the `stacklevel` keyword argument so that the logging system
chooses the correct frame to cite in messages.
Note: has no effect on Python < 3.8 because `stacklevel` only
appeared in that version.
'''
if (sys.version_info.major, sys.version_info.minor) < (3, 8):
# do not wrap older Python log calls, no stacklevel keyword argument
return log_call
def log_func_wrapper(*a, **kw):
stacklevel = kw.pop('stacklevel', 1)
return log_call(*a, stacklevel=stacklevel + stacklevel_increment + 1, **kw)
log_func_wrapper.__name__ = log_call.__name__
log_func_wrapper.__doc__ = log_call.__doc__
return log_func_wrapper
@decorator
def OBSOLETE(func, suggestion=None):
''' A decorator for obsolete functions or classes.
Use:
@OBSOLETE
def func(...):
or
@OBSOLETE("new_func_name")
def func(...):
This emits a warning log message before calling the decorated function.
Only one warning is emitted per calling location.
It also marks the function as deprecated using `@warnings.deprecated`.
'''
callers = set()
deprecation_message = "%s to %s:%d:%s()" % (
(
"OBSOLETE call" if suggestion is None else
("OBSOLETE (suggest %r) call" % suggestion)
),
(
func.__module__
if isinstance(func, type) else func.__code__.co_filename
),
(
getattr(func, '__firstlineno__', 0)
if isinstance(func, type) else func.__code__.co_firstlineno
),
func.__name__,
)
def OBSOLETE_func_wrapper(*args, **kwargs):
''' Wrap `func` to emit an "OBSOLETE" warning before calling `func`.
'''
frame = traceback.extract_stack(None, 2)[0]
caller = frame[0], frame[1]
if caller not in callers:
callers.add(caller)
warning(
"%s, called from %s:%d:%s",
deprecation_message,
frame[0],
frame[1],
frame[2],
)
return func(*args, **kwargs)
# mark as deprecated iE available
try:
from warnings import deprecated
except ImportError:
# @deprecated arrived in Python 3.13
pass
else:
OBSOLETE_func_wrapper = deprecated(deprecation_message)(
OBSOLETE_func_wrapper
)
funcname = getattr(func, '__name__', str(func))
funcdoc = getattr(func, '__doc__', None) or ''
doc = "OBSOLETE version of " + funcname
if suggestion:
doc += ', suggestion: ' + suggestion
return OBSOLETE_func_wrapper, dict(
__name__=f'@OBSOLETE({funcname})',
__doc__=f'{doc}\n\n{funcdoc}',
)
@OBSOLETE(suggestion='cs.cache.cachedmethod')
def cached(*a, **kw):
''' Former name for @cachedmethod.
'''
from cs.cache import cachedmethod
return cachedmethod(*a, **kw)
@OBSOLETE(suggestion='cs.cache.cachedmethod')
def cachedmethod(*a, **kw):
''' @cachedmethod is now in cs.cache.
'''
from cs.cache import cachedmethod
return cachedmethod(*a, **kw)
def contextual(func):
''' Wrap a simple function as a context manager.
This was written to support users of `@strable`,
which requires its `open_func` to return a context manager;
this turns an arbitrary function into a context manager.
Example promoting a trivial function:
>>> f = lambda: 3
>>> cf = contextual(f)
>>> with cf() as x: print(x)
3
'''
@contextmanager
def cmgr(*a, **kw):
''' Wrapper for `func` as a context manager.
'''
yield func(*a, **kw)
func_name = getattr(func, '__name__', str(func))
cmgr.__name__ = '@contextual(%s)' % func_name
cmgr.__doc__ = func.__doc__
return cmgr
@decorator
def strable(func, open_func=None):
''' Decorator for functions which may accept a `str`
instead of their core type.
Parameters:
* `func`: the function to decorate
* `open_func`: the "open" factory to produce the core type
if a string is provided;
the default is the builtin "open" function.
The returned value should be a context manager.
Simpler functions can be decorated with `@contextual`
to turn them into context managers if need be.
The usual (and default) example is a function to process an
open file, designed to be handed a file object but which may
be called with a filename. If the first argument is a `str`
then that file is opened and the function called with the
open file.
Examples:
@strable
def count_lines(f):
return len(line for line in f)
class Recording:
"Class representing a video recording."
...
@strable(open_func=Recording)
def process_video(r):
... do stuff with `r` as a Recording instance ...
*Note*: use of this decorator requires the `cs.pfx` module.
'''
from cs.pfx import Pfx # pylint: disable=import-outside-toplevel
if open_func is None:
open_func = open
if isgeneratorfunction(func):
def accepts_str(arg, *a, **kw):
if isinstance(arg, str):
with Pfx(arg), open_func(arg) as opened:
for item in func(opened, *a, **kw):
yield item
else:
for item in func(arg, *a, **kw):
yield item
else:
def accepts_str(arg, *a, **kw):
if isinstance(arg, str):
with Pfx(arg), open_func(arg) as opened:
return func(opened, *a, **kw)
return func(arg, *a, **kw)
return accepts_str
def with_(func, obj):
''' A decorator to run `func` inside a `with obj:`.
Example:
T = Thread(target=with_(some_context, target_func))
'''
def with_obj(*a, **kw):
with obj:
return func(*a, **kw)
return with_obj
def observable_class(property_names, only_unequal=False):
''' Class decorator to make various instance attributes observable.
Parameters:
* `property_names`:
an interable of instance property names to set up as
observable properties. As a special case a single `str` can
be supplied if only one attribute is to be observed.
* `only_unequal`:
only call the observers if the new property value is not
equal to the previous proerty value. This requires property
values to be comparable for inequality.
Default: `False`, meaning that all updates will be reported.
'''
if isinstance(property_names, str):
property_names = (property_names,)
# pylint: disable=protected-access
def make_observable_class(cls):
''' Annotate the class `cls` with observable properties.
'''
# push the per instance initialisation
old_init = cls.__init__
def new_init(self, *a, **kw):
''' New init, much like the old init...
'''
self._observable_class__observers = defaultdict(set)
old_init(self, *a, **kw)
cls.__init__ = new_init
def add_observer(self, attr, observer):
''' Add an observer on `.attr` to this instance.
'''
self._observable_class__observers[attr].add(observer)
cls.add_observer = add_observer
def remove_observer(self, attr, observer):
''' Remove an observer on `.attr` from this instance.
'''
self._observable_class__observers[attr].remove(observer)
cls.remove_observer = remove_observer
def report_observation(self, attr):
''' Notify all the observers of the current value of `attr`.
'''
val_attr = '_' + attr
value = getattr(self, val_attr, None)
for observer in self._observable_class__observers[attr]:
try:
observer(self, attr, value)
except Exception as e: # pylint: disable=broad-except
warning(
"%s.%s=%r: observer %s(...) raises: %s",
self,
val_attr,
value,
observer,
e,
exc_info=True
)
cls.report_observation = report_observation
def make_property(cls, attr):
''' Make `cls.attr` into a property which reports setattr events.
'''
val_attr = '_' + attr
def getter(self):
return getattr(self, val_attr)
getter.__name__ = attr
get_prop = property(getter)
setattr(cls, attr, get_prop)
def setter(self, new_value):
''' Set the attribute value and tell all the observers.
'''
old_value = getattr(self, val_attr, None)
setattr(self, val_attr, new_value)
if not only_unequal or old_value != new_value:
self.report_observation(attr)
setter.__name__ = attr
set_prop = get_prop.setter(setter)
setattr(cls, attr, set_prop)
for property_name in property_names:
if hasattr(cls, property_name):
raise ValueError("%s.%s already exists" % (cls, property_name))
make_property(cls, property_name)
return cls
return make_observable_class
@decorator
def default_params(func, _strict=False, **param_defaults):
''' A decorator to provide factory functions for default parameters.
This decorator accepts the following special keyword parameter:
* `_strict`: default `False`; if true only replace genuinely
missing parameters; if false also replace the traditional
`None` placeholder value
The remaining keyword parameters are factory functions
providing the respective default values.
Typical use as a decorator factory:
# in your support module
uses_ds3 = default_params(ds3client=get_ds3client)
# calling code which needs a ds3client
@uses_ds3
def do_something(.., *, ds3client, ...):
... make queries using ds3client ...
This replaces the standard boilerplate and avoids replicating
knowledge of the default factory as exhibited in this legacy code:
def do_something(.., *, ds3client=None, ...):
if ds3client is None:
ds3client = get_ds3client()
... make queries using ds3client ...
It's quite common for me to associate one of these with a
class. I have a `HasThreadState` mixin class which maintains
a thread-local state object which I use to store a per-thread
"ambient" instance of the class so that it does not need to
be plumbed through every call (including all the intermediate
calls which have no interest in the object, the horror!)
So I'll often do this:
class Thing(..., HasThreadState):
....
uses_thing = default_params(thing=Thing.default)
This can be used to provide the ambient instance of `Thing`
to functions while allowing the caller to omit any mention
of a `Thing` or to pass a specific instance if sensible.
(In this examplke, `Thing.default` is a method provided by the mixin.)
And then there's the atypical one off direct use,
which is not really a big win over the conventional way:
@default_params(dbconn=open_default_dbconn,debug=lambda:settings.DB_DEBUG_MODE)
def dbquery(query, *, dbconn):
dbconn.query(query)
'''
if not param_defaults:
raise ValueError("@default_params(%s): no defaults?" % (func,))
def update_kw(kw):
''' Update keyword parameters `kw` from the `param_defaults`.
'''
for param_name, param_default in param_defaults.items():
try:
v = kw[param_name]
except KeyError:
kw[param_name] = param_default()
else:
if v is None and not _strict:
kw[param_name] = param_default()
if isgeneratorfunction(func):
def defaulted_func(*a, **kw):
update_kw(kw)
yield from func(*a, **kw)
else:
def defaulted_func(*a, **kw):
update_kw(kw)
return func(*a, **kw)
defaulted_func.__name__ = func.__name__
# TODO: get the indent from some aspect of stripped_dedent
defaulted_func.__doc__ = '\n '.join(
[
getattr(func, '__doc__', '') or '',
'',
'This function also accepts the following optional keyword parameters:',
*[
'* `%s`: default from `%s()`' % (param_name, param_default)
for param_name, param_default in sorted(param_defaults.items())
],
]
)
sig0 = signature(func)
sig = sig0
modified_params = []
for param in sig0.parameters.values():
modified_param = None
try:
param_default = param_defaults[param.name]
except KeyError:
pass
else:
modified_param = param.replace(
annotation=typing.Optional[param.annotation],
default=None if param_default is param.empty else param_default,
)
if modified_param is None:
modified_param = param.replace()
modified_params.append(modified_param)
sig = sig.replace(parameters=modified_params)
defaulted_func.__signature__ = sig
return defaulted_func
@decorator
def uses_cmd_options(
func, _strict=False, _options_param_name='options', **option_defaults
):
''' A decorator to provide default keyword arguments
from the prevailing `cs.cmdutils.BaseCommandOptions`
if available, otherwise from `option_defaults`.
This exists to provide plumbing free access to options set
up by a command line invocation using `cs.cmdutils.BaseCommand`.
If no `option_defaults` are provided, a single `options`
keyword argument is provided which is the prevailing
`BaseCommand.Options` instance.
The decorator accepts two optional "private" keyword arguments
commencing with underscores:
* `_strict`: default `False`; if true then an `option_defaults`
will only be applied if the argument is _missing_ from the
function arguments, otherwise it will be applied if the
argument is missing or `None`
* `_options_param_name`: default `'options'`; this is the
name of the single `options` keyword argument which will be
supplied if there are no `option_defaults`
Examples:
@uses_cmd_options(doit=True, quiet=False)
def func(x, *, doit, quiet, **kw):
if not quiet:
print("something", x, kw)
if doit:
... do the thing ...
... etc ...
@uses_cmd_options()
def func(x, *, options, **kw):
if not options.quiet:
print("something", x, kw)
if options.doit:
... do the thing ...
... etc ...
'''
from cs.context import stackattrs
def uses_cmd_wrapper(*func_a, **func_kw):
# fill in the func_kw from the defaults
# and keep a record of the chosen values
# run with the prevailing BaseCommand suitably updated
try:
from cs.cmdutils import BaseCommand
except ImportError:
# missing cs.cmdutils or cs.context,
# make an options with no attributes
class Options:
'''Dummy options object for accruing attributes.'''
options = Options()
else:
options_class = BaseCommand.Options
options = options_class.default() or options_class()
option_updates = {}
if not option_defaults:
option_defaults[_options_param_name] = options
for option_name, option_default in option_defaults.items():
if _strict:
# skip if the option is not provided by the caller
if option_name in func_kw:
continue
elif func_kw.get(option_name) is not None:
# skip if the option is not provided by the caller
# or is provided as None
continue
option_value = getattr(options, option_name, None)
if option_value is None:
option_value = option_default
option_updates[option_name] = option_value
func_kw.update(option_updates)
with stackattrs(options, **option_updates), options:
return func(*func_a, **func_kw)
return uses_cmd_wrapper
uses_doit = uses_cmd_options(doit=True)
uses_force = uses_cmd_options(force=False)
uses_quiet = uses_cmd_options(quiet=False)
uses_verbose = uses_cmd_options(verbose=False)
@decorator
def verbosity(func, verbosity_level: int):
''' A decorator to run `func` if the ambient verbosity is >= `verbosity_level`.
'''
@uses_cmd_options(verbosity=0)
def verbose_wrapper(*a, verbosity, **kw):
if verbosity >= verbosity_level:
return func(*a, **kw)
return None
return verbose_wrapper
def verbose(func):
''' A decorator to run `func` if the ambient verbosity is >=1.
'''
return verbosity(func, 1)
def vv(func):
''' A decorator to run `func` if the ambient verbosity is >=2.
'''
return verbosity(func, 2)
# TODO: handle async functions
# pylint: disable=too-many-statements
@decorator
def promote(func, params=None, types=None):
''' A decorator to promote argument values automatically in annotated functions.
If the annotation is `Optional[some_type]` or `Union[some_type,None]`
then the promotion will be to `some_type` but a value of `None`
will be passed through unchanged.
The decorator accepts optional parameters:
* `params`: if supplied, only parameters in this list will
be promoted
* `types`: if supplied, only types in this list will be
considered for promotion
For any parameter with a type annotation, if that type has a
`.promote(value)` class method and the function is called with a
value not of the type of the annotation, the `.promote` method
will be called to promote the value to the expected type.