2 from attribute
import *
3 from exception
import *
5 class StringList (list):
9 def to_dbus_value (self
):
10 value
= dbus
.Array ([], signature
="v")
11 for text
, attrs
in self
:
12 value
.append (dbus
.Struct ((dbus
.String (text
), attrs
.to_dbus_value ())))
15 def from_dbus_value (self
, value
):
17 if not isinstance (value
, dbus
.Array
):
18 raise dbus
.Exception ("Candidates must from dbus.Array (a(sa(...))")
19 for candidate
in value
:
20 if not isinstance (candidate
, dbus
.Struct
):
21 raise IBusException ("Candidates must from dbus.Array (a(sa(...)))")
22 if len (candidate
) != 2 or \
23 not isinstance (candidate
[0], dbus
.String
):
24 raise IBusException ("Candidates must from dbus.Array (a(sa(...)))")
26 attrs
= attr_list_from_dbus_value (candidate
[1])
27 candidates
.append ((text
, attrs
))
33 def __init__ (self
, page_size
= 5, labels
= None):
34 self
._cursor
_visible
= False
36 self
._candidates
= StringList ()
37 self
.set_page_size (page_size
)
39 def set_page_size (self
, page_size
):
40 self
._page
_size
= page_size
42 def get_page_size (self
):
43 return self
._page
_size
45 def get_current_page_size (self
):
46 nr_candidate
= len (self
._candidates
)
47 nr_page
, last_page_size
= divmod (nr_candidate
, self
._page
_size
)
48 if self
._cursor
_pos
/ self
._page
_size
== nr_page
:
51 return self
._page
_size
53 def set_labels (self
, labels
):
54 self
._labels
== labels
56 def show_cursor (self
, show
= True):
57 self
._cursor
_visible
= show
59 def is_cursor_visible (self
):
60 return self
._cursor
_visible
62 def get_current_page_start (self
):
63 return (self
._cursor
_pos
/ self
._page
_size
) * self
._page
_size
65 def set_cursor_pos (self
, pos
):
66 if pos
>= len (self
._candidates
) or pos
< 0:
68 self
._cursor
_pos
= pos
71 def get_cursor_pos (self
):
72 return self
._cursor
_pos
74 def get_cursor_pos_in_current_page (self
):
75 page
, pos_in_page
= divmod (self
._cursor
_pos
, self
._page
_size
)
78 def set_cursor_pos_in_current_page (self
, pos
):
79 if pos
< 0 or pos
>= self
._page
_size
:
81 pos
+= self
.get_current_page_start ()
82 if pos
>= len (self
._candidates
):
84 self
._cursor
_pos
= pos
89 if self
._cursor
_pos
< self
._page
_size
:
92 self
._cursor
_pos
-= self
._page
_size
96 current_page
= self
._cursor
_pos
/ self
._page
_size
97 nr_candidates
= len (self
._candidates
)
98 max_page
= nr_candidates
/ self
._page
_size
100 if current_page
>= max_page
:
103 pos
= self
._cursor
_pos
+ self
._page
_size
104 if pos
>= nr_candidates
:
106 self
._cursor
_pos
= pos
110 def cursor_up (self
):
111 if self
._cursor
_pos
== 0:
114 self
._cursor
_pos
-= 1
117 def cursor_down (self
):
118 if self
._cursor
_pos
== len (self
._candidates
) - 1:
121 self
._cursor
_pos
+= 1
125 self
._candidates
.clean ()
128 def append_candidate (self
, candidate
, attrs
= None):
131 self
._candidates
.append ((candidate
, attrs
))
133 def get_candidate (self
, index
):
134 return self
._candidates
[index
]
136 def get_canidates_in_current_page (self
):
137 page
= self
._cursor
_pos
/ self
._page
_size
138 start_index
= page
* self
._page
_size
139 end_index
= min ((page
+ 1) * self
._page
_size
, len (self
._candidates
))
140 return self
._candidates
[start_index
:end_index
]
142 def get_current_candidate (self
):
143 return self
._candidates
[self
._cursor
_pos
]
145 def get_number_of_candidates (self
):
146 return len (self
._candidates
)
148 def to_dbus_value (self
):
149 value
= (dbus
.Int32 (self
._page
_size
),
150 dbus
.Int32 (self
._cursor
_pos
),
151 dbus
.Boolean (self
._cursor
_visible
),
152 self
._candidates
.to_dbus_value ())
153 return dbus
.Struct (value
)
155 def from_dbus_value (self
, value
):
156 if not isinstance (value
, dbus
.Struct
):
157 raise dbus
.Exception ("LookupTable must from dbus.Struct (uuba(...))")
159 if len (value
) != 4 or \
160 not isinstance (value
[0], dbus
.Int32
) or \
161 not isinstance (value
[1], dbus
.Int32
) or \
162 not isinstance (value
[2], dbus
.Boolean
):
163 raise dbus
.Exception ("LookupTable must from dbus.Struct (uuba(...))")
165 self
._candidates
.from_dbus_value (value
[3])
166 self
._page
_size
= value
[0]
167 self
._cursor
_pos
= value
[1]
168 self
._cursor
_visible
= value
[2]
170 def lookup_table_from_dbus_value (value
):
171 lookup_table
= LookupTable ()
172 lookup_table
.from_dbus_value (value
)
177 # attrs = AttrList ()
178 # attrs.append (AttributeBackground (RGB (233, 0,1), 0, 3))
179 # attrs.append (AttributeUnderline (1, 3, 5))
180 t
.append_candidate ("Hello")
181 value
= t
.to_dbus_value ()
183 t
= lookup_table_from_dbus_value (value
)
185 if __name__
== "__main__":