--- pylons / util.py 2009-12-29 14: 28: 20.000000000 +0600
+++ dev / null 2010-02-05 03: 44: 26.000000000 +0600
@@ -122.6 +122.8 @@
'Oneword'
"" "
+ module_name = module_name.split ('.') [- 1]
words = module_name.replace ('-', '_'). split ('_')
return '' .join ([w.title () for w in words])
# - * - coding: utf-8 - * -
import os
def scan_folder_recurse (folder, excl_names = ['__']):
"" "Recurse search for PY files in given folder" ""
all_files = []
for root, dir, files in os.walk (folder):
filelist = \
[os.path.join (root, fi) for files in files if fi.endswith ('. py')
and not any (fi.startswith (prefix) for prefix in excl_names)]
for f in filelist:
all_files.append (f)
return all_files
map = Mapper (directory = config ['pylons.paths'] ['controllers'],
always_scan = config ['debug'])
map.minimization = False
proj_root = os.path.dirname (config ['pylons.paths'] ['root'])
sep = os.path.sep
"" "GETTING ALL FILES ENTIRE CONTROLLERS FOLDER" ""
all_files = scan_folder_recurse (
config ['pylons.paths'] ['controllers'],
excl_names = ['__', 'daemon', 'models'])
log.debug ("Found% d controllers"% len (all_files))
log.debug ("Building route map")
cfg = ConfigParser.RawConfigParser ()
cfg.read (config ['global_conf'] ['__ file__'])
for file in all_files:
t_controller_name = module_path.split ('.') [- 1]
controller_name = '/'.join(module_path.split('.')[2:])
"" "IMPORTING MODULE" ""
controller = __import __ (module_path)
"" "IMPORTING MODULE ENVIRONMENT" ""
controller = sys.modules [module_path]
my_list = dir (controller)
name_re = re.compile (t_controller_name, re.IGNORECASE)
"" "We need classes with methods" ""
control = None
for element in my_list:
if name_re.search (element) and controller_re.search (element):
control = getattr (controller, element)
"" "If class found" ""
if control:
"" "Searching for need property" ""
for item in control .__ dict__:
try:
attrib = control .__ dict __ [item] .__ dict __ ['method']
"" "If Class has method property" ""
if attrib == 'path':
route_path = getattr (control, item) .route_path
else:
route_path = "/% s /% s"% (controller_name, item)
route_path = route_path.rstrip ('/')
"" "Two method to create variations of path" ""
map.connect (
route_path,
controller = controller_name,
action = item)
map.connect (
"% s /"% route_path,
controller = controller_name,
action = item)
log.info ("% s ::% s ---- >>>>% s ..... connected"% \
(controller_name, item, route_path))
except:
pass
log.info ('Route map complite ...')
# The ErrorController route (handles 404/500 error pages); it should
# likely to be resolved
map.connect ('/ error / {action}', controller = 'error')
map.connect ('/ error / {action} / {id}', controller = 'error')
return map
# - * - coding: utf-8 - * -
"" "Decorators for project
"" "
def route_action (path = None):
def decorate (f):
if path == None:
setattr (f, 'method', 'local')
else:
setattr (f, 'method', 'path')
setattr (f, 'route_path', path)
return f
return decorate
from PROJECT.lib.decorators import route_action
class SampleController (BaseController):
@route_action ('/ sample / hello_world')
def hello (self):
return "This is Sample / Hello method"
@route_action ()
def hello_to_me (self):
return "This is Sample / Hello_to_me local method"
@route_action ('/ sample / hello_world / {id}')
def hello (self, id):
return "This is the Sample / Hello World with ID:% d method"% id
Source: https://habr.com/ru/post/94413/
All Articles