Πέμπτη 29 Ιανουαρίου 2015

Python with PlantUML


import subprocess
import urllib.request
import webbrowser
from threading import Timer

class Watchdog:
    def __init__(self, timeout, userHandler=None):  # timeout in seconds
        self.timeout = timeout
        self.handler = userHandler if userHandler is not None else self.defaultHandler
        self.timer = Timer(self.timeout, self.handler)

    def reset(self):
        self.timer.cancel()
        self.timer = Timer(self.timeout, self.handler)

    def stop(self):
        self.timer.cancel()

    def defaultHandler(self):
        raise self

def test_vpn():
    fo = open("test_vpn.html", "w")
    fo.write("<html>")
    fo.write("\n")
    fo.write("<head>Test script for vpn connection based on IKEv2</head>")
    fo.write("\n")
    fo.write("<body>")
    fo.write("\n")
    fo.write("<p>Initiator-[#brown]>Responder: VID+phase1 policies</p>")
    test_steps(1)
    fo.write("\n")
    fo.write("<img src=\"IKEv2_flow.png\" alt=\"Oups, something went wrong with IKEv2_flow\">")
    fo.write("\n")
    fo.write("</body>")
    fo.write("\n")
    fo.write("</html>")
    fo.close()
    filename = webbrowser.open_new_tab('test_vpn.html')
    webbrowser.open_new_tab(filename)
   
   
def test_steps(step):
    if (step==1):
        fo2 = open("IKEv2_flow.txt", "w")
        fo2.write("@startuml")
        fo2.write("\n")
        fo2.write("title VPN connection based on IKEv2")
        fo2.write("\n")
        fo2.write("autonumber")
        fo2.write("\n")
        fo2.write("participant Initiator as Initiator")
        fo2.write("\n")
        fo2.write("participant Responder as Responder")
        fo2.write("\n")
        fo2.write("Initiator-[#brown]>Responder: VID+phase1 policies")
        fo2.write("\n")
        fo2.write("@enduml")
        fo2.close()
        watchdog = Watchdog(10)
        try:
            proc1 = subprocess.Popen(['java', '-jar', 'plantuml.jar'])
        except Watchdog:
          proc1.kill()
        watchdog.stop()
#main
test_vpn()

Usage if you regularly execute something and want to make sure it is executed at least every y seconds:
def myHandler():
  print "Whoa! Watchdog expired. Holy heavens!"
  sys.exit()

watchdog = Watchdog(y, myHandler)

def doSomethingRegularly():
  # make sure you do not return in here or call watchdog.reset() before returning
  watchdog.reset()
--------------------------------------------------------------------------------------------------------------------------
@startuml
title VPN connection based on IKEv2
autonumber
participant Initiator as Initiator
participant Responder as Responder
Initiator-[#brown]>Responder: VID+phase1 policies
@enduml
--------------------------------------------------------------------------------------------------------------------------
<html>
<head>Test script for vpn connection based on IKEv2</head>
<body>
<p>Initiator-[#brown]>Responder: VID+phase1 policies</p>
<img src="IKEv2_flow.png" alt="Oups, something went wrong with IKEv2_flow">
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------