~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/translations/model/pofilestatsjob.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-11-22 14:12:40 UTC
  • mfrom: (14193.3.12 bug-877195-code)
  • Revision ID: launchpad@pqm.canonical.com-20111122141240-1x8vopdtuy5wl4pe
[r=jcsackett][bug=877195] [r=jcsackett] move POFile statistics update
        into a job

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2011 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Job for merging translations."""
 
5
 
 
6
 
 
7
__metaclass__ = type
 
8
 
 
9
 
 
10
__all__ = [
 
11
    'POFileStatsJob',
 
12
    ]
 
13
 
 
14
import logging
 
15
from zope.component import getUtility
 
16
 
 
17
from storm.locals import (
 
18
    And,
 
19
    Int,
 
20
    Reference,
 
21
    )
 
22
from zope.interface import (
 
23
    classProvides,
 
24
    implements,
 
25
    )
 
26
 
 
27
from canonical.launchpad.webapp.interfaces import (
 
28
    DEFAULT_FLAVOR,
 
29
    IStoreSelector,
 
30
    MAIN_STORE,
 
31
    )
 
32
from lp.services.database.stormbase import StormBase
 
33
from lp.services.job.interfaces.job import IRunnableJob
 
34
from lp.services.job.model.job import Job
 
35
from lp.services.job.runner import BaseRunnableJob
 
36
from lp.translations.interfaces.pofilestatsjob import IPOFileStatsJobSource
 
37
from lp.translations.model.pofile import POFile
 
38
 
 
39
 
 
40
class POFileStatsJob(StormBase, BaseRunnableJob):
 
41
    """The details for a POFile status update job."""
 
42
 
 
43
    __storm_table__ = 'POFileStatsJob'
 
44
 
 
45
    # Instances of this class are runnable jobs.
 
46
    implements(IRunnableJob)
 
47
 
 
48
    # Oddly, BaseRunnableJob inherits from BaseRunnableJobSource so this class
 
49
    # is both the factory for jobs (the "implements", above) and the source
 
50
    # for runnable jobs (not the constructor of the job source, the class
 
51
    # provides the IJobSource interface itself).
 
52
    classProvides(IPOFileStatsJobSource)
 
53
 
 
54
    # The Job table contains core job details.
 
55
    job_id = Int('job', primary=True)
 
56
    job = Reference(job_id, Job.id)
 
57
 
 
58
    # This is the POFile which needs its statistics updated.
 
59
    pofile_id = Int('pofile')
 
60
    pofile = Reference(pofile_id, POFile.id)
 
61
 
 
62
    def __init__(self, pofile):
 
63
        self.job = Job()
 
64
        self.pofile = pofile
 
65
        super(POFileStatsJob, self).__init__()
 
66
 
 
67
    def getOperationDescription(self):
 
68
        """See `IRunnableJob`."""
 
69
        return 'updating POFile statistics'
 
70
 
 
71
    def run(self):
 
72
        """See `IRunnableJob`."""
 
73
        logger = logging.getLogger()
 
74
        logger.info('Updating statistics for %s' % self.pofile.title)
 
75
        self.pofile.updateStatistics()
 
76
 
 
77
    @staticmethod
 
78
    def iterReady():
 
79
        """See `IJobSource`."""
 
80
        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
 
81
        return store.find((POFileStatsJob),
 
82
            And(POFileStatsJob.job == Job.id,
 
83
                Job.id.is_in(Job.ready_jobs)))
 
84
 
 
85
 
 
86
def schedule(pofile):
 
87
    """Schedule a job to update a POFile's stats."""
 
88
    return POFileStatsJob(pofile)