Skip to content
This repository has been archived by the owner on Jan 3, 2018. It is now read-only.

Commit

Permalink
huTools.http API and function-based pyJasper Interface. Dependencie R…
Browse files Browse the repository at this point in the history
…eduction

This patch introduces a HTTP-API menat to allow easy use on pure
Python and AppEngine plattforms. All other huTools modules should
reley on them instead of httplib2 and others.

pyjasper now comes with an non Object Oriented interface and also strips
timestamps from generated PDFs to ensure the same input generates EXACTLY
the same output.

use hujson instead of simplejson.
  • Loading branch information
Maximillian Dornseif committed Oct 24, 2010
1 parent 5b82cfd commit 44aa713
Show file tree
Hide file tree
Showing 15 changed files with 1,754 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
dist/
docs/_build/doctrees/*
huTools.egg-info/
testenv/
pythonenv/
build/
30 changes: 16 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# setting the PATH seems only to work in GNUmake not in BSDmake
PATH := ./testenv/bin:$(PATH)
PATH := ./pythonenv/bin:$(PATH)

default: dependencies check test examples

Expand All @@ -16,18 +16,20 @@ check:
-/usr/local/hudorakit/bin/hd_pylint huTools

test:
PYTHONPATH=. python huTools/NetStringIO.py
PYTHONPATH=. python huTools/calendar/formats.py
PYTHONPATH=. python huTools/calendar/workdays.py
PYTHONPATH=. python huTools/checksumming.py
PYTHONPATH=. python huTools/humessaging.py
PYTHONPATH=. python huTools/luids.py
PYTHONPATH=. python huTools/obfuscation.py
PYTHONPATH=. python huTools/unicode.py
PYTHONPATH=. ./pythonenv/bin/python huTools/http/test.py
PYTHONPATH=. ./pythonenv/bin/python huTools/NetStringIO.py
PYTHONPATH=. ./pythonenv/bin/python huTools/calendar/formats.py
PYTHONPATH=. ./pythonenv/bin/python huTools/calendar/workdays.py
PYTHONPATH=. ./pythonenv/bin/python huTools/checksumming.py
PYTHONPATH=. ./pythonenv/bin/python huTools/humessaging.py
PYTHONPATH=. ./pythonenv/bin/python huTools/luids.py
PYTHONPATH=. ./pythonenv/bin/python huTools/obfuscation.py
PYTHONPATH=. ./pythonenv/bin/python huTools/unicode.py
PYJASPER_SERVLET_URL=http://127.0.0.1:8000/pyJasper/jasper.py PYTHONPATH=. ./pythonenv/bin/python huTools/pyjasper.py

coverage: dependencies
printf '.*/tests/.*\n.*test.py\n' > .figleaf-exclude.txt
printf '/usr/local/lib/.*\n/opt/.*\ntestenv/.*\n' >> .figleaf-exclude.txt
printf '/usr/local/lib/.*\n/opt/.*\npythonenv/.*\n' >> .figleaf-exclude.txt
printf '.*manage.py\n.*settings.py\n.*setup.py\n.*urls.py\n' >> .figleaf-exclude.txt
PYTHONPATH=. /usr/local/hudorakit/bin/hd_figleaf --ignore-pylibs huTools/NetStringIO.py
PYTHONPATH=. /usr/local/hudorakit/bin/hd_figleaf --ignore-pylibs huTools/ReReadingConfigParser.py
Expand All @@ -53,8 +55,8 @@ build: examples
python setup.py build

dependencies:
virtualenv testenv
pip -q install -E testenv -r requirements.txt
virtualenv pythonenv
pip -q install -E pythonenv -r requirements.txt

statistics:
sloccount --wide --details huTools | tee sloccount.sc
Expand All @@ -77,7 +79,7 @@ examples: doc/standards/examples/warenzugang.xml doc/standards/examples/kommiauf


clean:
rm -Rf testenv build dist html test.db pylint.out sloccount.sc pip-log.txt
rm -Rf testenv pythonenv build dist html test.db pylint.out sloccount.sc pip-log.txt
find . -name '*.pyc' -delete

.PHONY: build clean install upload check doc
.PHONY: build clean install upload check doc docs test
2 changes: 1 addition & 1 deletion doc/development.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Use global unique identifiers where ever possible. `huTools.luids.guid128()` cre
* Always test Iñtërnâtiônàlizætiøn by putting strange strings into input fields
* Always test `<script>alert("XSS");</script> & <bold>Co</bold>` by putting strange strings into input fields
* use [huTools](http://hudora.github.com/huTools/) where appropriate

* `Iñtërnâtiônàlizætiøn <script>alert("XSS");</script> %+'"<!--` might be a goot test string.

## Django Specifica

Expand Down
2 changes: 1 addition & 1 deletion doc/standards/examples/wms2logos_kommiauftrag.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Liefertermin>20091125</Liefertermin>
<Terminart>BIS</Terminart>
<Textcode1>8</Textcode1>
<Auftragstext>abholer: Abholung durch Spedition Dornseif$hebebuehne: True$</Auftragstext>
<Auftragstext>abholer: Abholung durch Spedition Dornseif$avisieren_unter: +49123456$hebebuehne: True$</Auftragstext>
<Textcode2>2</Textcode2>
<Kommissioniertext>etiketten_speicherort: c:\desktop\Programm Data\schrecklicher windows pfad$sortenrein: True$packhoehe: 1100</Kommissioniertext>
<EmpfaengerILN/>
Expand Down
85 changes: 85 additions & 0 deletions huTools/http/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env python
# encoding: utf-8
"""
huTools/http/__init__.py
Provides a simple interface for doing HTTP requests on stock Python and on Google Appengine. Provides
unicode aware application/x-www-form-urlencoded encoding and an do multipart/form-data (file upload)
without aditional code. On Google Appengine it incerass the timeout from 5 seconds to 10.
Usage is without suprises::
>>> status, header, body = fetch('http://www.postbin.org/o0ds54',
{'küh': 'Iñtërnâtiônàlizætiøn', 'just a test': 212},
headers={'X-Foo': 'Bar'})
File Upload just works::
>>> status, header, body = fetch('http://www.postbin.org/o0ds54',
{'hosts': open('/etc/hosts', 'r')}, 'POST')
"""
# Created by Maximillian Dornseif on 2010-10-24.
# Copyright (c) 2010 HUDORA. All rights reserved.

from huTools.http import tools
import logging
import poster_encode
import urllib
import urlparse

try:
from engine_appengine import request
except ImportError:
from engine_httplib2 import request


def fetch(url, content='', method='GET', credentials=None, headers=None, multipart=False):
"""Does a HTTP request with method `method` to `url`.
Returns (status, headers, content) whereas `status` is an integer status code, `headers` is a dict
containing the headers sent by the server and `content` is the body of the http response.
Parameters to fetch are::
* `url` is the fully qualified request URL. It may contain query prameters.
* `content` is the request body to be sent. It may be a dict which for all requests expect POST
is converted to query parameters. If there are query parameters already in the `url` they are merged
with `content`. For POST requests the data is encoded as application/x-www-form-urlencoded
or multipart/form-data and encoded. If the parameter `multipart` is `True` or if one of the values
in content has a `name` attribute (which is the case for file objects) multipart encoding is choosen.
* `headers` is a dict of header values
* `credentials` is reserved for future use
"""

if not headers:
headers = {}
if method == 'POST':
if hasattr(content, 'items'):
# we assume content is a dict which needs to be encoded
# decide to use multipart/form-data encoding or application/x-www-form-urlencoded
for v in content.values():
if hasattr(v, 'name'):
multipart = True
if multipart:
datagen, mp_headers = poster_encode.multipart_encode(content)
headers.update(mp_headers)
content = "".join(datagen)
else:
headers.update({'content-type': 'application/x-www-form-urlencoded'})
content = tools.urlencode(content)
else:
# url parmater encoding
if hasattr(content, 'items'):
scheme, netloc, path, params, query, fragment = urlparse.urlparse(url)
qdict = urlparse.parse_qs(query)
# ugly Unicode issues, see http://bugs.python.org/issue1712522
qdict.update(content)
query = tools.urlencode(qdict)
url = urlparse.urlunparse((scheme, netloc, path, params, query, fragment))
content = ''
# convert all header values to strings (what about unicode?)
for k, v in headers.items():
headers[k] = str(v)

return request(url, method, content, headers)
Loading

0 comments on commit 44aa713

Please sign in to comment.