Changeset 221:45ec490f2dba

Show
Ignore:
Timestamp:
2007-08-10 21:02:53 (1 year ago)
Author:
Stefan Schwarzer <sschwarzer@sschwarzer.net>
branch:
default
Message:
The number of directory levels is no longer a per-server value, but
is instead passed from page to page via a query parameter. Allow to
decrease/increase directory nesting levels up to large values.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • browser.py

    r185 r221  
    3737from websourcebrowser import converter 
    3838from websourcebrowser import pygmentsfinder 
     39from websourcebrowser import session 
    3940from websourcebrowser import template 
    4041from websourcebrowser import urlpath 
     
    182183            self.wfile.write(coding.encode(template.FOOTER)) 
    183184 
     185    def _path_and_params(self, url): 
     186        """ 
     187        Return a tuple, splitting the URL `url` into an URL without 
     188        query string and a query mapping as returned by `cgi.parse_qs`. 
     189        """ 
     190        params = session.default_session.get_from_url(url) 
     191        parsed_url = list(urlparse.urlparse(url)) 
     192        parsed_url[4] = "" 
     193        return urlparse.urlunparse(parsed_url), params 
     194 
    184195    def do_GET(self): 
    185196        """ 
     
    189200            self.handle_builtin(self.path) 
    190201            return 
     202        # separate path and query parameters 
     203        url, params = self._path_and_params(self.path) 
    191204        try: 
    192             path = urlpath.to_file_system(config.root, self.path
     205            path = urlpath.to_file_system(config.root, url
    193206        except urlpath.NotUnderRoot: 
    194207            # don't show items "above" the current directory; thereby 
     
    199212        else: 
    200213            if os.path.isdir(path): 
    201                 html = converter.dir_to_html(path) 
     214                # extract `dir_levels` parameter 
     215                html = converter.dir_to_html(path, params) 
    202216            elif self.is_image_path(path): 
    203                 html = converter.image_to_html(self.path
     217                html = converter.image_to_html(url
    204218            else: 
    205219                data = self.get_file(path, raw=True, 
    206                                       size=self._binary_test_size) 
     220                                     size=self._binary_test_size) 
    207221                if self.is_binary(data): 
    208222                    data = self.get_file(path, raw=True) 
     
    212226                    html = converter.text_to_html(text, path) 
    213227            # assume title is UTF-8-encoded though we don't know for sure 
    214             title = coding.decode(self.path[1:]) or u"." 
     228            title = coding.decode(url[1:]) or u"." 
    215229            title = urllib.unquote(title) 
    216230            self.emit_data(html, title=title, mtime=os.path.getmtime(path)) 
  • converter.py

    r218 r221  
    11# Copyright (C) 2007, Stefan Schwarzer 
    2 #  
     2# 
    33# Permission is hereby granted, free of charge, to any person 
    44# obtaining a copy of this software and associated documentation files 
     
    88# and to permit persons to whom the Software is furnished to do so, 
    99# subject to the following conditions: 
    10 #  
     10# 
    1111# The above copyright notice and this permission notice shall be 
    1212# included in all copies or substantial portions of the Software. 
    13 #  
     13# 
    1414# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
    1515# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
     
    3636from websourcebrowser import config 
    3737from websourcebrowser import pygmentsfinder 
     38from websourcebrowser import session 
    3839from websourcebrowser import urlpath 
    3940 
     
    117118    return False 
    118119 
    119 def dir_to_html(path): 
     120def _dir_options_to_html(path, params): 
     121    """ 
     122    Return the menu of options preceding the directory display. 
     123    """ 
     124    html_parts = [] 
     125    add_html = html_parts.append 
     126    # show horizontal menu of directory levels 
     127    add_html('<p class="Options">') 
     128    add_html('<a href="/%s/frames" target="_top">Reset view</a>' % 
     129             config.SPECIAL_DIR) 
     130    add_html('&nbsp;') 
     131    add_html('<a href="/%s/help" target="%s">Help</a>' % 
     132             (config.SPECIAL_DIR, config.HELP_WINDOW_TARGET)) 
     133    add_html('</p>') 
     134    add_html('<p class="Options">') 
     135    add_html('Directory levels: ') 
     136    dir_levels = params['dir_levels'] 
     137    # 1 should always be there 
     138    level_options = [(1, "1")] 
     139    # `dir_levels - 1` 
     140    numeric_value = dir_levels - 1 
     141    numeric_value = max(numeric_value, 1) 
     142    value = str(numeric_value) 
     143    level_options.append((numeric_value, value)) 
     144    # `dir_levels` 
     145    numeric_value = dir_levels 
     146    value = str(numeric_value) 
     147    level_options.append((numeric_value, value)) 
     148    # `dir_levels + 1` 
     149    numeric_value = dir_levels + 1 
     150    numeric_value = min(numeric_value, sys.maxint) 
     151    value = str(numeric_value) 
     152    level_options.append((numeric_value, value)) 
     153    # all 
     154    numeric_value = sys.maxint 
     155    value = "all" 
     156    level_options.append((numeric_value, value)) 
     157    # sort offered dir levels 
     158    level_options.sort() 
     159    # remove duplicates 
     160    found_numeric_values = [] 
     161    new_level_options = [] 
     162    for numeric_value, value in level_options: 
     163        # check if we had this integer already; skip if yes 
     164        if numeric_value in found_numeric_values: 
     165            continue 
     166        # it doesn't make sense to have huge numeric values except the maximum 
     167        if numeric_value > 100 and numeric_value != sys.maxint: 
     168            continue 
     169        if numeric_value == sys.maxint: 
     170            value = "all" 
     171        found_numeric_values.append(numeric_value) 
     172        new_level_options.append((numeric_value, value)) 
     173    # build menu 
     174    for numeric_value, value in new_level_options: 
     175        if numeric_value == dir_levels: 
     176            add_html(u'%s&nbsp;' % value) 
     177        else: 
     178            url = urlpath.to_url(config.root, path) 
     179            new_params = params.copy() 
     180            new_params['dir_levels'] = numeric_value 
     181            url = session.default_session.add_to_url(url, new_params) 
     182            add_html(u'<a href="%s">%s</a>&nbsp;' % (url, value)) 
     183    add_html('</p>') 
     184    return "\n".join(html_parts) 
     185 
     186def dir_to_html(path, params): 
    120187    """ 
    121188    Return HTML code unicode string for the directory `path`. 
     
    124191    start_level = dir_level(path) 
    125192    html_parts = [] 
    126     # show horizontal menu of directory levels 
    127     html_parts.append('<p class="Options">') 
    128     html_parts.append('<a href="/%s/frames" target="_top">Reset view</a>' % 
    129                       config.SPECIAL_DIR) 
    130     html_parts.append('&nbsp;') 
    131     html_parts.append('<a href="/%s/help" target="%s">Help</a>' % 
    132                       (config.SPECIAL_DIR, config.HELP_WINDOW_TARGET)) 
    133     html_parts.append('</p>') 
    134     html_parts.append('<p class="Options">') 
    135     html_parts.append('Directory levels: ') 
    136     for arg in ("1", "2", "3", "all"): 
    137         if arg == "all": 
    138             numeric_arg = sys.maxint 
    139         else: 
    140             numeric_arg = int(arg) 
    141         if numeric_arg == config.dir_levels: 
    142             html_parts.append(u'&nbsp;%s&nbsp;' % arg) 
    143         else: 
    144             old_url = urlpath.to_url(config.root, path) 
    145             html_parts.append( 
    146               (u'<a href="/%s/set?dir_levels=%s&old_url=%s">%s</a>&nbsp;') % 
    147               (config.SPECIAL_DIR, arg, old_url, arg)) 
    148     html_parts.append('</p>') 
     193    add_html = html_parts.append 
     194    add_html(_dir_options_to_html(path, params)) 
    149195    # show directory listing 
    150     html_parts.append(u'<table>') 
     196    add_html(u'<table>') 
    151197    if path != config.root: 
    152         html_parts.append(u'<tr><td><span class="Options">' 
    153                           u'<a href="..">up</a></span></td></tr>') 
     198        add_html(u'<tr><td><span class="Options">' 
     199                 u'<a href="..">up</a></span></td></tr>') 
    154200    for item in itertools.ifilterfalse(_ignore_item, 
    155                 walk(path, config.dir_levels)): 
     201                walk(path, params['dir_levels'])): 
    156202        # if the item is a directory, append a slash 
    157203        if os.path.isdir(item): 
     
    167213        link_text = cgi.escape(coding.decode(os.path.basename(item))) 
    168214        if os.access(item, os.R_OK): 
    169             link = u'<a href="%s"%s>%s</a>' % ( 
    170                    urlpath.to_url(config.root, item), 
    171                    target_attribute, link_text) 
     215            url = urlpath.to_url(config.root, item) 
     216            if os.path.isdir(item): 
     217                url = session.default_session.add_to_url(url, params) 
     218            link = u'<a href="%s"%s>%s</a>' % (url, target_attribute, link_text) 
    172219        else: 
    173220            # not really a link 
    174221            link = link_text 
    175         html_parts.append(u'<tr><td>%s%s%s</td></tr>' % 
    176                           (spacing, link, suffix)) 
    177     html_parts.append(u'</table>') 
     222        add_html(u'<tr><td>%s%s%s</td></tr>' % (spacing, link, suffix)) 
     223    add_html(u'</table>') 
    178224    return u"\n".join(html_parts) 
    179225 
  • session.py

    r220 r221  
    2727 
    2828from websourcebrowser import coding 
     29from websourcebrowser import config 
    2930 
    3031 
     
    102103        return urlparse.urlunparse(parsed_url).replace("+", "%20") 
    103104 
     105 
     106# define the default session, i. e. one with all the usual query 
     107#  parameters, including parameter conversion 
     108class DefaultSession(Session): 
     109    def __init__(self): 
     110        super(DefaultSession, self).__init__(['dir_levels']) 
     111 
     112    def get_from_url(self, url): 
     113        params = super(DefaultSession, self).get_from_url(url) 
     114        # set default 
     115        dir_levels = config.dir_levels 
     116        if 'dir_levels' in params: 
     117            dir_levels = params['dir_levels'] 
     118            try: 
     119                dir_levels_ = int(dir_levels) 
     120                if dir_levels_ >= 1: 
     121                    dir_levels = dir_levels_ 
     122            except ValueError: 
     123                pass 
     124        params['dir_levels'] = dir_levels 
     125        return params 
     126 
     127    def add_to_url(self, url, params): 
     128        if 'dir_levels' in params: 
     129            dir_levels = params['dir_levels'] 
     130        else: 
     131            dir_levels = config.dir_levels 
     132        params['dir_levels'] = str(dir_levels) 
     133        return super(DefaultSession, self).add_to_url(url, params) 
     134 
     135 
     136default_session = DefaultSession()