-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrols.py
161 lines (126 loc) · 5.04 KB
/
controls.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
# -*- coding: UTF-8 -*-
# Copyright (C) 2005 <jdavid@favela.(none)>
# Copyright (C) 2006 J. David Ibanez <[email protected]>
# Copyright (C) 2006 luis <[email protected]>
# Copyright (C) 2006-2008, 2010 Hervé Cauwelier <[email protected]>
# Copyright (C) 2008 Henry Obein <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Import from itools
from itools.csv import CSVFile
from itools.datatypes import Enumerate, Unicode
from itools.gettext import MSG
from itools.web import ERROR, STLView
# Import from ikaaro
from ikaaro.fields import Char_Field, File_Field
from ikaaro.folder import Folder
# Import from goodforms
from schema import Variable, Expression
from utils import FormatError
ERR_EMPTY_TITLE = ERROR(u'In controls, line {line}, title is missing.')
ERR_EMPTY_EXPRESSION = ERROR(u'In controls, line {line}, expression is missing.')
ERR_BAD_EXPRESSION = ERROR(u'In controls, line {line}, syntax error in expression: {err}')
ERR_BAD_LEVEL = ERROR(u'In controls, line {line}, unexpected level "{level}".')
ERR_EMPTY_VARIABLE = ERROR(u'In controls, line {line}, main variable is missing.')
class ControlLevel(Enumerate):
options = [
{'name': '0', 'value': u"Informative"},
{'name': '1', 'value': u"Warning"},
{'name': '2', 'value': u"Error"}]
@staticmethod
def decode(data):
data = data.strip()
return Enumerate.decode(data)
class ControlsHandler(CSVFile):
skip_header = True
has_header = True
class_csv_guess = True
schema = {
'number': Unicode(mandatory=True, title=MSG(u"Number")),
'title': Unicode(mandatory=True, title=MSG(u"Title")),
'expression': Expression(mandatory=True, title=MSG(u"Expression")),
'level': ControlLevel(mandatory=True, title=MSG(u"Level")),
'variable': Variable(mandatory=True, title=MSG(u"Main Variable"))}
columns = ['number', 'title', 'expression', 'level', 'variable']
class Controls_DebugView(STLView):
access = 'is_admin'
template = '/ui/goodforms/controls_debug.xml'
def get_namespace(self, resource, context):
controls = list(resource.get_controls())
return {'controls': controls,
'errors': resource.get_errors()}
class Controls(Folder):
class_id = 'controls'
class_title = MSG(u"Controls")
class_icon_css = 'fa-cog'
class_views = ['debug']
# Fields
data = File_Field(class_handler=ControlsHandler)
extension = Char_Field
filename = Char_Field
def _load_from_csv(self):
errors = self.get_errors()
if errors:
error = errors[0]
raise FormatError(error)
def get_errors(self):
errors = []
handler = self.get_value('data')
# Consistency check
# Starting from 1
lineno = 2
for row in handler.get_rows():
# Title
title = row.get_value('title').strip()
if not title:
err = ERR_EMPTY_TITLE.gettext(line=lineno)
errors.append(err)
# Expression
expression = row.get_value('expression')
if not expression:
err = ERR_EMPTY_EXPRESSION.gettext(line=lineno)
errors.append(err)
try:
# FIXME
namespace = {}
Expression.is_valid(expression, namespace)
except Exception, err:
err = ERR_BAD_EXPRESSION.gettext(line=lineno, err=err)
errors.append(err)
# Level
level = row.get_value('level')
if not ControlLevel.is_valid(level):
err = ERR_BAD_LEVEL.gettext(line=lineno, level=level)
errors.append(err)
# Variable
variable = row.get_value('variable')
if not variable:
err = ERR_EMPTY_VARIABLE.gettext(line=lineno)
errors.append(err)
lineno += 1
# Ok
return errors
def get_controls(self):
handler = self.get_value('data')
for row in handler.get_rows():
number = row.get_value('number')
title = row.get_value('title')
expr = row.get_value('expression')
level = row.get_value('level')
variable = row.get_value('variable')
page = variable[0]
yield (number, title, expr, level, page, variable)
# Views
debug = Controls_DebugView