Hog v10.58.0
context.tcl
Go to the documentation of this file.
1 
2 # This will be used to maintain project context throughout the flow
3 # Tools can query this instead of having to pass around variabls
4 # basically just a wrapper around a dict, but will be used to
5 # store and retrieve values in a more structured way.
6 #
7 # if we wanted to play nice with non tcl tools, maybe we implement faux "typing"
8 # by requiring types when adding elements to the context
9 # for example:
10 # Context::Set String LaunchSettings project_name "MyProject"
11 # Context::Set List LaunchSettings sources [list "file1.v" "file2.v"]
12 #
13 # {
14 # Launch Settings {
15 # type { dict }
16 # value {
17 # project_name {type {String} value {"MyProject"}}
18 # sources {type {list} value {{file1.v} {file2.v}}}
19 # }
20 # }
21 # }
22 #
23 # this would let us 'easily' export/import json for communication with other tools
24 
25 
26 namespace eval Context {
27  variable _ctx
28  if {![info exists _ctx]} { set _ctx [dict create] }
29 
30  proc Clear {} {
31  variable _ctx
32  set _ctx [dict create]
33  return $_ctx
34  }
35 
36  proc Load {inputDict} {
37  if {![_isDict $inputDict]} { error "ERR_INVALID_DICT" "" ERR_INVALID_DICT }
38  variable _ctx
39  set _ctx $inputDict
40  return $_ctx
41  }
42 
43  proc GetFullContext {} {
44  _validate {} 0
45  variable _ctx
46  return $_ctx
47  }
48 
49  proc Set {args} {
50  _validate $args 2
51 
52  variable _ctx
53  set value [lindex $args end]
54  set keyPath [lrange $args 0 end-1]
55 
56  if {[catch {dict set _ctx {*}$keyPath $value}]} { error "ERR_TYPE_CONFLICT" "" ERR_TYPE_CONFLICT }
57  return $value
58  }
59 
60  proc Get {args} {
61  _validate $args 1
62 
63  variable _ctx
64  if {![dict exists $_ctx {*}$args]} { error "ERR_NOT_FOUND" "" ERR_NOT_FOUND }
65  return [dict get $_ctx {*}$args]
66  }
67 
68  proc GetOr {defaultValue args} {
69  _validate $args 1
70 
71  variable _ctx
72  if {[dict exists $_ctx {*}$args]} { return [dict get $_ctx {*}$args] }
73  return $defaultValue
74  }
75 
76  proc Has {args} {
77  _validate $args 1
78 
79  variable _ctx
80  return [dict exists $_ctx {*}$args]
81  }
82 
83  proc Remove {args} {
84  _validate $args 1
85 
86  variable _ctx
87  if {[dict exists $_ctx {*}$args]} {
88  dict unset _ctx {*}$args
89  return 1
90  }
91 
92  return 0
93  }
94 
95  proc Append {args} {
96  _validate $args 2
97 
98  variable _ctx
99  set suffix [lindex $args end]
100  set keyPath [lrange $args 0 end-1]
101 
102  if {[dict exists $_ctx {*}$keyPath]} {
103  set current [dict get $_ctx {*}$keyPath]
104  } else {
105  set current ""
106  }
107 
108  set updated "$current$suffix"
109  if {[catch {dict set _ctx {*}$keyPath $updated}]} {
110  error "ERR_TYPE_CONFLICT" "" ERR_TYPE_CONFLICT
111  }
112  return $updated
113  }
114 
115  proc Lappend {args} {
116  _validate $args 2
117 
118  variable _ctx
119  set item [lindex $args end]
120  set keyPath [lrange $args 0 end-1]
121 
122  if {[dict exists $_ctx {*}$keyPath]} {
123  set current [dict get $_ctx {*}$keyPath]
124  } else {
125  set current [list]
126  }
127 
128  set updated [concat $current [list $item]]
129  if {[catch {dict set _ctx {*}$keyPath $updated}]} {
130  error "ERR_TYPE_CONFLICT" "" ERR_TYPE_CONFLICT
131  }
132  return $updated
133  }
134 
135  proc Merge {inputDict} {
136  if {![_isDict $inputDict]} { error "ERR_INVALID_DICT" "" ERR_INVALID_DICT }
137  _validate {} 0
138 
139  variable _ctx
140  set _ctx [dict merge $_ctx $inputDict]
141  return $_ctx
142  }
143 
144  proc Size {} {
145  _validate {} 0
146 
147  variable _ctx
148  return [dict size $_ctx]
149  }
150 
151  proc Keys {args} {
152  _validate {} 0
153 
154  variable _ctx
155 
156  if {[llength $args] == 0} { return [dict keys $_ctx] }
157 
158  if {![dict exists $_ctx {*}$args]} { return [list] }
159 
160  set value [dict get $_ctx {*}$args]
161  if {[catch {dict keys $value} keys]} { error "ERR_NOT_A_DICT" "" ERR_NOT_A_DICT }
162 
163  return $keys
164  }
165 
166  proc SaveToFile {filename {overwrite false}} {
167  variable _ctx
168 
169  # check if file exists
170 
171  if {![file exists $filename]} {
172  set fileId [open $filename "w"]
173  } elseif {$overwrite} {
174  set fileId [open $filename "w"]
175  } else {
176  error "ERR_FILE_EXISTS" "" ERR_FILE_EXISTS
177  }
178 
179  puts $fileId "$_ctx"
180  close $fileId
181  }
182 
183  proc LoadFromFile {filename} {
184  if {![file exists $filename]} { error "ERR_FILE_NOT_FOUND" "" ERR_FILE_NOT_FOUND }
185 
186  set fileId [open $filename "r"]
187  set fileContent [read $fileId]
188  close $fileId
189 
190  if {![_isDict $fileContent]} { error "ERR_INVALID_DICT" "" ERR_INVALID_DICT }
191 
192  variable _ctx
193  set _ctx $fileContent
194  return $_ctx
195  }
196 
197 
198  proc _isDict {value} {
199  return [expr {![catch {dict size $value}]}]
200  }
201 
202  proc _ctxValid {} {
203  variable _ctx
204  return [_isDict $_ctx]
205  }
206 
207  proc _validate {args {minArgs 0} {checkCtx 1}} {
208  if {[llength $args] < $minArgs} { error "not enough arguments" "" ERR_INVALID_ARGS }
209  if {$checkCtx && ![_ctxValid]} { error "invalid context" "" ERR_INVALID_CONTEXT }
210  }
211 }