[REF] Refactoring according to the review of CHS
[odoo/odoo.git] / openerp / 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', view=True, **kwargs):
28         self.model = model
29         self.id = id
30         self.view = view
31         super(Record, self).__init__(**kwargs)
32     def __str__(self):
33         return '!record {model: %s, id: %s}:' % (str(self.model,), str(self.id,))
34
35 class Python(YamlTag):
36     def __init__(self, model, severity=logging.ERROR, name="", **kwargs):
37         self.model= model
38         self.severity = severity
39         self.name = name
40         super(Python, self).__init__(**kwargs)
41     def __str__(self):
42         return '!python {model: %s}: |' % (str(self.model), )
43
44 class Menuitem(YamlTag):
45     def __init__(self, id, name, **kwargs):
46         self.id = id
47         self.name = name
48         super(Menuitem, self).__init__(**kwargs)
49
50 class Workflow(YamlTag):
51     def __init__(self, model, action, ref=None, **kwargs):
52         self.model = model
53         self.action = action
54         self.ref = ref
55         super(Workflow, self).__init__(**kwargs)
56     def __str__(self):
57         return '!workflow {model: %s, action: %s, ref: %s}' % (str(self.model,), str(self.action,), str(self.ref,))
58
59 class ActWindow(YamlTag):
60     def __init__(self, **kwargs):
61         super(ActWindow, self).__init__(**kwargs)
62
63 class Function(YamlTag):
64     def __init__(self, model, name, **kwargs):
65         self.model = model
66         self.name = name
67         super(Function, self).__init__(**kwargs)
68
69 class Report(YamlTag):
70     def __init__(self, model, name, string, **kwargs):
71         self.model = model
72         self.name = name
73         self.string = string
74         super(Report, self).__init__(**kwargs)
75
76 class Delete(YamlTag):
77     def __init__(self, **kwargs):
78         super(Delete, self).__init__(**kwargs)
79
80 class Context(YamlTag):
81     def __init__(self, **kwargs):
82         super(Context, self).__init__(**kwargs)
83
84 class Url(YamlTag):
85     def __init__(self, **kwargs):
86         super(Url, self).__init__(**kwargs)
87
88 class Eval(YamlTag):
89     def __init__(self, expression):
90         self.expression = expression
91         super(Eval, self).__init__()
92     def __str__(self):
93         return '!eval %s' % str(self.expression)
94     
95 class Ref(YamlTag):
96     def __init__(self, expr="False", *args, **kwargs):
97         self.expr = expr
98         super(Ref, self).__init__(*args, **kwargs)
99     def __str__(self):
100         return 'ref(%s)' % repr(self.expr)
101     
102 class IrSet(YamlTag):
103     def __init__(self):
104         super(IrSet, self).__init__()
105
106 def assert_constructor(loader, node):
107     kwargs = loader.construct_mapping(node)
108     return Assert(**kwargs)
109
110 def record_constructor(loader, node):
111     kwargs = loader.construct_mapping(node)
112     assert "model" in kwargs, "'model' argument is required for !record"
113     assert "id" in kwargs, "'id' argument is required for !record"
114     return Record(**kwargs)
115
116 def python_constructor(loader, node):
117     kwargs = loader.construct_mapping(node)
118     return Python(**kwargs)
119
120 def menuitem_constructor(loader, node):
121     kwargs = loader.construct_mapping(node)
122     return Menuitem(**kwargs)
123
124 def workflow_constructor(loader, node):
125     kwargs = loader.construct_mapping(node)
126     return Workflow(**kwargs)
127
128 def act_window_constructor(loader, node):
129     kwargs = loader.construct_mapping(node)
130     return ActWindow(**kwargs)
131
132 def function_constructor(loader, node):
133     kwargs = loader.construct_mapping(node)
134     return Function(**kwargs)
135
136 def report_constructor(loader, node):
137     kwargs = loader.construct_mapping(node)
138     return Report(**kwargs)
139
140 def delete_constructor(loader, node):
141     kwargs = loader.construct_mapping(node)
142     return Delete(**kwargs)
143
144 def context_constructor(loader, node):
145     kwargs = loader.construct_mapping(node)
146     return Context(**kwargs)
147
148 def url_constructor(loader, node):
149     kwargs = loader.construct_mapping(node)
150     return Url(**kwargs)
151
152 def eval_constructor(loader, node):
153     expression = loader.construct_scalar(node)
154     return Eval(expression)
155     
156 def ref_constructor(loader, tag_suffix, node):
157     if tag_suffix == "id":
158         kwargs = {"id": loader.construct_scalar(node)}
159     else:
160         kwargs = loader.construct_mapping(node)
161     return Ref(**kwargs)
162     
163 def ir_set_constructor(loader, node):
164     kwargs = loader.construct_mapping(node)
165     return IrSet(**kwargs)
166     
167 # Registers constructors for custom tags.
168 # Constructors are actually defined globally: do not redefined them in another
169 # class/file/package.  This means that module recorder need import this file.
170 def add_constructors():
171     yaml.add_constructor(u"!assert", assert_constructor)
172     yaml.add_constructor(u"!record", record_constructor)
173     yaml.add_constructor(u"!python", python_constructor)
174     yaml.add_constructor(u"!menuitem", menuitem_constructor)
175     yaml.add_constructor(u"!workflow", workflow_constructor)
176     yaml.add_constructor(u"!act_window", act_window_constructor)
177     yaml.add_constructor(u"!function", function_constructor)
178     yaml.add_constructor(u"!report", report_constructor)
179     yaml.add_constructor(u"!context", context_constructor)
180     yaml.add_constructor(u"!delete", delete_constructor)
181     yaml.add_constructor(u"!url", url_constructor)
182     yaml.add_constructor(u"!eval", eval_constructor)
183     yaml.add_multi_constructor(u"!ref", ref_constructor)
184     yaml.add_constructor(u"!ir_set", ir_set_constructor)
185 add_constructors()
186
187
188 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: