From 4417b73334e5c34ae6c6998b5523fbccaf3568f7 Mon Sep 17 00:00:00 2001 From: ederag Date: Tue, 28 Jan 2020 17:30:16 +0100 Subject: [PATCH] centralize __version__ and running installed --- src/hamster-cli.py | 19 +++++++++---------- src/hamster/__init__.py | 13 +++++++++++++ src/hamster/lib/configuration.py | 15 +++++++-------- src/hamster/lib/i18n.py | 10 ++++------ src/hamster/storage/db.py | 9 +++++---- wscript | 3 +++ 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/hamster-cli.py b/src/hamster-cli.py index 632ee785b..cfefe3ef1 100644 --- a/src/hamster-cli.py +++ b/src/hamster-cli.py @@ -31,6 +31,8 @@ from gi.repository import Gtk as gtk from gi.repository import Gio as gio +import hamster + from hamster import client, reports from hamster import logger as hamster_logger from hamster.overview import Overview @@ -165,15 +167,9 @@ class HamsterCli(object): def __init__(self): self.storage = client.Storage() - + # TODO: no longer used (should remove overview, about, prefs, add) def _launch_window(self, window_name): - try: - from hamster import defs - running_installed = True - except: - running_installed = False - - if running_installed: + if hamster.installed: import dbus bus = dbus.SessionBus() server = bus.get_object("org.gnome.Hamster.WindowServer", @@ -377,8 +373,7 @@ def _list(self, start_time, end_time, search=""): def version(self): - from hamster.lib.configuration import runtime - print(runtime.version) + print(hamster.__version__) if __name__ == '__main__': @@ -420,6 +415,7 @@ def version(self): look for an activity matching terms 'pancakes` between 1st and 30st August 2012. Will check against activity, category, description and tags """) + hamster_client = HamsterCli() app = Hamster() logger.debug("app instanciated") @@ -447,6 +443,9 @@ def version(self): # hamster_logger for the rest hamster_logger.setLevel(args.log_level) + if not hamster.installed: + logger.info("Running in devel mode") + action = args.action if action in ("about", "overview", "prefs"): diff --git a/src/hamster/__init__.py b/src/hamster/__init__.py index 9f6790180..ac0098799 100644 --- a/src/hamster/__init__.py +++ b/src/hamster/__init__.py @@ -9,6 +9,19 @@ logger = default_logger(__name__) +try: + # defs.py is created by waf from defs.py.in + from hamster import defs + __version__ = defs.VERSION + installed = True +except ImportError: + # if defs is not there, we are running from sources + from subprocess import getstatusoutput + rc, output = getstatusoutput("git describe --tags --always --dirty=+") + __version__ = "3.0.0-alpha" if rc else "{} (uninstalled)".format(output) + installed = False + del getstatusoutput, rc, output + # cleanup namespace del default_logger del gtk # performance is retained diff --git a/src/hamster/lib/configuration.py b/src/hamster/lib/configuration.py index ae9825329..210e236d3 100644 --- a/src/hamster/lib/configuration.py +++ b/src/hamster/lib/configuration.py @@ -34,6 +34,8 @@ from gi.repository import GObject as gobject from gi.repository import Gtk as gtk +import hamster + from hamster.lib import datetime as dt @@ -122,17 +124,14 @@ class RuntimeStore(Singleton): storage = None def __init__(self): - try: - from hamster import defs + self.version = hamster.__version__ + if hamster.installed: + from hamster import defs # only available when running installed self.data_dir = os.path.join(defs.DATA_DIR, "hamster") - self.version = defs.VERSION - except: - # if defs is not there, we are running from sources + else: + # running from sources module_dir = os.path.dirname(os.path.realpath(__file__)) self.data_dir = os.path.join(module_dir, '..', '..', '..', 'data') - from subprocess import getstatusoutput - rc, output = getstatusoutput("git describe --tags --always --dirty=+") - self.version = "" if rc else output + " (uninstalled)" self.data_dir = os.path.realpath(self.data_dir) self.storage = Storage() diff --git a/src/hamster/lib/i18n.py b/src/hamster/lib/i18n.py index dd3580426..7bb897735 100644 --- a/src/hamster/lib/i18n.py +++ b/src/hamster/lib/i18n.py @@ -2,21 +2,19 @@ import os import locale, gettext +import hamster + def setup_i18n(): #determine location of po files - try: - from hamster import defs - except: - defs = None - # to avoid confusion, we won't translate unless running installed # reason for that is that bindtextdomain is expecting # localedir/language/LC_MESSAGES/domain.mo format, but we have # localedir/language.mo at it's best (after build) # and there does not seem to be any way to run straight from sources - if defs: + if hamster.installed: + from hamster import defs # only available when running installed locale_dir = os.path.realpath(os.path.join(defs.DATA_DIR, "locale")) for module in (locale,gettext): diff --git a/src/hamster/storage/db.py b/src/hamster/storage/db.py index e36dd06c2..3cf4bb1e3 100644 --- a/src/hamster/storage/db.py +++ b/src/hamster/storage/db.py @@ -34,6 +34,7 @@ print("Could not import gio - requires pygobject. File monitoring will be disabled") gio = None +import hamster from hamster.lib import datetime as dt from hamster.lib.configuration import conf from hamster.lib.fact import Fact @@ -131,11 +132,11 @@ def __init_db_file(self, database_dir): break if not os.path.exists(db_path): # make a copy of the empty template hamster.db - try: - from hamster import defs + if hamster.installed: + from hamster import defs # only available when running installed data_dir = os.path.join(defs.DATA_DIR, "hamster") - except: - # if defs is not there, we are running from sources + else: + # running from sources module_dir = os.path.dirname(os.path.realpath(__file__)) if os.path.exists(os.path.join(module_dir, "data")): # running as flask app. XXX - detangle diff --git a/wscript b/wscript index b7ea6579a..da6fb90b1 100644 --- a/wscript +++ b/wscript @@ -1,7 +1,10 @@ # -*- python -*- + +# slight code duplication with hamster/__init__.py, but this is finally cleaner. from subprocess import getstatusoutput rc, output = getstatusoutput("git describe --tags --always --dirty=+") VERSION = '3.0.0-alpha' if rc else output + APPNAME = 'hamster' top = '.'