-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement generic env vars for queries.
- Loading branch information
Showing
7 changed files
with
100 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Query: | ||
query = None | ||
env = None | ||
|
||
def __init__(self, query) -> None: | ||
if type(query) == dict: | ||
self.query = query.get("query", "") | ||
query.pop('query', None) | ||
self.env = query | ||
elif type(query) == str: | ||
self.query = query | ||
self.env = {} | ||
|
||
def get_query(self) -> str: | ||
return self.query | ||
|
||
def set_query(self, query: str) -> None: | ||
self.query = query | ||
|
||
def get_env(self) -> dict: | ||
return self.env | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from .query import Query | ||
import string | ||
|
||
def sandbox(code): | ||
__import__ = None | ||
__loader__ = None | ||
__build_class__ = None | ||
exec = None | ||
|
||
from datetime import datetime, timedelta | ||
|
||
def sf_time(t: datetime): | ||
return t.isoformat(timespec='milliseconds') + "Z" | ||
|
||
def now(delta: timedelta = None): | ||
if delta: | ||
return sf_time(datetime.utcnow() + delta) | ||
else: | ||
return sf_time(datetime.utcnow()) | ||
|
||
try: | ||
return eval(code) | ||
except Exception as e: | ||
return e | ||
|
||
def substitute(args: dict, query_template: str, env: dict) -> str: | ||
for key, command in env.items(): | ||
args[key] = sandbox(command) | ||
for key, val in args.items(): | ||
query_template = query_template.replace('{' + key + '}', val) | ||
return query_template |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters