changeset 736:6246eb6d64a0

frontends: define the constants with classes and inheritance instance of using __builtin__
author souliane <souliane@mailoo.org>
date Sat, 23 Nov 2013 10:21:40 +0100
parents 682933ca304c
children 378af36155c2
files frontends/src/constants.py frontends/src/primitivus/constants.py frontends/src/primitivus/contact_list.py frontends/src/primitivus/primitivus frontends/src/quick_frontend/constants.py frontends/src/quick_frontend/quick_app.py frontends/src/quick_frontend/quick_chat.py frontends/src/quick_frontend/quick_utils.py frontends/src/wix/constants.py frontends/src/wix/contact_list.py frontends/src/wix/main_window.py
diffstat 11 files changed, 168 insertions(+), 94 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/frontends/src/constants.py	Sat Nov 23 10:21:40 2013 +0100
@@ -0,0 +1,34 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+# Primitivus: a SAT frontend
+# Copyright (C) 2009, 2010, 2011, 2012, 2013  Jérôme Poisson (goffi@goffi.org)
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+try:
+    _("dummy")
+except:  # libervia doesn't allow to explicit the exception here
+    _ = lambda text: text
+
+class Const(object):
+
+    # TODO: find a way to merge it with the ones of primitivus and wix
+    PRESENCE = {"": _("Online"),
+                "chat": _("Free for chat"),
+                "away": _("Away from keyboard"),
+                "dnd": _("Do not disturb"),
+                "xa": _("Away")
+                }
--- a/frontends/src/primitivus/constants.py	Sun Nov 24 11:22:20 2013 +0100
+++ b/frontends/src/primitivus/constants.py	Sat Nov 23 10:21:40 2013 +0100
@@ -16,59 +16,65 @@
 
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
-import __builtin__
+
+from sat_frontends.quick_frontend import constants
+
+
+class Const(constants.Const):
 
-__builtin__.__dict__['const_APP_NAME'] = "Primitivus"
-__builtin__.__dict__['const_PALETTE']  = [
-    ('title', 'black', 'light gray', 'standout,underline'),
-    ('title_focus', 'white,bold', 'light gray', 'standout,underline'),
-    ('selected', 'default', 'dark red'),
-    ('selected_focus', 'default,bold', 'dark red'),
-    ('default', 'default', 'default'),
-    ('default_focus', 'default,bold', 'default'),
-    ('alert', 'default,underline', 'default'),
-    ('alert_focus', 'default,bold,underline', 'default'),
-    ('date', 'light gray', 'default'),
-    ('my_nick', 'dark red,bold', 'default'),
-    ('other_nick', 'dark cyan,bold', 'default'),
-    ('menubar', 'light gray,bold', 'dark red'),
-    ('menubar_focus', 'light gray,bold', 'dark green'),
-    ('selected_menu', 'light gray,bold', 'dark green'),
-    ('menuitem', 'light gray,bold', 'dark red'),
-    ('menuitem_focus', 'light gray,bold', 'dark green'),
-    ('notifs', 'black,bold', 'yellow'),
-    ('notifs_focus', 'dark red', 'yellow'),
-    ('card_neutral', 'dark gray', 'white', 'standout,underline'),
-    ('card_neutral_selected', 'dark gray', 'dark green', 'standout,underline'),
-    ('card_special', 'brown', 'white', 'standout,underline'),
-    ('card_special_selected', 'brown', 'dark green', 'standout,underline'),
-    ('card_red', 'dark red', 'white', 'standout,underline'),
-    ('card_red_selected', 'dark red', 'dark green', 'standout,underline'),
-    ('card_black', 'black', 'white', 'standout,underline'),
-    ('card_black_selected', 'black', 'dark green', 'standout,underline'),
-    ('directory', 'dark cyan, bold', 'default'),
-    ('directory_focus', 'dark cyan, bold', 'dark green'),
-    ('separator', 'brown', 'default'),
-    ('warning', 'light red', 'default'),
-    ('progress_normal', 'default', 'black'),
-    ('progress_complete', 'default', 'light red'),
-    ('show_disconnected', 'dark gray', 'default'),
-    ('show_normal', 'default', 'default'),
-    ('show_normal_focus', 'default, bold', 'default'),
-    ('show_chat', 'dark green', 'default'),
-    ('show_chat_focus', 'dark green, bold', 'default'),
-    ('show_away', 'brown', 'default'),
-    ('show_away_focus', 'brown, bold', 'default'),
-    ('show_dnd', 'dark red', 'default'),
-    ('show_dnd_focus', 'dark red, bold', 'default'),
-    ('show_xa', 'dark red', 'default'),
-    ('show_xa_focus', 'dark red, bold', 'default'),
-    ('status', 'yellow', 'default'),
-    ('status_focus', 'yellow, bold', 'default'),
-    ]
-__builtin__.__dict__['const_SHOW_ICON'] = {"unavailable": (u'⨯', "show_disconnected"),
-                                          "": (u'✔', "show_normal"),
-                                          "chat": (u'✆', "show_chat"),
-                                          "away": (u'✈', "show_away"),
-                                          "dnd": (u'✖', "show_dnd"),
-                                          "xa": (u'☄', "show_xa")}
+    APP_NAME = "Primitivus"
+    PALETTE = [
+               ('title', 'black', 'light gray', 'standout,underline'),
+               ('title_focus', 'white,bold', 'light gray', 'standout,underline'),
+               ('selected', 'default', 'dark red'),
+               ('selected_focus', 'default,bold', 'dark red'),
+               ('default', 'default', 'default'),
+               ('default_focus', 'default,bold', 'default'),
+               ('alert', 'default,underline', 'default'),
+               ('alert_focus', 'default,bold,underline', 'default'),
+               ('date', 'light gray', 'default'),
+               ('my_nick', 'dark red,bold', 'default'),
+               ('other_nick', 'dark cyan,bold', 'default'),
+               ('menubar', 'light gray,bold', 'dark red'),
+               ('menubar_focus', 'light gray,bold', 'dark green'),
+               ('selected_menu', 'light gray,bold', 'dark green'),
+               ('menuitem', 'light gray,bold', 'dark red'),
+               ('menuitem_focus', 'light gray,bold', 'dark green'),
+               ('notifs', 'black,bold', 'yellow'),
+               ('notifs_focus', 'dark red', 'yellow'),
+               ('card_neutral', 'dark gray', 'white', 'standout,underline'),
+               ('card_neutral_selected', 'dark gray', 'dark green', 'standout,underline'),
+               ('card_special', 'brown', 'white', 'standout,underline'),
+               ('card_special_selected', 'brown', 'dark green', 'standout,underline'),
+               ('card_red', 'dark red', 'white', 'standout,underline'),
+               ('card_red_selected', 'dark red', 'dark green', 'standout,underline'),
+               ('card_black', 'black', 'white', 'standout,underline'),
+               ('card_black_selected', 'black', 'dark green', 'standout,underline'),
+               ('directory', 'dark cyan, bold', 'default'),
+               ('directory_focus', 'dark cyan, bold', 'dark green'),
+               ('separator', 'brown', 'default'),
+               ('warning', 'light red', 'default'),
+               ('progress_normal', 'default', 'black'),
+               ('progress_complete', 'default', 'light red'),
+               ('show_disconnected', 'dark gray', 'default'),
+               ('show_normal', 'default', 'default'),
+               ('show_normal_focus', 'default, bold', 'default'),
+               ('show_chat', 'dark green', 'default'),
+               ('show_chat_focus', 'dark green, bold', 'default'),
+               ('show_away', 'brown', 'default'),
+               ('show_away_focus', 'brown, bold', 'default'),
+               ('show_dnd', 'dark red', 'default'),
+               ('show_dnd_focus', 'dark red, bold', 'default'),
+               ('show_xa', 'dark red', 'default'),
+               ('show_xa_focus', 'dark red, bold', 'default'),
+               ('status', 'yellow', 'default'),
+               ('status_focus', 'yellow, bold', 'default'),
+               ]
+    PRESENCE = {"unavailable": (u'⨯', "show_disconnected"),
+                "": (u'✔', "show_normal"),
+                "chat": (u'✆', "show_chat"),
+                "away": (u'✈', "show_away"),
+                "dnd": (u'✖', "show_dnd"),
+                "xa": (u'☄', "show_xa")
+                }
+
--- a/frontends/src/primitivus/contact_list.py	Sun Nov 24 11:22:20 2013 +0100
+++ b/frontends/src/primitivus/contact_list.py	Sat Nov 23 10:21:40 2013 +0100
@@ -22,7 +22,7 @@
 from sat_frontends.quick_frontend.quick_contact_list import QuickContactList
 from sat_frontends.quick_frontend.quick_utils import escapePrivate, unescapePrivate
 from sat.tools.jid import JID
-
+from sat_frontends.primitivus.constants import Const
 
 class ContactList(urwid.WidgetWrap, QuickContactList):
     signals = ['click','change']
@@ -87,7 +87,7 @@
                     # contact group
                     value = widget.getValue()
                 elif isinstance(widget, sat_widgets.SelectableText):
-                    if  widget.data.startswith(const_PRIVATE_PREFIX):
+                    if  widget.data.startswith(Const.PRIVATE_PREFIX):
                         # muc private dialog
                         value = widget.getValue()
                     else:
@@ -137,7 +137,7 @@
         widgets = [] #list of built widgets
 
         for contact in contacts:
-            if contact.startswith(const_PRIVATE_PREFIX):
+            if contact.startswith(Const.PRIVATE_PREFIX):
                 contact_disp = ('alert' if contact in self.alert_jid else "show_normal", unescapePrivate(contact))
                 show_icon = ''
                 status = ''
@@ -152,7 +152,7 @@
                 if (not self.show_disconnected and show == "unavailable"
                     and not contact in self.alert_jid and contact != self.selected):
                     continue
-                show_icon, show_attr = const_SHOW_ICON.get(show,('','default'))
+                show_icon, show_attr = Const.PRESENCE.get(show, ('', 'default'))
                 contact_disp = ('alert' if contact in self.alert_jid else show_attr, nick or name or jid.node or jid.bare)
             display = [ show_icon + " " , contact_disp]
             if self.show_status:
--- a/frontends/src/primitivus/primitivus	Sun Nov 24 11:22:20 2013 +0100
+++ b/frontends/src/primitivus/primitivus	Sat Nov 23 10:21:40 2013 +0100
@@ -32,7 +32,7 @@
 from sat_frontends.primitivus.progress import Progress
 from sat_frontends.primitivus.notify import Notify
 from sat_frontends.tools.misc import InputHistory
-import sat_frontends.primitivus.constants
+from sat_frontends.primitivus.constants import Const
 import logging
 from logging import debug, info, error
 from sat.tools.jid  import JID
@@ -151,7 +151,7 @@
 
         ## main loop setup ##
         self.main_widget = ProfileManager(self)
-        self.loop = urwid.MainLoop(self.main_widget, const_PALETTE, event_loop=urwid.GLibEventLoop(), input_filter=self.inputFilter, unhandled_input=self.keyHandler)
+        self.loop = urwid.MainLoop(self.main_widget, const.PALETTE, event_loop=urwid.GLibEventLoop(), input_filter=self.inputFilter, unhandled_input=self.keyHandler)
 
         ##misc setup##
         self.chat_wins = ChatList(self)
@@ -590,7 +590,7 @@
             self.showPopUp(pop_up_widget)
 
     def onAboutRequest(self, menu):
-        self.showPopUp(sat_widgets.Alert(_("About"), const_APP_NAME + " v" + self.bridge.getVersion(), ok_cb=self.removePopUp))
+        self.showPopUp(sat_widgets.Alert(_("About"), const.APP_NAME + " v" + self.bridge.getVersion(), ok_cb=self.removePopUp))
 
     #MISC CALLBACKS#
 
--- a/frontends/src/quick_frontend/constants.py	Sun Nov 24 11:22:20 2013 +0100
+++ b/frontends/src/quick_frontend/constants.py	Sat Nov 23 10:21:40 2013 +0100
@@ -16,6 +16,10 @@
 
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
-import __builtin__
+
+from sat_frontends import constants
+
 
-__builtin__.__dict__['const_PRIVATE_PREFIX'] = "@PRIVATE@"
+class Const(constants.Const):
+
+    PRIVATE_PREFIX = "@PRIVATE@"
--- a/frontends/src/quick_frontend/quick_app.py	Sun Nov 24 11:22:20 2013 +0100
+++ b/frontends/src/quick_frontend/quick_app.py	Sat Nov 23 10:21:40 2013 +0100
@@ -25,7 +25,7 @@
 from sat_frontends.quick_frontend.quick_utils import escapePrivate, unescapePrivate
 from optparse import OptionParser
 
-import sat_frontends.quick_frontend.constants
+from sat_frontends.quick_frontend.constants import Const
 
 import gettext
 gettext.install('sat_frontend', unicode=True)
@@ -283,7 +283,7 @@
         self.chat_wins[win.bare].printMessage(from_jid, msg, profile, float(timestamp) if timestamp else '')
 
     def sendMessage(self, to_jid, message, subject='', mess_type="auto", extra={}, profile_key="@NONE@"):
-        if to_jid.startswith(const_PRIVATE_PREFIX):
+        if to_jid.startswith(Const.PRIVATE_PREFIX):
             to_jid = unescapePrivate(to_jid)
             mess_type = "chat"
         self.bridge.sendMessage(to_jid, message, subject, mess_type, extra, profile_key)
--- a/frontends/src/quick_frontend/quick_chat.py	Sun Nov 24 11:22:20 2013 +0100
+++ b/frontends/src/quick_frontend/quick_chat.py	Sat Nov 23 10:21:40 2013 +0100
@@ -20,6 +20,7 @@
 from logging import debug, info, warning, error
 from sat.tools.jid  import JID
 from sat_frontends.quick_frontend.quick_utils import unescapePrivate
+from sat_frontends.quick_frontend.constants import Const
 
 
 class QuickChat(object):
@@ -107,7 +108,7 @@
         def onHistoryError(err):
             error (_("Can't get history"))
 
-        if self.target.startswith(const_PRIVATE_PREFIX):
+        if self.target.startswith(Const.PRIVATE_PREFIX):
             target = unescapePrivate(self.target)
         else:
             target = self.target.bare
@@ -116,9 +117,9 @@
 
     def _get_nick(self, jid):
         """Return nick of this jid when possible"""
-        if self.target.startswith(const_PRIVATE_PREFIX):
+        if self.target.startswith(Const.PRIVATE_PREFIX):
             unescaped = unescapePrivate(self.target)
-            if jid.startswith(const_PRIVATE_PREFIX) or unescaped.bare == jid.bare:
+            if jid.startswith(Const.PRIVATE_PREFIX) or unescaped.bare == jid.bare:
                 return unescaped.resource
         return jid.resource if self.type == "group" else (self.host.contact_list.getCache(jid,'nick') or self.host.contact_list.getCache(jid,'name') or jid.node)
 
--- a/frontends/src/quick_frontend/quick_utils.py	Sun Nov 24 11:22:20 2013 +0100
+++ b/frontends/src/quick_frontend/quick_utils.py	Sat Nov 23 10:21:40 2013 +0100
@@ -19,15 +19,16 @@
 
 from sat.tools.jid  import JID
 from os.path import exists, splitext
+from sat_frontends.quick_frontend.constants import Const
 
 def escapePrivate(ori_jid):
     """Escape a private jid"""
-    return JID(const_PRIVATE_PREFIX + ori_jid.bare + '@' + ori_jid.resource)
+    return JID(Const.PRIVATE_PREFIX + ori_jid.bare + '@' + ori_jid.resource)
 
 def unescapePrivate(escaped_jid):
-    if not escaped_jid.startswith(const_PRIVATE_PREFIX):
+    if not escaped_jid.startswith(Const.PRIVATE_PREFIX):
         return escaped_jid
-    escaped_split = tuple(escaped_jid[len(const_PRIVATE_PREFIX):].split('@'))
+    escaped_split = tuple(escaped_jid[len(Const.PRIVATE_PREFIX):].split('@'))
     assert(len(escaped_split) == 3)
     return JID("%s@%s/%s" % escaped_split)
 
--- a/frontends/src/wix/constants.py	Sun Nov 24 11:22:20 2013 +0100
+++ b/frontends/src/wix/constants.py	Sat Nov 23 10:21:40 2013 +0100
@@ -1,17 +1,44 @@
-import sys,os.path
-import __builtin__
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+# Primitivus: a SAT frontend
+# Copyright (C) 2009, 2010, 2011, 2012, 2013  Jérôme Poisson (goffi@goffi.org)
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import sys
+import os.path
+import gettext
 import sat_frontends.wix
+from sat_frontends.quick_frontend import constants
 
+
+_ = gettext.gettext
 wix_root = os.path.dirname(sat_frontends.wix.__file__)
 
-__builtin__.__dict__['APP_NAME'] = "Wix"
-__builtin__.__dict__['LICENCE_PATH'] = os.path.join(wix_root,"COPYING")
+
+class Const(constants.Const):
 
-__builtin__.__dict__['msgOFFLINE']          = _("offline")
-__builtin__.__dict__['msgONLINE']           = _("online")
-__builtin__.__dict__['const_DEFAULT_GROUP'] = "Unclassed"
-__builtin__.__dict__['const_STATUS']        = [("", _("Online"), None),
-                                               ("chat", _("Free for chat"), "green"),
-                                               ("away", _("AFK"), "brown"),
-                                               ("dnd", _("DND"), "red"),
-                                               ("xa", _("Away"), "red")]
+    APP_NAME = "Wix"
+    LICENCE_PATH = os.path.join(wix_root, "COPYING")
+    msgOFFLINE = _("offline")
+    msgONLINE = _("online")
+    DEFAULT_GROUP = "Unclassed"
+    PRESENCE = [("", _("Online"), None),
+                ("chat", _("Free for chat"), "green"),
+                ("away", _("AFK"), "brown"),
+                ("dnd", _("DND"), "red"),
+                ("xa", _("Away"), "red")
+                ]
+
--- a/frontends/src/wix/contact_list.py	Sun Nov 24 11:22:20 2013 +0100
+++ b/frontends/src/wix/contact_list.py	Sat Nov 23 10:21:40 2013 +0100
@@ -19,6 +19,7 @@
 
 import wx
 from sat_frontends.quick_frontend.quick_contact_list import QuickContactList
+from sat_frontends.wix.constants import Const
 from logging import debug, info, error
 from cgi import escape
 from sat.tools.jid  import JID
@@ -113,7 +114,7 @@
         _show = self.getCache(jid,'show')
         if _show == None or _show == 'unavailable':
             return None
-        show =  filter(lambda x : x[0] == _show, const_STATUS)[0]
+        show = filter(lambda x : x[0] == _show, Const.PRESENCE)[0]
 
         #show[0]==shortcut
         #show[1]==human readable
--- a/frontends/src/wix/main_window.py	Sun Nov 24 11:22:20 2013 +0100
+++ b/frontends/src/wix/main_window.py	Sat Nov 23 10:21:40 2013 +0100
@@ -31,7 +31,7 @@
 import os.path
 from sat.tools.jid  import JID
 from logging import debug, info, warning, error
-import sat_frontends.wix.constants
+from sat_frontends.wix.constants import Const
 
 idCONNECT,\
 idDISCONNECT,\
@@ -72,7 +72,7 @@
 
         #ToolBar
         self.tools=self.CreateToolBar()
-        self.statusBox = wx.ComboBox(self.tools, -1, "Online", choices=[status[1] for status in const_STATUS],
+        self.statusBox = wx.ComboBox(self.tools, -1, "Online", choices=[status[1] for status in Const.PRESENCE],
                                       style=wx.CB_DROPDOWN | wx.CB_READONLY)
         self.tools.AddControl(self.statusBox)
         self.tools.AddSeparator()
@@ -122,7 +122,7 @@
         connectMenu.Append(idDISCONNECT, _("&Disconnect	CTRL-d"),_(" Disconnect from the server"))
         connectMenu.Append(idPARAM,_("&Parameters"),_(" Configure the program"))
         connectMenu.AppendSeparator()
-        connectMenu.Append(idABOUT,_("A&bout"),_(" About %s") % APP_NAME)
+        connectMenu.Append(idABOUT, _("A&bout"), _(" About %s") % Const.APP_NAME)
         connectMenu.Append(idEXIT,_("E&xit"),_(" Terminate the program"))
         contactMenu = wx.Menu()
         contactMenu.Append(idADD_CONTACT, _("&Add contact"),_(" Add a contact to your list"))
@@ -212,10 +212,10 @@
     def setStatusOnline(self, online=True):
         """enable/disable controls, must be called when local user online status change"""
         if online:
-            self.SetStatusText(msgONLINE)
+            self.SetStatusText(Const.msgONLINE)
             self.tools.Enable()
         else:
-            self.SetStatusText(msgOFFLINE)
+            self.SetStatusText(Const.msgOFFLINE)
             self.tools.Disable()
         return
 
@@ -351,7 +351,7 @@
         self.bridge.disconnect(self.profile)
 
     def __updateStatus(self):
-        show = filter(lambda x:x[1] == self.statusBox.GetValue(), const_STATUS)[0][0]
+        show = filter(lambda x:x[1] == self.statusBox.GetValue(), Const.PRESENCE)[0][0]
         status =  self.statusTxt.GetValue()
         self.bridge.setPresence(show=show, statuses={'default':status}, profile_key=self.profile)  #FIXME: manage multilingual statuses
 
@@ -367,15 +367,15 @@
 
     def onAbout(self, e):
         about = wx.AboutDialogInfo()
-        about.SetName(APP_NAME)
+        about.SetName(Const.APP_NAME)
         about.SetVersion (unicode(self.bridge.getVersion()))
         about.SetCopyright(u"(C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson aka Goffi")
         about.SetDescription( _(u"%(name)s is a SàT (Salut à Toi) frontend\n"+
-        u"%(name)s is based on WxPython, and is the standard graphic interface of SàT") % {'name':APP_NAME})
+        u"%(name)s is based on WxPython, and is the standard graphic interface of SàT") % {'name': Const.APP_NAME})
         about.SetWebSite(("http://www.goffi.org", "Goffi's non-hebdo (french)"))
         about.SetDevelopers([ "Goffi (Jérôme Poisson)"])
         try:
-            with open(LICENCE_PATH,"r") as licence:
+            with open(Const.LICENCE_PATH, "r") as licence:
                 about.SetLicence(''.join(licence.readlines()))
         except:
             pass