source: watchlistplugin/0.12/tracwatchlist/manual.py

Last change on this file was 15264, checked in by Ryan J Ollos, 8 years ago

Remove unnecessary svn:mime-type on py files

svn:mime-type was set to "plain" for many files.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Id URL Rev Author Date
File size: 4.1 KB
Line 
1# -*- coding: utf-8 -*-
2"""
3= Watchlist Plugin for Trac =
4Plugin Website:  http://trac-hacks.org/wiki/WatchlistPlugin
5Trac website:    http://trac.edgewall.org/
6
7Copyright (c) 2008-2010 by Martin Scharrer <martin@scharrer-online.de>
8All rights reserved.
9
10The i18n support was added by Steffen Hoffmann <hoff.st@web.de>.
11
12This program is free software: you can redistribute it and/or modify
13it under the terms of the GNU General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17This program is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20GNU General Public License for more details.
21
22For 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
33from  pkg_resources          import  resource_filename
34from  datetime               import  datetime
35import os
36
37from  trac.core              import  *
38from  genshi.builder         import  tag
39from  trac.web.api           import  IRequestHandler, HTTPNotFound
40from  trac.wiki.formatter    import  format_to_html
41from  trac.mimeview.api      import  Context
42from  trac.util.text         import  unicode_unquote, to_unicode
43from  tracwatchlist.util     import  LC_TIME
44
45
46class 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
Note: See TracBrowser for help on using the repository browser.