Exemplo n.º 1
0
    def save(self, path, encoding=u'utf-8', fmt=None, fps=None, overwrite=False):
        if not overwrite and os.path.exists(path):
            logger.info('file "%s" exists, generating new path', path)
            path = genUniquePath(path)

        logger.info('saving subtitles to "%s", %s', path,
                utils.fmtstr(enc=encoding, fmt=fmt, fps=fps))

        try:
            if fmt is None:
                ext = os.path.splitext(path)[1].lower()
                fmts = [ x['type'] for x in filetypes.subtitleTypes if x['ext'] == ext ]
                if len(fmts):
                    fmt = fmts[0]
            if fmt is None:
                raise Exception(_('Unknown file extension'))

            with open(path, 'w', encoding=encoding, errors='replace') as fp:
                super().to_file(fp, format_=fmt, fps=fps)

        except Exception as e:
            raise error.Error(_('Can\'t save subtitle file') + '. ' + str(e)) \
                    .add('path', path) \
                    .add('encoding', encoding) \
                    .addn('format', fmt) \
                    .addn('fps', fps)

        return path
Exemplo n.º 2
0
def _formatPattern(pattern, formatter):
    try:
        return pattern.format(
            **formatter, **{
                'if': ConditionalFormatter(formatter),
                'if_not': ConditionalFormatter(formatter, inverted=True)
            })

    except KeyError as e:
        raise Error(_('Invalid output pattern, invalid keyword: {}').format(e),
                    pattern=pattern)

    except Exception as e:
        raise Error(_('Invalid output pattern, {}').format(e), pattern=pattern)
Exemplo n.º 3
0
def loadDictionary(langKey, langVal, minLen=0):
    langKeyInfo = languages.get(code3=langKey)
    langValInfo = languages.get(code3=langVal)

    minKeyLen = langKeyInfo.ngrams or minLen
    minValLen = langValInfo.ngrams or minLen

    dictionary = gizmo.Dictionary()

    def addEntry(key, val):
        if len(key) >= minKeyLen and len(val) >= minValLen:
            if langKeyInfo.rightToLeft: key = key[::-1]
            if langValInfo.rightToLeft: val = val[::-1]
            for k in splitNgrams(key, langKeyInfo.ngrams):
                for v in splitNgrams(val, langValInfo.ngrams):
                    dictionary.add(k.lower(), v)

    asset = assets.getAsset('dict', (langKey, langVal))
    if asset.localVersion():
        for key, val in asset.readDictionary():
            addEntry(key, val)
    else:
        asset = assets.getAsset('dict', (langVal, langKey))
        if asset.localVersion():
            for key, val in asset.readDictionary():
                addEntry(val, key)

    if not asset.localVersion():
        raise Error(_('There is no dictionary for transaltion from {} to {}') \
                .format(langKey, langVal)) \
                .add('language1', langKey) \
                .add('language2', langVal)

    logger.info('dictionary ready with %u entries', dictionary.size())
    return dictionary
Exemplo n.º 4
0
def timeStampApproxFmt(time):
    try:
        h = time / 3600
        if h >= 0.9:
            return _('{} hours').format(round(max(h, 1)))
        m = time / 60
        if m <= 1:
            return _('less than minute')
        if m >= 15:
            m = round(m / 5) * 5
        else:
            m = round(m)
        if m == 1:
            return _('1 minute')
        else:
            return _('{} minutes').format(m)
    except:
        return '-'
Exemplo n.º 5
0
    def run(self):
        """Start updater (asynchronously)."""
        if self.isRunning():
            raise RuntimeError(_('Another update in progress'))

        self._exception = None
        self._updated = False
        self._thread = threading.Thread(target=self._run,
                                        name='AssetListUpdater')
        self._thread.start()
Exemplo n.º 6
0
    def validate(self, localOnly=False):
        """Check if all assets on the list are available.

        Parameters
        ----------
        localOnly: bool, optional
            If `True` this method will check if all assets are installed
            locally, otherwise it will check if assets are available either
            locally or on asset server.

        Raises
        ------
        Error
            At least one asset is not available.
        """
        if localOnly:
            assets = self.notInstalled()
        else:
            assets = self.missing()

        if assets:
            msg = []
            speech = [asset for asset in assets if asset.type == 'speech']
            dicts = [asset for asset in assets if asset.type == 'dict']

            if speech:
                langs = ', '.join(
                    [languages.getName(a.params[0]) for a in speech])
                msg += [ _('Synchronization with {} audio is currently not supported.') \
                        .format(langs) ]
            if dicts:
                langs = [
                    ' - '.join([languages.getName(p) for p in a.params])
                    for a in dicts
                ]
                msg += [ _('Synchronization between languages {} is currently not supported.') \
                        .format(', '.join(langs)) ]

            msg += ['', _('missing assets:')]
            msg += [' - ' + asset.getPrettyName() for asset in assets]
            raise error.Error('\n'.join(msg))
Exemplo n.º 7
0
def getSpeechAudioFormat(speechModel):
    try:
        sampleFormat = getattr(gizmo.AVSampleFormat,
                               speechModel.get('sampleformat', 'S16'))

        sampleRate = speechModel.get('samplerate', 16000)
        if type(sampleRate) == str:
            sampleRate = int(sampleRate)

        return gizmo.AudioFormat(sampleFormat, sampleRate, 1)
    except:
        raise error.Error(_('Invalid speech audio format'))
Exemplo n.º 8
0
def loadSpeechModel(lang):
    logger.info('loading speech recognition model for language %s', lang)

    asset = assets.getAsset('speech', [lang])
    if asset.localVersion():
        model = asset.readSpeechModel()
        logger.debug('model ready: %s', model)
        return model

    raise error.Error(
        _('There is no speech recognition model for language {}').format(
            lang)).add('language', lang)
Exemplo n.º 9
0
    def _run(self, timeout):
        try:
            remote = self._asset._getRemoteData()
            url = remote.get('url')

            for key in ['url', 'sig', 'type']:
                if not isinstance(remote.get(key), str):
                    logger.warning('invalid asset remote data %r', remote)
                    return

            with tempfile.TemporaryFile() as fp:
                hash = self._download(fp, url, remote.get('size'), timeout)

                if not self._terminated:
                    try:
                        self._verify(fp, remote.get('sig'), hash)
                    except:
                        raise Error(_('Signature verification failed'),
                                    asset=self._asset.getId(),
                                    url=url)

                if not self._terminated:
                    try:
                        self._asset._removeLocalData()
                        self._install(fp, remote.get('type'))
                    except Exception:
                        self._asset._removeLocalData()
                        raise Error(_('Asset installation failed'),
                                    asset=self._asset.getId(),
                                    url=url)

        except:
            e = sys.exc_info()
            self._exception = e
            logger.error('updater failed', exc_info=True)

        finally:
            with self._lock:
                for onEnd in self._onEnd:
                    onEnd(self._asset, self._terminated, self._exception)
Exemplo n.º 10
0
 def selectBy(self, type=None, lang=None):
     """Select stream by type and language (or only one of them)."""
     for s in self.streams.values():
         if self.types and s.type not in self.types:
             continue
         if type and not s.type.startswith(type):
             continue
         if lang and lang != s.lang.lower():
             continue
         return self.select(s.no)
     raise Error(_('There is no matching stream in {}').format(self.path)) \
             .addn('path', self.path) \
             .addn('type', type) \
             .addn('language', lang)
Exemplo n.º 11
0
    def load(self):
        try:
            if os.path.isfile(config.configpath):
                with open(config.configpath, encoding='utf8') as fp:
                    cfg = json.load(fp)
                    logger.info('configuration loaded from %s',
                                config.configpath)
                    logger.debug('configuration: %r', cfg)

                    dirty = self.dirty
                    self.set(temp=False, **{**persistent, **cfg})
                    self.dirty = dirty
        except Exception as err:
            raise Error(_('Cannot load settings file, {}').format(err),
                        path=config.configpath)
Exemplo n.º 12
0
    def install(self):
        """Run local installer.

        Application must be terminated immediately to let installer work.
        """
        with self._lock:
            try:
                self.installerVersion()
                instPath = os.path.join(self.localDir, self._getLocalData().get('install'))
                logger.info('executing installer %s', instPath)
                mode = os.stat(instPath).st_mode
                if (mode & stat.S_IEXEC) == 0:
                    os.chmod(instPath, mode | stat.S_IEXEC)
                subprocess.Popen(instPath, cwd=self.localDir)

            except:
                logger.error('cannot install update %s', self.path, exc_info=True)
                self._removeLocalData()
                raise Error(_('Update instalation failed miserably'))
Exemplo n.º 13
0
def validateTask(task, outputRequired=False):
    sub, ref, out = task.sub, task.ref, task.out
    if sub is None or not sub.path or sub.no is None:
        raise Error(_('Subtitles not set'), task=task)
    if ref is None or not ref.path or ref.no is None:
        raise Error(_('Reference file not set'), task=task)
    if outputRequired and (not out or not out.path):
        raise Error(_('Output file not set'), task=task)
    if sub.path == ref.path and sub.no == ref.no:
        raise Error(_('Subtitles can\'t be the same as reference'), task=task)
    if ref.type == 'audio' and not ref.lang:
        raise Error(_('Select reference language first'), task=task)
    if out and out.path:
        try:
            out.validateOutputPattern()
        except:
            raise Error(_('Invalid output pattern'), task=task)
Exemplo n.º 14
0
from subsync.translations import _

maxDistInfo = _(
    '''Max adjustement. Subtitle time will be changed no more than this value. Higher value will result in longer synchronization, but if you set this too low, synchronization will fail.'''
)

effortInfo = _(
    '''How hard should I work to synchronize subtitles. Higher value will result with better synchronization, but it will take longer.'''
)

noLanguageSelectedQuestion = _(
    '''Langauge selection is not mandatory, but could drastically improve synchronization accuracy.
Are you sure?''')

### SettingsWin ###

maxPointDistInfo = _(
    '''Maximum acceptable synchronization error, in seconds. Synchronization points with error greater than this will be discarded.'''
)

minPointsNoInfo = _(
    '''Minumum number of synchronization points. Should not be set too high because it could result with generating large number of false positives.'''
)

minWordLenInfo = _(
    '''Minimum word length, in letters. Shorter words will not be used as synchronization points. Applies only to alphabet-based languages.'''
)

minWordSimInfo = _(
    '''Minimum words similarity for synchronization points. Between 0.0 and 1.0.'''
)
Exemplo n.º 15
0
 def getPrettyName(self):
     return _('dictionary {} / {}').format(
             languages.getName(self.params[0]),
             languages.getName(self.params[1]))
Exemplo n.º 16
0
 def getPrettyName(self):
     return _('{} speech recognition model').format(
             languages.getName(self.params[0]))
Exemplo n.º 17
0
 def getPrettyName(self):
     return _('Application upgrade')
Exemplo n.º 18
0
 def getDescription(self):
     return _('auto')
Exemplo n.º 19
0
 def getDescription(self):
     return _('all channels')
Exemplo n.º 20
0
])


def mkLanguage(code3=None,
               code2=None,
               name=None,
               encodings=[],
               rightToLeft=False,
               ngrams=None,
               extraCodes=None):
    return LanguageInfo(code3, code2, name, encodings, rightToLeft, ngrams,
                        extraCodes)


languages = [
    mkLanguage('alb', 'sq', _('Albanian'), ['Windows-1252', 'ISO-8859-1']),
    mkLanguage('ara',
               'ar',
               _('Arabic'), ['ISO-8859-6', 'Windows-1256'],
               rightToLeft=True),
    mkLanguage('bel', 'be', _('Belarusian'), ['ISO-8859-5']),
    mkLanguage('bul', 'bg', _('Bulgarian'), ['Windows-1251', 'ISO-8859-5']),
    mkLanguage('cat', 'ca', _('Catalan'), ['Windows-1252', 'ISO-8859-1']),
    mkLanguage('chi',
               'zh',
               _('Chinese'), ['GB18030'],
               ngrams=2,
               extraCodes=['cht', 'chs', 'cmn']),
    mkLanguage('cze', 'cs', _('Czech'), ['Windows-1250', 'ISO-8859-2']),
    mkLanguage('dan', 'da', _('Danish'), ['Windows-1252', 'ISO-8859-1']),
    mkLanguage('dut',