Skip to content

Commit 7f12f31

Browse files
committed
Change default location for config files to ~/.config/wpm/ (Close #42)
1 parent 9b7d421 commit 7f12f31

File tree

6 files changed

+25
-14
lines changed

6 files changed

+25
-14
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ GPATH
88
GRTAGS
99
GTAGS
1010
stats
11+
.mypy_cache/
12+
.vscode/

README.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ zero-based integer will be used.
142142
Format of race history
143143
----------------------
144144

145-
wpm will save scores in a CSV file in `~/.wpm.csv`. This file can be loaded
145+
wpm will save scores in a CSV file in `~/.config/wpm/wpm.csv`. This file can be loaded
146146
directly into Excel. It uses the same format as TypeRacer, with the addition of
147147
a few extra columns at the end. That means is should be possible to use
148148
existing TypeRacer score history tools with this file with minor modifications.
@@ -164,7 +164,7 @@ tag str A user supplied tag for that score (e.g., keyboard)
164164
========== ======== =======================================================
165165

166166
Should there be any problem saving or loading the score history, it will copy
167-
the existing file into `~/.wpm.csv.backup` and create a new one.
167+
the existing file into `~/.config/wpm/wpm.csv.backup` and create a new one.
168168

169169
Tagging races
170170
-------------
@@ -185,10 +185,10 @@ grouped by each tag. It shows things like the average over time, along with
185185
confidence and prediction intervals. An item like `n-10` means "the last 10
186186
games".
187187

188-
The ~/.wpmrc file
188+
The wpmrc file
189189
-----------------
190190

191-
The first time you start wpm, it writes a `.wpmrc` file to your home directory.
191+
The first time you start wpm, it writes a `wpmrc` file to `~/.config/wpm/wpmrc`.
192192
It contains user settings that you can change. They are given in the table
193193
below.
194194

wpm/commandline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def parse_args():
5151
argp.add_argument("--cpm", default=False, action="store_true",
5252
help="Shows CPM instead of WPM in stats")
5353

54-
argp.add_argument("--stats-file", default="~/.wpm.csv", type=str,
54+
argp.add_argument("--stats-file", default="~/.config/wpm/wpm.csv", type=str,
5555
help="File to record score history to (CSV format)")
5656

5757
argp.add_argument("--id", "-i", default=None, type=int,

wpm/config.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ def int_tuple(s):
3737
except ValueError:
3838
raise ConfigError("Required format is (integer, integer): %s" % s)
3939

40+
def get_config_directory():
41+
xdg_config_home = os.getenv("XDG_CONFIG_HOME")
42+
if xdg_config_home == "":
43+
xdg_config_home = os.path.join(os.getenv("HOME"), ".config")
44+
return os.path.join(xdg_config_home, "wpm")
45+
4046
DEFAULTS = {
4147
"curses": {
4248
"escdelay": (str, 15, "Curses ESCDELAY"),
@@ -87,19 +93,21 @@ def __getattr__(self, name):
8793
try:
8894
return convert(value)
8995
except ConfigError as e:
90-
raise ConfigError("Error in .wpmrc section %r option %r: %s" %
96+
raise ConfigError("Error in wpmrc section %r option %r: %s" %
9197
(self.section, name, e))
9298

9399

94100
class Config(object):
95-
"""Contains the user configuration, backed by the .wpmrc file."""
101+
"""Contains the user configuration, backed by the wpmrc file."""
96102
# pylint: disable=too-many-public-methods
97103

98104
config = None
99105

100106
def __init__(self):
101107
Config.config = configparser.ConfigParser()
102-
self.filename = os.path.expanduser("~/.wpmrc")
108+
config_directory = get_config_directory()
109+
self.filename = os.path.join(config_directory, "wpmrc")
110+
os.makedirs(config_directory, exist_ok=True)
103111

104112
if os.path.isfile(self.filename):
105113
self.load()
@@ -113,19 +121,19 @@ def verify(self):
113121
"""Verifies wpmrc values."""
114122
level = self.wpm.confidence_level
115123
if not (0 < level < 1):
116-
raise ConfigError("The .wpmrc confidence level must be within [0, 1>")
124+
raise ConfigError("The wpmrc confidence level must be within [0, 1>")
117125

118126
def load(self):
119-
"""Loads ~/.wpmrc config settings."""
127+
"""Loads wpmrc config settings."""
120128
Config.config.read(self.filename)
121129

122130
def save(self):
123-
"""Saves settings to ~/.wpmrc"""
131+
"""Saves settings to wpmrc"""
124132
with open(self.filename, "wt") as file_obj:
125133
Config.config.write(file_obj)
126134

127135
def add_defaults(self):
128-
"""Adds missing sections and options to your ~/.wpmrc file."""
136+
"""Adds missing sections and options to your wpmrc file."""
129137
for section, values in sorted(DEFAULTS.items()):
130138
if not Config.config.has_section(section):
131139
Config.config.add_section(section)

wpm/error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ class WpmError(RuntimeError):
1616
pass
1717

1818
class ConfigError(WpmError):
19-
"""Incorrect .wpmrc option."""
19+
"""Incorrect wpmrc option."""
2020
pass

wpm/stats.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import datetime
1717
import math
1818
import os
19+
from .config import get_config_directory
1920

2021
class Timestamp(object):
2122
"""Methods for dealing with timestamps."""
@@ -200,7 +201,7 @@ def load(filename=None):
200201
"""Loads stats from a CSV file."""
201202

202203
if filename is None:
203-
filename = os.path.expanduser("~/.wpm.csv")
204+
filename = os.path.join(get_config_directory(), "wpm.csv")
204205

205206
games = collections.defaultdict(list)
206207
current_tag = None

0 commit comments

Comments
 (0)