File: style.py

package info (click to toggle)
genometools 1.5.10%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 53,852 kB
  • sloc: ansic: 355,882; ruby: 30,295; python: 4,880; sh: 3,190; makefile: 1,197; perl: 219; pascal: 159; haskell: 37; sed: 5
file content (219 lines) | stat: -rw-r--r-- 8,126 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2014 Daniel Standage <daniel.standage@gmail.com>
# Copyright (c) 2008-2010 Sascha Steinbiss <steinbiss@zbh.uni-hamburg.de>
# Copyright (c) 2008-2010 Center for Bioinformatics, University of Hamburg
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

from gt.dlload import gtlib, CollectFunc
from gt.annotationsketch.color import Color
from gt.core.error import Error, gterror
from gt.core.gtstr import Str
from gt.core.str_array import StrArray
from gt.extended.genome_node import GenomeNode

STYLE_OK = 0
STYLE_NOT_SET = 1
STYLE_ERROR = 2


class Style:

    def __init__(self, ptr=None):
        if ptr:
            self.style = ptr
            self.own = False
        else:
            e = Error()
            self.style = gtlib.gt_style_new(e._as_parameter_)
            if self.style == 0 or self.style == None:
                gterror(e)
            self.own = True
        self._as_parameter_ = self.style

    def __del__(self):
        if self.own:
            try:
                gtlib.gt_style_delete(self.style)
            except AttributeError:
                pass

    def from_param(cls, obj):
        if not isinstance(obj, Style):
            raise TypeError("argument must be a Style")
        return obj._as_parameter_

    from_param = classmethod(from_param)

    def load_file(self, filename):
        err = Error()
        rval = gtlib.gt_style_load_file(self.style,
                                        str(filename).encode('UTF-8'), err)
        if rval != 0:
            gterror(err)

    def load_str(self, string):
        err = Error()
        strg = Str(str(string).encode("utf-8"))
        rval = gtlib.gt_style_load_str(self.style, strg, err)
        if rval != 0:
            gterror(err)

    def to_str(self):
        err = Error()
        string = Str()
        if gtlib.gt_style_to_str(self.style, string._as_parameter_,
                                 err._as_parameter_) == 0:
            return str(string)
        else:
            gterror(err)

    def clone(self):
        sty = Style()
        string = self.to_str()
        print(string)
        sty.load_str(str(string))
        return sty

    def get_color(self, section, key, gn=None):
        from ctypes import byref
        color = Color()
        err = Error()
        gnp = None
        if gn:
            gnp = gn._as_parameter_
        rval = gtlib.gt_style_get_color(self.style, section, key, byref(color),
                                        gnp, err._as_parameter_)
        if rval == STYLE_OK:
            return color
        elif rval == STYLE_NOT_SET:
            return None
        elif rval == STYLE_ERROR:
            gterror(err)

    def set_color(self, section, key, color):
        from ctypes import byref
        gtlib.gt_style_set_color(self.style, section, key, byref(color))

    def get_cstr(self, section, key, gn=None):
        string = Str()
        err = Error()
        gnp = None
        if gn:
            gnp = gn._as_parameter_
        rval = gtlib.gt_style_get_str(self.style, section, key,
                                      string._as_parameter_, gnp, err._as_parameter_)
        if rval == STYLE_OK:
            return str(string)
        elif rval == STYLE_NOT_SET:
            return None
        elif rval == STYLE_ERROR:
            gterror(err)

    def set_cstr(self, section, key, value):
        string = Str(str(value.encode("utf-8")))
        gtlib.gt_style_set_str(self.style, section, key, string._as_parameter_)

    def get_num(self, section, key, gn=None):
        from ctypes import c_double, byref
        double = c_double()
        err = Error()
        gnp = None
        if gn:
            gnp = gn._as_parameter_
        rval = gtlib.gt_style_get_num(self.style, section, key, byref(double),
                                      gnp, err._as_parameter_)
        if rval == STYLE_OK:
            return double.value
        elif rval == STYLE_NOT_SET:
            return None
        elif rval == STYLE_ERROR:
            gterror(err)

    def set_num(self, section, key, number):
        from ctypes import c_double
        num = c_double(number)
        gtlib.gt_style_set_num(self.style, section, key, num)

    def get_bool(self, section, key, gn=None):
        from ctypes import byref, c_int
        bool = c_int()
        err = Error()
        gnp = None
        if gn:
            gnp = gn._as_parameter_
        rval = gtlib.gt_style_get_bool(self.style, section, key, byref(bool),
                                       gnp, err._as_parameter_)
        if rval == STYLE_OK:
            if bool.value == 1:
                return True
            else:
                return False
        elif rval == STYLE_NOT_SET:
            return None
        elif rval == STYLE_ERROR:
            gterror(err)

    def set_bool(self, section, key, val):
        if val == True:
            gtlib.gt_style_set_bool(self.style, section, key, 1)
        else:
            gtlib.gt_style_set_bool(self.style, section, key, 0)

    def unset(self, section, key):
        gtlib.gt_style_unset(self.style, section, key)

    def register(cls, gtlib):
        from ctypes import c_char_p, c_double, c_float, c_void_p, \
            POINTER, c_int
        gtlib.gt_style_delete.restype = None
        gtlib.gt_style_delete.argtypes = [c_void_p]
        gtlib.gt_style_get_bool.restype = c_int
        gtlib.gt_style_get_bool.argtypes = [c_void_p, c_char_p, c_char_p,
                                            POINTER(c_int), c_void_p, c_void_p]
        gtlib.gt_style_get_color.restype = c_int
        gtlib.gt_style_get_color.argtypes = [c_void_p, c_char_p,
                                             c_char_p, POINTER(Color), c_void_p, c_void_p]
        gtlib.gt_style_get_num.restype = c_int
        gtlib.gt_style_get_num.argtypes = [c_void_p, c_char_p, c_char_p,
                                           POINTER(c_double), c_void_p, c_void_p]
        gtlib.gt_style_get_str.restype = c_int
        gtlib.gt_style_get_str.argtypes = [c_void_p, c_char_p, c_char_p,
                                           c_void_p, c_void_p, c_void_p]
        gtlib.gt_style_load_file.restype = c_int
        gtlib.gt_style_load_file.argtypes = [c_void_p, c_char_p, c_void_p]
        gtlib.gt_style_load_str.restype = c_int
        gtlib.gt_style_load_str.argtypes = [c_void_p, c_void_p, c_void_p]
        gtlib.gt_style_new.restype = c_void_p
        gtlib.gt_style_new.argtypes = [c_void_p]
        gtlib.gt_style_set_bool.restype = None
        gtlib.gt_style_set_bool.argtypes = [
            c_void_p, c_char_p, c_char_p, c_int]
        gtlib.gt_style_set_color.restype = None
        gtlib.gt_style_set_color.argtypes = [c_void_p, c_char_p, c_char_p,
                                             POINTER(Color)]
        gtlib.gt_style_set_num.restype = None
        gtlib.gt_style_set_num.argtypes = [c_void_p, c_char_p, c_char_p,
                                           c_double]
        gtlib.gt_style_set_str.restype = None
        gtlib.gt_style_set_str.argtypes = [c_void_p, c_char_p, c_char_p,
                                           c_void_p]
        gtlib.gt_style_to_str.restype = int
        gtlib.gt_style_to_str.argtypes = [c_void_p, c_void_p, c_void_p]
        gtlib.gt_style_unset.restype = None
        gtlib.gt_style_unset.argtypes = [c_void_p, c_char_p, c_char_p]

    register = classmethod(register)