mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2025-01-30 19:18:31 +00:00
Refactor state/config classes to be more flexible
This commit is contained in:
parent
50a354c0a1
commit
6e056f8e20
2 changed files with 104 additions and 81 deletions
|
@ -210,88 +210,70 @@ class Manager (object):
|
||||||
|
|
||||||
class StateString (object):
|
class StateString (object):
|
||||||
|
|
||||||
"""Descriptor for binding to AppState classes."""
|
"""Descriptor for binding to StateSection classes."""
|
||||||
|
|
||||||
def __init__ (self, option, section = None):
|
def __init__ (self, option):
|
||||||
|
|
||||||
self.option = option
|
self.option = option
|
||||||
self.section = section
|
|
||||||
|
|
||||||
def get_section (self, state):
|
def __get__ (self, section, section_class = None):
|
||||||
|
|
||||||
if self.section is None:
|
import ConfigParser
|
||||||
return state._default_section
|
|
||||||
else:
|
|
||||||
return self.section
|
|
||||||
|
|
||||||
def get_getter (self, state):
|
if section is None:
|
||||||
|
return self
|
||||||
|
|
||||||
return state._parser.get
|
try:
|
||||||
|
return self.get (section)
|
||||||
|
except (ConfigParser.NoSectionError,
|
||||||
|
ConfigParser.NoOptionError,):
|
||||||
|
return self.get_default (section)
|
||||||
|
|
||||||
def get_default (self, state):
|
def __set__ (self, section, value):
|
||||||
|
|
||||||
|
import ConfigParser
|
||||||
|
|
||||||
|
self.set (section, value)
|
||||||
|
|
||||||
|
def get (self, section):
|
||||||
|
|
||||||
|
return section.get (self)
|
||||||
|
|
||||||
|
def get_default (self, section):
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def __get__ (self, state, state_class = None):
|
def set (self, section, value):
|
||||||
|
|
||||||
import ConfigParser
|
|
||||||
|
|
||||||
if state is None:
|
|
||||||
return self
|
|
||||||
|
|
||||||
getter = self.get_getter (state)
|
|
||||||
section = self.get_section (state)
|
|
||||||
|
|
||||||
try:
|
|
||||||
return getter (section, self.option)
|
|
||||||
except (ConfigParser.NoSectionError,
|
|
||||||
ConfigParser.NoOptionError,):
|
|
||||||
return self.get_default (state)
|
|
||||||
|
|
||||||
def __set__ (self, state, value):
|
|
||||||
|
|
||||||
import ConfigParser
|
|
||||||
|
|
||||||
if value is None:
|
if value is None:
|
||||||
value = ""
|
value = ""
|
||||||
|
|
||||||
section = self.get_section (state)
|
section.set (self, str (value))
|
||||||
option = self.option
|
|
||||||
option_value = str (value)
|
|
||||||
|
|
||||||
try:
|
|
||||||
state._parser.set (section, option, option_value)
|
|
||||||
except ConfigParser.NoSectionError:
|
|
||||||
state._parser.add_section (section)
|
|
||||||
state._parser.set (section, option, option_value)
|
|
||||||
|
|
||||||
class StateBool (StateString):
|
class StateBool (StateString):
|
||||||
|
|
||||||
"""Descriptor for binding to AppState classes."""
|
"""Descriptor for binding to StateSection classes."""
|
||||||
|
|
||||||
def get_getter (self, state):
|
def get (self, section):
|
||||||
|
|
||||||
return state._parser.getboolean
|
return section.state._parser.getboolean (section._name, self.option)
|
||||||
|
|
||||||
class StateInt (StateString):
|
class StateInt (StateString):
|
||||||
|
|
||||||
"""Descriptor for binding to AppState classes."""
|
"""Descriptor for binding to StateSection classes."""
|
||||||
|
|
||||||
def get_getter (self, state):
|
def get (self, section):
|
||||||
|
|
||||||
return state._parser.getint
|
return section.state._parser.getint (section._name, self.option)
|
||||||
|
|
||||||
class StateInt4 (StateString):
|
class StateInt4 (StateString):
|
||||||
|
|
||||||
"""Descriptor for binding to AppState classes. This implements storing a
|
"""Descriptor for binding to StateSection classes. This implements storing
|
||||||
tuple of 4 integers."""
|
a tuple of 4 integers."""
|
||||||
|
|
||||||
def __get__ (self, state, state_class = None):
|
def get (self, section):
|
||||||
|
|
||||||
if state is None:
|
value = StateString.get (self, section)
|
||||||
return self
|
|
||||||
|
|
||||||
value = StateString.__get__ (self, state)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
l = value.split (",")
|
l = value.split (",")
|
||||||
|
@ -302,7 +284,7 @@ class StateInt4 (StateString):
|
||||||
except (AttributeError, TypeError, ValueError,):
|
except (AttributeError, TypeError, ValueError,):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def __set__ (self, state, value):
|
def set (self, section, value):
|
||||||
|
|
||||||
if value is None:
|
if value is None:
|
||||||
svalue = ""
|
svalue = ""
|
||||||
|
@ -311,39 +293,36 @@ class StateInt4 (StateString):
|
||||||
else:
|
else:
|
||||||
svalue = ", ".join ((str (v) for v in value))
|
svalue = ", ".join ((str (v) for v in value))
|
||||||
|
|
||||||
return StateString.__set__ (self, state, svalue)
|
return StateString.set (self, section, svalue)
|
||||||
|
|
||||||
class StateItem (StateString):
|
class StateItem (StateString):
|
||||||
|
|
||||||
"""Descriptor for binding to AppState classes. This implements storing a
|
"""Descriptor for binding to StateSection classes. This implements storing
|
||||||
class controlled by a Manager class."""
|
a class controlled by a Manager class."""
|
||||||
|
|
||||||
def __init__ (self, option, manager_class, section = None):
|
def __init__ (self, option, manager_class):
|
||||||
|
|
||||||
StateString.__init__ (self, option, section = section)
|
StateString.__init__ (self, option)
|
||||||
|
|
||||||
self.manager = manager_class
|
self.manager = manager_class
|
||||||
|
|
||||||
def __get__ (self, state, state_class = None):
|
def get (self, section):
|
||||||
|
|
||||||
if state is None:
|
value = SectionString.get (self, section)
|
||||||
return self
|
|
||||||
|
|
||||||
value = StateString.__get__ (self, state)
|
|
||||||
|
|
||||||
if not value:
|
if not value:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return self.parse_item (value)
|
return self.parse_item (value)
|
||||||
|
|
||||||
def __set__ (self, state, value):
|
def set (self, section, value):
|
||||||
|
|
||||||
if value is None:
|
if value is None:
|
||||||
svalue = ""
|
svalue = ""
|
||||||
else:
|
else:
|
||||||
svalue = value.name
|
svalue = value.name
|
||||||
|
|
||||||
StateString.__set__ (self, state, svalue)
|
StateString.set (self, section, svalue)
|
||||||
|
|
||||||
def parse_item (self, value):
|
def parse_item (self, value):
|
||||||
|
|
||||||
|
@ -356,15 +335,12 @@ class StateItem (StateString):
|
||||||
|
|
||||||
class StateItemList (StateItem):
|
class StateItemList (StateItem):
|
||||||
|
|
||||||
"""Descriptor for binding to AppState classes. This implements storing an
|
"""Descriptor for binding to StateSection classes. This implements storing
|
||||||
ordered set of Manager items."""
|
an ordered set of Manager items."""
|
||||||
|
|
||||||
def __get__ (self, state, state_class = None):
|
def get (self, section):
|
||||||
|
|
||||||
if state is None:
|
value = StateString.get (self, section)
|
||||||
return self
|
|
||||||
|
|
||||||
value = StateString.__get__ (self, state)
|
|
||||||
|
|
||||||
if not value:
|
if not value:
|
||||||
return []
|
return []
|
||||||
|
@ -379,23 +355,54 @@ class StateItemList (StateItem):
|
||||||
|
|
||||||
return classes
|
return classes
|
||||||
|
|
||||||
def __set__ (self, state, value):
|
def get_default (self, section):
|
||||||
|
|
||||||
|
return []
|
||||||
|
|
||||||
|
def set (self, section, value):
|
||||||
|
|
||||||
if value is None:
|
if value is None:
|
||||||
svalue = ""
|
svalue = ""
|
||||||
else:
|
else:
|
||||||
svalue = ", ".join ((v.name for v in value))
|
svalue = ", ".join ((v.name for v in value))
|
||||||
|
|
||||||
StateString.__set__ (self, state, svalue)
|
|
||||||
|
|
||||||
class AppState (object):
|
StateString.set (self, section, svalue)
|
||||||
|
|
||||||
_default_section = "state"
|
class StateSection (object):
|
||||||
|
|
||||||
|
_name = None
|
||||||
|
|
||||||
|
def __init__ (self, state):
|
||||||
|
|
||||||
|
self.state = state
|
||||||
|
|
||||||
|
if self._name is None:
|
||||||
|
raise NotImplementedError ("subclasses must override the _name attribute")
|
||||||
|
|
||||||
|
def get (self, state_string):
|
||||||
|
|
||||||
|
return self.state._parser.get (self._name, state_string.option)
|
||||||
|
|
||||||
|
def set (self, state_string, value):
|
||||||
|
|
||||||
|
import ConfigParser
|
||||||
|
|
||||||
|
parser = self.state._parser
|
||||||
|
|
||||||
|
try:
|
||||||
|
parser.set (self._name, state_string.option, value)
|
||||||
|
except ConfigParser.NoSectionError:
|
||||||
|
parser.add_section (self._name)
|
||||||
|
parser.set (self._name, state_string.option, value)
|
||||||
|
|
||||||
|
class State (object):
|
||||||
|
|
||||||
def __init__ (self, filename, old_filenames = ()):
|
def __init__ (self, filename, old_filenames = ()):
|
||||||
|
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
|
|
||||||
|
self.sections = {}
|
||||||
|
|
||||||
self._filename = filename
|
self._filename = filename
|
||||||
self._parser = ConfigParser.RawConfigParser ()
|
self._parser = ConfigParser.RawConfigParser ()
|
||||||
success = self._parser.read ([filename])
|
success = self._parser.read ([filename])
|
||||||
|
@ -405,6 +412,10 @@ class AppState (object):
|
||||||
if success:
|
if success:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
def add_section_class (self, section_class):
|
||||||
|
|
||||||
|
self.sections[section_class._name] = section_class (self)
|
||||||
|
|
||||||
def save (self):
|
def save (self):
|
||||||
|
|
||||||
# TODO Py2.5: Use 'with' statement.
|
# TODO Py2.5: Use 'with' statement.
|
||||||
|
|
|
@ -927,7 +927,7 @@ class Window (object):
|
||||||
self.update_progress_id = None
|
self.update_progress_id = None
|
||||||
|
|
||||||
self.window_state = Common.GUI.WindowState ()
|
self.window_state = Common.GUI.WindowState ()
|
||||||
self.column_manager = ViewColumnManager (app.state)
|
self.column_manager = ViewColumnManager (app.state_section)
|
||||||
|
|
||||||
self.actions = Common.GUI.Actions ()
|
self.actions = Common.GUI.Actions ()
|
||||||
|
|
||||||
|
@ -993,7 +993,8 @@ class Window (object):
|
||||||
|
|
||||||
def attach (self):
|
def attach (self):
|
||||||
|
|
||||||
self.window_state.attach (window = self.gtk_window, state = self.app.state)
|
self.window_state.attach (window = self.gtk_window,
|
||||||
|
state = self.app.state_section)
|
||||||
|
|
||||||
self.clipboard = gtk.Clipboard (self.gtk_window.get_display (),
|
self.clipboard = gtk.Clipboard (self.gtk_window.get_display (),
|
||||||
gtk.gdk.SELECTION_CLIPBOARD)
|
gtk.gdk.SELECTION_CLIPBOARD)
|
||||||
|
@ -1261,13 +1262,23 @@ class Window (object):
|
||||||
|
|
||||||
gobject.idle_add (idle_set)
|
gobject.idle_add (idle_set)
|
||||||
|
|
||||||
class AppState (Common.GUI.AppState):
|
class AppStateSection (Common.GUI.StateSection):
|
||||||
|
|
||||||
|
_name = "state"
|
||||||
|
|
||||||
geometry = Common.GUI.StateInt4 ("window-geometry")
|
geometry = Common.GUI.StateInt4 ("window-geometry")
|
||||||
maximized = Common.GUI.StateBool ("window-maximized")
|
maximized = Common.GUI.StateBool ("window-maximized")
|
||||||
|
|
||||||
column_order = Common.GUI.StateItemList ("column-order", ViewColumnManager)
|
column_order = Common.GUI.StateItemList ("column-order", ViewColumnManager)
|
||||||
columns_visible = Common.GUI.StateItemList ("columns-visible", ViewColumnManager)
|
columns_visible = Common.GUI.StateItemList ("columns-visible", ViewColumnManager)
|
||||||
|
|
||||||
|
class AppState (Common.GUI.State):
|
||||||
|
|
||||||
|
def __init__ (self, *a, **kw):
|
||||||
|
|
||||||
|
Common.GUI.State.__init__ (self, *a, **kw)
|
||||||
|
|
||||||
|
self.add_section_class (AppStateSection)
|
||||||
|
|
||||||
class App (object):
|
class App (object):
|
||||||
|
|
||||||
|
@ -1296,6 +1307,7 @@ class App (object):
|
||||||
state_filename = os.path.join (config_home, "gst-debug-viewer", "state")
|
state_filename = os.path.join (config_home, "gst-debug-viewer", "state")
|
||||||
|
|
||||||
self.state = AppState (state_filename)
|
self.state = AppState (state_filename)
|
||||||
|
self.state_section = self.state.sections["state"]
|
||||||
|
|
||||||
self.windows = [Window (self)]
|
self.windows = [Window (self)]
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue