Пример #1
0
def cli_base(verbose, debug):
    mk_folder()
    # only the first basicConfig() is respected.
    if debug:
        logging.basicConfig(level=logging.DEBUG)
    if verbose:
        logging.basicConfig(level=logging.INFO)
Пример #2
0
def convert(target_brain: str, tag: str) -> None:
    "move your brain to the new central location"
    mk_folder()
    if tag is None:
        tag_name = datetime.datetime.now().strftime("pyborg-%m-%d-%y-import-archive")
    else:
        tag_name = tag
    output = os.path.join(folder, "brains", "{}.zip".format(tag_name))
    shutil.copy2(target_brain, output)
    print("Imported your archive.zip as {}".format(output))
Пример #3
0
def cli_base(ctx, verbose, debug, my_version):
    mk_folder()
    # only the first basicConfig() is respected.
    if debug:
        logging.basicConfig(level=logging.DEBUG)
    if verbose:
        logging.basicConfig(level=logging.INFO)
    if my_version:
        # Skip directly to version command like the user would expect a gnu program to.
        ctx.invoke(version)
        ctx.exit()
    elif ctx.invoked_subcommand is None:
        # still do normal things via the named help even
        ctx.invoke(help)
Пример #4
0
def cli_base(ctx: click.Context, verbose: bool, debug: bool, my_version: bool) -> None:
    """Pyborg is a markov chain bot for IRC
    (and other services [even at the same time with the same database])
    that generates replies based on messages and it’s database"""
    mk_folder()
    # only the first basicConfig() is respected.
    if debug:
        logging.basicConfig(level=logging.DEBUG)
    if verbose:
        logging.basicConfig(level=logging.INFO)
    if my_version:
        # Skip directly to version command like the user would expect a gnu program to.
        ctx.invoke(version)
        ctx.exit()
    elif ctx.invoked_subcommand is None:
        # still do normal things via the named help even
        ctx.invoke(local_help)
Пример #5
0
    def __init__(self, brain=None):
        """
        Open the dictionary. Resize as required.
        """

        self.settings = self.load_settings()
        self.answers = FakeAns()
        self.unfilterd = {}
        mk_folder()

        # Read the dictionary
        logger.info("Reading dictionary...")
        if brain is None:
            self.brain_path = 'archive.zip'
        else:
            self.brain_path = brain
        try:
            self.words, self.lines = self.load_brain_json(self.brain_path)
        except (EOFError, IOError) as e:
            # Create mew database
            self.words = {}
            self.lines = {}
            logger.error(e)
            folder = click.get_app_dir("Pyborg")
            name = datetime.datetime.now().strftime("%m-%d-%y-auto-{}.pyborg.json").format(str(uuid.uuid4())[:4])
            self.brain_path = os.path.join(folder, "brains", name)
            logger.info("Error reading saves. New database created.")

        # Is a resizing required?
        if len(self.words) != self.settings.num_words:
            logger.info("Updating dictionary information...")
            self.settings.num_words = len(self.words)
            num_contexts = 0
            # Get number of contexts
            for x in self.lines.keys():
                num_contexts += len(self.lines[x][0].split())
            self.settings.num_contexts = num_contexts
            # Save new values
            self.settings.save()

        # Is an aliases update required ?
        compteur = 0
        for x in self.settings.aliases.keys():
            compteur += len(self.settings.aliases[x])
        if compteur != self.settings.num_aliases:
            logger.info("check dictionary for new aliases")
            self.settings.num_aliases = compteur

            for x in self.words.keys():
                # is there aliases ?
                if x[0] != '~':
                    for z in self.settings.aliases.keys():
                        for alias in self.settings.aliases[z]:
                            pattern = "^%s$" % alias
                            if self.re.search(pattern, x):
                                logger.info("replace %s with %s" % (x, z))
                                self.replace(x, z)

            for x in self.words.keys():
                if not (x in self.settings.aliases.keys()) and x[0] == '~':
                    logger.info("unlearn %s" % x)
                    self.settings.num_aliases -= 1
                    self.unlearn(x)
                    logger.info("unlearned aliases %s" % x)

        # unlearn words in the unlearn.txt file.
        try:
            with open("unlearn.txt", 'r') as f:
                for line in f.readlines():
                    self.unlearn(line)
        except (EOFError, IOError) as e:
            logger.debug("No words to unlearn", exc_info=e)
        self.settings.save()