Dear web2py Forum,
I thought it would be a great idea if i wrote a utility script which would
take as input a web2py app name and
create - HTML Pages and
- Associated Default Controller Actions
for each: Menu Item of the Input Application Name
I am sure many of you are chuckling because of the difficulties i have been having accessing the web2py environment without running web2py. :)
My code so far is at the bottom.
It results in this error stack.
Traceback (most recent call last):
File "C:\web2py\pages4menu.py", line 127, in <module>
test() #test
File "C:\web2py\pages4menu.py", line 116, in test
pages4menu('menu2pagest')
File "C:\web2py\pages4menu.py", line 58, in pages4menu
from JoeTempPkg import menu as jmenu
File "C:\web2py\JoeTempPkg\menu.py", line 39, in <module>
(T('Home'), False, URL('default', 'index'), []),
File "C:\web2py\gluon\html.py", line 300, in URL
raise SyntaxError('not enough information to build the url (%s %s %s)' % (application, controller, function))
SyntaxError: not enough information to build the url (None default index)
If anyone has any suggestions, please let me know.
Thanks in advance.
Love and peace,
Joe
Here is the code. Thanks again.
'''
pages4menu.py creates
- HTML Pages and
- Associated Default Controller Actions
for each:
Menu Item of the Input Application Name
Usage: pages4menu.py inAppName
Example: pages4menu.py myapp
Created on Dec 28, 2013
@author: Joe Dorocak aka Joe Codeswell
License: Use this as you please. :)
Use with caution - I have done minimal testing.
RIGHT NOW IT DOESN'T WORK AT ALL! :)
No warranty is implied. :)
'''
'''RESEARCH
google: web2py use gluon in a python program
http://www.web2pyslices.com/slice/show/1478/using-modules-in-web2py
google: use gluon in python
Add gluon to global python path
https://www.mail-archive.com/web...@googlegroups.com/msg62545.html
web2py group: custom_import.
book 'Accessing the API from Python modules'
http://www.web2py.com/books/default/chapter/29/04/the-core#Accessing-the-API-from-Python-modules
find: Web2py defines some global objects (request, response, session, cache, T) that can only exist when an HTTP request is present (or is faked).
find also: The core API entities in the web2py execution environment are request, response, session, cache, URL, HTTP, redirect and T and are discussed below.
MAYBE i need to do this as an app?
First: post this stuff on the google group.
'''
import sys, os, tempfile, shutil, importlib
from gluon.html import *
def pages4menu(appName):
assert os.path.basename(os.path.abspath(os.curdir)) == 'web2py', 'This program must be run from the web2py directory.'
actionTemplate = '''def %s(): return dict()
'''
pageTemplate = '''{{response.title = "%s"}}
{{extend 'layout.html'}}
<h1>Welcome to Epiphany Listens</h1>
<p>This is the default/public/index.html template</p>
{{#=BEAUTIFY(response._vars)}}
'''
# create temporary dir/pkg for file manipulation/import
joeTmpPkgPath = createJoeTempPkg()
appPath = os.path.join('.','applications',appName)
appMenuPath = os.path.join(appPath,'models','menu.py')
prepTempMenu(appMenuPath, joeTmpPkgPath)
from JoeTempPkg import menu as jmenu
print [item for item in dir(jmenu) if not item.startswith("__")]
# destroy temporary dir/pkg RECURSIVELY
destroyTempPkg(joeTmpPkgPath)
def prepTempMenu(appMenuPath, joeTmpPkgPath):
# copy the appMenuFile into the tempPkg for import
shutil.copy(appMenuPath, joeTmpPkgPath)
insertStr = 'from gluon.html import *\n'
insertStr += 'from gluon.storage import *\n'
insertStr += 'response = Storage()\n'
insertStr += 'request = Storage()\n'
insertStr += 'def T(aString): return aString\n'
insertStr += 'request.application = "helloApp"\n'
insertStr += 'response.meta = Storage()\n'
insertStr += 'response.meta.author = "helloAuthor"\n'
insertStr += 'request.application = "app_name"\n'
insertStr += 'request.controller = "default"\n'
insertStr += 'request.function = "myfunc"\n'
f = open(os.path.join(joeTmpPkgPath,'menu.py'), 'r'); s1 = f.read(); f.close()
l = s1.splitlines(); l.insert(1, insertStr); s2 = '\n'.join(l)
f = open(os.path.join(joeTmpPkgPath,'menu.py'), 'w'); f.write(s2); f.close()
def destroyTempPkg(path):
shutil.rmtree(path)
def createJoeTempPkg():
#tempDirAbsPath = tempfile.mkdtemp()
joeTmpPkgPath = os.path.join('.','JoeTempPkg')
if not os.path.exists(joeTmpPkgPath):
os.mkdir(joeTmpPkgPath)
else:
# delete everything in the joeTmpPkgPath folder - http://stackoverflow.com/questions/185936/delete-folder-contents-in-python
folder = joeTmpPkgPath
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
os.unlink(file_path)
except Exception, e:
print e
# make the tempdir a package to expedite importing files from here
f = open(os.path.join(joeTmpPkgPath,'__init__.py'), 'w'); f.write('\n'); f.close()
return joeTmpPkgPath
def test():
#os.chdir(r"..") # test assertion program must be run from the web2py directory.
pages4menu('menu2pagest')
NUM_ARGS = 1
def main():
args = sys.argv[1:]
if len(args) != NUM_ARGS or "-h" in args or "--help" in args:
print __doc__
sys.exit(2)
pages4menu(args[0])
if __name__ == '__main__':
test() #test
#main() #production
"""possible change of approach
# new approach
## don't import instead
## get stuff and ddo your own menu
# import menu.py
# see variable-packages http://stackoverflow.com/questions/6677424 find Dan Copeland
# http://docs.python.org/2/library/functions.html#__import__
# http://docs.python.org/2/library/importlib.html
# http://effbot.org/zone/import-confusion.htm
# my_module = importlib.import_module("package.path.%s" % module_name)
# menuMod = importlib.import_module('menu.py', package=tempPkgAbsPath)
# from gluon import * # see http://stackoverflow.com/questions/13649411
# problem menu is not importing anything
# because of the web2py approach
# import response.menu
# to temp/menu.py prepend
# from gluon.html import *
"""
"""saved experiments
PythonWin 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for further copyright information.
>>> pwd
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
NameError: name 'pwd' is not defined
>>> os.path.abspath(os.curdir)
'C:\\web2py'
>>> os.path.abspath(os.curdir).dirname
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: 'str' object has no attribute 'dirname'
>>> os.path.abspath.dirname
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: 'function' object has no attribute 'dirname'
>>> os.path.abspath.dirname()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: 'function' object has no attribute 'dirname'
>>> os.path.dirname()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: dirname() takes exactly 1 argument (0 given)
>>> print os.path.basename(os.curdir)
>>> print os.path.abspath.basename(os.curdir)
.
>>> print os.path.abspath.basename(os.curdir)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: 'function' object has no attribute 'basename'
>>> print os.path.abspath(os.curdir).basename
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: 'function' object has no attribute 'basename'
>>> print os.path.abspath(os.curdir).basename
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: 'str' object has no attribute 'basename'
>>> print os.path.basename(os.path.abspath(os.curdir))
web2py
>>>
pages4menu.py creates
- HTML Pages and
- Associated Default Controller Actions
for each
Menu Item of the Input Application Name
Usage: pages4menu.py inAppName
Example: pages4menu.py myapp
Created on Dec 28, 2013
@author: Joe Dorocak aka Joe Codeswell
License: Use this as you please. :)
Use with caution - I have done minimal testing.
No warranty is implied. :)
Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 323, in RunScript
debugger.run(codeObject, __main__.__dict__, start_stepping=0)
File "C:\Python27\Lib\site-packages\Pythonwin\pywin\debugger\__init__.py", line 60, in run
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
File "C:\Python27\Lib\site-packages\Pythonwin\pywin\debugger\debugger.py", line 655, in run
exec cmd in globals, locals
File "C:\web2py\pages4menu.py", line 17, in <module>
'''
File "C:\web2py\pages4menu.py", line 35, in test
pages4menu('menu2pagest')
File "C:\web2py\pages4menu.py", line 22, in pages4menu
assert os.path.basename(os.path.abspath(os.curdir)) == 'web2py', 'This program must be run from the web2py directory.'
AssertionError: This program must be run from the web2py directory.
Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 323, in RunScript
debugger.run(codeObject, __main__.__dict__, start_stepping=0)
File "C:\Python27\Lib\site-packages\Pythonwin\pywin\debugger\__init__.py", line 60, in run
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
File "C:\Python27\Lib\site-packages\Pythonwin\pywin\debugger\debugger.py", line 655, in run
exec cmd in globals, locals
File "C:\web2py\pages4menu.py", line 17, in <module>
'''
File "C:\web2py\pages4menu.py", line 35, in test
pages4menu('menu2pagest')
File "C:\web2py\pages4menu.py", line 22, in pages4menu
assert os.path.basename(os.path.abspath(os.curdir)) == 'web2py', 'This program must be run from the web2py directory.'
AssertionError: This program must be run from the web2py directory.
>>> os.chdir(r"web2py")
>>> import shutil
>>> os.path.join('./applications/',appName,)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
NameError: name 'appName' is not defined
>>> appName = 'menu2pagest'
>>> os.path.join('./applications/',appName,)
'./applications/menu2pagest'
>>> os.path.join(list('./applications/',appName,'/models/menu.py'))
'/models/menu.py'
>>> os.path.join(['./applications/',appName,'/models/menu.py'])
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: list() takes at most 1 argument (3 given)
>>> os.path.join('./applications/',appName,'/models/menu.py')
['./applications/', 'menu2pagest', '/models/menu.py']
>>> os.path.abspath(os.path.join('./applications/',appName,'/models/menu.py'))
'C:\\models\\menu.py'
>>> os.path.abspath('.')
'C:\\web2py'
>>> appName
'menu2pagest'
>>> os.path.abspath(os.path.join('./applications/',appName))
'C:\\web2py\\applications\\menu2pagest'
>>> os.path.abspath(os.path.join('./applications/',appName,'/models/))
Traceback ( File "<interactive input>", line 1
os.path.abspath(os.path.join('./applications/',appName,'/models/))
^
SyntaxError: EOL while scanning string literal
>>> os.path.abspath(os.path.join('./applications/',appName,'/models/'))
'C:\\models'
>>> os.path.join('.',appName,'models','menu.py')
'.\\menu2pagest\\models\\menu.py'
>>> os.path.abspath(os.path.join('.',appName,'models','menu.py'))
'C:\\web2py\\menu2pagest\\models\\menu.py'
>>> os.path.isfile()
False
>>> p = os.path.abspath(os.path.join('.','applications',appName,'models','menu.py'))
>>> os.path.isfile(p)
False
>>> p
'C:\\web2py\\menu2pagest\\models\\menu.py'
>>> p = os.path.abspath(os.path.join('.','applications',appName,'models','menu.py'))
>>> os.path.isfile(p)
True
>>> appPath = os.path.join('.','applications',appName)
>>> appPath
'.\\applications\\menu2pagest'
>>> menuMod = importlib.import_module('menu.py', package=p)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
NameError: name 'importlib' is not defined
>>> import importlib
>>> menuMod = importlib.import_module('menu.py', package=p)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named menu.py
>>> menuMod = importlib.import_module('C:\\web2py\\menu2pagest\\models\\menu.py')
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
ImportError: Import by filename is not supported.
>>> os.chdir('C:\\web2py\\menu2pagest\\models\\menu.py')
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
WindowsError: [Error 3] The system cannot find the path specified: 'C:\\web2py\\menu2pagest\\models\\menu.py'
>>> os.chdir('C:\\web2py\\applications\\menu2pagest\\models\\menu.py')
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
WindowsError: [Error 267] The directory name is invalid: 'C:\\web2py\\applications\\menu2pagest\\models\\menu.py'
>>> os.chdir('C:\\web2py\\applications\\menu2pagest\\models')
>>> import menu.py
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ImportError: No module named menu.py
>>> os.chdir('C:\\web2py\\')
>>> os.mkdir(os.path.join('.','joeTempPkg')
... )
>>> f = open(os.path.join(tmpPkgPath,'__init__.py'), 'w'); f.write('\n'); f.close()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
NameError: name 'tmpPkgPath' is not defined
>>> tmpPkgPath = os.path.join('.','joeTempPkg')
>>> f = open(os.path.join(tmpPkgPath,'__init__.py'), 'w'); f.write('\n'); f.close()
>>> from joeTempPkg import menu
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ImportError: cannot import name menu
>>> appPath = os.path.join('.','applications',appName)
>>> appMenuPath = os.path.join(appPath,'models','menu.py')
>>> shutil.copy(appMenuPath, joeTempPkg)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
NameError: name 'joeTempPkg' is not defined
>>> joeTempPkg = tmpPkgPath
>>> shutil.copy(appMenuPath, joeTempPkg)
>>> from joeTempPkg import menu
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\web2py\joeTempPkg\menu.py", line 8, in <module>
response.logo = A(B('web',SPAN(2),'py'),XML('™ '),
NameError: name 'A' is not defined
>>> from joeTempPkg.menu import *Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\web2py\joeTempPkg\menu.py", line 8, in <module>
response.logo = A(B('web',SPAN(2),'py'),XML('™ '),
NameError: name 'A' is not defined
>>> from joeTempPkg.menu import *
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\web2py\joeTempPkg\menu.py", line 8, in <module>
response.logo = A(B('web',SPAN(2),'py'),XML('™ '),
NameError: name 'A' is not defined
>>>
"""
Thanks again - Joe.