def test_timezones(): pofile = po.pofile() # The following will only work on Unix because of tzset() and %z if time.__dict__.has_key('tzset'): os.environ['TZ'] = 'Asia/Kabul' time.tzset() assert time.timezone == -16200 # Typically "+0430" assert poheader.tzstring() == time.strftime("%z") os.environ['TZ'] = 'Asia/Seoul' time.tzset() assert time.timezone == -32400 # Typically "+0900" assert poheader.tzstring() == time.strftime("%z") os.environ['TZ'] = 'Africa/Johannesburg' time.tzset() assert time.timezone == -7200 # Typically "+0200" assert poheader.tzstring() == time.strftime("%z") os.environ['TZ'] = 'Africa/Windhoek' time.tzset() assert time.timezone == -3600 # Typically "+0100" # For some reason python's %z doesn't know about Windhoek DST #assert poheader.tzstring() == time.strftime("%z") os.environ['TZ'] = 'UTC' time.tzset() assert time.timezone == 0 # Typically "+0000" assert poheader.tzstring() == time.strftime("%z")
def test_timezones(): # The following will only work on Unix because of tzset() and %z if "tzset" in time.__dict__: os.environ["TZ"] = "Asia/Kabul" time.tzset() assert time.timezone == -16200 # Typically "+0430" assert poheader.tzstring() == time.strftime("%z") os.environ["TZ"] = "Asia/Seoul" time.tzset() assert time.timezone == -32400 # Typically "+0900" assert poheader.tzstring() == time.strftime("%z") os.environ["TZ"] = "Africa/Johannesburg" time.tzset() assert time.timezone == -7200 # Typically "+0200" assert poheader.tzstring() == time.strftime("%z") os.environ["TZ"] = "UTC" time.tzset() assert time.timezone == 0 # Typically "+0000" assert poheader.tzstring() == time.strftime("%z")
def test_timezones(): pofile = po.pofile() # The following will only work on Unix because of tzset() and %z if 'tzset' in time.__dict__: os.environ['TZ'] = 'Asia/Kabul' time.tzset() assert time.timezone == -16200 # Typically "+0430" assert poheader.tzstring() == time.strftime("%z") os.environ['TZ'] = 'Asia/Seoul' time.tzset() assert time.timezone == -32400 # Typically "+0900" assert poheader.tzstring() == time.strftime("%z") os.environ['TZ'] = 'Africa/Johannesburg' time.tzset() assert time.timezone == -7200 # Typically "+0200" assert poheader.tzstring() == time.strftime("%z") os.environ['TZ'] = 'Africa/Windhoek' time.tzset() assert time.timezone == -3600 # Typically "+0100" # For some reason python's %z doesn't know about Windhoek DST #assert poheader.tzstring() == time.strftime("%z") os.environ['TZ'] = 'UTC' time.tzset() assert time.timezone == 0 # Typically "+0000" assert poheader.tzstring() == time.strftime("%z")
def test_timezones(): pofile = po.pofile() # The following will only work on Unix because of tzset() and %z if 'tzset' in time.__dict__: os.environ['TZ'] = 'Asia/Kabul' time.tzset() assert time.timezone == -16200 # Typically "+0430" assert poheader.tzstring() == time.strftime("%z") os.environ['TZ'] = 'Asia/Seoul' time.tzset() assert time.timezone == -32400 # Typically "+0900" assert poheader.tzstring() == time.strftime("%z") os.environ['TZ'] = 'Africa/Johannesburg' time.tzset() assert time.timezone == -7200 # Typically "+0200" assert poheader.tzstring() == time.strftime("%z") os.environ['TZ'] = 'UTC' time.tzset() assert time.timezone == 0 # Typically "+0000" assert poheader.tzstring() == time.strftime("%z")
def _update_header(self): """Make sure that headers are complete and update with current time (if applicable).""" # This method comes from Virtaal 0.2's main_window.py:Virtaal._on_file_save(). # It makes sure that, if we are working with a PO file, that all header info is present. if isinstance(self._trans_store, poheader): name = self.controller.main_controller.get_translator_name() email = self.controller.main_controller.get_translator_email() team = self.controller.main_controller.get_translator_team() if name is None or email is None or team is None: # User cancelled raise Exception('Save cancelled.') pan_app.settings.translator["name"] = name pan_app.settings.translator["email"] = email pan_app.settings.translator["team"] = team pan_app.settings.write() header_updates = {} header_updates["PO_Revision_Date"] = time.strftime("%Y-%m-%d %H:%M") + tzstring() header_updates["X_Generator"] = pan_app.x_generator if name or email: header_updates["Last_Translator"] = u"%s <%s>" % (name, email) self._trans_store.updatecontributor(name, email) if team: header_updates["Language-Team"] = team target_lang = self.controller.main_controller.lang_controller.target_lang header_updates["Language"] = target_lang.code plural = target_lang.plural nplurals = target_lang.nplurals if plural: self._trans_store.updateheaderplural(nplurals, plural) self._trans_store.updateheader(add=True, **header_updates)
def update_unit(self, unit, request): ''' Updates backend file and unit. ''' store = self.get_store() src = unit.get_source_plurals()[0] need_save = False # Find all units with same source for pounit in store.findunits(src): # Does context match? if pounit.getcontext() == unit.context: # Is it plural? if hasattr(pounit.target, 'strings'): potarget = join_plural(pounit.target.strings) else: potarget = pounit.target # Is there any change if unit.target != potarget or unit.fuzzy != pounit.isfuzzy(): # Update fuzzy flag pounit.markfuzzy(unit.fuzzy) # Store translations if unit.is_plural(): pounit.settarget(unit.get_target_plurals()) else: pounit.settarget(unit.target) # We need to update backend need_save = True # We should have only one match break # Save backend if there was a change if need_save: author = self.get_author_name(request.usr) # Update po file header if hasattr(store, 'updateheader'): po_revision_date = datetime.now().strftime('%Y-%m-%d %H:%M') + poheader.tzstring() store.updateheader( add = True, last_translator = author, plural_forms = self.language.get_plural_form(), language = self.language.code, PO_Revision_Date = po_revision_date, x_generator = 'Weblate %s' % weblate.VERSION ) # commit possible previous changes (by other author) self.commit_pending(author) # save translation changes store.save() # commit Git repo if needed self.git_commit(author, sync = True) return need_save, pounit
def updateheader(self, user=None): had_header = False if isinstance(self.file.store, po.pofile): had_header = self.file.store.header() po_revision_date = time.strftime('%Y-%m-%d %H:%M') + poheader.tzstring() headerupdates = {'PO_Revision_Date': po_revision_date, 'X_Generator': x_generator} language = self.translation_project.language headerupdates['Language'] = language.code if language.nplurals and language.pluralequation: self.file.store.updateheaderplural(language.nplurals, language.pluralequation) if user is not None and user.is_authenticated(): headerupdates['Last_Translator'] = '%s <%s>' % (user.first_name or user.username, user.email) else: #FIXME: maybe insert settings.TITLE or domain here? headerupdates['Last_Translator'] = 'Anonymous Pootle User' self.file.store.updateheader(add=True, **headerupdates) return had_header
def _update_header(self): """Make sure that headers are complete and update with current time (if applicable).""" # This method comes from Virtaal 0.2's main_window.py:Virtaal._on_file_save(). # It makes sure that, if we are working with a PO file, that all header info is present. from translate.storage.poheader import poheader, tzstring if isinstance(self._trans_store, poheader): name = self.controller.main_controller.get_translator_name() email = self.controller.main_controller.get_translator_email() team = self.controller.main_controller.get_translator_team() if name is None or email is None or team is None: # User cancelled raise Exception('Save cancelled.') pan_app.settings.translator["name"] = name pan_app.settings.translator["email"] = email pan_app.settings.translator["team"] = team pan_app.settings.write() header_updates = {} import time header_updates["PO_Revision_Date"] = time.strftime( "%Y-%m-%d %H:%M") + tzstring() header_updates["X_Generator"] = pan_app.x_generator if name or email: header_updates["Last_Translator"] = u"%s <%s>" % (name, email) self._trans_store.updatecontributor(name, email) if team: header_updates["Language-Team"] = team target_lang = self.controller.main_controller.lang_controller.target_lang header_updates["Language"] = target_lang.code project_code = self.controller.main_controller.checks_controller.code if project_code: header_updates["X-Project-Style"] = project_code self._trans_store.updateheader(add=True, **header_updates) plural = target_lang.plural nplurals = target_lang.nplurals if plural: self._trans_store.updateheaderplural(nplurals, plural)
def update_store_header(self, profile=None): language = self.translation_project.language source_language = self.translation_project.project.source_language self.file.store.settargetlanguage(language.code) self.file.store.setsourcelanguage(source_language.code) if isinstance(self.file.store, poheader.poheader): mtime = self.get_mtime() if mtime is None: mtime = datetime.datetime.now() if profile is None: try: submit = self.translation_project.submission_set.filter(creation_time=mtime).latest() if submit.submitter.user.username != 'nobody': profile = submit.submitter except ObjectDoesNotExist: try: lastsubmit = self.translation_project.submission_set.latest() if lastsubmit.submitter.user.username != 'nobody': profile = lastsubmit.submitter mtime = min(lastsubmit.creation_time, mtime) except ObjectDoesNotExist: pass po_revision_date = mtime.strftime('%Y-%m-%d %H:%M') + poheader.tzstring() headerupdates = {'PO_Revision_Date': po_revision_date, 'X_Generator': x_generator, 'X_POOTLE_MTIME': '%s.%d' % (mtime.strftime('%s'), mtime.microsecond), } if profile and profile.user.is_authenticated(): headerupdates['Last_Translator'] = '%s <%s>' % (profile.user.first_name or profile.user.username, profile.user.email) else: #FIXME: maybe insert settings.TITLE or domain here? headerupdates['Last_Translator'] = 'Anonymous Pootle User' self.file.store.updateheader(add=True, **headerupdates) if language.nplurals and language.pluralequation: self.file.store.updateheaderplural(language.nplurals, language.pluralequation)
def get_po_revision_date(self, mtime): return ("%s%s" % (mtime.strftime('%Y-%m-%d %H:%M'), poheader.tzstring()))
def update_unit(self, unit, request, user=None): ''' Updates backend file and unit. ''' if user is None: user = request.user # Save with lock acquired with self.subproject.repository_lock: src = unit.get_source_plurals()[0] add = False pounit, add = self.store.find_unit(unit.context, src) # Bail out if we have not found anything if pounit is None or pounit.is_obsolete(): return False, None # Check for changes if (not add and unit.target == pounit.get_target() and unit.fuzzy == pounit.is_fuzzy()): return False, pounit # Store translations if unit.is_plural(): pounit.set_target(unit.get_target_plurals()) else: pounit.set_target(unit.target) # Update fuzzy flag pounit.mark_fuzzy(unit.fuzzy) # Optionally add unit to translation file if add: self.store.add_unit(pounit) # We need to update backend now author = self.get_author_name(user) # Update po file header po_revision_date = ( datetime.now().strftime('%Y-%m-%d %H:%M') + poheader.tzstring() ) # Prepare headers to update headers = { 'add': True, 'last_translator': author, 'plural_forms': self.language.get_plural_form(), 'language': self.language_code, 'PO_Revision_Date': po_revision_date, } # Optionally store language team with link to website if self.subproject.project.set_translation_team: headers['language_team'] = '%s <%s>' % ( self.language.name, get_site_url(self.get_absolute_url()), ) # Optionally store email for reporting bugs in source report_source_bugs = self.subproject.report_source_bugs if report_source_bugs != '': headers['report_msgid_bugs_to'] = report_source_bugs # Update genric headers self.store.update_header( **headers ) # commit possible previous changes (by other author) self.commit_pending(request, author) # save translation changes self.store.save() # commit VCS repo if needed self.git_commit(request, author, timezone.now(), sync=True) return True, pounit
def update_unit(self, unit, request, user=None): ''' Updates backend file and unit. ''' if user is None: user = request.user # Save with lock acquired with self.subproject.git_lock: src = unit.get_source_plurals()[0] add = False pounit, add = self.store.find_unit(unit.context, src) # Bail out if we have not found anything if pounit is None: return False, None # Check for changes if (unit.target == pounit.get_target() and unit.fuzzy == pounit.is_fuzzy()): return False, pounit # Store translations if unit.is_plural(): pounit.set_target(unit.get_target_plurals()) else: pounit.set_target(unit.target) # Update fuzzy flag pounit.mark_fuzzy(unit.fuzzy) # Optionally add unit to translation file if add: self.store.add_unit(pounit) # We need to update backend now author = self.get_author_name(user) # Update po file header po_revision_date = (datetime.now().strftime('%Y-%m-%d %H:%M') + poheader.tzstring()) # Prepare headers to update headers = { 'add': True, 'last_translator': author, 'plural_forms': self.language.get_plural_form(), 'language': self.language_code, 'PO_Revision_Date': po_revision_date, 'x_generator': 'Weblate %s' % weblate.VERSION } # Optionally store language team with link to website if self.subproject.project.set_translation_team: headers['language_team'] = '%s <%s>' % ( self.language.name, get_site_url(self.get_absolute_url()), ) # Optionally store email for reporting bugs in source report_source_bugs = self.subproject.report_source_bugs if report_source_bugs != '': headers['report_msgid_bugs_to'] = report_source_bugs # Update genric headers self.store.update_header(**headers) # commit possible previous changes (by other author) self.commit_pending(request, author) # save translation changes self.store.save() # commit Git repo if needed self.git_commit(request, author, timezone.now(), sync=True) return True, pounit
def get_po_revision_date(self, mtime): return ( "%s%s" % (mtime.strftime('%Y-%m-%d %H:%M'), poheader.tzstring()))