You should place this file in web2py folder. And run command:
python shell.py appname
Here appname is very import, and it'll set request.folder to that app.
If you want to switch other apps, you shuld quit from the shell first.
Here is my simple test output:
D:\project\svn\web2py>shell.py address
warning: importing mysqlite3.dbapi2 as sqlite3
warning: no MySQLdb driver
warning: no psycopg2 driver
warning: no cx_Oracle driver
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> db=SQLDB("sqlite://d:/project/svn/web2py/db.db")
>>> db.define_table('address',
... SQLField('name', required=True,
requires=[IS_NOT_IN_DB(db, 'address.name')]),
... SQLField('telphone'),
... SQLField('mobile'),
... SQLField('homephone'),
... SQLField('email'),
... SQLField('work_address'),
... SQLField('home_address'),
... SQLField('birthday', 'date',
requires=IS_NULL_OR(IS_DATETIME())),
... SQLField('room'),
... SQLField('department'),
... )
<SQLStorage {'ALL': <gluon.sql.SQLALL instance at 0x00F410A8>,
'home_address': <gluon.sql.SQLField instance at 0x00F3FF08>, 'name':
<gluon.sql.SQLField instance
at 0x00F3F8F0>, 'room': <gluon.sql.SQLField instance at 0x00F41058>,
'_referenced_by': [], 'fields': ['id', 'name', 'telphone', 'mobile',
'homephone', 'email',
'work_address', 'home_address', 'birthday', 'room', 'department'],
'_db': <SQLStorage {'_connection': <pysqlite2.dbapi2.Connection object
at 0x00F0B320>, '_las
tsql': '', '_dbname': 'sqlite', '_execute': <function <lambda> at
0x00F3BE70>, '_folder': 'applications\\address\\databases', '_uri':
'sqlite://d:/project/svn/w
eb2py/db.db', 'tables': ['address'], '_translator': {'is null': 'IS
NULL', 'upper': 'UPPER(%(field)s)', 'lower': 'LOWER(%(field)s)',
'string': 'CHAR(%(length)s)
', 'reference': 'REFERENCES %(foreign_key)s ON DELETE
%(on_delete_action)s', 'text': 'TEXT', 'time': 'TIME', 'upload':
'CHAR(64)', 'datetime': 'TIMESTAMP', 'is
not null': 'IS NOT NULL', 'boolean': 'CHAR(1)', 'blob': 'BLOB',
'double': 'DOUBLE', 'date': 'DATE', 'integer': 'INTEGER', 'password':
'CHAR(%(length)s)', 'extra
ct': "web2py_extract('%(name)s',%(field)s)", 'id': 'INTEGER PRIMARY
KEY AUTOINCREMENT'}, 'address': <SQLStorage {...}>, '_cursor':
<pysqlite2.dbapi2.Cursor obje
ct at 0x00F3C290>}>, '_dbt':
'applications\\address\\databases\\0ce06eabddf1494b3165e3e1e9be64bf_address.table',
'telphone': <gluon.sql.SQLField instance at 0x0
0F3FD78>, 'email': <gluon.sql.SQLField instance at 0x00F3FE68>,
'mobile': <gluon.sql.SQLField instance at 0x00F3FDC8>, 'birthday':
<gluon.sql.SQLField instance
at 0x00F3FFA8>, '_tablename': 'address', 'department':
<gluon.sql.SQLField instance at 0x00F41080>, 'id': <gluon.sql.SQLField
instance at 0x00F41148>, 'homephon
e': <gluon.sql.SQLField instance at 0x00F3FE40>, 'work_address':
<gluon.sql.SQLField instance at 0x00F3FEB8>}>
>>> db().select(db.address.ALL)
<gluon.sql.SQLRows instance at 0x00F414B8>
>>> a = db().select(db.address.ALL)
>>> str(a)
'address.id,address.name,address.telphone,address.mobile,address.homephone,address.email,address.work_address,address.home_address,address.birthday,address.room
,address.department\r\n1,limodou,,,,,,,1973-12-26,,\r\n2,zoom,,,,,,,1973-12-26,,\r\n3,xlp223,,,,,,,1973-12-26,,\r\n5,ppppp,,,,,,,,,\r\n'
>>> a.response
[(1, u'limodou', u'', u'', u'', None, None, None, u'1973-12-26', u'',
u''), (2, u'zoom', u'', u'', u'', u'', None, None, u'1973-12-26', u'',
u''), (3, u'xlp223'
, u'', u'', u'', u'', None, None, u'1973-12-26', u'', u''), (5,
u'ppppp', u'', u'', u'', u'', None, None, None, u'', u'')]
>>> print FORM(TEXTAREA(_id='text'),INPUT(_type='button',_value='markdown',
... _onclick="ajax('ajaxwiki_onclick',['text'],'html')"))
<form enctype="multipart/form-data" method="post"><textarea id="text"
rows="10" cols="40"></textarea><input
onclick="ajax('ajaxwiki_onclick',['text'],'html')" v
alue="markdown" type="button"/></form>
--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
meide <<wxPython UI module>>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou
Massimo
> <shell.py>
Massimo
On Mar 12, 2008, at 12:23 AM, limodou wrote:
> 'address.id,address.name,address.telphone,address.mobile,address.homep
> hone,address.email,address.work_address,address.home_address,address.b
> irthday,address.room
> ,address.department\r\n1,limodou,,,,,,,1973-12-26,,\r\n2,zoom,,,,,,,
> 1973-12-26,,\r\n3,xlp223,,,,,,,1973-12-26,,\r\n5,ppppp,,,,,,,,,\r\n'
>>>> a.response
> [(1, u'limodou', u'', u'', u'', None, None, None, u'1973-12-26', u'',
> u''), (2, u'zoom', u'', u'', u'', u'', None, None, u'1973-12-26', u'',
> u''), (3, u'xlp223'
> , u'', u'', u'', u'', None, None, u'1973-12-26', u'', u''), (5,
> u'ppppp', u'', u'', u'', u'', None, None, None, u'', u'')]
>>>> print FORM(TEXTAREA(_id='text'),INPUT
>>>> (_type='button',_value='markdown',
> ... _onclick="ajax('ajaxwiki_onclick',
> ['text'],'html')"))
> <form enctype="multipart/form-data" method="post"><textarea id="text"
> rows="10" cols="40"></textarea><input
> onclick="ajax('ajaxwiki_onclick',['text'],'html')" v
> alue="markdown" type="button"/></form>
>
> --
> I like python!
> UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
> meide <<wxPython UI module>>: http://code.google.com/p/meide/
> My Blog: http://www.donews.net/limodou
>
> >
> <shell.py>
Massimo
> <shell.py>
>>> execfile('applications/Address/models/db.py')
python shell.py someapp
and someapp does not exist it will ask if you want to create it.
Massimo
> In the shell I can:
> execfile('applications/cookbook/modules/module1.py')
> and the classes and functions inside it will be available.
And for modules, I think you should import them but not execfile them.
>
> Not so in the real web2py. Trying to use any function or class defined
> in module1 after being execfile-d in a controller function only
> produces a:
> NameError: global name 'xxx' is not defined
> even though 'xxx' is in dir() (???).
>
--
python shell.py -p appname
or it'll auto find ipython and run.
And I also add automatically load model files process,
model_path = os.path.join(request.folder,'models', '*.py')
from glob import glob
for f in glob(model_path):
fname, ext = os.path.splitext(f)
execfile(f, environment)
print 'Imported "%s" model file' % fname
so, when you start shell.py, it'll report all imported model files,
and you can directly use:
db().select(db.address.ALL)
to test something, don't need to load model file manually any more.
Hope Massimo can merge it to svn.
> <shell.py>
Massimo
And I found you add this line:
if options.plain: raise Exception
But I don't know why your code will fail? What's the error? Because
optparse is an official module. I also test shell.py in python 2.5.
I just gave a web2py hat to Guido Van Rossum.
Massimo
> <shell.py>