Example #1
0
def do_file(file_name, lang_codes):
    if verbose:
        sys.stderr.write('%s...\n' % file_name)
    split_file_name = file_name.split('/')
    d1, d2 = split_file_name[0:2]
    if d1 in lang_codes:
        check_lang = d1
        lang_dir_index = 0
    elif d2 in lang_codes:
        check_lang = d2
        lang_dir_index = 1
    else:
        check_lang = lang
    if check_lang == C:
        raise Exception('cannot determine language for ' + file_name)
    else:
        if os.path.splitext(file_name)[1] == '.texidoc':
            original = file_name.replace(os.path.join(check_lang, 'texidocs'),
                                         'snippets', 1)
            original = original.replace('.texidoc', '.ly', 1)
        else:
            original = dir_lang(file_name, 'en', lang_dir_index)
        translated_contents = open(file_name, encoding='utf8').read()

        # experimental feature
        if not touch_committishes in (current_revision, 'HEAD'):
            (changes_in_original, error) = \
                buildlib.check_translated_doc(original,
                                              file_name,
                                              translated_contents,
                                              upper_revision=touch_committishes)
            if not error and not changes_in_original in ('', '\n'):
                translated_contents = \
                    buildlib.revision_re.sub('GIT committish: ' + current_revision,
                                             translated_contents, 1)
                f = open(file_name, 'w',
                         encoding='utf8').write(translated_contents)
                return

    (diff_string, error) \
        = buildlib.check_translated_doc(original,
                                        file_name,
                                        translated_contents,
                                        color=use_colors and not update_mode)

    if error:
        sys.stderr.write('warning: %s: %s' % (file_name, error))

    if update_mode:
        if error or len(diff_string) >= os.path.getsize(original):
            buildlib.read_pipe(text_editor + ' ' + file_name + ' ' + original)
        elif diff_string:
            diff_file = original + '.diff'
            f = open(diff_file, 'w', encoding='utf8')
            f.write(diff_string)
            f.close()
            buildlib.read_pipe(text_editor + ' ' + file_name + ' ' + diff_file)
            os.remove(diff_file)
    else:
        sys.stdout.write(diff_string)
def do_file (file_name, lang_codes):
    if verbose:
        sys.stderr.write ('%s...\n' % file_name)
    split_file_name = file_name.split ('/')
    d1, d2 = split_file_name[0:2]
    if d1 in lang_codes:
        check_lang = d1
        lang_dir_index = 0
    elif d2 in lang_codes:
        check_lang = d2
        lang_dir_index = 1
    else:
        check_lang = lang
    if check_lang == C:
        raise Exception ('cannot determine language for ' + file_name)
    else:
        if os.path.splitext (file_name)[1] == '.texidoc':
            original = file_name.replace (os.path.join (check_lang, 'texidocs'), 'snippets', 1)
            original = original.replace ('.texidoc', '.ly', 1)
        else:
            original = dir_lang (file_name, '', lang_dir_index)
        translated_contents = open (file_name).read ()

        ## experimental feature
        if not touch_committishes in (current_revision, 'HEAD'):
            (changes_in_original, error) = \
                buildlib.check_translated_doc (original,
                                               file_name,
                                               translated_contents,
                                               upper_revision=touch_committishes)
            if not error and not changes_in_original in ('', '\n'):
                translated_contents = \
                    buildlib.revision_re.sub ('GIT committish: ' + current_revision,
                                              translated_contents, 1)
                f = open (file_name, 'w').write (translated_contents)
                return
    (diff_string, error) \
        = buildlib.check_translated_doc (original,
                                         file_name,
                                         translated_contents,
                                         color=use_colors and not update_mode)

    if error:
        sys.stderr.write ('warning: %s: %s' % (file_name, error))

    if update_mode:
        if error or len (diff_string) >= os.path.getsize (original):
            buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + original)
        elif diff_string:
            diff_file = original + '.diff'
            f = open (diff_file, 'w')
            f.write (diff_string)
            f.close ()
            buildlib.read_pipe (text_editor + ' ' + file_name + ' ' + diff_file)
            os.remove (diff_file)
    else:
        sys.stdout.write (diff_string)
Example #3
0
    def __init__(self, filename, masterdocument, parent_translation=None):
        TelyDocument.__init__(self, filename)
        self.masterdocument = masterdocument
        if not hasattr(self, 'language'):
            self.language = ''
        if not self.language and parent_translation:
            self.language = parent_translation.__dict__.get('language', '')
        if self.language == 'en':
            print(filename + ': language en specified: set @documentlanguage',
                  self.filename[:2])
            self.language = ''
        if not self.language and filename[2] == '/':
            print(filename + ': no language specified: add @documentlanguage',
                  self.filename[:2])
            self.language = filename[:2]
        if self.language:
            self.translation = translation[self.language]
        else:
            self.translation = lambda x: x
        self.title = self.translation(self.title)

        # record authoring information
        self.translators = ['']
        if parent_translation:
            self.translators = parent_translation.__dict__.get(
                'translators', [''])
        m = translators_re.findall(self.contents)
        if m:
            self.translators = [
                n.strip()
                for n in reduce(operator.add, [n.split(',') for n in m])
            ]
        if self.language != self.filename[:2]:
            print('Barf:', self.filename)
            barf
        if (not isinstance(self, UntranslatedTelyDocument)
                and (not self.translators or not self.translators[0])
                and not 'macros.itexi' in self.filename):
            error(self.filename + ''': error: no translator name found
    please specify one ore more lines in the master file
    @c Translator: FirstName LastName[, FirstName LastName]..''')
        self.checkers = []
        m = checkers_re.findall(self.contents)
        if m:
            self.checkers = [
                n.strip()
                for n in reduce(operator.add, [n.split(',') for n in m])
            ]
        if not self.checkers and isinstance(parent_translation,
                                            TranslatedTelyDocument):
            self.checkers = parent_translation.checkers

        # check whether translation is pre- or post-GDP
        m = status_re.search(self.contents)
        if m:
            self.post_gdp = bool(post_gdp_re.search(m.group(1)))
        else:
            self.post_gdp = False

        # record which parts (nodes) of the file are actually translated
        self.partially_translated = not skeleton_str in self.contents
        nodes = node_re.split(self.contents)
        self.translated_nodes = [not untranslated_node_str in n for n in nodes]

        # calculate translation percentage
        master_total_word_count = sum(masterdocument.word_count)
        translation_word_count = \
            sum([masterdocument.word_count[k] * self.translated_nodes[k]
                 for k in range(min(len(masterdocument.word_count),
                                    len(self.translated_nodes)))])
        self.translation_percentage = \
            100 * translation_word_count / master_total_word_count

        # calculate how much the file is outdated
        (diff_string, git_error) = \
            buildlib.check_translated_doc(
                masterdocument.filename, self.filename, self.contents)
        if git_error:
            sys.stderr.write('warning: %s: %s' % (self.filename, git_error))
            self.uptodate_percentage = None
        else:
            diff = diff_string.splitlines()
            insertions = sum([
                len(l) - 1 for l in diff
                if l.startswith('+') and not l.startswith('+++')
            ])
            deletions = sum([
                len(l) - 1 for l in diff
                if l.startswith('-') and not l.startswith('---')
            ])
            outdateness_percentage = 50.0 * (deletions + insertions) / \
                (masterdocument.size + 0.5 * (deletions - insertions))
            self.uptodate_percentage = 100 - int(outdateness_percentage)
            if self.uptodate_percentage > 100:
                alternative = 50
                progress("%s: strange uptodateness percentage %d %%, \
setting to %d %%" % (self.filename, self.uptodate_percentage, alternative))
                self.uptodate_percentage = alternative
            elif self.uptodate_percentage < 1:
                alternative = 1
                progress("%s: strange uptodateness percentage %d %%, \
setting to %d %%" % (self.filename, self.uptodate_percentage, alternative))
                self.uptodate_percentage = alternative
    def __init__ (self, filename, masterdocument, parent_translation=None):
        TelyDocument.__init__ (self, filename)
        self.masterdocument = masterdocument
        if not hasattr (self, 'language'):
            self.language = ''
        if not self.language and parent_translation:
            self.language = parent_translation.__dict__.get ('language', '')
        if self.language == 'en':
            print filename + ': language en specified: set @documentlanguage', self.filename[:2]
            self.language = ''
        if not self.language and filename[2] == '/':
            print filename + ': no language specified: add @documentlanguage', self.filename[:2]
            self.language = filename[:2]
        if self.language:
            self.translation = translation[self.language]
        else:
            self.translation = lambda x: x
        self.title = self.translation (self.title)

        ## record authoring information
        self.translators = ['']
        if parent_translation:
            self.translators = parent_translation.__dict__.get ('translators', [''])
        m = translators_re.findall (self.contents)
        if m:
            self.translators = [n.strip () for n in
                                reduce (operator.add, [n.split (',') for n in m])]
        if self.language != self.filename[:2]:
            print 'Barf:', self.filename
            barf
        if (not isinstance (self, UntranslatedTelyDocument)
            and (not self.translators or not self.translators[0])
            and not 'macros.itexi' in self.filename):
            error (self.filename + ''': error: no translator name found
    please specify one ore more lines in the master file
    @c Translator: FirstName LastName[, FirstName LastName]..''')
        self.checkers = []
        m = checkers_re.findall (self.contents)
        if m:
            self.checkers = [n.strip () for n in
                             reduce (operator.add, [n.split (',') for n in m])]
        if not self.checkers and isinstance (parent_translation, TranslatedTelyDocument):
            self.checkers = parent_translation.checkers

        ## check whether translation is pre- or post-GDP
        m = status_re.search (self.contents)
        if m:
            self.post_gdp = bool (post_gdp_re.search (m.group (1)))
        else:
            self.post_gdp = False

        ## record which parts (nodes) of the file are actually translated
        self.partially_translated = not skeleton_str in self.contents
        nodes = node_re.split (self.contents)
        self.translated_nodes = [not untranslated_node_str in n for n in nodes]

        ## calculate translation percentage
        master_total_word_count = sum (masterdocument.word_count)
        translation_word_count = \
            sum ([masterdocument.word_count[k] * self.translated_nodes[k]
                  for k in range (min (len (masterdocument.word_count),
                                       len (self.translated_nodes)))])
        self.translation_percentage = \
            100 * translation_word_count / master_total_word_count

        ## calculate how much the file is outdated
        (diff_string, git_error) = \
            buildlib.check_translated_doc (masterdocument.filename, self.filename, self.contents)
        if git_error:
            sys.stderr.write ('warning: %s: %s' % (self.filename, git_error))
            self.uptodate_percentage = None
        else:
            diff = diff_string.splitlines ()
            insertions = sum ([len (l) - 1 for l in diff
                               if l.startswith ('+')
                               and not l.startswith ('+++')])
            deletions = sum ([len (l) - 1 for l in diff
                              if l.startswith ('-')
                              and not l.startswith ('---')])
            outdateness_percentage = 50.0 * (deletions + insertions) / \
                (masterdocument.size + 0.5 * (deletions - insertions))
            self.uptodate_percentage = 100 - int (outdateness_percentage)
            if self.uptodate_percentage > 100:
                alternative = 50
                progress ("%s: strange uptodateness percentage %d %%, \
setting to %d %%" % (self.filename, self.uptodate_percentage, alternative))
                self.uptodate_percentage = alternative
            elif self.uptodate_percentage < 1:
                alternative = 1
                progress ("%s: strange uptodateness percentage %d %%, \
setting to %d %%" % (self.filename, self.uptodate_percentage, alternative))
                self.uptodate_percentage = alternative
    def __init__ (self, filename, masterdocument, parent_translation=None):
        TelyDocument.__init__ (self, filename)

        self.masterdocument = masterdocument
        if not hasattr (self, 'language') \
                and hasattr (parent_translation, 'language'):
            self.language = parent_translation.language
        if hasattr (self, 'language'):
            self.translation = translation[self.language]
        else:
            self.translation = lambda x: x
        self.title = self.translation (self.title)

        ## record authoring information
        m = translators_re.search (self.contents)
        if m:
            self.translators = [n.strip () for n in m.group (1).split (',')]
        else:
            try:
                self.translators = parent_translation.translators
            except:
                if 'macros.itexi' in self.filename:
                    self.translators = ['']
                else:
                    error ('%s: no translator name found, \nplease \
specify at least one in the master file as a line containing\n\
@c Translators: FirstName1 LastName1, FirstName2 LastName2' % self.filename)
        m = checkers_re.search (self.contents)
        if m:
            self.checkers = [n.strip () for n in m.group (1).split (',')]
        elif isinstance (parent_translation, TranslatedTelyDocument):
            self.checkers = parent_translation.checkers
        else:
            self.checkers = []

        ## check whether translation is pre- or post-GDP
        m = status_re.search (self.contents)
        if m:
            self.post_gdp = bool (post_gdp_re.search (m.group (1)))
        else:
            self.post_gdp = False

        ## record which parts (nodes) of the file are actually translated
        self.partially_translated = not skeleton_str in self.contents
        nodes = node_re.split (self.contents)
        self.translated_nodes = [not untranslated_node_str in n for n in nodes]

        ## calculate translation percentage
        master_total_word_count = sum (masterdocument.word_count)
        translation_word_count = \
            sum ([masterdocument.word_count[k] * self.translated_nodes[k]
                  for k in range (min (len (masterdocument.word_count),
                                       len (self.translated_nodes)))])
        self.translation_percentage = \
            100 * translation_word_count / master_total_word_count

        ## calculate how much the file is outdated
        (diff_string, git_error) = \
            buildlib.check_translated_doc (masterdocument.filename, self.filename, self.contents)
        if git_error:
            sys.stderr.write ('warning: %s: %s' % (self.filename, git_error))
            self.uptodate_percentage = None
        else:
            diff = diff_string.splitlines ()
            insertions = sum ([len (l) - 1 for l in diff
                               if l.startswith ('+')
                               and not l.startswith ('+++')])
            deletions = sum ([len (l) - 1 for l in diff
                              if l.startswith ('-')
                              and not l.startswith ('---')])
            outdateness_percentage = 50.0 * (deletions + insertions) / \
                (masterdocument.size + 0.5 * (deletions - insertions))
            self.uptodate_percentage = 100 - int (outdateness_percentage)
            if self.uptodate_percentage > 100:
                alternative = 50
                progress ("%s: strange uptodateness percentage %d %%, \
setting to %d %%" % (self.filename, self.uptodate_percentage, alternative))
                self.uptodate_percentage = alternative
            elif self.uptodate_percentage < 1:
                alternative = 1
                progress ("%s: strange uptodateness percentage %d %%, \
setting to %d %%" % (self.filename, self.uptodate_percentage, alternative))
                self.uptodate_percentage = alternative