Merge lp:~rachidbm/ubuntu-l10n-tools/pot-priority into lp:ubuntu-l10n-tools

Proposed by Rachid
Status: Merged
Merge reported by: David Planella
Merged at revision: not available
Proposed branch: lp:~rachidbm/ubuntu-l10n-tools/pot-priority
Merge into: lp:ubuntu-l10n-tools
Diff against target: 262 lines (+141/-50)
5 files modified
ul10n_tools/lp_set_pot_priority/PotUtil.py (+82/-0)
ul10n_tools/lp_set_pot_priority/__init__.py (+48/-50)
ul10n_tools/lp_set_pot_priority/test.csv (+4/-0)
ul10n_tools/ul10n_toolsconfig.py (+1/-0)
ul10n_tools/utils/launchpadmanager.py (+6/-0)
To merge this branch: bzr merge lp:~rachidbm/ubuntu-l10n-tools/pot-priority
Reviewer Review Type Date Requested Status
David Planella Approve
Review via email: mp+64016@code.launchpad.net

This proposal supersedes a proposal from 2011-06-09.

Description of the change

Retrieve active series from Launchpad
Added class to handle the stuff regarding POT and Launchpad
Prioritise just one template or give an CSV file
Edit 1 pot over several distro codenames

Removed unnecessary has_header check in CSV reader
Removed obsolete imports

To post a comment you must log in.
Revision history for this message
David Planella (dpm) wrote :

Looks good, thanks Rachid!

Just a couple of things

* On ul10n_tools/lp_set_pot_priority/__init__.py the "The missing bits from the API are the traversal bits where you'd be able to walk" line and others introduce unnecessary trailing space. Please try to look at the diff before sending future merge proposals
* The new test.csv file has a line with an invalid prioritiy (72a00). Is this intentional?
* It'd be great to start adding automated tests for all tools, and move away this .csv file to be part of the tests. Some background info: https://wiki.ubuntu.com/MeetingLogs/appdevweek1104/RockSolidPython
* I'd suggest moving the PotUtil module somewhere else rather than inside ul10n_tools/lp_set_pot_priority/, as in principle it should be not directly related to setting the priority, but rather in the future it should be able to set any template properties-

These are not blockers, so I'll merge the branch now. Good work!

review: Approve
Revision history for this message
Rachid (rachidbm) wrote :

Thanks for the review. I agree with your remarks.

The invalid property in test.csv was intentional for testing the NON happy flow. This can be removed when we have proper automatic tests.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'ul10n_tools/lp_set_pot_priority/PotUtil.py'
--- ul10n_tools/lp_set_pot_priority/PotUtil.py 1970-01-01 00:00:00 +0000
+++ ul10n_tools/lp_set_pot_priority/PotUtil.py 2011-06-09 13:54:16 +0000
@@ -0,0 +1,82 @@
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Original author: Rachid BM <rachidbm@ubuntu.com>
5#
6# This program is free software: you can redistribute it and/or modify it
7# under the terms of the GNU General Public License version 3, as published
8# by the Free Software Foundation.
9#
10# This program is distributed in the hope that it will be useful, but
11# WITHOUT ANY WARRANTY; without even the implied warranties of
12# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13# PURPOSE. See the GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License along
16# with this program. If not, see <http://www.gnu.org/licenses/>.
17
18import logging
19import sys
20import csv
21
22class PotUtil(object):
23 """
24 Utility class for changing properties of po templates
25 """
26
27
28 def __init__(self, launchpad, releases):
29 self.launchpad = launchpad
30 self.releases = releases
31 self.api_url = self.launchpad.distributions['ubuntu']
32 logging.debug("API URL: {0}".format(self.api_url))
33
34
35 def set_priority(self, source, template, priority):
36 """
37 Sets the priority of a template
38
39 Keyword arguments:
40 source -- the source package
41 template -- the name of the template
42 priority -- the new priority
43 """
44 print "Setting {0}/{1}'s priority to {2}...".format(source, template,
45 priority)
46
47 for release in self.releases:
48 URL = '{0}/{1}/+source/{2}/+pots/{3}'.format(self.api_url, release,
49 source, template)
50 logging.debug("API URL: {0}".format(URL))
51 pot = self.launchpad.load(URL)
52 try:
53 pot.priority = int(priority)
54 except:
55 print >> sys.stderr, 'ERROR: {0} is not a valid integer as ' \
56 'priority.'.format(priority)
57 sys.exit(1)
58 pot.lp_save()
59
60
61 def set_priorities_csv(self, csv_filename):
62 """
63 Read templates/priorities from the CSV file
64 and set the priority of each template
65
66 Keyword arguments:
67 csv_filename -- the filename of the CSV file to read
68 """
69 # Read the CSV file
70 with open(csv_filename, 'rb') as f:
71 reader = csv.DictReader(f)
72 prio_key, srcpkg_key, pot_key = reader.fieldnames
73 for row in reader:
74 priority = row[prio_key]
75 source = row[srcpkg_key].strip()
76 template = row[pot_key].strip()
77 self.set_priority(source, template, priority)
78
79
80 def set_name(self, source, template, new_name):
81 # TODO
82 pass
083
=== modified file 'ul10n_tools/lp_set_pot_priority/__init__.py'
--- ul10n_tools/lp_set_pot_priority/__init__.py 2011-06-08 11:28:20 +0000
+++ ul10n_tools/lp_set_pot_priority/__init__.py 2011-06-09 13:54:16 +0000
@@ -7,12 +7,10 @@
7#7#
8# https://translations.launchpad.net/ubuntu/natty/+templates8# https://translations.launchpad.net/ubuntu/natty/+templates
9#9#
10# (for the staging server)10# Note that we cannot fully use the LP API in a nice way, but the current script
11#
12# Note that we cannot fully use the LP API in a nice way, but the current script
13# uses a workaround that works pretty well.11# uses a workaround that works pretty well.
14# The missing bits from the API are the traversal bits where you'd be able to walk12# The missing bits from the API are the traversal bits where you'd be able to walk
15# the templates from within already exposed objects, instead of using13# the templates from within already exposed objects, instead of using
16# "launchpad.load()" directly14# "launchpad.load()" directly
1715
18# TODO: Generalize the script to be able to:16# TODO: Generalize the script to be able to:
@@ -20,36 +18,47 @@
20# - Disable templates (active, exported_in_languagepacks properties)18# - Disable templates (active, exported_in_languagepacks properties)
2119
22import sys20import sys
23import csv
24import logging21import logging
25import optparse22import optparse
26from ul10n_tools.utils.launchpadmanager import get_launchpad23import platform
24from ul10n_tools.utils.launchpadmanager import (get_launchpad,
25 get_distro_codenames)
26from ul10n_tools.lp_set_pot_priority.PotUtil import PotUtil
2727
28LEVELS = ( logging.ERROR,28LEVELS = ( logging.ERROR,
29 logging.WARNING,29 logging.WARNING,
30 logging.INFO,30 logging.INFO,
31 logging.DEBUG,31 logging.DEBUG,
32 )32 )
33DEFAULT_DISTRO_CODENAME = platform.dist()[2] # Codename of current system.
3334
34def main():35def main():
35 # Support for command line options.36 # Support for command line options.
36 # TODO: allow the script to be called without a CSV file, i.e.
37 # specifying --source, --template and --priority
38 # TODO: allow specifying a comma- or whitespace-separated list of distro
39 # series over which to loop over and set priorities
40 USAGE = """%prog [OPTIONS] FILE37 USAGE = """%prog [OPTIONS] FILE
4138
42 Sets the priorities of translatable templates in Launchpad according to the39 Sets the properties of translatable templates in Launchpad"""
43 given CSV file"""
44 parser = optparse.OptionParser(usage = USAGE)40 parser = optparse.OptionParser(usage = USAGE)
41 parser.add_option("-c", "--distro-codenames", dest="distro_codenames",
42 help="Specify one or more distro's codenames. Default is the version of your system. Comma-seperated when specifying more than one.")
45 parser.add_option('-d', '--debug', dest='debug_mode', action='store_true',43 parser.add_option('-d', '--debug', dest='debug_mode', action='store_true',
46 help = 'Print the maximum debugging info (implies -vvv)')44 help = 'Print the maximum debugging info (implies -vvv)')
45 parser.add_option('-f', '--csv-file', dest='csv_file',
46 help = 'Specify a CSV file with templates and their desired priorities')
47 parser.add_option('-l', '--use-production', dest='use_staging',
48 action='store_false',
49 help = 'Use the launchpad "production" server. Default is "staging"')
50 parser.add_option('-p', '--priority', dest='priority',
51 help = 'Set priority. Use this in conjunction with -t and -s')
52 parser.add_option('-s', '--source', dest='source',
53 help = 'Source package. Use this in conjunction with -t')
54 parser.add_option('-t', '--template', dest='template',
55 help = 'Template name. Use this in conjunction with -s')
47 parser.add_option('-v', '--verbose', dest='logging_level', action='count',56 parser.add_option('-v', '--verbose', dest='logging_level', action='count',
48 help = 'Set error_level output to warning, info, and then debug')57 help = 'Set error_level output to warning, info, and then debug')
49 parser.add_option('-p', '--use-production', dest='use_staging', action='store_false',
50 help = 'Use the launchpad "production" server. Default is "staging"')
5158
52 parser.set_defaults(logging_level = 0, use_staging = True)59 parser.set_defaults(logging_level = 0,
60 use_staging = True,
61 distro_codenames = DEFAULT_DISTRO_CODENAME)
53 (options, args) = parser.parse_args()62 (options, args) = parser.parse_args()
5463
55 # Set the verbosity64 # Set the verbosity
@@ -58,38 +67,27 @@
58 logging.basicConfig(level = LEVELS[options.logging_level],67 logging.basicConfig(level = LEVELS[options.logging_level],
59 format = '%(asctime)s %(levelname)s %(message)s')68 format = '%(asctime)s %(levelname)s %(message)s')
6069
61 # TODO: this should be an option (see TODO above)70 # Log into Launchpad
62 UBUNTU_RELEASE = 'natty'71 launchpad = get_launchpad(options.use_staging)
6372
64 # Error checking (the pattern must be specified)73 releases = options.distro_codenames.strip().split(',')
65 # TODO: do this checking only if --source or --template haven't been specified74 logging.debug("Use staging: {0}".format(options.use_staging))
66 # TODO: when implementing this, check that whenever --source or --template are75 logging.debug("Specified releases: {0}".format(', '.join(releases)))
67 # specified, the other one is as well76
68 if len(args) < 1:77 # Validate specified distro's codenames
69 print >> sys.stderr, 'ERROR: Not enough arguments, a CSV file must be specified'78 for rel in releases:
79 if not rel in get_distro_codenames(launchpad):
80 print >> sys.stderr, "ERROR: {0} is not a valid Ubuntu release " \
81 "name.".format(rel)
82 sys.exit(1)
83
84 pot_util = PotUtil(launchpad, releases)
85
86 if options.source and options.template and options.priority:
87 pot_util.set_priority(options.source, options.template, options.priority)
88 elif options.csv_file:
89 pot_util.set_priorities_csv(options.csv_file)
90 else:
91 print >> sys.stderr, 'ERROR: Not enough arguments. Either specify a' \
92 ' CSV file with -f or the parameters -s, -t and -p must be specified.'
70 sys.exit(1)93 sys.exit(1)
71 else:
72 priorities_file = args[0]
73 logging.debug("Using CSV file: {0}".format(priorities_file))
74
75 logging.debug("Use staging: %s" % (options.use_staging))
76
77 launchpad = get_launchpad(options.use_staging)
78
79 with open(priorities_file, 'rb') as f:
80 reader = csv.DictReader(f)
81 prio_key, srcpkg_key, pot_key = reader.fieldnames
82 for row in reader:
83 priority = int(row[prio_key])
84 srcpkg = row[srcpkg_key].strip()
85 pot = row[pot_key].strip()
86
87 print "Setting {0}/{1}'s priority to {2}...".format(srcpkg, pot,
88 priority)
89 template = launchpad.load(
90 # TODO: make the 'natty' part a command line option (e.g. --distro natty)
91 'https://api.launchpad.net/1.0/ubuntu/' +
92 '{0}/+source/{1}/+pots/{2}'.format(UBUNTU_RELEASE, srcpkg, pot))
93 template.priority = priority
94 template.lp_save()
95
9694
=== added file 'ul10n_tools/lp_set_pot_priority/test.csv'
--- ul10n_tools/lp_set_pot_priority/test.csv 1970-01-01 00:00:00 +0000
+++ ul10n_tools/lp_set_pot_priority/test.csv 2011-06-09 13:54:16 +0000
@@ -0,0 +1,4 @@
1Priority,Source package,Template name
29010,sessioninstaller,sessioninstaller
37200,gnome-user-share,gnome-user-share
472a00,gsettings-desktop-schemas,gsettings-desktop-schemas
05
=== modified file 'ul10n_tools/ul10n_toolsconfig.py'
--- ul10n_tools/ul10n_toolsconfig.py 2011-04-09 08:51:51 +0000
+++ ul10n_tools/ul10n_toolsconfig.py 2011-06-09 13:54:16 +0000
@@ -26,6 +26,7 @@
26from gettext import gettext as _26from gettext import gettext as _
27gettext.textdomain('ul10n-tools')27gettext.textdomain('ul10n-tools')
2828
29
29class project_path_not_found(Exception):30class project_path_not_found(Exception):
30 """Raised when we can't find the project directory."""31 """Raised when we can't find the project directory."""
3132
3233
=== modified file 'ul10n_tools/utils/launchpadmanager.py'
--- ul10n_tools/utils/launchpadmanager.py 2011-05-30 08:41:38 +0000
+++ ul10n_tools/utils/launchpadmanager.py 2011-06-09 13:54:16 +0000
@@ -31,3 +31,9 @@
3131
32 return launchpad32 return launchpad
3333
34
35def get_distro_codenames(launchpad):
36 """Returns a list of active distro series retrieved from Launchpad"""
37 ubuntu = launchpad.distributions['ubuntu']
38 ubuntu_series = ubuntu.series_collection
39 return [series.name for series in ubuntu.series_collection if series.active]

Subscribers

People subscribed via source and target branches