def connect(self, host=None, database=0, unix_socket=False, password=None): """Returns a connection to a Redis database. If *host* is None, it will fall back to the settings in the config and ignore the rest of the arguments. It will also share the connection across any plugins using the default configuration. Passing *host* will make it connect to a specific database that is not shared at all. Subsequent calls to this will return the connection initialized the first call unless it has been closed. :param host: The host name. If no port is specified, it will use 6379. Ex.: ``localhost:1234``. :type host: str :param database: The database number that should be used. :type database: int :param unix_socket: Whether or not *host* should be interpreted as a unix socket path. :type unix_socket: bool :raises: RuntimeError """ if not host and not self._conn: # Resort to default settings in config? if not Redis._conn: conf = minqlx.get_config() if not "Redis" in conf or not "Host" in conf["Redis"] or not "Database" in conf["Redis"]: raise RuntimeError("No host and/or database number passed nor set in config.") elif "UseUnixSocket" in conf["Redis"] and conf.getboolean("Redis", "UseUnixSocket"): db = int(conf["Redis"]["Database"]) if "Password" in conf["Redis"] and conf["Redis"]["Password"]: Redis._pass = conf["Redis"]["Password"] Redis._conn = redis.StrictRedis(unix_socket_path=conf["Redis"]["Host"], db=db, password=Redis._pass, decode_responses=True) else: host = conf["Redis"]["Host"] split_host = host.split(":") if len(split_host) > 1: port = int(split_host[1]) else: port = 6379 # Default port. db = int(conf["Redis"]["Database"]) if "Password" in conf["Redis"] and conf["Redis"]["Password"]: Redis._pass = conf["Redis"]["Password"] Redis._pool = redis.ConnectionPool(host=split_host[0], port=port, db=db, password=Redis._pass) Redis._conn = redis.StrictRedis(connection_pool=Redis._pool, decode_responses=True) # TODO: Why does self._conn get set when doing Redis._conn? self._conn = None return Redis._conn elif not self._conn: split_host = host.split(":") if len(split_host) > 1: port = int(split_host[1]) else: port = 6379 # Default port. if unix_socket: self._conn = redis.StrictRedis(unix_socket_path=host, db=db, password=password, decode_responses=True) else: self._pool = redis.ConnectionPool(host=split_host[0], port=port, db=db, password=password) self._conn = redis.StrictRedis(connection_pool=self._pool, decode_responses=True) return self._conn
def is_eligible_player(self, player, is_client_cmd): """Check if a player has the rights to execute the command.""" # Check if config overrides permission. conf = minqlx.get_config() perm = self.permission client_cmd_perm = self.client_cmd_perm perm_key = "perm_" + self.name[0] client_cmd_perm_key = "client_cmd_perm_" + self.name[0] if self.plugin.name in conf: if perm_key in conf[self.plugin.name]: perm = int(conf[self.plugin.name][perm_key]) if is_client_cmd and client_cmd_perm_key in conf[self.plugin.name]: client_cmd_perm = int(conf[self.plugin.name][client_cmd_perm_key]) if (player.steam_id == minqlx.owner() or (not is_client_cmd and perm == 0) or (is_client_cmd and client_cmd_perm == 0)): return True player_perm = self.plugin.db.get_permission(player) if is_client_cmd: return player_perm >= client_cmd_perm else: return player_perm >= perm
def config(self): """minqlx's config. Same as :func:`minqlx.get_config()`.""" return minqlx.get_config()
def connect(self, host=None, database=0, unix_socket=False, password=None): """Returns a connection to a Redis database. If *host* is None, it will fall back to the settings in the config and ignore the rest of the arguments. It will also share the connection across any plugins using the default configuration. Passing *host* will make it connect to a specific database that is not shared at all. Subsequent calls to this will return the connection initialized the first call unless it has been closed. :param host: The host name. If no port is specified, it will use 6379. Ex.: ``localhost:1234``. :type host: str :param database: The database number that should be used. :type database: int :param unix_socket: Whether or not *host* should be interpreted as a unix socket path. :type unix_socket: bool :raises: RuntimeError """ if not host and not self._conn: # Resort to default settings in config? if not Redis._conn: conf = minqlx.get_config() if not "Redis" in conf or not "Host" in conf[ "Redis"] or not "Database" in conf["Redis"]: raise RuntimeError( "No host and/or database number passed nor set in config." ) elif "UseUnixSocket" in conf["Redis"] and conf.getboolean( "Redis", "UseUnixSocket"): db = int(conf["Redis"]["Database"]) if "Password" in conf["Redis"] and conf["Redis"][ "Password"]: Redis._pass = conf["Redis"]["Password"] Redis._conn = redis.StrictRedis( unix_socket_path=conf["Redis"]["Host"], db=db, password=Redis._pass, decode_responses=True) else: host = conf["Redis"]["Host"] split_host = host.split(":") if len(split_host) > 1: port = int(split_host[1]) else: port = 6379 # Default port. db = int(conf["Redis"]["Database"]) if "Password" in conf["Redis"] and conf["Redis"][ "Password"]: Redis._pass = conf["Redis"]["Password"] Redis._pool = redis.ConnectionPool(host=split_host[0], port=port, db=db, password=Redis._pass) Redis._conn = redis.StrictRedis( connection_pool=Redis._pool, decode_responses=True) # TODO: Why does self._conn get set when doing Redis._conn? self._conn = None return Redis._conn elif not self._conn: split_host = host.split(":") if len(split_host) > 1: port = int(split_host[1]) else: port = 6379 # Default port. if unix_socket: self._conn = redis.StrictRedis(unix_socket_path=host, db=db, password=password, decode_responses=True) else: self._pool = redis.ConnectionPool(host=split_host[0], port=port, db=db, password=password) self._conn = redis.StrictRedis(connection_pool=self._pool, decode_responses=True) return self._conn