diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 7dbf9a6..6f0fd86 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -77,7 +77,7 @@ Options: ########################################################### -def install(opts, conf, temporary=False, keys=[]): +def cmd_install(opts, conf, temporary=False, keys=[]): """install dotfiles for this profile""" dotfiles = conf.get_dotfiles(opts['profile']) if keys: @@ -139,45 +139,7 @@ def install(opts, conf, temporary=False, keys=[]): return True -def apply_trans(opts, dotfile): - """apply the transformation to the dotfile - return None if fails and new source if succeed""" - src = dotfile.src - new_src = '{}.{}'.format(src, TRANS_SUFFIX) - err = False - for trans in dotfile.trans: - if opts['debug']: - LOG.dbg('executing transformation {}'.format(trans)) - s = os.path.join(opts['dotpath'], src) - temp = os.path.join(opts['dotpath'], new_src) - if not trans.transform(s, temp): - msg = 'transformation \"{}\" failed for {}' - LOG.err(msg.format(trans.key, dotfile.key)) - err = True - break - if err: - if new_src and os.path.exists(new_src): - remove(new_src) - return None - return new_src - - -def _select(selections, dotfiles): - selected = [] - for selection in selections: - df = next( - (x for x in dotfiles - if os.path.expanduser(x.dst) == os.path.expanduser(selection)), - None - ) - if df: - selected.append(df) - else: - LOG.err('no dotfile matches \"{}\"'.format(selection)) - return selected - - -def compare(opts, conf, tmp, focus=[], ignore=[]): +def cmd_compare(opts, conf, tmp, focus=[], ignore=[]): """compare dotfiles and return True if all identical""" dotfiles = conf.get_dotfiles(opts['profile']) if dotfiles == []: @@ -241,7 +203,7 @@ def compare(opts, conf, tmp, focus=[], ignore=[]): return same -def update(opts, conf, paths): +def cmd_update(opts, conf, paths): """update the dotfile(s) from path(s)""" updater = Updater(conf, opts['dotpath'], opts['dry'], opts['safe'], opts['debug']) @@ -249,7 +211,7 @@ def update(opts, conf, paths): updater.update(path, opts['profile']) -def importer(opts, conf, paths): +def cmd_importer(opts, conf, paths): """import dotfile(s) from paths""" home = os.path.expanduser(TILD) cnt = 0 @@ -309,7 +271,7 @@ def importer(opts, conf, paths): LOG.log('\n{} file(s) imported.'.format(cnt)) -def list_profiles(conf): +def cmd_list_profiles(conf): """list all profiles""" LOG.log('Available profile(s):') for p in conf.get_profiles(): @@ -317,7 +279,7 @@ def list_profiles(conf): LOG.log('') -def list_files(opts, conf, templateonly=False): +def cmd_list_files(opts, conf, templateonly=False): """list all dotfiles for a specific profile""" if not opts['profile'] in conf.get_profiles(): LOG.warn('unknown profile \"{}\"'.format(opts['profile'])) @@ -336,13 +298,60 @@ def list_files(opts, conf, templateonly=False): LOG.sub('{}'.format(dotfile.dst)) LOG.log('') +########################################################### +# helpers +########################################################### -def header(): + +def _header(): """print the header""" LOG.log(BANNER) LOG.log('') +def _select(selections, dotfiles): + selected = [] + for selection in selections: + df = next( + (x for x in dotfiles + if os.path.expanduser(x.dst) == os.path.expanduser(selection)), + None + ) + if df: + selected.append(df) + else: + LOG.err('no dotfile matches \"{}\"'.format(selection)) + return selected + + +def apply_trans(opts, dotfile): + """apply the transformation to the dotfile + return None if fails and new source if succeed""" + src = dotfile.src + new_src = '{}.{}'.format(src, TRANS_SUFFIX) + err = False + for trans in dotfile.trans: + if opts['debug']: + LOG.dbg('executing transformation {}'.format(trans)) + s = os.path.join(opts['dotpath'], src) + temp = os.path.join(opts['dotpath'], new_src) + if not trans.transform(s, temp): + msg = 'transformation \"{}\" failed for {}' + LOG.err(msg.format(trans.key, dotfile.key)) + err = True + break + if err: + if new_src and os.path.exists(new_src): + remove(new_src) + return None + return new_src + + +########################################################### +# main +########################################################### + + def main(): """entry point""" ret = True @@ -371,40 +380,39 @@ def main(): if ENV_NOBANNER not in os.environ \ and opts['banner'] \ and not args['--no-banner']: - header() + _header() try: if args['list']: # list existing profiles - list_profiles(conf) + cmd_list_profiles(conf) elif args['listfiles']: # list files for selected profile - list_files(opts, conf, templateonly=args['--template']) + cmd_list_files(opts, conf, templateonly=args['--template']) elif args['install']: # install the dotfiles stored in dotdrop - keys = args[''] - ret = install(opts, conf, temporary=args['--temp'], - keys=keys) + ret = cmd_install(opts, conf, temporary=args['--temp'], + keys=args['']) elif args['compare']: # compare local dotfiles with dotfiles stored in dotdrop tmp = get_tmpdir() opts['dopts'] = args['--dopts'] - ret = compare(opts, conf, tmp, focus=args['--file'], - ignore=args['--ignore']) + ret = cmd_compare(opts, conf, tmp, focus=args['--file'], + ignore=args['--ignore']) # clean tmp directory remove(tmp) elif args['import']: # import dotfile(s) - importer(opts, conf, args['']) + cmd_importer(opts, conf, args['']) elif args['update']: # update a dotfile - update(opts, conf, args['']) + cmd_update(opts, conf, args['']) except KeyboardInterrupt: LOG.err('interrupted') diff --git a/tests/test_compare.py b/tests/test_compare.py index 3e04ed2..3b0165a 100644 --- a/tests/test_compare.py +++ b/tests/test_compare.py @@ -10,8 +10,8 @@ import os import yaml from dotdrop.config import Cfg -from dotdrop.dotdrop import importer -from dotdrop.dotdrop import compare +from dotdrop.dotdrop import cmd_importer +from dotdrop.dotdrop import cmd_compare from dotdrop.dotfile import Dotfile from dotdrop.installer import Installer from dotdrop.comparator import Comparator @@ -103,7 +103,7 @@ class TestCompare(unittest.TestCase): dfiles = [d1, d2, d3, d4, d5] # import the files - importer(opts, conf, dfiles) + cmd_importer(opts, conf, dfiles) conf, opts = load_config(confpath, profile) # compare the files @@ -138,10 +138,10 @@ class TestCompare(unittest.TestCase): self.assertTrue(results == expected) # test compare from dotdrop - self.assertFalse(compare(opts, conf, tmp)) + self.assertFalse(cmd_compare(opts, conf, tmp)) # test focus - self.assertFalse(compare(opts, conf, tmp, focus=d4)) - self.assertFalse(compare(opts, conf, tmp, focus='/tmp/fake')) + self.assertFalse(cmd_compare(opts, conf, tmp, focus=d4)) + self.assertFalse(cmd_compare(opts, conf, tmp, focus='/tmp/fake')) def main(): diff --git a/tests/test_import.py b/tests/test_import.py index 75b0edc..bb49605 100644 --- a/tests/test_import.py +++ b/tests/test_import.py @@ -9,11 +9,11 @@ import unittest import os import yaml -from dotdrop.dotdrop import importer -from dotdrop.dotdrop import list_profiles -from dotdrop.dotdrop import list_files -from dotdrop.dotdrop import header -from dotdrop.dotdrop import update +from dotdrop.dotdrop import cmd_importer +from dotdrop.dotdrop import cmd_list_profiles +from dotdrop.dotdrop import cmd_list_files +from dotdrop.dotdrop import _header +from dotdrop.dotdrop import cmd_update from dotdrop.config import Cfg from tests.helpers import * @@ -107,11 +107,11 @@ class TestImport(unittest.TestCase): # import the dotfiles dfiles = [dotfile1, dotfile2, dotfile3, dotfile4, dotfile5] - importer(opts, conf, dfiles) + cmd_importer(opts, conf, dfiles) # import symlink opts[Cfg.key_dotfiles_link] = True sfiles = [dotfile6, dotfile7] - importer(opts, conf, sfiles) + cmd_importer(opts, conf, sfiles) opts[Cfg.key_dotfiles_link] = False # reload the config @@ -193,15 +193,15 @@ class TestImport(unittest.TestCase): self.assertTrue(os.path.islink(dotfile7)) self.assertTrue(os.path.realpath(dotfile7) == indt7) - list_profiles(conf) - list_files(opts, conf) - header() + cmd_list_profiles(conf) + cmd_list_files(opts, conf) + _header() # fake test update editcontent = 'edited' edit_content(dotfile1, editcontent) opts['safe'] = False - update(opts, conf, [dotfile1]) + cmd_update(opts, conf, [dotfile1]) c2 = open(indt1, 'r').read() self.assertTrue(editcontent == c2) diff --git a/tests/test_install.py b/tests/test_install.py index 237d473..e1b15ec 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -9,9 +9,9 @@ import filecmp from tests.helpers import * from dotdrop.dotfile import Dotfile -from dotdrop.dotdrop import install from dotdrop.installer import Installer from dotdrop.action import Action +from dotdrop.dotdrop import cmd_install class TestInstall(unittest.TestCase): @@ -176,7 +176,7 @@ exec bspwm opts['debug'] = True opts['showdiff'] = True opts['variables'] = {} - install(opts, conf) + cmd_install(opts, conf) # now compare the generated files self.assertTrue(os.path.exists(dst1)) diff --git a/tests/test_update.py b/tests/test_update.py index 6f7dfc6..9d0133e 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -10,8 +10,8 @@ import os import yaml from dotdrop.config import Cfg -from dotdrop.dotdrop import importer -from dotdrop.dotdrop import update +from dotdrop.dotdrop import cmd_update +from dotdrop.dotdrop import cmd_importer from dotdrop.dotfile import Dotfile from tests.helpers import * @@ -68,7 +68,7 @@ class TestUpdate(unittest.TestCase): dfiles = [d1, dir1] # import the files - importer(opts, conf, dfiles) + cmd_importer(opts, conf, dfiles) conf, opts = load_config(confpath, profile) # edit the files @@ -86,7 +86,7 @@ class TestUpdate(unittest.TestCase): # update it opts['safe'] = False opts['debug'] = True - update(opts, conf, [d1, dir1]) + cmd_update(opts, conf, [d1, dir1]) # test content newcontent = open(d1, 'r').read()