##!/usr/bin/python2 ## -*- coding: utf-8 -*- ##CREATED: 7/2012 ##AUTHOR: MICHAL ODSTRCIL <%page args="shot, page, default_path" cached="True" cache_dir='mako_modules' /> <%! import os, re, datetime from pygolem_lite.web import cat, emph, get_page_paths from os.path import split, exists ###from pygolem_lite.MoinMoin import code2html from pygolem_lite import web, Shot, loadconst from pygolem_lite.code2html import code2html %> <% title = 'Golem No: ' + str(shot) + ' - the only fully opensource tokamak' shot = int(shot) if page == "": page = "home" allowed_types = "(htm|html|php|template)" # types allowed for loading as webpage page_path, base_path, page = get_page_paths(shot, page, default_path) page_path_short = re.sub("^"+default_path+"\/[\d]+\/", "", page_path) page_path_short = re.sub("\/\/", "\/", page_path_short + '/') # remove shots/ + shot_number from path show_source = re.match(".+\.(py|m|pro|c|sh|template|cfg)$", page) show_html = re.match(".+\.(htm|html)$", page) ###### setup pygolem loading ########### web.cData = Shot(shot) get = web.cData.get_nice ######## setup pygolem loading ########### def page_list(base,number= None ): """ return list of acceptable pages in "base" directory""" if show_source: sub = "" # do not replace template ending for source files else: sub = "(\.template)" l = sorted([ re.sub('(.+)'+sub+'$',r"\1", file) \ for file in os.listdir( base) \ if ( re.match(".+\."+allowed_types+"$", file) \ and not re.match('.+~', file))]) if number is not None: try: return sorted(l)[number] except: return "" return l def get_page_name(name): """ remove from page name numbers at beginning and .htm ending""" name = re.sub('(\d+_)(.+)([\.\w]*)$', r'\2', name) name = re.sub('(.+)\.([\.\w]+)$', r'\1', name) # remove everything before lst backslash name = re.sub('(.+)\/(.+)$', r'\2', name) return name if page == "": # if page is directory take the first web page page_name = get_page_name(page_list(page_path+'/'+page,0)) else: page_name = get_page_name(page) def make_menu(base, levels, name = ""): if base_path in base: base = re.sub(base_path, base_path + "includes/", base) else: base = base_path + base try: if levels == -1: if name == "": name = get_page_name(base) make_item( '/'+ base + '/', name) elif levels == 0: # load directly all html try: paths = page_list( base) paths = sorted(paths) #print "===page list", base, paths for item in paths: name = get_page_name(item) make_item( '/'+ base + '/' + item, name) except Exception, e: print "!!!!!!! local menu failed " + str(e) , base, else: try: paths = sorted( [ '/' + base + '/' + i for i in os.listdir(base) ]) ##print paths for path in paths: ##print path if levels == 1: make_item( path + '/' , split(path)[-1], -1) elif levels == 2 and os.path.isdir(path): for item in os.listdir( path): make_item( path+'/'+item+'/' , item, -1) except Exception, e: print "menu loading failed !!!!!!!!!!!!!!!!!!!! " + str(e), base except Exception, e: print "!!!!!!! menu_make failed " + str(e) def check_item(path): """ Check if the item is allowed in includes directory """ if base_path in path: if not "includes" in path: path = re.sub(base_path, base_path + "/includes/", path) return os.path.exists(path) or os.path.exists(path + ".template") else: return True %> <%def name="make_item(path, item, value=None)"> <% if not check_item(path): print "check_item", path, "failed" return "" ## remove names, dates, initials name = re.sub('(\d+)([a-zA-Z\ ]+)([_\.\w]+)', r'\2', item) name = re.sub('([\w]+)/([a-zA-Z\ ]+)', r'\2', name) path = re.sub('(includes/)(.+)', r'\2', path) # remove includes path from full path if value == None: value = 4 # do not show anything %> %if value == -1: %else: %endif