Created
February 14, 2019 16:47
-
-
Save EricKotato/6cb8d7f9def38cb1a169251165898d91 to your computer and use it in GitHub Desktop.
Changing font for Cinnamon and Gnome shells.
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
#!/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import os | |
import sys | |
import argparse | |
THEME_PATH = '/usr/share/themes' | |
HOME_THEME_PATH = os.path.join(os.environ['HOME'],'.themes') | |
# [0] theme path, [1] theme name, [2] shell name, [3] font list | |
TEMPLATE = '@import url("file://{0}/{1}/{2}/{2}.css");\nstage {{ font-family: {3}; }}' | |
""" | |
Usage: ./patchthemes.py YourFontName1 YourFontName2 | |
You can run it without args to use only sans-serif font. | |
--serif or -s will use serif font as fallback. | |
--force or -f will replace files in home path if they exist. | |
""" | |
def main(args): | |
try: | |
lang, country = os.environ['LANG'].split('.')[0].split('_') | |
if lang in ('ru','be','uk') or country in ('RU','BY','UA'): | |
phrases = { | |
'desc': 'Патч шрифта для панелей Cinnamon и Gnome.', | |
'fonts_meta': 'ШРИФТ', | |
'fonts_desc': 'имя шрифта для использования', | |
'serif_desc': 'использовать шрифт с засечками как fallback (по умолчанию без засечек)', | |
'force_desc': 'заменять существующие файлы', | |
'exist_error': 'Файл {} существует (используйте --force для замены)', | |
'nowrite_error': 'Не удалось записать изменения в {}, возможно, файл открыт в другом приложении, или у пользователя нет прав для его изменения.' | |
} | |
else: | |
raise Exception | |
except: | |
phrases = { | |
'desc': 'Patch Cinnamon and Gnome shell theme font.', | |
'fonts_meta': 'FONT', | |
'fonts_desc': 'font name to use', | |
'serif_desc': 'use serif font for fallback instead of sans-serif', | |
'force_desc': 'replace existing files', | |
'exist_error': 'File {} exists (use --force to replace)', | |
'nowrite_error': 'Can\'t write changes to {}, maybe file is used by other program or this user have no rights to edit it.' | |
} | |
parser = argparse.ArgumentParser(description=phrases['desc']) | |
parser.add_argument('fonts', metavar=phrases['fonts_meta'], nargs='*', help=phrases['fonts_desc']) | |
parser.add_argument('--serif', '-s', action='store_true', help=phrases['serif_desc'], default=False) | |
parser.add_argument('--force', '-f', action='store_true', help=phrases['force_desc'], default=False) | |
parsed_args = parser.parse_args(args) | |
font_list = [] | |
for f in parsed_args.fonts: | |
font_list.append('"{}"'.format(f) if ' ' in f else f) | |
font_list.append('serif' if parsed_args.serif else 'sans-serif') | |
fonts = ", ".join(font_list) | |
with os.scandir(THEME_PATH) as it: | |
for entry in it: | |
if not entry.is_dir(): | |
continue | |
for sh in ('cinnamon', 'gnome-shell'): | |
filepath = "{0}{1}{0}.css".format(sh, os.path.sep) | |
if not os.path.exists(os.path.join(entry.path, filepath)): | |
continue | |
home_file_path = os.path.join(HOME_THEME_PATH, entry.name, filepath) | |
if os.path.exists(home_file_path) and not parsed_args.force: | |
print(phrases['exist_error'].format(home_file_path)) | |
else: | |
try: | |
os.makedirs(os.path.join(HOME_THEME_PATH, entry.name, sh), mode=0o755, exist_ok=True) | |
except: | |
pass | |
try: | |
with open(home_file_path, mode='w', encoding="utf-8") as f: | |
f.write(TEMPLATE.format(THEME_PATH, entry.name, sh, fonts)) | |
except: | |
print(phrases['nowrite_error'].format(home_file_path)) | |
if __name__ == '__main__': | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment