Refine coding style of perperty.py

This commit is contained in:
Huang Peng 2008-07-15 13:03:21 +08:00
Родитель 1ee87eeb90
Коммит 1f755d9d31
1 изменённых файлов: 79 добавлений и 77 удалений

Просмотреть файл

@ -38,16 +38,16 @@ import dbus
PROP_TYPE_NORMAL = 0
PROP_TYPE_TOGGLE = 1
PROP_TYPE_RADIO = 2
PROP_TYPE_MENU = 3
PROP_TYPE_RADIO = 2
PROP_TYPE_MENU = 3
PROP_TYPE_SEPARATOR = 4
PROP_STATE_UNCHECKED = 0
PROP_STATE_CHECKED = 1
PROP_STATE_INCONSISTENT = 2
class Property:
def __init__ (self, name,
class Property(object):
def __init__(self, name,
type = PROP_TYPE_NORMAL,
label = "",
icon = "",
@ -55,6 +55,7 @@ class Property:
sensitive = True,
visible = True,
state = PROP_STATE_UNCHECKED):
super(Property, self).__init__()
self._name = name
self._type = type
self._label = label
@ -63,51 +64,51 @@ class Property:
self._sensitive = sensitive
self._visible = visible
self._state = state
self._sub_props = PropList ()
self._sub_props = PropList()
def set_sub_props (self, props):
def set_sub_props(self, props):
self._sub_props = props
def get_sub_props (self):
def get_sub_props(self):
return self._sub_props
def get_name (self):
def get_name(self):
return self._name
def get_type (self):
def get_type(self):
return self._type
def set_label (self, label):
def set_label(self, label):
self._label = label
def get_label (self):
def get_label(self):
return self._label
def set_tooltip (self, tooltip):
def set_tooltip(self, tooltip):
self._tooltip = tooltip
def get_tooltip (self):
def get_tooltip(self):
return self._tooltip
def set_state (self, state):
def set_state(self, state):
self._state = state
def get_state (self):
def get_state(self):
return self._state
def set_sensitive (self, sensitive):
def set_sensitive(self, sensitive):
self._sensitive = sensitive
def get_sensitive (self):
def get_sensitive(self):
return self._sensitive
def set_visible (self, visible):
def set_visible(self, visible):
self._visible = visible
def get_visible (self):
def get_visible(self):
return self._visible
def is_same (self, prop, test_all = True):
def is_same(self, prop, test_all = True):
if self._name != prop._name or self._type != prop._type:
return False
if not test_all:
@ -119,23 +120,23 @@ class Property:
self._visible != prop._visible or \
self._state != prop._state:
return False
return self._sub_props.is_same (prop._sub_props, test_all)
return self._sub_props.is_same(prop._sub_props, test_all)
def to_dbus_value (self):
sub_props = self._sub_props.to_dbus_value ()
values = (dbus.String (self._name),
dbus.Int32 (self._type),
dbus.String (self._label),
dbus.String (self._icon),
dbus.String (self._tooltip),
dbus.Boolean (self._sensitive),
dbus.Boolean (self._visible),
dbus.Int32 (self._state),
def to_dbus_value(self):
sub_props = self._sub_props.to_dbus_value()
values = (dbus.String(self._name),
dbus.Int32(self._type),
dbus.String(self._label),
dbus.String(self._icon),
dbus.String(self._tooltip),
dbus.Boolean(self._sensitive),
dbus.Boolean(self._visible),
dbus.Int32(self._state),
sub_props)
return dbus.Struct (values)
return dbus.Struct(values)
def from_dbus_value (self, value):
def from_dbus_value(self, value):
self._name, \
self._type, \
self._label, \
@ -146,74 +147,75 @@ class Property:
self._state, \
props = value
self._sub_props = prop_list_from_dbus_value (props)
self._sub_props = prop_list_from_dbus_value(props)
def property_from_dbus_value (value):
p = Property ("")
p.from_dbus_value (value)
def property_from_dbus_value(value):
p = Property("")
p.from_dbus_value(value)
return p
class PropList:
def __init__ (self):
class PropList(object):
def __init__(self):
super(PropLis, self).__init__()
self._props = []
def append (self, prop):
self._props.append (prop)
def append(self, prop):
self._props.append(prop)
def prepand (self, prop):
self._props.insert (0, prop)
def prepand(self, prop):
self._props.insert(0, prop)
def insert (self, index, prop):
self._props.insert (index, prop)
def insert(self, index, prop):
self._props.insert(index, prop)
def get_properties (self):
def get_properties(self):
return self._props[:]
def is_same (self, props, test_all = True):
if len (props.get_properties ()) != len (self.get_properties ()):
def is_same(self, props, test_all = True):
if len(props.get_properties()) != len(self.get_properties()):
return False
for a, b in zip (self.get_properties(), props.get_properties ()):
if not a.is_same (b, test_all):
for a, b in zip(self.get_properties(), props.get_properties()):
if not a.is_same(b, test_all):
return False
return False
def to_dbus_value (self):
props = map (lambda p: p.to_dbus_value (), self._props)
return dbus.Array (props, signature = "v")
def to_dbus_value(self):
props = map(lambda p: p.to_dbus_value(), self._props)
return dbus.Array(props, signature = "v")
def from_dbus_value (self, value):
def from_dbus_value(self, value):
props = []
for p in value:
props.append (property_from_dbus_value (p))
props.append(property_from_dbus_value(p))
self._props = props
def __iter__ (self):
return self._props.__iter__ ()
def __iter__(self):
return self._props.__iter__()
def __getitem__ (self, i):
return self._props.__getitem__ (i)
def __getitem__(self, i):
return self._props.__getitem__(i)
def prop_list_from_dbus_value (value):
props = PropList ()
props.from_dbus_value (value)
def prop_list_from_dbus_value(value):
props = PropList()
props.from_dbus_value(value)
return props
def test ():
props = PropList ()
props.append (Property ("a"))
props.append (Property ("b"))
props.append (Property ("c"))
props.append (Property ("d"))
value = props.to_dbus_value ()
print prop_list_from_dbus_value (value)
def test():
props = PropList()
props.append(Property("a"))
props.append(Property("b"))
props.append(Property("c"))
props.append(Property("d"))
value = props.to_dbus_value()
print prop_list_from_dbus_value(value)
p = Property ("z")
p.set_sub_props (props)
props = PropList ()
props.append (p)
value = props.to_dbus_value ()
print prop_list_from_dbus_value (value)
p = Property("z")
p.set_sub_props(props)
props = PropList()
props.append(p)
value = props.to_dbus_value()
print prop_list_from_dbus_value(value)
if __name__ == "__main__":
test ()
test()