|
@@ -14,8 +14,8 @@ except ImportError: # Python 2 |
|
|
|
|
|
|
|
|
from colorama import Fore, Style |
|
|
from colorama import Fore, Style |
|
|
|
|
|
|
|
|
__all__ = ["get_bookmarks", "add_bookmarks", "delete_bookmarks", |
|
|
|
|
|
"list_bookmarks"] |
|
|
|
|
|
|
|
|
__all__ = ["get_default_config_path", "get_bookmarks", "add_bookmarks", |
|
|
|
|
|
"delete_bookmarks", "list_bookmarks"] |
|
|
|
|
|
|
|
|
YELLOW = Fore.YELLOW + Style.BRIGHT |
|
|
YELLOW = Fore.YELLOW + Style.BRIGHT |
|
|
RED = Fore.RED + Style.BRIGHT |
|
|
RED = Fore.RED + Style.BRIGHT |
|
@@ -25,50 +25,50 @@ INDENT1 = " " * 3 |
|
|
def _ensure_dirs(path): |
|
|
def _ensure_dirs(path): |
|
|
"""Ensure the directories within the given pathname exist.""" |
|
|
"""Ensure the directories within the given pathname exist.""" |
|
|
dirname = os.path.dirname(path) |
|
|
dirname = os.path.dirname(path) |
|
|
if not os.path.exists(dirname): # Race condition, meh... |
|
|
|
|
|
|
|
|
if dirname and not os.path.exists(dirname): # Race condition, meh... |
|
|
os.makedirs(dirname) |
|
|
os.makedirs(dirname) |
|
|
|
|
|
|
|
|
def _get_config_path(): |
|
|
|
|
|
"""Return the path to the configuration file.""" |
|
|
|
|
|
xdg_cfg = os.environ.get("XDG_CONFIG_HOME") or os.path.join("~", ".config") |
|
|
|
|
|
return os.path.join(os.path.expanduser(xdg_cfg), "gitup", "config.ini") |
|
|
|
|
|
|
|
|
|
|
|
def _migrate_old_config_path(): |
|
|
def _migrate_old_config_path(): |
|
|
"""Migrate the old config location (~/.gitup) to the new one.""" |
|
|
"""Migrate the old config location (~/.gitup) to the new one.""" |
|
|
old_path = os.path.expanduser(os.path.join("~", ".gitup")) |
|
|
old_path = os.path.expanduser(os.path.join("~", ".gitup")) |
|
|
if os.path.exists(old_path): |
|
|
if os.path.exists(old_path): |
|
|
new_path = _get_config_path() |
|
|
|
|
|
|
|
|
new_path = get_default_config_path() |
|
|
_ensure_dirs(new_path) |
|
|
_ensure_dirs(new_path) |
|
|
os.rename(old_path, new_path) |
|
|
os.rename(old_path, new_path) |
|
|
|
|
|
|
|
|
def _load_config_file(): |
|
|
|
|
|
|
|
|
def _load_config_file(config_path=None): |
|
|
"""Read the config file and return a SafeConfigParser() object.""" |
|
|
"""Read the config file and return a SafeConfigParser() object.""" |
|
|
_migrate_old_config_path() |
|
|
_migrate_old_config_path() |
|
|
config = configparser.SafeConfigParser() |
|
|
config = configparser.SafeConfigParser() |
|
|
# Don't lowercase option names, because we are storing paths there: |
|
|
# Don't lowercase option names, because we are storing paths there: |
|
|
config.optionxform = lambda opt: opt |
|
|
config.optionxform = lambda opt: opt |
|
|
config.read(_get_config_path()) |
|
|
|
|
|
|
|
|
config.read(config_path or get_default_config_path()) |
|
|
return config |
|
|
return config |
|
|
|
|
|
|
|
|
def _save_config_file(config): |
|
|
|
|
|
"""Save config changes to the config file returned by _get_config_path.""" |
|
|
|
|
|
|
|
|
def _save_config_file(config, config_path=None): |
|
|
|
|
|
"""Save config changes to the given config file.""" |
|
|
_migrate_old_config_path() |
|
|
_migrate_old_config_path() |
|
|
cfg_path = _get_config_path() |
|
|
|
|
|
|
|
|
cfg_path = config_path or get_default_config_path() |
|
|
_ensure_dirs(cfg_path) |
|
|
_ensure_dirs(cfg_path) |
|
|
with open(cfg_path, "w") as config_file: |
|
|
with open(cfg_path, "w") as config_file: |
|
|
config.write(config_file) |
|
|
config.write(config_file) |
|
|
|
|
|
|
|
|
def get_bookmarks(): |
|
|
|
|
|
|
|
|
def get_default_config_path(): |
|
|
|
|
|
"""Return the default path to the configuration file.""" |
|
|
|
|
|
xdg_cfg = os.environ.get("XDG_CONFIG_HOME") or os.path.join("~", ".config") |
|
|
|
|
|
return os.path.join(os.path.expanduser(xdg_cfg), "gitup", "config.ini") |
|
|
|
|
|
|
|
|
|
|
|
def get_bookmarks(config_path=None): |
|
|
"""Get a list of all bookmarks, or an empty list if there are none.""" |
|
|
"""Get a list of all bookmarks, or an empty list if there are none.""" |
|
|
config = _load_config_file() |
|
|
|
|
|
|
|
|
config = _load_config_file(config_path) |
|
|
try: |
|
|
try: |
|
|
return [path for path, _ in config.items("bookmarks")] |
|
|
return [path for path, _ in config.items("bookmarks")] |
|
|
except configparser.NoSectionError: |
|
|
except configparser.NoSectionError: |
|
|
return [] |
|
|
return [] |
|
|
|
|
|
|
|
|
def add_bookmarks(paths): |
|
|
|
|
|
|
|
|
def add_bookmarks(paths, config_path=None): |
|
|
"""Add a list of paths as bookmarks to the config file.""" |
|
|
"""Add a list of paths as bookmarks to the config file.""" |
|
|
config = _load_config_file() |
|
|
|
|
|
|
|
|
config = _load_config_file(config_path) |
|
|
if not config.has_section("bookmarks"): |
|
|
if not config.has_section("bookmarks"): |
|
|
config.add_section("bookmarks") |
|
|
config.add_section("bookmarks") |
|
|
|
|
|
|
|
@@ -81,7 +81,7 @@ def add_bookmarks(paths): |
|
|
path_name = os.path.split(path)[1] |
|
|
path_name = os.path.split(path)[1] |
|
|
config.set("bookmarks", path, path_name) |
|
|
config.set("bookmarks", path, path_name) |
|
|
added.append(path) |
|
|
added.append(path) |
|
|
_save_config_file(config) |
|
|
|
|
|
|
|
|
_save_config_file(config, config_path) |
|
|
|
|
|
|
|
|
if added: |
|
|
if added: |
|
|
print(YELLOW + "Added bookmarks:") |
|
|
print(YELLOW + "Added bookmarks:") |
|
@@ -92,9 +92,9 @@ def add_bookmarks(paths): |
|
|
for path in exists: |
|
|
for path in exists: |
|
|
print(INDENT1, path) |
|
|
print(INDENT1, path) |
|
|
|
|
|
|
|
|
def delete_bookmarks(paths): |
|
|
|
|
|
|
|
|
def delete_bookmarks(paths, config_path=None): |
|
|
"""Remove a list of paths from the bookmark config file.""" |
|
|
"""Remove a list of paths from the bookmark config file.""" |
|
|
config = _load_config_file() |
|
|
|
|
|
|
|
|
config = _load_config_file(config_path) |
|
|
|
|
|
|
|
|
deleted, notmarked = [], [] |
|
|
deleted, notmarked = [], [] |
|
|
if config.has_section("bookmarks"): |
|
|
if config.has_section("bookmarks"): |
|
@@ -105,7 +105,7 @@ def delete_bookmarks(paths): |
|
|
deleted.append(path) |
|
|
deleted.append(path) |
|
|
else: |
|
|
else: |
|
|
notmarked.append(path) |
|
|
notmarked.append(path) |
|
|
_save_config_file(config) |
|
|
|
|
|
|
|
|
_save_config_file(config, config_path) |
|
|
else: |
|
|
else: |
|
|
notmarked = [os.path.abspath(path) for path in paths] |
|
|
notmarked = [os.path.abspath(path) for path in paths] |
|
|
|
|
|
|
|
@@ -118,9 +118,9 @@ def delete_bookmarks(paths): |
|
|
for path in notmarked: |
|
|
for path in notmarked: |
|
|
print(INDENT1, path) |
|
|
print(INDENT1, path) |
|
|
|
|
|
|
|
|
def list_bookmarks(): |
|
|
|
|
|
|
|
|
def list_bookmarks(config_path=None): |
|
|
"""Print all of our current bookmarks.""" |
|
|
"""Print all of our current bookmarks.""" |
|
|
bookmarks = get_bookmarks() |
|
|
|
|
|
|
|
|
bookmarks = get_bookmarks(config_path=config_path) |
|
|
if bookmarks: |
|
|
if bookmarks: |
|
|
print(YELLOW + "Current bookmarks:") |
|
|
print(YELLOW + "Current bookmarks:") |
|
|
for bookmark_path in bookmarks: |
|
|
for bookmark_path in bookmarks: |
|
|