Bug 91423
Summary: | Python class to make this code simpler | ||
---|---|---|---|
Product: | [Retired] Red Hat Linux | Reporter: | Shannon -jj Behrens <jjinux> |
Component: | redhat-config-services | Assignee: | Daniel Walsh <dwalsh> |
Status: | CLOSED NOTABUG | QA Contact: | |
Severity: | medium | Docs Contact: | |
Priority: | low | ||
Version: | 9 | CC: | behdad, mitr |
Target Milestone: | --- | Keywords: | FutureFeature |
Target Release: | --- | ||
Hardware: | All | ||
OS: | Linux | ||
URL: | http://ironorchid.com/jjinux/articles/pytemp-0.1.tar.gz | ||
Whiteboard: | |||
Fixed In Version: | Doc Type: | Enhancement | |
Doc Text: | Story Points: | --- | |
Clone Of: | Environment: | ||
Last Closed: | 2004-09-13 15:11:14 UTC | Type: | --- |
Regression: | --- | Mount Type: | --- |
Documentation: | --- | CRM: | |
Verified Versions: | Category: | --- | |
oVirt Team: | --- | RHEL 7.3 requirements from Atomic Host: | |
Cloudforms Team: | --- | Target Upstream Version: | |
Embargoed: |
Description
Shannon -jj Behrens
2003-05-22 14:53:20 UTC
"""This module contains the LibGladeApplication class.""" import signal import gtk from gtk import glade class LibGladeApplication: """This is the base class for applications that use Glade. The following attributes are used: xml - This is an instance of glade.XML which encapsulates the Glade GUI. """ def __init__(self, gladeFile): """Setup the appropriate signal handlers and call setHandlers. This must be called, but only after the subclass initializes the xml attribute. """ signal.signal(signal.SIGINT, signal.SIG_DFL) self.xml = glade.XML(gladeFile) self.setHandlers() def setHandlers(self): """Automatically autoconnect all of the handlers. Any methods (even in subclasses) that start with "on_" will be treated as a handler that is automatically connected to by xml.signal_autoconnect. """ handlers = {} for i in dir(self): if i.startswith("on_"): handlers[i] = getattr(self, i) self.xml.signal_autoconnect(handlers) def __getattr__(self, name): """If self doesn't have the attribute, check self.xml.""" obj = self.xml.get_widget(name) if obj: return obj else: raise AttributeError("%s instance has no attribute '%s'" % (self.__class__.__name__, name)) def on_quit_activate(self, *args): """Ignore args and call gtk.mainquit().""" gtk.mainquit() How embarrassing--there was a stale comment in the __init__ method. It should be updated to: def __init__(self, gladeFile): """Setup the appropriate signal handlers and call setHandlers.""" signal.signal(signal.SIGINT, signal.SIG_DFL) self.xml = glade.XML(gladeFile) self.setHandlers() Sorry! In the off chance that you guys actually are interested in this code, I've also written a wrapper class that makes attribute references for GtkObjects more Pythonic: widget.set_text(widget.get_text() + "foo") becomes: widget.text = widget.text + "foo" The LibGladeApplication now automatically wraps GtkObjects in a new GtkAttributesFacade instance when returning them. I'll include the code in the next two posts. """This module contains the GtkAttributesFacade class.""" class GtkAttributesFacade: """Wrap a GTK instance to simplify the way attributes are referenced. Given a GTK instance i, make it possible for any functions i.get_foo() and i.set_foo(value) to be accessed via i.foo like a normal attribute. The following attributes are used: instance - This is the GTK instance that is being wrapped. """ def __init__(self, instance): """Accept the instance.""" self.__dict__["instance"] = instance def __setattr__(self, name, value): """Simplify the way attributes are referenced. When trying to do self.foo = something, if there is a self.instance.set_foo() method, use it. Otherwise, just set the attribute in self. Return value so that chaining is possible. """ setter = "set_" + name if hasattr(self.instance, setter): apply(getattr(self.instance, setter), [value]) else: self.__dict__[name] = value return value def __getattr__(self, name): """Simplify the way attributes are referenced. Remember that this method is called after a failed lookup in self. Try looking for self.instance.foo. Next, try looking for a self.instance.get_foo() method, and call it if it exists. Otherwise, raise an exception. """ if hasattr(self.instance, name): return getattr(self.instance, name) getter = "get_" + name if hasattr(self.instance, getter): return apply(getattr(self.instance, getter)) raise AttributeError( "%s instance has no attribute '%s' " % (self.instance.__class__.__name__, name)) """This module contains the LibGladeApplication class.""" import signal import gtk from gtk import glade from GtkAttributesFacade import GtkAttributesFacade class LibGladeApplication: """This is the base class for applications that use Glade. The following attributes are used: xml - This is an instance of glade.XML which encapsulates the Glade GUI. """ def __init__(self, gladeFile): """Setup the appropriate signal handlers and call setHandlers.""" signal.signal(signal.SIGINT, signal.SIG_DFL) self.xml = glade.XML(gladeFile) self.setHandlers() def setHandlers(self): """Automatically autoconnect all of the handlers. Any methods (even in subclasses) that start with "on_" will be treated as a handler that is automatically connected to by xml.signal_autoconnect. """ handlers = {} for i in dir(self): if i.startswith("on_"): handlers[i] = getattr(self, i) self.xml.signal_autoconnect(handlers) def __getattr__(self, name): """If self doesn't have the attribute, check self.xml. If self.xml does have the attribute, wrap it in a GtkAttributesFacade instance, cache it in self, and return it. """ obj = self.xml.get_widget(name) if obj: obj = GtkAttributesFacade(obj) setattr(self, name, obj) return obj raise AttributeError("%s instance has no attribute '%s'" % (self.__class__.__name__, name)) def on_quit_activate(self, *args): """Ignore args and call gtk.mainquit().""" gtk.mainquit() I apologize for "spamming" you guys with this code. I was just trying to be helpful. If you guys are actually interested, drop me an email. I'll go away now. ;) Haven't had a chance to look at/incorporate your stuff, but it looks interesting. I would like to encourage you to continue your effort. That is what makes Open Source great. Dan Your LibGladeApplication Class will only work in the case where the on_XXX match on on_XXX method call. So some of redhat-config-services would need to be changed. Have you rewritten redhat-config-services to use your patch(s). If so please submit the changes and I will try it out. Dan Shannon, Please join the fedora-config-list mailing list. There are folks there working on the redhat-config-* tools and your contributions would be encouraged. http://www.redhat.com/mailman/listinfo/fedora-config-list Thanks! Hmm, I'm trying like crazy to join that mailing list :-/ I've subscribed and confirmed twice, but I never get the final confirmation notice :-/ Maybe I should get a non-Yahoo address. |