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.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2011-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. # See the LICENSE file 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. CONFIG_FILENAME = os.path.join(os.path.expanduser("~"), ".gitup")
  12. YELLOW = Fore.YELLOW + Style.BRIGHT
  13. RED = Fore.RED + Style.BRIGHT
  14. INDENT1 = " " * 3
  15. def _load_config_file():
  16. """Read the config file and return a SafeConfigParser() object."""
  17. config = configparser.SafeConfigParser()
  18. # Don't lowercase option names, because we are storing paths there:
  19. config.optionxform = str
  20. config.read(CONFIG_FILENAME)
  21. return config
  22. def _save_config_file(config):
  23. """Save config changes to the config file specified by CONFIG_FILENAME."""
  24. with open(CONFIG_FILENAME, "wb") as config_file:
  25. config.write(config_file)
  26. def get_bookmarks():
  27. """Get a list of all bookmarks, or an empty list if there are none."""
  28. config = _load_config_file()
  29. try:
  30. return config.items("bookmarks")
  31. except configparser.NoSectionError:
  32. return []
  33. def add_bookmarks(paths):
  34. """Add a list of paths as bookmarks to the config file."""
  35. config = _load_config_file()
  36. if not config.has_section("bookmarks"):
  37. config.add_section("bookmarks")
  38. added, exists = [], []
  39. for path in paths:
  40. path = os.path.abspath(path)
  41. if config.has_option("bookmarks", path):
  42. exists.append(path)
  43. else:
  44. path_name = os.path.split(path)[1]
  45. config.set("bookmarks", path, path_name)
  46. added.append(path)
  47. _save_config_file(config)
  48. if added:
  49. print(YELLOW + "Added bookmarks:")
  50. for path in added:
  51. print(INDENT1, path)
  52. if exists:
  53. print(RED + "Already bookmarked:")
  54. for path in exists:
  55. print(INDENT1, path)
  56. def delete_bookmarks(paths):
  57. """Remove a list of paths from the bookmark config file."""
  58. config = _load_config_file()
  59. deleted, notmarked = [], []
  60. if config.has_section("bookmarks"):
  61. for path in paths:
  62. path = os.path.abspath(path)
  63. config_was_changed = config.remove_option("bookmarks", path)
  64. if config_was_changed:
  65. deleted.append(path)
  66. else:
  67. notmarked.append(path)
  68. _save_config_file(config)
  69. else:
  70. notmarked = [os.path.abspath(path) for path in paths]
  71. if deleted:
  72. print(YELLOW + "Deleted bookmarks:")
  73. for path in deleted:
  74. print(INDENT1, path)
  75. if notmarked:
  76. print(RED + "Not bookmarked:")
  77. for path in notmarked:
  78. print(INDENT1, path)
  79. def list_bookmarks():
  80. """Print all of our current bookmarks."""
  81. bookmarks = get_bookmarks()
  82. if bookmarks:
  83. print(YELLOW + "Current bookmarks:")
  84. for bookmark_path, _ in bookmarks:
  85. print(INDENT1, bookmark_path)
  86. else:
  87. print("You have no bookmarks to display.")