Skip to content

Commit 7198c0a

Browse files
Merge pull request #42 from ibuildthecloud/main
chore: add get_env helper
2 parents 577586d + 3ca5a5b commit 7198c0a

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

gptscript/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from gptscript.gptscript import GPTScript
2+
from gptscript.confirm import AuthResponse
3+
from gptscript.frame import RunFrame, CallFrame, PromptFrame
4+
from gptscript.opts import GlobalOptions
5+
from gptscript.prompt import PromptResponse
6+
from gptscript.run import Run, RunBasicCommand, Options
7+
from gptscript.text import Text
8+
from gptscript.tool import ToolDef, Tool
9+
from gptscript.exec_utils import get_env

gptscript/exec_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import json
12
import os
23
import subprocess
34
import sys
5+
import base64
6+
import gzip
47

58
if sys.platform == "win32":
69
import msvcrt
@@ -84,3 +87,15 @@ def _stream_cmd(cmd, args=[], input=None, fds=tuple(), close_fds=True):
8487
return process
8588
except Exception as e:
8689
raise e
90+
91+
92+
def get_env(key, default=''):
93+
v = os.environ.get(key, '')
94+
if v == '':
95+
return default
96+
if v.startswith('{"_gz":"') and v.endswith('"}'):
97+
try:
98+
return gzip.decompress(base64.b64decode(v[8:-2])).decode('utf-8')
99+
except Exception as e:
100+
pass
101+
return v

tests/test_gptscript.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import base64
2+
import gzip
3+
import json
14
import os
25
import platform
36
import subprocess
@@ -13,6 +16,7 @@
1316
from gptscript.run import Run
1417
from gptscript.text import Text
1518
from gptscript.tool import ToolDef, ArgumentSchema, Property, Tool
19+
from gptscript.exec_utils import get_env
1620

1721

1822
# Ensure the OPENAI_API_KEY is set for testing
@@ -512,3 +516,10 @@ async def process_event(r: Run, frame: CallFrame | RunFrame | PromptFrame):
512516

513517
assert not prompt_event_found, "Prompt event occurred"
514518
assert "prompt event occurred" in out, "Unexpected output: " + out
519+
520+
521+
def test_get_env():
522+
os.environ['TEST_ENV'] = json.dumps({
523+
'_gz': base64.b64encode(gzip.compress(b'test value')).decode('utf-8'),
524+
}).replace(' ', '')
525+
assert 'test value' == get_env('TEST_ENV')

0 commit comments

Comments
 (0)