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