From 7d941ce2fe7b6cb4a796e7d25ff25c083aeba561 Mon Sep 17 00:00:00 2001 From: Christian Jacobs Date: Sun, 29 Nov 2015 22:25:41 +0000 Subject: [PATCH] Write configuration files to ~/.config to keep the user's home directory uncluttered. --- CHANGELOG.md | 1 + bin/pyqso | 9 +++++++-- pyqso/adif.py | 2 +- pyqso/dx_cluster.py | 5 ++++- pyqso/logbook.py | 2 +- pyqso/menu.py | 2 +- pyqso/preferences_dialog.py | 15 +++++++++------ pyqso/record_dialog.py | 6 +++--- 8 files changed, 27 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f85f3fc..d9cba81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - Various code cleanups (thanks to András Veres-Szentkirályi). - Brought the list of valid modes up-to-date. - Updated the list of bands and their frequency ranges. +- Configuration files are now written to ~/.config to keep the user's home directory uncluttered. ## [0.2] - 2015-03-07 ### Added diff --git a/bin/pyqso b/bin/pyqso index 0929072..df4f8ee 100755 --- a/bin/pyqso +++ b/bin/pyqso @@ -53,11 +53,16 @@ class PyQSO(Gtk.Window): # Call the constructor of the super class (Gtk.Window) Gtk.Window.__init__(self, title="PyQSO") - # Get any application-specific preferences from the configuration file + # Check that the directory for holding PyQSO configuration files exists. If it doesn't, create it now. + if not os.path.exists(os.path.expanduser('~/.config/pyqso')): + os.makedirs(os.path.expanduser('~/.config/pyqso')) + + # Get any application-specific preferences from the configuration file config = configparser.ConfigParser() + # Check that the configuration file actually exists (and is readable) # otherwise, we will resort to the defaults. - have_config = (config.read(os.path.expanduser("~/.pyqso.ini")) != []) + have_config = (config.read(os.path.expanduser("~/.config/pyqso/preferences.ini")) != []) self.set_size_request(800, 600) # Default to an 800 x 600 resolution. self.set_position(Gtk.WindowPosition.CENTER) diff --git a/pyqso/adif.py b/pyqso/adif.py index b1b7138..f82b9a9 100644 --- a/pyqso/adif.py +++ b/pyqso/adif.py @@ -235,7 +235,7 @@ class ADIF: # ADIF-related configuration options config = configparser.ConfigParser() - have_config = (config.read(expanduser('~/.pyqso.ini')) != []) + have_config = (config.read(expanduser('~/.config/pyqso/preferences.ini')) != []) (section, option) = ("adif", "merge_comment") if(have_config and config.has_option(section, option) and config.get(section, option) == "True"): merge_comment = True diff --git a/pyqso/dx_cluster.py b/pyqso/dx_cluster.py index f3fcee5..f21b0e8 100644 --- a/pyqso/dx_cluster.py +++ b/pyqso/dx_cluster.py @@ -27,7 +27,7 @@ import os.path from pyqso.telnet_connection_dialog import * -BOOKMARKS_FILE = os.path.expanduser('~/.pyqso_bookmarks.ini') +BOOKMARKS_FILE = os.path.expanduser('~/.config/pyqso/bookmarks.ini') class DXCluster(Gtk.VBox): """ A tool for connecting to a DX cluster (specifically Telnet-based DX clusters). """ @@ -146,6 +146,9 @@ class DXCluster(Gtk.VBox): config.set(host, "username", username) config.set(host, "password", password) + # Write the bookmarks to file. + if not os.path.exists(os.path.expanduser('~/.config/pyqso')): + os.makedirs(os.path.expanduser('~/.config/pyqso')) with open(BOOKMARKS_FILE, 'w') as f: config.write(f) diff --git a/pyqso/logbook.py b/pyqso/logbook.py index 807a0c2..66c1d00 100644 --- a/pyqso/logbook.py +++ b/pyqso/logbook.py @@ -499,7 +499,7 @@ class Logbook(Gtk.Notebook): column.connect("clicked", self.sort_log, i+1) config = configparser.ConfigParser() - have_config = (config.read(expanduser('~/.pyqso.ini')) != []) + have_config = (config.read(expanduser('~/.config/pyqso/preferences.ini')) != []) (section, option) = ("view", AVAILABLE_FIELD_NAMES_ORDERED[i].lower()) if(have_config and config.has_option(section, option)): column.set_visible(config.get(section, option) == "True") diff --git a/pyqso/menu.py b/pyqso/menu.py index 8454105..39d5eb3 100644 --- a/pyqso/menu.py +++ b/pyqso/menu.py @@ -210,7 +210,7 @@ class Menu(Gtk.MenuBar): mitem_toolbox = Gtk.CheckMenuItem("Toolbox") config = configparser.ConfigParser() - have_config = (config.read(os.path.expanduser('~/.pyqso.ini')) != []) + have_config = (config.read(os.path.expanduser('~/.config/pyqso/preferences.ini')) != []) (section, option) = ("general", "show_toolbox") if(have_config and config.has_option(section, option)): mitem_toolbox.set_active(config.get(section, option) == "True") diff --git a/pyqso/preferences_dialog.py b/pyqso/preferences_dialog.py index 8586289..9f46bdc 100644 --- a/pyqso/preferences_dialog.py +++ b/pyqso/preferences_dialog.py @@ -31,6 +31,8 @@ except ImportError: from pyqso.adif import * +PREFERENCES_FILE = os.path.expanduser("~/.config/pyqso/preferences.ini") + class PreferencesDialog(Gtk.Dialog): """ A dialog to specify the PyQSO preferences. """ @@ -102,7 +104,8 @@ class PreferencesDialog(Gtk.Dialog): for key in list(records_data.keys()): config.set("records", key.lower(), str(records_data[key])) - with open(os.path.expanduser('~/.pyqso.ini'), 'w') as f: + # Write the preferences to file. + with open(os.path.expanduser(PREFERENCES_FILE), 'w') as f: config.write(f) return @@ -118,7 +121,7 @@ class GeneralPage(Gtk.VBox): # Remember that the have_config conditional in the PyQSO class may be out-of-date the next time the user opens up the preferences dialog # because a configuration file may have been created after launching the application. Let's check to see if one exists again... config = configparser.ConfigParser() - have_config = (config.read(os.path.expanduser('~/.pyqso.ini')) != []) + have_config = (config.read(PREFERENCES_FILE) != []) self.sources = {} @@ -153,7 +156,7 @@ class ViewPage(Gtk.VBox): Gtk.VBox.__init__(self, spacing=2) config = configparser.ConfigParser() - have_config = (config.read(os.path.expanduser('~/.pyqso.ini')) != []) + have_config = (config.read(PREFERENCES_FILE) != []) self.sources = {} @@ -205,7 +208,7 @@ class HamlibPage(Gtk.VBox): Gtk.VBox.__init__(self, spacing=2) config = configparser.ConfigParser() - have_config = (config.read(os.path.expanduser('~/.pyqso.ini')) != []) + have_config = (config.read(PREFERENCES_FILE) != []) self.sources = {} @@ -289,7 +292,7 @@ class RecordsPage(Gtk.VBox): # Remember that the have_config conditional in the PyQSO class may be out-of-date the next time the user opens up the preferences dialog # because a configuration file may have been created after launching the application. Let's check to see if one exists again... config = configparser.ConfigParser() - have_config = (config.read(os.path.expanduser('~/.pyqso.ini')) != []) + have_config = (config.read(PREFERENCES_FILE) != []) self.sources = {} @@ -491,7 +494,7 @@ class ADIFPage(Gtk.VBox): # Remember that the have_config conditional in the PyQSO class may be out-of-date the next time the user opens up the preferences dialog # because a configuration file may have been created after launching the application. Let's check to see if one exists again... config = configparser.ConfigParser() - have_config = (config.read(os.path.expanduser('~/.pyqso.ini')) != []) + have_config = (config.read(PREFERENCES_FILE) != []) self.sources = {} diff --git a/pyqso/record_dialog.py b/pyqso/record_dialog.py index 6a59b93..e077259 100644 --- a/pyqso/record_dialog.py +++ b/pyqso/record_dialog.py @@ -55,7 +55,7 @@ class RecordDialog(Gtk.Dialog): # Check if a configuration file is present, since we might need it to set up the rest of the dialog. config = configparser.ConfigParser() - have_config = (config.read(expanduser('~/.pyqso.ini')) != []) + have_config = (config.read(expanduser('~/.config/pyqso/preferences.ini')) != []) ## QSO DATA FRAME qso_frame = Gtk.Frame() @@ -517,7 +517,7 @@ class RecordDialog(Gtk.Dialog): # Get the database name. config = configparser.ConfigParser() - have_config = (config.read(expanduser('~/.pyqso.ini')) != []) + have_config = (config.read(expanduser('~/.config/pyqso/preferences.ini')) != []) try: if(have_config and config.has_option("records", "callsign_database")): database = config.get("records", "callsign_database") @@ -588,7 +588,7 @@ class RecordDialog(Gtk.Dialog): # Check if a configuration file is present. config = configparser.ConfigParser() - have_config = (config.read(expanduser('~/.pyqso.ini')) != []) + have_config = (config.read(expanduser('~/.config/pyqso/preferences.ini')) != []) # Do we want to use UTC or the computer's local time? (section, option) = ("records", "use_utc")