| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | """ |
|---|
| 3 | = Watchlist Plugin for Trac = |
|---|
| 4 | Plugin Website: http://trac-hacks.org/wiki/WatchlistPlugin |
|---|
| 5 | Trac website: http://trac.edgewall.org/ |
|---|
| 6 | |
|---|
| 7 | Copyright (c) 2008-2010 by Martin Scharrer <martin@scharrer-online.de> |
|---|
| 8 | All rights reserved. |
|---|
| 9 | |
|---|
| 10 | The i18n support was added by Steffen Hoffmann <hoff.st@web.de>. |
|---|
| 11 | |
|---|
| 12 | This program is free software: you can redistribute it and/or modify |
|---|
| 13 | it under the terms of the GNU General Public License as published by |
|---|
| 14 | the Free Software Foundation, either version 3 of the License, or |
|---|
| 15 | (at your option) any later version. |
|---|
| 16 | |
|---|
| 17 | This program is distributed in the hope that it will be useful, |
|---|
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 20 | GNU General Public License for more details. |
|---|
| 21 | |
|---|
| 22 | For a copy of the GNU General Public License see |
|---|
| 23 | <http://www.gnu.org/licenses/>. |
|---|
| 24 | |
|---|
| 25 | $Id: manual.py 15264 2016-02-11 04:22:34Z rjollos $ |
|---|
| 26 | """ |
|---|
| 27 | |
|---|
| 28 | __url__ = ur"$URL: //trac-hacks.org/svn/watchlistplugin/0.12/tracwatchlist/manual.py $"[6:-2] |
|---|
| 29 | __author__ = ur"$Author: rjollos $"[9:-2] |
|---|
| 30 | __revision__ = int("0" + ur"$Rev: 15264 $"[6:-2].strip('M')) |
|---|
| 31 | __date__ = ur"$Date: 2016-02-11 04:22:34 +0000 (Thu, 11 Feb 2016) $"[7:-2] |
|---|
| 32 | |
|---|
| 33 | from pkg_resources import resource_filename |
|---|
| 34 | from datetime import datetime |
|---|
| 35 | import os |
|---|
| 36 | |
|---|
| 37 | from trac.core import * |
|---|
| 38 | from genshi.builder import tag |
|---|
| 39 | from trac.web.api import IRequestHandler, HTTPNotFound |
|---|
| 40 | from trac.wiki.formatter import format_to_html |
|---|
| 41 | from trac.mimeview.api import Context |
|---|
| 42 | from trac.util.text import unicode_unquote, to_unicode |
|---|
| 43 | from tracwatchlist.util import LC_TIME |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | class WatchlistManual(Component): |
|---|
| 47 | implements( IRequestHandler ) |
|---|
| 48 | |
|---|
| 49 | manuals = {} |
|---|
| 50 | |
|---|
| 51 | def __init__(self): |
|---|
| 52 | dir = resource_filename('tracwatchlist', 'manuals') |
|---|
| 53 | for page in os.listdir(dir): |
|---|
| 54 | if page == '.svn': |
|---|
| 55 | continue |
|---|
| 56 | language = page.strip('.txt').replace('_','-') |
|---|
| 57 | filename = os.path.join(dir, page) |
|---|
| 58 | if os.path.isfile(filename): |
|---|
| 59 | self.manuals[language] = filename |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | ## Methods for IRequestHandler ########################################## |
|---|
| 63 | def match_request(self, req): |
|---|
| 64 | return req.path_info.startswith("/watchlist/manual") |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | def process_request(self, req): |
|---|
| 68 | path = req.path_info[ len("/watchlist/manual") : ].strip('/') |
|---|
| 69 | if path.startswith('attachments'): |
|---|
| 70 | return self.handle_attachment(req, path) |
|---|
| 71 | |
|---|
| 72 | language = path |
|---|
| 73 | if not language: |
|---|
| 74 | language = req.session.get('language', 'en-US') |
|---|
| 75 | |
|---|
| 76 | # Try to find a suitable language if no manual exists |
|---|
| 77 | # in the requested one. |
|---|
| 78 | if language not in self.manuals: |
|---|
| 79 | # Try to find a main language, |
|---|
| 80 | # e.g. 'xy' instead of 'xy-ZV' |
|---|
| 81 | l = language.split('-')[0] |
|---|
| 82 | language = 'en-US' # fallback if no other is found |
|---|
| 83 | if l in self.manuals: |
|---|
| 84 | language = l |
|---|
| 85 | else: |
|---|
| 86 | # Prefer 'en-US' before any other English dialect |
|---|
| 87 | if l == 'en' and 'en-US' in self.manuals: |
|---|
| 88 | language = 'en-US' |
|---|
| 89 | else: |
|---|
| 90 | # If there is none try to find |
|---|
| 91 | # any other 'xy-*' language |
|---|
| 92 | l += '-' |
|---|
| 93 | for lang in sorted(self.manuals.keys()): |
|---|
| 94 | if lang.startswith(l): |
|---|
| 95 | language = lang |
|---|
| 96 | break |
|---|
| 97 | req.redirect(req.href.watchlist('manual',language)) |
|---|
| 98 | |
|---|
| 99 | try: |
|---|
| 100 | f = open(self.manuals[language], 'r') |
|---|
| 101 | text = to_unicode( f.read() ) |
|---|
| 102 | except Exception as e: |
|---|
| 103 | raise HTTPNotFound(e) |
|---|
| 104 | |
|---|
| 105 | wldict = dict( |
|---|
| 106 | format_text=lambda text: format_to_html(self.env, Context.from_request(req), text), |
|---|
| 107 | text=text) |
|---|
| 108 | return ("watchlist_manual.html", wldict, "text/html") |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | def handle_attachment(self, req, path): |
|---|
| 112 | path = path[ len('attachments') : ].strip('/') |
|---|
| 113 | dir = resource_filename('tracwatchlist', 'manuals/attachments') |
|---|
| 114 | filename = os.path.join(dir, path) |
|---|
| 115 | if os.path.isfile(filename): |
|---|
| 116 | req.send_file(filename) |
|---|
| 117 | else: |
|---|
| 118 | raise HTTPNotFound(path) |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | # EOF |
|---|