1
0
mirror of https://github.com/wting/autojump synced 2024-10-27 20:34:07 +00:00
wting_autojump/bin/data.py

103 lines
3.2 KiB
Python
Raw Normal View History

2013-12-16 17:20:40 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
2013-12-16 17:20:40 +00:00
2013-12-16 21:19:04 +00:00
from codecs import open
from itertools import ifilter
2013-12-16 17:20:40 +00:00
from itertools import imap
from operator import itemgetter
2013-12-16 17:20:40 +00:00
import os
import shutil
import sys
from time import time
2013-12-16 17:20:40 +00:00
from utils import create_dir
2013-12-16 17:20:40 +00:00
from utils import is_osx
from utils import move_file
2013-12-16 17:20:40 +00:00
BACKUP_THRESHOLD = 24 * 60 * 60
2013-12-16 17:20:40 +00:00
def load(config):
xdg_aj_home = os.path.join(
os.path.expanduser('~'),
'.local',
'share',
'autojump')
if is_osx() and os.path.exists(xdg_aj_home):
migrate_osx_xdg_data(config)
if os.path.exists(config['data_path']):
try:
2013-12-16 21:19:04 +00:00
with open(config['data_path'], 'r', encoding='utf-8', errors='replace') as f:
lines = f.readlines()
except (IOError, EOFError):
return load_backup(config)
2013-12-16 21:19:04 +00:00
# example: u'10.0\t/home/user\n' -> ['10.0', u'/home/user']
parse = lambda x: x.strip().split('\t')
2013-12-16 21:19:04 +00:00
# example: ['10.0', u'/home/user'] -> (u'/home/user', 10.0)
2013-12-16 21:39:49 +00:00
convert = lambda x: (x[1], float(x[0]))
correct_length = lambda x: len(x) == 2
return dict(imap(convert, ifilter(correct_length, imap(parse, lines))))
2013-12-16 17:20:40 +00:00
return {}
def load_backup(config):
2013-12-16 21:19:04 +00:00
if os.path.exists(config['backup_path']):
move_file(config['backup_path'], config['data_path'])
return load(config)
return {}
2013-12-16 17:20:40 +00:00
def migrate_osx_xdg_data(config):
"""
Older versions incorrectly used Linux XDG_DATA_HOME paths on OS X. This
migrates autojump files from ~/.local/share/autojump to ~/Library/autojump
"""
2013-12-16 22:26:41 +00:00
assert is_osx(), "This function should only be run on OS X."
2013-12-16 17:20:40 +00:00
xdg_data_home = os.path.join(os.path.expanduser('~'), '.local', 'share')
xdg_aj_home = os.path.join(xdg_data_home, 'autojump')
data_path = os.path.join(xdg_aj_home, 'autojump.txt'),
2013-12-16 21:19:04 +00:00
backup_path = os.path.join(xdg_aj_home, 'autojump.txt.bak'),
2013-12-16 17:20:40 +00:00
if os.path.exists(data_path):
move_file(data_path, config['data_path'])
2013-12-16 21:19:04 +00:00
if os.path.exists(backup_path):
move_file(backup_path, config['backup_path'])
2013-12-16 17:20:40 +00:00
# cleanup
shutil.rmtree(xdg_aj_home)
if len(os.listdir(xdg_data_home)) == 0:
shutil.rmtree(xdg_data_home)
2013-12-16 17:20:40 +00:00
def save(config, data):
"""Save data and create backup, creating a new data file if necessary."""
create_dir(os.path.dirname(config['data_path']))
2013-12-16 17:20:40 +00:00
# atomically save by writing to temporary file and moving to destination
try:
2013-12-16 21:19:04 +00:00
# write to temp file
with open(config['tmp_path'], 'w', encoding='utf-8', errors='replace') as f:
for path, weight in data.iteritems():
2013-12-16 21:19:04 +00:00
f.write((unicode("%s\t%s\n" % (weight, path)).encode('utf-8')))
f.flush()
os.fsync(f)
except IOError as ex:
print("Error saving autojump data (disk full?)" % ex, file=sys.stderr)
sys.exit(1)
2013-12-16 21:19:04 +00:00
# create backup file if it doesn't exist or is older than BACKUP_THRESHOLD
if not os.path.exists(config['backup_path']) or \
(time() - os.path.getmtime(config['backup_path']) > BACKUP_THRESHOLD):
move_file(config['data_path'], config['backup_path'])
# move temp_file -> autojump.txt
move_file(config['tmp_path'], config['data_path'])