Skip to content

Commit 5d0659d

Browse files
committed
feat(inner): Python/config_read_env
1 parent a0011fa commit 5d0659d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/pylib/Python/config_read_env.nim

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
3+
import std/os
4+
import std/strutils
5+
6+
using name: string
7+
func toAllUpper(name): string {.inline.} =
8+
for c in name:
9+
case c
10+
of 'a'..'z': result.add chr(ord(c) or 0b100000)
11+
else: discard # '_' is discarded, so for other chars
12+
func toPyEnv*(name): string {.inline.} = "PYTHON" & name.toAllUpper
13+
proc ib_i*[T](name; flagInit: T): T =
14+
let v = getEnv(name)
15+
if v.len == 0: return
16+
max(flagInit, T(
17+
try:
18+
let val = parseInt(v)
19+
if val < 0: 1 # PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1
20+
else: val
21+
except ValueError: 1
22+
))
23+
proc ib_e*[T](name; flagInit: T): T = flagInit or T existsEnv name
24+
proc ib_b*(name; flagInit: int): int = min 1, ib_i(name, flagInit)
25+
proc ib_b*(name; flagInit: bool): bool = ib_i(name, flagInit)

0 commit comments

Comments
 (0)