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
|
%global _empty_manifest_terminate_build 0
Name: python-Py-Meta-Utils
Version: 0.7.8
Release: 1
Summary: Metaclass utilities for Python
License: MIT
URL: https://github.com/briancappello/py-meta-utils
Source0: https://mirrors.nju.edu.cn/pypi/web/packages/76/68/4ed513aea63fada70a3285c6d64d8cba976acfae0bcf21f388175ccf826c/Py-Meta-Utils-0.7.8.tar.gz
BuildArch: noarch
Requires: python3-m2r
Requires: python3-sphinx
Requires: python3-sphinx-material
%description
# Py Meta Utils
## Useful Links
* [Official Documentation on Read The Docs](http://py-meta-utils.readthedocs.io/)
* [Source Code on GitHub](https://github.com/briancappello/py-meta-utils)
* [PyPI](https://pypi.org/project/Py-Meta-Utils/)
## The Meta Options Factory Pattern as a library, and related metaclass utilities
When you as a library or framework author want your end users to be able to write something like this:
```python
class SomeClass(YourBaseClass):
class Meta:
option1 = 'value1'
option2 = 'value2'
option3 = 'value3'
```
And you need a way to define each of these option/value pairs, and a way to "attach" custom behavior to them (ie, code that manipulates code using a custom `metaclass` on `YourBaseClass`). There are a couple common-ish patterns to accomplish this. Django and Graphene have one way, Marshmallow another, and Factory Boy another (and no doubt probably others). But Factory Boy's implementation is by far the most powerful and flexible one I've come across.
I discovered this pattern while reading the source code of [factory_boy](https://factoryboy.readthedocs.io/en/latest/) (specifically, [this file](https://github.com/FactoryBoy/factory_boy/blob/master/factory/base.py)).
And I decided to extract it and turn it into a reusable library. In the process, I ended up refactoring a few things and adding a couple niceties to improve upon its usage.
Let's take a look at a silly example to allow your end users to be able to optionally enable logging of the actions of a class from a library you're writing:
```python
class EndUserClass(YourLoggableService):
class Meta:
debug: bool = True
verbosity: int = 2
log_destination: str = '/tmp/end-user-class.log'
```
The first step is to define your custom [MetaOption](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.MetaOption) subclasses:
- All that's absolutely required to implement is the constructor and its `name` argument. That said, it's recommended to also specify the `default` and `inherit` arguments for the sake of being explicit.
- The `check_value` method is optional, but useful for making sure your users aren't giving you garbage.
- The `get_value` method has a default implementation that normally you shouldn't need to override, unless your default value is mutable or you have advanced logic.
- There's also a `contribute_to_class` method that we'll cover later on.
```python
import os
import sys
# first we have to import what we need from py_meta_utils
from py_meta_utils import (McsArgs, MetaOption, MetaOptionsFactory,
process_factory_meta_options, _missing)
# then we have to declare the meta options the meta options factory should support
class DebugMetaOption(MetaOption):
def __init__(self):
super().__init__(name='debug', default=False, inherit=True)
def check_value(self, value, mcs_args: McsArgs):
if not isinstance(value, bool):
raise TypeError(f'The {self.name} Meta option must be a bool')
class VerbosityMetaOption(MetaOption):
def __init__(self):
super().__init__(name='verbosity', default=1, inherit=True)
def check_value(self, value, mcs_args: McsArgs):
if value not in {1, 2, 3}:
raise ValueError(f'The {self.name} Meta option must either 1, 2, or 3')
class LogDestinationMetaOption(MetaOption):
def __init__(self):
super().__init__(name='log_destination', default=_missing, inherit=True)
# this pattern is useful if you need a mutable default value like [] or {}
def get_value(self, Meta, base_classes_meta, mcs_args: McsArgs):
value = super().get_value(Meta, base_classes_meta, mcs_args)
return value if value != _missing else 'stdout'
def check_value(self, value, mcs_args: McsArgs):
if value in {'stdout', 'stderr'}:
return
try:
dir_exists = os.path.exists(os.path.dirname(value))
except:
dir_exists = False
if not dir_exists:
raise ValueError(f'The {self.name} Meta option must be one of `stdout`, '
'`stderr`, or a valid filepath')
```
The next step is to subclass [MetaOptionsFactory](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.MetaOptionsFactory) and specify the [MetaOption](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.MetaOption) subclasses you want:
```python
class LoggingMetaOptionsFactory(MetaOptionsFactory):
_options = [
DebugMetaOption,
VerbosityMetaOption,
LogDestinationMetaOption,
]
```
Then you need a metaclass to actually apply the factory options:
```python
class LoggingMetaclass(type):
def __new__(mcs, name, bases, clsdict):
mcs_args = McsArgs(mcs, name, bases, clsdict)
process_factory_meta_options(mcs_args, LoggingMetaOptionsFactory)
return super().__new__(*mcs_args)
```
And lastly, create the public class, using the metaclass just defined:
```python
class YourLoggableService(metaclass=LoggingMetaclass):
def do_important_stuff(self):
if self.Meta.verbosity < 2:
self._log('doing important stuff')
else:
self._log('doing really detailed important stuff like so')
def _log(self, msg):
if not self.Meta.debug:
return
if self.Meta.log_destination == 'stdout':
print(msg)
elif self.Meta.log_destination == 'stderr':
sys.stderr.write(msg)
sys.stderr.flush()
elif self.Meta.log_destination:
with open(self.Meta.log_destination, 'a') as f:
f.write(msg)
```
The options factory automatically adds the `Meta` attribute to the class-under-construction (in this example, `YourLoggableService`). (In this case the `Meta` attribute will be populated with the default values as supplied by the [MetaOption](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.MetaOption) subclasses specified by the factory.) In the case where the class-under-construction has a partial `Meta` class, the missing meta options will be added to it.(*)
(*) In effect that's what happens, and for all practical purposes is probably how you should think about it, but technically speaking, the class-under-construction's `Meta` attribute actually gets replaced with a populated instance of the specified [MetaOptionsFactory](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.MetaOptionsFactory) subclass.
The one thing we didn't cover is `MetaOption.contribute_to_class`. This is an optional callback hook that allows `MetaOption` subclasses to, well, contribute something to the class-under-construction. Most likely it adds/removes attributes to/from the class, or perhaps it wraps some method(s) with a decorator or something else entirely.
A good simple example can be found in the source code for the included [AbstractMetaOption](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.AbstractMetaOption):
```python
ABSTRACT_ATTR = '__abstract__'
class AbstractMetaOption(MetaOption):
def __init__(self):
super().__init__(name='abstract', default=False, inherit=False)
def get_value(self, Meta, base_classes_meta, mcs_args: McsArgs):
# class attributes take precedence over the class Meta's value
if mcs_args.clsdict.get(ABSTRACT_ATTR, False) is True:
return True
return super().get_value(Meta, base_classes_meta, mcs_args) is True
def contribute_to_class(self, mcs_args: McsArgs, value):
if value is True:
mcs_args.clsdict[ABSTRACT_ATTR] = True
else:
mcs_args.clsdict[ABSTRACT_ATTR] = False
```
A number of libraries use the `__abstract__` class attribute to determine whether or not the class-under-construction should be considered concrete or not, but they won't understand class `Meta` options. Therefore, we implement `MetaOption.contribute_to_class` to set the `__abstract__` class attribute to the appropriate value for backwards compatibility with such libraries.
## Included Metaclass Utilities
### Singleton
[Singleton](http://localhost:8000/api.html#singleton) is an included metaclass that makes any class utilizing it a singleton:
```python
from py_meta_utils import Singleton
class YourSingleton(metaclass=Singleton):
pass
instance = YourSingleton()
assert instance == YourSingleton()
```
Classes using [Singleton](http://localhost:8000/api.html#singleton) can be subclassed, however, you must inform the base class of your subclass:
```python
from py_meta_utils import Singleton
class BaseSingleton(metaclass=Singleton):
pass
class Extended(BaseSingleton):
pass
BaseSingleton.set_singleton_class(Extended)
base_instance = BaseSingleton()
extended_instance = Extended()
assert base_instance == extended_instance == BaseSingleton() == Extended()
```
### deep_getattr
```python
deep_getattr(clsdict, bases, 'attr_name', [default])
```
`deep_getattr` acts just like `getattr` would on a constructed class object, except this operates on the pre-class-construction class dictionary and base classes. In other words, first we look for the attribute in the class dictionary, and then we search all the base classes (in method resolution order), finally returning the default value if the attribute was not found in any of the class dictionary or base classes (or it raises `AttributeError` if `default` not given).
### OptionalMetaclass and OptionalClass
```python
try:
from optional_dependency import SomeClass
except ImportError:
from py_meta_utils import OptionalClass as SomeClass
class Optional(SomeClass):
pass
```
## License
MIT
%package -n python3-Py-Meta-Utils
Summary: Metaclass utilities for Python
Provides: python-Py-Meta-Utils
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-Py-Meta-Utils
# Py Meta Utils
## Useful Links
* [Official Documentation on Read The Docs](http://py-meta-utils.readthedocs.io/)
* [Source Code on GitHub](https://github.com/briancappello/py-meta-utils)
* [PyPI](https://pypi.org/project/Py-Meta-Utils/)
## The Meta Options Factory Pattern as a library, and related metaclass utilities
When you as a library or framework author want your end users to be able to write something like this:
```python
class SomeClass(YourBaseClass):
class Meta:
option1 = 'value1'
option2 = 'value2'
option3 = 'value3'
```
And you need a way to define each of these option/value pairs, and a way to "attach" custom behavior to them (ie, code that manipulates code using a custom `metaclass` on `YourBaseClass`). There are a couple common-ish patterns to accomplish this. Django and Graphene have one way, Marshmallow another, and Factory Boy another (and no doubt probably others). But Factory Boy's implementation is by far the most powerful and flexible one I've come across.
I discovered this pattern while reading the source code of [factory_boy](https://factoryboy.readthedocs.io/en/latest/) (specifically, [this file](https://github.com/FactoryBoy/factory_boy/blob/master/factory/base.py)).
And I decided to extract it and turn it into a reusable library. In the process, I ended up refactoring a few things and adding a couple niceties to improve upon its usage.
Let's take a look at a silly example to allow your end users to be able to optionally enable logging of the actions of a class from a library you're writing:
```python
class EndUserClass(YourLoggableService):
class Meta:
debug: bool = True
verbosity: int = 2
log_destination: str = '/tmp/end-user-class.log'
```
The first step is to define your custom [MetaOption](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.MetaOption) subclasses:
- All that's absolutely required to implement is the constructor and its `name` argument. That said, it's recommended to also specify the `default` and `inherit` arguments for the sake of being explicit.
- The `check_value` method is optional, but useful for making sure your users aren't giving you garbage.
- The `get_value` method has a default implementation that normally you shouldn't need to override, unless your default value is mutable or you have advanced logic.
- There's also a `contribute_to_class` method that we'll cover later on.
```python
import os
import sys
# first we have to import what we need from py_meta_utils
from py_meta_utils import (McsArgs, MetaOption, MetaOptionsFactory,
process_factory_meta_options, _missing)
# then we have to declare the meta options the meta options factory should support
class DebugMetaOption(MetaOption):
def __init__(self):
super().__init__(name='debug', default=False, inherit=True)
def check_value(self, value, mcs_args: McsArgs):
if not isinstance(value, bool):
raise TypeError(f'The {self.name} Meta option must be a bool')
class VerbosityMetaOption(MetaOption):
def __init__(self):
super().__init__(name='verbosity', default=1, inherit=True)
def check_value(self, value, mcs_args: McsArgs):
if value not in {1, 2, 3}:
raise ValueError(f'The {self.name} Meta option must either 1, 2, or 3')
class LogDestinationMetaOption(MetaOption):
def __init__(self):
super().__init__(name='log_destination', default=_missing, inherit=True)
# this pattern is useful if you need a mutable default value like [] or {}
def get_value(self, Meta, base_classes_meta, mcs_args: McsArgs):
value = super().get_value(Meta, base_classes_meta, mcs_args)
return value if value != _missing else 'stdout'
def check_value(self, value, mcs_args: McsArgs):
if value in {'stdout', 'stderr'}:
return
try:
dir_exists = os.path.exists(os.path.dirname(value))
except:
dir_exists = False
if not dir_exists:
raise ValueError(f'The {self.name} Meta option must be one of `stdout`, '
'`stderr`, or a valid filepath')
```
The next step is to subclass [MetaOptionsFactory](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.MetaOptionsFactory) and specify the [MetaOption](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.MetaOption) subclasses you want:
```python
class LoggingMetaOptionsFactory(MetaOptionsFactory):
_options = [
DebugMetaOption,
VerbosityMetaOption,
LogDestinationMetaOption,
]
```
Then you need a metaclass to actually apply the factory options:
```python
class LoggingMetaclass(type):
def __new__(mcs, name, bases, clsdict):
mcs_args = McsArgs(mcs, name, bases, clsdict)
process_factory_meta_options(mcs_args, LoggingMetaOptionsFactory)
return super().__new__(*mcs_args)
```
And lastly, create the public class, using the metaclass just defined:
```python
class YourLoggableService(metaclass=LoggingMetaclass):
def do_important_stuff(self):
if self.Meta.verbosity < 2:
self._log('doing important stuff')
else:
self._log('doing really detailed important stuff like so')
def _log(self, msg):
if not self.Meta.debug:
return
if self.Meta.log_destination == 'stdout':
print(msg)
elif self.Meta.log_destination == 'stderr':
sys.stderr.write(msg)
sys.stderr.flush()
elif self.Meta.log_destination:
with open(self.Meta.log_destination, 'a') as f:
f.write(msg)
```
The options factory automatically adds the `Meta` attribute to the class-under-construction (in this example, `YourLoggableService`). (In this case the `Meta` attribute will be populated with the default values as supplied by the [MetaOption](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.MetaOption) subclasses specified by the factory.) In the case where the class-under-construction has a partial `Meta` class, the missing meta options will be added to it.(*)
(*) In effect that's what happens, and for all practical purposes is probably how you should think about it, but technically speaking, the class-under-construction's `Meta` attribute actually gets replaced with a populated instance of the specified [MetaOptionsFactory](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.MetaOptionsFactory) subclass.
The one thing we didn't cover is `MetaOption.contribute_to_class`. This is an optional callback hook that allows `MetaOption` subclasses to, well, contribute something to the class-under-construction. Most likely it adds/removes attributes to/from the class, or perhaps it wraps some method(s) with a decorator or something else entirely.
A good simple example can be found in the source code for the included [AbstractMetaOption](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.AbstractMetaOption):
```python
ABSTRACT_ATTR = '__abstract__'
class AbstractMetaOption(MetaOption):
def __init__(self):
super().__init__(name='abstract', default=False, inherit=False)
def get_value(self, Meta, base_classes_meta, mcs_args: McsArgs):
# class attributes take precedence over the class Meta's value
if mcs_args.clsdict.get(ABSTRACT_ATTR, False) is True:
return True
return super().get_value(Meta, base_classes_meta, mcs_args) is True
def contribute_to_class(self, mcs_args: McsArgs, value):
if value is True:
mcs_args.clsdict[ABSTRACT_ATTR] = True
else:
mcs_args.clsdict[ABSTRACT_ATTR] = False
```
A number of libraries use the `__abstract__` class attribute to determine whether or not the class-under-construction should be considered concrete or not, but they won't understand class `Meta` options. Therefore, we implement `MetaOption.contribute_to_class` to set the `__abstract__` class attribute to the appropriate value for backwards compatibility with such libraries.
## Included Metaclass Utilities
### Singleton
[Singleton](http://localhost:8000/api.html#singleton) is an included metaclass that makes any class utilizing it a singleton:
```python
from py_meta_utils import Singleton
class YourSingleton(metaclass=Singleton):
pass
instance = YourSingleton()
assert instance == YourSingleton()
```
Classes using [Singleton](http://localhost:8000/api.html#singleton) can be subclassed, however, you must inform the base class of your subclass:
```python
from py_meta_utils import Singleton
class BaseSingleton(metaclass=Singleton):
pass
class Extended(BaseSingleton):
pass
BaseSingleton.set_singleton_class(Extended)
base_instance = BaseSingleton()
extended_instance = Extended()
assert base_instance == extended_instance == BaseSingleton() == Extended()
```
### deep_getattr
```python
deep_getattr(clsdict, bases, 'attr_name', [default])
```
`deep_getattr` acts just like `getattr` would on a constructed class object, except this operates on the pre-class-construction class dictionary and base classes. In other words, first we look for the attribute in the class dictionary, and then we search all the base classes (in method resolution order), finally returning the default value if the attribute was not found in any of the class dictionary or base classes (or it raises `AttributeError` if `default` not given).
### OptionalMetaclass and OptionalClass
```python
try:
from optional_dependency import SomeClass
except ImportError:
from py_meta_utils import OptionalClass as SomeClass
class Optional(SomeClass):
pass
```
## License
MIT
%package help
Summary: Development documents and examples for Py-Meta-Utils
Provides: python3-Py-Meta-Utils-doc
%description help
# Py Meta Utils
## Useful Links
* [Official Documentation on Read The Docs](http://py-meta-utils.readthedocs.io/)
* [Source Code on GitHub](https://github.com/briancappello/py-meta-utils)
* [PyPI](https://pypi.org/project/Py-Meta-Utils/)
## The Meta Options Factory Pattern as a library, and related metaclass utilities
When you as a library or framework author want your end users to be able to write something like this:
```python
class SomeClass(YourBaseClass):
class Meta:
option1 = 'value1'
option2 = 'value2'
option3 = 'value3'
```
And you need a way to define each of these option/value pairs, and a way to "attach" custom behavior to them (ie, code that manipulates code using a custom `metaclass` on `YourBaseClass`). There are a couple common-ish patterns to accomplish this. Django and Graphene have one way, Marshmallow another, and Factory Boy another (and no doubt probably others). But Factory Boy's implementation is by far the most powerful and flexible one I've come across.
I discovered this pattern while reading the source code of [factory_boy](https://factoryboy.readthedocs.io/en/latest/) (specifically, [this file](https://github.com/FactoryBoy/factory_boy/blob/master/factory/base.py)).
And I decided to extract it and turn it into a reusable library. In the process, I ended up refactoring a few things and adding a couple niceties to improve upon its usage.
Let's take a look at a silly example to allow your end users to be able to optionally enable logging of the actions of a class from a library you're writing:
```python
class EndUserClass(YourLoggableService):
class Meta:
debug: bool = True
verbosity: int = 2
log_destination: str = '/tmp/end-user-class.log'
```
The first step is to define your custom [MetaOption](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.MetaOption) subclasses:
- All that's absolutely required to implement is the constructor and its `name` argument. That said, it's recommended to also specify the `default` and `inherit` arguments for the sake of being explicit.
- The `check_value` method is optional, but useful for making sure your users aren't giving you garbage.
- The `get_value` method has a default implementation that normally you shouldn't need to override, unless your default value is mutable or you have advanced logic.
- There's also a `contribute_to_class` method that we'll cover later on.
```python
import os
import sys
# first we have to import what we need from py_meta_utils
from py_meta_utils import (McsArgs, MetaOption, MetaOptionsFactory,
process_factory_meta_options, _missing)
# then we have to declare the meta options the meta options factory should support
class DebugMetaOption(MetaOption):
def __init__(self):
super().__init__(name='debug', default=False, inherit=True)
def check_value(self, value, mcs_args: McsArgs):
if not isinstance(value, bool):
raise TypeError(f'The {self.name} Meta option must be a bool')
class VerbosityMetaOption(MetaOption):
def __init__(self):
super().__init__(name='verbosity', default=1, inherit=True)
def check_value(self, value, mcs_args: McsArgs):
if value not in {1, 2, 3}:
raise ValueError(f'The {self.name} Meta option must either 1, 2, or 3')
class LogDestinationMetaOption(MetaOption):
def __init__(self):
super().__init__(name='log_destination', default=_missing, inherit=True)
# this pattern is useful if you need a mutable default value like [] or {}
def get_value(self, Meta, base_classes_meta, mcs_args: McsArgs):
value = super().get_value(Meta, base_classes_meta, mcs_args)
return value if value != _missing else 'stdout'
def check_value(self, value, mcs_args: McsArgs):
if value in {'stdout', 'stderr'}:
return
try:
dir_exists = os.path.exists(os.path.dirname(value))
except:
dir_exists = False
if not dir_exists:
raise ValueError(f'The {self.name} Meta option must be one of `stdout`, '
'`stderr`, or a valid filepath')
```
The next step is to subclass [MetaOptionsFactory](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.MetaOptionsFactory) and specify the [MetaOption](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.MetaOption) subclasses you want:
```python
class LoggingMetaOptionsFactory(MetaOptionsFactory):
_options = [
DebugMetaOption,
VerbosityMetaOption,
LogDestinationMetaOption,
]
```
Then you need a metaclass to actually apply the factory options:
```python
class LoggingMetaclass(type):
def __new__(mcs, name, bases, clsdict):
mcs_args = McsArgs(mcs, name, bases, clsdict)
process_factory_meta_options(mcs_args, LoggingMetaOptionsFactory)
return super().__new__(*mcs_args)
```
And lastly, create the public class, using the metaclass just defined:
```python
class YourLoggableService(metaclass=LoggingMetaclass):
def do_important_stuff(self):
if self.Meta.verbosity < 2:
self._log('doing important stuff')
else:
self._log('doing really detailed important stuff like so')
def _log(self, msg):
if not self.Meta.debug:
return
if self.Meta.log_destination == 'stdout':
print(msg)
elif self.Meta.log_destination == 'stderr':
sys.stderr.write(msg)
sys.stderr.flush()
elif self.Meta.log_destination:
with open(self.Meta.log_destination, 'a') as f:
f.write(msg)
```
The options factory automatically adds the `Meta` attribute to the class-under-construction (in this example, `YourLoggableService`). (In this case the `Meta` attribute will be populated with the default values as supplied by the [MetaOption](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.MetaOption) subclasses specified by the factory.) In the case where the class-under-construction has a partial `Meta` class, the missing meta options will be added to it.(*)
(*) In effect that's what happens, and for all practical purposes is probably how you should think about it, but technically speaking, the class-under-construction's `Meta` attribute actually gets replaced with a populated instance of the specified [MetaOptionsFactory](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.MetaOptionsFactory) subclass.
The one thing we didn't cover is `MetaOption.contribute_to_class`. This is an optional callback hook that allows `MetaOption` subclasses to, well, contribute something to the class-under-construction. Most likely it adds/removes attributes to/from the class, or perhaps it wraps some method(s) with a decorator or something else entirely.
A good simple example can be found in the source code for the included [AbstractMetaOption](https://py-meta-utils.readthedocs.io/en/latest/api.html#py_meta_utils.AbstractMetaOption):
```python
ABSTRACT_ATTR = '__abstract__'
class AbstractMetaOption(MetaOption):
def __init__(self):
super().__init__(name='abstract', default=False, inherit=False)
def get_value(self, Meta, base_classes_meta, mcs_args: McsArgs):
# class attributes take precedence over the class Meta's value
if mcs_args.clsdict.get(ABSTRACT_ATTR, False) is True:
return True
return super().get_value(Meta, base_classes_meta, mcs_args) is True
def contribute_to_class(self, mcs_args: McsArgs, value):
if value is True:
mcs_args.clsdict[ABSTRACT_ATTR] = True
else:
mcs_args.clsdict[ABSTRACT_ATTR] = False
```
A number of libraries use the `__abstract__` class attribute to determine whether or not the class-under-construction should be considered concrete or not, but they won't understand class `Meta` options. Therefore, we implement `MetaOption.contribute_to_class` to set the `__abstract__` class attribute to the appropriate value for backwards compatibility with such libraries.
## Included Metaclass Utilities
### Singleton
[Singleton](http://localhost:8000/api.html#singleton) is an included metaclass that makes any class utilizing it a singleton:
```python
from py_meta_utils import Singleton
class YourSingleton(metaclass=Singleton):
pass
instance = YourSingleton()
assert instance == YourSingleton()
```
Classes using [Singleton](http://localhost:8000/api.html#singleton) can be subclassed, however, you must inform the base class of your subclass:
```python
from py_meta_utils import Singleton
class BaseSingleton(metaclass=Singleton):
pass
class Extended(BaseSingleton):
pass
BaseSingleton.set_singleton_class(Extended)
base_instance = BaseSingleton()
extended_instance = Extended()
assert base_instance == extended_instance == BaseSingleton() == Extended()
```
### deep_getattr
```python
deep_getattr(clsdict, bases, 'attr_name', [default])
```
`deep_getattr` acts just like `getattr` would on a constructed class object, except this operates on the pre-class-construction class dictionary and base classes. In other words, first we look for the attribute in the class dictionary, and then we search all the base classes (in method resolution order), finally returning the default value if the attribute was not found in any of the class dictionary or base classes (or it raises `AttributeError` if `default` not given).
### OptionalMetaclass and OptionalClass
```python
try:
from optional_dependency import SomeClass
except ImportError:
from py_meta_utils import OptionalClass as SomeClass
class Optional(SomeClass):
pass
```
## License
MIT
%prep
%autosetup -n Py-Meta-Utils-0.7.8
%build
%py3_build
%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .
%files -n python3-Py-Meta-Utils -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Mon May 29 2023 Python_Bot <Python_Bot@openeuler.org> - 0.7.8-1
- Package Spec generated
|