[IMP] Added missing vim mode lines
[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     return Record(**kwargs)
113
114 def python_constructor(loader, node):
115     kwargs = loader.construct_mapping(node)
116     return Python(**kwargs)
117
118 def menuitem_constructor(loader, node):
119     kwargs = loader.construct_mapping(node)
120     return Menuitem(**kwargs)
121
122 def workflow_constructor(loader, node):
123     kwargs = loader.construct_mapping(node)
124     return Workflow(**kwargs)
125
126 def act_window_constructor(loader, node):
127     kwargs = loader.construct_mapping(node)
128     return ActWindow(**kwargs)
129
130 def function_constructor(loader, node):
131     kwargs = loader.construct_mapping(node)
132     return Function(**kwargs)
133
134 def report_constructor(loader, node):
135     kwargs = loader.construct_mapping(node)
136     return Report(**kwargs)
137
138 def delete_constructor(loader, node):
139     kwargs = loader.construct_mapping(node)
140     return Delete(**kwargs)
141
142 def context_constructor(loader, node):
143     kwargs = loader.construct_mapping(node)
144     return Context(**kwargs)
145
146 def url_constructor(loader, node):
147     kwargs = loader.construct_mapping(node)
148     return Url(**kwargs)
149
150 def eval_constructor(loader, node):
151     expression = loader.construct_scalar(node)
152     return Eval(expression)
153     
154 def ref_constructor(loader, tag_suffix, node):
155     if tag_suffix == "id":
156         kwargs = {"id": loader.construct_scalar(node)}
157     else:
158         kwargs = loader.construct_mapping(node)
159     return Ref(**kwargs)
160     
161 def ir_set_constructor(loader, node):
162     kwargs = loader.construct_mapping(node)
163     return IrSet(**kwargs)
164     
165 # Registers constructors for custom tags.
166 # Constructors are actually defined globally: do not redefined them in another
167 # class/file/package.  This means that module recorder need import this file.
168 def add_constructors():
169     yaml.add_constructor(u"!assert", assert_constructor)
170     yaml.add_constructor(u"!record", record_constructor)
171     yaml.add_constructor(u"!python", python_constructor)
172     yaml.add_constructor(u"!menuitem", menuitem_constructor)
173     yaml.add_constructor(u"!workflow", workflow_constructor)
174     yaml.add_constructor(u"!act_window", act_window_constructor)
175     yaml.add_constructor(u"!function", function_constructor)
176     yaml.add_constructor(u"!report", report_constructor)
177     yaml.add_constructor(u"!context", context_constructor)
178     yaml.add_constructor(u"!delete", delete_constructor)
179     yaml.add_constructor(u"!url", url_constructor)
180     yaml.add_constructor(u"!eval", eval_constructor)
181     yaml.add_multi_constructor(u"!ref", ref_constructor)
182     yaml.add_constructor(u"!ir_set", ir_set_constructor)
183 add_constructors()
184
185
186 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: