Skip to content

Commit

Permalink
Fix relative imports with exec
Browse files Browse the repository at this point in the history
stangirala committed Feb 25, 2022
1 parent 817644f commit c46fa4a
Showing 4 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions library/setup.cfg
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[metadata]
name = greppo
version = 0.0.22
version = 0.0.23
author = Adithya Krishnan and Sarma Tangirala
author_email = greppomail@gmail.com
description = Build responsive web-apps for geospatial applications.
long_description = file: description.md
description = Build responsive web-apps for geospatial applications.
long_description = file: Readme.md
long_description_content_type = text/markdown
url = https://greppo.io/
project_urls =
Source = https://github.com/greppo-io/greppo
Documentation = https://docs.greppo.io/
Documentation = https://docs.greppo.io/
classifiers =
Development Status :: 2 - Pre-Alpha
Programming Language :: Python :: 3.9
7 changes: 7 additions & 0 deletions library/src/greppo/user_script_utils.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,9 @@
import io
import json
import logging
import pathlib
import secrets
import sys
from _ast import Assign
from _ast import Attribute
from _ast import Call
@@ -182,6 +184,8 @@ def visit_Call(self, node):


def run_script(script_name, input_updates, hex_token_generator):
script_dir = str(pathlib.Path(script_name).parent)

with open(script_name) as f:
lines = f.read()
user_code = ast.parse(lines, script_name)
@@ -222,7 +226,10 @@ def run_script(script_name, input_updates, hex_token_generator):
logger.debug(ast.unparse(user_code))
logger.debug('\n----------------------------\n\n')

# Add path and then pop later to handle relative imports within the user script.
sys.path.append(script_dir)
exec(compile(user_code, script_name, "exec"), locals_copy, locals_copy)
sys.path.remove(script_dir)

raster_reference_payload = locals_copy.get("gpo_raster_reference_payload", None)

3 changes: 3 additions & 0 deletions library/tests/app.py
Original file line number Diff line number Diff line change
@@ -3,6 +3,9 @@
import geopandas as gpd
import numpy as np
from greppo import app
from relative.realtive2 import k

assert k == 10, "relative imports should work"

# TODO fix relative link for key file and data files

1 change: 1 addition & 0 deletions library/tests/relative/realtive2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
k = 10

0 comments on commit c46fa4a

Please sign in to comment.