def create_terminology_project(self): """Create the terminology project. The terminology project is used to display terminology suggestions while translating. """ criteria = { 'code': "terminology", 'fullname': u"Terminology", 'source_language': self.require_english(), 'checkstyle': "terminology", } po = Format.objects.get(name="po") terminology = self._create_object(Project, **criteria)[0] terminology.filetypes.add(po) terminology.config["pootle_fs.fs_url"] = os.path.join( settings.POOTLE_TRANSLATION_DIRECTORY, terminology.code) terminology.config["pootle_fs.fs_type"] = "localfs" terminology.config["pootle_fs.translation_mappings"] = dict( default="/<language_code>/<dir_path>/<filename>.<ext>") plugin = FSPlugin(terminology) plugin.fetch() plugin.add() plugin.sync()
def test_init_fs_project_cmd_nosync(settings, test_fs, tmpdir, revision): settings.POOTLE_FS_WORKING_PATH = str(tmpdir) fs_path = test_fs.path("data/fs/example_fs/non_gnu_style_minimal/") tr_path = "<language_code>/<filename>.<ext>" call_command( "init_fs_project", "foo", fs_path, tr_path, "--nosync", "--checkstyle=standard", "--filetypes=po", "--source-language=en", "--name=Foo" ) project = Project.objects.get(code='foo') assert project is not None assert project.code == "foo" assert project.fullname == "Foo" assert "po" in project.filetypes.values_list("name", flat=True) assert project.checkstyle == "standard" assert project.source_language.code == "en" assert project.treestyle == 'pootle_fs' assert project.config.get('pootle_fs.fs_type') == 'localfs' assert project.config.get('pootle_fs.fs_url') == fs_path assert project.config.get( 'pootle_fs.translation_mappings')['default'] == tr_path assert project.translationproject_set.all().count() == 0 plugin = FSPlugin(project) plugin.fetch() state = plugin.state() assert "fs_untracked: 1" in str(state)
def test_init_fs_project_cmd_nosync(settings, test_fs, tmpdir, revision): settings.POOTLE_FS_WORKING_PATH = str(tmpdir) fs_path = test_fs.path("data/fs/example_fs/non_gnu_style_minimal/") tr_path = "<language_code>/<filename>.<ext>" call_command( "init_fs_project", "foo", fs_path, tr_path, "--nosync", "--checkstyle=standard", "--filetypes=po", "--source-language=en", "--name=Foo" ) project = Project.objects.get(code='foo') assert project is not None assert project.code == "foo" assert project.fullname == "Foo" assert "po" in project.filetypes.values_list("name", flat=True) assert project.checkstyle == "standard" assert project.source_language.code == "en" assert project.treestyle == 'pootle_fs' assert project.config.get('pootle_fs.fs_type') == 'localfs' assert project.config.get('pootle_fs.fs_url') == fs_path assert project.config.get( 'pootle_fs.translation_mappings')['default'] == tr_path assert project.translationproject_set.all().count() == 0 plugin = FSPlugin(project) plugin.fetch() state = plugin.state() assert "fs_untracked: 1" in str(state)
def _sync_translations(db_unit): store = db_unit.store tp = store.translation_project project = tp.project language = tp.language plugin = FSPlugin(project) plugin.fetch() plugin.sync() file_store = db_unit.store.deserialize( open(os.path.join(plugin.fs_url, language.code, store.name)).read()) file_unit = file_store.findid(db_unit.getid()) return file_store, file_unit
def project_fs(tmpdir, settings): from pootle_project.models import Project from pootle_fs.utils import FSPlugin project = Project.objects.get(code="project0") new_url = os.path.join(str(tmpdir), "__src__") project.config["pootle_fs.fs_url"] = new_url plugin = FSPlugin(project) os.makedirs(new_url) settings.POOTLE_FS_WORKING_PATH = str(tmpdir) plugin.fetch() return plugin
def project_fs(tmpdir, settings): from pootle_project.models import Project from pootle_fs.utils import FSPlugin project = Project.objects.get(code="project0") new_url = os.path.join(str(tmpdir), "__src__") project.config["pootle_fs.fs_url"] = new_url plugin = FSPlugin(project) os.makedirs(new_url) settings.POOTLE_FS_WORKING_PATH = str(tmpdir) plugin.fetch() return plugin
def _parse_tps_to_create(self, project): plugin = FSPlugin(project) plugin.fetch() untracked_languages = set( fs.pootle_path.split("/")[1] for fs in plugin.state()["fs_untracked"]) new_langs = ([ lang for lang in untracked_languages if lang in self.languages ] if self.languages else untracked_languages) return Language.objects.filter(code__in=new_langs).exclude( code__in=project.translationproject_set.values_list( "language__code", flat=True))
def handle_all_stores(self, translation_project, **options): path_glob = "%s*" % translation_project.pootle_path plugin = FSPlugin(translation_project.project) plugin.fetch() if translation_project.project.pk not in self.warn_on_conflict: state = plugin.state() if any(k in state for k in ["conflict", "conflict_untracked"]): logger.warn( "The project '%s' has conflicting changes in the database " "and translation files. Use `pootle fs resolve` to tell " "pootle how to merge", translation_project.project.code) self.warn_on_conflict.append(translation_project.project.pk) if not options["skip_missing"]: plugin.add(pootle_path=path_glob, update="fs") plugin.sync(pootle_path=path_glob, update="fs")
def _sync_translations(db_unit): store = db_unit.store tp = store.translation_project project = tp.project language = tp.language plugin = FSPlugin(project) plugin.fetch() plugin.sync() file_store = db_unit.store.deserialize( open(os.path.join( plugin.fs_url, language.code, store.name)).read()) file_unit = file_store.findid(db_unit.getid()) return file_store, file_unit
def tutorial(english, settings): """Require `tutorial` test project.""" import pytest_pootle from pootle_fs.utils import FSPlugin shutil.copytree( os.path.join(os.path.dirname(pytest_pootle.__file__), "data", "po", "tutorial"), os.path.join(settings.POOTLE_TRANSLATION_DIRECTORY, "tutorial")) project = _require_project('tutorial', 'Tutorial', english, settings) plugin = FSPlugin(project) plugin.fetch() plugin.add() plugin.sync() return project
def handle_all_stores(self, translation_project, **options): path_glob = "%s*" % translation_project.pootle_path plugin = FSPlugin(translation_project.project) plugin.fetch() if translation_project.project.pk not in self.warn_on_conflict: state = plugin.state() if any(k in state for k in ["conflict", "conflict_untracked"]): logger.warn( "The project '%s' has conflicting changes in the database " "and translation files. Use `pootle fs resolve` to tell " "pootle how to merge", translation_project.project.code) self.warn_on_conflict.append( translation_project.project.pk) if not options["skip_missing"]: plugin.add(pootle_path=path_glob, update="fs") plugin.sync(pootle_path=path_glob, update="fs")
def handle(self, **options): source_language_code = options['source_language'] try: source_language = Language.objects.get(code=source_language_code) except Language.DoesNotExist as e: self.stdout.write('%s: Unknown language code.' % source_language_code) raise CommandError(e) fs_type, fs_url = parse_fs_url(options['fs']) code = options['code'] name = options['name'] or code.capitalize() try: project = Project.objects.create( code=code, fullname=name, treestyle='pootle_fs', checkstyle=options['checkstyle'], source_language=source_language) except ValidationError as e: raise CommandError(e) for filetype in options["filetypes"] or ["po"]: try: filetype = Format.objects.get(name=filetype) project.filetypes.add(filetype) except Format.DoesNotExist as e: raise CommandError(e) project.config['pootle_fs.fs_type'] = fs_type project.config['pootle_fs.fs_url'] = fs_url project.config['pootle_fs.translation_mappings'] = { 'default': options['translation_mapping'] } if options['sync']: try: plugin = FSPlugin(project) plugin.fetch() plugin.add() plugin.sync() except FSFetchError as e: project.delete() raise CommandError(e)
def tutorial(english, settings): """Require `tutorial` test project.""" import pytest_pootle from pootle_fs.utils import FSPlugin shutil.copytree( os.path.join( os.path.dirname(pytest_pootle.__file__), "data", "po", "tutorial"), os.path.join( settings.POOTLE_TRANSLATION_DIRECTORY, "tutorial")) project = _require_project('tutorial', 'Tutorial', english, settings) plugin = FSPlugin(project) plugin.fetch() plugin.add() plugin.sync() return project
def handle(self, **options): source_language_code = options['source_language'] try: source_language = Language.objects.get(code=source_language_code) except Language.DoesNotExist as e: self.stdout.write('%s: Unknown language code.' % source_language_code) raise CommandError(e) fs_type, fs_url = parse_fs_url(options['fs']) code = options['code'] name = options['name'] or code.capitalize() try: project = Project.objects.create( code=code, fullname=name, checkstyle=options['checkstyle'], source_language=source_language) except ValidationError as e: raise CommandError(e) for filetype in options["filetypes"] or ["po"]: try: filetype = Format.objects.get(name=filetype) project.filetypes.add(filetype) except Format.DoesNotExist as e: raise CommandError(e) project.config['pootle_fs.fs_type'] = fs_type project.config['pootle_fs.fs_url'] = fs_url project.config['pootle_fs.translation_mappings'] = { 'default': options['translation_mapping'] } if options['sync']: try: plugin = FSPlugin(project) plugin.fetch() plugin.add() plugin.sync() except FSFetchError as e: project.delete() raise CommandError(e)
def create_default_projects(self): """Create the default projects that we host. You might want to add your projects here, although you can also add things through the web interface later. """ en = self.require_english() po = Format.objects.get(name="po") criteria = { 'code': u"tutorial", 'source_language': en, 'fullname': u"Tutorial", 'checkstyle': "standard" } tutorial = self._create_object(Project, **criteria)[0] tutorial.filetypes.add(po) tutorial.config["pootle_fs.fs_type"] = "localfs" tutorial.config["pootle_fs.fs_url"] = ( "{POOTLE_TRANSLATION_DIRECTORY}%s" % tutorial.code) tutorial.config["pootle_fs.translation_mappings"] = dict( default="/<language_code>/<dir_path>/<filename>.<ext>") plugin = FSPlugin(tutorial) plugin.fetch() plugin.add() plugin.sync() criteria = { 'active': True, 'title': "Project instructions", 'body': ('Tutorial project where users can play with Pootle and learn ' 'more about translation and localisation.\n' '\n' 'For more help on localisation, visit the [localization ' 'guide](http://docs.translatehouse.org/projects/' 'localization-guide/en/latest/guide/start.html).'), 'virtual_path': "announcements/projects/" + tutorial.code, } self._create_object(Announcement, **criteria)
def create_default_projects(self): """Create the default projects that we host. You might want to add your projects here, although you can also add things through the web interface later. """ en = self.require_english() po = Format.objects.get(name="po") criteria = { 'code': u"tutorial", 'source_language': en, 'fullname': u"Tutorial", 'checkstyle': "standard"} tutorial = self._create_object(Project, **criteria)[0] tutorial.filetypes.add(po) tutorial.config["pootle_fs.fs_url"] = os.path.join( settings.POOTLE_TRANSLATION_DIRECTORY, tutorial.code) tutorial.config["pootle_fs.fs_type"] = "localfs" tutorial.config["pootle_fs.translation_mappings"] = dict( default="/<language_code>/<dir_path>/<filename>.<ext>") plugin = FSPlugin(tutorial) plugin.fetch() plugin.add() plugin.sync() criteria = { 'active': True, 'title': "Project instructions", 'body': ( 'Tutorial project where users can play with Pootle and learn ' 'more about translation and localisation.\n' '\n' 'For more help on localisation, visit the [localization ' 'guide](http://docs.translatehouse.org/projects/' 'localization-guide/en/latest/guide/start.html).'), 'virtual_path': "announcements/projects/"+tutorial.code, } self._create_object(Announcement, **criteria)
def handle(self, **options): source_language_code = options['source_language'] try: source_language = Language.objects.get(code=source_language_code) except Language.DoesNotExist as e: self.stdout.write('%s: Unknown language code.' % source_language_code) raise CommandError(e) fs_type, fs_url = parse_fs_url(options['fs']) code = options['code'] name = options['name'] or code.capitalize() try: project = Project.objects.create( code=code, fullname=name, localfiletype=options['file_type'], treestyle='none', checkstyle=options['checkstyle'], source_language=source_language ) project.update_all_cache() except IntegrityError as e: self.stdout.write('Project code "%s" already exists.' % options['code']) raise CommandError(e) project.config['pootle_fs.fs_type'] = fs_type project.config['pootle_fs.fs_url'] = fs_url project.config['pootle_fs.translation_paths'] = { 'default': options['translation_path'] } if options['sync']: plugin = FSPlugin(project) plugin.fetch() plugin.sync()
def create_terminology_project(self): """Create the terminology project. The terminology project is used to display terminology suggestions while translating. """ criteria = { 'code': "terminology", 'fullname': u"Terminology", 'source_language': self.require_english(), 'checkstyle': "terminology", } po = Format.objects.get(name="po") terminology = self._create_object(Project, **criteria)[0] terminology.filetypes.add(po) terminology.config["pootle_fs.fs_type"] = "localfs" terminology.config["pootle_fs.fs_url"] = ( "{POOTLE_TRANSLATION_DIRECTORY}%s" % terminology.code) terminology.config["pootle_fs.translation_mappings"] = dict( default="/<language_code>/<dir_path>/<filename>.<ext>") plugin = FSPlugin(terminology) plugin.fetch() plugin.add() plugin.sync()
def setup_git_env(django_db_setup, django_db_blocker): """Sets up the site DB only if tests requested to use the DB (autouse).""" from django.conf import settings import pytest_pootle from pytest_pootle.factories import (ProjectDBFactory, TranslationProjectFactory) from pootle_fs.utils import FSPlugin from pootle_language.models import Language import tempfile with django_db_blocker.unblock(): project0 = ProjectDBFactory( source_language=Language.objects.get(code="en"), code="git_project_0") language0 = Language.objects.get(code="language0") TranslationProjectFactory(project=project0, language=language0) initial_src_path = os.path.abspath( os.path.join(os.path.dirname(pytest_pootle.__file__), "data/fs/example_fs")) fs_dir = tempfile.mkdtemp() settings.POOTLE_FS_PATH = fs_dir repo_path = os.path.join(fs_dir, "__git_src_project_0__") Repo.init(repo_path, bare=True) with tmp_git(repo_path) as (tmp_repo_path, tmp_repo): with get_dir_util() as dir_util: dir_util.copy_tree(initial_src_path, tmp_repo_path) tmp_repo.index.commit("Initial commit") tmp_repo.remotes.origin.push("master:master") project0.config["pootle_fs.fs_type"] = "git" project0.config["pootle_fs.fs_url"] = repo_path project0.config["pootle_fs.translation_mappings"] = { "default": "/<language_code>/<dir_path>/<filename>.<ext>" } plugin = FSPlugin(project0) plugin.fetch() plugin.add() plugin.sync() # create_test_suite(plugin) project1 = ProjectDBFactory( source_language=Language.objects.get(code="en"), code="git_project_1") TranslationProjectFactory(project=project1, language=language0) repo_path = os.path.join(fs_dir, "__git_src_project_1__") Repo.init(repo_path, bare=True) with tmp_git(repo_path) as (tmp_repo_path, tmp_repo): with get_dir_util() as dir_util: dir_util.copy_tree(initial_src_path, tmp_repo_path) tmp_repo.index.add(["*"]) tmp_repo.index.commit("Initial commit") tmp_repo.remotes.origin.push("master:master") project1.config["pootle_fs.fs_type"] = "git" project1.config["pootle_fs.fs_url"] = repo_path project1.config["pootle_fs.translation_mappings"] = { "default": "/<language_code>/<dir_path>/<filename>.<ext>" }
def hg_env(post_db_setup, _django_cursor_wrapper): from django.conf import settings import pytest_pootle from pytest_pootle.factories import ( ProjectDBFactory, TranslationProjectFactory) from pootle_fs.utils import FSPlugin from pootle_language.models import Language import tempfile with _django_cursor_wrapper: project0 = ProjectDBFactory( source_language=Language.objects.get(code="en"), code="hg_project_0") language0 = Language.objects.get(code="language0") TranslationProjectFactory(project=project0, language=language0) initial_src_path = os.path.abspath( os.path.join( os.path.dirname(pytest_pootle.__file__), "data/fs/example_fs")) fs_dir = tempfile.mkdtemp() settings.POOTLE_FS_PATH = fs_dir repo_path = os.path.join(fs_dir, "__hg_src_project_0__") hglib.init(repo_path) with tmp_hg(repo_path) as (tmp_repo_path, tmp_repo): with get_dir_util() as dir_util: dir_util.copy_tree(initial_src_path, tmp_repo_path) tmp_repo.add() tmp_repo.commit("Initial commit") tmp_repo.push() project0.config["pootle_fs.fs_type"] = "hg" project0.config["pootle_fs.fs_url"] = repo_path project0.config["pootle_fs.translation_paths"] = { "default": "/<language_code>/<dir_path>/<filename>.<ext>"} plugin = FSPlugin(project0) plugin.add() plugin.fetch() plugin.sync() # create_test_suite(plugin) project1 = ProjectDBFactory( source_language=Language.objects.get(code="en"), code="hg_project_1") TranslationProjectFactory(project=project1, language=language0) repo_path = os.path.join(fs_dir, "__hg_src_project_1__") hglib.init(repo_path) with tmp_hg(repo_path) as (tmp_repo_path, tmp_repo): with get_dir_util() as dir_util: dir_util.copy_tree(initial_src_path, tmp_repo_path) tmp_repo.add() tmp_repo.commit("Initial commit") tmp_repo.push() project1.config["pootle_fs.fs_type"] = "hg" project1.config["pootle_fs.fs_url"] = repo_path project1.config["pootle_fs.translation_paths"] = { "default": "/<language_code>/<dir_path>/<filename>.<ext>"}
def hg_env(post_db_setup, _django_cursor_wrapper): from django.conf import settings import pytest_pootle from pytest_pootle.factories import (ProjectDBFactory, TranslationProjectFactory) from pootle_fs.utils import FSPlugin from pootle_language.models import Language import tempfile with _django_cursor_wrapper: project0 = ProjectDBFactory( source_language=Language.objects.get(code="en"), code="hg_project_0") language0 = Language.objects.get(code="language0") TranslationProjectFactory(project=project0, language=language0) initial_src_path = os.path.abspath( os.path.join(os.path.dirname(pytest_pootle.__file__), "data/fs/example_fs")) fs_dir = tempfile.mkdtemp() settings.POOTLE_FS_PATH = fs_dir repo_path = os.path.join(fs_dir, "__hg_src_project_0__") hglib.init(repo_path) with tmp_hg(repo_path) as (tmp_repo_path, tmp_repo): with get_dir_util() as dir_util: dir_util.copy_tree(initial_src_path, tmp_repo_path) tmp_repo.add() tmp_repo.commit("Initial commit") tmp_repo.push() project0.config["pootle_fs.fs_type"] = "hg" project0.config["pootle_fs.fs_url"] = repo_path project0.config["pootle_fs.translation_paths"] = { "default": "/<language_code>/<dir_path>/<filename>.<ext>" } plugin = FSPlugin(project0) plugin.add() plugin.fetch() plugin.sync() # create_test_suite(plugin) project1 = ProjectDBFactory( source_language=Language.objects.get(code="en"), code="hg_project_1") TranslationProjectFactory(project=project1, language=language0) repo_path = os.path.join(fs_dir, "__hg_src_project_1__") hglib.init(repo_path) with tmp_hg(repo_path) as (tmp_repo_path, tmp_repo): with get_dir_util() as dir_util: dir_util.copy_tree(initial_src_path, tmp_repo_path) tmp_repo.add() tmp_repo.commit("Initial commit") tmp_repo.push() project1.config["pootle_fs.fs_type"] = "hg" project1.config["pootle_fs.fs_url"] = repo_path project1.config["pootle_fs.translation_paths"] = { "default": "/<language_code>/<dir_path>/<filename>.<ext>" }
def test_plugin_fetch(git_project_1): git_plugin = FSPlugin(git_project_1) assert git_plugin.is_cloned is False git_plugin.fetch() assert git_plugin.is_cloned is True