| 1 | from translations.lib.types.pot import POTManager |
|---|
| 2 | from translations.models import POFile |
|---|
| 3 | from txcommon.log import logger |
|---|
| 4 | |
|---|
| 5 | class POTHandler: |
|---|
| 6 | """ |
|---|
| 7 | POTManager abstraction layer, specific to the projects app. |
|---|
| 8 | |
|---|
| 9 | You can use this higher-level object to interact with a |
|---|
| 10 | component's statistics instead of meddling with the lower- |
|---|
| 11 | level POTManager. Each Component obect gets one of these |
|---|
| 12 | as ``component.trans``. |
|---|
| 13 | |
|---|
| 14 | """ |
|---|
| 15 | |
|---|
| 16 | def __init__(self, component): |
|---|
| 17 | self.component = component |
|---|
| 18 | # TODO: Make Unit init the browser on its own |
|---|
| 19 | component.unit._init_browser() |
|---|
| 20 | browser = component.unit.browser |
|---|
| 21 | if hasattr(browser, 'filepath'): |
|---|
| 22 | filepath = browser.filepath |
|---|
| 23 | else: |
|---|
| 24 | filepath = None |
|---|
| 25 | logger.debug('File path: %s' % filepath) |
|---|
| 26 | self.tm = POTManager(component.get_files(), |
|---|
| 27 | browser.path, |
|---|
| 28 | component.source_lang, |
|---|
| 29 | component.file_filter, |
|---|
| 30 | filepath) |
|---|
| 31 | |
|---|
| 32 | def get_manager(self): |
|---|
| 33 | return self.tm |
|---|
| 34 | |
|---|
| 35 | def set_stats_for_lang(self, lang, try_to_merge=True): |
|---|
| 36 | """Set stats for a specific language.""" |
|---|
| 37 | return self.tm.create_lang_stats(lang, self.component, try_to_merge) |
|---|
| 38 | |
|---|
| 39 | def set_stats(self): |
|---|
| 40 | """Calculate stats for all translations of the component.""" |
|---|
| 41 | |
|---|
| 42 | logger.debug("Setting stats for %s" % self.component) |
|---|
| 43 | |
|---|
| 44 | # Copying the source file to the static dir |
|---|
| 45 | try: |
|---|
| 46 | potfiles = self.tm.get_source_files() |
|---|
| 47 | for potfile in potfiles: |
|---|
| 48 | self.tm.copy_file_to_static_dir(potfile) |
|---|
| 49 | except (AttributeError, IOError): |
|---|
| 50 | # TODO: There is no source file (POT) |
|---|
| 51 | # It looks like an intltool POT-based, what should we do? |
|---|
| 52 | pass |
|---|
| 53 | |
|---|
| 54 | # Set the source file (pot) to the database |
|---|
| 55 | self.tm.set_source_stats(self.component, is_msgmerged=False) |
|---|
| 56 | |
|---|
| 57 | for lang in self.tm.get_langs(): |
|---|
| 58 | self.set_stats_for_lang(lang) |
|---|
| 59 | |
|---|
| 60 | self.clear_old_stats() |
|---|
| 61 | |
|---|
| 62 | def clear_old_stats(self): |
|---|
| 63 | """ |
|---|
| 64 | Clear stats present on the database and msgmerge dir, that are not |
|---|
| 65 | anymore in the upstream repository |
|---|
| 66 | """ |
|---|
| 67 | pofiles = POFile.objects.select_related().filter( |
|---|
| 68 | component=self.component) |
|---|
| 69 | pots = self.tm.get_source_files() |
|---|
| 70 | files = self.tm.get_po_files() |
|---|
| 71 | for pot in pots: |
|---|
| 72 | files.append(pot) |
|---|
| 73 | logger.info(files) |
|---|
| 74 | for stat in pofiles: |
|---|
| 75 | if stat.filename not in files: |
|---|
| 76 | self.tm.delete_file_from_static_dir(stat.filename) |
|---|
| 77 | stat.delete() |
|---|
| 78 | |
|---|
| 79 | def clear_stats(self): |
|---|
| 80 | """Clear stats for all translations of the component.""" |
|---|
| 81 | |
|---|
| 82 | # Deleting all stats for the component |
|---|
| 83 | logger.debug("Clearing stats for %s" % self.component) |
|---|
| 84 | self.tm.delete_stats_for_object(self.component) |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | def get_stats(self): |
|---|
| 88 | """Return stats for the component.""" |
|---|
| 89 | return self.tm.get_stats(self.component) |
|---|
| 90 | |
|---|
| 91 | def get_file_content(self, filename, is_msgmerged): |
|---|
| 92 | """Return stats for the component.""" |
|---|
| 93 | return self.tm.get_file_content(filename, is_msgmerged) |
|---|
| 94 | |
|---|
| 95 | def get_source_stats(self): |
|---|
| 96 | return self.tm.get_source_stats(self.component) |
|---|
| 97 | |
|---|
| 98 | def guess_language(self, filename): |
|---|
| 99 | """Set stats for a specific language.""" |
|---|
| 100 | return self.tm.guess_language(filename) |
|---|
| 101 | |
|---|
| 102 | def msgfmt_check(self, po_contents): |
|---|
| 103 | """Check a POT/PO file with msgfmt -c.""" |
|---|
| 104 | return self.tm.msgfmt_check(po_contents) |
|---|