~dpm/+junk/ubuntu-l10n-tools

« back to all changes in this revision

Viewing changes to ubuntu_l10n_tools/lp_get_templates/__init__.py

  • Committer: David Planella
  • Date: 2011-11-26 15:07:48 UTC
  • mfrom: (0.1.33 ubuntu-l10n-tools)
  • Revision ID: david.planella@ubuntu.com-20111126150748-og8gsowsfymsp18m
Merged ubuntu-l10n-tools

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# get-lp-templates - Given a source package, lists all its translation
 
4
# templates
 
5
#
 
6
# Author: David Planella <david.planella@ubuntu.com>
 
7
#
 
8
# Copyright 2009 David Planella <david.planella@ubuntu.com>.
 
9
#
 
10
# This program is free software: you can redistribute it and/or modify it
 
11
# under the terms of the GNU General Public License version 3, as published
 
12
# by the Free Software Foundation.
 
13
#
 
14
# This program is distributed in the hope that it will be useful, but
 
15
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
16
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
17
# PURPOSE.  See the GNU General Public License for more details.
 
18
#
 
19
# You should have received a copy of the GNU General Public License along
 
20
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
 
 
22
import urllib2
 
23
import re
 
24
import logging
 
25
import optparse
 
26
 
 
27
LEVELS = (logging.ERROR,
 
28
          logging.WARNING,
 
29
          logging.INFO,
 
30
          logging.DEBUG,
 
31
          )
 
32
 
 
33
LP_TRANSLATIONS_PAGE = "https://translations.launchpad.net/%s/%s/+source/%s"
 
34
RE_TEMPLATE = '.*<a href="/%s/%s/\+source/%s/\+pots/[a-zA-Z0-9\-\.]*"'
 
35
DISTRO_ID = 'ubuntu'
 
36
 
 
37
 
 
38
class Ignore404ErrorHandler(urllib2.HTTPDefaultErrorHandler):
 
39
    """ Error handler which does not raise a HTTPError if a page is not
 
40
    found, but returns the Error page instead.
 
41
    """
 
42
 
 
43
    def http_error_404(self, req, fp, code, msg, hdrs):
 
44
        return fp
 
45
 
 
46
#def templates_list_page_get(distro, distro_codename, src_pkg):
 
47
#    templates_list_page = []
 
48
#    try:
 
49
#        sock = urllib2.urlopen(LP_TRANSLATIONS_PAGE % (distro,
 
50
#                               distro_codename, src_pkg))
 
51
#        templates_list_page = sock.read()
 
52
#        sock.close()
 
53
#    except:
 
54
#        pass
 
55
#
 
56
#    return templates_list_page
 
57
 
 
58
def templates_get(distro_id, distro_codename, source_package):
 
59
 
 
60
    opener = urllib2.build_opener(Ignore404ErrorHandler)
 
61
    URL_TEMPLATE = LP_TRANSLATIONS_PAGE % (distro_id, distro_codename,
 
62
                                           source_package)
 
63
    logging.debug("Open URL: {0}".format(URL_TEMPLATE))
 
64
 
 
65
    page = opener.open(URL_TEMPLATE)
 
66
    data = page.read()
 
67
 
 
68
    #templates = RE_TEMPLATE.findall(data)
 
69
    templates_page_urls_regex = \
 
70
        r'.*<a href="/%s/%s/\+source/%s/\+pots/[a-zA-Z0-9\-\.]*"' \
 
71
            % (distro_id, distro_codename, source_package)
 
72
    templates_page_urls = re.findall(templates_page_urls_regex, data)
 
73
 
 
74
    return templates_page_urls
 
75
 
 
76
 
 
77
def main():
 
78
    # Support for command line options.
 
79
    USAGE = """%prog [OPTIONS]
 
80
 
 
81
    Given a source package and a distribution codename, lists all its
 
82
    translation templates available in Launchpad"""
 
83
    parser = optparse.OptionParser(usage = USAGE)
 
84
    parser.add_option('-d', '--debug', dest='debug_mode',
 
85
        action='store_true',
 
86
        help='Print the maximum debugging info (implies -vvv)')
 
87
    parser.add_option('-v', '--verbose', dest='logging_level',
 
88
        action='count',
 
89
        help='Set error_level output to warning, info, and then debug')
 
90
    parser.add_option("-c", "--distro-codename", dest="distro_codename",
 
91
            help="Specify the distro's codename. E.g. 'oneiric'")
 
92
    parser.add_option("-s", "--source-package", dest="source_package",
 
93
            help="Specify the source package.")
 
94
 
 
95
    parser.set_defaults(logging_level = 0)
 
96
    (options, args) = parser.parse_args()
 
97
 
 
98
    # Set the verbosity
 
99
    if options.debug_mode:
 
100
        options.logging_level = 3
 
101
    logging.basicConfig(level=LEVELS[options.logging_level],
 
102
        format='%(asctime)s %(levelname)s %(message)s')
 
103
 
 
104
    # TODO: change to args[0] instead of -s
 
105
    if not options.source_package:
 
106
        print "Must specify a source package"
 
107
    else:
 
108
        logging.debug("Source package: {0}".format(options.source_package))
 
109
        templates = templates_get(DISTRO_ID, options.distro_codename,
 
110
                                  options.source_package)
 
111
        if templates:
 
112
            for template in templates:
 
113
                print str(template.split("/")[-1:])
 
114
 
 
115
if __name__ == "__main__":
 
116
    main()