Move hostfile line processing to new method.

This commit is contained in:
Darko Poljak 2016-08-19 21:56:24 +02:00
parent 72505e0f5f
commit 7f1e41f769

View file

@ -89,6 +89,25 @@ class Config(object):
self.local.create_files_dirs() self.local.create_files_dirs()
self.remote.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 @staticmethod
def hosts(source): def hosts(source):
"""Yield hosts from source. """Yield hosts from source.
@ -99,13 +118,7 @@ class Config(object):
import fileinput import fileinput
try: try:
for host in fileinput.input(files=(source)): for host in fileinput.input(files=(source)):
# remove comment if present host = Config.hostfile_process_line(host)
comment_index = host.find('#')
if comment_index >= 0:
host = host[:comment_index]
# remove leading and trailing whitespaces
host = host.strip()
# skip empty lines
if host: if host:
yield host yield host
except (IOError, OSError) as e: except (IOError, OSError) as e: