Skip to content

Commit

Permalink
Merge pull request #2 from Prince2347X/1.1.0(beta)
Browse files Browse the repository at this point in the history
1.1.0
  • Loading branch information
Prince2347X authored May 2, 2021
2 parents c2f4049 + 96dd189 commit 8b35bcf
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------

Expand Down
5 changes: 4 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
sys.path.insert(0, os.path.abspath('..'))
sys.path.insert(0,os.path.abspath('.'))

version = "v1.1.0"



# -- Project information -----------------------------------------------------

Expand All @@ -25,7 +28,7 @@
author = 'Prince2347X'

# The full version, including alpha/beta/rc tags
release = "1.0.2"
release = version


# -- General configuration ---------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion docs/source/errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ Errors
:members:

.. autoclass:: pydoodle.errors.BadRequest
:members:
:members:

.. autoclass:: pydoodle.errors.LinkNotSupported
:members:
7 changes: 7 additions & 0 deletions examples/example_links.py
Original file line number Diff line number Diff line change
@@ -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)

2 changes: 1 addition & 1 deletion pydoodle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# METADATA #
############

__version__ = "v1.0.2"
__version__ = "v1.1.0"
__title__ = "pydoodle"
__license__ = "MIT"
__author__ = "Prince2347X"
Expand Down
4 changes: 4 additions & 0 deletions pydoodle/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 24 additions & 4 deletions pydoodle/jdoodle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import requests
from .errors import UnauthorizedRequest, BadRequest, LanguageNotSupported
from .errors import UnauthorizedRequest, BadRequest, LanguageNotSupported, LinkNotSupported


class Output:
Expand All @@ -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):
Expand All @@ -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`.
Expand All @@ -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):
Expand All @@ -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,
Expand Down

0 comments on commit 8b35bcf

Please sign in to comment.