mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-05 00:59:42 +00:00
add import and trans{_r,_w}
This commit is contained in:
@@ -56,28 +56,36 @@ class CfgAggregator:
|
||||
"""remove this dotfile from this profile"""
|
||||
return self.cfgyaml.del_dotfile_from_profile(dotfile.key, profile.key)
|
||||
|
||||
def new_dotfile(self, src, dst, link, chmod=None):
|
||||
def new_dotfile(self, src, dst, link, chmod=None,
|
||||
trans_read=None, trans_write=None):
|
||||
"""
|
||||
import a new dotfile
|
||||
@src: path in dotpath
|
||||
@dst: path in FS
|
||||
@link: LinkType
|
||||
@chmod: file permission
|
||||
@trans_read: read transformation
|
||||
@trans_write: write transformation
|
||||
"""
|
||||
dst = self.path_to_dotfile_dst(dst)
|
||||
dotfile = self.get_dotfile_by_src_dst(src, dst)
|
||||
if not dotfile:
|
||||
dotfile = self._create_new_dotfile(src, dst, link, chmod=chmod)
|
||||
# add the dotfile
|
||||
dotfile = self._create_new_dotfile(src, dst, link, chmod=chmod,
|
||||
trans_read=trans_read,
|
||||
trans_write=trans_write)
|
||||
|
||||
if not dotfile:
|
||||
return False
|
||||
|
||||
# add to profile
|
||||
key = dotfile.key
|
||||
ret = self.cfgyaml.add_dotfile_to_profile(key, self.profile_key)
|
||||
if ret:
|
||||
msg = 'new dotfile {} to profile {}'
|
||||
self.log.dbg(msg.format(key, self.profile_key))
|
||||
|
||||
# save the config and reload it
|
||||
if ret:
|
||||
self._save_and_reload()
|
||||
return ret
|
||||
@@ -205,15 +213,26 @@ class CfgAggregator:
|
||||
# accessors for public methods
|
||||
########################################################
|
||||
|
||||
def _create_new_dotfile(self, src, dst, link, chmod=None):
|
||||
def _create_new_dotfile(self, src, dst, link, chmod=None,
|
||||
trans_read=None, trans_write=None):
|
||||
"""create a new dotfile"""
|
||||
# get a new dotfile with a unique key
|
||||
key = self._get_new_dotfile_key(dst)
|
||||
self.log.dbg('new dotfile key: {}'.format(key))
|
||||
# add the dotfile
|
||||
if not self.cfgyaml.add_dotfile(key, src, dst, link, chmod=chmod):
|
||||
trans_r_key = trans_w_key = None
|
||||
if trans_read:
|
||||
trans_r_key = trans_read.key
|
||||
if trans_write:
|
||||
trans_w_key = trans_write.key
|
||||
if not self.cfgyaml.add_dotfile(key, src, dst, link,
|
||||
chmod=chmod,
|
||||
trans_r_key=trans_r_key,
|
||||
trans_w_key=trans_w_key):
|
||||
return None
|
||||
return Dotfile(key, dst, src)
|
||||
return Dotfile(key, dst, src,
|
||||
trans_r=trans_read,
|
||||
trans_w=trans_write)
|
||||
|
||||
########################################################
|
||||
# parsing
|
||||
@@ -282,7 +301,7 @@ class CfgAggregator:
|
||||
# patch trans_w/trans_r in dotfiles
|
||||
self._patch_keys_to_objs(self.dotfiles,
|
||||
"trans_r",
|
||||
self._get_trans_w_args(self._get_trans_r),
|
||||
self._get_trans_w_args(self.get_trans_r),
|
||||
islist=False)
|
||||
self._patch_keys_to_objs(self.dotfiles,
|
||||
"trans_w",
|
||||
@@ -453,7 +472,7 @@ class CfgAggregator:
|
||||
return trans
|
||||
return getit
|
||||
|
||||
def _get_trans_r(self, key):
|
||||
def get_trans_r(self, key):
|
||||
"""return the trans_r with this key"""
|
||||
try:
|
||||
return next(x for x in self.trans_r if x.key == key)
|
||||
|
||||
@@ -394,7 +394,8 @@ class CfgYaml:
|
||||
self._dirty = True
|
||||
return True
|
||||
|
||||
def add_dotfile(self, key, src, dst, link, chmod=None):
|
||||
def add_dotfile(self, key, src, dst, link, chmod=None,
|
||||
trans_r_key=None, trans_w_key=None):
|
||||
"""add a new dotfile"""
|
||||
if key in self.dotfiles.keys():
|
||||
return False
|
||||
@@ -404,10 +405,15 @@ class CfgYaml:
|
||||
self._dbg('new dotfile dst: {}'.format(dst))
|
||||
self._dbg('new dotfile link: {}'.format(link))
|
||||
self._dbg('new dotfile chmod: {}'.format(chmod))
|
||||
self._dbg('new dotfile trans_r: {}'.format(trans_r_key))
|
||||
self._dbg('new dotfile trans_w: {}'.format(trans_w_key))
|
||||
|
||||
# create the dotfile dict
|
||||
df_dict = {
|
||||
self.key_dotfile_src: src,
|
||||
self.key_dotfile_dst: dst,
|
||||
}
|
||||
|
||||
# link
|
||||
dfl = self.settings[self.key_settings_link_dotfile_default]
|
||||
if str(link) != dfl:
|
||||
@@ -417,6 +423,15 @@ class CfgYaml:
|
||||
if chmod:
|
||||
df_dict[self.key_dotfile_chmod] = str(format(chmod, 'o'))
|
||||
|
||||
# trans_r/trans_w
|
||||
if trans_r_key:
|
||||
df_dict[self.key_trans_r] = str(trans_r_key)
|
||||
if trans_w_key:
|
||||
df_dict[self.key_trans_w] = str(trans_w_key)
|
||||
|
||||
if self._debug:
|
||||
self._dbg('dotfile dict: {}'.format(df_dict))
|
||||
|
||||
# add to global dict
|
||||
self._yaml_dict[self.key_dotfiles][key] = df_dict
|
||||
self._dirty = True
|
||||
|
||||
@@ -529,6 +529,7 @@ def cmd_importer(opts):
|
||||
paths = opts.import_path
|
||||
importer = Importer(opts.profile, opts.conf,
|
||||
opts.dotpath, opts.diff_command,
|
||||
opts.variables,
|
||||
dry=opts.dry, safe=opts.safe,
|
||||
debug=opts.debug,
|
||||
keepdot=opts.keepdot,
|
||||
@@ -538,7 +539,8 @@ def cmd_importer(opts):
|
||||
tmpret = importer.import_path(path, import_as=opts.import_as,
|
||||
import_link=opts.import_link,
|
||||
import_mode=opts.import_mode,
|
||||
import_transw=opts.import_transw)
|
||||
import_transw=opts.import_transw,
|
||||
import_transr=opts.import_transr)
|
||||
if tmpret < 0:
|
||||
ret = False
|
||||
elif tmpret > 0:
|
||||
|
||||
@@ -15,19 +15,21 @@ from dotdrop.utils import strip_home, get_default_file_perms, \
|
||||
get_unique_tmp_name, removepath
|
||||
from dotdrop.linktypes import LinkTypes
|
||||
from dotdrop.comparator import Comparator
|
||||
from dotdrop.templategen import Templategen
|
||||
|
||||
|
||||
class Importer:
|
||||
"""dotfile importer"""
|
||||
|
||||
def __init__(self, profile, conf, dotpath, diff_cmd,
|
||||
dry=False, safe=True, debug=False,
|
||||
variables, dry=False, safe=True, debug=False,
|
||||
keepdot=True, ignore=None):
|
||||
"""constructor
|
||||
@profile: the selected profile
|
||||
@conf: configuration manager
|
||||
@dotpath: dotfiles dotpath
|
||||
@diff_cmd: diff command to use
|
||||
@variables: dictionary of variables for the templates
|
||||
@dry: simulate
|
||||
@safe: ask for overwrite if True
|
||||
@debug: enable debug
|
||||
@@ -38,19 +40,25 @@ class Importer:
|
||||
self.conf = conf
|
||||
self.dotpath = dotpath
|
||||
self.diff_cmd = diff_cmd
|
||||
self.variables = variables
|
||||
self.dry = dry
|
||||
self.safe = safe
|
||||
self.debug = debug
|
||||
self.keepdot = keepdot
|
||||
self.ignore = ignore or []
|
||||
|
||||
self.templater = Templategen(variables=self.variables,
|
||||
base=self.dotpath,
|
||||
debug=self.debug)
|
||||
|
||||
self.umask = get_umask()
|
||||
self.log = Logger(debug=self.debug)
|
||||
|
||||
def import_path(self, path, import_as=None,
|
||||
import_link=LinkTypes.NOLINK,
|
||||
import_mode=False,
|
||||
import_transw=""):
|
||||
import_transw="",
|
||||
import_transr=""):
|
||||
"""
|
||||
import a dotfile pointed by path
|
||||
returns:
|
||||
@@ -66,18 +74,22 @@ class Importer:
|
||||
|
||||
# check transw if any
|
||||
trans_write = None
|
||||
trans_read = None
|
||||
if import_transw:
|
||||
trans_write = self.conf.get_trans_w(import_transw)
|
||||
if import_transr:
|
||||
trans_read = self.conf.get_trans_r(import_transr)
|
||||
|
||||
return self._import(path, import_as=import_as,
|
||||
import_link=import_link,
|
||||
import_mode=import_mode,
|
||||
trans_write=trans_write)
|
||||
trans_write=trans_write,
|
||||
trans_read=trans_read)
|
||||
|
||||
def _import(self, path, import_as=None,
|
||||
import_link=LinkTypes.NOLINK,
|
||||
import_mode=False,
|
||||
trans_write=None):
|
||||
trans_write=None, trans_read=None):
|
||||
"""
|
||||
import path
|
||||
returns:
|
||||
@@ -135,13 +147,14 @@ class Importer:
|
||||
if not self._import_file(src, dst, trans_write=trans_write):
|
||||
return -1
|
||||
|
||||
# TODO add trans_write
|
||||
# TODO add trans_read too
|
||||
return self._import_in_config(path, src, dst, perm, linktype,
|
||||
import_mode)
|
||||
import_mode,
|
||||
trans_w=trans_write,
|
||||
trans_r=trans_read)
|
||||
|
||||
def _import_in_config(self, path, src, dst, perm,
|
||||
linktype, import_mode):
|
||||
linktype, import_mode,
|
||||
trans_r=None, trans_w=None):
|
||||
"""
|
||||
import path
|
||||
returns:
|
||||
@@ -159,7 +172,9 @@ class Importer:
|
||||
chmod = perm
|
||||
|
||||
# add file to config file
|
||||
retconf = self.conf.new_dotfile(src, dst, linktype, chmod=chmod)
|
||||
retconf = self.conf.new_dotfile(src, dst, linktype, chmod=chmod,
|
||||
trans_read=trans_r,
|
||||
trans_write=trans_w)
|
||||
if not retconf:
|
||||
self.log.warn('\"{}\" ignored during import'.format(path))
|
||||
return 0
|
||||
@@ -292,7 +307,8 @@ class Importer:
|
||||
return path
|
||||
self.log.dbg('executing write transformation {}'.format(trans))
|
||||
tmp = get_unique_tmp_name()
|
||||
if not trans.transform(path, tmp, debug=self.debug):
|
||||
if not trans.transform(path, tmp, debug=self.debug,
|
||||
templater=self.templater):
|
||||
msg = 'transformation \"{}\" failed for {}'
|
||||
self.log.err(msg.format(trans.key, path))
|
||||
if os.path.exists(tmp):
|
||||
|
||||
@@ -60,7 +60,8 @@ Usage:
|
||||
dotdrop install [-VbtfndDaW] [-c <path>] [-p <profile>]
|
||||
[-w <nb>] [<key>...]
|
||||
dotdrop import [-Vbdfm] [-c <path>] [-p <profile>] [-i <pattern>...]
|
||||
[-l <link>] [-S <key>] [-s <path>] <path>...
|
||||
[--transr=<key>] [--transw=<key>]
|
||||
[-l <link>] [-s <path>] <path>...
|
||||
dotdrop compare [-LVbz] [-c <path>] [-p <profile>]
|
||||
[-w <nb>] [-C <file>...] [-i <pattern>...]
|
||||
dotdrop update [-VbfdkPz] [-c <path>] [-p <profile>]
|
||||
@@ -90,7 +91,8 @@ Options:
|
||||
-p --profile=<profile> Specify the profile to use [default: {}].
|
||||
-P --show-patch Provide a one-liner to manually patch template.
|
||||
-s --as=<path> Import as a different path from actual path.
|
||||
-S --transw=<key> Apply trans_write key on import.
|
||||
--transr=<key> Associate trans_read key on import.
|
||||
--transw=<key> Apply trans_write key on import.
|
||||
-t --temp Install to a temporary directory for review.
|
||||
-T --template Only template dotfiles.
|
||||
-V --verbose Be verbose.
|
||||
@@ -275,6 +277,7 @@ class Options(AttrMonitor):
|
||||
self.import_ignore.append('*{}'.format(self.install_backup_suffix))
|
||||
self.import_ignore = uniq_list(self.import_ignore)
|
||||
self.import_transw = self.args['--transw']
|
||||
self.import_transr = self.args['--transr']
|
||||
|
||||
def _apply_args_update(self):
|
||||
"""update specifics"""
|
||||
|
||||
Reference in New Issue
Block a user