-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Use lockfile for writes. - Use 'r+' rather than 'rw'. (The latter raises an exception in python 3). - Catch ValueError if data file isn't JSON. - Add `get_data_path` helper function.
- Loading branch information
1 parent
a6fa41d
commit e2dfc82
Showing
3 changed files
with
27 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,35 @@ | ||
import os | ||
import json | ||
|
||
from lockfile.pidlockfile import PIDLockFile | ||
|
||
DATAFILE = os.path.expanduser( | ||
os.path.join(os.getenv('TASKDATA', '~/.task'), 'bugwarrior.data')) | ||
from bugwarrior.config import get_data_path | ||
|
||
|
||
DATAFILE = os.path.join(get_data_path(), 'bugwarrior.data') | ||
LOCKFILE = os.path.join(get_data_path(), 'bugwarrior-data.lockfile') | ||
|
||
|
||
def get(key): | ||
try: | ||
with open(DATAFILE, 'r') as jsondata: | ||
data = json.load(jsondata) | ||
return data[key] | ||
try: | ||
data = json.load(jsondata) | ||
except ValueError: # File does not contain JSON. | ||
return None | ||
else: | ||
return data[key] | ||
except IOError: # File does not exist. | ||
return None | ||
|
||
|
||
def set(key, value): | ||
try: | ||
with open(DATAFILE, 'rw') as jsondata: | ||
data = json.load(jsondata) | ||
data[key] = value | ||
json.dump(data, jsondata) | ||
except IOError: # File does not exist. | ||
with open(DATAFILE, 'w+') as jsondata: | ||
json.dump({key: value}, jsondata) | ||
with PIDLockFile(LOCKFILE): | ||
try: | ||
with open(DATAFILE, 'r+') as jsondata: | ||
data = json.load(jsondata) | ||
data[key] = value | ||
json.dump(data, jsondata) | ||
except IOError: # File does not exist. | ||
with open(DATAFILE, 'w+') as jsondata: | ||
json.dump({key: value}, jsondata) |