-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_state.py
More file actions
121 lines (80 loc) · 2.51 KB
/
Copy pathweb_state.py
File metadata and controls
121 lines (80 loc) · 2.51 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
###########################################################
#
# Copyright (c) 2005, Southpaw Technology
# All Rights Reserved
#
# PROPRIETARY INFORMATION. This software is proprietary to
# Southpaw Technology, and is not to be reproduced, transmitted,
# or disclosed in any way without written permission.
#
#
#
__all__ = ['WebState']
'''Class that stores the global environmental state that a given widget lives
in. This state can be stacked
Usage:
# a widget will want to define a state and pass it through the hierarchy
web_state = WebState.get()
state = {
'search_key': search_key,
'process': process
}
web_state.push(state)
# later a widget will be able to retrieve the state as such
web_state = WebState.get()
search_key = web_state.get_value("search_key")
# common usage would be for a widget to only define states in the
# get_display method. This ensures proper hierarchical propogation of
# the state
def get_display(my):
state = {}
web_state.push(state)
div = DivWdg()
# need to pass through add
xx = Whatever()
div.add(xx, use_state=True)
web_state.pop()
return div
'''
from pyasm.common import Container
class WebState(object):
def __init__(my):
my.states = []
my.push()
def push(my, state=None):
if state:
my.current_state = state
else:
my.current_state = {}
my.states.append( my.current_state )
def get_current(my):
return my.current_state
def pop(my):
my.current_state = my.states.pop()
return my.states.pop()
def set_value(my, name, value):
my.current_state[name] = value
def get_value(my, name):
return my.current_state.get(name)
# DEPRECATED
def add_state(my, name, value):
my.current_state[name] = value
# DEPRECATED
def get_state(my, name):
if my.current_state.has_key(name):
return my.current_state[name]
else:
return ""
# DEPRECATED
def add_state_to_url(my, url):
# add all of the states to a link
for name, value in my.current_state.items():
url.set_option(name, value)
def get():
# try getting from the web from
state = Container.get("WebState")
if not state:
state = WebState()
Container.put("WebState", state)
return state
get = staticmethod(get)