-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdnsclean.py
More file actions
100 lines (78 loc) · 2.82 KB
/
Copy pathdnsclean.py
File metadata and controls
100 lines (78 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/env python
# -*- coding: utf-8 *-*
# Copyright (c) Former03 GmbH
# See LICENSE for details.
"""txStateMachine
Reads commands from the stdin and returns results.
"""
from twisted.internet import stdio, reactor
from twisted.protocols import basic
from os import linesep
from config import states, startState, endStates
class Echo(basic.LineReceiver):
delimiter = linesep
currentState = None
startState = None
endStates = {}
sharedData = {}
states = {}
def makeConnection(self, transport):
print "making Connection..."
self.currentState = self.getInitialState()
basic.LineReceiver.makeConnection(self, transport)
def connectionMade(self):
self.transport.write(self.startState.render())
def lineReceived(self, line):
# ignore blank lines.
if not line: return
# go out if the user wants us to.
if line in ('exit', 'quit', 'aus', 'off'):
self.endSession()
else:
nextState, data = self.currentState.process(line)
self.currentState = self.states.get(nextState)(data)
print self.endStates.keys(), nextState
if nextState in self.endStates.keys():
self.sendLine(self.currentState.render())
self.endSession()
else:
self.transport.write(self.currentState.render())
def getInitialState(self):
if not self.startState:
raise NotImplemented("startState is missing")
return self.startState
def endSession(self):
self.sendLine('Goodbye!')
self.transport.loseConnection()
def addStates(self, states):
for name, state in states.iteritems():
self.addState(state)
def addState(self, state):
self.states[state.name] = state
if state.isStart:
if self.startState is not None:
raise NotImplemented("Self startnode already exists!")
else:
self.startState = state
if state.isEnd:
self.endStates[state.name] = state
def setStart(self, state):
if self.startState is not None:
raise NotImplemented("Startnode already set!")
self.startState = state
def setEndStates(self, endStates):
self.endStates = endStates
def connectionLost(self, reason):
# stop the reactor, only because this is meant to be run in Stdio.
reactor.stop()
def main():
try:
stateMachine = Echo()
stateMachine.addStates(states)
except Exception, e:
print "Some error occured: {err}".format(err=e.getErrorMessage())
sys.exit("Quitting due to error in programming")
stdio.StandardIO(stateMachine)
reactor.run()
if __name__ == '__main__':
main()