A console script that allows you to easily update multiple git repositories at once
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.py 3.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2011-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. # Released under the terms of the MIT License. See LICENSE for details.
  5. from __future__ import print_function
  6. import ConfigParser as configparser
  7. import os
  8. from colorama import Fore, Style
  9. __all__ = ["get_bookmarks", "add_bookmarks", "delete_bookmarks",
  10. "list_bookmarks"]
  11. YELLOW = Fore.YELLOW + Style.BRIGHT
  12. RED = Fore.RED + Style.BRIGHT
  13. INDENT1 = " " * 3
  14. def _ensure_dirs(path):
  15. """Ensure the directories within the given pathname exist."""
  16. dirname = os.path.dirname(path)
  17. if not os.path.exists(dirname): # Race condition, meh...
  18. os.makedirs(dirname)
  19. def _get_config_path():
  20. """Return the path to the configuration file."""
  21. xdg_cfg = os.environ.get("XDG_CONFIG_HOME") or os.path.join("~", ".config")
  22. return os.path.join(os.path.expanduser(xdg_cfg), "gitup", "config.ini")
  23. def _migrate_old_config_path():
  24. """Migrate the old config location (~/.gitup) to the new one."""
  25. old_path = os.path.expanduser(os.path.join("~", ".gitup"))
  26. if os.path.exists(old_path):
  27. new_path = _get_config_path()
  28. _ensure_dirs(new_path)
  29. os.rename(old_path, new_path)
  30. def _load_config_file():
  31. """Read the config file and return a SafeConfigParser() object."""
  32. _migrate_old_config_path()
  33. config = configparser.SafeConfigParser()
  34. # Don't lowercase option names, because we are storing paths there:
  35. config.optionxform = str
  36. config.read(_get_config_path())
  37. return config
  38. def _save_config_file(config):
  39. """Save config changes to the config file returned by _get_config_path."""
  40. _migrate_old_config_path()
  41. cfg_path = _get_config_path()
  42. _ensure_dirs(cfg_path)
  43. with open(cfg_path, "wb") as config_file:
  44. config.write(config_file)
  45. def get_bookmarks():
  46. """Get a list of all bookmarks, or an empty list if there are none."""
  47. config = _load_config_file()
  48. try:
  49. return config.items("bookmarks")
  50. except configparser.NoSectionError:
  51. return []
  52. def add_bookmarks(paths):
  53. """Add a list of paths as bookmarks to the config file."""
  54. config = _load_config_file()
  55. if not config.has_section("bookmarks"):
  56. config.add_section("bookmarks")
  57. added, exists = [], []
  58. for path in paths:
  59. path = os.path.abspath(path)
  60. if config.has_option("bookmarks", path):
  61. exists.append(path)
  62. else:
  63. path_name = os.path.split(path)[1]
  64. config.set("bookmarks", path, path_name)
  65. added.append(path)
  66. _save_config_file(config)
  67. if added:
  68. print(YELLOW + "Added bookmarks:")
  69. for path in added:
  70. print(INDENT1, path)
  71. if exists:
  72. print(RED + "Already bookmarked:")
  73. for path in exists:
  74. print(INDENT1, path)
  75. def delete_bookmarks(paths):
  76. """Remove a list of paths from the bookmark config file."""
  77. config = _load_config_file()
  78. deleted, notmarked = [], []
  79. if config.has_section("bookmarks"):
  80. for path in paths:
  81. path = os.path.abspath(path)
  82. config_was_changed = config.remove_option("bookmarks", path)
  83. if config_was_changed:
  84. deleted.append(path)
  85. else:
  86. notmarked.append(path)
  87. _save_config_file(config)
  88. else:
  89. notmarked = [os.path.abspath(path) for path in paths]
  90. if deleted:
  91. print(YELLOW + "Deleted bookmarks:")
  92. for path in deleted:
  93. print(INDENT1, path)
  94. if notmarked:
  95. print(RED + "Not bookmarked:")
  96. for path in notmarked:
  97. print(INDENT1, path)
  98. def list_bookmarks():
  99. """Print all of our current bookmarks."""
  100. bookmarks = get_bookmarks()
  101. if bookmarks:
  102. print(YELLOW + "Current bookmarks:")
  103. for bookmark_path, _ in bookmarks:
  104. print(INDENT1, bookmark_path)
  105. else:
  106. print("You have no bookmarks to display.")