Skip to content

Commit

Permalink
centralize __version__ and running installed
Browse files Browse the repository at this point in the history
  • Loading branch information
ederag committed Jan 28, 2020
1 parent 9a0f0ba commit 4417b73
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 28 deletions.
19 changes: 9 additions & 10 deletions src/hamster-cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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__':
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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"):
Expand Down
13 changes: 13 additions & 0 deletions src/hamster/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 7 additions & 8 deletions src/hamster/lib/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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()
Expand Down
10 changes: 4 additions & 6 deletions src/hamster/lib/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 5 additions & 4 deletions src/hamster/storage/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions wscript
Original file line number Diff line number Diff line change
@@ -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 = '.'
Expand Down

0 comments on commit 4417b73

Please sign in to comment.