mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-07 12:13:32 +00:00
refactor the parsing
This commit is contained in:
@@ -6,15 +6,23 @@ represents a dotfile in dotdrop
|
||||
"""
|
||||
|
||||
from dotdrop.linktypes import LinkTypes
|
||||
from dotdrop.dictparser import DictParser
|
||||
from dotdrop.action import Action
|
||||
|
||||
|
||||
class Dotfile:
|
||||
class Dotfile(DictParser):
|
||||
"""Represent a dotfile."""
|
||||
# dotfile keys
|
||||
key_noempty = 'ignoreempty'
|
||||
key_trans_r = 'trans'
|
||||
key_trans_w = 'trans_write'
|
||||
|
||||
def __init__(self, key, dst, src,
|
||||
actions={}, trans_r=None, trans_w=None,
|
||||
actions=[], trans_r=[], trans_w=[],
|
||||
link=LinkTypes.NOLINK, cmpignore=[],
|
||||
noempty=False, upignore=[]):
|
||||
"""constructor
|
||||
"""
|
||||
constructor
|
||||
@key: dotfile key
|
||||
@dst: dotfile dst (in user's home usually)
|
||||
@src: dotfile src (in dotpath)
|
||||
@@ -26,39 +34,73 @@ class Dotfile:
|
||||
@noempty: ignore empty template if True
|
||||
@upignore: patterns to ignore when updating
|
||||
"""
|
||||
self.key = key
|
||||
self.dst = dst
|
||||
self.src = src
|
||||
self.link = link
|
||||
# ensure link of right type
|
||||
if type(link) != LinkTypes:
|
||||
raise Exception('bad value for link: {}'.format(link))
|
||||
self.actions = actions
|
||||
self.cmpignore = cmpignore
|
||||
self.dst = dst
|
||||
self.key = key
|
||||
self.link = LinkTypes.get(link)
|
||||
self.noempty = noempty
|
||||
self.src = src
|
||||
self.trans_r = trans_r
|
||||
self.trans_w = trans_w
|
||||
self.cmpignore = cmpignore
|
||||
self.noempty = noempty
|
||||
self.upignore = upignore
|
||||
|
||||
def get_vars(self):
|
||||
"""return this dotfile templating vars"""
|
||||
_vars = {}
|
||||
_vars['_dotfile_abs_src'] = self.src
|
||||
_vars['_dotfile_abs_dst'] = self.dst
|
||||
_vars['_dotfile_key'] = self.key
|
||||
_vars['_dotfile_link'] = self.link.name.lower()
|
||||
def get_dotfile_variables(self):
|
||||
"""return this dotfile specific variables"""
|
||||
return {
|
||||
'_dotfile_abs_src': self.src,
|
||||
'_dotfile_abs_dst': self.dst,
|
||||
'_dotfile_key': self.key,
|
||||
'_dotfile_link': str(self.link),
|
||||
}
|
||||
|
||||
return _vars
|
||||
def get_pre_actions(self):
|
||||
"""return all 'pre' actions"""
|
||||
return [a for a in self.actions if a.kind == Action.pre]
|
||||
|
||||
def __str__(self):
|
||||
msg = 'key:\"{}\", src:\"{}\", dst:\"{}\", link:\"{}\"'
|
||||
return msg.format(self.key, self.src, self.dst, self.link.name.lower())
|
||||
def get_post_actions(self):
|
||||
"""return all 'post' actions"""
|
||||
return [a for a in self.actions if a.kind == Action.post]
|
||||
|
||||
def __repr__(self):
|
||||
return 'dotfile({})'.format(self.__str__())
|
||||
def get_trans_r(self):
|
||||
"""return trans_r object"""
|
||||
if self.trans_r:
|
||||
return self.trans_r[0]
|
||||
return None
|
||||
|
||||
def get_trans_w(self):
|
||||
"""return trans_w object"""
|
||||
if self.trans_w:
|
||||
return self.trans_w[0]
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def _adjust_yaml_keys(cls, value):
|
||||
"""patch dict"""
|
||||
value['noempty'] = value.get(cls.key_noempty, False)
|
||||
value['trans_r'] = value.get(cls.key_trans_r)
|
||||
if value['trans_r']:
|
||||
# ensure is a list
|
||||
value['trans_r'] = [value['trans_r']]
|
||||
value['trans_w'] = value.get(cls.key_trans_w)
|
||||
if value['trans_w']:
|
||||
# ensure is a list
|
||||
value['trans_w'] = [value['trans_w']]
|
||||
# remove old entries
|
||||
value.pop(cls.key_noempty, None)
|
||||
value.pop(cls.key_trans_r, None)
|
||||
value.pop(cls.key_trans_w, None)
|
||||
return value
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.__dict__ == other.__dict__
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.dst) ^ hash(self.src) ^ hash(self.key)
|
||||
|
||||
def __str__(self):
|
||||
msg = 'key:\"{}\", src:\"{}\", dst:\"{}\", link:\"{}\"'
|
||||
return msg.format(self.key, self.src, self.dst, str(self.link))
|
||||
|
||||
def __repr__(self):
|
||||
return 'dotfile({!s})'.format(self)
|
||||
|
||||
Reference in New Issue
Block a user