1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-15 17:20:05 +00:00

adding a new Transform object to simplify trans/actions logic

This commit is contained in:
Ziirish
2018-06-01 22:11:49 +02:00
parent a867a850d8
commit aa3ad5e8ab
4 changed files with 33 additions and 24 deletions

View File

@@ -11,13 +11,25 @@ import os
from dotdrop.logger import Logger from dotdrop.logger import Logger
class Action: class Cmd:
def __init__(self, key, action): def __init__(self, key, action):
self.key = key self.key = key
self.action = action self.action = action
self.log = Logger() self.log = Logger()
def __str__(self):
return 'key:{} -> \"{}\"'.format(self.key, self.action)
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __hash__(self):
return hash(self.key) ^ hash(self.action)
class Action(Cmd):
def execute(self): def execute(self):
ret = 1 ret = 1
self.log.sub('executing \"{}\"'.format(self.action)) self.log.sub('executing \"{}\"'.format(self.action))
@@ -27,6 +39,9 @@ class Action:
self.log.warn('action interrupted') self.log.warn('action interrupted')
return ret == 0 return ret == 0
class Transform(Cmd):
def transform(self, arg0, arg1): def transform(self, arg0, arg1):
'''execute transformation with {0} and {1} '''execute transformation with {0} and {1}
where {0} is the file to transform and where {0} is the file to transform and
@@ -43,12 +58,3 @@ class Action:
except KeyboardInterrupt: except KeyboardInterrupt:
self.log.warn('action interrupted') self.log.warn('action interrupted')
return ret == 0 return ret == 0
def __str__(self):
return 'key:{} -> \"{}\"'.format(self.key, self.action)
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __hash__(self):
return hash(self.key) ^ hash(self.action)

View File

@@ -10,7 +10,7 @@ import os
# local import # local import
from dotdrop.dotfile import Dotfile from dotdrop.dotfile import Dotfile
from dotdrop.logger import Logger from dotdrop.logger import Logger
from dotdrop.action import Action from dotdrop.action import Action, Transform
class Cfg: class Cfg:
@@ -106,10 +106,8 @@ class Cfg:
res = { res = {
self.key_actions_pre: [], self.key_actions_pre: [],
self.key_actions_post: [], self.key_actions_post: [],
'actions': []
} }
for entry in entries: for entry in entries:
key = 'actions'
action = None action = None
if self.key_actions_pre in actions and \ if self.key_actions_pre in actions and \
entry in actions[self.key_actions_pre]: entry in actions[self.key_actions_pre]:
@@ -123,10 +121,21 @@ class Cfg:
self.log.warn('unknown action \"{}\"'.format(entry)) self.log.warn('unknown action \"{}\"'.format(entry))
continue continue
else: else:
key = self.key_actions_post
action = actions[entry] action = actions[entry]
res[key].append(action) res[key].append(action)
return res return res
def _parse_trans(self, trans, entries):
""" parse trans specified for an element """
res = []
for entry in entries:
if entry not in trans.keys():
self.log.warn('unknown trans \"{}\"'.format(entry))
continue
res.append(trans[entry])
return res
def _complete_configs(self): def _complete_configs(self):
""" set config defaults if not present """ """ set config defaults if not present """
if self.key_backup not in self.configs: if self.key_backup not in self.configs:
@@ -155,7 +164,7 @@ class Cfg:
if self.key_trans in self.content: if self.key_trans in self.content:
if self.content[self.key_trans] is not None: if self.content[self.key_trans] is not None:
for k, v in self.content[self.key_trans].items(): for k, v in self.content[self.key_trans].items():
self.trans[k] = Action(k, v) self.trans[k] = Transform(k, v)
# parse the profiles # parse the profiles
self.profiles = self.content[self.key_profiles] self.profiles = self.content[self.key_profiles]
@@ -183,7 +192,7 @@ class Cfg:
actions = self._parse_actions(self.actions, entries) actions = self._parse_actions(self.actions, entries)
entries = v[self.key_dotfiles_trans] if \ entries = v[self.key_dotfiles_trans] if \
self.key_dotfiles_trans in v else [] self.key_dotfiles_trans in v else []
trans = self._parse_actions(self.trans, entries) trans = self._parse_trans(self.trans, entries)
if len(trans) > 0 and link: if len(trans) > 0 and link:
msg = 'transformations disabled for \"{}\"'.format(dst) msg = 'transformations disabled for \"{}\"'.format(dst)
msg += ' because link is True' msg += ' because link is True'

View File

@@ -97,10 +97,10 @@ def install(opts, conf):
else: else:
src = dotfile.src src = dotfile.src
tmp = None tmp = None
if 'actions' in dotfile.trans and dotfile.trans['actions']: if dotfile.trans:
tmp = '{}.{}'.format(src, TRANS_SUFFIX) tmp = '{}.{}'.format(src, TRANS_SUFFIX)
err = False err = False
for trans in dotfile.trans['actions']: for trans in dotfile.trans:
LOG.dbg('executing transformation {}'.format(trans)) LOG.dbg('executing transformation {}'.format(trans))
s = os.path.join(opts['dotpath'], src) s = os.path.join(opts['dotpath'], src)
temp = os.path.join(opts['dotpath'], tmp) temp = os.path.join(opts['dotpath'], tmp)
@@ -120,12 +120,6 @@ def install(opts, conf):
if os.path.exists(tmp): if os.path.exists(tmp):
remove(tmp) remove(tmp)
if len(r) > 0: if len(r) > 0:
if 'actions' in dotfile.actions:
actions = dotfile.actions['actions']
# execute action
for action in actions:
LOG.dbg('executing action {}'.format(action))
action.execute()
if Cfg.key_actions_post in dotfile.actions: if Cfg.key_actions_post in dotfile.actions:
actions = dotfile.actions[Cfg.key_actions_post] actions = dotfile.actions[Cfg.key_actions_post]
# execute action # execute action

View File

@@ -8,7 +8,7 @@ represents a dotfile in dotdrop
class Dotfile: class Dotfile:
def __init__(self, key, dst, src, def __init__(self, key, dst, src,
actions={}, trans={}, link=False): actions={}, trans=[], link=False):
# key of dotfile in the config # key of dotfile in the config
self.key = key self.key = key
# where to install this dotfile # where to install this dotfile