summaryrefslogtreecommitdiffstats
path: root/iredis/client.py
diff options
context:
space:
mode:
Diffstat (limited to 'iredis/client.py')
-rw-r--r--iredis/client.py52
1 files changed, 45 insertions, 7 deletions
diff --git a/iredis/client.py b/iredis/client.py
index 1b1bf38..6555ff1 100644
--- a/iredis/client.py
+++ b/iredis/client.py
@@ -55,25 +55,35 @@ class Client:
def __init__(
self,
- host=None,
- port=None,
+ host="127.0.0.1",
+ port=6379,
db=0,
password=None,
path=None,
scheme="redis",
username=None,
client_name=None,
+ prompt=None,
):
self.host = host
self.port = port
self.db = db
self.path = path
- # FIXME username is not using...
self.username = username
self.client_name = client_name
self.scheme = scheme
self.password = password
+ # cli args --prompt will overwrite the prompt in iredisrc config file
+ self.prompt = ""
+ if config.prompt:
+ self.prompt = config.prompt
+ if prompt:
+ self.prompt = prompt
+
+ self.client_id = None
+ self.client_addr = None
+
self.build_connection()
# all command upper case
@@ -93,6 +103,13 @@ class Client:
else:
config.no_version_reason = "--no-info flag activated"
+ if self.prompt and "client_addr" in self.prompt:
+ self.client_addr = ":".join(
+ str(x) for x in self.connection._sock.getsockname()
+ )
+ if self.prompt and "client_id" in self.prompt:
+ self.client_id = str(self.execute("CLIENT ID"))
+
if config.version and re.match(r"([\d\.]+)", config.version):
self.auth_compat(config.version)
@@ -131,6 +148,11 @@ class Client:
"socket_keepalive": config.socket_keepalive,
"client_name": client_name,
}
+
+ # if username is set without setting paswword, password will be ignored
+ if password:
+ connection_kwargs["username"] = username
+
if scheme == "rediss":
connection_class = SSLConnection
else:
@@ -141,6 +163,7 @@ class Client:
"password": password,
"path": path,
"client_name": client_name,
+ "username": username,
}
connection_class = UnixDomainSocketConnection
@@ -150,7 +173,8 @@ class Client:
connection_kwargs["encoding_errors"] = "replace"
logger.debug(
- f"connection_class={connection_class}, connection_kwargs={connection_kwargs}"
+ f"connection_class={connection_class},"
+ f" connection_kwargs={connection_kwargs}"
)
return connection_class(**connection_kwargs)
@@ -191,6 +215,18 @@ class Client:
config.version = version
def __str__(self):
+ if self.prompt: # not None and not empty
+ return self.prompt.format(
+ client_name=self.client_name,
+ db=self.db,
+ host=self.host,
+ path=self.path,
+ port=self.port,
+ username=self.username,
+ client_addr=self.client_addr,
+ client_id=self.client_id,
+ )
+
if self.scheme == "unix":
prompt = f"redis {self.path}"
else:
@@ -198,7 +234,8 @@ class Client:
if self.db:
prompt = f"{prompt}[{self.db}]"
- return prompt
+
+ return f"{prompt}> "
def client_execute_command(self, command_name, *args):
command = command_name.upper()
@@ -222,7 +259,8 @@ class Client:
Here we retry once for ConnectionError.
"""
logger.info(
- f"execute by connection: connection={connection}, name={command_name}, {args}, {options}"
+ f"execute by connection: connection={connection}, name={command_name},"
+ f" {args}, {options}"
)
retry_times = config.retry_times # FIXME configurable
last_error = None
@@ -248,7 +286,7 @@ class Client:
last_error = e
retry_times -= 1
need_refresh_connection = True
- except (ResponseError) as e:
+ except ResponseError as e:
response_message = str(e)
if response_message.startswith("MOVED"):
return self.reissue_with_redirect(