Ejemplo n.º 1
0
    def writeSubtitles(self, convertedSubtitles, filePath, encoding):
        try:
            file_ = File(filePath)
        except:
            # File doesn't exist, we can safely write to it
            File.write(filePath, convertedSubtitles, encoding)
            log.info(_("File %s saved.") % filePath)
        else:
            # A little hack to ensure that translator won't make a mistake
            choices = { 'yes': _('y'), 'no': _('n'), 'quit': _('q'), 'backup': _('b') }
            choice = ''

            if self._args.force:
                choice = choices["yes"]
            while(choice not in choices.values()):
                vals = {
                    "file": filePath, "yes": choices["yes"], "no": choices['no'],
                    "bck": choices['backup'], "quit": choices["quit"]
                }

                choice = input(
                    _("File '%(file)s' exists. Overwrite? [%(yes)s/%(no)s/%(bck)s/%(quit)s]") % 
                    vals)

            if choice == choices['backup']:
                backupFilePath = file_.backup()
                log.info(_("Backup: %s") % backupFilePath)
                log.info(_("Overwriting %s") % filePath)
                file_.overwrite(convertedSubtitles, encoding)
            elif choice == choices['no']:
                log.info(_("Skipping %s") % filePath)
                return
            elif choice == choices['yes']:
                log.info(_("Overwriting %s") % filePath)
                file_.overwrite(convertedSubtitles, encoding)
            elif choice == choices['quit']:
                log.info(_("Quitting converting work."))
                sys.exit(0)