File: TinyTable.py

package info (click to toggle)
zope-tinytable 0b2-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 124 kB
  • ctags: 52
  • sloc: python: 375; sh: 54; makefile: 45
file content (473 lines) | stat: -rw-r--r-- 16,283 bytes parent folder | download | duplicates (2)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#
#	$Endicor: TinyTable.py,v 1.28 1999/04/26 22:06:55 tsarna Exp $
#
# Copyright (c) 1998-1999 Endicor Technologies, Inc.
# All rights reserved. Written by Ty Sarna <tsarna@endicor.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
__doc__='''Tiny Table data manager product

$Endicor: TinyTable.py,v 1.28 1999/04/26 22:06:55 tsarna Exp $'''

__version__='$Revision: 1.28 $'[11:-2]

######################### Imported Modules #########################

from Globals import HTMLFile, MessageDialog, Persistent
from Shared.DC.ZRDB.Results import Results
from PersistentMapping import PersistentMapping
from DateTime import DateTime
from App.Extensions import getBrain
import OFS.ObjectManager, OFS.SimpleItem, Acquisition, AccessControl.Role
import Record, Missing, string, types
import ImportExport

######################### Folder Methods #########################

addItemForm=HTMLFile('Add', globals())
def addItem(self, id, title='', columns='', REQUEST=None):
    """Add a TinyTable to a folder

    The argument 'self' will be bound to a folder.

    The arguments, 'id' and 'title' are minimal, and most applications
    will have additional needed arguments.
    """
    self._setObject(id, TinyTable(id, title, columns))
    if REQUEST is not None: return self.manage_main(self,REQUEST)

######################### Helper Functions #########################

IntValued = 'i'
LongValued = 'l'
FloatValued = 'n'
StringValued = 's'
DateTimeValued = 'd'
DateValued = 'D'

TypeNames = {
    IntValued : ':int',
    LongValued : ':long',
    FloatValued : ':float',
    StringValued : '',
    DateTimeValued : ':datetime',
    DateValued : ':date'
}

def TypeCode(t):
    if t == LongValued:
        return 'i'
    elif t == DateValued:
        return 'd'
    else:
        return t

def CoerceType(x, t):
    if x is Missing.Value:
        return x
    elif t == IntValued:
        if type(x) == type(0.0):
            return int(x)
        if type(x) != type(0):
            if (type(x) != type('')):
                x = str(x)

            try:
                x = string.atoi(x)
            except:
                x = 0
        return x
    elif t == LongValued:
        if type(x) != type(0L):
            if (type(x) != type('')):
                x = str(x)

            try:
                x = string.atol(x)
            except:
                x = 0L
        return x
    elif t == FloatValued:
        if type(x) != type(0.0):
            if (type(x) != type('')):
                x = str(x)

            try:
                x = string.atof(x)
            except:
                x = 0.0
        return x
    elif t == DateTimeValued:
        if not (type(x) == types.InstanceType and x.__class__ == DateTime):
            try:
                x = DateTime(x)
            except:
                x = Missing.Value
        return x
    elif t == DateValued:
        if not (type(x) == types.InstanceType and x.__class__ == DateTime):
            try:
                # force date-only
                x = DateTime(x)
            except:
                x = Missing.Value
        return DateTime(x.Date())
    else:
        return str(x)

######################### TinyTable Class #########################

class TinyTable(
    OFS.SimpleItem.Item,
    Persistent,
    Acquisition.Implicit,
    AccessControl.Role.RoleManager,
    ):
    """TinyTable is a product designed to manage a small amount of
tabular data.  It's intended to fill the gap between a Z Table or an Z
SQL Methods accessed SQL table, which are overkill for many tasks, and
folder token properties, which allow only a single "column".  TinyTable
also makes it possible to look up an item within the list, or to return
a subset of the list rows where columns equal particular values.

TinyTable Properties

  Columns

    *Columns* is a list of one or more column names separated by
    spaces. Columns are string-typed by default, but may optionally
    be integers, long integers, floating-point, or DateTime if the
    column name is suffixed with ':int', ':long', ':float', or ':date'
    or ':datetime' respectively.  ':date' and ':datetime' both store
    Zope DateTime values, but ':date' values are forced to be date-only,
    with no time-of-day information.

    The first column is special. An index will be built on this
    column for "lookup" use (see below). The index built is unique.
    That is, if there are multiple rows with the same first-column
    value, only one row will appear in the index, and only one row
    will be returned from an index query. If this is a problem, use
    a filter on the first row instead (see below).

  Data

    The data consists of newline-separated rows containing columns
    separated by commas.  Any input data will be adjusted to conform to
    the column specification.  If the row contains too many columns the
    excess will be trimmed.  If the row contains to few columns, columns
    containing NULL will be added.  String values in a column specified
    to take a number will be replaced by 0.
    
    The form of values is similar to Python syntax. Strings are enclosed
    in single or double quotes, and backslash escapes are possible.
    Numbers may be entered just as in Python.  Full Python syntax for
    floating point numbers is supported, including exponent notation. 
    Dates and Date-Times are represented by strings in any of the
    formats thet the Zope DateTime class understands.  Missing (NULL)
    may also be given as a value for a cell, by using 'NULL' or 'None',
    or by simply omitting the value (for example, 1,,3' is treated as
    '1,NULL,3')

    Python comments ('#') and line continuations may also be used.
    Note, however, that once TinyTables extracts the data from the input
    text, the text is thrown away.  When visiting the management edit
    interface again, the text will be regenerated from the stored data.
    Comments, blank lines, line continuations, and such will all be lost
    since they don't alter the data itself.
    
  Querying a TinyTable

    Assume you have a table named MyTable. It has these properties:

    Columns::

      last first middle n:int x:long

    and the following data::

      "smith", "john", "x", 0, 0L
      "smith", "bob", "x", 0, 0L
      "smith", "bob", "z", 0, 0L
      "jones", "bob", "y", 0, 0L
      "jones", "john", "y", 0, 0L
      "jones", "john", "z", 0, 0L 

    The data can be queried from DTML in several ways:

      Full Query::

        <!--#in MyTable-->

        Iterates through all rows of the TinyTable. Within the
        region contained by 'in' tag, the column names will be
        available as variables and so can be insterted. For
        example on the first iteration, '<!--#var first-->' will
        be replaced with 'john'.

      Index Query::

        <!--#in "MyTable('jones')"-->

        The passed argument will be looked up in the table's
        index of the first column. Because the index is unique,
        either zero (if no matching rows) or one (if any
        matching rows) rows will be iterated through. In this
        case, any *one* of the three rows with a last name of
        'jones' could be returned. The choice of which row is
        returned when multiple rows have the same index value is
        unspecified.

      Filter Query::

        <!--#in "MyTable(last='jones')"-->
        <!--#in "MyTable(first='john')"-->
        <!--#in "MyTable(last='jones', middle='y')"-->

        When one or more named arguments is given, a filter
        query is performed. Each argument name must be the name
        of a column, and the corresponding value is compared
        against that column in each row. Only matching rows are
        returned. The first example above, in contrast with the
        index query example, returns *all three* rows where the
        last name is 'jones'.

        While an Index Query operates only on the first column,
        a filter query can operate on any column. In the second
        exmple above, all three rows with the first name 'john'
        are returned.

        Finally, multiple filters may be specified. In this
        case only rows matching all contraints are iterated
        through. In the third example above, only the two rows
        where the last name is 'jones' and the middle initial is
        'y' will be returned.
        """

    # Specify a name for the item type:
    meta_type = 'TinyTable'

    # Specify a relative URL for the icon used to display icons:
    icon = 'misc_/TinyTable/icon'

    # Specify definitions for tabs:
    manage_options=(
	{"label":"Properties",  "action":"manage_main"},
	{"label":"Advanced",    "action":"manage_advancedForm"},
        {"label":"View",        "action":"manage_view"},
	{"label":"Security",    "action":"manage_access"},
	{"label":"About",       "action":"manage_about"},
	)

    # Specify how individual operations add up to "permissions":
    __ac_permissions__=(
	('View management screens', ('manage_tabs','manage_main',
	                             'manage_about','manage_advancedForm')),
	('Change permissions',      ('manage_access',)           ),
	('Change TinyTable',        ('manage_edit','manage_editData',
	                             'manage_advanced')),
	('Query TinyTable Data',    ('','index_html','manage_view')),
	)

    def __init__(self, id, title='', columns=''):
	self.id = id
        self._dataver = 1
	self._SetState(title, columns)

        self._rows = []
        self._index = {}
        self._n_rows = 0

        self.class_name_ = self.class_file_ = ""
        self._v_brain = None

    # Provide a "View" interface:
    manage_view = HTMLFile("View", globals())

    # Provide a "About..." interface:
    manage_about = HTMLFile("About", globals())

    # Provide interface for changing properties:
    manage_main=HTMLFile('Edit', globals())
    def manage_edit(self, title, columns, REQUEST=None):
	"""Change item properties

	Note that we return people to our own interface, not to
	the folder we were in before.
	"""
        self._SetState(title, columns)

        # make existing data conform to new column specification
        self._rows = map(self._FixRow, self._rows)

        # and regenerte the index, incase the above changed any data
        self._GenerateIndex()

        return self.manage_editedDialog(REQUEST)

    def _SetState(self, title, columns):
	self.title = title

        if self._dataver < 1:
            del self.__dict__['delim_char_']

        self._DigestColumns(columns)
        self._dataver = 1

    manage_advancedForm = HTMLFile("Advanced", globals())
    def manage_advanced(self, class_name, class_file, REQUEST=None):
        """Change Advanced Settings"""
        self.class_name_, self.class_file_ = class_name, class_file
        self._v_brain = getBrain(self.class_file_, self.class_name_, 1)
        return self.manage_editedDialog(REQUEST)

    def manage_editData(self, data, REQUEST=None):
	"""Change item data"""
        self._rows = map(self._FixRow, ImportExport.ImportData(data))
        self._n_rows = len(self._rows)

        self._GenerateIndex()
        
        return self.manage_editedDialog(REQUEST)

    def _DigestColumns(self, column_list):
        self._col_index = {}
        self._col_names = []
        self._types = []
        self._items = []
        cols = string.split(column_list)

        for col in cols:
            item = {}
            x = string.split(col, ':')
            self._col_names.append(x[0])
            item['name'] = x[0]
            t = StringValued
            if len(x) > 1:
                if x[1] == 'int':
                    t = IntValued
                elif x[1] == 'long':
                    t = LongValued
                elif x[1] == 'float':
                    t = FloatValued
                elif x[1] == 'datetime':
                    t = DateTimeValued
                elif x[1] == 'date':
                    t = DateValued
            self._types.append(t)
            item['type'] = TypeCode(t)
            self._items.append(item)

        self.n_cols = len(self._col_names)
        self.index_column = self._col_names[0]

        col_num = 0
        for col in self._col_names:
            self._col_index[col] = col_num
            col_num = col_num + 1

    def _FixRow(self, row):
        # force row to match specified number of columns
        if len(row) > self.n_cols:
            row = row[:self.n_cols]
        elif len(row) < self.n_cols:
            row = row + (self.n_cols - len(row)) * [Missing.Value]

        # Ensure correct types
        newrow = []
        for i in range(0, self.n_cols):
            newrow.append(CoerceType(row[i], self._types[i]))

        return newrow

    def _GenerateIndex(self):
        self._index = {}

        for i in range(0, self._n_rows):
            self._index[self._rows[i][0]] = i

    def cols_text(self):
        l = []

        for i in range(0, self.n_cols):
            l.append(self._col_names[i] + TypeNames[self._types[i]])

        return string.join(l, ' ')
        
    def data_text(self):
        return ImportExport.ExportData(self._rows)

    def index_html(self):
        """Returns an HTML representation of the TinyTable's data"""

        s = "<table border=1><tr><th>"
        s = s + string.join(self._col_names, "</th>\n<th>") + "</th></tr>\n"
        for row in self._rows:
            s = s + "<tr><td>" + \
                string.join(map(str, row), "</td>\n<td>") + "</td></tr>\n"
        return s + "</table>"

    def _results(self, rows):
        if hasattr(self, '_v_brain'):
            brain = self._v_brain
        else:
            brain = self._v_brain = getBrain(self.class_file_, self.class_name_)

        return Results((self._items, rows), brains=brain, parent=None)

    def __call__(self, *args, **kargs):
        if len(args) == 1:
            if self._index.has_key(args[0]):
                return self._results([self._rows[self._index[args[0]]]])
            else:
                return None
        elif len(kargs):
            rf = RowFilter(self, kargs)
            l = []

            for i in range(0, self._n_rows):
                if rf(self._rows[i]):
                    l.append(self._rows[i])
            return self._results(l)
        else:
            return self._results(self._rows)

######################### Helper Classes #########################

class RowFilter:
    def __init__(self, table, rules):
        self.cols = []
        self.vals = []
        self.n_rules = 0

        for col in rules.keys():
            if table._col_index.has_key(col):
                self.cols.append(table._col_index[col])
                self.vals.append(rules[col])
                self.n_rules = self.n_rules + 1

    def __call__(self, row):
        for i in range(0, self.n_rules):
            if row[self.cols[i]] != self.vals[i]:
                return 0
        return 1