opennel-pymanager/tests/simulate_program.py

82 lines
2.5 KiB
Python
Executable file

#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Script use to simulate opennel program (input/output terminal)
# Copyright (C) 2017 AleaJactaEst
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import argparse
import sys
import time
import signal
class ManageSignal:
def __init__(self):
self.kill_now = False
def activate(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self,signum, frame):
self.kill_now = True
class SimulateProgram():
def __init__(self):
self.line = 0
def print_output(self, message):
self.line += 1
print(self.line, message)
def main(self, noloop, timeout, refuse_kill):
manageSignal = ManageSignal()
if refuse_kill:
manageSignal.activate()
loop = not noloop
self.print_output("Initializing")
self.print_output("Starting")
self.print_output("Started")
while loop is True:
try:
msg = input()
self.print_output(msg)
except (KeyboardInterrupt, EOFError):
loop = refuse_kill
time.sleep(timeout)
self.print_output("End")
def main(args=sys.argv[1:]):
""" Main function
:param list args: root password
"""
parser = argparse.ArgumentParser(description='Simulate program')
parser.add_argument('--no-loop', action='store_true',
help='disable loop', default=False)
parser.add_argument('--timeout', type=int,
default=10, help='timeout')
parser.add_argument('--disable-kill', action='store_true',
help='disable loop', default=False)
args = parser.parse_args()
simulate = SimulateProgram()
simulate.main(args.no_loop, args.timeout, args.disable_kill)
if __name__ == '__main__':
main()