[REVERT] r3591: causing problem to install some modules
[odoo/odoo.git] / bin / tools / yaml_tag.py
1 import yaml
2 import logging
3
4 class YamlTag(object):
5     """
6     Superclass for constructors of custom tags defined in yaml file.
7     __str__ is overriden in subclass and used for serialization in module recorder.
8     """
9     def __init__(self, **kwargs):
10         self.__dict__.update(kwargs)
11     def __getitem__(self, key):
12         return getattr(self, key)
13     def __getattr__(self, attr):
14         return None
15     def __repr__(self):
16         return "<%s %s>" % (self.__class__.__name__, sorted(self.__dict__.items()))
17
18 class Assert(YamlTag):
19     def __init__(self, model, id=None, severity=logging.WARNING, string="NONAME", **kwargs):
20         self.model = model
21         self.id = id
22         self.severity = severity
23         self.string = string
24         super(Assert, self).__init__(**kwargs)
25     
26 class Record(YamlTag):
27     def __init__(self, model, id, use='id', **kwargs):
28         self.model = model
29         self.id = id
30         super(Record, self).__init__(**kwargs)
31     def __str__(self):
32         return '!record {model: %s, id: %s}:' % (str(self.model,), str(self.id,))
33     
34 class Python(YamlTag):
35     def __init__(self, model, severity=logging.WARNING, name="", **kwargs):
36         self.model= model
37         self.severity = severity
38         self.name = name
39         super(Python, self).__init__(**kwargs)
40     def __str__(self):
41         return '!python {model: %s}: |' % (str(self.model), )
42
43 class Menuitem(YamlTag):
44     def __init__(self, id, name, **kwargs):
45         self.id = id
46         self.name = name
47         super(Menuitem, self).__init__(**kwargs)
48
49 class Workflow(YamlTag):
50     def __init__(self, model, action, ref=None, **kwargs):
51         self.model = model
52         self.action = action
53         self.ref = ref
54         super(Workflow, self).__init__(**kwargs)
55     def __str__(self):
56         return '!workflow {model: %s, action: %s, ref: %s}' % (str(self.model,), str(self.action,), str(self.ref,))
57
58 class ActWindow(YamlTag):
59     def __init__(self, **kwargs):
60         super(ActWindow, self).__init__(**kwargs)
61
62 class Function(YamlTag):
63     def __init__(self, model, name, **kwargs):
64         self.model = model
65         self.name = name
66         super(Function, self).__init__(**kwargs)
67
68 class Report(YamlTag):
69     def __init__(self, model, name, string, **kwargs):
70         self.model = model
71         self.name = name
72         self.string = string
73         super(Report, self).__init__(**kwargs)
74
75 class Delete(YamlTag):
76     def __init__(self, **kwargs):
77         super(Delete, self).__init__(**kwargs)
78
79 class Context(YamlTag):
80     def __init__(self, **kwargs):
81         super(Context, self).__init__(**kwargs)
82
83 class Url(YamlTag):
84     def __init__(self, **kwargs):
85         super(Url, self).__init__(**kwargs)
86
87 class Eval(YamlTag):
88     def __init__(self, expression):
89         self.expression = expression
90         super(Eval, self).__init__()
91     def __str__(self):
92         return '!eval %s' % str(self.expression)
93     
94 class Ref(YamlTag):
95     def __init__(self, expr="False", *args, **kwargs):
96         self.expr = expr
97         super(Ref, self).__init__(*args, **kwargs)
98     def __str__(self):
99         return 'ref(%s)' % repr(self.expr)
100     
101 class IrSet(YamlTag):
102     def __init__(self):
103         super(IrSet, self).__init__()
104
105 def assert_constructor(loader, node):
106     kwargs = loader.construct_mapping(node)
107     return Assert(**kwargs)
108
109 def record_constructor(loader, node):
110     kwargs = loader.construct_mapping(node)
111     return Record(**kwargs)
112
113 def python_constructor(loader, node):
114     kwargs = loader.construct_mapping(node)
115     return Python(**kwargs)
116
117 def menuitem_constructor(loader, node):
118     kwargs = loader.construct_mapping(node)
119     return Menuitem(**kwargs)
120
121 def workflow_constructor(loader, node):
122     kwargs = loader.construct_mapping(node)
123     return Workflow(**kwargs)
124
125 def act_window_constructor(loader, node):
126     kwargs = loader.construct_mapping(node)
127     return ActWindow(**kwargs)
128
129 def function_constructor(loader, node):
130     kwargs = loader.construct_mapping(node)
131     return Function(**kwargs)
132
133 def report_constructor(loader, node):
134     kwargs = loader.construct_mapping(node)
135     return Report(**kwargs)
136
137 def delete_constructor(loader, node):
138     kwargs = loader.construct_mapping(node)
139     return Delete(**kwargs)
140
141 def context_constructor(loader, node):
142     kwargs = loader.construct_mapping(node)
143     return Context(**kwargs)
144
145 def url_constructor(loader, node):
146     kwargs = loader.construct_mapping(node)
147     return Url(**kwargs)
148
149 def eval_constructor(loader, node):
150     expression = loader.construct_scalar(node)
151     return Eval(expression)
152     
153 def ref_constructor(loader, tag_suffix, node):
154     if tag_suffix == "id":
155         kwargs = {"id": loader.construct_scalar(node)}
156     else:
157         kwargs = loader.construct_mapping(node)
158     return Ref(**kwargs)
159     
160 def ir_set_constructor(loader, node):
161     kwargs = loader.construct_mapping(node)
162     return IrSet(**kwargs)
163     
164 # Registers constructors for custom tags.
165 # Constructors are actually defined globally: do not redefined them in another
166 # class/file/package.  This means that module recorder need import this file.
167 def add_constructors():
168     yaml.add_constructor(u"!assert", assert_constructor)
169     yaml.add_constructor(u"!record", record_constructor)
170     yaml.add_constructor(u"!python", python_constructor)
171     yaml.add_constructor(u"!menuitem", menuitem_constructor)
172     yaml.add_constructor(u"!workflow", workflow_constructor)
173     yaml.add_constructor(u"!act_window", act_window_constructor)
174     yaml.add_constructor(u"!function", function_constructor)
175     yaml.add_constructor(u"!report", report_constructor)
176     yaml.add_constructor(u"!context", context_constructor)
177     yaml.add_constructor(u"!delete", delete_constructor)
178     yaml.add_constructor(u"!url", url_constructor)
179     yaml.add_constructor(u"!eval", eval_constructor)
180     yaml.add_multi_constructor(u"!ref", ref_constructor)
181     yaml.add_constructor(u"!ir_set", ir_set_constructor)
182 add_constructors()
183