/
main.py
177 lines (120 loc) · 3.82 KB
/
main.py
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
from logging import Formatter
import logging
verboseLevel = logging.DEBUG
formatter = Formatter('[%(levelname)s] %(message)s','%Y:%m:%d:%H:%M:%S')
logger = logging.root
streamHandler = logging.StreamHandler()
logger.setLevel(verboseLevel)
streamHandler.setFormatter(formatter)
streamHandler.setLevel(verboseLevel)
logger.addHandler(streamHandler)
from utils.printTracer import PrintTracer
PrintTracer.enable()
# -------------------
import os
'''
from pluginmate.loader.importLoader import ImportLoader
loader = ImportLoader()
install_dir = os.path.dirname(__file__)
test_dir = os.path.join(install_dir, 'test')
plug_dir = os.path.join(test_dir, 'plugins')
loader.load([plug_dir])
#'''
import pluginmate
from pluginmate import implements, interface, abstract
from pluginmate.core.extensionPoint import ExtensionPoint
pluginmate.env.push('myenv')
@interface(abstract=False)
class ITest:
def f(self): pass
@implements(ITest)
class B:
def __init__(self):
pass
def f(self):
print("B f")
pluginmate.env.push('myenv2')
b = B()
b.disable()
print('--------------')
e = ExtensionPoint(ITest)
pluginmate.env.pop()
print(e.services())
pluginmate.env.pop()
print (pluginmate.env('myenv'))
#'''
'''
@interface
class IOptionManager:
pass
@implements(IOptionManager)
class OptionMager:
def __init__(self):
self.__values = defaultdict(dict)
pass
def get(self, section, name, default):
data = self.__values[section]
if not name in data:
data[name] = default
return data[name]
def set(self, section, name, value):
data = self.__values[section]
data[name] = value
a = OptionMager()
e = ExtensionPoint(IOptionManager)
print(e.service())
class VirtualOption(object):
def __init__(self, name, section=None, default=None, static=False):
self.name = name
self.static = static
self.section = section
self.default = default
self.data = ExtensionPoint(IOptionManager)
def __get__(self, instance, owner):
return self.data.service().get(section=self.section, name=self.name, default=self.default)
#return getattr(instance,self.name)
def __set__(self, instance, value):
return self.data.service().set(section=self.section, name=self.name, value=value)
#return setattr(instance, self.name, value)
def option(section=None, **kwargs):
frame = inspect.stack()[1][0]
locals_ = frame.f_locals
if locals_ == frame.f_globals:
raise SyntaxError('option() can only be used in a class definition')
if '__module__' in locals_:
# Class decorator. Initialize the VirtualOption, and then
# create a locally-available Option instance.
for x in inspect.stack():
print(x)
print(inspect.stack()[1][0].f_locals)
for name, value in kwargs.items():
locals_[name] = VirtualOption(name, section=section, default=value, static=True)
else:
# Class constructor. If a VirtualOption does not exist in this
# class, create it. Create an Option instance in this instance.
for name, value in kwargs.items():
if not name in locals_["self"].__class__.__dict__:
setattr(locals_["self"].__class__, name, VirtualOption(name, section=section, default=value, static=False))
#setattr(locals_["self"], '_'+name,value)
#print('__module__' in frame.f_locals)
######################################
class SillyWalksMetaClass(type):
def __init__(self, class_name, bases, namespace):
print("!!!", class_name)
class X:
def __init__(self):
option(y=0, section='x')
pass
#print(self.x, self.y, self.z)
x1 = X()
x2 = X()
#x1.x = 5
#x2.x = 6
x1.y = 5
x2.y = 6
#print(x1.x)
#print(x2.x)
print(x1.y)
print(x2.y)
print ('----')
#'''