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
1=== added file 'ul10n_tools/lp_set_pot_priority/PotUtil.py'
2--- ul10n_tools/lp_set_pot_priority/PotUtil.py 1970-01-01 00:00:00 +0000
3+++ ul10n_tools/lp_set_pot_priority/PotUtil.py 2011-06-09 13:54:16 +0000
4@@ -0,0 +1,82 @@
5+#!/usr/bin/env python
6+# -*- coding: utf-8 -*-
7+#
8+# Original author: Rachid BM <rachidbm@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 logging
23+import sys
24+import csv
25+
26+class PotUtil(object):
27+ """
28+ Utility class for changing properties of po templates
29+ """
30+
31+
32+ def __init__(self, launchpad, releases):
33+ self.launchpad = launchpad
34+ self.releases = releases
35+ self.api_url = self.launchpad.distributions['ubuntu']
36+ logging.debug("API URL: {0}".format(self.api_url))
37+
38+
39+ def set_priority(self, source, template, priority):
40+ """
41+ Sets the priority of a template
42+
43+ Keyword arguments:
44+ source -- the source package
45+ template -- the name of the template
46+ priority -- the new priority
47+ """
48+ print "Setting {0}/{1}'s priority to {2}...".format(source, template,
49+ priority)
50+
51+ for release in self.releases:
52+ URL = '{0}/{1}/+source/{2}/+pots/{3}'.format(self.api_url, release,
53+ source, template)
54+ logging.debug("API URL: {0}".format(URL))
55+ pot = self.launchpad.load(URL)
56+ try:
57+ pot.priority = int(priority)
58+ except:
59+ print >> sys.stderr, 'ERROR: {0} is not a valid integer as ' \
60+ 'priority.'.format(priority)
61+ sys.exit(1)
62+ pot.lp_save()
63+
64+
65+ def set_priorities_csv(self, csv_filename):
66+ """
67+ Read templates/priorities from the CSV file
68+ and set the priority of each template
69+
70+ Keyword arguments:
71+ csv_filename -- the filename of the CSV file to read
72+ """
73+ # Read the CSV file
74+ with open(csv_filename, 'rb') as f:
75+ reader = csv.DictReader(f)
76+ prio_key, srcpkg_key, pot_key = reader.fieldnames
77+ for row in reader:
78+ priority = row[prio_key]
79+ source = row[srcpkg_key].strip()
80+ template = row[pot_key].strip()
81+ self.set_priority(source, template, priority)
82+
83+
84+ def set_name(self, source, template, new_name):
85+ # TODO
86+ pass
87
88=== modified file 'ul10n_tools/lp_set_pot_priority/__init__.py'
89--- ul10n_tools/lp_set_pot_priority/__init__.py 2011-06-08 11:28:20 +0000
90+++ ul10n_tools/lp_set_pot_priority/__init__.py 2011-06-09 13:54:16 +0000
91@@ -7,12 +7,10 @@
92 #
93 # https://translations.launchpad.net/ubuntu/natty/+templates
94 #
95-# (for the staging server)
96-#
97-# Note that we cannot fully use the LP API in a nice way, but the current script
98+# Note that we cannot fully use the LP API in a nice way, but the current script
99 # uses a workaround that works pretty well.
100-# The missing bits from the API are the traversal bits where you'd be able to walk
101-# the templates from within already exposed objects, instead of using
102+# The missing bits from the API are the traversal bits where you'd be able to walk
103+# the templates from within already exposed objects, instead of using
104 # "launchpad.load()" directly
105
106 # TODO: Generalize the script to be able to:
107@@ -20,36 +18,47 @@
108 # - Disable templates (active, exported_in_languagepacks properties)
109
110 import sys
111-import csv
112 import logging
113 import optparse
114-from ul10n_tools.utils.launchpadmanager import get_launchpad
115+import platform
116+from ul10n_tools.utils.launchpadmanager import (get_launchpad,
117+ get_distro_codenames)
118+from ul10n_tools.lp_set_pot_priority.PotUtil import PotUtil
119
120 LEVELS = ( logging.ERROR,
121 logging.WARNING,
122 logging.INFO,
123 logging.DEBUG,
124 )
125+DEFAULT_DISTRO_CODENAME = platform.dist()[2] # Codename of current system.
126
127 def main():
128 # Support for command line options.
129- # TODO: allow the script to be called without a CSV file, i.e.
130- # specifying --source, --template and --priority
131- # TODO: allow specifying a comma- or whitespace-separated list of distro
132- # series over which to loop over and set priorities
133 USAGE = """%prog [OPTIONS] FILE
134
135- Sets the priorities of translatable templates in Launchpad according to the
136- given CSV file"""
137+ Sets the properties of translatable templates in Launchpad"""
138 parser = optparse.OptionParser(usage = USAGE)
139+ parser.add_option("-c", "--distro-codenames", dest="distro_codenames",
140+ help="Specify one or more distro's codenames. Default is the version of your system. Comma-seperated when specifying more than one.")
141 parser.add_option('-d', '--debug', dest='debug_mode', action='store_true',
142 help = 'Print the maximum debugging info (implies -vvv)')
143+ parser.add_option('-f', '--csv-file', dest='csv_file',
144+ help = 'Specify a CSV file with templates and their desired priorities')
145+ parser.add_option('-l', '--use-production', dest='use_staging',
146+ action='store_false',
147+ help = 'Use the launchpad "production" server. Default is "staging"')
148+ parser.add_option('-p', '--priority', dest='priority',
149+ help = 'Set priority. Use this in conjunction with -t and -s')
150+ parser.add_option('-s', '--source', dest='source',
151+ help = 'Source package. Use this in conjunction with -t')
152+ parser.add_option('-t', '--template', dest='template',
153+ help = 'Template name. Use this in conjunction with -s')
154 parser.add_option('-v', '--verbose', dest='logging_level', action='count',
155 help = 'Set error_level output to warning, info, and then debug')
156- parser.add_option('-p', '--use-production', dest='use_staging', action='store_false',
157- help = 'Use the launchpad "production" server. Default is "staging"')
158
159- parser.set_defaults(logging_level = 0, use_staging = True)
160+ parser.set_defaults(logging_level = 0,
161+ use_staging = True,
162+ distro_codenames = DEFAULT_DISTRO_CODENAME)
163 (options, args) = parser.parse_args()
164
165 # Set the verbosity
166@@ -58,38 +67,27 @@
167 logging.basicConfig(level = LEVELS[options.logging_level],
168 format = '%(asctime)s %(levelname)s %(message)s')
169
170- # TODO: this should be an option (see TODO above)
171- UBUNTU_RELEASE = 'natty'
172-
173- # Error checking (the pattern must be specified)
174- # TODO: do this checking only if --source or --template haven't been specified
175- # TODO: when implementing this, check that whenever --source or --template are
176- # specified, the other one is as well
177- if len(args) < 1:
178- print >> sys.stderr, 'ERROR: Not enough arguments, a CSV file must be specified'
179+ # Log into Launchpad
180+ launchpad = get_launchpad(options.use_staging)
181+
182+ releases = options.distro_codenames.strip().split(',')
183+ logging.debug("Use staging: {0}".format(options.use_staging))
184+ logging.debug("Specified releases: {0}".format(', '.join(releases)))
185+
186+ # Validate specified distro's codenames
187+ for rel in releases:
188+ if not rel in get_distro_codenames(launchpad):
189+ print >> sys.stderr, "ERROR: {0} is not a valid Ubuntu release " \
190+ "name.".format(rel)
191+ sys.exit(1)
192+
193+ pot_util = PotUtil(launchpad, releases)
194+
195+ if options.source and options.template and options.priority:
196+ pot_util.set_priority(options.source, options.template, options.priority)
197+ elif options.csv_file:
198+ pot_util.set_priorities_csv(options.csv_file)
199+ else:
200+ print >> sys.stderr, 'ERROR: Not enough arguments. Either specify a' \
201+ ' CSV file with -f or the parameters -s, -t and -p must be specified.'
202 sys.exit(1)
203- else:
204- priorities_file = args[0]
205- logging.debug("Using CSV file: {0}".format(priorities_file))
206-
207- logging.debug("Use staging: %s" % (options.use_staging))
208-
209- launchpad = get_launchpad(options.use_staging)
210-
211- with open(priorities_file, 'rb') as f:
212- reader = csv.DictReader(f)
213- prio_key, srcpkg_key, pot_key = reader.fieldnames
214- for row in reader:
215- priority = int(row[prio_key])
216- srcpkg = row[srcpkg_key].strip()
217- pot = row[pot_key].strip()
218-
219- print "Setting {0}/{1}'s priority to {2}...".format(srcpkg, pot,
220- priority)
221- template = launchpad.load(
222- # TODO: make the 'natty' part a command line option (e.g. --distro natty)
223- 'https://api.launchpad.net/1.0/ubuntu/' +
224- '{0}/+source/{1}/+pots/{2}'.format(UBUNTU_RELEASE, srcpkg, pot))
225- template.priority = priority
226- template.lp_save()
227-
228
229=== added file 'ul10n_tools/lp_set_pot_priority/test.csv'
230--- ul10n_tools/lp_set_pot_priority/test.csv 1970-01-01 00:00:00 +0000
231+++ ul10n_tools/lp_set_pot_priority/test.csv 2011-06-09 13:54:16 +0000
232@@ -0,0 +1,4 @@
233+Priority,Source package,Template name
234+9010,sessioninstaller,sessioninstaller
235+7200,gnome-user-share,gnome-user-share
236+72a00,gsettings-desktop-schemas,gsettings-desktop-schemas
237
238=== modified file 'ul10n_tools/ul10n_toolsconfig.py'
239--- ul10n_tools/ul10n_toolsconfig.py 2011-04-09 08:51:51 +0000
240+++ ul10n_tools/ul10n_toolsconfig.py 2011-06-09 13:54:16 +0000
241@@ -26,6 +26,7 @@
242 from gettext import gettext as _
243 gettext.textdomain('ul10n-tools')
244
245+
246 class project_path_not_found(Exception):
247 """Raised when we can't find the project directory."""
248
249
250=== modified file 'ul10n_tools/utils/launchpadmanager.py'
251--- ul10n_tools/utils/launchpadmanager.py 2011-05-30 08:41:38 +0000
252+++ ul10n_tools/utils/launchpadmanager.py 2011-06-09 13:54:16 +0000
253@@ -31,3 +31,9 @@
254
255 return launchpad
256
257+
258+def get_distro_codenames(launchpad):
259+ """Returns a list of active distro series retrieved from Launchpad"""
260+ ubuntu = launchpad.distributions['ubuntu']
261+ ubuntu_series = ubuntu.series_collection
262+ return [series.name for series in ubuntu.series_collection if series.active]

Subscribers

People subscribed via source and target branches