mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-11 13:44:16 +00:00
refactoring
This commit is contained in:
@@ -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"""
|
"""install dotfiles for this profile"""
|
||||||
dotfiles = conf.get_dotfiles(opts['profile'])
|
dotfiles = conf.get_dotfiles(opts['profile'])
|
||||||
if keys:
|
if keys:
|
||||||
@@ -139,45 +139,7 @@ def install(opts, conf, temporary=False, keys=[]):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def apply_trans(opts, dotfile):
|
def cmd_compare(opts, conf, tmp, focus=[], ignore=[]):
|
||||||
"""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=[]):
|
|
||||||
"""compare dotfiles and return True if all identical"""
|
"""compare dotfiles and return True if all identical"""
|
||||||
dotfiles = conf.get_dotfiles(opts['profile'])
|
dotfiles = conf.get_dotfiles(opts['profile'])
|
||||||
if dotfiles == []:
|
if dotfiles == []:
|
||||||
@@ -241,7 +203,7 @@ def compare(opts, conf, tmp, focus=[], ignore=[]):
|
|||||||
return same
|
return same
|
||||||
|
|
||||||
|
|
||||||
def update(opts, conf, paths):
|
def cmd_update(opts, conf, paths):
|
||||||
"""update the dotfile(s) from path(s)"""
|
"""update the dotfile(s) from path(s)"""
|
||||||
updater = Updater(conf, opts['dotpath'], opts['dry'],
|
updater = Updater(conf, opts['dotpath'], opts['dry'],
|
||||||
opts['safe'], opts['debug'])
|
opts['safe'], opts['debug'])
|
||||||
@@ -249,7 +211,7 @@ def update(opts, conf, paths):
|
|||||||
updater.update(path, opts['profile'])
|
updater.update(path, opts['profile'])
|
||||||
|
|
||||||
|
|
||||||
def importer(opts, conf, paths):
|
def cmd_importer(opts, conf, paths):
|
||||||
"""import dotfile(s) from paths"""
|
"""import dotfile(s) from paths"""
|
||||||
home = os.path.expanduser(TILD)
|
home = os.path.expanduser(TILD)
|
||||||
cnt = 0
|
cnt = 0
|
||||||
@@ -309,7 +271,7 @@ def importer(opts, conf, paths):
|
|||||||
LOG.log('\n{} file(s) imported.'.format(cnt))
|
LOG.log('\n{} file(s) imported.'.format(cnt))
|
||||||
|
|
||||||
|
|
||||||
def list_profiles(conf):
|
def cmd_list_profiles(conf):
|
||||||
"""list all profiles"""
|
"""list all profiles"""
|
||||||
LOG.log('Available profile(s):')
|
LOG.log('Available profile(s):')
|
||||||
for p in conf.get_profiles():
|
for p in conf.get_profiles():
|
||||||
@@ -317,7 +279,7 @@ def list_profiles(conf):
|
|||||||
LOG.log('')
|
LOG.log('')
|
||||||
|
|
||||||
|
|
||||||
def list_files(opts, conf, templateonly=False):
|
def cmd_list_files(opts, conf, templateonly=False):
|
||||||
"""list all dotfiles for a specific profile"""
|
"""list all dotfiles for a specific profile"""
|
||||||
if not opts['profile'] in conf.get_profiles():
|
if not opts['profile'] in conf.get_profiles():
|
||||||
LOG.warn('unknown profile \"{}\"'.format(opts['profile']))
|
LOG.warn('unknown profile \"{}\"'.format(opts['profile']))
|
||||||
@@ -336,13 +298,60 @@ def list_files(opts, conf, templateonly=False):
|
|||||||
LOG.sub('{}'.format(dotfile.dst))
|
LOG.sub('{}'.format(dotfile.dst))
|
||||||
LOG.log('')
|
LOG.log('')
|
||||||
|
|
||||||
|
###########################################################
|
||||||
|
# helpers
|
||||||
|
###########################################################
|
||||||
|
|
||||||
def header():
|
|
||||||
|
def _header():
|
||||||
"""print the header"""
|
"""print the header"""
|
||||||
LOG.log(BANNER)
|
LOG.log(BANNER)
|
||||||
LOG.log('')
|
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():
|
def main():
|
||||||
"""entry point"""
|
"""entry point"""
|
||||||
ret = True
|
ret = True
|
||||||
@@ -371,40 +380,39 @@ def main():
|
|||||||
if ENV_NOBANNER not in os.environ \
|
if ENV_NOBANNER not in os.environ \
|
||||||
and opts['banner'] \
|
and opts['banner'] \
|
||||||
and not args['--no-banner']:
|
and not args['--no-banner']:
|
||||||
header()
|
_header()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
if args['list']:
|
if args['list']:
|
||||||
# list existing profiles
|
# list existing profiles
|
||||||
list_profiles(conf)
|
cmd_list_profiles(conf)
|
||||||
|
|
||||||
elif args['listfiles']:
|
elif args['listfiles']:
|
||||||
# list files for selected profile
|
# list files for selected profile
|
||||||
list_files(opts, conf, templateonly=args['--template'])
|
cmd_list_files(opts, conf, templateonly=args['--template'])
|
||||||
|
|
||||||
elif args['install']:
|
elif args['install']:
|
||||||
# install the dotfiles stored in dotdrop
|
# install the dotfiles stored in dotdrop
|
||||||
keys = args['<key>']
|
ret = cmd_install(opts, conf, temporary=args['--temp'],
|
||||||
ret = install(opts, conf, temporary=args['--temp'],
|
keys=args['<key>'])
|
||||||
keys=keys)
|
|
||||||
|
|
||||||
elif args['compare']:
|
elif args['compare']:
|
||||||
# compare local dotfiles with dotfiles stored in dotdrop
|
# compare local dotfiles with dotfiles stored in dotdrop
|
||||||
tmp = get_tmpdir()
|
tmp = get_tmpdir()
|
||||||
opts['dopts'] = args['--dopts']
|
opts['dopts'] = args['--dopts']
|
||||||
ret = compare(opts, conf, tmp, focus=args['--file'],
|
ret = cmd_compare(opts, conf, tmp, focus=args['--file'],
|
||||||
ignore=args['--ignore'])
|
ignore=args['--ignore'])
|
||||||
# clean tmp directory
|
# clean tmp directory
|
||||||
remove(tmp)
|
remove(tmp)
|
||||||
|
|
||||||
elif args['import']:
|
elif args['import']:
|
||||||
# import dotfile(s)
|
# import dotfile(s)
|
||||||
importer(opts, conf, args['<paths>'])
|
cmd_importer(opts, conf, args['<paths>'])
|
||||||
|
|
||||||
elif args['update']:
|
elif args['update']:
|
||||||
# update a dotfile
|
# update a dotfile
|
||||||
update(opts, conf, args['<paths>'])
|
cmd_update(opts, conf, args['<paths>'])
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
LOG.err('interrupted')
|
LOG.err('interrupted')
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ import os
|
|||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from dotdrop.config import Cfg
|
from dotdrop.config import Cfg
|
||||||
from dotdrop.dotdrop import importer
|
from dotdrop.dotdrop import cmd_importer
|
||||||
from dotdrop.dotdrop import compare
|
from dotdrop.dotdrop import cmd_compare
|
||||||
from dotdrop.dotfile import Dotfile
|
from dotdrop.dotfile import Dotfile
|
||||||
from dotdrop.installer import Installer
|
from dotdrop.installer import Installer
|
||||||
from dotdrop.comparator import Comparator
|
from dotdrop.comparator import Comparator
|
||||||
@@ -103,7 +103,7 @@ class TestCompare(unittest.TestCase):
|
|||||||
dfiles = [d1, d2, d3, d4, d5]
|
dfiles = [d1, d2, d3, d4, d5]
|
||||||
|
|
||||||
# import the files
|
# import the files
|
||||||
importer(opts, conf, dfiles)
|
cmd_importer(opts, conf, dfiles)
|
||||||
conf, opts = load_config(confpath, profile)
|
conf, opts = load_config(confpath, profile)
|
||||||
|
|
||||||
# compare the files
|
# compare the files
|
||||||
@@ -138,10 +138,10 @@ class TestCompare(unittest.TestCase):
|
|||||||
self.assertTrue(results == expected)
|
self.assertTrue(results == expected)
|
||||||
|
|
||||||
# test compare from dotdrop
|
# test compare from dotdrop
|
||||||
self.assertFalse(compare(opts, conf, tmp))
|
self.assertFalse(cmd_compare(opts, conf, tmp))
|
||||||
# test focus
|
# test focus
|
||||||
self.assertFalse(compare(opts, conf, tmp, focus=d4))
|
self.assertFalse(cmd_compare(opts, conf, tmp, focus=d4))
|
||||||
self.assertFalse(compare(opts, conf, tmp, focus='/tmp/fake'))
|
self.assertFalse(cmd_compare(opts, conf, tmp, focus='/tmp/fake'))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
@@ -9,11 +9,11 @@ import unittest
|
|||||||
import os
|
import os
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from dotdrop.dotdrop import importer
|
from dotdrop.dotdrop import cmd_importer
|
||||||
from dotdrop.dotdrop import list_profiles
|
from dotdrop.dotdrop import cmd_list_profiles
|
||||||
from dotdrop.dotdrop import list_files
|
from dotdrop.dotdrop import cmd_list_files
|
||||||
from dotdrop.dotdrop import header
|
from dotdrop.dotdrop import _header
|
||||||
from dotdrop.dotdrop import update
|
from dotdrop.dotdrop import cmd_update
|
||||||
from dotdrop.config import Cfg
|
from dotdrop.config import Cfg
|
||||||
|
|
||||||
from tests.helpers import *
|
from tests.helpers import *
|
||||||
@@ -107,11 +107,11 @@ class TestImport(unittest.TestCase):
|
|||||||
|
|
||||||
# import the dotfiles
|
# import the dotfiles
|
||||||
dfiles = [dotfile1, dotfile2, dotfile3, dotfile4, dotfile5]
|
dfiles = [dotfile1, dotfile2, dotfile3, dotfile4, dotfile5]
|
||||||
importer(opts, conf, dfiles)
|
cmd_importer(opts, conf, dfiles)
|
||||||
# import symlink
|
# import symlink
|
||||||
opts[Cfg.key_dotfiles_link] = True
|
opts[Cfg.key_dotfiles_link] = True
|
||||||
sfiles = [dotfile6, dotfile7]
|
sfiles = [dotfile6, dotfile7]
|
||||||
importer(opts, conf, sfiles)
|
cmd_importer(opts, conf, sfiles)
|
||||||
opts[Cfg.key_dotfiles_link] = False
|
opts[Cfg.key_dotfiles_link] = False
|
||||||
|
|
||||||
# reload the config
|
# reload the config
|
||||||
@@ -193,15 +193,15 @@ class TestImport(unittest.TestCase):
|
|||||||
self.assertTrue(os.path.islink(dotfile7))
|
self.assertTrue(os.path.islink(dotfile7))
|
||||||
self.assertTrue(os.path.realpath(dotfile7) == indt7)
|
self.assertTrue(os.path.realpath(dotfile7) == indt7)
|
||||||
|
|
||||||
list_profiles(conf)
|
cmd_list_profiles(conf)
|
||||||
list_files(opts, conf)
|
cmd_list_files(opts, conf)
|
||||||
header()
|
_header()
|
||||||
|
|
||||||
# fake test update
|
# fake test update
|
||||||
editcontent = 'edited'
|
editcontent = 'edited'
|
||||||
edit_content(dotfile1, editcontent)
|
edit_content(dotfile1, editcontent)
|
||||||
opts['safe'] = False
|
opts['safe'] = False
|
||||||
update(opts, conf, [dotfile1])
|
cmd_update(opts, conf, [dotfile1])
|
||||||
c2 = open(indt1, 'r').read()
|
c2 = open(indt1, 'r').read()
|
||||||
self.assertTrue(editcontent == c2)
|
self.assertTrue(editcontent == c2)
|
||||||
|
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ import filecmp
|
|||||||
|
|
||||||
from tests.helpers import *
|
from tests.helpers import *
|
||||||
from dotdrop.dotfile import Dotfile
|
from dotdrop.dotfile import Dotfile
|
||||||
from dotdrop.dotdrop import install
|
|
||||||
from dotdrop.installer import Installer
|
from dotdrop.installer import Installer
|
||||||
from dotdrop.action import Action
|
from dotdrop.action import Action
|
||||||
|
from dotdrop.dotdrop import cmd_install
|
||||||
|
|
||||||
|
|
||||||
class TestInstall(unittest.TestCase):
|
class TestInstall(unittest.TestCase):
|
||||||
@@ -176,7 +176,7 @@ exec bspwm
|
|||||||
opts['debug'] = True
|
opts['debug'] = True
|
||||||
opts['showdiff'] = True
|
opts['showdiff'] = True
|
||||||
opts['variables'] = {}
|
opts['variables'] = {}
|
||||||
install(opts, conf)
|
cmd_install(opts, conf)
|
||||||
|
|
||||||
# now compare the generated files
|
# now compare the generated files
|
||||||
self.assertTrue(os.path.exists(dst1))
|
self.assertTrue(os.path.exists(dst1))
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ import os
|
|||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from dotdrop.config import Cfg
|
from dotdrop.config import Cfg
|
||||||
from dotdrop.dotdrop import importer
|
from dotdrop.dotdrop import cmd_update
|
||||||
from dotdrop.dotdrop import update
|
from dotdrop.dotdrop import cmd_importer
|
||||||
from dotdrop.dotfile import Dotfile
|
from dotdrop.dotfile import Dotfile
|
||||||
|
|
||||||
from tests.helpers import *
|
from tests.helpers import *
|
||||||
@@ -68,7 +68,7 @@ class TestUpdate(unittest.TestCase):
|
|||||||
dfiles = [d1, dir1]
|
dfiles = [d1, dir1]
|
||||||
|
|
||||||
# import the files
|
# import the files
|
||||||
importer(opts, conf, dfiles)
|
cmd_importer(opts, conf, dfiles)
|
||||||
conf, opts = load_config(confpath, profile)
|
conf, opts = load_config(confpath, profile)
|
||||||
|
|
||||||
# edit the files
|
# edit the files
|
||||||
@@ -86,7 +86,7 @@ class TestUpdate(unittest.TestCase):
|
|||||||
# update it
|
# update it
|
||||||
opts['safe'] = False
|
opts['safe'] = False
|
||||||
opts['debug'] = True
|
opts['debug'] = True
|
||||||
update(opts, conf, [d1, dir1])
|
cmd_update(opts, conf, [d1, dir1])
|
||||||
|
|
||||||
# test content
|
# test content
|
||||||
newcontent = open(d1, 'r').read()
|
newcontent = open(d1, 'r').read()
|
||||||
|
|||||||
Reference in New Issue
Block a user