annotate semiconginev2/old/settings.nim @ 1221:000befd9479f compiletime-tests

did: remove support for git, for now
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 21:05:47 +0700
parents 56781cc0fc7c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
1 import std/logging
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/streams
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/parsecfg
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/strutils
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
5 import std/parseutils
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
6 import std/strformat
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
7 import std/tables
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
8 import std/os
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
9
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
10 import ./core
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
11
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
12 when CONFIGHOTRELOAD:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
13 var
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
14 configUpdates: Channel[(string, string)]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
15 configUpdates.open()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
16
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
17 # runtime configuration
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
18 # =====================
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
19 # namespace is the path from the CONFIGROOT to the according settings file without the file extension
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
20 # a settings file must always have the extension CONFIGEXTENSION
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
21 # a fully qualified settings identifier can be in the form {namespace}.{section}.{key}
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
22 # {key} and {section} may not contain dots
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
23
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
24 # a "namespace" is the path from the settings root to an *.CONFIGEXTENSION file, without the file extension
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
25 # settings is a namespace <-> settings mapping
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
26 var allsettings: Table[string, Config]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
27
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
28 proc configRoot(): string =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
29 joinPath(absolutePath(getAppDir()), CONFIGROOT)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
30
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
31 proc getFile(namespace: string): string =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
32 joinPath(configRoot(), namespace & "." & CONFIGEXTENSION)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
33
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
34 iterator walkConfigNamespaces(): string =
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 840
diff changeset
35 for file in walkDirRec(dir = configRoot(), relative = true, checkDir = true):
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
36 if file.endsWith("." & CONFIGEXTENSION):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
37 yield file[0 ..< ^(CONFIGEXTENSION.len + 1)]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
38
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
39 proc loadAllConfig(): Table[string, Config] =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
40 for ns in walkConfigNamespaces():
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
41 result[ns] = ns.getFile().loadConfig()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
42
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 920
diff changeset
43 proc ReloadSettings*() =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
44 allsettings = loadAllConfig()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
45
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
46 proc configStr(key, section, namespace: string): string =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
47 when CONFIGHOTRELOAD:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
48 while configUpdates.peek() > 0:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
49 let (updatedNamespace, updatedConfig) = configUpdates.recv()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
50 allsettings[updatedNamespace] = loadConfig(newStringStream(updatedConfig))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
51 if not allsettings.hasKey(namespace):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
52 raise newException(Exception, &"Settings {namespace}.{section}.{key} was not found")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
53 allsettings[namespace].getSectionValue(section, key)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
54
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 920
diff changeset
55 proc Setting*[T: int|float|string](key, section, namespace: string): T =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
56 when T is int:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
57 let value = configStr(key, section, namespace)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
58 if parseInt(value, result) == 0:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
59 raise newException(Exception, &"Unable to parse int from settings {namespace}.{section}.{key}: {value}")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
60 elif T is float:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
61 let value = configStr(key, section, namespace)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
62 if parseFloat(value, result) == 0:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
63 raise newException(Exception, &"Unable to parse float from settings {namespace}.{section}.{key}: {value}")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
64 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
65 result = configStr(key, section, namespace)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
66
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 920
diff changeset
67 proc Setting*[T: int|float|string](identifier: string): T =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
68 # identifier can be in the form:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
69 # {namespace}.{key}
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
70 # {namespace}.{section}.{key}
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
71 let parts = identifier.rsplit(".")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
72 if parts.len == 1:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
73 raise newException(Exception, &"Setting with name {identifier} has no namespace")
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
74 if parts.len == 2: result = Setting[T](parts[1], "", parts[0])
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
75 else: result = Setting[T](parts[^1], parts[^2], joinPath(parts[0 .. ^3]))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
76
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 920
diff changeset
77 proc HadConfigUpdate*(): bool =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
78 when CONFIGHOTRELOAD == true:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
79 result = configUpdates.peek() > 0
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
80
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
81 allsettings = loadAllConfig()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
82
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
83 when CONFIGHOTRELOAD == true:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
84 import std/times
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
85
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
86 proc configFileWatchdog() {.thread.} =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
87 var configModTimes: Table[string, Time]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
88 while true:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
89 for namespace in walkConfigNamespaces():
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
90 if not (namespace in configModTimes):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
91 configModTimes[namespace] = Time()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
92 let lastMod = namespace.getFile().getLastModificationTime()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
93 if lastMod > configModTimes[namespace]:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
94 configModTimes[namespace] = lastMod
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
95 let configStr = newFileStream(namespace.getFile()).readAll()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
96 configUpdates.send((namespace, configStr))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
97 sleep CONFIGHOTRELOADINTERVAL
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
98 var thethread: Thread[void]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
99 createThread(thethread, configFileWatchdog)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
100
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
101 if DEBUG:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
102 setLogFilter(lvlAll)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
103 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
104 setLogFilter(lvlWarn)