Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Park committed Jul 22, 2016
2 parents 344c843 + b97745f commit 2795ca0
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 61 deletions.
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ COPY tox.ini /python-dev/tox.ini
WORKDIR /python-dev

# allow interactive bash inside docker container
CMD ./run_python.sh $API_KEY $FILENAME $ALT_URL $GIT_USERNAME $VERSION
CMD ./run_python.sh

VOLUME ["/source"]
2 changes: 1 addition & 1 deletion docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ Build the docker image, e.g. `docker build -t basistech/python:1.1 .`

Run an example as `docker run -e API_KEY=api-key -v "path-to-local-python-dir:/source" basistech/python:1.1`

To test against a specific source file, add `-e FILENAME=filename` before the `-v`, to test against an alternate url, add `-e ALT_URL=alternate_url`, and optionally if you would like to regenerate gh-pages from the changes made to the development source you can add `-e GIT_USERNAME=git-username -e VERSION=version` before the `-v`. In order to push the gh-pages to git remember to mount .ssh and .gitconfig to the root dir `-v path-to-.ssh-dir:/root/.ssh -v path-to-.gitconfig:/root/.gitconfig`.
To test against a specific source file, add `-e FILENAME=filename` before the `-v`, to test against an alternate url, add `-e ALT_URL=alternate_url`.
54 changes: 13 additions & 41 deletions docker/run_python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

retcode=0
ping_url="https://api.rosette.com/rest/v1"
errors=( "Exception" "processingFailure" "badRequest" "ParseError" "ValueError" "SyntaxError" "AttributeError" "ImportError" )

#------------------ Functions ----------------------------------------------------
#Gets called when the user doesn't provide any args
Expand All @@ -10,12 +11,14 @@ function HELP {
echo " API_KEY - Rosette API key (required)"
echo " FILENAME - Python source file (optional)"
echo " ALT_URL - Alternate service URL (optional)"
echo " GIT_USERNAME - Git username where you would like to push regenerated gh-pages (optional)"
echo " VERSION - Build version (optional)"
echo "Compiles and runs the source file(s) using the local development source."
exit 1
}

if [ ! -z ${ALT_URL} ]; then
ping_url=${ALT_URL}
fi

#Checks if Rosette API key is valid
function checkAPI {
match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "forbidden")
Expand Down Expand Up @@ -55,41 +58,25 @@ function runExample() {
fi
echo "${result}"
echo -e "\n---------- ${1} end -------------"
if [[ "${result}" == *"Exception"* ]]; then
echo "Exception found"
retcode=1
elif [[ "$result" == *"processingFailure"* ]]; then
retcode=1
elif [[ "$result" == *"AttributeError"* ]]; then
retcode=1
elif [[ "$result" == *"ImportError"* ]]; then
retcode=1
fi
for err in "${errors[@]}"; do
if [[ ${result} == *"${err}"* ]]; then
retcode=1
fi
done
}
#------------------ Functions End ------------------------------------------------

#Gets API_KEY, FILENAME and ALT_URL if present
while getopts ":API_KEY:FILENAME:ALT_URL:GIT_USERNAME:VERSION" arg; do
while getopts ":API_KEY:FILENAME:ALT_URL" arg; do
case "${arg}" in
API_KEY)
API_KEY=${OPTARG}
usage
;;
ALT_URL)
ALT_URL=${OPTARG}
usage
;;
FILENAME)
FILENAME=${OPTARG}
usage
;;
GIT_USERNAME)
GIT_USERNAME=${OPTARG}
usage
;;
VERSION)
VERSION={OPTARG}
usage
;;
esac
done
Expand All @@ -109,8 +96,10 @@ if [ ! -z ${API_KEY} ]; then
python /python-dev/setup.py install
cd /python-dev/examples
if [ ! -z ${FILENAME} ]; then
echo -e "\nRunning example against: ${ping_url}\n"
runExample ${FILENAME}
else
echo -e "\nRunning examples against: ${ping_url}\n"
for file in *.py; do
runExample ${file}
done
Expand All @@ -123,21 +112,4 @@ fi
cd /python-dev
tox

#Generate gh-pages and push them to git account (if git username is provided)
if [ ! -z ${GIT_USERNAME} ] && [ ! -z ${VERSION} ]; then
#clone python git repo
cd /
git clone [email protected]:${GIT_USERNAME}/python.git
cd python
git checkout origin/gh-pages -b gh-pages
git branch -d develop
#generate gh-pages and set ouput dir to git repo (gh-pages branch)
cd /python-dev
.tox/py27/bin/epydoc -v --no-private --no-frames --css epydoc.css -o /python rosette/*.py
cd /python
git add .
git commit -a -m "publish python apidocs ${VERSION}"
git push
fi

exit ${retcode}
2 changes: 1 addition & 1 deletion examples/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ RUN chmod 755 /python/examples/run_python.sh
WORKDIR /python/examples

# allow interactive bash inside docker container
CMD ./run_python.sh $API_KEY $FILENAME $ALT_URL
CMD ./run_python.sh

VOLUME ["/source"]
25 changes: 12 additions & 13 deletions examples/docker/run_python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

retcode=0
ping_url="https://api.rosette.com/rest/v1"
errors=( "Exception" "processingFailure" "badRequest" "ParseError" "ValueError" "SyntaxError" "AttributeError" "ImportError" )

#------------------ Functions ----------------------------------------------------

Expand All @@ -15,6 +16,10 @@ function HELP {
exit 1
}

if [ ! -z ${ALT_URL} ]; then
ping_url=${ALT_URL}
fi

#Checks if Rosette API key is valid
function checkAPI() {
match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "forbidden")
Expand Down Expand Up @@ -54,16 +59,11 @@ function runExample() {
fi
echo "${result}"
echo -e "\n---------- ${1} end -------------"
if [[ "${result}" == *"Exception"* ]]; then
echo "Exception found"
retcode=1
elif [[ "$result" == *"processingFailure"* ]]; then
retcode=1
elif [[ "$result" == *"AttributeError"* ]]; then
retcode=1
elif [[ "$result" == *"ImportError"* ]]; then
retcode=1
fi
for err in "${errors[@]}"; do
if [[ ${result} == *"${err}"* ]]; then
retcode=1
fi
done
}

#------------------ Functions End ------------------------------------------------
Expand All @@ -73,15 +73,12 @@ while getopts ":API_KEY:FILENAME:ALT_URL" arg; do
case "${arg}" in
API_KEY)
API_KEY=${OPTARG}
usage
;;
FILENAME)
FILENAME=${OPTARG}
usage
;;
ALT_URL)
ALT_URL=${OPTARG}
usage
;;
esac
done
Expand All @@ -97,8 +94,10 @@ cp /source/examples/*.* .
if [ ! -z ${API_KEY} ]; then
checkAPI
if [ ! -z ${FILENAME} ]; then
echo -e "\nRunning example against: ${ping_url}\n"
runExample ${FILENAME}
else
echo -e "\nRunning examples against: ${ping_url}\n"
for file in *.py; do
runExample ${file}
done
Expand Down
3 changes: 2 additions & 1 deletion examples/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):
entities_text_data = "Bill Murray will appear in new Ghostbusters film: Dr. Peter Venkman was spotted filming a cameo in Boston this… http://dlvr.it/BnsFfS"
params = DocumentParameters()
params["content"] = entities_text_data
return api.entities(params) # entity linking is turned off
params["genre"] = "social-media"
return api.entities(params)

parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
parser.add_argument('-k', '--key', help='Rosette API Key', required=True)
Expand Down
3 changes: 2 additions & 1 deletion examples/entities_linked.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):
params = DocumentParameters()
params["content"] = entities_linked_text_data
params["genre"] = "social-media"
return api.entities(params, True) # entity linking is turned on
# This syntax is deprecated, call api.entities(params)
return api.entities(params, True)


parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
Expand Down
2 changes: 1 addition & 1 deletion examples/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):

language_data = "Por favor Señorita, says the man."
params = DocumentParameters()

params["content"] = language_data
api.setCustomHeaders("X-RosetteAPI-App", "python-app")
return api.language(params)


Expand Down
2 changes: 1 addition & 1 deletion rosette/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
limitations under the License.
"""

__version__ = '1.1.1'
__version__ = '1.2.0'
62 changes: 62 additions & 0 deletions rosette/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import os
from socket import gaierror
import requests
import re
import warnings

_BINDING_VERSION = '1.1.1'
_GZIP_BYTEARRAY = bytearray([0x1F, 0x8b, 0x08])
Expand Down Expand Up @@ -366,6 +368,17 @@ def info(self):
identifying data."""
url = self.service_url + "info"
headers = {'Accept': 'application/json', 'X-RosetteAPI-Binding': 'python', 'X-RosetteAPI-Binding-Version': _BINDING_VERSION}

customHeaders = self.api.getCustomHeaders()
pattern = re.compile('^X-RosetteAPI-')
if customHeaders is not None:
for key in customHeaders.keys():
if pattern.match(key) is not None:
headers[key] = customHeaders[key]
else:
raise RosetteException("badHeader", "Custom header name must begin with \"X-RosetteAPI-\"", key)
self.api.clearCustomHeaders()

if self.debug:
headers['X-RosetteAPI-Devel'] = 'true'
self.logger.info('info: ' + url)
Expand All @@ -382,6 +395,17 @@ def ping(self):

url = self.service_url + 'ping'
headers = {'Accept': 'application/json', 'X-RosetteAPI-Binding': 'python', 'X-RosetteAPI-Binding-Version': _BINDING_VERSION}

customHeaders = self.api.getCustomHeaders()
pattern = re.compile('^X-RosetteAPI-')
if customHeaders is not None:
for key in customHeaders.keys():
if pattern.match(key) is not None:
headers[key] = customHeaders[key]
else:
raise RosetteException("badHeader", "Custom header name must begin with \"X-RosetteAPI-\"", key)
self.api.clearCustomHeaders()

if self.debug:
headers['X-RosetteAPI-Devel'] = 'true'
self.logger.info('Ping: ' + url)
Expand Down Expand Up @@ -426,9 +450,21 @@ def call(self, parameters):
params_to_serialize = parameters.serialize(self.api.options)
headers = {}
if self.user_key is not None:

customHeaders = self.api.getCustomHeaders()
pattern = re.compile('^X-RosetteAPI-')
if customHeaders is not None:
for key in customHeaders.keys():
if pattern.match(key) is not None:
headers[key] = customHeaders[key]
else:
raise RosetteException("badHeader", "Custom header name must begin with \"X-RosetteAPI-\"", key)
self.api.clearCustomHeaders()

headers["X-RosetteAPI-Key"] = self.user_key
headers["X-RosetteAPI-Binding"] = "python"
headers["X-RosetteAPI-Binding-Version"] = _BINDING_VERSION

if self.useMultipart:
params = dict(
(key,
Expand Down Expand Up @@ -502,6 +538,7 @@ def __init__(
self.connection_refresh_duration = refresh_duration
self.http_connection = None
self.options = {}
self.customHeaders = {}

def _connect(self, parsedUrl):
""" Simple connection method
Expand Down Expand Up @@ -638,6 +675,30 @@ def clearOptions(self):
"""
self.options.clear()

def setCustomHeaders(self, name, value):
"""
Sets custom headers
@param headers: array of custom headers to be set
"""
if value is None:
self.customHeaders.pop(name, None)
else:
self.customHeaders[name] = value

def getCustomHeaders(self):
"""
Get custom headers
"""
return self.customHeaders

def clearCustomHeaders(self):
"""
Clears custom headers
"""

self.customHeaders.clear()

def ping(self):
"""
Create a ping L{EndpointCaller} for the server and ping it.
Expand Down Expand Up @@ -705,6 +766,7 @@ def entities(self, parameters, resolve_entities=False):
@type resolve_entities: Boolean
@return: A python dictionary containing the results of entity extraction."""
if resolve_entities:
warnings.warn("entities(params,resolve_entities) is deprecated and replaced by entities(params).", DeprecationWarning)
return EndpointCaller(self, "entities/linked").call(parameters)
else:
return EndpointCaller(self, "entities").call(parameters)
Expand Down
Loading

0 comments on commit 2795ca0

Please sign in to comment.