~bennabiy/+junk/python-xlib

« back to all changes in this revision

Viewing changes to Xlib/rdb.py

  • Committer: Package Import Robot
  • Author(s): Andrew Shadura, Ramkumar Ramachandra, Andrew Shadura
  • Date: 2015-08-13 08:14:19 UTC
  • mfrom: (6.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20150813081419-hdefinnghp2iydkx
Tags: 0.14+20091101-3
[ Ramkumar Ramachandra ]
* Remove useless debugging output (Closes: #565996)

[ Andrew Shadura ]
* Switch to 3.0 (quilt) format.
* Rename patches.
* Use debhelper 9 in its short form.
* Use pybuild.
* Bump Standards-Version.
* Don't build or install PostScript documentation and info files.
* Use system-provided texi2html instead of a shipped version
  (Closes: #795057).
* Update debian/copyright (Closes: #795057).
* Don't install Makefile or texi2html with the documentation.
* Set executable bit for examples.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
 
24
24
# Standard modules
25
 
import string
26
 
import types
27
25
import re
28
26
import sys
 
27
import functools
29
28
 
30
29
# Xlib modules
31
 
from support import lock
 
30
from Xlib.support import lock
32
31
 
33
32
# Set up a few regexpes for parsing string representation of resources
34
33
 
69
68
 
70
69
        """
71
70
 
72
 
        if type(file) is types.StringType:
 
71
        if type(file) is str:
73
72
            file = open(file, 'r')
74
73
 
75
74
        self.insert_string(file.read())
84
83
        """
85
84
 
86
85
        # First split string into lines
87
 
        lines = string.split(data, '\n')
 
86
        lines = data.split('\n')
88
87
 
89
88
        while lines:
90
89
            line = lines[0]
122
121
            for i in range(1, len(splits), 2):
123
122
                s = splits[i]
124
123
                if len(s) == 3:
125
 
                    splits[i] = chr(string.atoi(s, 8))
 
124
                    splits[i] = chr(int(s, 8))
126
125
                elif s == 'n':
127
126
                    splits[i] = '\n'
128
127
 
129
128
            # strip the last value part to get rid of any
130
129
            # unescaped blanks
131
 
            splits[-1] = string.rstrip(splits[-1])
 
130
            splits[-1] = splits[-1].rstrip()
132
131
 
133
 
            value = string.join(splits, '')
 
132
            value = ''.join(splits)
134
133
 
135
134
            self.insert(res, value)
136
135
 
172
171
        for i in range(1, len(parts), 2):
173
172
 
174
173
            # Create a new mapping/value group
175
 
            if not db.has_key(parts[i - 1]):
 
174
            if parts[i - 1] not in db:
176
175
                db[parts[i - 1]] = ({}, {})
177
176
 
178
177
            # Use second mapping if a loose binding, first otherwise
182
181
                db = db[parts[i - 1]][0]
183
182
 
184
183
        # Insert value into the derived db
185
 
        if db.has_key(parts[-1]):
 
184
        if parts[-1] in db:
186
185
            db[parts[-1]] = db[parts[-1]][:2] + (value, )
187
186
        else:
188
187
            db[parts[-1]] = ({}, {}, value)
189
188
 
190
189
        self.lock.release()
191
190
 
192
 
    def __getitem__(self, (name, cls)):
 
191
    def __getitem__(self, nc):
193
192
        """db[name, class]
194
193
 
195
194
        Return the value matching the resource identified by NAME and
196
195
        CLASS.  If no match is found, KeyError is raised.
197
196
        """
 
197
        name, cls = nc
198
198
 
199
199
        # Split name and class into their parts
200
200
 
201
 
        namep = string.split(name, '.')
202
 
        clsp = string.split(cls, '.')
 
201
        namep = name.split('.')
 
202
        clsp = cls.split('.')
203
203
 
204
204
        # It is an error for name and class to have different number
205
205
        # of parts
218
218
 
219
219
            # Precedence order: name -> class -> ?
220
220
 
221
 
            if self.db.has_key(namep[0]):
 
221
            if namep[0] in self.db:
222
222
                bin_insert(matches, _Match((NAME_MATCH, ), self.db[namep[0]]))
223
223
 
224
 
            if self.db.has_key(clsp[0]):
 
224
            if clsp[0] in self.db:
225
225
                bin_insert(matches, _Match((CLASS_MATCH, ), self.db[clsp[0]]))
226
226
 
227
 
            if self.db.has_key('?'):
 
227
            if '?' in self.db:
228
228
                bin_insert(matches, _Match((WILD_MATCH, ), self.db['?']))
229
229
 
230
230
 
240
240
 
241
241
            # Special case for resources which begins with a loose
242
242
            # binding, e.g. '*foo.bar'
243
 
            if self.db.has_key(''):
 
243
            if '' in self.db:
244
244
                bin_insert(matches, _Match((), self.db[''][1]))
245
245
 
246
246
 
376
376
        return argv
377
377
 
378
378
 
 
379
@functools.total_ordering
379
380
class _Match:
380
381
    def __init__(self, path, dbs):
381
382
        self.path = path
382
383
 
383
 
        if type(dbs) is types.TupleType:
 
384
        if type(dbs) is tuple:
384
385
            self.skip = 0
385
386
            self.group = dbs
386
387
 
388
389
            self.skip = 1
389
390
            self.db = dbs
390
391
 
391
 
    def __cmp__(self, other):
392
 
        return cmp(self.path, other.path)
 
392
    def __eq__(self, other):
 
393
        return self.path == other.path
 
394
 
 
395
    def __lt__(self, other):
 
396
        return self.path < other.path
393
397
 
394
398
    def match_length(self):
395
399
        return len(self.path)
396
400
 
397
401
    def match(self, part, score):
398
402
        if self.skip:
399
 
            if self.db.has_key(part):
 
403
            if part in self.db:
400
404
                return _Match(self.path + (score, ), self.db[part])
401
405
            else:
402
406
                return None
403
407
        else:
404
 
            if self.group[0].has_key(part):
 
408
            if part in self.group[0]:
405
409
                return _Match(self.path + (score, ), self.group[0][part])
406
 
            elif self.group[1].has_key(part):
 
410
            elif part in self.group[1]:
407
411
                return _Match(self.path + (score + 1, ), self.group[1][part])
408
412
            else:
409
413
                return None
460
464
    upper = len(list) - 1
461
465
 
462
466
    while lower <= upper:
463
 
        center = (lower + upper) / 2
 
467
        center = (lower + upper) // 2
464
468
        if element < list[center]:
465
469
            upper = center - 1
466
470
        elif element > list[center]:
482
486
    for comp, group in src.items():
483
487
 
484
488
        # DEST already contains this component, update it
485
 
        if dest.has_key(comp):
 
489
        if comp in dest:
486
490
 
487
491
            # Update tight and loose binding databases
488
492
            update_db(dest[comp][0], group[0])
536
540
                      ('\000', '\\000'),
537
541
                      ('\n', '\\n')):
538
542
 
539
 
        value = string.replace(value, char, esc)
 
543
        value = value.replace(char, esc)
540
544
 
541
545
    # If first or last character is space or tab, escape them.
542
546
    if value[0] in ' \t':