def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError('Incorrect number of arguments')
        try:
            f = codecs.open(args[0], "r", "utf-8" )
        except IndexError:
            raise CommandError('No dictionary file specified')
        except IOError:
            raise CommandError('Dictionary file "%s" not found' % args[0])

        verbosity = int(options['verbosity'])

        self.numberOfImports = 0
        self.numberOfErrors = 0

        reader = WordReader(f)
        while True:
            try:
                word = reader.next()
                if '#' in word['untranslated']:
                    # ignore rows where the untranslated word has some # in it. These lines need to be fixed
                    raise Exception("Invalid characters ('#') in untranslated: %s" % word['untranslated'])
                validateBraille(word['braille'])
                try:
                    GlobalWord(**word).save()
                except IntegrityError:
                    raise Exception('Duplicate word "%s"' % word)
                self.numberOfImports += 1
            except StopIteration:
                break
            except Exception as e:
                self.error(e, reader.currentLine())

        f.close()
        if verbosity >= 1:
            self.log("\n%s words were successfully updated" % self.numberOfImports)
            if self.numberOfErrors > 0:
                self.log("%s errors" % self.numberOfErrors)
Beispiel #2
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError('Incorrect number of arguments')
        try:
            f = open(args[0])
        except IndexError:
            raise CommandError('No dictionary file specified')
        except IOError:
            raise CommandError('Dictionary file "%s" not found' % args[0])

        self.logger = codecs.getwriter(self.stdout.encoding if self.stdout.isatty() else 'utf-8')(self.stdout)

        verbosity = int(options['verbosity'])
        dry_run = options['dry_run']
        force = options['force']

        if not dry_run and not force:
            self.log("Warning: this action cannot be undone. Specify the --dry-run option to do a simulation first.")
            raw_input("Hit Enter to continue, or Ctrl-C to abort.")

        self.numberOfDeletes = 0
        self.numberOfErrors = 0

        if dry_run:
            verbosity = 2
        if verbosity >= 1:
            self.log("Deleting global words...")
            self.log("==================================================================================================================")
            self.log(columnize(("type", "grade", "untranslated", "braille"), (5,5,50,50)))
            self.log("==================================================================================================================")

        reader = WordReader(f)
        while True:
            try:
                deletion = reader.next()
                word = None
                try:
                    word = GlobalWord.objects.get(
                                untranslated=deletion['untranslated'],
                                braille=deletion['braille'],
                                grade=deletion['grade'],
                                type=deletion['type'],
                                homograph_disambiguation=deletion['homograph_disambiguation'])
                except ObjectDoesNotExist:
                    raise Exception("Word could not be found in the global dictionary: %s" % deletion)
                if verbosity >= 1:
                    self.log(columnize((inverseTypeMap[word.type],
                                        str(word.grade),
                                        word.untranslated,
                                        word.braille),
                                       (5,5,50,50)))
                if not dry_run:
                    word.delete()
                self.numberOfDeletes += 1
            except StopIteration:
                break
            except Exception as e:
                self.log(self.error(e, reader.currentLine()))

        f.close()
        if verbosity >= 1:
            self.log("==================================================================================================================")
            self.log("%s words were deleted" % self.numberOfDeletes)
            if self.numberOfErrors > 0:
                self.log("%s errors" % self.numberOfErrors)
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError("Incorrect number of arguments")
        try:
            f = open(args[0])
        except IndexError:
            raise CommandError("No dictionary file specified")
        except IOError:
            raise CommandError('Dictionary file "%s" not found' % args[0])

        self.logger = codecs.getwriter(self.stdout.encoding if self.stdout.isatty() else "utf-8")(self.stdout)

        verbosity = int(options["verbosity"])
        dry_run = options["dry_run"]
        force = options["force"]

        if not dry_run and not force:
            self.log("Warning: this action cannot be undone. Specify the --dry-run option to do a simulation first.")
            raw_input("Hit Enter to continue, or Ctrl-C to abort.")

        self.numberOfUpdates = 0
        self.numberOfInserts = 0
        self.numberOfErrors = 0
        self.numberOfWarnings = 0

        if dry_run:
            verbosity = 2
        if verbosity >= 1:
            self.log("Adding words to temporary table...")
        clearTempWords()
        reader = WordReader(f)
        while True:
            try:
                word = reader.next()
                validateBraille(word["braille"])
                insertTempWord(word)
            except StopIteration:
                break
            except Exception as e:
                self.log(self.error(e, reader.currentLine()))

        if verbosity >= 1:
            self.log("Updating global words...")
            self.log(
                "=================================================================================================================="
            )
            self.log(columnize(("type", "grade", "untranslated", "braille"), (5, 5, 50, 50)))
            self.log(
                "=================================================================================================================="
            )

        for change in changedOrNewWords():
            try:
                word = None
                try:
                    word = getGlobalWord(change)
                except:
                    pass
                if word != None:
                    if verbosity >= 1:
                        warning = None
                        if change["type"] != word.type:
                            warning = self.warning("Type changed")
                        else:
                            try:
                                compareBraille(change["braille"], word.braille)
                            except Exception as e:
                                warning = self.warning("Significant change")
                        self.log(
                            columnize(
                                (
                                    inverseTypeMap[word.type]
                                    if change["type"] == word.type
                                    else color.DELETED
                                    + inverseTypeMap[word.type]
                                    + color.END
                                    + ".."
                                    + color.INSERTED
                                    + inverseTypeMap[change["type"]]
                                    + color.END,
                                    str(word.grade),
                                    word.untranslated,
                                    colorDiff(
                                        word.braille,
                                        change["braille"],
                                        (color.DELETED, color.END),
                                        (color.INSERTED, color.END),
                                    ),
                                ),
                                (5, 5, 50, 50),
                            )
                            + ((" # %s" % warning) if warning != None else "")
                        )
                    if not dry_run:
                        word.braille = change["braille"]
                        word.type = change["type"]
                        word.save()
                    self.numberOfUpdates += 1
                else:
                    if verbosity >= 1:
                        self.log(
                            color.INSERTED
                            + columnize(
                                (
                                    inverseTypeMap[change["type"]],
                                    str(change["grade"]),
                                    change["untranslated"],
                                    change["braille"],
                                ),
                                (5, 5, 50, 50),
                            )
                            + color.END
                            + " # %s" % self.warning("New word")
                        )
                    if not dry_run:
                        GlobalWord(**change).save()
                    self.numberOfInserts += 1
            except Exception as e:
                self.log(self.error(e))

        f.close()
        if verbosity >= 1:
            self.log(
                "=================================================================================================================="
            )
            self.log("%s words were successfully updated" % self.numberOfUpdates)
            self.log("%s words were added" % self.numberOfInserts)
            if self.numberOfErrors > 0:
                self.log("%s errors" % self.numberOfErrors)
            if self.numberOfWarnings > 0:
                self.log("%s warnings" % self.numberOfWarnings)
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError('Incorrect number of arguments')
        try:
            f = open(args[0])
        except IndexError:
            raise CommandError('No dictionary file specified')
        except IOError:
            raise CommandError('Dictionary file "%s" not found' % args[0])
        
        verbosity = int(options['verbosity'])
        dry_run = options['dry_run']
        if not dry_run:
            self.log("Warning: this action cannot be undone. Specify the --dry-run option to do a simulation first.")
            raw_input("Hit Enter to continue, or Ctrl-C to abort.")
        
        self.numberOfUpdates = 0
        self.numberOfErrors = 0
        self.numberOfWarnings = 0

        if verbosity >= 1:
            self.log("Adding words to temporary table...")
        clearTempWords()
        reader = WordReader(f)
        while True:
            try:
                word = reader.next()
                validateBraille(word['braille'])
                insertTempWord(word)
            except StopIteration:
                break
            except Exception as e:
                self.error(e, reader.currentLine())

        if verbosity >= 1:
            self.log("Updating global words...")
        if dry_run:
            self.columnize(("type", "grade", "untranslated", "braille"), (4,5,50,50))
            self.columnize(("----", "-----", "------------", "-------"), (4,5,50,50))

        for change in changedWords():
            try:
                word = getGlobalWord(change)
                if dry_run:
                    self.columnize((inverseTypeMap[word.type], word.grade, word.untranslated,
                                    colorDiff(word.braille, change['braille'], (color.DELETED, color.END), (color.INSERTED, color.END))),
                                   (4,5,50,50))
                try:
                    compareBraille(change['braille'], word.braille)
                except Exception as e:
                    self.warning(e)
                if not dry_run:
                    word.braille = change['braille']
                    word.save()
                self.numberOfUpdates += 1
            except Exception as e:
                self.error(e)

        f.close()
        if verbosity >= 1:
            self.log("\n%s words were successfully updated" % self.numberOfUpdates)
            if self.numberOfErrors > 0:
                self.log("%s errors" % self.numberOfErrors)
            if self.numberOfWarnings > 0:
                self.log("%s warnings" % self.numberOfWarnings)