cdist/cdist/hostsource.py

77 lines
2.1 KiB
Python
Raw Permalink Normal View History

2016-12-03 17:12:38 +00:00
# -*- coding: utf-8 -*-
#
# 2016-2017 Darko Poljak (darko.poljak at gmail.com)
2016-12-03 17:12:38 +00:00
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
#
import fileinput
def hostfile_process_line(line, strip_func=str.strip):
"""Return entry from read line or None if no entry present."""
if not line:
return None
# remove comment if present
comment_index = line.find('#')
if comment_index >= 0:
foo = line[:comment_index]
else:
foo = line
# remove leading and trailing whitespaces
foo = strip_func(foo)
# skip empty lines
if foo:
return foo
else:
return None
2016-12-03 17:12:38 +00:00
class HostSource(object):
"""
Host source object.
Source can be a sequence or filename (stdin if \'-\').
In case of filename each line represents one host.
"""
def __init__(self, source):
self.source = source
def _process_file_line(self, line):
return hostfile_process_line(line)
2016-12-03 17:12:38 +00:00
def _hosts_from_sequence(self):
for host in self.source:
yield host
def _hosts_from_file(self):
for line in fileinput.input(files=(self.source)):
host = self._process_file_line(line)
if host:
yield host
def hosts(self):
2016-12-07 17:39:43 +00:00
if not self.source:
2016-12-03 17:12:38 +00:00
return
if isinstance(self.source, str):
yield from self._hosts_from_file()
else:
yield from self._hosts_from_sequence()
def __call__(self):
yield from self.hosts()