From fe2f1ada04f50d53289ed93ebed2b36daa638e87 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 31 Dec 2010 19:46:08 -0600 Subject: [PATCH] Applying changes from template --- src/constants.py | 1 + src/dialcentral_qt.py | 6 ++-- src/util/io.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++++- src/util/linux.py | 74 +++++++++++++++++++++++++++++++++++++++++--- src/util/tp_utils.py | 2 +- support/builddeb.py | 22 ++++--------- 6 files changed, 162 insertions(+), 25 deletions(-) diff --git a/src/constants.py b/src/constants.py index 164d117..b2fa223 100644 --- a/src/constants.py +++ b/src/constants.py @@ -10,3 +10,4 @@ _user_settings_ = "%s/settings.ini" % _data_path_ _custom_notifier_settings_ = "%s/notifier.ini" % _data_path_ _user_logpath_ = "%s/%s.log" % (_data_path_, __app_name__) _notifier_logpath_ = "%s/notifier.log" % _data_path_ +IS_MAEMO = True diff --git a/src/dialcentral_qt.py b/src/dialcentral_qt.py index c935e68..820d6d7 100755 --- a/src/dialcentral_qt.py +++ b/src/dialcentral_qt.py @@ -21,7 +21,6 @@ import session _moduleLogger = logging.getLogger(__name__) -IS_MAEMO = True class Dialcentral(object): @@ -359,7 +358,7 @@ class MainWindow(object): for tabIndex, (tabTitle, tabIcon) in enumerate( zip(self._TAB_TITLES, self._TAB_ICONS) ): - if IS_MAEMO: + if constants.IS_MAEMO: icon = self._app.get_icon(tabIcon) if icon is None: self._tabWidget.addTab(self._tabsContents[tabIndex].toplevel, tabTitle) @@ -413,7 +412,7 @@ class MainWindow(object): self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w")) self._closeWindowAction.triggered.connect(self._on_close_window) - if IS_MAEMO: + if constants.IS_MAEMO: fileMenu = self._window.menuBar().addMenu("&File") fileMenu.addAction(self._loginTabAction) fileMenu.addAction(self._refreshTabAction) @@ -477,6 +476,7 @@ class MainWindow(object): def close(self): for child in self.walk_children(): + child.window.destroyed.disconnect(self._on_child_close) child.close() self._window.close() diff --git a/src/util/io.py b/src/util/io.py index aece2dd..aac896d 100644 --- a/src/util/io.py +++ b/src/util/io.py @@ -7,7 +7,12 @@ import os import pickle import contextlib import itertools -import functools +import codecs +import csv +try: + import cStringIO as StringIO +except ImportError: + import StringIO @contextlib.contextmanager @@ -127,3 +132,78 @@ def relpath(p1, p2): return os.path.join(*relParts) else: return "."+os.sep + + +class UTF8Recoder(object): + """ + Iterator that reads an encoded stream and reencodes the input to UTF-8 + """ + def __init__(self, f, encoding): + self.reader = codecs.getreader(encoding)(f) + + def __iter__(self): + return self + + def next(self): + return self.reader.next().encode("utf-8") + + +class UnicodeReader(object): + """ + A CSV reader which will iterate over lines in the CSV file "f", + which is encoded in the given encoding. + """ + + def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds): + f = UTF8Recoder(f, encoding) + self.reader = csv.reader(f, dialect=dialect, **kwds) + + def next(self): + row = self.reader.next() + return [unicode(s, "utf-8") for s in row] + + def __iter__(self): + return self + +class UnicodeWriter(object): + """ + A CSV writer which will write rows to CSV file "f", + which is encoded in the given encoding. + """ + + def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds): + # Redirect output to a queue + self.queue = StringIO.StringIO() + self.writer = csv.writer(self.queue, dialect=dialect, **kwds) + self.stream = f + self.encoder = codecs.getincrementalencoder(encoding)() + + def writerow(self, row): + self.writer.writerow([s.encode("utf-8") for s in row]) + # Fetch UTF-8 output from the queue ... + data = self.queue.getvalue() + data = data.decode("utf-8") + # ... and reencode it into the target encoding + data = self.encoder.encode(data) + # write to the target stream + self.stream.write(data) + # empty queue + self.queue.truncate(0) + + def writerows(self, rows): + for row in rows: + self.writerow(row) + + +def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs): + # csv.py doesn't do Unicode; encode temporarily as UTF-8: + csv_reader = csv.reader(utf_8_encoder(unicode_csv_data), + dialect=dialect, **kwargs) + for row in csv_reader: + # decode UTF-8 back to Unicode, cell by cell: + yield [unicode(cell, 'utf-8') for cell in row] + + +def utf_8_encoder(unicode_csv_data): + for line in unicode_csv_data: + yield line.encode('utf-8') diff --git a/src/util/linux.py b/src/util/linux.py index 4837f2a..4e77445 100644 --- a/src/util/linux.py +++ b/src/util/linux.py @@ -1,13 +1,79 @@ #!/usr/bin/env python +import os import logging +try: + from xdg import BaseDirectory as _BaseDirectory + BaseDirectory = _BaseDirectory +except ImportError: + BaseDirectory = None + + +_moduleLogger = logging.getLogger(__name__) + + +_libc = None + def set_process_name(name): try: # change process name for killall - import ctypes - libc = ctypes.CDLL('libc.so.6') - libc.prctl(15, name, 0, 0, 0) + global _libc + if _libc is None: + import ctypes + _libc = ctypes.CDLL('libc.so.6') + _libc.prctl(15, name, 0, 0, 0) except Exception, e: - logging.warning('Unable to set processName: %s" % e') + _moduleLogger.warning('Unable to set processName: %s" % e') + + +def get_new_resource(resourceType, resource, name): + if BaseDirectory is not None: + if resourceType == "data": + base = BaseDirectory.xdg_data_home + if base == "/usr/share/mime": + # Ugly hack because somehow Maemo 4.1 seems to be set to this + base = os.path.join(os.path.expanduser("~"), ".%s" % resource) + elif resourceType == "config": + base = BaseDirectory.xdg_config_home + elif resourceType == "cache": + base = BaseDirectory.xdg_cache_home + else: + raise RuntimeError("Unknown type: "+resourceType) + else: + base = os.path.join(os.path.expanduser("~"), ".%s" % resource) + + filePath = os.path.join(base, resource, name) + dirPath = os.path.dirname(filePath) + if not os.path.exists(dirPath): + # Looking before I leap to not mask errors + os.makedirs(dirPath) + + return filePath + + +def get_existing_resource(resourceType, resource, name): + if BaseDirectory is not None: + if resourceType == "data": + base = BaseDirectory.xdg_data_home + elif resourceType == "config": + base = BaseDirectory.xdg_config_home + elif resourceType == "cache": + base = BaseDirectory.xdg_cache_home + else: + raise RuntimeError("Unknown type: "+resourceType) + else: + base = None + + if base is not None: + finalPath = os.path.join(base, name) + if os.path.exists(finalPath): + return finalPath + + altBase = os.path.join(os.path.expanduser("~"), ".%s" % resource) + finalPath = os.path.join(altBase, name) + if os.path.exists(finalPath): + return finalPath + else: + raise RuntimeError("Resource not found: %r" % ((resourceType, resource, name), )) diff --git a/src/util/tp_utils.py b/src/util/tp_utils.py index c40f4fe..7c55c42 100644 --- a/src/util/tp_utils.py +++ b/src/util/tp_utils.py @@ -25,7 +25,7 @@ class WasMissedCall(object): self._didReport = False self._onTimeout = gobject_utils.Timeout(self._on_timeout) - self._onTimeout.start(seconds=10) + self._onTimeout.start(seconds=60) chan[telepathy.interfaces.CHANNEL_INTERFACE_GROUP].connect_to_signal( "MembersChanged", diff --git a/support/builddeb.py b/support/builddeb.py index a44cd9e..363e853 100755 --- a/support/builddeb.py +++ b/support/builddeb.py @@ -132,8 +132,8 @@ def build_package(distribution): p["/usr/share/icons/hicolor/64x64/hildon"] = ["64x64-dialcentral.png|dialcentral.png"] p["/usr/share/icons/hicolor/scalable/hildon"] = ["scale-dialcentral.png|dialcentral.png"] + print p if distribution == "debian": - print p print p.generate( version="%s-%s" % (__version__, __build__), changelog=__changelog__, @@ -142,9 +142,7 @@ def build_package(distribution): changes=False, dsc=False, ) - print "Building for %s finished" % distribution else: - print p print p.generate( version="%s-%s" % (__version__, __build__), changelog=__changelog__, @@ -153,20 +151,12 @@ def build_package(distribution): changes=True, dsc=True, ) - print "Building for %s finished" % distribution + print "Building for %s finished" % distribution if __name__ == "__main__": - if len(sys.argv) > 1: - try: - import optparse - except ImportError: - optparse = None - - if optparse is not None: - parser = optparse.OptionParser() - (commandOptions, commandArgs) = parser.parse_args() + if len(sys.argv) == 1: + distribution = "fremantle" else: - commandArgs = None - commandArgs = ["diablo"] - build_package(commandArgs[0]) + distribution = sys.argv[1] + build_package(distribution) -- 1.7.9.5