Last active
August 29, 2015 14:19
-
-
Save d9k/e52d122c490e51ebba17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import argparse | |
import subprocess | |
import pprint | |
import os.path | |
import configparser | |
home = os.path.expanduser("~") | |
profiles_mozilla_path = home + r"/.mozilla/firefox" | |
profiles_ini_path = profiles_mozilla_path + r"/profiles.ini" | |
backup_cfg_folder_path = home + r"/scripts/cfg/firefox" | |
backup_cfg_path = backup_cfg_folder_path + r"/userContent.css" | |
#TODO список утилит + проверка через which + задание из параметра | |
edit_tool = "sublime-text" | |
edit_in_bg = True | |
merge_tool = "meld" | |
merge_in_bg = True | |
q = lambda string: '"' + string + '"' | |
pr = lambda obj: pprint.pprint(obj) | |
bg = lambda _bool: "&" if _bool else "" | |
#TODO: try add bg non-required named param | |
def bash(*command_parts): | |
subprocess.call(["bash", "-c", " ".join(command_parts)]) | |
pass | |
def ini_get(ini: configparser.RawConfigParser, section: str, option: str): | |
if not ini.has_option(section, option): | |
return None | |
return ini.get(section, option) | |
def get_profile_from_firefox_ini(ini_path): | |
ini = configparser.RawConfigParser() | |
ini.read(ini_path) | |
for section in ini.sections(): | |
if ini_get(ini, section, 'Default') == 1: | |
return ini[section]['Path'] | |
for section in ini.sections(): | |
if ini_get(ini, section, 'Name') == "default": | |
return ini[section]['Path'] | |
return None | |
def main(): | |
parser = argparse.ArgumentParser(description='this script creates firefox css file') | |
parser.add_argument( | |
'--merge', '-m', | |
help='merge files, not just edit', | |
action='store_true' | |
) | |
args = parser.parse_args() | |
#pprint.pprint(args) | |
if not os.path.isfile(profiles_ini_path): | |
print("File", q(profiles_ini_path), "not found") | |
return | |
profile_name = get_profile_from_firefox_ini(profiles_ini_path) | |
if not profile_name: | |
print("No profile-name found in", q(profiles_ini_path)) | |
return | |
profile_path = profiles_mozilla_path + '/' + profile_name | |
if not os.path.isdir(profile_path): | |
print("No profile folder at " + q(profile_path)) | |
return | |
user_css_folder_path = profile_path + "/chrome" | |
user_css_path = user_css_folder_path + "/userContent.css" | |
bash("mkdir", "-p", user_css_folder_path) | |
bash("touch", user_css_path) | |
if args.merge: | |
bash(merge_tool, user_css_path, backup_cfg_path, bg(merge_in_bg)) | |
else: | |
bash(edit_tool, user_css_path, bg(edit_in_bg)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment