~bennabiy/+junk/python-xlib

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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# Xlib.rdb -- X resource database implementation
#
#    Copyright (C) 2000 Peter Liljenberg <petli@ctrl-c.liu.se>
#
#    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


# See end of file for an explanation of the algorithm and
# data structures used.


# Standard modules
import string
import types
import re
import sys

# Xlib modules
from support import lock

# Set up a few regexpes for parsing string representation of resources

comment_re = re.compile(r'^\s*!')
resource_spec_re = re.compile(r'^\s*([-_a-zA-Z0-9?.*]+)\s*:\s*(.*)$')
value_escape_re = re.compile('\\\\([ \tn\\\\]|[0-7]{3,3})')
resource_parts_re = re.compile(r'([.*]+)')

# Constants used for determining which match is best

NAME_MATCH = 0
CLASS_MATCH = 2
WILD_MATCH = 4
MATCH_SKIP = 6

# Option error class
class OptionError(Exception):
    pass


class ResourceDB:
    def __init__(self, file = None, string = None, resources = None):
        self.db = {}
        self.lock = lock.allocate_lock()

        if file is not None:
            self.insert_file(file)
        if string is not None:
            self.insert_string(string)
        if resources is not None:
            self.insert_resources(resources)

    def insert_file(self, file):
        """insert_file(file)

        Load resources entries from FILE, and insert them into the
        database.  FILE can be a filename (a string)or a file object.

        """

        if type(file) is types.StringType:
            file = open(file, 'r')

        self.insert_string(file.read())


    def insert_string(self, data):
        """insert_string(data)

        Insert the resources entries in the string DATA into the
        database.

        """

        # First split string into lines
        lines = string.split(data, '\n')

        while lines:
            line = lines[0]
            del lines[0]

            # Skip empty line
            if not line:
                continue

            # Skip comments
            if comment_re.match(line):
                continue

            # Handle continued lines
            while line[-1] == '\\':
                if lines:
                    line = line[:-1] + lines[0]
                    del lines[0]
                else:
                    line = line[:-1]
                    break

            # Split line into resource and value
            m = resource_spec_re.match(line)

            # Bad line, just ignore it silently
            if not m:
                continue

            res, value = m.group(1, 2)

            # Convert all escape sequences in value
            splits = value_escape_re.split(value)

            for i in range(1, len(splits), 2):
                s = splits[i]
                if len(s) == 3:
                    splits[i] = chr(string.atoi(s, 8))
                elif s == 'n':
                    splits[i] = '\n'

            # strip the last value part to get rid of any
            # unescaped blanks
            splits[-1] = string.rstrip(splits[-1])

            value = string.join(splits, '')

            self.insert(res, value)


    def insert_resources(self, resources):
        """insert_resources(resources)

        Insert all resources entries in the list RESOURCES into the
        database.  Each element in RESOURCES should be a tuple:

          (resource, value)

        Where RESOURCE is a string and VALUE can be any Python value.

        """

        for res, value in resources:
            self.insert(res, value)

    def insert(self, resource, value):
        """insert(resource, value)

        Insert a resource entry into the database.  RESOURCE is a
        string and VALUE can be any Python value.

        """

        # Split res into components and bindings
        parts = resource_parts_re.split(resource)

        # If the last part is empty, this is an invalid resource
        # which we simply ignore
        if parts[-1] == '':
            return

        self.lock.acquire()

        db = self.db
        for i in range(1, len(parts), 2):

            # Create a new mapping/value group
            if not db.has_key(parts[i - 1]):
                db[parts[i - 1]] = ({}, {})

            # Use second mapping if a loose binding, first otherwise
            if '*' in parts[i]:
                db = db[parts[i - 1]][1]
            else:
                db = db[parts[i - 1]][0]

        # Insert value into the derived db
        if db.has_key(parts[-1]):
            db[parts[-1]] = db[parts[-1]][:2] + (value, )
        else:
            db[parts[-1]] = ({}, {}, value)

        self.lock.release()

    def __getitem__(self, (name, cls)):
        """db[name, class]

        Return the value matching the resource identified by NAME and
        CLASS.  If no match is found, KeyError is raised.
        """

        # Split name and class into their parts

        namep = string.split(name, '.')
        clsp = string.split(cls, '.')

        # It is an error for name and class to have different number
        # of parts

        if len(namep) != len(clsp):
            raise ValueError('Different number of parts in resource name/class: %s/%s' % (name, cls))

        complen = len(namep)
        matches = []

        # Lock database and wrap the lookup code in a try-finally
        # block to make sure that it is unlocked.

        self.lock.acquire()
        try:

            # Precedence order: name -> class -> ?

            if self.db.has_key(namep[0]):
                bin_insert(matches, _Match((NAME_MATCH, ), self.db[namep[0]]))

            if self.db.has_key(clsp[0]):
                bin_insert(matches, _Match((CLASS_MATCH, ), self.db[clsp[0]]))

            if self.db.has_key('?'):
                bin_insert(matches, _Match((WILD_MATCH, ), self.db['?']))


            # Special case for the unlikely event that the resource
            # only has one component
            if complen == 1 and matches:
                x = matches[0]
                if x.final(complen):
                    return x.value()
                else:
                    raise KeyError((name, cls))


            # Special case for resources which begins with a loose
            # binding, e.g. '*foo.bar'
            if self.db.has_key(''):
                bin_insert(matches, _Match((), self.db[''][1]))


            # Now iterate over all components until we find the best match.

            # For each component, we choose the best partial match among
            # the mappings by applying these rules in order:

            # Rule 1: If the current group contains a match for the
            # name, class or '?', we drop all previously found loose
            # binding mappings.

            # Rule 2: A matching name has precedence over a matching
            # class, which in turn has precedence over '?'.

            # Rule 3: Tight bindings have precedence over loose
            # bindings.

            while matches:

                # Work on the first element == the best current match

                x = matches[0]
                del matches[0]

                # print 'path:  ', x.path
                # if x.skip:
                #         print 'skip:  ', x.db
                # else:
                #         print 'group: ', x.group
                # print

                i = x.match_length()

                for part, score in ((namep[i], NAME_MATCH),
                                    (clsp[i], CLASS_MATCH),
                                    ('?', WILD_MATCH)):

                    # Attempt to find a match in x
                    match = x.match(part, score)
                    if match:
                        # Hey, we actually found a value!
                        if match.final(complen):
                            return match.value()

                        # Else just insert the new match
                        else:
                            bin_insert(matches, match)

                    # Generate a new loose match
                    match = x.skip_match(complen)
                    if match:
                        bin_insert(matches, match)

            # Oh well, nothing matched
            raise KeyError((name, cls))

        finally:
            self.lock.release()

    def get(self, res, cls, default = None):
        """get(name, class [, default])

        Return the value matching the resource identified by NAME and
        CLASS.  If no match is found, DEFAULT is returned, or None if
        DEFAULT isn't specified.

        """

        try:
            return self[(res, cls)]
        except KeyError:
            return default

    def update(self, db):
        """update(db)

        Update this database with all resources entries in the resource
        database DB.

        """

        self.lock.acquire()
        update_db(self.db, db.db)
        self.lock.release()

    def output(self):
        """output()

        Return the resource database in text representation.
        """

        self.lock.acquire()
        text = output_db('', self.db)
        self.lock.release()
        return text

    def getopt(self, name, argv, opts):
        """getopt(name, argv, opts)

        Parse X command line options, inserting the recognised options
        into the resource database.

        NAME is the application name, and will be prepended to all
        specifiers.  ARGV is the list of command line arguments,
        typically sys.argv[1:].

        OPTS is a mapping of options to resource specifiers.  The key is
        the option flag (with leading -), and the value is an instance of
        some Option subclass:

        NoArg(specifier, value): set resource to value.
        IsArg(specifier):        set resource to option itself
        SepArg(specifier):       value is next argument
        ResArg:                  resource and value in next argument
        SkipArg:                 ignore this option and next argument
        SkipLine:                ignore rest of arguments
        SkipNArgs(count):        ignore this option and count arguments

        The remaining, non-option, oparguments is returned.

        rdb.OptionError is raised if there is an error in the argument list.
        """

        while argv and argv[0] and argv[0][0] == '-':
            try:
                argv = opts[argv[0]].parse(name, self, argv)
            except KeyError:
                raise OptionError('unknown option: %s' % argv[0])
            except IndexError:
                raise OptionError('missing argument to option: %s' % argv[0])

        return argv


class _Match:
    def __init__(self, path, dbs):
        self.path = path

        if type(dbs) is types.TupleType:
            self.skip = 0
            self.group = dbs

        else:
            self.skip = 1
            self.db = dbs

    def __cmp__(self, other):
        return cmp(self.path, other.path)

    def match_length(self):
        return len(self.path)

    def match(self, part, score):
        if self.skip:
            if self.db.has_key(part):
                return _Match(self.path + (score, ), self.db[part])
            else:
                return None
        else:
            if self.group[0].has_key(part):
                return _Match(self.path + (score, ), self.group[0][part])
            elif self.group[1].has_key(part):
                return _Match(self.path + (score + 1, ), self.group[1][part])
            else:
                return None

    def skip_match(self, complen):
        # Can't make another skip if we have run out of components
        if len(self.path) + 1 >= complen:
            return None

        # If this already is a skip match, clone a new one
        if self.skip:
            if self.db:
                return _Match(self.path + (MATCH_SKIP, ), self.db)
            else:
                return None

        # Only generate a skip match if the loose binding mapping
        # is non-empty
        elif self.group[1]:
            return _Match(self.path + (MATCH_SKIP, ), self.group[1])

        # This is a dead end match
        else:
            return None

    def final(self, complen):
        if not self.skip and len(self.path) == complen and len(self.group) > 2:
            return 1
        else:
            return 0

    def value(self):
        return self.group[2]


#
# Helper function for ResourceDB.__getitem__()
#

def bin_insert(list, element):
    """bin_insert(list, element)

    Insert ELEMENT into LIST.  LIST must be sorted, and ELEMENT will
    be inserted to that LIST remains sorted.  If LIST already contains
    ELEMENT, it will not be duplicated.

    """

    if not list:
        list.append(element)
        return

    lower = 0
    upper = len(list) - 1

    while lower <= upper:
        center = (lower + upper) / 2
        if element < list[center]:
            upper = center - 1
        elif element > list[center]:
            lower = center + 1
        elif element == list[center]:
            return

    if element < list[upper]:
        list.insert(upper, element)
    elif element > list[upper]:
        list.insert(upper + 1, element)


#
# Helper functions for ResourceDB.update()
#

def update_db(dest, src):
    for comp, group in src.items():

        # DEST already contains this component, update it
        if dest.has_key(comp):

            # Update tight and loose binding databases
            update_db(dest[comp][0], group[0])
            update_db(dest[comp][1], group[1])

            # If a value has been set in SRC, update
            # value in DEST

            if len(group) > 2:
                dest[comp] = dest[comp][:2] + group[2:]

        # COMP not in src, make a deep copy
        else:
            dest[comp] = copy_group(group)

def copy_group(group):
    return (copy_db(group[0]), copy_db(group[1])) + group[2:]

def copy_db(db):
    newdb = {}
    for comp, group in db.items():
        newdb[comp] = copy_group(group)

    return newdb


#
# Helper functions for output
#

def output_db(prefix, db):
    res = ''
    for comp, group in db.items():

        # There's a value for this component
        if len(group) > 2:
            res = res + '%s%s: %s\n' % (prefix, comp, output_escape(group[2]))

        # Output tight and loose bindings
        res = res + output_db(prefix + comp + '.', group[0])
        res = res + output_db(prefix + comp + '*', group[1])

    return res

def output_escape(value):
    value = str(value)
    if not value:
        return value

    for char, esc in (('\\', '\\\\'),
                      ('\000', '\\000'),
                      ('\n', '\\n')):

        value = string.replace(value, char, esc)

    # If first or last character is space or tab, escape them.
    if value[0] in ' \t':
        value = '\\' + value
    if value[-1] in ' \t' and value[-2:-1] != '\\':
        value = value[:-1] + '\\' + value[-1]

    return value


#
# Option type definitions
#

class Option:
    def __init__(self):
        pass

    def parse(self, name, db, args):
        pass

class NoArg(Option):
    """Value is provided to constructor."""
    def __init__(self, specifier, value):
        self.specifier = specifier
        self.value = value

    def parse(self, name, db, args):
        db.insert(name + self.specifier, self.value)
        return args[1:]

class IsArg(Option):
    """Value is the option string itself."""
    def __init__(self, specifier):
        self.specifier = specifier

    def parse(self, name, db, args):
        db.insert(name + self.specifier, args[0])
        return args[1:]

class SepArg(Option):
    """Value is the next argument."""
    def __init__(self, specifier):
        self.specifier = specifier

    def parse(self, name, db, args):
        db.insert(name + self.specifier, args[1])
        return args[2:]

class ResArgClass(Option):
    """Resource and value in the next argument."""
    def parse(self, name, db, args):
        db.insert_string(args[1])
        return args[2:]

ResArg = ResArgClass()

class SkipArgClass(Option):
    """Ignore this option and next argument."""
    def parse(self, name, db, args):
        return args[2:]

SkipArg = SkipArgClass()

class SkipLineClass(Option):
    """Ignore rest of the arguments."""
    def parse(self, name, db, args):
        return []

SkipLine = SkipLineClass()

class SkipNArgs(Option):
    """Ignore this option and the next COUNT arguments."""
    def __init__(self, count):
        self.count = count

    def parse(self, name, db, args):
        return args[1 + self.count:]



def get_display_opts(options, argv = sys.argv):
    """display, name, db, args = get_display_opts(options, [argv])

    Parse X OPTIONS from ARGV (or sys.argv if not provided).

    Connect to the display specified by a *.display resource if one is
    set, or to the default X display otherwise.  Extract the
    RESOURCE_MANAGER property and insert all resources from ARGV.

    The four return values are:
      DISPLAY -- the display object
      NAME    -- the application name (the filname of ARGV[0])
      DB      -- the created resource database
      ARGS    -- any remaining arguments
    """

    from Xlib import display, Xatom
    import os

    name = os.path.splitext(os.path.basename(argv[0]))[0]

    optdb = ResourceDB()
    leftargv = optdb.getopt(name, argv[1:], options)

    dname = optdb.get(name + '.display', name + '.Display', None)
    d = display.Display(dname)

    rdbstring = d.screen(0).root.get_full_property(Xatom.RESOURCE_MANAGER,
                                                   Xatom.STRING)
    if rdbstring:
        data = rdbstring.value
    else:
        data = None

    db = ResourceDB(string = data)
    db.update(optdb)

    return d, name, db, leftargv


# Common X options
stdopts = {'-bg': SepArg('*background'),
           '-background': SepArg('*background'),
           '-fg': SepArg('*foreground'),
           '-foreground': SepArg('*foreground'),
           '-fn': SepArg('*font'),
           '-font': SepArg('*font'),
           '-name': SepArg('.name'),
           '-title': SepArg('.title'),
           '-synchronous': NoArg('*synchronous', 'on'),
           '-xrm': ResArg,
           '-display': SepArg('.display'),
           '-d': SepArg('.display'),
           }


# Notes on the implementation:

# Resource names are split into their components, and each component
# is stored in a mapping.  The value for a component is a tuple of two
# or three elements:

#   (tightmapping, loosemapping [, value])

# tightmapping contains the next components which are connected with a
# tight binding (.).  loosemapping contains the ones connected with
# loose binding (*).  If value is present, then this component is the
# last component for some resource which that value.

# The top level components are stored in the mapping r.db, where r is
# the resource object.

# Example:  Inserting "foo.bar*gazonk: yep" into an otherwise empty
# resource database would give the folliwing structure:

# { 'foo': ( { 'bar': ( { },
#                       { 'gazonk': ( { },
#                                     { },
#                                     'yep')
#                         }
#                       )
#              },
#            {})
#   }