<commit_before>import os
import logging
import shelve
from .aur import AurPackage, NotInAURException
from .common import DATA_DIR
from .package import Package

logger = logging.getLogger(__name__)


class PackageNotInRepositoryException(Exception):
    pass


class Repository:
    def __init__(self, dir):
        self.dir = dir
        if not os.path.isdir(self.dir):
            os.mkdir(self.dir)

        self._open_packages = {}
        self.db = shelve.open(os.path.join(self.dir, 'types.db'))

    def __del__(self):
        self.db.close()

    def __repr__(self):
        return '<Repository("{}")>'.format(self.dir)

    def package(self, name, type="default"):
        if name not in self._open_packages:
            if name in self.db:
                type = self.db[name]
            if type == "aur":
                package = AurPackage(name, self)
            elif type == "manual":
                package = Package(name, self)
            elif type == "default":
                try:
                    package = AurPackage(name, self)
                    type = "aur"
                except NotInAURException as e:
                    raise PackageNotInRepositoryException() from e
            else:
                raise ValueError("Unsupported value for argument type",
                                 type)
            logger.debug('Creating a Package object of type %s', type)
            self.db[name] = type
            self._open_packages[name] = package
        return self._open_packages[name]
#TODO : treat dev packages separately

def default_repository():
    return Repository(DATA_DIR)<commit_msg>Warn gently when an installed package disapeared from AUR instead of crashing.<commit_after>import os
import logging
import shelve
from .aur import AurPackage, NotInAURException
from .common import DATA_DIR
from .package import Package

logger = logging.getLogger(__name__)


class PackageNotInRepositoryException(Exception):
    pass


class Repository:
    def __init__(self, dir):
        self.dir = dir
        if not os.path.isdir(self.dir):
            os.mkdir(self.dir)

        self._open_packages = {}
        self.db = shelve.open(os.path.join(self.dir, 'types.db'))

    def __del__(self):
        self.db.close()

    def __repr__(self):
        return '<Repository("{}")>'.format(self.dir)

    def package(self, name, type="default"):
        if name not in self._open_packages:
            if name in self.db:
                type = self.db[name]
            if type == "aur":
                try:
                    package = AurPackage(name, self)
                except NotInAURException:
                    package = Package(name, self)
                    logger.warn('Package %s used to be in AUR but is not any '
                                'more. You may want to find an alternative. '
                                'To remove this message, delete the folder %s .'
                        %(name, package.dir))
            elif type == "manual":
                package = Package(name, self)
            elif type == "default":
                try:
                    package = AurPackage(name, self)
                    type = "aur"
                except NotInAURException as e:
                    raise PackageNotInRepositoryException() from e
            else:
                raise ValueError("Unsupported value for argument type",
                                 type)
            logger.debug('Creating a Package object of type %s', type)
            self.db[name] = type
            self._open_packages[name] = package
        return self._open_packages[name]
#TODO : treat dev packages separately

def default_repository():
    return Repository(DATA_DIR)
{'fraction_non_alphanumeric': 0.0897642015005359, 'fraction_numerical': 0.0, 'mean_word_length': 1.78790141896938, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5793725', 'n_tokens_mistral': 994, 'n_tokens_neox': 949, 'n_words': 287}