add new message object
Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
This commit is contained in:
parent
ca1c5ff713
commit
22a83d2c93
1 changed files with 66 additions and 0 deletions
66
cdist/core/message.py
Normal file
66
cdist/core/message.py
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# 2013 Nico Schottelius (nico-cdist at schottelius.org)
|
||||||
|
#
|
||||||
|
# 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 logging
|
||||||
|
import shutil
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
import cdist
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class Message(object):
|
||||||
|
"""Support messaging between types
|
||||||
|
|
||||||
|
"""
|
||||||
|
def __init__(self, prefix, global_messages):
|
||||||
|
self.prefix = prefix
|
||||||
|
self.global_messages = global_messages
|
||||||
|
|
||||||
|
self.messages_in = tempfile.mkstemp(suffix='.cdist_message_in')
|
||||||
|
self.messages_out = tempfile.mkstemp(suffix='.cdist_message_out')
|
||||||
|
|
||||||
|
shutil.copyfile(self.global_messages, self.messages_in)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def env(self, env):
|
||||||
|
env = {}
|
||||||
|
env['__messages_in'] = self.messages_in
|
||||||
|
env['__messages_out'] = self.messages_out
|
||||||
|
|
||||||
|
return env
|
||||||
|
|
||||||
|
def _cleanup(self):
|
||||||
|
os.remove(self.messages_in)
|
||||||
|
os.remove(self.messages_out)
|
||||||
|
|
||||||
|
def _merge_messages(self):
|
||||||
|
with open(self.messages_in) as fd:
|
||||||
|
content = fd.readlines()
|
||||||
|
|
||||||
|
with open(self.global_messages, 'a') as fd:
|
||||||
|
for line in content:
|
||||||
|
fd.write("%s:%s" % (self.prefix, line))
|
||||||
|
|
||||||
|
def merge_messages(self):
|
||||||
|
self._merge_messages()
|
||||||
|
self._cleanup()
|
Loading…
Reference in a new issue