Skip to content

Commit

Permalink
Follow XDG base directory specification
Browse files Browse the repository at this point in the history
To become a better citizen, we now follow the XDG base directory
specifications - this makes it easier to configure where we dump data,
and helps avoid cluttering users' home directories.

We still read ~/.webmacs, but we give a deprecation warning - we
intend to ignore this directory in the future.
  • Loading branch information
TLATER committed Feb 11, 2019
1 parent 034a46a commit 5029760
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/user_configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ User configuration
==================

**webmacs** can be configured by writing Python code. The files should live in
a ``~/.webmacs/init`` directory, starting with an ``__init__.py`` file.
a ``$XDG_CONFIG_HOME/webmacs/init`` directory, starting with an ``__init__.py`` file.

If this file exists, it will be loaded early in the application.

Expand Down
3 changes: 2 additions & 1 deletion tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from webmacs import (windows, buffers, WINDOWS_HANDLER, current_buffer,
current_window, current_minibuffer)
from webmacs import variables as wvariables
from webmacs.main import XDG_DATA_HOME
from webmacs.webbuffer import create_buffer
from webmacs.window import Window
from webmacs.webbuffer import close_buffer
Expand Down Expand Up @@ -78,7 +79,7 @@ def qapp(wm, qapp_args):
_app_requires()
global _app
# TODO FIXME use another path for tests
conf_path = os.path.join(os.path.expanduser("~"), ".webmacs")
conf_path = os.path.join(XDG_DATA_HOME, "webmacs")
_app = Application(conf_path, ["webmacs"])
return _app

Expand Down
46 changes: 39 additions & 7 deletions webmacs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@
from . import variables, filter_webengine_output


XDG_CONFIG_HOME = os.getenv("XDG_CONFIG_HOME",
os.path.expanduser("~/.config"))

XDG_DATA_HOME = os.getenv("XDG_DATA_HOME",
os.path.expanduser("~/.local/share"))


log_to_disk = variables.define_variable(
"log-to-disk-max-files",
"Maximum number of log files to keep. Log files are stored in"
" ~/.webmacs/logs. Setting this to 0 will deactivate file logging"
" completely.",
" $XDG_DATA_HOME/webmacs/logs. Setting this to 0 will deactivate file "
" logging completely.",
0,
type=variables.Int(min=0),
)
Expand Down Expand Up @@ -130,6 +137,11 @@ def parse_args(argv=None):
" If the given instance name is the empty string, an"
" automatically generated name will be used.")

parser.add_argument("-c", "--config",
default=os.path.join(XDG_CONFIG_HOME, "webmacs"),
help="The path from which to load the configuration "
"module.")

parser.add_argument("--list-instances", action="store_true",
help="List running instances and exit.")

Expand Down Expand Up @@ -236,9 +248,29 @@ def main():
if n.isdigit()]
opts.instance = str(max(uniq) + 1) if uniq else "1"

conf_path = os.path.join(os.path.expanduser("~"), ".webmacs")
conf_path = opts.config
if not os.path.isdir(conf_path):
os.makedirs(conf_path)
deprecated = os.path.expanduser("~/.webmacs")
if os.path.isdir(deprecated):
# Since logging has not been setup yet, use manual print
print("Warning: '{}' as an init directory has been deprecated. "
"Please use '{}' or configure it using '-c' instead."
.format(deprecated, conf_path), file=sys.stderr)
conf_path = deprecated
else:
os.makedirs(conf_path)

data_path = os.path.join(XDG_DATA_HOME, "webmacs")
if not os.path.isdir(data_path):
deprecated = os.path.expanduser("~/.webmacs")
if os.path.isdir(deprecated):
# Since logging has not been setup yet, use manual print
print("Warning: '{}' as a data directory has been deprecated. "
"Please move your profile and adblock directories to '{}'."
.format(deprecated, data_path), file=sys.stderr)
data_path = deprecated
else:
os.makedirs(data_path)

out_filter = filter_webengine_output.make_filter()

Expand Down Expand Up @@ -271,7 +303,7 @@ def main():
"Error reading the user configuration."
)

app = Application(conf_path, [
app = Application(data_path, [
# The first argument passed to the QApplication args defines
# the x11 property WM_CLASS.
"webmacs" if opts.instance == "default"
Expand All @@ -290,13 +322,13 @@ def main():
user_init.init(opts)
except Exception:
_handle_user_init_error(
conf_path,
data_path,
"Error executing user init function in %s."
% user_init.__file__
)

if log_to_disk.value > 0:
setup_logging_on_disk(os.path.join(conf_path, "logs"),
setup_logging_on_disk(os.path.join(data_path, "logs"),
backup_count=log_to_disk.value)
app.post_init()
signal_wakeup(app)
Expand Down

0 comments on commit 5029760

Please sign in to comment.