Changeset 119:94d25dbb9625
- Timestamp:
- 2007-08-01 19:21:50
(1 year ago)
- Author:
- Stefan Schwarzer <sschwarzer@sschwarzer.net>
- branch:
- default
- Message:
Simplified error handling.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r118 |
r119 |
|
| 36 | 36 | import config |
|---|
| 37 | 37 | import pygments_finder |
|---|
| 38 | | |
|---|
| 39 | | |
|---|
| 40 | | class RestrictedItem(object): |
|---|
| 41 | | def __init__(self, item, message): |
|---|
| 42 | | self.item = item |
|---|
| 43 | | self.message = message |
|---|
| 44 | | def __str__(self): |
|---|
| 45 | | return self.message |
|---|
| | 38 | import urlpath |
|---|
| | 39 | |
|---|
| 46 | 40 | |
|---|
| 47 | 41 | def dir_level(path): |
|---|
| … | … | |
| 74 | 68 | dirs_and_files = os.listdir(path) |
|---|
| 75 | 69 | except OSError: |
|---|
| 76 | | yield RestrictedItem(path, "Directory is not readable.") |
|---|
| 77 | 70 | return |
|---|
| 78 | 71 | # filter out other items than directories and files and add a |
|---|
| … | … | |
| 151 | 144 | for item in itertools.ifilterfalse(ignore_item, |
|---|
| 152 | 145 | walk(path, config.dir_levels)): |
|---|
| 153 | | if isinstance(item, RestrictedItem): |
|---|
| 154 | | html_parts.append(u'<tr><td class="Error">%s</td></tr>' % item) |
|---|
| 155 | | continue |
|---|
| 156 | 146 | # if the item is a directory, append a slash |
|---|
| 157 | | elif os.path.isdir(item): |
|---|
| | 147 | if os.path.isdir(item): |
|---|
| 158 | 148 | target_attribute = u"" |
|---|
| 159 | 149 | suffix = u" /" |
|---|
| … | … | |
| 164 | 154 | (dir_level(item) - start_level - 1) |
|---|
| 165 | 155 | link_text = cgi.escape(coding.decode(os.path.basename(item))) |
|---|
| 166 | | if not os.access(item, os.R_OK): |
|---|
| 167 | | link_text = u' <span class="Error">%s</span>' % link_text |
|---|
| 168 | | link = u'<a href="%s"%s>%s</a>' % ( |
|---|
| 169 | | urllib.quote(item.replace(config.root, "")), |
|---|
| 170 | | target_attribute, link_text) |
|---|
| | 156 | if os.access(item, os.R_OK): |
|---|
| | 157 | link = u'<a href="%s"%s>%s</a>' % ( |
|---|
| | 158 | urlpath.to_url(config.root, item), |
|---|
| | 159 | target_attribute, link_text) |
|---|
| | 160 | else: |
|---|
| | 161 | # not really a link |
|---|
| | 162 | link = link_text |
|---|
| 171 | 163 | html_parts.append(u'<tr><td>%s%s%s</td></tr>' % |
|---|
| 172 | 164 | (spacing, link, suffix)) |
|---|