Skip to content

Commit 1362913

Browse files
committed
Expand ~ via pathlib
1 parent 8f5268e commit 1362913

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ A barebones "Custom autocorrect"/text replacement plugin for Zim.
55
### How to use
66

77
1. Move `textreplacer.py` into the plugins folder
8-
2. Create your replacements json (see `example.json` for reference) and copy its full path
9-
2. Enable the plugin in Zim and click "Configure"
8+
2. Create your replacements json and copy its full path
9+
* see `example.json` for reference
10+
* on Linux, moving `example.json` to `~/.local/share/zim/plugins/text_replacements.json` is enough to get going; you can skip the rest of the steps
11+
3. Enable the plugin in Zim and click "Configure"
1012
4. Paste in the path from step 2 and click "OK"
1113

1214
From now on, every time you type one of the words you specified it will get replaced with the replacement you've chosen; this can be undone via undo (Ctrl+Z).
@@ -40,12 +42,13 @@ ERROR: Failed to load json from bad path: Error text here
4042

4143
Some common errors and how to fix them:
4244

43-
* `[Errno 2] No such file or directory: 'bad path'`: check the path; it should be a full, absolute path (eg. no `~` or `$HOME`)
45+
* `[Errno 2] No such file or directory: 'bad path'`: check the path; aside from `~` and `~user` constructs you should use an absolute path (see [pathlib.Path](https://docs.python.org/3/library/pathlib.html#pathlib.Path) for more information)
4446
* Check your json if any of these show up:
4547
* `Extra data: line n, column m, (char l)` (or `Expecting value: ...` etc): something's not json
4648
* `invalid json object`: couldn't be loaded as a Python `dict`
4749
* `json contains invalid keys`: non-string keys (how?)
4850
* `json contains invalid values`: non-string values
51+
* For more information, see the [Python json library docs](https://docs.python.org/3/library/json.html)
4952

5053
### System-wide alternatives
5154

textreplace.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from json import load
2+
from pathlib import Path
23

34
from zim.plugins import PluginClass
45
from zim.gui.pageview import PageViewExtension
@@ -20,7 +21,7 @@ class TextReplacerPlugin(PluginClass):
2021
}
2122

2223
plugin_preferences = (
23-
('json_path', 'string', _('Path to text replacements dictionary json file'), ''), # T: plugin preference
24+
('json_path', 'string', _('Path to text replacements dictionary json file'), '~/.local/share/zim/plugins/text_replacements.json'), # T: plugin preference
2425
)
2526

2627

@@ -30,7 +31,7 @@ def __init__(self, plugin, pageview):
3031
self.replacements = {}
3132
path = self.plugin.preferences['json_path']
3233
try:
33-
with open(path, 'r') as f:
34+
with open(Path(path).expanduser(), 'r') as f:
3435
json = load(f)
3536
assert type(json) == dict, 'invalid json object'
3637
assert all(type(key) == str for key in json.keys()), 'json contains invalid keys'

0 commit comments

Comments
 (0)