Skip to content

Commit

Permalink
Merge pull request #1102 from BlackOrder/cli_interceptors_accepts_url
Browse files Browse the repository at this point in the history
Cli interceptors accepts url
  • Loading branch information
farirat authored Nov 7, 2023
2 parents 7028913 + 8a41ef3 commit f74cc31
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
18 changes: 15 additions & 3 deletions jasmin/protocols/cli/mointerceptorm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import inspect
import pickle
import urllib.parse, urllib.request, urllib.error
from jasmin.protocols.cli.managers import PersistableManager, Session
from jasmin.protocols.cli.filtersm import MOFILTERS
from jasmin.routing.jasminApi import MOInterceptorScript
Expand Down Expand Up @@ -133,9 +134,20 @@ def parse_args_and_call_with_instance(self, *args, **kwargs):
stype, script_path = validate_typed_script(arg)

if stype == 'python3':
# Open file and get its content
with open(script_path, 'r') as content_file:
pyCode = content_file.read()
pathscheme = urllib.parse.urlparse(script_path).scheme
if pathscheme == '':
# Open file and get its content
with open(script_path, 'r') as content_file:
pyCode = content_file.read()
elif pathscheme in ['https', 'http', 'ftp', 'file']:
try:
with urllib.request.urlopen(script_path) as content_file:
pyCode = content_file.read().decode('utf-8')
except urllib.error.URLError as e:
# Handle errors that may occur while reading the file from a URL
return self.protocol.sendData('[URL]: %s' % str(e))
else:
raise NotImplementedError("Not implemented yet !")

# Test compilation of the script
compile(pyCode, '', 'exec')
Expand Down
18 changes: 15 additions & 3 deletions jasmin/protocols/cli/mtinterceptorm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import inspect
import pickle
import urllib.parse, urllib.request, urllib.error
from jasmin.protocols.cli.managers import PersistableManager, Session
from jasmin.protocols.cli.filtersm import MTFILTERS
from jasmin.routing.jasminApi import MTInterceptorScript
Expand Down Expand Up @@ -133,9 +134,20 @@ def parse_args_and_call_with_instance(self, *args, **kwargs):
stype, script_path = validate_typed_script(arg)

if stype == 'python3':
# Open file and get its content
with open(script_path, 'r') as content_file:
pyCode = content_file.read()
pathscheme = urllib.parse.urlparse(script_path).scheme
if pathscheme == '':
# Open file and get its content
with open(script_path, 'r') as content_file:
pyCode = content_file.read()
elif pathscheme in ['https', 'http', 'ftp', 'file']:
try:
with urllib.request.urlopen(script_path) as content_file:
pyCode = content_file.read().decode('utf-8')
except urllib.error.URLError as e:
# Handle errors that may occur while reading the file from a URL
return self.protocol.sendData('[URL]: %s' % str(e))
else:
raise NotImplementedError("Not implemented yet !")

# Test compilation of the script
compile(pyCode, '', 'exec')
Expand Down
6 changes: 6 additions & 0 deletions misc/doc/sources/management/jcli/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,12 @@ Here's an example of adding a **DefaultInterceptor** to a python script::

.. note:: As of now, only **python3** script is permitted.

.. note:: The path to the script can be any of the fallowing:

* **python3(/path/to/script.py)** or **python3(file://path/to/script.py)**: The path must be absolute, relative path is not supported
* **python3(https://example.com/path/to/script.py)**: The script is a remote python3 script. The script will be
downloaded and copied to Jasmin core. Accepts http, https, and ftp protocols.

.. note:: Pay attention that the given script is copied to Jasmin core, do not expect Jasmin to refresh the script
code when you update it, you'll need to redefine the *mointerceptor* rule again so Jasmin will refresh the script.

Expand Down

0 comments on commit f74cc31

Please sign in to comment.