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
1 changed files with 20 additions and 7 deletions

View File

@ -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: