PageSourceSearch

https://docs.dask.org/en/stable/_static/config_converter.js

js dask.org collected 2026-09-24 08:37:58 UTC 10,187 bytes, 320 lines download raw bytes

1/**
2 * On window load check for the "configConvertUtilYAML" and "configConvertUtilEnv" text areas.
3 * If they exist add event handlers to convert YAML to environment variables and vice-versa
4 * when the user types in either field.
5 *
6 * This script includes unit tests which can be run manually in the developer console with `runTests()`.
7 *
8 */
9window.addEventListener('load', (event) => {
10    var configConvertUtilYAML = document.getElementById("configConvertUtilYAML")
11    var configConvertUtilEnv = document.getElementById("configConvertUtilEnv")
12    var configConvertUtilCode = document.getElementById("configConvertUtilCode")
13
14    if (
15        typeof (configConvertUtilYAML) != 'undefined' &&
16        configConvertUtilYAML != null &&
17        typeof (configConvertUtilEnv) != 'undefined' &&
18        configConvertUtilEnv != null &&
19        typeof (configConvertUtilCode) != 'undefined' &&
20        configConvertUtilCode != null) {
21
22        configConvertUtilYAML.addEventListener("keyup", (event) => {
23            try {
24                config = parseYAML(configConvertUtilYAML.value)
25                configConvertUtilEnv.value = dumpEnv(config)
26                configConvertUtilCode.value = dumpCode(config)
27            }
28            catch (err) {
29                console.log(err)
30                configConvertUtilEnv.value = err.message
31                configConvertUtilCode.value = err.message
32            }
33
34        })
35        configConvertUtilEnv.addEventListener("keyup", (event) => {
36            config = parseEnv(configConvertUtilEnv.value)
37            configConvertUtilYAML.value = dumpYAML(config)
38            configConvertUtilCode.value = dumpCode(config)
39        })
40        configConvertUtilCode.addEventListener("keyup", (event) => {
41            config = parseCode(configConvertUtilCode.value)
42            configConvertUtilYAML.value = dumpYAML(config)
43            configConvertUtilEnv.value = dumpEnv(config)
44        })
45
46    }
47});
48
49/**
50 * Parse a YAML string into a JS object.
51 *
52 * @param {string} raw - Raw YAML input
53 * @return {object} The parsed data
54 */
55function parseYAML(raw) {
56    return YAML.parse(raw)
57}
58
59/**
60 * Parse an environment variable declaration into a JS object.
61 *
62 * @param {string} raw - Raw environment variable declaration
63 * @return {object} The parsed data
64 */
65function parseEnv(raw) {
66    objects = []
67    raw = raw.trim()
68    raw.split("\n").forEach(envVar => {
69        let x
70        x = envVar.split("DASK_")
71        envVar = x[1]
72        x = envVar.split("=")
73        envVar = x[0]
74        let namespace = envVar.split("__").map(toKey)
75        let value = x[1]
76        while (true) {
77            if (value === true || value === false) {
78                break
79            }
80            try {
81                value = JSON.parse(value)
82                break
83            } catch (err) {
84                break
85            }
86        }
87        let object = value
88        for (var i = namespace.length - 1; i >= 0; i--) {
89            new_object = {}
90            new_object[namespace[i]] = object
91            object = new_object
92        }
93        objects.push(object)
94    })
95    return mergeDeep(...objects)
96}
97
98/**
99 * Parse Python dot notation code examples into a JS object.
100 *
101 * @param {string} raw - Raw Python dot notation code examples
102 * @return {object} The parsed data
103 */
104function parseCode(raw) {
105    objects = []
106    raw = raw.trim()
107    raw.split("\n").forEach(envVar => {
108        let x
109        x = envVar.split("set({\"")
110        envVar = x[1]
111        x = envVar.split("})")
112        envVar = x[0]
113        x = envVar.split("\": ")
114        envVar = x[0]
115        let namespace = envVar.split(".").map(toKey)
116        let value = x[1]
117        while (true) {
118            if (value === "True") {
119                value = true
120                break
121            }
122            if (value === "False") {
123                value = false
124                break
125            }
126            if (value === true || value === false) {
127                break
128            }
129            try {
130                value = JSON.parse(value)
131                break
132            } catch (err) {
133                break
134            }
135        }
136        let object = value
137        for (var i = namespace.length - 1; i >= 0; i--) {
138            new_object = {}
139            new_object[namespace[i]] = object
140            object = new_object
141        }
142        objects.push(object)
143    })
144    return mergeDeep(...objects)
145}
146
147/**
148 * Dump a JS config object to a YAML string.
149 *
150 * @param {string} config - Config object
151 * @return {str} YAML configuration
152 */
153function dumpYAML(config) {
154    return YAML.stringify(config, 10, 2)
155}
156
157/**
158 * Dump a JS config object to a set of environment variable declarations.
159 *
160 * @param {string} config - Config object
161 * @return {string} environment variable declarations
162 *
163 * @example
164 * dumpEnv({"array": {"chunk-size": "128 MB"}})
165 * // returns 'export DASK_ARRAY__CHUNK_SIZE="128 MB"'
166 */
167function dumpEnv(config) {
168    return walkEnv(config).join("\n")
169}
170
171/**
172 * Walk through config object and construct env var namespace. Recursive method.
173 *
174 * @param {string} config - Config object
175 * @param {string} prefix - The prefix for the current level of nesting. Always starts with "DASK_"
176 * @return {list} list of environment variable declarations
177 *
178 * @example
179 * walkEnv({"array": {"chunk-size": "128 MB"}})
180 * // returns ['export DASK_ARRAY__CHUNK_SIZE="128 MB"']
181 */
182function walkEnv(config, prefix = "DASK_") {
183    let vars = []
184    if (config === null) {
185        return config
186    }
187    Object.keys(config).forEach(key => {
188        if (typeof (config[key]) === 'object' && !Array.isArray(config[key])) {
189            vars = vars.concat(walkEnv(config[key], `${prefix + fromKey(key)}__`))
190        } else {
191            vars.push(`export ${prefix}${fromKey(key)}=${JSON.stringify(config[key])}`)
192        }
193    });
194    return vars
195}
196
197/**
198 * Dump a JS config object to a set of dot notation Python examples.
199 *
200 * @param {string} config - Config object
201 * @return {string} Python dot notation examples
202 *
203 * @example
204 * dumpEnv({"array": {"chunk-size": "128 MB"}})
205 * // returns '>>> dask.config.set({"array.chunk-size": "128 MB"})'
206 */
207function dumpCode(config) {
208    return walkCode(config).join("\n")
209}
210
211/**
212 * Walk through config object and construct dot notation namespace. Recursive method.
213 *
214 * @param {string} config - Config object
215 * @param {string} prefix - The prefix for the current level of nesting.
216 * @return {list} list of environment variable declarations
217 *
218 * @example
219 * walkCode({"array": {"chunk-size": "128 MB"}})
220 * // returns ['>>> dask.config.set({"array.chunk-size": "128 MB"})']
221 */
222function walkCode(config, prefix = "") {
223    let vars = []
224    if (config === null) {
225        return config
226    }
227    Object.keys(config).forEach(key => {
228        if (typeof (config[key]) === 'object' && !Array.isArray(config[key])) {
229            vars = vars.concat(walkCode(config[key], `${prefix + key}.`))
230        } else {
231            value = JSON.stringify(config[key])
232            if (value === true || value === "true") {
233                value = "True"
234            }
235            if (value === false || value === "false") {
236                value = "False"
237            }
238            vars.push(`>>> dask.config.set({"${prefix}${key}": ${value}})`)
239        }
240    });
241    return vars
242}
243
244/**
245 * Convert environment variable style key to YAML style key
246 *
247 * @param {string} str - Environment variable style key
248 * @return {string} YAML style key
249 *
250 * @example
251 * toKey("HELLO_WORLD")
252 * // returns "hello-world"
253 */
254function toKey(str) {
255    return str.toLowerCase().replaceAll("_", "-")
256}
257
258/**
259 * Convert YAML style key to environment variable style key
260 *
261 * @param {string} str - YAML style key
262 * @return {string} Environment variable style key
263 *
264 * @example
265 * toKey("hello-world")
266 * // returns "HELLO_WORLD"
267 */
268function fromKey(key) {
269    return key.toUpperCase().replaceAll("-", "_")
270}
271
272/**
273 * Deep merge a set of objects
274 *
275 * @param {list} objects - Objects to be merged (wrapped arguments)
276 * @return {object} Merged object
277 *
278 * @example
279 * mergeDeep({"hello": "world"}, {"foo": "bar"})
280 * // returns {"hello": "world", "foo": "bar"}
281 */
282function mergeDeep(...objects) {
283    const isObject = obj => obj && typeof obj === 'object';
284
285    return objects.reduce((prev, obj) => {
286        Object.keys(obj).forEach(key => {
287            const pVal = prev[key];
288            const oVal = obj[key];
289
290            if (Array.isArray(pVal) && Array.isArray(oVal)) {
291                prev[key] = pVal.concat(...oVal);
292            }
293            else if (isObject(pVal) && isObject(oVal)) {
294                prev[key] = mergeDeep(pVal, oVal);
295            }
296            else {
297                prev[key] = oVal;
298            }
299        });
300
301        return prev;
302    }, {});
303}
304
305/** Tests
306 *
307 * To run these tests build the documentation and open the configuration.html page in your browser.
308 * Then open developer tools and run `runTests()` in the console.
309 *
310 */
311function runTests() {
312    console.assert(dumpEnv(parseYAML("distributed:\n  logging:\n    distributed: info\n")) == 'export DASK_DISTRIBUTED__LOGGING__DISTRIBUTED="info"')
313    console.assert(dumpEnv(parseYAML("distributed:\n    deploy:\n      lost-worker-timeout: 15s")) == 'export DASK_DISTRIBUTED__DEPLOY__LOST_WORKER_TIMEOUT="15s"')
314    console.assert(dumpCode(parseYAML("distributed:\n  logging:\n    distributed: info\n")) == '>>> dask.config.set({"distributed.logging.distributed": "info"})')
315    console.assert(dumpYAML(parseEnv('export DASK_DISTRIBUTED__LOGGING__DISTRIBUTED="info"')) == "distributed:\n  logging:\n    distributed: info\n")
316    console.assert(dumpCode(parseEnv('export DASK_DISTRIBUTED__LOGGING__DISTRIBUTED="info"')) == '>>> dask.config.set({"distributed.logging.distributed": "info"})')
317    console.assert(dumpEnv(parseCode('>>> dask.config.set({"distributed.logging.distributed": "info"})')) == 'export DASK_DISTRIBUTED__LOGGING__DISTRIBUTED="info"')
318    console.assert(dumpYAML(parseCode('>>> dask.config.set({"distributed.logging.distributed": "info"})')) == "distributed:\n  logging:\n    distributed: info\n")
319    console.log("Tests complete")
320}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.