Passed
Push — master ( a28e2d...565a73 )
by Konstantin
02:23 queued 12s
created

ocrd_page_user_methods.main()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 0
1
#!/usr/bin/env python
2
3
# source: https://bitbucket.org/dkuhlman/generateds/src/default/gends_user_methods.py
4
5
import re
6
7
#
8
# You must include the following class definition at the top of
9
#   your method specification file.
10
#
11
class MethodSpec():
12
    def __init__(self, name='', source='', class_names='',
13
            class_names_compiled=None):
14
        """MethodSpec -- A specification of a method.
15
        Member variables:
16
            name -- The method name
17
            source -- The source code for the method.  Must be
18
                indented to fit in a class definition.
19
            class_names -- A regular expression that must match the
20
                class names in which the method is to be inserted.
21
            class_names_compiled -- The compiled class names.
22
                generateDS.py will do this compile for you.
23
        """
24
        self.name = name
25
        self.source = source
26
        if class_names is None:
27
            self.class_names = ('.*', )
28
        else:
29
            self.class_names = class_names
30
        if class_names_compiled is None:
31
            self.class_names_compiled = re.compile(self.class_names)
32
        else:
33
            self.class_names_compiled = class_names_compiled
34
    def get_name(self):
35
        return self.name
36
    def set_name(self, name):
37
        self.name = name
38
    def get_source(self):
39
        return self.source
40
    def set_source(self, source):
41
        self.source = source
42
    def get_class_names(self):
43
        return self.class_names
44
    def set_class_names(self, class_names):
45
        self.class_names = class_names
46
        self.class_names_compiled = re.compile(class_names)
47
    def get_class_names_compiled(self):
48
        return self.class_names_compiled
49
    def set_class_names_compiled(self, class_names_compiled):
50
        self.class_names_compiled = class_names_compiled
51
    def match_name(self, class_name):
52
        """Match against the name of the class currently being generated.
53
        If this method returns True, the method will be inserted in
54
          the generated class.
55
        """
56
        if self.class_names_compiled.search(class_name):
57
            return True
58
        return False
59
    def get_interpolated_source(self, values_dict):
60
        """Get the method source code, interpolating values from values_dict
61
        into it.  The source returned by this method is inserted into
62
        the generated class.
63
        """
64
        source = self.source % values_dict
65
        return source
66
    def show(self):
67
        print('specification:')
68
        print('    name: %s' % (self.name, ))
69
        print(self.source)
70
        print('    class_names: %s' % (self.class_names, ))
71
        print('    names pat  : %s' % (self.class_names_compiled.pattern, ))
72
73
74
#
75
# Provide one or more method specification such as the following.
76
# Notes:
77
# - Each generated class contains a class variable _member_data_items.
78
#   This variable contains a list of instances of class _MemberSpec.
79
#   See the definition of class _MemberSpec near the top of the
80
#   generated superclass file and also section "User Methods" in
81
#   the documentation, as well as the examples below.
82
83
#
84
# Replace the following method specifications with your own.
85
86
#
87
# Hash by memory adress/id()
88
#
89
hash_by_id = MethodSpec(name='hash',
90
    source='''\
91
    def __hash__(self):
92
        return hash(self.id)
93
''',
94
    class_names=r'^.*$',
95
    )
96
#
97
# Provide a list of your method specifications.
98
#   This list of specifications must be named METHOD_SPECS.
99
#
100
METHOD_SPECS = (
101
    hash_by_id,
102
    )
103
104
105
def test():
106
    for spec in METHOD_SPECS:
107
        spec.show()
108
109
def main():
110
    test()
111
112
113
if __name__ == '__main__':
114
    main()
115