예제 #1
0
def merge_translation_file(merge: List[str], translation_dict: TranslationDict,
                           outfile: List[str], add: List[str],
                           ignore: Set[str], carry: bool, no_diverse: bool):
    """Merge in translations to designated ODK files.

    Args:
        merge: The files to merge into
        translation_dict: The object with translation information
        outfile: Where to write the merged files. Should be the same length as
            `merge`.
        add: Languages to add
        ignore: Languages to ignore when merging
        carry: If true, carry text from the source language to the translations
        no_diverse: If true, do not insert a translation that has various
            choices
    """
    for merge_source, merge_destination in zip(merge, outfile):
        xlsform = Xlsform(merge_source)
        xlsform.add_languages(add)
        xlsform.merge_translations(translation_dict,
                                   ignore,
                                   carry=carry,
                                   no_diverse=no_diverse)
        xlsform.write_out(merge_destination)
        print('Merged translations into file: "{}"'.format(merge_destination))
예제 #2
0
def compute_prepend_numbers(inpath, col, outpath, rm_on_empty=False):
    """Compute numbers based on mini-language and prepend to all labels.

    This program highlights cells in the following two specific cases:

    (1) The numbering column says there should be a number in the label, but
        there is no number found in the original label. In this case, the
        number is add to the label.
    (2) The numbering column does not produce a number, but the original label
        has a number. In this case, the number is removed.

    Adding a number means to join the number, the string '. ', and the text of
    the cell.

    Args:
        inpath (str): The path where to find the source file.
        col (str): The name of the column where to find numbering.
        outpath (str): The path where to write the new xlsxfile.
        rm_on_empty (bool): Remove numbers that exist when numbering column is
            blank.

    """
    xlsform = Xlsform(inpath)
    survey = xlsform['survey']
    context = NumberingContext()
    for cell in survey.column(col):
        context.next(str(cell))
    for i, header in enumerate(survey.column_headers()):
        if header.startswith('label') or header.startswith('ppp_label'):
            header_skipped = False
            for num, cell in zip(context.string_iter(), survey.column(i)):
                if not header_skipped:
                    header_skipped = True
                    continue
                if num:
                    old_text = str(cell)
                    cell_num, the_rest = utils.td_split_text(old_text)
                    new_text = '. '.join((num, the_rest))
                    cell.value = new_text
                    if not cell_num:
                        # Highlight yellow for adding a number
                        cell.set_highlight()
                    elif new_text != old_text:
                        # Highlight orange for changing a number
                        cell.set_highlight('HL_ORANGE')
                elif cell and rm_on_empty:
                    cell_num, the_rest = utils.td_split_text(str(cell))
                    if cell_num:
                        cell.value = the_rest
                        cell.set_highlight()
    xlsform.write_out(outpath)