Пример #1
0
    def upload_translations(self):
        info2 = Configuration(self.options_config).export_pattern_to_path(
            self.lang())
        base_path = os.path.normpath(
            Configuration(self.options_config).get_base_path()) + os.sep

        translations_language = info2[1::3]
        translations_path = self.preserve_hierarchy(info2[::3])
        translations_parameters = info2[2::3]

        # Creating branch if needed
        if self.any_options.branch and self.any_options.branch not in self.get_info_branches(
        ):
            self.create_directory(self.any_options.branch, is_branch=True)

        for i, source_file, params in zip(translations_language,
                                          translations_path,
                                          translations_parameters):
            for language, item in six.iteritems(i):
                if params.get('dest'):
                    if '/' in item:
                        items = source_file[source_file.rfind("/"):]
                        source_file = params.get('dest').join(
                            source_file.rsplit(items, 1))
                    else:
                        source_file = params.get('dest')
                full_path = base_path.replace('\\', '/') + item
                check_l_option = self.any_options.language
                if check_l_option:
                    if language == check_l_option:
                        self.upload_translations_files(full_path, language,
                                                       source_file)
                else:
                    self.upload_translations_files(full_path, language,
                                                   source_file)
Пример #2
0
    def download_project(self):
        # GET https://api.crowdin.com/api/project/{project-identifier}/download/{package}.zip?key={project-key}
        self.build_project()
        base_path = os.path.normpath(
            Configuration(self.options_config).get_base_path()) + os.sep
        if self.any_options.dlanguage:
            lang = self.any_options.dlanguage
        else:
            lang = "all"
        url = {
            'post': 'GET',
            'url_par1': '/api/project/',
            'url_par2': True,
            'url_par3': '/download/{0}.zip'.format(lang),
            'url_par4': True
        }
        params = {'json': 'json'}

        # files that exists in archive and doesn't match current project configuration
        unmatched_files = []

        with zipfile.ZipFile(io.BytesIO(self.true_connection(url,
                                                             params))) as z:
            # for i in self.exists(Configuration().get_files_source()):
            unzip_dict = {}
            translations_file = Configuration(
                self.options_config).export_pattern_to_path(self.lang(),
                                                            download=True)
            trans_file_no_mapping = Configuration(
                self.options_config).export_pattern_to_path(self.lang())
            for i, y in zip(translations_file[1::3],
                            trans_file_no_mapping[1::3]):
                for k, v in six.iteritems(y):
                    for key, value in six.iteritems(i):
                        if k == key:
                            unzip_dict[value] = v
            # print unzip_dict
            matched_files = []
            for structure in z.namelist():
                if not structure.endswith("/"):
                    for key, value in six.iteritems(unzip_dict):
                        if structure == key:
                            matched_files.append(structure)
                            source = z.open(structure)
                            target = open(os.path.join(base_path, value), "wb")
                            logger.info("Download: {0}".format(value))
                            with source, target:
                                shutil.copyfileobj(source, target)
                            # z.extract(structure, base_path)

                    if not structure in unmatched_files and not structure in matched_files:
                        unmatched_files.append(structure)

            if unmatched_files:
                logger.info(
                    "\nWarning: Downloaded translations do not match current project "
                    "configuration. Some of the resulted files will be omitted."
                )
                for i in unmatched_files:
                    print(i)
Пример #3
0
    def preserve_hierarchy(self, common_path):
        common_path = [
            i[1:] if i[:1] == '/' and i.count('/') == 1 else i
            for i in common_path
        ]
        preserve_hierarchy = Configuration(
            self.options_config).preserve_hierarchy

        if preserve_hierarchy is False:
            for i in common_path:
                if i.count('/') >= 2 and i.count('//') == 0:
                    check_list = []
                    for x in common_path:
                        new = x[:x.rfind("/")]
                        check_list.append(new[new.rfind("/"):])
                    if check_list.count(check_list[0]) == len(check_list):
                        sorted_list = [
                            x[:x.rfind("/")] + '/' for x in common_path
                        ]
                    else:
                        sorted_list = []
                        for x in common_path:
                            g = x[:x.rfind("/")]
                            sorted_list.append(g[:g.rfind("/")])
                    common_path = [
                        s.replace(os.path.commonprefix(sorted_list), '', 1)
                        for s in common_path
                    ]
                break
        return common_path
Пример #4
0
 def list_project_files(self):
     # print self.any_options
     listing = []
     if self.any_options.sources == 'project':
         project_files = self.parse(self.get_info_files())
         for i in project_files:
             print(i)
             listing.append(i)
     if self.any_options.sources == 'sources':
         sources_files = Configuration(
             self.options_config).get_files_source()
         for i in sources_files[::3]:
             print(i)
             listing.append(i)
     if self.any_options.sources == 'translations':
         translations_file = Configuration(
             self.options_config).export_pattern_to_path(self.lang())
         for i in translations_file[1::3]:
             for key, value in six.iteritems(i):
                 print(value)
                 listing.append(value)
     return listing
Пример #5
0
 def test(self):
     print(Configuration(self.options_config).get_files_source())
Пример #6
0
    def download_project(self):
        # GET https://api.crowdin.com/api/project/{project-identifier}/download/{package}.zip?key={project-key}
        self.build_project()
        base_path = os.path.normpath(
            Configuration(self.options_config).get_base_path()) + os.sep
        if self.any_options.dlanguage:
            lang = self.any_options.dlanguage
        else:
            lang = "all"
        url = {
            'post': 'GET',
            'url_par1': '/api/project/',
            'url_par2': True,
            'url_par3': '/download/{0}.zip'.format(lang),
            'url_par4': True
        }
        params = {'json': 'json'}
        if self.any_options.branch:
            params['branch'] = self.any_options.branch
        # files that exists in archive and doesn't match current project configuration
        unmatched_files = []

        with zipfile.ZipFile(io.BytesIO(self.true_connection(url,
                                                             params))) as z:
            # for i in self.exists(Configuration().get_files_source()):
            unzip_dict = {}
            lang = self.lang()
            translations_file = Configuration(
                self.options_config).export_pattern_to_path(lang,
                                                            download=True)
            trans_file_no_mapping = Configuration(
                self.options_config).export_pattern_to_path(lang)
            for i, y in zip(translations_file[1::3],
                            trans_file_no_mapping[1::3]):
                for k, v in six.iteritems(y):
                    for key, value in six.iteritems(i):
                        if k == key:
                            unzip_dict[value] = v
                            if self.any_options.branch:
                                unzip_dict[self.any_options.branch + '/' +
                                           value] = v

            initial_files = unzip_dict.keys()
            for target_lang in lang:
                for source_file in list(initial_files):
                    # change only for target_lang files
                    for lang_key in target_lang:
                        if target_lang[lang_key] in source_file:
                            if source_file == unzip_dict[source_file]:
                                f = os.path.basename(source_file)
                            else:
                                r_source = list(
                                    reversed(source_file.split('/')))
                                r_target = list(
                                    reversed(
                                        unzip_dict[source_file].split('/')))
                                f = ''
                                # print(r_source)
                                # print(r_target)
                                for i in range(len(r_target) - 1):
                                    if r_target[i] == r_source[i]:
                                        f = '/' + r_target[i] + f

                            if not self.any_options.branch:
                                k = target_lang[lang_key] + '/' + f
                            else:
                                k = self.any_options.branch + '/' + target_lang[
                                    lang_key] + '/' + f
                            k = k.replace('//', '/')
                            unzip_dict[k] = unzip_dict[source_file]

            matched_files = []
            for structure in z.namelist():
                if not structure.endswith("/"):
                    for key, value in six.iteritems(unzip_dict):
                        if structure == key:
                            matched_files.append(structure)
                            source = z.open(structure)
                            target_path = os.path.join(base_path, value)
                            target_dir = os.path.dirname(target_path)
                            if not os.path.isdir(target_dir):
                                os.makedirs(target_dir)

                            target = open(target_path, "wb")
                            logger.info("Download: {0} to {1}".format(
                                key, target_path))
                            with source, target:
                                shutil.copyfileobj(source, target)
                                # z.extract(structure, base_path)

                    if structure not in unmatched_files and structure not in matched_files:
                        unmatched_files.append(structure)

            if unmatched_files:
                logger.warning(
                    "\n Warning: Downloaded translations do not match current project configuration. "
                    "Some of the resulted files will be omitted.")
                for i in unmatched_files:
                    print(i)
Пример #7
0
    def upload_sources(self, dirss=False):
        dirs = []
        files = []
        project_files = self.parse(self.get_info_files(),
                                   branch=self.any_options.branch)
        for item in project_files:

            p = "/"
            f = item[:item.rfind("/")]
            l = f[1:].split("/")
            i = 0
            while i < len(l):
                p = p + l[i] + "/"
                i += 1
                if p not in dirs:
                    dirs.append(p)

            if not item.endswith("/"):
                files.append(item)

        all_info = Configuration(self.options_config).get_files_source()

        base_path = os.path.normpath(
            Configuration(self.options_config).get_base_path()) + os.sep
        common_path = self.preserve_hierarchy(all_info[::3])
        # sources_path = common_path
        translations_path = all_info[1::3]
        sources_parameters = all_info[2::3]

        # Creating branch if needed
        if self.any_options.branch and self.any_options.branch not in self.get_info_branches(
        ):
            self.create_directory(self.any_options.branch, is_branch=True)

        # Creating directories
        for item in common_path:
            if '/' in item and not item[:item.rfind("/")] in dirs:
                items = item[:item.rfind("/")]
                # print items
                p = "/"
                if items[0] == '/':
                    items = items[1:]
                l = items.split("/")
                i = 0
                while i < len(l):
                    p = p + l[i] + "/"
                    i += 1
                    if p not in dirs and not p == '//':
                        dirs.append(p)
                        self.create_directory(p)

        # Uploading/updating files
        for item, export_patterns, true_path, parameters in zip(
                common_path, translations_path, all_info[::3],
                sources_parameters):

            if parameters.get('dest'):
                if '/' in item:
                    items = item[item.rfind("/"):]
                    item = parameters.get('dest').join(item.rsplit(items, 1))
                else:
                    item = parameters.get('dest')
            if item[0] != '/':
                ite = "/" + item
            else:
                ite = item
            full_path = base_path.replace('\\', '/') + true_path
            print(full_path)

            if ite not in files:
                self.upload_files(full_path, export_patterns, parameters, item)
            else:
                self.update_files(full_path, export_patterns, parameters, item)
        if dirss:
            return dirs