From e3febf46ebac5188487b43efa609ad66afabb5b5 Mon Sep 17 00:00:00 2001 From: Roman Dobosz Date: Tue, 15 Nov 2016 10:32:48 +0100 Subject: [PATCH] Changed default config file location to XDG_CONFIG_HOME --- README.rst | 19 ++++++++++++------- src/models/m_config.py | 37 +++++++++++++++++++++++++++++++------ src/models/m_main.py | 8 ++------ 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/README.rst b/README.rst index 254a833..e564975 100644 --- a/README.rst +++ b/README.rst @@ -143,11 +143,16 @@ NOTES ===== Catalog file is plain sqlite database (optionally compressed with bzip2). All -images are stored in ~/.pygtktalog/images directory. Names for images are -generated sha512 hash from image file itself. There is small possibility for two -identical hash for different image files. However, no images are overwritten. -Thumbnail filename for each image is simply concatenation of image filename in -images directory and '_t' string. +images are stored in ``[CONFIG_PATH]/images`` directory, where ``[CONFIG_PATH]`` +is one of the following location: + +- ``$XDG_CONFIG_HOME/pygtktalog`` +- ``$HOME/.pytgtktalog`` + +Names for images are generated sha512 hash from image file itself. There is +small possibility for two identical hash for different image files. However, no +images are overwritten. Thumbnail filename for each image is simply +concatenation of image filename in images directory and ``_t`` string. There is also converter from old database to new for internal use only. In public release there will be no other formats so it will be useless, and @@ -156,8 +161,8 @@ images without big image will be lost. There are serious changes with application design, and I decided, that is better to keep media unpacked on disk, instead of pack it every time with save and unpack with open methods. New design prevent from deleting any file from media directory (placed in -~/.pygtktalog/images). Functionality for exporting images and corresponding db -file is planned. +``[CONFIG_PATH]/images``). Functionality for exporting images and corresponding +db file is planned. LICENSE ======= diff --git a/src/models/m_config.py b/src/models/m_config.py index 5e1c41d..0bf856f 100644 --- a/src/models/m_config.py +++ b/src/models/m_config.py @@ -156,15 +156,10 @@ class ConfigModel(Model): dstring = ('cd','ejectapp','imgprog') - try: - path = os.path.join(os.environ['HOME'], ".pygtktalog") - except KeyError: - raise KeyError, "Cannot stat path for current user home!" - - path = os.path.join(path, "config.ini") def __init__(self): Model.__init__(self) + self._set_config_path() self.category_tree = gtk.ListStore(gobject.TYPE_STRING) self.refresh_ext() @@ -332,6 +327,36 @@ class ConfigModel(Model): self.search_history = self.search_history[:self.HISTORY_MAX] return + def _set_config_path(self): + """ + Look for configuration files in following location: + ~/.config/pygtktalog/config.ini + ~/.pygtktalog/config.ini + + Note, that ~/.config is the default XDG defined location for the + configuration. Apropriate XDG variable wil be checked first, in order + to locate configuration directory. + """ + xdg_path = os.getenv('XDG_CONFIG_HOME', + os.path.expanduser("~/.config")) + # Default config file location + cfg_file = os.path.join(xdg_path, "pygtktalog/config.ini") + + # Check for other possibilities + for path in (os.path.join(xdg_path, "pygtktalog/config.ini"), + os.path.expanduser("~/.pygtktalog/config.ini")): + if os.path.exists(path): + cfg_file = path + break + + if not os.path.exists(cfg_file): + try: + os.makedirs(os.path.dirname(cfg_file)) + except OSError: + pass + + self.path = cfg_file + def __str__(self): """show prefs in string way""" string = "[varname]\tvalue\n" diff --git a/src/models/m_main.py b/src/models/m_main.py index e328d6d..c753131 100644 --- a/src/models/m_main.py +++ b/src/models/m_main.py @@ -174,11 +174,8 @@ class MainModel(ModelMT): # - #rrggbb self.tag_cloud = [] - try: - path = os.path.join(os.environ['HOME'], ".pygtktalog") - imgpath = os.path.join(path, "images") - except KeyError: - raise KeyError, "Cannot stat path for current user home!" + path = os.path.dirname(self.config.path) + imgpath = os.path.join(path, "images") if os.path.exists(path): if not os.path.isdir(path): @@ -2012,4 +2009,3 @@ class MainModel(ModelMT): #name = name.replace('?','_') return name -