forked from jon-jacky/PyModel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebModel.py
More file actions
86 lines (61 loc) · 2.16 KB
/
WebModel.py
File metadata and controls
86 lines (61 loc) · 2.16 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
"""
WebModel, loosely based on C# WebApplication sample at nmodel.codeplex.com
BUT this model is synchronous - Login is not split into start, finish actions
"""
### Model
# State, with initial values
mode = 'Initializing' # can't call it 'state', that has special meaning
usersLoggedIn = list()
userToInt = dict()
# Actions and enabling conditions
def Initialize():
global mode
mode = 'Running'
def InitializeEnabled():
return mode == 'Initializing'
def Login(user, password):
global usersLoggedIn
if (password == 'Correct'):
usersLoggedIn.append(user)
return 'Success'
else:
return 'Failure'
def LoginEnabled(user, password): # arg list must match Action
return (mode == 'Running' and user not in usersLoggedIn)
def Logout(user):
global usersLoggedIn
usersLoggedIn.remove(user)
def LogoutEnabled(user):
return (mode == 'Running' and user in usersLoggedIn)
def UpdateInt(user, number):
global userToInt
userToInt[user] = number
def UpdateIntEnabled(user, number):
return (mode == 'Running' and user in usersLoggedIn)
def ReadInt(user):
return userToInt.get(user, 0) # return default 0 if user not in dictionary
def ReadIntEnabled(user):
return (mode == 'Running' and user in usersLoggedIn)
def Accepting():
return not usersLoggedIn
### Metadata
state = ('mode', 'usersLoggedIn', 'userToInt')
actions = (Initialize, Login, Logout, UpdateInt, ReadInt)
enablers = { Initialize:(InitializeEnabled,), Login:(LoginEnabled,),
Logout:(LogoutEnabled,), UpdateInt:(UpdateIntEnabled,),
ReadInt:(ReadIntEnabled,) }
cleanup = (Logout,)
# default domains
users = ['VinniPuhh', 'OleBrumm']
passwords = ['Correct', 'Incorrect']
numbers = [ 1, 2 ] # different from default 0
domains = { Login: {'user':users, 'password':passwords},
Logout: {'user':users},
UpdateInt: {'user':users, 'number':numbers},
ReadInt: {'user':users} }
# needed for multiple test runs in one session
def Reset():
global mode, usersLoggedin, userToInt
mode = 'Initializing'
del usersLoggedIn[:]
userToInt.clear()