Example #1
0
    def test_output_file_description_is_same(self):
        """Test that multiline descriptions are unaffected"""
        out_str = remove_unwanted_keys(self.data)

        in_yaml = yaml.load(self.data)
        out_yaml = yaml.load(out_str)

        self.assertEqual(in_yaml['Test term 1']['description'],
                         out_yaml['Test term 1']['description'])
Example #2
0
    def test_english_file_validity(self):
        filename = 'en.yaml'

        with open(os.path.join(TRANSLATIONS_DIR, filename)) as f:
            f_yaml = yaml.load(f)
            for term in f_yaml:
                # check for invalid keys
                for key in f_yaml[term]:
                    if term == '_meta':
                        self.assertIn(key,
                                      self.valid_meta_keys,
                                      msg='\nInvalid meta key in %s: "%s"' %
                                      (filename, key))
                        continue

                    self.assertIn(
                        key,
                        self.valid_term_keys_en,
                        msg='\nInvalid key in %s: "%s" in term "%s"' %
                        (filename, key, term))

                # check for key types
                for key in f_yaml[term]:
                    value = f_yaml[term][key]

                    self.assertTrue(isinstance(value, self.get_key_types(key)),
                        msg='\nInvalid key type in "%s" in term "%s":\
                            \n"%s" must be of type %s' \
                            % (filename, term, key, ' or '.join(self.get_type_names_for_key(key)))
                    )
Example #3
0
def update_translation_files(f_name, update_all):
    if not f_name and not update_all:
        return

    if f_name and not os.path.exists(os.path.join(TRANSLATIONS_DIR, f_name)):
        print("\nERROR: File '%s' doesn't exist" % f_name)
        return

    for filename in os.listdir(TRANSLATIONS_DIR):
        if f_name and filename != f_name:
            continue

        if filename == 'en.yaml':
            continue

        en_fp = os.path.join(TRANSLATIONS_DIR, 'en.yaml')
        out_fp = os.path.join(TRANSLATIONS_DIR, filename)

        with open(out_fp, 'r') as out_file:
            old_yaml = yaml.load(out_file)

        with open(en_fp, 'r') as en_file:
            out_str = remove_unwanted_keys(en_file.read())
            new_yaml = yaml.load(out_str)

        for term in old_yaml:
            for key in old_yaml[term]:
                try:
                    if (key == 'description'):
                        continue
                    new_yaml[term][key] = old_yaml[term][key]
                except KeyError:
                    pass

        out_yaml = io.StringIO()
        yaml.dump(new_yaml, stream=out_yaml)

        # replace extra blank lines
        out_str = re.sub(r'^\s*$', '', out_yaml.getvalue(), flags=re.MULTILINE)

        with open(out_fp, 'w') as out_file:
            out_file.write(out_str)

    if (f_name):
        print("\nUpdated file %s" % f_name)
    else:
        print("\nUpdated files.")
Example #4
0
    def test_output_files_only_have_description_and_variations_keys(self):
        out_str = remove_unwanted_keys(self.data)

        out_yaml = yaml.load(out_str)

        for term in out_yaml:
            for key in out_yaml[term]:
                self.assertIn(key, ['value', 'description'])
Example #5
0
def convert(destination, no_overwrite, pretty):
    for filename in os.listdir(TRANSLATIONS_DIR):
        in_fp = os.path.join(TRANSLATIONS_DIR, filename)
        out_fp = os.path.join(destination, filename.replace('.yaml', '.json'))

        if os.path.exists(out_fp) and no_overwrite:
            continue

        with open(in_fp, 'r') as in_f, open(out_fp, 'w') as out_f:
            in_yaml = yaml.load(in_f)
            out_f.write(
                json.dumps(in_yaml,
                           indent=2 if pretty else None,
                           separators=(', ', ': ') if pretty else (',', ':')))

    print("\nDone.")