Skip to content

Commit

Permalink
Merge pull request #42 from ibuildthecloud/main
Browse files Browse the repository at this point in the history
chore: add get_env helper
  • Loading branch information
ibuildthecloud authored Aug 2, 2024
2 parents 577586d + 3ca5a5b commit 7198c0a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions gptscript/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from gptscript.gptscript import GPTScript
from gptscript.confirm import AuthResponse
from gptscript.frame import RunFrame, CallFrame, PromptFrame
from gptscript.opts import GlobalOptions
from gptscript.prompt import PromptResponse
from gptscript.run import Run, RunBasicCommand, Options
from gptscript.text import Text
from gptscript.tool import ToolDef, Tool
from gptscript.exec_utils import get_env
15 changes: 15 additions & 0 deletions gptscript/exec_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import json
import os
import subprocess
import sys
import base64
import gzip

if sys.platform == "win32":
import msvcrt
Expand Down Expand Up @@ -84,3 +87,15 @@ def _stream_cmd(cmd, args=[], input=None, fds=tuple(), close_fds=True):
return process
except Exception as e:
raise e


def get_env(key, default=''):
v = os.environ.get(key, '')
if v == '':
return default
if v.startswith('{"_gz":"') and v.endswith('"}'):
try:
return gzip.decompress(base64.b64decode(v[8:-2])).decode('utf-8')
except Exception as e:
pass
return v
11 changes: 11 additions & 0 deletions tests/test_gptscript.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import base64
import gzip
import json
import os
import platform
import subprocess
Expand All @@ -13,6 +16,7 @@
from gptscript.run import Run
from gptscript.text import Text
from gptscript.tool import ToolDef, ArgumentSchema, Property, Tool
from gptscript.exec_utils import get_env


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

assert not prompt_event_found, "Prompt event occurred"
assert "prompt event occurred" in out, "Unexpected output: " + out


def test_get_env():
os.environ['TEST_ENV'] = json.dumps({
'_gz': base64.b64encode(gzip.compress(b'test value')).decode('utf-8'),
}).replace(' ', '')
assert 'test value' == get_env('TEST_ENV')

0 comments on commit 7198c0a

Please sign in to comment.