Пример #1
0
    def __init__(self, database, collection="fs"):
        """Create a new instance of :class:`GridFS`.

        Raises :class:`TypeError` if `database` is not an instance of
        :class:`~pymongo.database.Database`.

        :Parameters:
          - `database`: database to use
          - `collection` (optional): root collection to use

        .. note::

            Instantiating a GridFS object will implicitly create it indexes.
            This could leads to errors if the underlaying connection is closed
            before the indexes creation request has returned. To avoid this you
            should use the defer returned by :meth:`GridFS.indexes_created`.

        .. versionadded:: 1.6
           The `collection` parameter.
        """
        if not isinstance(database, Database):
            raise TypeError(
                "TxMongo: database must be an instance of Database.")

        self.__database = database
        self.__collection = database[collection]
        self.__files = self.__collection.files
        self.__chunks = self.__collection.chunks
        self.__indexes_created_defer = defer.DeferredList([
            self.__files.create_index(
                filter.sort(ASCENDING("filename") + ASCENDING("uploadDate"))),
            self.__chunks.create_index(
                filter.sort(ASCENDING("files_id") + ASCENDING("n")),
                unique=True)
        ])
Пример #2
0
class UserSubscriptions(db.MongoObject):
    """User's url subscriptions.
    User identified by his jid.
    """

    collection_name = "users_subscriptions"
    indexes = (
        (ASCENDING(("jid", "url")), True),
        (ASCENDING("url"), False),
    )

    def __init__(self, jid):
        self._jid = jid

    @defer.inlineCallbacks
    def is_subscribed(self, url):
        """Is user subscribed to this url."""
        res = yield self._db.find_one(
            {"jid": self._jid, "url": url},
            fields=[])
        defer.returnValue(bool(res))

    def subscribe(self, url):
        return self._db.insert(
            {"jid": self._jid, "url": url})

    def unsubscribe(self, url):
        return self._db.remove(
            {"jid": self._jid, "url": url})

    @classmethod
    def unsubscribe_all(cls, url):
        return cls._db.remove(
            {"url": url})

    def get_list(self):
        """Return user's subscriptions."""
        return self._db.find(
            {"jid": self._jid},
            fields=["url"])

    def count(self):
        """Return user's subscriptions count."""
        return self._db.count(
            {"jid": self._jid})

    @classmethod
    def find(cls, url):
        """Return users subscribed to given url."""
        return cls._db.find(
            {"url": url},
            fields=["jid"])
Пример #3
0
 def __init__(self, host, port, db, queue_col, page_col, jobid):
     store = MongoConnection(host, port)[db]
     self.jobid = jobid
     self.pageStore = store[page_col]
     self.queueStore = store[queue_col]
     self.queueStore.ensure_index(mongosort(ASCENDING('_job')),
                                  background=True)
Пример #4
0
    def get_last_version(self, filename):
        """Get a file from GridFS by ``"filename"``.

        Returns the most recently uploaded file in GridFS with the
        name `filename` as an instance of
        :class:`~gridfs.grid_file.GridOut`. Raises
        :class:`~gridfs.errors.NoFile` if no such file exists.

        An index on ``{filename: 1, uploadDate: -1}`` will
        automatically be created when this method is called the first
        time.

        :Parameters:
          - `filename`: ``"filename"`` of the file to get

        .. versionadded:: 1.6
        """
        self.__files.ensure_index(
            filter.sort(ASCENDING("filename") + DESCENDING("uploadDate")))

        doc = yield self.__files.find_one({"filename": filename},
                                          filter=filter.sort(
                                              DESCENDING("uploadDate")))
        if doc is None:
            raise NoFile("TxMongo: no file in gridfs with filename {0}".format(
                repr(filename)))

        defer.returnValue(GridOut(self.__collection, doc))
Пример #5
0
    def __init__(self, database, collection="fs"):
        """Create a new instance of :class:`GridFS`.

        Raises :class:`TypeError` if `database` is not an instance of
        :class:`~pymongo.database.Database`.

        :Parameters:
          - `database`: database to use
          - `collection` (optional): root collection to use

        .. versionadded:: 1.6
           The `collection` parameter.
        """
        if not isinstance(database, Database):
            raise TypeError(
                "TxMongo: database must be an instance of Database.")

        self.__database = database
        self.__collection = database[collection]
        self.__files = self.__collection.files
        self.__chunks = self.__collection.chunks
        self.__chunks.create_index(
            filter.sort(ASCENDING("files_id") + ASCENDING("n")), unique=True)
Пример #6
0
class UserSettings(db.MongoObject):
    
    collection_name = "users_settings"
    indexes = (
        (ASCENDING("jid"), True),
    )

    def __init__(self, jid):
        self._jid = jid

    @defer.inlineCallbacks
    def is_exists(self):
        try:
            yield self._db.insert({"jid": self._jid}, safe=True)
        except txmongo._pymongo.errors.OperationFailure:
            defer.returnValue(True)
        else:
            defer.returnValue(False)
Пример #7
0
    def get_version(self, filename=None, version=-1):
        """Get a file from GridFS by ``"filename"``.
        Returns a version of the file in GridFS whose filename matches
        `filename` and whose metadata fields match the supplied keyword
        arguments, as an instance of :class:`~gridfs.grid_file.GridOut`.
        Version numbering is a convenience atop the GridFS API provided
        by MongoDB. If more than one file matches the query (either by
        `filename` alone, by metadata fields, or by a combination of
        both), then version ``-1`` will be the most recently uploaded
        matching file, ``-2`` the second most recently
        uploaded, etc. Version ``0`` will be the first version
        uploaded, ``1`` the second version, etc. So if three versions
        have been uploaded, then version ``0`` is the same as version
        ``-3``, version ``1`` is the same as version ``-2``, and
        version ``2`` is the same as version ``-1``. Note that searching by
        random (unindexed) meta data is not supported here.
        Raises :class:`~gridfs.errors.NoFile` if no such version of
        that file exists.
        :Parameters:
          - `filename`: ``"filename"`` of the file to get, or `None`
          - `version` (optional): version of the file to get (defaults
            to -1, the most recent version uploaded)
        """
        query = {"filename": filename}
        skip = abs(version)
        if version < 0:
            skip -= 1
            myorder = DESCENDING("uploadDate")
        else:
            myorder = ASCENDING("uploadDate")

        def ok(cursor):
            if cursor:
                return GridOut(self.__collection, cursor[0])

            raise NoFile("no version %d for filename %r" % (version, filename))

        return self.__files.find(query, filter=filter.sort(myorder), limit=1, skip=skip)\
            .addCallback(ok)
Пример #8
0
    def get_last_version(self, filename):
        """Get a file from GridFS by ``"filename"``.

        Returns the most recently uploaded file in GridFS with the
        name `filename` as an instance of
        :class:`~gridfs.grid_file.GridOut`. Raises
        :class:`~gridfs.errors.NoFile` if no such file exists.

        An index on ``{filename: 1, uploadDate: -1}`` will
        automatically be created when this method is called the first
        time.

        :Parameters:
          - `filename`: ``"filename"`` of the file to get

        .. versionadded:: 1.6
        """
        self.__files.ensure_index(filter.sort(ASCENDING("filename") + \
                                   DESCENDING("uploadDate")))

        d = self.__files.find({"filename": filename},
                                  filter=filter.sort(DESCENDING('uploadDate')))
        d.addCallback(self._cb_get_last_version, filename)
        return d
Пример #9
0
class Host(db.MongoObject):
    """Host settings."""

    collection_name = "hosts"
    indexes = (
        (ASCENDING("host"), True),
    )

    def __init__(self, host):
        self._host = host

    @defer.inlineCallbacks
    def inc_errors(self):
        """Increment host errors and return
        errors count.
        """
        yield self._db.update(
            {"host": self._host},
            {"$inc": {"errors": 1}},
            upsert=True)
        res = yield self._db.find_one(
            {"host": self._host},
            fields=["errors"])
        defer.returnValue(res["errors"])
Пример #10
0
def sortasc(field):
    return mongosort(ASCENDING(field))
Пример #11
0
class Subscription(db.MongoObject):
    """List of all users subscriptions.
    Url should be unique field.
    Fetchers and parsers use this class
    for processing subscriptions.
    """

    collection_name = "subscriptions"
    indexes = (
        (ASCENDING("url"), True),
        (ASCENDING("jid"), False),
    )

    def __init__(self, url):
        self._url = url

    @classmethod
    def create(cls, sub):
        return cls._db.insert(sub)

    def remove(self):
        return self._db.remove(
            {"url": self._url})

    @defer.inlineCallbacks
    def remove_empty(self):
        """Remove subscription if it has no
        subscribers.
        """
        coll_name = UserSubscriptions.collection_name
        subscriber_exists = yield self._all_db(coll_name).find_one(
            {"url": self._url},
            fields=[])
        if not subscriber_exists:
            yield self._db.remove({"url": self._url})

    @classmethod
    def get_list(cls):
        """All subscriptions."""
        return cls._db.find()

    @defer.inlineCallbacks
    def get_last_modified(self):
        res = yield self._db.find_one(
            {"url": self._url},
            fields=["last_modified"])
        if "last_modified" in res:
            defer.returnValue(res["last_modified"])

    def set_last_modified(self, last_modified):
        return self._db.update(
            {"url": self._url},
            {"$set": {"last_modified": last_modified}})

    @defer.inlineCallbacks
    def get_last(self):
        res = yield self._db.find_one(
            {"url": self._url},
            fields=["last"])
        if res:
            defer.returnValue(res["last"])

    def set_last(self, last):
        return self._db.update(
            {"url": self._url},
            {"$set": {"last": last}})

    @defer.inlineCallbacks
    def get_jid(self):
        res = yield self._db.find_one(
            {"url": self._url},
            fields=["jid"])
        if res:
            defer.returnValue(res["jid"])

    @classmethod
    @defer.inlineCallbacks
    def get_url_by_jid(cls, jid):
        res = yield cls._db.find_one(
            {"jid": jid},
            fields=["url"])
        if res:
            defer.returnValue(res["url"])