mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-08 19:34:17 +00:00
implement transformation parameters (#177)
This commit is contained in:
@@ -15,6 +15,7 @@ from dotdrop.dictparser import DictParser
|
||||
|
||||
class Cmd(DictParser):
|
||||
eq_ignore = ('log',)
|
||||
descr = 'command'
|
||||
|
||||
def __init__(self, key, action):
|
||||
"""constructor
|
||||
@@ -24,6 +25,43 @@ class Cmd(DictParser):
|
||||
self.key = key
|
||||
self.action = action
|
||||
|
||||
def execute(self, templater=None, debug=False):
|
||||
"""execute the command in the shell"""
|
||||
ret = 1
|
||||
action = self.action
|
||||
if templater:
|
||||
action = templater.generate_string(self.action)
|
||||
if debug:
|
||||
self.log.dbg('{} \"{}\" -> \"{}\"'.format(self.descr,
|
||||
self.action,
|
||||
action))
|
||||
cmd = action
|
||||
args = []
|
||||
if self.args:
|
||||
args = self.args
|
||||
if templater:
|
||||
args = [templater.generate_string(a) for a in args]
|
||||
try:
|
||||
cmd = action.format(*args)
|
||||
except IndexError:
|
||||
err = 'bad {}: \"{}\"'.format(self.descr, action)
|
||||
err += ' with \"{}\"'.format(args)
|
||||
self.log.warn(err)
|
||||
return False
|
||||
except KeyError:
|
||||
err = 'bad {}: \"{}\"'.format(self.descr, action)
|
||||
err += ' with \"{}\"'.format(args)
|
||||
self.log.warn(err)
|
||||
return False
|
||||
self.log.sub('executing \"{}\"'.format(cmd))
|
||||
try:
|
||||
ret = subprocess.call(cmd, shell=True)
|
||||
except KeyboardInterrupt:
|
||||
self.log.warn('{} interrupted'.format(self.descr))
|
||||
if ret != 0:
|
||||
self.log.warn('{} returned code {}'.format(self.descr, ret))
|
||||
return ret == 0
|
||||
|
||||
@classmethod
|
||||
def _adjust_yaml_keys(cls, value):
|
||||
return {'action': value}
|
||||
@@ -55,6 +93,7 @@ class Action(Cmd):
|
||||
|
||||
pre = 'pre'
|
||||
post = 'post'
|
||||
descr = 'action'
|
||||
|
||||
def __init__(self, key, kind, action):
|
||||
"""constructor
|
||||
@@ -66,6 +105,12 @@ class Action(Cmd):
|
||||
self.kind = kind
|
||||
self.args = []
|
||||
|
||||
def copy(self, args):
|
||||
"""return a copy of this object with arguments"""
|
||||
action = Action(self.key, self.kind, self.action)
|
||||
action.args = args
|
||||
return action
|
||||
|
||||
@classmethod
|
||||
def parse(cls, key, value):
|
||||
"""parse key value into object"""
|
||||
@@ -73,12 +118,6 @@ class Action(Cmd):
|
||||
v['kind'], v['action'] = value
|
||||
return cls(key=key, **v)
|
||||
|
||||
def copy(self, args):
|
||||
"""return a copy of this object with arguments"""
|
||||
action = Action(self.key, self.kind, self.action)
|
||||
action.args = args
|
||||
return action
|
||||
|
||||
def __str__(self):
|
||||
out = '{}: \"{}\" ({})'
|
||||
return out.format(self.key, self.action, self.kind)
|
||||
@@ -86,40 +125,23 @@ class Action(Cmd):
|
||||
def __repr__(self):
|
||||
return 'action({})'.format(self.__str__())
|
||||
|
||||
def execute(self, templater=None, debug=False):
|
||||
"""execute the action in the shell"""
|
||||
ret = 1
|
||||
action = self.action
|
||||
if templater:
|
||||
action = templater.generate_string(self.action)
|
||||
if debug:
|
||||
self.log.dbg('action \"{}\" -> \"{}\"'.format(self.action,
|
||||
action))
|
||||
cmd = action
|
||||
args = [templater.generate_string(a) for a in self.args]
|
||||
try:
|
||||
cmd = action.format(*args)
|
||||
except IndexError:
|
||||
err = 'bad action: \"{}\"'.format(action)
|
||||
err += ' with \"{}\"'.format(args)
|
||||
self.log.warn(err)
|
||||
return False
|
||||
except KeyError:
|
||||
err = 'bad action: \"{}\"'.format(action)
|
||||
err += ' with \"{}\"'.format(args)
|
||||
self.log.warn(err)
|
||||
return False
|
||||
self.log.sub('executing \"{}\"'.format(cmd))
|
||||
try:
|
||||
ret = subprocess.call(cmd, shell=True)
|
||||
except KeyboardInterrupt:
|
||||
self.log.warn('action interrupted')
|
||||
if ret != 0:
|
||||
self.log.warn('action returned code {}'.format(ret))
|
||||
return ret == 0
|
||||
|
||||
|
||||
class Transform(Cmd):
|
||||
descr = 'transformation'
|
||||
|
||||
def __init__(self, key, action):
|
||||
"""constructor
|
||||
@key: action key
|
||||
@trans: action string
|
||||
"""
|
||||
super(Transform, self).__init__(key, action)
|
||||
self.args = []
|
||||
|
||||
def copy(self, args):
|
||||
"""return a copy of this object with arguments"""
|
||||
trans = Transform(self.key, self.action)
|
||||
trans.args = args
|
||||
return trans
|
||||
|
||||
def transform(self, arg0, arg1, templater=None, debug=False):
|
||||
"""
|
||||
@@ -127,23 +149,13 @@ class Transform(Cmd):
|
||||
where {0} is the file to transform
|
||||
and {1} is the result file
|
||||
"""
|
||||
ret = 1
|
||||
action = self.action
|
||||
if templater:
|
||||
action = templater.generate_string(action)
|
||||
if debug:
|
||||
self.log.dbg('trans \"{}\" -> \"{}\"'.format(self.action,
|
||||
action))
|
||||
cmd = action.format(arg0, arg1)
|
||||
if os.path.exists(arg1):
|
||||
msg = 'transformation \"{}\": destination exists: {}'
|
||||
self.log.warn(msg.format(cmd, arg1))
|
||||
self.log.warn(msg.format(self.key, arg1))
|
||||
return False
|
||||
self.log.sub('transforming with \"{}\"'.format(cmd))
|
||||
try:
|
||||
ret = subprocess.call(cmd, shell=True)
|
||||
except KeyboardInterrupt:
|
||||
self.log.warn('transformation interrupted')
|
||||
if ret != 0:
|
||||
self.log.warn('transformation returned code {}'.format(ret))
|
||||
return ret == 0
|
||||
|
||||
if not self.args:
|
||||
self.args = []
|
||||
self.args.insert(0, arg1)
|
||||
self.args.insert(0, arg0)
|
||||
return self.execute(templater=templater, debug=debug)
|
||||
|
||||
@@ -102,9 +102,13 @@ class CfgAggregator:
|
||||
|
||||
# patch trans_w/trans_r in dotfiles
|
||||
self._patch_keys_to_objs(self.dotfiles,
|
||||
"trans_r", self._get_trans_r, islist=False)
|
||||
"trans_r",
|
||||
self._get_trans_w_args(self._get_trans_r),
|
||||
islist=False)
|
||||
self._patch_keys_to_objs(self.dotfiles,
|
||||
"trans_w", self._get_trans_w, islist=False)
|
||||
"trans_w",
|
||||
self._get_trans_w_args(self._get_trans_w),
|
||||
islist=False)
|
||||
|
||||
def _patch_keys_to_objs(self, containers, keys, get_by_key, islist=True):
|
||||
"""
|
||||
@@ -331,12 +335,29 @@ class CfgAggregator:
|
||||
# we have args
|
||||
key, *args = fields
|
||||
if self.debug:
|
||||
self.log.dbg('action with parm: {} and {}'.format(key, args))
|
||||
msg = 'action with parm: {} and {}'
|
||||
self.log.dbg(msg.format(key, args))
|
||||
action = self._get_action(key).copy(args)
|
||||
else:
|
||||
action = self._get_action(key)
|
||||
return action
|
||||
|
||||
def _get_trans_w_args(self, getter):
|
||||
"""return transformation by key with the arguments"""
|
||||
def getit(key):
|
||||
fields = shlex.split(key)
|
||||
if len(fields) > 1:
|
||||
# we have args
|
||||
key, *args = fields
|
||||
if self.debug:
|
||||
msg = 'trans with parm: {} and {}'
|
||||
self.log.dbg(msg.format(key, args))
|
||||
trans = getter(key).copy(args)
|
||||
else:
|
||||
trans = getter(key)
|
||||
return trans
|
||||
return getit
|
||||
|
||||
def _get_trans_r(self, key):
|
||||
"""return the trans_r with this key"""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user