Skip to content

Commit

Permalink
Data store improvements.
Browse files Browse the repository at this point in the history
- 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
ryneeverett committed Mar 9, 2016
1 parent a6fa41d commit e2dfc82
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
13 changes: 2 additions & 11 deletions bugwarrior/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import keyring
import click

from taskw.warrior import TaskWarriorBase

from bugwarrior.config import get_taskrc_path, load_config
from bugwarrior.config import get_data_path, load_config
from bugwarrior.services import aggregate_issues, get_service
from bugwarrior.db import (
get_defined_udas_as_strings,
Expand Down Expand Up @@ -45,14 +43,7 @@ def pull(dry_run, flavor, interactive):
# Load our config file
config = load_config(main_section, interactive)

tw_config = TaskWarriorBase.load_config(get_taskrc_path(config, main_section))
lockfile_path = os.path.join(
os.path.expanduser(
tw_config['data']['location']
),
'bugwarrior.lockfile'
)

lockfile_path = os.path.join(get_data_path(), 'bugwarrior.lockfile')
lockfile = PIDLockFile(lockfile_path)
lockfile.acquire(timeout=10)
try:
Expand Down
4 changes: 4 additions & 0 deletions bugwarrior/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,9 @@ def get_taskrc_path(conf, main_section):
)


def get_data_path():
return os.path.expanduser(os.getenv('TASKDATA', '~/.task'))


# This needs to be imported here and not above to avoid a circular-import.
from bugwarrior.services import get_service
33 changes: 21 additions & 12 deletions bugwarrior/data.py
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)

0 comments on commit e2dfc82

Please sign in to comment.