Hog v10.58.0
datastore.tcl
Go to the documentation of this file.
1 
2 source [file join [file dirname [info script]] tobj.tcl]
3 
4 namespace eval DataStore {
5  variable _store_template {
6  namespace eval @DS@ {
7  variable _ctx
8  if {![info exists _ctx]} { set _ctx [tdict create] }
9 
10  proc Clear {} {
11  variable _ctx
12  set _ctx [tdict create]
13  }
14 
15  proc GetFullDataStore {} {
16  variable _ctx
17  return $_ctx
18  }
19 
20 
21  proc Load {tDictNode} {
22  if {![tobj isobj $tDictNode] || [tobj type $tDictNode] ne "Dict"} {
23  error "@DS@::Load: expected Dict tobj" "" {CTX_INVALID_ARGS}
24  }
25  variable _ctx
26  set _ctx $tDictNode
27  }
28 
29  proc SetStr {args} {
30  if {[llength $args] < 2} { error "@DS@::SetStr: too few args" "" {CTX_INVALID_ARGS} }
31  variable _ctx
32  tdict set _ctx {*}[lrange $args 0 end-1] [tobj String [lindex $args end]]
33  }
34 
35  proc SetNum {args} {
36  if {[llength $args] < 2} { error "@DS@::SetNum: too few args" "" {CTX_INVALID_ARGS} }
37  variable _ctx
38  tdict set _ctx {*}[lrange $args 0 end-1] [tobj Number [lindex $args end]]
39  }
40 
41  proc SetBool {args} {
42  if {[llength $args] < 2} { error "@DS@::SetBool: too few args" "" {CTX_INVALID_ARGS} }
43  variable _ctx
44  tdict set _ctx {*}[lrange $args 0 end-1] [tobj Bool [lindex $args end]]
45  }
46 
47  proc SetNull {args} {
48  if {[llength $args] < 1} { error "@DS@::SetNull: too few args" "" {CTX_INVALID_ARGS} }
49  variable _ctx
50  tdict set _ctx {*}$args [tobj Null]
51  }
52 
53  proc Set {args} {
54  if {[llength $args] < 2} { error "@DS@::Set: too few args" "" {CTX_INVALID_ARGS} }
55  variable _ctx
56  tdict set _ctx {*}[lrange $args 0 end-1] [lindex $args end]
57  }
58 
59  proc Lappend {args} {
60  if {[llength $args] < 2} { error "@DS@::Lappend: too few args" "" {CTX_INVALID_ARGS} }
61  variable _ctx
62  set tl [tdict get $_ctx {*}[lrange $args 0 end-1]]
63  if {![tobj isobj $tl] || [tobj type $tl] ne "List"} {
64  error "@DS@::Lappend: not a List node at path" "" {CTX_NOT_A_LIST}
65  }
66 
67  if {![tobj isobj [lindex $args end]]} {
68  error "@DS@::Lappend: expected tobj for value" "" {CTX_INVALID_ARGS}
69  }
70 
71  tlist append tl [lindex $args end]
72  tdict set _ctx {*}[lrange $args 0 end-1] $tl
73  }
74 
75  proc GetObj {args} {
76  if {[llength $args] < 1} { error "@DS@::GetObj: too few args" "" {CTX_INVALID_ARGS} }
77  variable _ctx
78  if {![tdict exists $_ctx {*}$args]} { error "@DS@::GetObj: key not found: $args" "" {CTX_NOT_FOUND} }
79  return [tdict get $_ctx {*}$args]
80  }
81 
82  proc GetObjOr {args} {
83  if {[llength $args] < 2} { error "@DS@::GetObjOr: too few args" "" {CTX_INVALID_ARGS} }
84  variable _ctx
85  set keys [lrange $args 0 end-1]
86  set defaultObj [lindex $args end]
87  if {[tdict exists $_ctx {*}$keys]} {
88  return [tdict get $_ctx {*}$keys]
89  }
90  return $defaultObj
91  }
92 
93  proc Get {args} {
94  if {[llength $args] < 1} { error "@DS@::Get: too few args" "" {CTX_INVALID_ARGS} }
95  variable _ctx
96  if {![tdict exists $_ctx {*}$args]} { error "@DS@::Get: key not found: $args" "" {CTX_NOT_FOUND} }
97  set _node [tdict get $_ctx {*}$args]
98  set _type [tobj type $_node]
99  if {$_type eq "Dict" || $_type eq "List"} { return $_node }
100  return [tobj value $_node]
101  }
102 
103  proc GetOr {args} {
104  if {[llength $args] < 2} { error "@DS@::GetOr: too few args" "" {CTX_INVALID_ARGS} }
105  variable _ctx
106  set keys [lrange $args 0 end-1]
107  set defaultObj [lindex $args end]
108  if {[tdict exists $_ctx {*}$keys]} {
109  set _node [tdict get $_ctx {*}$keys]
110  set _type [tobj type $_node]
111  if {$_type eq "Dict" || $_type eq "List"} { return $_node }
112  return [tobj value $_node]
113  }
114  return $defaultObj
115  }
116 
117  proc Exists {args} {
118  if {[llength $args] < 1} { error "@DS@::Exists: too few args" "" {CTX_INVALID_ARGS} }
119  variable _ctx
120  return [tdict exists $_ctx {*}$args]
121  }
122 
123  proc Remove {args} {
124  if {[llength $args] < 1} { error "@DS@::Remove: too few args" "" {CTX_INVALID_ARGS} }
125  variable _ctx
126  if {![tdict exists $_ctx {*}$args]} { return 0 }
127  set _ctx [tdict remove $_ctx {*}$args]
128  return 1
129  }
130 
131  proc Size {} {
132  variable _ctx
133  return [tdict size $_ctx]
134  }
135 
136  proc Keys {args} {
137  variable _ctx
138  if {[llength $args] == 0} { return [tdict keys $_ctx] }
139  if {![tdict exists $_ctx {*}$args]} { return {} }
140  set sub [tdict get $_ctx {*}$args]
141  if {![tobj isobj $sub] || [tobj type $sub] ne "Dict"} {
142  error "@DS@::Keys: not a Dict node at path" "" {CTX_NOT_A_DICT}
143  }
144  return [dict keys [tobj value $sub]]
145  }
146 
147  proc ToJson {{flag {}}} {
148  variable _ctx
149  if {$flag eq "-pretty"} { return [tobj tojson $_ctx -pretty] }
150  return [tobj tojson $_ctx]
151  }
152 
153  proc SaveJsonToFile {filename {overwrite 0}} {
154  variable _ctx
155  set json [ToJson -pretty]
156  if {[file exists $filename] && !$overwrite} {
157  error "@DS@::SaveJsonToFile: file exists" "" {CTX_FILE_EXISTS}
158  }
159  set fh [open $filename w]
160  puts $fh $json
161  close $fh
162  }
163 
164  proc SaveToFile {filename {overwrite 0}} {
165  variable _ctx
166  if {[file exists $filename] && !$overwrite} {
167  error "@DS@::SaveToFile: file exists" "" {CTX_FILE_EXISTS}
168  }
169  set fh [open $filename w]
170  puts $fh $_ctx
171  close $fh
172  }
173 
174  proc LoadFromFile {filename} {
175  if {![file exists $filename]} {
176  error "@DS@::LoadFromFile: file not found" "" {CTX_FILE_NOT_FOUND}
177  }
178  set fh [open $filename r]
179  set data [string trim [read $fh]]
180  close $fh
181  if {![tobj isobj $data] || [tobj type $data] ne "Dict"} {
182  error "@DS@::LoadFromFile: invalid context data" "" {CTX_INVALID_DATA}
183  }
184  #TODO: import json
185  variable _ctx
186  set _ctx $data
187  }
188  }
189 
190 
191  }
192 
193 
194  variable inIDE 0
195  variable _stores [list]
196  proc create {name} {
197  variable _store_template
198 
199  if {[namespace exists $name]} {
200  error "DataStore::create: namespace '$name' already exists" "" {NS_ALREADY_EXISTS}
201  }
202  variable _stores
203  lappend _stores $name
204  set ds [string map [list @DS@ $name] $_store_template]
205  uplevel #0 $ds
206  }
207 
208  proc Serialize {} {
209  variable _stores
210  set root [tdict create]
211  foreach name $_stores {
212  if {[namespace exists ::${name}]} {
213  tdict set root $name [${name}::GetFullDataStore]
214  }
215  }
216  return $root
217  }
218 
219  proc Deserialize {tDictRoot} {
220  if {![tobj isobj $tDictRoot] || [tobj type $tDictRoot] ne "Dict"} {
221  error "DataStore::Deserialize: expected Dict tobj" "" {CTX_INVALID_ARGS}
222  }
223  foreach name [tdict keys $tDictRoot] {
224  puts "Deserializing context store '$name'"
225  if {![namespace exists ::${name}]} { create $name }
226  ${name}::Load [tdict get $tDictRoot $name]
227  }
228 
229 
230  }
231 
232  proc _OnError {cmd } {
233  set root [Serialize]
234 
235  set exitCode [expr {[llength $cmd] > 1 ? [lindex $cmd 1] : 0}]
236  set exitInfo [tdict create]
237  tdict set exitInfo code [tobj Number $exitCode]
238  if {$exitCode != 0} {
239  tdict set exitInfo error_info [tobj String [expr {[info exists ::errorInfo] ? $::errorInfo : ""}]]
240  tdict set exitInfo error_code [tobj String [expr {[info exists ::errorCode] ? $::errorCode : ""}]]
241  }
242 
243  set locs [dict create]
244  for {set i 1} {$i <= [info frame]} {incr i} {
245  set f [info frame $i]
246  if {[dict exists $f level] && [dict exists $f file]} {
247  dict set locs [dict get $f level] "[file tail [dict get $f file]]:[dict get $f line]"
248  }
249  }
250  set stack_frames [list]
251  for {set i 1} {$i <= [info level]} {incr i} {
252  set loc [expr {[dict exists $locs $i] ? [dict get $locs $i] : "?"}]
253  lappend stack_frames "[info level $i] ($loc)"
254  }
255  tdict set exitInfo stack_trace [tlist create {*}$stack_frames]
256 
257  tdict set root _exit $exitInfo
258 
259  set json [tobj tojson $root -pretty]
260  set filename [file join [Repo::GetOr repo_path . ] .hog_context_error.json]
261  set fh [open $filename w]
262  puts $fh $json
263  close $fh
264  }
265 
266  proc _OnExit {cmd op} {
267  set root [Serialize]
268 
269  set exitCode [expr {[llength $cmd] > 1 ? [lindex $cmd 1] : 0}]
270  set exitInfo [tdict create]
271  tdict set exitInfo code [tobj Number $exitCode]
272  if {$exitCode != 0} {
273  tdict set exitInfo error_info [tobj String [expr {[info exists ::errorInfo] ? $::errorInfo : ""}]]
274  tdict set exitInfo error_code [tobj String [expr {[info exists ::errorCode] ? $::errorCode : ""}]]
275  }
276  tdict set root _exit $exitInfo
277 
278  set json [tobj tojson $root -pretty]
279  if {$DataStore::inIDE} {
280  set filename [file join [Repo::GetOr repo_path . ] .hog_context_ide_exit.json]
281  } else {
282  set filename [file join [Repo::GetOr repo_path . ] .hog_context_launcher_exit.json]
283  }
284  set fh [open $filename w]
285  puts $fh $json
286  close $fh
287  }
288 
289 
290  trace add execution exit enter DataStore::_OnExit
291 }