Exemplo n.º 1
0
    def __init__(self, connection, name):
        """Get a database by connection and name.

        Raises TypeError if name is not an instance of (str, unicode). Raises
        InvalidName if name is not a valid database name.

        :Parameters:
          - `connection`: a connection to Mongo
          - `name`: database name
        """
        if not isinstance(name, types.StringTypes):
            raise TypeError("name must be an instance of (str, unicode)")

        self.__check_name(name)

        self.__name = unicode(name)
        self.__connection = connection
        # TODO remove the callable_value wrappers after deprecation is complete
        self.__name_w = helpers.callable_value(self.__name, "Database.name")
        self.__connection_w = helpers.callable_value(self.__connection, "Database.connection")

        self.__incoming_manipulators = []
        self.__incoming_copying_manipulators = []
        self.__outgoing_manipulators = []
        self.__outgoing_copying_manipulators = []
        self.add_son_manipulator(ObjectIdInjector())
Exemplo n.º 2
0
    def port(self):
        """Current connected port.

        .. versionchanged:: 1.3
           ``port`` is now a property rather than a method. The ``port()``
           method is deprecated.
        """
        return helpers.callable_value(self.__port, "Connection.port")
Exemplo n.º 3
0
    def __init__(self, database, name, options=None):
        """Get / create a Mongo collection.

        Raises TypeError if name is not an instance of (str, unicode). Raises
        InvalidName if name is not a valid collection name. Raises TypeError if
        options is not an instance of dict. If options is non-empty a create
        command will be sent to the database. Otherwise the collection will be
        created implicitly on first use.

        :Parameters:
          - `database`: the database to get a collection from
          - `name`: the name of the collection to get
          - `options`: dictionary of collection options.
            see `pymongo.database.Database.create_collection` for details.
        """
        if not isinstance(name, types.StringTypes):
            raise TypeError("name must be an instance of (str, unicode)")

        if not isinstance(options, (types.DictType, types.NoneType)):
            raise TypeError("options must be an instance of dict")

        if not name or ".." in name:
            raise InvalidName("collection names cannot be empty")
        if "$" in name and not (name.startswith("oplog.$main") or
                                name.startswith("$cmd")):
            raise InvalidName("collection names must not "
                              "contain '$': %r" % name)
        if name[0] == "." or name[-1] == ".":
            raise InvalidName("collecion names must not start "
                              "or end with '.': %r" % name)

        self.__database = database
        self.__name = unicode(name)
        self.__full_name = u"%s.%s" % (self.__database.name, self.__name)
        # TODO remove the callable_value wrappers after deprecation is complete
        self.__database_w = helpers.callable_value(self.__database,
                                                   "Collection.database")
        self.__name_w = helpers.callable_value(self.__name,
                                               "Collection.name")
        self.__full_name_w = helpers.callable_value(self.__full_name,
                                                    "Collection.full_name")
        if options is not None:
            self.__create(options)