Esempio n. 1
0
def check_files_match(src, response):
    from pootle_fs.models import StoreFS

    assert all(
        os.path.exists(os.path.join(src, p.fs_path.strip("/")))
        for p in response["pushed_to_fs"])
    assert not any(
        os.path.exists(os.path.join(src, p.fs_path.strip("/")))
        for p in response["pruned_from_fs"])
    # Any Store/files that have been synced should now match
    synced = (response["pulled_to_pootle"] + response["pushed_to_fs"] +
              response["merged_from_fs"] + response["merged_from_pootle"] +
              response["merged_from_fs"])
    for action in synced:
        store_fs = StoreFS.objects.get(pootle_path=action.pootle_path)
        file_path = os.path.join(src, store_fs.path.strip("/"))
        store_f = io.BytesIO(store_fs.store.serialize())
        with open(file_path) as src_file:
            file_store = getclass(src_file)(src_file.read())
            serialized = getclass(store_f)(store_f.read())
            assert ([
                (x.source, x.target) for x in file_store.units if x.source
            ] == [(x.source, x.target) for x in serialized.units if x.source])

    for action in response["removed"]:
        store = action.original_status.store_fs.store
        if store:
            assert store.obsolete is True
        assert action.original_status.store_fs.file.exists is False
        assert action.original_status.store_fs.pk is None
        assert not os.path.exists(os.path.join(src, action.fs_path.strip("/")))
Esempio n. 2
0
def test_store_po_serializer(test_fs, store_po):

    with test_fs.open("data/po/complex.po") as test_file:
        test_string = test_file.read()
        ttk_po = getclass(test_file)(test_string)

    store_po.update(store_po.deserialize(test_string))
    store_io = io.BytesIO(store_po.serialize())
    store_ttk = getclass(store_io)(store_io.read())
    assert len(store_ttk.units) == len(ttk_po.units)
Esempio n. 3
0
def test_update_ts_plurals(store_po, test_fs):
    with test_fs.open(['data', 'ts', 'add_plurals.ts']) as f:
        file_store = getclass(f)(f.read())
    store_po.update(file_store)
    assert store_po.units[0].hasplural()

    with test_fs.open(['data', 'ts', 'update_plurals.ts']) as f:
        file_store = getclass(f)(f.read())
    store_po.update(file_store)
    assert store_po.units[0].hasplural()
Esempio n. 4
0
def test_update_ts_plurals(store_po, test_fs):
    with test_fs.open(['data', 'ts', 'add_plurals.ts']) as f:
        file_store = getclass(f)(f.read())
    store_po.update(file_store)
    assert store_po.units[0].hasplural()

    with test_fs.open(['data', 'ts', 'update_plurals.ts']) as f:
        file_store = getclass(f)(f.read())
    store_po.update(file_store)
    assert store_po.units[0].hasplural()
Esempio n. 5
0
def test_store_po_serializer(test_fs, store_po):

    with test_fs.open("data/po/complex.po") as test_file:
        test_string = test_file.read()
        ttk_po = getclass(test_file)(test_string)

    store_po.update(store_po.deserialize(test_string))
    store_io = io.BytesIO(store_po.serialize())
    store_ttk = getclass(store_io)(store_io.read())
    assert len(store_ttk.units) == len(ttk_po.units)
Esempio n. 6
0
def test_store_po_serializer(test_fs, store_po):

    with test_fs.open("data/po/complex.po", "rb") as test_file:
        file_bytes = test_file.read()
        ttk_po = getclass(test_file)(file_bytes)

        store_po.update(store_po.deserialize(file_bytes))
        store_bytes = store_po.serialize()
        store_io = io.BytesIO(store_bytes)
        store_ttk = getclass(store_io)(store_bytes)
        assert len(store_ttk.units) == len(ttk_po.units)
Esempio n. 7
0
 def load_file(self, filename):
     cls = factory.getclass(filename)
     self.store = cls.parsefile(filename)
     self.index = int(self.config.phrase_index)
     self.config.register(lambda: self.index, "phrase_index")
     self.scroll(None, 0)
     self.config.file = filename
Esempio n. 8
0
    def setup_terminology(self):
        import pytest_pootle
        from pytest_pootle.factories import (
            ProjectDBFactory, StoreDBFactory, TranslationProjectFactory)
        from pootle_language.models import Language

        source_language = Language.objects.get(code="en")
        terminology = ProjectDBFactory(code="terminology",
                                       checkstyle="terminology",
                                       fullname="Terminology",
                                       source_language=source_language)
        term_file = os.path.join(
            os.path.dirname(pytest_pootle.__file__),
            *("data", "po", "terminology.po"))
        with open(term_file) as f:
            term_ttk = getclass(f)(f.read())
        for language in Language.objects.all():
            tp = TranslationProjectFactory(
                project=terminology, language=language)
            if language.code not in ["language0", "language1"]:
                continue
            store = StoreDBFactory(
                parent=tp.directory,
                translation_project=tp,
                name="terminology.po")
            store.update(term_ttk)
Esempio n. 9
0
def test_store_get_file_class():
    store = Store.objects.filter(
        translation_project__project__code="project0",
        translation_project__language__code="language0").first()

    # this matches because po is recognised by ttk
    assert store.syncer.file_class == getclass(store)
Esempio n. 10
0
def import_file(file, user=None):
    f = getclass(file)(file.read())
    if not hasattr(f, "parseheader"):
        raise UnsupportedFiletypeError(_("Unsupported filetype '%s', only PO "
                                         "files are supported at this time\n")
                                       % file.name)
    header = f.parseheader()
    pootle_path = header.get("X-Pootle-Path")
    if not pootle_path:
        raise MissingPootlePathError(_("File '%s' missing X-Pootle-Path "
                                       "header\n") % file.name)

    rev = header.get("X-Pootle-Revision")
    if not rev or not rev.isdigit():
        raise MissingPootleRevError(_("File '%s' missing or invalid "
                                      "X-Pootle-Revision header\n")
                                    % file.name)
    rev = int(rev)

    try:
        store, created = Store.objects.get_or_create(pootle_path=pootle_path)
    except Exception as e:
        raise FileImportError(_("Could not create '%s'. Missing "
                                "Project/Language? (%s)") % (file.name, e))

    try:
        store.update(overwrite=True, store=f, user=user,
                     submission_type=SubmissionTypes.UPLOAD)
    except Exception as e:
        # This should not happen!
        logger.error("Error importing file: %s" % str(e))
        raise FileImportError(_("There was an error uploading your file"))
Esempio n. 11
0
 def _getclass(self, obj):
     try:
         return getclass(obj)
     except ValueError:
         raise ValueError(
             "Unable to find conversion class for Store '%s'"
             % self.store.name)
Esempio n. 12
0
def import_file(file):
    f = getclass(file)(file.read())
    header = f.parseheader()
    pootle_path = header.get("X-Pootle-Path")
    if not pootle_path:
        raise ValueError(_("File %r missing X-Pootle-Path header\n") % (file.name))

    rev = header.get("X-Pootle-Revision")
    if not rev or not rev.isdigit():
        raise ValueError(
            _("File %r missing or invalid X-Pootle-Revision header\n") % (file.name)
        )
    rev = int(rev)

    try:
        store, created = Store.objects.get_or_create(pootle_path=pootle_path)
        if rev < store.get_max_unit_revision():
            # TODO we could potentially check at the unit level and only reject
            # units older than most recent. But that's in store.update().
            raise ValueError(
                _("File %r was rejected because its X-Pootle-Revision is too old.")
                % (file.name)
            )
    except Exception as e:
        raise ValueError(
            _("Could not create %r. Missing Project/Language? (%s)")
            % (file.name, e)
        )

    store.update(overwrite=True, store=f)
Esempio n. 13
0
 def _getclass(self, obj):
     try:
         return getclass(obj)
     except ValueError:
         raise ValueError(
             "Unable to find conversion class for Store '%s'"
             % self.store.name)
Esempio n. 14
0
def unit_syncer(tp0):
    from pootle_store.constants import TRANSLATED

    store = tp0.stores.live().first()
    unit = store.units.filter(state=TRANSLATED).first()
    ttk = getclass(store)
    return unit, ttk.UnitClass
Esempio n. 15
0
def pytest_generate_tests(metafunc):
    import pytest_pootle

    if "terminology_units" in metafunc.fixturenames:
        term_file = os.path.join(os.path.dirname(pytest_pootle.__file__), *("data", "po", "terminology.po"))
        with open(term_file) as f:
            _terms = [x.source for x in getclass(f)(f.read()).units[1:]]
        metafunc.parametrize("terminology_units", _terms)
Esempio n. 16
0
def test_update_with_non_ascii(store0, test_fs):
    store0.state = PARSED
    orig_units = store0.units.count()
    with test_fs.open(
        ["data", "po", "tutorial", "en", "tutorial_non_ascii.po"], "rb") as f:
        store = getclass(f)(f.read())
    store0.update(store)
    assert store0.units[orig_units].target == "Hèḽḽě, ŵôrḽḓ"
Esempio n. 17
0
def create_store(units=None):
    _units = []
    for src, target in units or []:
        _units.append(STRING_UNIT % {"src": src, "target": target})
    units = "\n\n".join(_units)
    string_store = STRING_STORE % {"units": units}
    io_store = io.BytesIO(string_store.encode("utf-8"))
    return getclass(io_store)(io_store.read())
Esempio n. 18
0
def test_update_with_non_ascii(store0, test_fs):
    store0.state = PARSED
    orig_units = store0.units.count()
    with test_fs.open(['data', 'po', 'tutorial', 'en',
                       'tutorial_non_ascii.po']) as f:
        store = getclass(f)(f.read())
    store0.update(store)
    assert store0.units[orig_units].target == "Hèḽḽě, ŵôrḽḓ"
Esempio n. 19
0
def test_update_from_ts(store0, test_fs):
    store0.parsed = True
    orig_units = store0.units.count()
    with test_fs.open(['data', 'ts', 'tutorial', 'en', 'tutorial.ts']) as f:
        store = getclass(f)(f.read())
    store0.update(store)
    assert (not store0.units[orig_units].hasplural())
    assert (store0.units[orig_units + 1].hasplural())
Esempio n. 20
0
def test_update_with_non_ascii(store0, test_fs):
    store0.state = PARSED
    orig_units = store0.units.count()
    with test_fs.open(['data', 'po', 'tutorial', 'en',
                       'tutorial_non_ascii.po']) as f:
        store = getclass(f)(f.read())
    store0.update(store)
    assert store0.units[orig_units].target == "Hèḽḽě, ŵôrḽḓ"
Esempio n. 21
0
def test_store_po_deserializer(test_fs, store_po):

    with test_fs.open("data/po/complex.po") as test_file:
        test_string = test_file.read()
        ttk_po = getclass(test_file)(test_string)

    store_po.update(store_po.deserialize(test_string))
    assert len(ttk_po.units) - 1 == store_po.units.count()
Esempio n. 22
0
def test_store_po_deserializer(test_fs, store_po):

    with test_fs.open("data/po/complex.po") as test_file:
        test_string = test_file.read()
        ttk_po = getclass(test_file)(test_string)

    store_po.update(store_po.deserialize(test_string))
    assert len(ttk_po.units) - 1 == store_po.units.count()
Esempio n. 23
0
def test_update_from_ts(store0, test_fs):
    store0.parsed = True
    orig_units = store0.units.count()
    with test_fs.open(['data', 'ts', 'tutorial', 'en', 'tutorial.ts']) as f:
        store = getclass(f)(f.read())
    store0.update(store)
    assert(not store0.units[orig_units].hasplural())
    assert(store0.units[orig_units + 1].hasplural())
Esempio n. 24
0
def test_update_with_non_ascii(en_tutorial_po, test_fs):
    # Parse store
    en_tutorial_po.update_from_disk()
    tp = en_tutorial_po.translation_project
    with test_fs.open(['data', 'po', tp.real_path,
                       'tutorial_non_ascii.po']) as f:
        store = getclass(f)(f.read())
    en_tutorial_po.update(store)
    assert en_tutorial_po.units[0].target == "Hèllö, wôrld"
Esempio n. 25
0
def pytest_generate_tests(metafunc):
    import pytest_pootle

    if 'terminology_units' in metafunc.fixturenames:
        term_file = os.path.join(os.path.dirname(pytest_pootle.__file__),
                                 *("data", "po", "terminology.po"))
        with open(term_file) as f:
            _terms = [x.source for x in getclass(f)(f.read()).units[1:]]
        metafunc.parametrize("terminology_units", _terms)
Esempio n. 26
0
def test_update_with_non_ascii(en_tutorial_po, test_fs):
    # Parse store
    en_tutorial_po.updater.update_from_disk()
    tp = en_tutorial_po.translation_project
    with test_fs.open(['data', 'po', tp.real_path,
                       'tutorial_non_ascii.po']) as f:
        store = getclass(f)(f.read())
    en_tutorial_po.update(store)
    assert en_tutorial_po.units[0].target == "Hèllö, wôrld"
Esempio n. 27
0
def parse_locale_po(lang):
    unitdict = OrderedDict()
    with open('locale/%s/xtle.po' % lang, 'rb') as f:
        # content = f.read()
        pofile = getclass(f)(f)

        for unit in pofile.units[1:]:
            unitdict[unit.getnotes().split("]")[0][1:]] = unit.target
    return unitdict
Esempio n. 28
0
def test_store_syncer(tp0):
    store = tp0.stores.live().first()
    store_tp = store.translation_project
    assert isinstance(store.syncer, PoStoreSyncer)
    assert store.syncer.file_class == getclass(store)
    assert store.syncer.translation_project == store_tp
    assert store.syncer.language == store_tp.language
    assert store.syncer.project == store_tp.project
    assert store.syncer.source_language == store_tp.project.source_language
Esempio n. 29
0
 def deserialize(self):
     if not self.file_exists:
         return
     deserialized = ""
     with open(self.file_path) as f:
         deserialized = f.read()
     if self.store_exists:
         return self.store.deserialize(deserialized)
     serial_io = io.BytesIO(deserialized)
     return getclass(serial_io)(serial_io.read())
Esempio n. 30
0
def test_update_from_ts(en_tutorial_po, test_fs):
    # Parse store
    en_tutorial_po.updater.update_from_disk()
    tp = en_tutorial_po.translation_project
    with test_fs.open(['data', 'ts', tp.real_path, 'tutorial.ts']) as f:
        store = getclass(f)(f.read())
    en_tutorial_po.update(store)

    assert (not en_tutorial_po.units[1].hasplural())
    assert (en_tutorial_po.units[2].hasplural())
Esempio n. 31
0
 def deserialize(self):
     if not self.file_exists:
         return
     deserialized = ""
     with open(self.file_path) as f:
         deserialized = f.read()
     if self.store_exists:
         return self.store.deserialize(deserialized)
     serial_io = io.BytesIO(deserialized)
     return getclass(serial_io)(serial_io.read())
Esempio n. 32
0
def test_update_from_ts(en_tutorial_po, test_fs):
    # Parse store
    en_tutorial_po.update_from_disk()
    tp = en_tutorial_po.translation_project
    with test_fs.open(['data', 'ts', tp.real_path, 'tutorial.ts']) as f:
        store = getclass(f)(f.read())
    en_tutorial_po.update(store)

    assert(not en_tutorial_po.units[1].hasplural())
    assert(en_tutorial_po.units[2].hasplural())
Esempio n. 33
0
def _update_from_upload_file(tutorial, update_file,
                             content_type="text/x-gettext-translation",
                             user=None, submission_type=None):
    with open(update_file, "r") as f:
        upload = SimpleUploadedFile(os.path.basename(update_file),
                                    f.read(),
                                    content_type)
    test_store = getclass(upload)(upload.read())
    tutorial.update(overwrite=False, only_newer=False)
    tutorial.update(overwrite=True, store=test_store,
                    user=user, submission_type=submission_type)
Esempio n. 34
0
def test_wrap_store_fs_pull_submission_type(store_fs_file_store):
    fs_file = store_fs_file_store
    fs_file.push()
    with open(fs_file.file_path, "r+") as target:
        ttk = getclass(target)(target.read())
        target.seek(0)
        ttk.units[1].target = "BAR"
        target.write(str(ttk))
        target.truncate()
    fs_file.pull()
    assert (fs_file.store.units[0].submission_set.latest().type ==
            SubmissionTypes.SYSTEM)
Esempio n. 35
0
def import_file(file, prj, lang=None):
    f = getclass(file)(file.read())
    if not hasattr(f, "parseheader"):
        raise Exception("Format is not supported")

    header = f.parseheader()
    if not lang:
        lang = header['Language']
    if not lang:
        raise Exception("Language not set")
    for unit in f.getunits():
        extract_phrase(prj,unit,lang,f.getheaderplural()[1])
Esempio n. 36
0
def moz_env(post_db_setup, _django_cursor_wrapper, ios_pootle_file):

    from django.conf import settings

    from pytest_pootle.factories import (
        ProjectDBFactory, StoreDBFactory,
        TranslationProjectFactory)

    from pootle_fs.models import ProjectFS
    from pootle_language.models import Language

    with _django_cursor_wrapper:
        ios_project = ProjectDBFactory(
            source_language=Language.objects.get(code="en"),
            code="ios",
            localfiletype="xliff")
        ios_tp = TranslationProjectFactory(
            project=ios_project,
            language=Language.objects.get(code="language0"))
        ios_store = StoreDBFactory(
            parent=ios_tp.directory,
            translation_project=ios_tp,
            name="firefox-ios.xliff")
        ios_bytes = io.BytesIO(ios_pootle_file.encode("utf8"))
        ios_store.update(
            getclass(ios_bytes)(ios_bytes.read()))

        fs_dir = tempfile.mkdtemp()
        settings.POOTLE_FS_PATH = fs_dir

        repo_path = os.path.join(fs_dir, "__moz_ios_src__")
        Repo.init(repo_path, bare=True)

        with tmp_git(repo_path) as (tmp_repo_path, tmp_repo):
            config_file = os.path.join(tmp_repo_path, ".pootle.ini")
            with open(config_file, "w") as ini:
                config = (
                    "[default]\n"
                    "serializers = ios\n"
                    "deserializers = ios\n"
                    "translation_path = <lang>/<filename>.xliff")
                ini.write(config)
            tmp_repo.index.add([".pootle.ini"])
            tmp_repo.index.commit("Add Pootle configuration")
            tmp_repo.remotes.origin.push("master:master")

        ios_fs = ProjectFS.objects.create(
            project=ios_project,
            fs_type="git",
            url=repo_path)
        ios_plugin = ios_fs.plugin
        ios_plugin.add_translations()
Esempio n. 37
0
def test_wrap_store_fs_pull_submission_type(store_fs_file_store):
    fs_file = store_fs_file_store
    fs_file.push()
    with open(fs_file.file_path, "r+") as target:
        ttk = getclass(target)(target.read())
        target.seek(0)
        ttk.units[1].target = "BAR"
        target.write(str(ttk))
        target.truncate()
    fs_file.pull()
    assert (
        fs_file.store.units[0].submission_set.latest().type
        == SubmissionTypes.SYSTEM)
Esempio n. 38
0
def _update_from_upload_file(store, update_file,
                             content_type="text/x-gettext-translation",
                             user=None, submission_type=None):
    with open(update_file, "r") as f:
        upload = SimpleUploadedFile(os.path.basename(update_file),
                                    f.read(),
                                    content_type)
    if store.state < PARSED:
        store.update(store.file.store)
    test_store = getclass(upload)(upload.read())
    store_revision = parse_pootle_revision(test_store)
    store.update(test_store, store_revision=store_revision,
                 user=user, submission_type=submission_type)
Esempio n. 39
0
def _update_from_upload_file(store, update_file,
                             content_type="text/x-gettext-translation",
                             user=None, submission_type=None):
    with open(update_file, "r") as f:
        upload = SimpleUploadedFile(os.path.basename(update_file),
                                    f.read(),
                                    content_type)
    if store.state < PARSED:
        store.update(store.file.store)
    test_store = getclass(upload)(upload.read())
    store_revision = parse_pootle_revision(test_store)
    store.update(test_store, store_revision=store_revision,
                 user=user, submission_type=submission_type)
Esempio n. 40
0
def test_unit_ts_plurals(store_po, test_fs):
    with test_fs.open(['data', 'ts', 'add_plurals.ts']) as f:
        file_store = getclass(f)(f.read())
    unit = Unit(store=store_po)
    unit_ts = file_store.units[0]
    unit.update(unit_ts)
    assert unit.hasplural()
    unit.save()
    unit = Unit.objects.get(id=unit.id)
    assert unit.hasplural()
    unit.save()
    unit = Unit.objects.get(id=unit.id)
    assert unit.hasplural()
Esempio n. 41
0
def test_unit_ts_plurals(store_po, test_fs):
    with test_fs.open(['data', 'ts', 'add_plurals.ts']) as f:
        file_store = getclass(f)(f.read())
    unit = Unit(store=store_po, index=1)
    unit_ts = file_store.units[0]
    unit.update(unit_ts)
    assert unit.hasplural()
    unit.save()
    unit = Unit.objects.get(id=unit.id)
    assert unit.hasplural()
    unit.save()
    unit = Unit.objects.get(id=unit.id)
    assert unit.hasplural()
Esempio n. 42
0
def _update_from_upload_file(tutorial,
                             update_file,
                             content_type="text/x-gettext-translation",
                             user=None,
                             submission_type=None):
    with open(update_file, "r") as f:
        upload = SimpleUploadedFile(os.path.basename(update_file), f.read(),
                                    content_type)
    test_store = getclass(upload)(upload.read())
    tutorial.update(overwrite=False, only_newer=False)
    tutorial.update(overwrite=True,
                    store=test_store,
                    user=user,
                    submission_type=submission_type)
Esempio n. 43
0
 def deserialize(self, create=False):
     if not create and not self.file_exists:
         return
     if self.file_exists:
         with open(self.file_path) as f:
             f = AttributeProxy(f)
             f.location_root = self.store_fs.project.local_fs_path
             store_file = (
                 self.store.syncer.file_class(f)
                 if self.store and self.store.syncer.file_class
                 else getclass(f)(f.read()))
         return store_file
     if self.store_exists:
         return self.store.deserialize(self.store.serialize())
Esempio n. 44
0
 def deserialize(self, create=False):
     if not create and not self.file_exists:
         return
     if self.file_exists:
         with open(self.file_path, 'rb') as f:
             f = AttributeProxy(f)
             f.location_root = self.store_fs.project.local_fs_path
             store_file = (self.store.syncer.file_class(f)
                           if self.store and self.store.syncer.file_class
                           else getclass(f)(f.read()))
         if store_file.units:
             return store_file
     if self.store_exists:
         return self.store.deserialize(self.store.serialize())
Esempio n. 45
0
def test_store_syncer(tp0):
    store = tp0.stores.live().first()
    assert isinstance(store.syncer, PoStoreSyncer)
    assert store.syncer.file_class == getclass(store)
    assert store.syncer.translation_project == store.translation_project
    assert (
        store.syncer.language
        == store.translation_project.language)
    assert (
        store.syncer.project
        == store.translation_project.project)
    assert (
        store.syncer.source_language
        == store.translation_project.project.source_language)
Esempio n. 46
0
def create_store(pootle_path=None, store_revision=None, units=None):
    _units = []
    for src, target in units or []:
        _units.append(STRING_UNIT % {"src": src, "target": target})
    units = "\n\n".join(_units)
    x_pootle_headers = ""
    if pootle_path and store_revision:
        x_pootle_headers = (STRING_POOTLE_HEADERS.strip()
                            % {"pootle_path": pootle_path,
                               "revision": store_revision})
    string_store = STRING_STORE % {"x_pootle_headers": x_pootle_headers,
                                   "units": units}
    io_store = io.BytesIO(string_store.encode())
    return getclass(io_store)(io_store.read())
Esempio n. 47
0
def create_store(pootle_path=None, store_revision=None, units=None):
    _units = []
    for src, target in units or []:
        _units.append(STRING_UNIT % {"src": src, "target": target})
    units = "\n\n".join(_units)
    x_pootle_headers = ""
    if pootle_path and store_revision:
        x_pootle_headers = (STRING_POOTLE_HEADERS.strip()
                            % {"pootle_path": pootle_path,
                               "revision": store_revision})
    string_store = STRING_STORE % {"x_pootle_headers": x_pootle_headers,
                                   "units": units}
    io_store = io.BytesIO(string_store.encode())
    return getclass(io_store)(io_store.read())
Esempio n. 48
0
def test_update_from_ts(en_tutorial_po):
    from django.conf import settings
    from translate.storage.factory import getclass

    # Parse store
    en_tutorial_po.update_from_disk()
    ts_dir = os.path.join(settings.ROOT_DIR, 'tests', 'data', 'ts')
    tp = en_tutorial_po.translation_project
    store_filepath = os.path.join(ts_dir, tp.real_path, 'tutorial.ts')
    with open(store_filepath) as storefile:
        store = getclass(storefile)(storefile.read())
    en_tutorial_po.update(store)

    assert(not en_tutorial_po.units[1].hasplural())
    assert(en_tutorial_po.units[2].hasplural())
Esempio n. 49
0
def test_update_from_ts(en_tutorial_po):
    from django.conf import settings
    from translate.storage.factory import getclass

    # Parse store
    en_tutorial_po.update_from_disk()
    ts_dir = os.path.join(settings.ROOT_DIR, 'tests', 'data', 'ts')
    tp = en_tutorial_po.translation_project
    store_filepath = os.path.join(ts_dir, tp.real_path, 'tutorial.ts')
    with open(store_filepath) as storefile:
        store = getclass(storefile)(storefile.read())
    en_tutorial_po.update(store)

    assert (not en_tutorial_po.units[1].hasplural())
    assert (en_tutorial_po.units[2].hasplural())
Esempio n. 50
0
def test_update_from_ts(en_tutorial_po):
    from translate.storage.factory import getclass

    # Parse store
    en_tutorial_po.update_from_disk()
    ts_dir = os.path.join(os.path.dirname(pytest_pootle.__file__), 'data',
                          'ts')
    tp = en_tutorial_po.translation_project
    store_filepath = os.path.join(ts_dir, tp.real_path, 'tutorial.ts')
    with open(store_filepath) as storefile:
        store = getclass(storefile)(storefile.read())
    en_tutorial_po.update(store)

    assert (not en_tutorial_po.units[1].hasplural())
    assert (en_tutorial_po.units[2].hasplural())
Esempio n. 51
0
def test_update_from_ts(en_tutorial_po):
    from translate.storage.factory import getclass

    # Parse store
    en_tutorial_po.update_from_disk()
    ts_dir = os.path.join(
        os.path.dirname(pytest_pootle.__file__),
        'data', 'ts')
    tp = en_tutorial_po.translation_project
    store_filepath = os.path.join(ts_dir, tp.real_path, 'tutorial.ts')
    with open(store_filepath) as storefile:
        store = getclass(storefile)(storefile.read())
    en_tutorial_po.update(store)

    assert(not en_tutorial_po.units[1].hasplural())
    assert(en_tutorial_po.units[2].hasplural())
Esempio n. 52
0
def moz_env(post_db_setup, _django_cursor_wrapper, ios_pootle_file):

    from django.conf import settings

    from pytest_pootle.factories import (ProjectDBFactory, StoreDBFactory,
                                         TranslationProjectFactory)

    from pootle_fs.models import ProjectFS
    from pootle_language.models import Language

    with _django_cursor_wrapper:
        ios_project = ProjectDBFactory(
            source_language=Language.objects.get(code="en"),
            code="ios",
            localfiletype="xliff")
        ios_tp = TranslationProjectFactory(
            project=ios_project,
            language=Language.objects.get(code="language0"))
        ios_store = StoreDBFactory(parent=ios_tp.directory,
                                   translation_project=ios_tp,
                                   name="firefox-ios.xliff")
        ios_bytes = io.BytesIO(ios_pootle_file.encode("utf8"))
        ios_store.update(getclass(ios_bytes)(ios_bytes.read()))

        fs_dir = tempfile.mkdtemp()
        settings.POOTLE_FS_PATH = fs_dir

        repo_path = os.path.join(fs_dir, "__moz_ios_src__")
        Repo.init(repo_path, bare=True)

        with tmp_git(repo_path) as (tmp_repo_path, tmp_repo):
            config_file = os.path.join(tmp_repo_path, ".pootle.ini")
            with open(config_file, "w") as ini:
                config = ("[default]\n"
                          "serializers = ios\n"
                          "deserializers = ios\n"
                          "translation_path = <lang>/<filename>.xliff")
                ini.write(config)
            tmp_repo.index.add([".pootle.ini"])
            tmp_repo.index.commit("Add Pootle configuration")
            tmp_repo.remotes.origin.push("master:master")

        ios_fs = ProjectFS.objects.create(project=ios_project,
                                          fs_type="git",
                                          url=repo_path)
        ios_plugin = ios_fs.plugin
        ios_plugin.add_translations()
Esempio n. 53
0
def test_store_get_file_class():
    store = Store.objects.filter(
        translation_project__project__code="project0",
        translation_project__language__code="language0").first()

    # this matches because po is recognised by ttk
    assert store.syncer.file_class == getclass(store)

    # file_class is cached so lets delete it
    del store.syncer.__dict__["file_class"]

    class CustomFormatClass(object):
        pass

    @provider(format_classes)
    def format_class_provider(**kwargs):
        return dict(po=CustomFormatClass)

    # we get the CutomFormatClass as it was registered
    assert store.syncer.file_class is CustomFormatClass

    # the Store.filetype is used in this case not the name
    store.name = "new_store_name.foo"
    del store.syncer.__dict__["file_class"]
    assert store.syncer.file_class is CustomFormatClass

    # lets register a foo filetype
    format_registry = formats.get()
    foo_filetype = format_registry.register("foo", "foo")

    store.filetype = foo_filetype
    store.save()

    # oh no! not recognised by ttk
    del store.syncer.__dict__["file_class"]
    with pytest.raises(ValueError):
        store.syncer.file_class

    @provider(format_classes)
    def another_format_class_provider(**kwargs):
        return dict(foo=CustomFormatClass)

    # works now
    assert store.syncer.file_class is CustomFormatClass

    format_classes.disconnect(format_class_provider)
    format_classes.disconnect(another_format_class_provider)
Esempio n. 54
0
def test_store_get_file_class():
    store = Store.objects.filter(
        translation_project__project__code="project0",
        translation_project__language__code="language0").first()

    # this matches because po is recognised by ttk
    assert store.syncer.file_class == getclass(store)

    # file_class is cached so lets delete it
    del store.syncer.__dict__["file_class"]

    class CustomFormatClass(object):
        pass

    @provider(format_classes)
    def format_class_provider(**kwargs):
        return dict(po=CustomFormatClass)

    # we get the CutomFormatClass as it was registered
    assert store.syncer.file_class is CustomFormatClass

    # the Store.filetype is used in this case not the name
    store.name = "new_store_name.foo"
    del store.syncer.__dict__["file_class"]
    assert store.syncer.file_class is CustomFormatClass

    # lets register a foo filetype
    format_registry = formats.get()
    foo_filetype = format_registry.register("foo", "foo")

    store.filetype = foo_filetype
    store.save()

    # oh no! not recognised by ttk
    del store.syncer.__dict__["file_class"]
    with pytest.raises(ValueError):
        store.syncer.file_class

    @provider(format_classes)
    def another_format_class_provider(**kwargs):
        return dict(foo=CustomFormatClass)

    # works now
    assert store.syncer.file_class is CustomFormatClass

    format_classes.disconnect(format_class_provider)
    format_classes.disconnect(another_format_class_provider)
Esempio n. 55
0
def test_wrap_store_fs_pull_merge_default(store_fs_file):
    fs_file = store_fs_file
    fs_file.pull()
    unit = fs_file.store.units[0]
    unit.target = "FOO"
    unit.save()
    with open(fs_file.file_path, "r+") as target:
        ttk = getclass(target)(target.read())
        target.seek(0)
        ttk.units[1].target = "BAR"
        target.write(str(ttk))
        target.truncate()
    assert fs_file.fs_changed is True
    assert fs_file.pootle_changed is True
    fs_file.pull(merge=True)
    assert fs_file.store.units[0].target == "BAR"
    assert fs_file.store.units[0].get_suggestions()[0].target == "FOO"
Esempio n. 56
0
File: utils.py Progetto: arky/pootle
def import_file(f, user=None):
    ttk = getclass(f)(f.read())
    if not hasattr(ttk, "parseheader"):
        raise UnsupportedFiletypeError(_("Unsupported filetype '%s', only PO "
                                         "files are supported at this time\n",
                                         f.name))
    header = ttk.parseheader()
    pootle_path = header.get("X-Pootle-Path")
    if not pootle_path:
        raise MissingPootlePathError(_("File '%s' missing X-Pootle-Path "
                                       "header\n", f.name))

    rev = header.get("X-Pootle-Revision")
    if not rev or not rev.isdigit():
        raise MissingPootleRevError(_("File '%s' missing or invalid "
                                      "X-Pootle-Revision header\n",
                                      f.name))
    rev = int(rev)

    try:
        store = Store.objects.get(pootle_path=pootle_path)
    except Store.DoesNotExist as e:
        raise FileImportError(
            _("Could not create '%(filename)s'. Missing "
              "Project/Language? (%(error)s)",
              dict(filename=f.name, error=e)))

    tp = store.translation_project
    allow_add_and_obsolete = ((tp.project.checkstyle == 'terminology'
                               or tp.is_template_project)
                              and check_user_permission(user,
                                                        'administrate',
                                                        tp.directory))
    try:
        store.update(store=ttk, user=user,
                     submission_type=SubmissionTypes.UPLOAD,
                     store_revision=rev,
                     allow_add_and_obsolete=allow_add_and_obsolete)
    except Exception as e:
        # This should not happen!
        logger.error("Error importing file: %s", str(e))
        raise FileImportError(_("There was an error uploading your file"))
Esempio n. 57
0
def complex_po(test_fs):
    """A Store with some complex Units"""
    from pootle_translationproject.models import TranslationProject

    from pytest_pootle.factories import StoreDBFactory

    tp = TranslationProject.objects.get(
        project__code="project0",
        language__code="language0")

    store = StoreDBFactory(
        parent=tp.directory,
        translation_project=tp,
        name="complex_store.po")

    with test_fs.open(("data", "po", "complex.po")) as f:
        ttk = getclass(f)(f.read())

    store.update(ttk)
    return store
Esempio n. 58
0
    def setup_complex_po(self):
        import pytest_pootle
        from pytest_pootle.factories import StoreDBFactory
        from pootle_translationproject.models import TranslationProject

        po_file = os.path.join(
            os.path.dirname(pytest_pootle.__file__),
            *("data", "po", "complex.po"))
        with open(po_file) as f:
            ttk = getclass(f)(f.read())

        tp = TranslationProject.objects.get(
            project__code="project0",
            language__code="language0")

        store = StoreDBFactory(
            parent=tp.directory,
            translation_project=tp,
            name="complex.po")
        store.update(ttk)