diff --git a/README.md b/README.md index 14f3bf2..7aa4049 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ pip install pydoodle ``` - [example.py](https://github.com/Prince2347X/pydoodle/blob/master/examples/example.py) -> Basic example on how to use! - [example_stdIn.py](https://github.com/Prince2347X/pydoodle/blob/master/examples/example_stdIn.py) -> Example on how to use stdIn (inputs). + - [example_links.py](https://github.com/Prince2347X/pydoodle/blob/master/examples/example_links.py) -> Example on how to use links as script. ### Misc diff --git a/docs/source/api.rst b/docs/source/api.rst index b1b825b..500c799 100644 --- a/docs/source/api.rst +++ b/docs/source/api.rst @@ -6,6 +6,19 @@ Compiler .. autoclass:: pydoodle.Compiler :members: +.. note:: + All types of links aren't yet supported and not to be provided in the ``script`` parameter. + Currently, supported links are: `pastebin.com`_, `hatstebin.com`_, `textbin.net`_ and `pastie.org`_. + Working on to add more of them. Want to suggest? `Start a new discussion`_ on github repo. + +.. Attention:: Only provide links which can be visible by everyone without any kind of password. + + .. _pastebin.com: https://pastebin.com + .. _hatstebin.com: https://hastebin.com + .. _textbin.net: https://textbin.net + .. _pastie.org: https://pastie.org + .. _Start a new discussion: https://github.com/Prince2347X/pydoodle/discussions/new + Output -------- diff --git a/docs/source/conf.py b/docs/source/conf.py index b122de7..ebcb81d 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -17,6 +17,9 @@ sys.path.insert(0, os.path.abspath('..')) sys.path.insert(0,os.path.abspath('.')) +version = "v1.1.0" + + # -- Project information ----------------------------------------------------- @@ -25,7 +28,7 @@ author = 'Prince2347X' # The full version, including alpha/beta/rc tags -release = "1.0.2" +release = version # -- General configuration --------------------------------------------------- diff --git a/docs/source/errors.rst b/docs/source/errors.rst index 94c8ce4..1c5d42f 100644 --- a/docs/source/errors.rst +++ b/docs/source/errors.rst @@ -8,4 +8,7 @@ Errors :members: .. autoclass:: pydoodle.errors.BadRequest - :members: \ No newline at end of file + :members: + +.. autoclass:: pydoodle.errors.LinkNotSupported + :members: diff --git a/examples/example_links.py b/examples/example_links.py new file mode 100644 index 0000000..764e50a --- /dev/null +++ b/examples/example_links.py @@ -0,0 +1,7 @@ +import pydoodle + +c = pydoodle.Compiler(clientId="client-id", + clientSecret="client-secret") # which you'll get from https://jdoodle.com/compiler-api +result = c.execute(script="https://pastebin.com/vJFXrKX3", language="python3", link=True) #NOTE: I've set the `link` parameter to True because I'm providing link in script parameter. +print(result.output) + diff --git a/pydoodle/__init__.py b/pydoodle/__init__.py index 66d1c82..6622e40 100644 --- a/pydoodle/__init__.py +++ b/pydoodle/__init__.py @@ -4,7 +4,7 @@ # METADATA # ############ -__version__ = "v1.0.2" +__version__ = "v1.1.0" __title__ = "pydoodle" __license__ = "MIT" __author__ = "Prince2347X" diff --git a/pydoodle/errors.py b/pydoodle/errors.py index 0620a99..c88c3fe 100644 --- a/pydoodle/errors.py +++ b/pydoodle/errors.py @@ -11,3 +11,7 @@ class UnauthorizedRequest(Exception): class BadRequest(Exception): """Raised when invalid language or versionIndex is provided. """ pass + +class LinkNotSupported(Exception): + """Raised if the provided link isn't supported yet by pydoodle.""" + pass diff --git a/pydoodle/jdoodle.py b/pydoodle/jdoodle.py index c9f0c38..063f946 100644 --- a/pydoodle/jdoodle.py +++ b/pydoodle/jdoodle.py @@ -1,5 +1,5 @@ import requests -from .errors import UnauthorizedRequest, BadRequest, LanguageNotSupported +from .errors import UnauthorizedRequest, BadRequest, LanguageNotSupported, LinkNotSupported class Output: @@ -25,7 +25,6 @@ class Compiler: """ def __init__(self, clientId: str, clientSecret: str): - """""" if not isinstance(clientId, str): raise TypeError elif not isinstance(clientSecret, str): @@ -45,15 +44,32 @@ def __init__(self, clientId: str, clientSecret: str): 'whitespace', 'yabasic'] self.json = {} - def execute(self, script: str, language: str, stdIn: str = None, versionIndex: int = None) -> Output: + def _get_raw_link(self, link: str) -> str: + if "pastebin" in link or "hastebin" in link: + return f"{link[:link.find('.com')]}.com/raw{link[link.rfind('/'):]}" + elif "textbin" in link: + return f"{link[:link.find('.net')]}.net/raw{link[link.rfind('/'):]}" + elif "pastie" in link: + return f"{link}/raw" + else: + raise LinkNotSupported("Not able to fetch script.") + + def _read_link(self, link: str) -> str: + raw_link = self._get_raw_link(link) + r = requests.get(raw_link) + return r.text + + def execute(self, script: str, language: str, link: bool = False, stdIn: str = None, versionIndex: int = None) -> Output: """ Executes the provided script. - :parameter script: The script to be executed. + :parameter script: The script to be executed. You can provide link of any code hosting site such as pastebin, hastebin, etc. (You've to set `link` parameter to `True`) :type script: str :parameter language: Language of the script. :type language: str + :parameter link: Tell if a link of any code hosting site(like pastebin, hastebin, etc..) is provided in script parameter. Defaults to `False`. If `True` it takes the script as link and fetch the script from the link. + :type link: bool :parameter stdIn: StdIn of script (If Any), defaults to `None`. In case of multiple inputs, they should be separated by `||` (double pipe). :type stdIn: str, optional :parameter versionIndex: Version Index of language, defaults to `0`. @@ -65,6 +81,7 @@ def execute(self, script: str, language: str, stdIn: str = None, versionIndex: i :raises: :class:`UnauthorizedRequest`: Raised if either your clientID or clientSecret is invalid. :raises: :class:`LanguageNotSupported`: Raised if wrong language code is provided. :raises: :class:`BadRequest`: Raised when invalid language or versionIndex is provided. + :raises: :class:`LinkNotSupported`: Raised if the provided link isn't supported yet by pydoodle. """ if not isinstance(script, str): @@ -78,6 +95,9 @@ def execute(self, script: str, language: str, stdIn: str = None, versionIndex: i raise TypeError else: versionIndex = 0 + link = True if script.startswith("https://") else link + if link is not False: + script = self._read_link(script) self.json = {"clientId": self.clientID, "clientSecret": self.clientSecret,