Advertisement
Guest User

Untitled

a guest
May 25th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import argparse
  4. import subprocess
  5. import pprint
  6. import os.path
  7. import configparser
  8.  
  9. home = os.path.expanduser("~")
  10. profiles_mozilla_path = home + r"/.mozilla/firefox"
  11. profiles_ini_path = profiles_mozilla_path + r"/profiles.ini"
  12. backup_cfg_folder_path = home + r"/scripts/cfg/firefox"
  13. backup_cfg_path = backup_cfg_folder_path + r"/userContent.css"
  14.  
  15. #TODO список утилит + проверка через which + задание из параметра
  16. edit_tool = "sublime-text"
  17. edit_in_bg = True
  18. merge_tool = "meld"
  19. merge_in_bg = True
  20.  
  21.  
  22. q = lambda string: '"' + string + '"'
  23.  
  24. pr = lambda obj: pprint.pprint(obj)
  25.  
  26. bg = lambda _bool: "&" if _bool else ""
  27.  
  28.  
  29. #TODO: try add bg non-required named param
  30. def bash(*command_parts):
  31. subprocess.call(["bash", "-c", " ".join(command_parts)])
  32. pass
  33.  
  34.  
  35. def ini_get(ini: configparser.RawConfigParser, section: str, option: str):
  36. if not ini.has_option(section, option):
  37. return None
  38. return ini.get(section, option)
  39.  
  40.  
  41. def get_profile_from_firefox_ini(ini_path):
  42. ini = configparser.RawConfigParser()
  43. ini.read(ini_path)
  44.  
  45. for section in ini.sections():
  46. if ini_get(ini, section, 'Default') == 1:
  47. return ini[section]['Path']
  48.  
  49. for section in ini.sections():
  50. if ini_get(ini, section, 'Name') == "default":
  51. return ini[section]['Path']
  52.  
  53. return None
  54.  
  55.  
  56. def main():
  57. parser = argparse.ArgumentParser(description='this script creates firefox css file')
  58. parser.add_argument(
  59. '--merge', '-m',
  60. help='merge files, not just edit',
  61. action='store_true'
  62. )
  63. args = parser.parse_args()
  64. #pprint.pprint(args)
  65. if not os.path.isfile(profiles_ini_path):
  66. print("File", q(profiles_ini_path), "not found")
  67. return
  68. profile_name = get_profile_from_firefox_ini(profiles_ini_path)
  69. if not profile_name:
  70. print("No profile-name found in", q(profiles_ini_path))
  71. return
  72. profile_path = profiles_mozilla_path + '/' + profile_name
  73. if not os.path.isdir(profile_path):
  74. print("No profile folder at " + q(profile_path))
  75. return
  76. user_css_folder_path = profile_path + "/chrome"
  77. user_css_path = user_css_folder_path + "/userContent.css"
  78. bash("mkdir", "-p", user_css_folder_path)
  79. bash("touch", user_css_path)
  80. if args.merge:
  81. bash(merge_tool, user_css_path, backup_cfg_path, bg(merge_in_bg))
  82. else:
  83. bash(edit_tool, user_css_path, bg(edit_in_bg))
  84.  
  85. if __name__ == "__main__":
  86. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement