Skip to content

Commit

Permalink
Allow relative and absolute paths for scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
r00tdaemon committed May 6, 2020
1 parent ee8e5c3 commit d3c63a5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ To configure the tool copy `conf.json.example` to `conf.json`.
- `body` - The response body for given route. Used if `"response_type` is "static"
- `methods` - HTTP methods for which this response should be sent.
- `response_type` - How to generate the response. Can be "static" or "script"
- `script` - Name of python module from which to generate the response. Used if `"response_type` is "script"
- `script` - Name of python module from which to generate the response. Used if `"response_type` is "script". User can provide either a default plugin(`customresp`), a relative path to python script or an absolute path to the script.

To run - `python3 server.py`

### Plugins
Users can create their own plugins which will allow them to generate dynamic responses based on the requests received. All the plugins reside in the `plugins` directory.
Users can create their own plugins which will allow them to generate dynamic responses based on the requests received.

To create a plugin you need to import the base plugin class and override its abstract method.
```python
from plugins.base import Plugin
from simserve.plugins.base import Plugin

class MyResponse(Plugin):
def response(self, request):
Expand Down
38 changes: 36 additions & 2 deletions simserve/handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
import sys
import inspect
import importlib
import importlib.util
from typing import (
Dict,
Union,
Expand All @@ -22,9 +25,41 @@ def _get_resp(self, method: str) -> Union[Dict, None]:
return resp
return None

@staticmethod
def _load_script(path: str):
if importlib.util.find_spec(f"simserve.plugins.{os.path.splitext(os.path.basename(path))[0]}"):
return importlib.import_module(f"simserve.plugins.{path}")
elif os.path.isfile(f"{os.getcwd()}/{path}.py") or os.path.isfile(f"{os.getcwd()}/{path}"):
path = f"{os.getcwd()}/{path}"
path = f"{path}.py" if os.path.isfile(f"{path}.py") else f"{path}"
module_name = f"__simserve_plugin__.{os.path.splitext(os.path.basename(path))[0]}"
try:
spec = importlib.util.spec_from_file_location(module_name, path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
except Exception as e:
access_log.error(f"Could not load plugin. {e}")
raise tornado.web.HTTPError(500)
return module
elif os.path.isfile(path):
module_name = f"__simserve_plugin__.{os.path.splitext(os.path.basename(path))[0]}"
try:
spec = importlib.util.spec_from_file_location(module_name, path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
except Exception as e:
access_log.error(f"Could not load plugin. {e}")
raise tornado.web.HTTPError(500)
return module
else:
access_log.error("Couldn't find plugin to load.")
raise tornado.web.HTTPError(500)

def _get_resp_body(self, resp: Dict):
if resp.get("response_type") == "script":
plug_module = importlib.import_module(f"simserve.plugins.{resp.get('script')}")
plug_module = self._load_script(resp.get('script'))
_, plug = inspect.getmembers(
plug_module,
lambda x: inspect.isclass(x) and not inspect.isabstract(x) and issubclass(x, Plugin)
Expand All @@ -49,7 +84,6 @@ def _format_response(resp) -> str:
log = f"\n----- Response -----\n"
for k, v in sorted(resp.get("headers")):
log += f"{k}: {v}\n"
# TODO: Log for script responses.
log += f"\n{resp.get('body')}\n"
log += f"----- End -----\n"
return log
Expand Down

0 comments on commit d3c63a5

Please sign in to comment.