Move hostfile line processing to new method.
This commit is contained in:
parent
72505e0f5f
commit
7f1e41f769
1 changed files with 20 additions and 7 deletions
|
@ -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:
|
||||||
|
|
Loading…
Reference in a new issue