1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
# -*- coding: utf-8 -*-
# Copyright (c) 2019 SubDownloader Developers - See COPYING - GPLv3
import logging
from pathlib import Path
import re
from subprocess import check_output
log = logging.getLogger('generate.ui')
PYUIC = 'pyuic5'
self_mtime = Path(__file__).lstat().st_mtime
class UiFile(object):
def __init__(self, source, target, import_from):
self.source = source
self.target = target
self.import_from = import_from
@staticmethod
def generate_target(source):
src = Path(source)
return src.parent / (src.stem + '_ui.py')
def target_outdated(self):
outdated = False
error = False
if not self.source.exists():
error = True
log.error('File {} does not exist.'.format(self.source))
if error:
raise ValueError('Fatal error.')
if self.target.exists():
if self_mtime > self.target.lstat().st_mtime:
outdated = True
if self.source.lstat().st_mtime > self.target.lstat().st_mtime:
outdated = True
else:
outdated = True
return outdated
def run(self, dry):
args = [PYUIC, str(self.source), '--import-from', self.import_from]
log.info('Running {}'.format(args))
if not dry:
output = check_output(args)
fixed = self.fix_translations(output)
with self.target.open(mode='wb') as f:
f.write(fixed)
def clean(self, dry):
log.info('Removing {}'.format(self.target))
if not dry:
self.target.unlink()
@classmethod
def fix_translations(cls, text):
new_string, _ = re.subn(b'_translate\(".*?",\s(".*?")\s*?\)', b'_(\g<1>)', text)
return new_string
|