File: varietes.py

package info (click to toggle)
djvusmooth 0.2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 408 kB
  • ctags: 915
  • sloc: python: 4,595; xml: 97; makefile: 54
file content (177 lines) | stat: -rw-r--r-- 4,265 bytes parent folder | download
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
# encoding=UTF-8
# Copyright © 2008, 2009 Jakub Wilk <jwilk@jwilk.net>
#
# This package 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; version 2 dated June, 1991.
#
# This package 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.

import re
import exceptions
import warnings
import weakref

class NotOverriddenWarning(exceptions.UserWarning):
    pass

def not_overridden(f):
    r'''
    >>> warnings.filterwarnings('error', category=NotOverriddenWarning)
    >>> class B(object):
    ...   @not_overridden
    ...   def f(self, x, y): pass
    >>> class C(B):
    ...   def f(self, x, y): return x * y
    >>> B().f(6, 7)
    Traceback (most recent call last):
    ...
    NotOverriddenWarning: `lib.varietes.B.f()` is not overriden
    >>> C().f(6, 7)
    42
    '''
    def new_f(self, *args, **kwargs):
        cls = type(self)
        warnings.warn(
            '`%s.%s.%s()` is not overriden' % (cls.__module__, cls.__name__, f.__name__),
            category = NotOverriddenWarning,
            stacklevel = 2
        )

        return f(self, *args, **kwargs)
    return new_f

def wref(o):
    r'''
    Return a weak reference to object. This is almost the same as
    `weakref.ref()`, but accepts `None` too.

    >>> class O(object):
    ...   pass
    >>> x = O()
    >>> xref = wref(x)
    >>> xref() is x
    True
    >>> del x
    >>> xref() is None
    True

    >>> xref = wref(None)
    >>> xref() is None
    True
    '''
    if o is None:
        ref = weakref.ref(set())
        assert ref() is None
    else:
        ref = weakref.ref(o)
    return ref

def indents_to_tree(lines):
    r'''
    >>> lines = [
    ... 'bacon',
    ... '   egg',
    ... '       eggs',
    ... 'ham',
    ... '   sausage',
    ... '   spam',
    ... '       bacon',
    ... '   egg'
    ... ]
    >>> indents_to_tree(lines)
    [None, ['bacon', ['egg', ['eggs']]], ['ham', ['sausage'], ['spam', ['bacon']], ['egg']]]

    '''
    root = [None]
    memo = [(-1, root)]
    for line in lines:
        old_len = len(line)
        line = line.lstrip()
        current = [line]
        indent = old_len - len(line)
        while memo[-1][0] >= indent:
            memo.pop()
        memo[-1][1].append(current)
        memo += (indent, current),
    return root

URI_SPECIAL_CHARACTERS = \
(
    ':/?#[]@' +    # RFC 3986, `gen-delims`
    '!$&()*+,;=' + # RFC 3986, `sub-delims`
    '%'            # RFC 3986, `pct-encoded`
)

def fix_uri(s):
    r'''
    >>> uri = 'http://eggs.spam/'
    >>> fix_uri(uri) == uri
    True
    >>> uri = fix_uri('http://eggs.spam/eggs and spam/')
    >>> uri
    'http://eggs.spam/eggs%20and%20spam/'
    >>> fix_uri(uri) == uri
    True
    '''
    from urllib import quote
    if isinstance(s, unicode):
        s = s.encode('UTF-8')
    return quote(s, safe=URI_SPECIAL_CHARACTERS)

replace_control_characters = re.compile('[\0-\x1f]+').sub

_is_html_color = re.compile('^[#][0-9a-fA-F]{6}$').match

def is_html_color(s):
    '''
    >>> is_html_color('#000000')
    True
    >>> is_html_color('#ffffff')
    True
    >>> is_html_color('#FFFFFF')
    True
    >>> is_html_color('#c0c0f0')
    True
    >>> is_html_color('')
    False
    >>> is_html_color('#bcdefg')
    False
    >>> is_html_color('#ffffff ')
    False
    >>> is_html_color(' #ffffff')
    False
    '''
    return bool(_is_html_color(s))

class idict(object):

    '''
    >>> o = idict(eggs = 'spam', ham = 'eggs')
    >>> o
    lib.varietes.idict(eggs='spam', ham='eggs')
    >>> o.eggs
    'spam'
    >>> o.ham
    'eggs'
    >>> o.spam
    Traceback (most recent call last):
    ...
    AttributeError: 'idict' object has no attribute 'spam'
    '''

    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)
    
    def __repr__(self):
        return '%s.%s(%s)' % \
        (
            self.__module__,
            self.__class__.__name__,
            ', '.join('%s=%r' % (k, v) for k, v in self.__dict__.iteritems())
        )

# vim:ts=4 sw=4 et