Changeset 221:45ec490f2dba
- Timestamp:
- 2007-08-10 21:02:53 (1 year ago)
- Files:
-
- browser.py (modified) (5 diffs)
- converter.py (modified) (6 diffs)
- session.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
browser.py
r185 r221 37 37 from websourcebrowser import converter 38 38 from websourcebrowser import pygmentsfinder 39 from websourcebrowser import session 39 40 from websourcebrowser import template 40 41 from websourcebrowser import urlpath … … 182 183 self.wfile.write(coding.encode(template.FOOTER)) 183 184 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 184 195 def do_GET(self): 185 196 """ … … 189 200 self.handle_builtin(self.path) 190 201 return 202 # separate path and query parameters 203 url, params = self._path_and_params(self.path) 191 204 try: 192 path = urlpath.to_file_system(config.root, self.path)205 path = urlpath.to_file_system(config.root, url) 193 206 except urlpath.NotUnderRoot: 194 207 # don't show items "above" the current directory; thereby … … 199 212 else: 200 213 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) 202 216 elif self.is_image_path(path): 203 html = converter.image_to_html( self.path)217 html = converter.image_to_html(url) 204 218 else: 205 219 data = self.get_file(path, raw=True, 206 size=self._binary_test_size)220 size=self._binary_test_size) 207 221 if self.is_binary(data): 208 222 data = self.get_file(path, raw=True) … … 212 226 html = converter.text_to_html(text, path) 213 227 # 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"." 215 229 title = urllib.unquote(title) 216 230 self.emit_data(html, title=title, mtime=os.path.getmtime(path)) converter.py
r218 r221 1 1 # Copyright (C) 2007, Stefan Schwarzer 2 # 2 # 3 3 # Permission is hereby granted, free of charge, to any person 4 4 # obtaining a copy of this software and associated documentation files … … 8 8 # and to permit persons to whom the Software is furnished to do so, 9 9 # subject to the following conditions: 10 # 10 # 11 11 # The above copyright notice and this permission notice shall be 12 12 # included in all copies or substantial portions of the Software. 13 # 13 # 14 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 15 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF … … 36 36 from websourcebrowser import config 37 37 from websourcebrowser import pygmentsfinder 38 from websourcebrowser import session 38 39 from websourcebrowser import urlpath 39 40 … … 117 118 return False 118 119 119 def dir_to_html(path): 120 def _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(' ') 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 ' % 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> ' % (url, value)) 183 add_html('</p>') 184 return "\n".join(html_parts) 185 186 def dir_to_html(path, params): 120 187 """ 121 188 Return HTML code unicode string for the directory `path`. … … 124 191 start_level = dir_level(path) 125 192 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(' ') 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' %s ' % 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> ') % 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)) 149 195 # show directory listing 150 html_parts.append(u'<table>')196 add_html(u'<table>') 151 197 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>') 154 200 for item in itertools.ifilterfalse(_ignore_item, 155 walk(path, config.dir_levels)):201 walk(path, params['dir_levels'])): 156 202 # if the item is a directory, append a slash 157 203 if os.path.isdir(item): … … 167 213 link_text = cgi.escape(coding.decode(os.path.basename(item))) 168 214 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) 172 219 else: 173 220 # not really a link 174 221 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>') 178 224 return u"\n".join(html_parts) 179 225 session.py
r220 r221 27 27 28 28 from websourcebrowser import coding 29 from websourcebrowser import config 29 30 30 31 … … 102 103 return urlparse.urlunparse(parsed_url).replace("+", "%20") 103 104 105 106 # define the default session, i. e. one with all the usual query 107 # parameters, including parameter conversion 108 class 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 136 default_session = DefaultSession()