##!/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 "arerwres ", levels ##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 "!!!!!!!!! " + 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 "" else: print "check_item", path, "good" ## 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 path = re.sub("/"+base_path, '', path) if value == None: value = 4 # do not show anything %> %if value == -1: %else: %endif
  • %if not show_html: "; } elseif ( $status == 4 ) { echo "" ; } ?> %endif ${name}
  • ${ title } ## switch shot number
    % if page == 'home':

    Tokamak GOLEM - Shot Database - ${shot}

    % elif show_source:

    ${ "Source code :: " + page_name }

    % else:

    ${ page_name }

    %endif ## !! INCLUDING OTHER WEB PAGES !!! %if show_source: # generate source code web page ${ code2html( page_path+'/'+page ) | trim} ## include without preprocessing, whitespace trimming,HTML escaping } %elif page in ['home', 'energetics'] : # base path ${emph("[Template source]")}
    ##<% print "!"*100 , base_path, page %> <%include file="${base_path + page}.php.template" /> %else: <% if re.match(".+\."+allowed_types+"$", page): page_tmp = page # link to an allowed type else: page_tmp = page_list(page_path+'/'+page,0) # in case of link to directory index_page = page_path+'/'+ page_tmp page_tmp = re.sub( '(.+)\.php', r'\1', index_page) %> % if exists(page_tmp) and re.match('.+\.php', index_page): ${ code2html( page_path+'/'+page ) | trim} % elif exists(index_page + '.template'): ## include with mako preprocessing ${emph("[Template source]")} %if os.path.exists(page_path + '/log') and os.path.getsize(page_path + '/log') > 0: ${emph("[Log]")} %endif <% print "!"*100 print index_page print page_path, page_tmp %> <%include file="${ index_page + '.template'}" /> % else: ## include without preprocessing, whitespace trimming,HTML escaping %if os.path.exists(page_path + '/log') and os.path.getsize(page_path + '/log') > 0: ${emph("[Log]")} %endif %if os.path.exists(index_page) and not os.path.isdir(index_page): <% print " generating index page ", index_page %> ${cat(index_page, []) | trim} %else: ##%except IOError, msg: <% print " missing index page ", index_page %>

    Some problem during loading of page ${index_page}

    ##Error:${msg}
    Check if directory contains one of the allowed files ${allowed_types}
    List of files:
      %try: %for item in sorted(os.listdir(index_page)):
    • ${item} %endfor %except: pass %endtry
    ##%endtry %endif % endif %endif
    % try: % if page in ["home", 'Data.php', 'References.php', "About.php"]:

    Diagnostics

      <% make_menu('diagnostics',2) %>

    Analysis

      <% make_menu('analysis',2) %>

    DAS

      <% make_menu('DAS',1) %>

    Vacuum log

      <% make_menu(base_path + 'nabijeni', -1, 'Charging log') %>

    Other

      <% make_item('Data.php', 'Data') ##make_item('References.php','References') make_item('http://golem.fjfi.cvut.cz/wiki/Bibliography','References') make_item('About.php','About') make_item('http://golem.fjfi.cvut.cz/wiki/', 'Wiki') make_item('http://golem.fjfi.cvut.cz/utils/', 'Utilities') %>

    Navigation

      <% make_item('/'+default_path+'/' + str(shot+1), 'Next') make_item('/'+default_path+'/' + str(shot-1), 'Previous') make_item('/'+default_path+'/0', 'Current') %>
    %elif not show_source:

    Menu

      <% make_menu(page_path, 0) %> ${make_item('/'+default_path+'/' + str(shot), '« Back to homepage')}

    Navigation

      <% make_item('/'+default_path+'/' + str(shot+1) +'/'+ page_path_short, 'Next') make_item('/'+default_path+'/' + str(shot-1) +'/'+ page_path_short , 'Previous') make_item('/'+default_path+'/0/' +'/'+ page_path_short, 'Current') %>
    %else:

    Navigation

      <% make_item("javascript:history.go(-1)", "Return") %>
    %endif %except:

    Menu failed

    ##raise %endtry
    <% # get disk usage of current directory command = "du -ch "+page_path+" | egrep '(celkem|total)'" disk_use = re.sub( '([\d\,\.]+\w)(.+\n)', r'\1', os.popen(command).readline()) %>