transtool_import.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:django-transtool 作者: liminspace 项目源码 文件源码
def handle(self, *args, **options):
        if not TRANSTOOL_DL_URL or not TRANSTOOL_DL_KEY:
            raise CommandError('Please, set TRANSTOOL_DL_URL and TRANSTOOL_DL_KEY settings.')
        if options['mo_only'] and options['po_only']:
            raise CommandError('Use only --mo-only or --po-only but not both.')
        self.stdout.write('Download file: Send POST request to {}'.format(TRANSTOOL_DL_URL))
        r = requests.post(TRANSTOOL_DL_URL, {
            'key': TRANSTOOL_DL_KEY,
            'po-only': str(int(options['po_only'])),
            'mo-only': str(int(options['mo_only'])),
        }, stream=True)
        if r.status_code != 200:
            self.stdout.write('Request status code is not 200: {}'.format(r.status_code))
            self.stdout.write('Fail.', ending='\n\n')
            sys.exit(1)
        file_content = BytesIO()
        for chunk in r.iter_content(chunk_size=(16 * 1024)):
            file_content.write(chunk)
        file_content.seek(0, os.SEEK_END)
        file_content_size = file_content.tell()
        self.stdout.write('Downloaded file {} {} bytes'.format(r.headers['Content-Type'], file_content_size))
        if options['po_only']:
            exts = ['.po']
        elif options['mo_only']:
            exts = ['.mo']
        else:
            exts = ['.po', '.mo']
        diff_info = self._get_diff_info(file_content, exts)
        if options['diff']:
            self.print_diff_info(diff_info)
        else:
            self.copy_files(diff_info, file_content)
        self.stdout.write('Done.', ending='\n\n')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号