From 7f1e41f76904ee1d53646396e9f8e4253ca0b9be Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Fri, 19 Aug 2016 21:56:24 +0200 Subject: [PATCH] Move hostfile line processing to new method. --- cdist/config.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/cdist/config.py b/cdist/config.py index 8d83a072..2f28a22f 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -89,6 +89,25 @@ class Config(object): self.local.create_files_dirs() self.remote.create_files_dirs() + @staticmethod + def hostfile_process_line(line): + """Return host from read line or None if no host present.""" + if not line: + return None + # remove comment if present + comment_index = line.find('#') + if comment_index >= 0: + host = line[:comment_index] + else: + host = line + # remove leading and trailing whitespaces + host = host.strip() + # skip empty lines + if host: + return host + else: + return None + @staticmethod def hosts(source): """Yield hosts from source. @@ -99,13 +118,7 @@ class Config(object): import fileinput try: for host in fileinput.input(files=(source)): - # remove comment if present - comment_index = host.find('#') - if comment_index >= 0: - host = host[:comment_index] - # remove leading and trailing whitespaces - host = host.strip() - # skip empty lines + host = Config.hostfile_process_line(host) if host: yield host except (IOError, OSError) as e: