MobileBlur

Diff
Login

Differences From Artifact [e6d16bd699]:

To Artifact [20bba43f9c]:


13
14
15
16
17
18
19
20

21
22
23
24
25
26
27
28

29
30
31
32
33
34
35
13
14
15
16
17
18
19

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36







-
+








+







import sys
import code
import logging
import types
import re
import optparse
import glob

import traceback
import fileutils
import settings
from utils import web2py_uuid
from compileapp import build_environment, read_pyc, run_models_in
from restricted import RestrictedError
from globals import Request, Response, Session
from storage import Storage
from admin import w2p_unpack
from dal import BaseAdapter


logger = logging.getLogger("web2py")

def exec_environment(
    pyfile='',
    request=None,
46
47
48
49
50
51
52
53
54
55



56
57
58
59
60
61
62
47
48
49
50
51
52
53



54
55
56
57
58
59
60
61
62
63







-
-
-
+
+
+







        Builds a web2py environment and optionally executes a Python
        file into the environment.
        A Storage dictionary containing the resulting environment is returned.
        The working directory must be web2py root -- this is the web2py default.

    """

    if request==None: request = Request()
    if response==None: response = Response()
    if session==None: session = Session()
    if request is None: request = Request()
    if response is None: response = Response()
    if session is None: session = Session()

    if request.folder is None:
        mo = re.match(r'(|.*/)applications/(?P<appname>[^/]+)', pyfile)
        if mo:
            appname = mo.group('appname')
            request.folder = os.path.join('applications', appname)
        else:
146
147
148
149
150
151
152
153


154
155
156
157
158
159
160
147
148
149
150
151
152
153

154
155
156
157
158
159
160
161
162







-
+
+









def run(
    appname,
    plain=False,
    import_models=False,
    startfile=None,
    bpython=False
    bpython=False,
    python_code=False
    ):
    """
    Start interactive shell or run Python script (startfile) in web2py
    controller environment. appname is formatted like:

    a      web2py application name
    a/c    exec the controller c into the application environment
198
199
200
201
202
203
204

205
206











207
208
209
210
211
212
213
214
215
216
217
218
219






220
221
222
223
224
225







226
227
228
229
230
231
232
200
201
202
203
204
205
206
207


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237






238
239
240
241
242
243
244
245
246
247
248
249
250
251







+
-
-
+
+
+
+
+
+
+
+
+
+
+













+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+








    if f:
        exec ('print %s()' % f, _env)
    elif startfile:
        exec_pythonrc()
        try:
            execfile(startfile, _env)
            if import_models: BaseAdapter.close_all_instances('commit')
        except RestrictedError, e:
            print e.traceback
        except Exception, e:
            print traceback.format_exc()
            if import_models: BaseAdapter.close_all_instances('rollback')
    elif python_code:
        exec_pythonrc()
        try:
            exec(python_code, _env)
            if import_models: BaseAdapter.close_all_instances('commit')
        except Exception, e:
            print traceback.format_exc()
            if import_models: BaseAdapter.close_all_instances('rollback')
    else:
        if not plain:
            if bpython:
                try:
                    import bpython
                    bpython.embed(locals_=_env)
                    return
                except:
                    logger.warning(
                        'import bpython error; trying ipython...')
            else:
                try:
                    import IPython
                    if IPython.__version__ >= '0.11':
                        from IPython.frontend.terminal.embed import InteractiveShellEmbed
                        shell = InteractiveShellEmbed(user_ns=_env)
                        shell()
                        return
                    else:
                    # following 2 lines fix a problem with IPython; thanks Michael Toomim
                    if '__builtins__' in _env:
                        del _env['__builtins__']
                    shell = IPython.Shell.IPShell(argv=[], user_ns=_env)
                    shell.mainloop()
                    return
                        # following 2 lines fix a problem with
                        # IPython; thanks Michael Toomim
                        if '__builtins__' in _env:
                            del _env['__builtins__']
                        shell = IPython.Shell.IPShell(argv=[],user_ns=_env)
                        shell.mainloop()
                        return
                except:
                    logger.warning(
                        'import IPython error; use default python shell')
        try:
            import readline
            import rlcompleter
        except ImportError:
393
394
395
396
397
398
399
400


412
413
414
415
416
417
418
419
420
421








+
+
    else:
        startfile = ''
    run(options.shell, options.plain, startfile=startfile, bpython=options.bpython)


if __name__ == '__main__':
    execute_from_command_line()