Skip to content
This repository

Enable webpy to be used within another package #51

Merged
merged 3 commits into from over 3 years ago

3 participants

Aaron Swartz Anand Chitipothu

Removing the import test from web/__init__.py allows webpy to be placed inside another package.

It's nice to be able to "package" webpy apps for environments where webpy isn't installed in the system and I don't have shell access. This way I can still deploy the applications in a self contained manner. git submodules FTW!

All relevant tests seem OK. (3 failing but those are because I don't have psycopg2 in this environment)

python test/alltests.py
Unable to import psycopg2 (ignoring PostgresTest)
Unable to import pysqlite2.dbapi2 (ignoring SqliteTest_pysqlite2)
No module named MySQLdb (ignoring MySQLTest)
Unable to import psycopg (ignoring PostgresTest_psycopg)
Unable to import pgdb (ignoring PostgresTest_pgdb)
No module named DBUtils(ignoring testPooling)
......................................................................................................................EEE...

Example Before:

mkdir myapp
cd myapp/
mkdir myapp
touch myapp/__init__.py
git clone git://github.com/webpy/webpy.git
Cloning into webpy...
remote: Counting objects: 2620, done.
remote: Compressing objects: 100% (911/911), done.
remote: Total 2620 (delta 1818), reused 2411 (delta 1640)
Receiving objects: 100% (2620/2620), 436.76 KiB | 355 KiB/s, done.
Resolving deltas: 100% (1818/1818), done.
$(cd myapp && ln -s ../webpy/web)
python -c"import myapp; import myapp.web"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "myapp/web/__init__.py", line 29, in <module>
    import test
  File "myapp/web/test.py", line 6, in <module>
    import web
ImportError: No module named web
echo $?
1

Example After:

mkdir myapp
cd myapp/
mkdir myapp
touch myapp/__init__.py
git clone git://github.com/serverhorror/webpy.git
Cloning into webpy...
remote: Counting objects: 2632, done.
remote: Compressing objects: 100% (923/923), done.
remote: Total 2632 (delta 1827), reused 2411 (delta 1640)
Receiving objects: 100% (2632/2632), 437.93 KiB | 328 KiB/s, done.
Resolving deltas: 100% (1827/1827), done.
$(cd webpy/ && git checkout tp-embeddable)
Branch tp-embeddable set up to track remote branch tp-embeddable from origin.
Switched to a new branch 'tp-embeddable'
$(cd myapp && ln -s ../webpy/web)
python -c"import myapp; import myapp.web"
echo $? # see: no error :)
0
serverhorror added some commits
serverhorror create all dirs if root doesn't exist
* Use os.makedirs instead of os.mkdir
* Use os.path.abspath to get rid of eventual '..' elements
87a142d
serverhorror listen on 127.0.0.1 by default instead of 0.0.0.0
There shouldn't be any worries about opening up an
unsecured application just because it is run with
the defaults.
f89041c
serverhorror Make webpy embeddable in another package
Don't `import test` at the package level of `web`.
`test` imports `web` and that isn't available if
webpy is placed within another package
10e01ff
Aaron Swartz
Owner

This makes sense to me.

Aaron Swartz aaronsw merged commit 10e01ff into from
Aaron Swartz aaronsw closed this
Aaron Swartz
Owner

Seeing no objection, I've merged these.

Anand Chitipothu
Collaborator

This won't change the default bind address. The app.request method is used for testing stuff, not for starting the dev server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 3 unique commits by 1 author.

Mar 08, 2011
serverhorror create all dirs if root doesn't exist
* Use os.makedirs instead of os.mkdir
* Use os.path.abspath to get rid of eventual '..' elements
87a142d
serverhorror listen on 127.0.0.1 by default instead of 0.0.0.0
There shouldn't be any worries about opening up an
unsecured application just because it is run with
the defaults.
f89041c
Mar 17, 2011
serverhorror Make webpy embeddable in another package
Don't `import test` at the package level of `web`.
`test` imports `web` and that isn't available if
webpy is placed within another package
10e01ff
This page is out of date. Refresh to see the latest.
1  web/__init__.py
@@ -26,7 +26,6 @@
26 26 from debugerror import *
27 27 from application import *
28 28 from browser import *
29   -import test
30 29 try:
31 30 import webopenid as openid
32 31 except ImportError:
6 web/application.py
@@ -133,7 +133,7 @@ def add_processor(self, processor):
133 133 self.processors.append(processor)
134 134
135 135 def request(self, localpart='/', method='GET', data=None,
136   - host="0.0.0.0:8080", headers=None, https=False, **kw):
  136 + host="127.0.0.1:8080", headers=None, https=False, **kw):
137 137 """Makes request to this application for the specified path and method.
138 138 Response will be a storage object with data, status and headers.
139 139
@@ -161,10 +161,10 @@ def request(self, localpart='/', method='GET', data=None,
161 161 ...
162 162 >>> response = app.request("/redirect")
163 163 >>> response.headers['Location']
164   - 'http://0.0.0.0:8080/foo'
  164 + 'http://127.0.0.1:8080/foo'
165 165 >>> response = app.request("/redirect", https=True)
166 166 >>> response.headers['Location']
167   - 'https://0.0.0.0:8080/foo'
  167 + 'https://127.0.0.1:8080/foo'
168 168
169 169 The headers argument specifies HTTP headers as a mapping object
170 170 such as a dict.
5 web/session.py
@@ -4,6 +4,7 @@
4 4 """
5 5
6 6 import os, time, datetime, random, base64
  7 +import os.path
7 8 try:
8 9 import cPickle as pickle
9 10 except ImportError:
@@ -217,7 +218,9 @@ class DiskStore(Store):
217 218 def __init__(self, root):
218 219 # if the storage root doesn't exists, create it.
219 220 if not os.path.exists(root):
220   - os.mkdir(root)
  221 + os.makedirs(
  222 + os.path.abspath(root)
  223 + )
221 224 self.root = root
222 225
223 226 def _get_path(self, key):

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.