mirror of
https://github.com/wting/autojump
synced 2024-10-27 20:34:07 +00:00
simplify unicode encode/decoding
This commit is contained in:
parent
39b5e3030d
commit
7671fcbd06
58
bin/data.py
58
bin/data.py
@ -2,6 +2,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
|
from codecs import open
|
||||||
from itertools import imap
|
from itertools import imap
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
import os
|
import os
|
||||||
@ -10,11 +11,8 @@ import sys
|
|||||||
from time import time
|
from time import time
|
||||||
|
|
||||||
from utils import create_dir
|
from utils import create_dir
|
||||||
from utils import decode
|
|
||||||
from utils import is_osx
|
from utils import is_osx
|
||||||
from utils import is_python3
|
|
||||||
from utils import move_file
|
from utils import move_file
|
||||||
from utils import unico as unicode
|
|
||||||
|
|
||||||
|
|
||||||
BACKUP_THRESHOLD = 24 * 60 * 60
|
BACKUP_THRESHOLD = 24 * 60 * 60
|
||||||
@ -32,28 +30,24 @@ def load(config):
|
|||||||
|
|
||||||
if os.path.exists(config['data_path']):
|
if os.path.exists(config['data_path']):
|
||||||
try:
|
try:
|
||||||
if is_python3():
|
with open(config['data_path'], 'r', encoding='utf-8', errors='replace') as f:
|
||||||
with open(data_path, 'r', encoding='utf-8') as f:
|
lines = f.readlines()
|
||||||
lines = f.readlines()
|
|
||||||
else:
|
|
||||||
with open(data_path, 'r') as f:
|
|
||||||
lines = f.readlines()
|
|
||||||
except (IOError, EOFError):
|
except (IOError, EOFError):
|
||||||
return load_backup(config)
|
return load_backup(config)
|
||||||
|
|
||||||
# example: '10.0\t/home/user\n' -> ['10.0', '/home/user']
|
# example: u'10.0\t/home/user\n' -> ['10.0', u'/home/user']
|
||||||
parse = lambda x: x.strip().split('\t')
|
parse = lambda x: x.strip().split('\t')
|
||||||
|
|
||||||
# example: ['10.0', '/home/user'] -> (u'/home/user', 10.0)
|
# example: ['10.0', u'/home/user'] -> (u'/home/user', 10.0)
|
||||||
convert = lambda x: (decode(x[1], 'utf-8'), float(x[0]))
|
convert = lambda x: (x[1], float(x[0])
|
||||||
|
|
||||||
return dict(imap(convert, imap(parse, lines)))
|
return dict(imap(convert, imap(parse, lines)))
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def load_backup(config):
|
def load_backup(config):
|
||||||
if os.path.exists(config['data_backup_path']):
|
if os.path.exists(config['backup_path']):
|
||||||
move_file(config['data_backup_path'], config['data_path'])
|
move_file(config['backup_path'], config['data_path'])
|
||||||
return load(config)
|
return load(config)
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@ -68,12 +62,12 @@ def migrate_osx_xdg_data(config):
|
|||||||
xdg_data_home = os.path.join(os.path.expanduser('~'), '.local', 'share')
|
xdg_data_home = os.path.join(os.path.expanduser('~'), '.local', 'share')
|
||||||
xdg_aj_home = os.path.join(xdg_data_home, 'autojump')
|
xdg_aj_home = os.path.join(xdg_data_home, 'autojump')
|
||||||
data_path = os.path.join(xdg_aj_home, 'autojump.txt'),
|
data_path = os.path.join(xdg_aj_home, 'autojump.txt'),
|
||||||
data_backup_path = os.path.join(xdg_aj_home, 'autojump.txt.bak'),
|
backup_path = os.path.join(xdg_aj_home, 'autojump.txt.bak'),
|
||||||
|
|
||||||
if os.path.exists(data_path):
|
if os.path.exists(data_path):
|
||||||
move_file(data_path, config['data_path'])
|
move_file(data_path, config['data_path'])
|
||||||
if os.path.exists(data_backup_path):
|
if os.path.exists(backup_path):
|
||||||
move_file(data_backup_path, config['data_backup_path'])
|
move_file(backup_path, config['backup_path'])
|
||||||
|
|
||||||
# cleanup
|
# cleanup
|
||||||
shutil.rmtree(xdg_aj_home)
|
shutil.rmtree(xdg_aj_home)
|
||||||
@ -86,29 +80,25 @@ def save(config, data):
|
|||||||
create_dir(os.path.dirname(config['data_path']))
|
create_dir(os.path.dirname(config['data_path']))
|
||||||
|
|
||||||
# atomically save by writing to temporary file and moving to destination
|
# atomically save by writing to temporary file and moving to destination
|
||||||
temp_file = tempfile.NamedTemporaryFile(
|
|
||||||
dir=os.path.dirname(config['data_path']),
|
|
||||||
delete=False)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for path, weight in sorted(
|
# write to temp file
|
||||||
data.iteritems(),
|
with open(config['tmp_path'], 'w', encoding='utf-8', errors='replace') as f:
|
||||||
key=itemgetter(1),
|
for path, weight in sorted(
|
||||||
reverse=True):
|
data.iteritems(),
|
||||||
temp_file.write((unicode("%s\t%s\n" % (weight, path)).encode("utf-8")))
|
key=itemgetter(1),
|
||||||
|
reverse=True):
|
||||||
|
f.write((unicode("%s\t%s\n" % (weight, path)).encode('utf-8')))
|
||||||
|
|
||||||
temp_file.flush()
|
f.flush()
|
||||||
os.fsync(temp_file)
|
os.fsync(f)
|
||||||
temp_file.close()
|
|
||||||
except IOError as ex:
|
except IOError as ex:
|
||||||
print("Error saving autojump data (disk full?)" % ex, file=sys.stderr)
|
print("Error saving autojump data (disk full?)" % ex, file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# if no backup file or backup file is older than 24 hours,
|
# create backup file if it doesn't exist or is older than BACKUP_THRESHOLD
|
||||||
# move autojump.txt -> autojump.txt.bak
|
if not os.path.exists(config['backup_path']) or \
|
||||||
if not os.path.exists(config['data_backup_path']) or \
|
(time() - os.path.getmtime(config['backup_path']) > BACKUP_THRESHOLD):
|
||||||
(time() - os.path.getmtime(config['data_backup_path']) > BACKUP_THRESHOLD):
|
move_file(config['data_path'], config['backup_path'])
|
||||||
move_file(config['data_path'], config['data_backup_path'])
|
|
||||||
|
|
||||||
# move temp_file -> autojump.txt
|
# move temp_file -> autojump.txt
|
||||||
move_file(temp_file.name, config['data_path'])
|
move_file(temp_file.name, config['data_path'])
|
||||||
|
31
bin/utils.py
31
bin/utils.py
@ -17,14 +17,6 @@ def create_dir(path):
|
|||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
def is_python2():
|
|
||||||
return sys.version_info[0] > 2
|
|
||||||
|
|
||||||
|
|
||||||
def is_python3():
|
|
||||||
return sys.version_info[0] > 3
|
|
||||||
|
|
||||||
|
|
||||||
def is_linux():
|
def is_linux():
|
||||||
return platform.system() == 'Linux'
|
return platform.system() == 'Linux'
|
||||||
|
|
||||||
@ -37,29 +29,6 @@ def is_windows():
|
|||||||
return platform.system() == 'Windows'
|
return platform.system() == 'Windows'
|
||||||
|
|
||||||
|
|
||||||
def decode(string, encoding=None, errors="strict"):
|
|
||||||
"""
|
|
||||||
Decoding step for Python 2 which does not default to unicode.
|
|
||||||
"""
|
|
||||||
if is_python2():
|
|
||||||
return string
|
|
||||||
else:
|
|
||||||
if encoding is None:
|
|
||||||
encoding = sys.getfilesystemencoding()
|
|
||||||
return string.decode(encoding, errors)
|
|
||||||
|
|
||||||
|
|
||||||
def unico(string):
|
|
||||||
"""
|
|
||||||
If Python 2, convert to a unicode object.
|
|
||||||
"""
|
|
||||||
print("custom unicode")
|
|
||||||
if sys.version_info[0] > 2:
|
|
||||||
return string
|
|
||||||
else:
|
|
||||||
return unicode(string)
|
|
||||||
|
|
||||||
|
|
||||||
def move_file(src, dst):
|
def move_file(src, dst):
|
||||||
"""
|
"""
|
||||||
Atomically move file.
|
Atomically move file.
|
||||||
|
Loading…
Reference in New Issue
Block a user