@@ -6,6 +6,7 @@ v0.3.1 (unreleased): | |||
- Cleaned up the bookmark file format, fixing a related Windows bug. The script | |||
will automatically migrate to the new one. | |||
- Fixed a bug related to Python 3 compatibility. | |||
- Fixed unicode support. | |||
v0.3 (released June 7, 2015): | |||
@@ -31,19 +31,21 @@ def _load_config_file(config_path=None): | |||
cfg_path = config_path or get_default_config_path() | |||
try: | |||
with open(cfg_path, "r") as config_file: | |||
return config_file.read().split("\n") | |||
with open(cfg_path, "rb") as config_file: | |||
paths = config_file.read().split(b"\n") | |||
return [path.decode("utf8") for path in paths] | |||
except IOError: | |||
return [] | |||
def _save_config_file(config, config_path=None): | |||
"""Save config changes to the given config file.""" | |||
def _save_config_file(bookmarks, config_path=None): | |||
"""Save the bookmarks list to the given config file.""" | |||
run_migrations() | |||
cfg_path = config_path or get_default_config_path() | |||
_ensure_dirs(cfg_path) | |||
with open(cfg_path, "w") as config_file: | |||
config_file.write("\n".join(config)) | |||
dump = b"\n".join(path.encode("utf8") for path in bookmarks) | |||
with open(cfg_path, "wb") as config_file: | |||
config_file.write(dump) | |||
def get_default_config_path(): | |||
"""Return the default path to the configuration file.""" | |||
@@ -3,8 +3,6 @@ | |||
# Copyright (C) 2011-2016 Ben Kurtovic <ben.kurtovic@gmail.com> | |||
# Released under the terms of the MIT License. See LICENSE for details. | |||
__all__ = ["run_migrations"] | |||
import os | |||
try: | |||
@@ -14,6 +12,8 @@ except ImportError: # Python 2 | |||
from ConfigParser import SafeConfigParser as ConfigParser, NoSectionError | |||
PY3K = False | |||
__all__ = ["run_migrations"] | |||
def _get_old_path(): | |||
"""Return the old default path to the configuration file.""" | |||
xdg_cfg = os.environ.get("XDG_CONFIG_HOME") or os.path.join("~", ".config") | |||
@@ -45,12 +45,14 @@ def _migrate_old_format(): | |||
bookmarks = [path for path, _ in config.items("bookmarks")] | |||
except NoSectionError: | |||
bookmarks = [] | |||
if PY3K: | |||
bookmarks = [path.encode("utf8") for path in bookmarks] | |||
new_path = os.path.join(os.path.split(old_path)[0], "bookmarks") | |||
os.rename(old_path, new_path) | |||
with open(new_path, "wb") as handle: | |||
handle.write("\n".join(bookmarks)) | |||
handle.write(b"\n".join(bookmarks)) | |||
def run_migrations(): | |||
"""Run any necessary migrations to ensure the config file is up-to-date.""" | |||
@@ -7,6 +7,7 @@ from __future__ import print_function | |||
import argparse | |||
import os | |||
import sys | |||
from colorama import init as color_init, Fore, Style | |||
@@ -15,6 +16,12 @@ from .config import (get_default_config_path, get_bookmarks, add_bookmarks, | |||
delete_bookmarks, list_bookmarks) | |||
from .update import update_bookmarks, update_directories | |||
def _decode(path): | |||
"""Decode the given string using the system's filesystem encoding.""" | |||
if sys.version_info.major > 2: | |||
return path | |||
return path.decode(sys.getfilesystemencoding()) | |||
def main(): | |||
"""Parse arguments and then call the appropriate function(s).""" | |||
parser = argparse.ArgumentParser( | |||
@@ -30,7 +37,7 @@ def main(): | |||
group_m = parser.add_argument_group("miscellaneous") | |||
group_u.add_argument( | |||
'directories_to_update', nargs="*", metavar="path", | |||
'directories_to_update', nargs="*", metavar="path", type=_decode, | |||
help="""update all repositories in this directory (or the directory | |||
itself, if it is a repo)""") | |||
group_u.add_argument( | |||
@@ -48,15 +55,16 @@ def main(): | |||
group_b.add_argument( | |||
'-a', '--add', dest="bookmarks_to_add", nargs="+", metavar="path", | |||
help="add directory(s) as bookmarks") | |||
type=_decode, help="add directory(s) as bookmarks") | |||
group_b.add_argument( | |||
'-d', '--delete', dest="bookmarks_to_del", nargs="+", metavar="path", | |||
type=_decode, | |||
help="delete bookmark(s) (leaves actual directories alone)") | |||
group_b.add_argument( | |||
'-l', '--list', dest="list_bookmarks", action="store_true", | |||
help="list current bookmarks") | |||
group_b.add_argument( | |||
'-b', '--bookmark-file', nargs="?", metavar="path", | |||
'-b', '--bookmark-file', nargs="?", metavar="path", type=_decode, | |||
help="use a specific bookmark config file (default: {0})".format( | |||
get_default_config_path())) | |||