renommer le script

This commit is contained in:
Jephté Clain 2017-06-15 02:24:17 +04:00
parent f3c028005a
commit 5a3504e141
1 changed files with 37 additions and 17 deletions

View File

@ -174,15 +174,26 @@ class HostObject(object):
def get_attr(self, name): return self.attrs.get(name, None) def get_attr(self, name): return self.attrs.get(name, None)
def set_attr(self, name, value): def set_attr(self, name, value):
if name not in self.attrs: self.attrs[name] = [] if name not in self.attrs: self.attrs[name] = []
self.attrs[name].append(value) if value not in self.attrs[name]: self.attrs[name].append(value)
def reset_attr(self, name, value=None): def reset_attr(self, name, value=None):
if name in self.attrs: del self.attrs[name] if name in self.attrs: del self.attrs[name]
if value is not None: self.set_attr(name, value) if value is not None: self.set_attr(name, value)
def copy_attrs(self, other, reset=True): def add_attr(self, name, value):
if name not in self.attrs: self.attrs[name] = []
self.attrs[name].append(value)
def del_attr(self, name, value):
if name not in self.attrs: return
self.attrs[name].remove(value)
if not self.attrs[name]: del self.attrs[name]
def modify_attr(self, name, value, method='set'):
if method == 'set': self.set_attr(name, value)
elif method == 'reset': self.reset_attr(name, value)
elif method == 'add': self.add_attr(name, value)
elif method == 'del': self.del_attr(name, value)
def copy_attrs(self, other, method='set'):
for name, values in other.attrs.items(): for name, values in other.attrs.items():
for value in values: for value in values:
if reset: self.reset_attr(name, value) self.modify_attr(name, value, method)
else: self.set_attr(name, value)
def split_scalar(arg): def split_scalar(arg):
if '=' in arg: if '=' in arg:
@ -192,10 +203,16 @@ def split_scalar(arg):
value = None value = None
if name.endswith('%'): if name.endswith('%'):
name = name[:-1] name = name[:-1]
reset = True method = 'reset'
elif name.endswith('+'):
name = name[:-1]
method = 'add'
elif name.endswith('-'):
name = name[:-1]
method = 'del'
else: else:
reset = False method = 'set'
return name, reset, value return name, method, value
def split_list(arg): def split_list(arg):
if '=' in arg: if '=' in arg:
@ -259,25 +276,28 @@ class Parser(object):
- until: définir un groupe qui va jusqu'à la prochaine ligne 'group' - until: définir un groupe qui va jusqu'à la prochaine ligne 'group'
""" """
otype = args[0:1] and args[0] or 'host' otype = args[0:1] and args[0] or 'host'
if otype not in self.groups:
raise ValueError('%s: invalid object type' % otype)
gtype = args[1:2] and args[1] or 'until' gtype = args[1:2] and args[1] or 'until'
self.groups[otype]['type'] = gtype self.groups[otype]['type'] = gtype
if gtype == 'defaults': if gtype == 'defaults':
self.groups[otype]['current'] = {None: True} self.groups[otype]['current'] = {None: True}
elif gtype in ('once', 'until'): elif gtype in ('once', 'until'):
self.groups[otype]['current'] = {} self.groups[otype]['current'] = {}
else:
raise ValueError('%s: invalid group type' % gtype)
############################################################################ ############################################################################
def handle_attr(self, args): def handle_attr(self, args):
otype = self.attr_otype otype = self.attr_otype
assert otype is not None, "attr_otype should not be None" assert otype is not None, "attr_otype should not be None"
for nv in args: for nv in args:
name, reset, value = split_scalar(nv) name, method, value = split_scalar(nv)
if value is None: if value is None:
reset = True method = 'set'
value = '1' value = '1'
for id in self.groups[otype]['current']: for id in self.groups[otype]['current']:
if reset: self.objects[otype][id].reset_attr(name) self.objects[otype][id].modify_attr(name, value, method)
self.objects[otype][id].set_attr(name, value)
############################################################################ ############################################################################
def handle_default_profile(self, args): def handle_default_profile(self, args):
@ -355,14 +375,14 @@ class Parser(object):
for nvs in nvss: for nvs in nvss:
name, values = split_list(nvs) name, values = split_list(nvs)
for host in values: for host in values:
dir = None
if host:
if '/' in host: if '/' in host:
dir, host = path.split(path.abspath(path.expanduser(host))) dir, host = path.split(path.abspath(path.expanduser(host)))
else:
dir = None
host = self.__fix_host(host) host = self.__fix_host(host)
if name: if name:
ho = self.__host_object(name) ho = self.__host_object(name)
ho.add_host(host) if host: ho.add_host(host)
ho.copy_attrs(default_ho) ho.copy_attrs(default_ho)
if dir is not None: ho.reset_attr('dir', dir) if dir is not None: ho.reset_attr('dir', dir)
self.add_group('host', name) self.add_group('host', name)