Skip to content

chore: add get_env helper #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')