From: Oleg Broytman Date: Tue, 11 Jul 2023 17:11:57 +0000 (+0300) Subject: Refactor(dotfiles2html.py) X-Git-Url: https://git.phdru.name/?p=phdru.name%2Fphdru.name.git;a=commitdiff_plain;h=c857a9b208a03bcf88b894b718a11871deab8b1a Refactor(dotfiles2html.py) --- diff --git a/dotfiles2html/dotfiles2html.py b/dotfiles2html/dotfiles2html.py index 7c6f95a..a5065cd 100755 --- a/dotfiles2html/dotfiles2html.py +++ b/dotfiles2html/dotfiles2html.py @@ -1,16 +1,32 @@ #! /usr/bin/env python __author__ = "Oleg Broytman " -__copyright__ = "Copyright (C) 2004-2020 PhiloSoft Design" +__copyright__ = "Copyright (C) 2004-2023 PhiloSoft Design" import filecmp import os import re import shutil -os.umask(0022) # octal; -rw-r--r-- from m_lib.m_path import get_homedir + home_dir = get_homedir() +os.umask(0o022) # octal; -rw-r--r-- + + +home_file_types = ( + ("bashrc", "sh"), ("profile", "sh"), ("shellrc", "sh"), + ("mailcap", "conf"), ("mime.types", "conf"), + ("muttrc", "muttrc"), ("procmailrc", "procmail"), + ("gitconfig", "gitconfig"), ("vimrc", "vim"), +) + +mc_file_types = (("mc.ext", "conf"), ("menu", "conf")) + +python_file_types = ( + ("init.py", "python"), + ("pdbrc", "python"), ("pdbrc.py", "python") +) def add_headers(fname, first, prev, next, last): @@ -44,7 +60,10 @@ def add_headers(fname, first, prev, next, last): if next: html_file.write("#attr $Next = \"%s.html\"\n" % next) html_file.write("#attr $Last = \"%s.html\"\n" % last) - html_file.write('#attr $alternates = (("Plain text version", "text/plain", "%s"),)' % txt_name) + html_file.write( + '#attr $alternates = (("Plain text version", "text/plain", "%s"),)' + % txt_name + ) html_file.write("\n") html_file.write("#def body_html\n") html_file.write("#raw\n") @@ -57,53 +76,33 @@ def add_headers(fname, first, prev, next, last): html_file.close() -home_file_types = ( - ("bashrc", "sh"), ("profile", "sh"), ("shellrc", "sh"), - ("mailcap", "conf"), ("mime.types", "conf"), - ("muttrc", "muttrc"), ("procmailrc", "procmail"), - ("gitconfig", "gitconfig"), ("vimrc", "vim"), -) - -mc_file_types = (("mc.ext", "conf"), ("menu", "conf")) - -python_file_types = (("init.py", "python"), - ("pdbrc", "python"), ("pdbrc.py", "python")) - def process_dotfile(i, fname, ftype, file_types): - if os.path.exists('.' + fname) and not os.path.exists(fname): - shutil.copy2('.' + fname, fname) - - if not os.path.exists(fname): - return - file = open(fname, 'r') text = file.read() file.close() - text = re.sub('cgmem_nice \d+ ', '', text) + text = re.sub('cgmem_nice \\d+ ', '', text) if fname in ('mailcap', 'mc.ext', 'menu'): - file = open(fname, 'w') text = text.replace('mplay', 'mplayer') text = text.replace('mplayerer', 'mplayer') if fname == 'mailcap': text = text.replace( - '-a -n "`which mplayer 2>/dev/null`" -a -n "`which mplayer 2>/dev/null`"', + '-a -n "`which mplayer 2>/dev/null`" ' + '-a -n "`which mplayer 2>/dev/null`"', '-a -n "`which mplayer 2>/dev/null`"') - file.write(text) - file.close() - - elif fname == 'vimrc': file = open(fname, 'w') - text = text.replace('\033', '') file.write(text) file.close() - os.system("""exec gvim -f -c "set filetype=%s" -c ":source %s/dotfile2html.vim" "%s" - """ % (ftype, home_dir, fname)) + os.system( + 'exec gvim -f -c "set filetype=%s" ' + '-c ":source %s/dotfile2html.vim" "%s"' + % (ftype, home_dir, fname) + ) first = file_types[0][0] - if i==0: + if i == 0: prev = None else: prev = file_types[i-1][0] @@ -114,7 +113,8 @@ def process_dotfile(i, fname, ftype, file_types): last = file_types[-1][0] add_headers(fname, first, prev, next, last) - if os.path.exists(fname + ".tmpl") and filecmp.cmp(fname + ".html", fname + ".tmpl"): + if os.path.exists(fname + ".tmpl") \ + and filecmp.cmp(fname + ".html", fname + ".tmpl"): os.remove(fname + ".html") else: os.rename(fname + ".html", fname + ".tmpl") @@ -124,16 +124,19 @@ def process_dotfile(i, fname, ftype, file_types): os.rename(fname, new_name) fname = new_name - if fname == 'vimrc' and os.path.exists('.' + fname): - shutil.copy2('.' + fname, fname) - if os.path.exists(fname): - os.chmod(fname, 0644) + os.chmod(fname, 0o644) + def process_files(file_types): for i, (fname, ftype) in enumerate(file_types): + if os.path.exists('.' + fname) and not os.path.exists(fname): + shutil.copy2('.' + fname, fname) + + if not os.path.exists(fname): + continue + process_dotfile(i, fname, ftype, file_types) -process_files(home_file_types) -process_files(mc_file_types) -process_files(python_file_types) + +process_files(home_file_types + mc_file_types + python_file_types)