-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptredis
executable file
·59 lines (53 loc) · 1.86 KB
/
ptredis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
"""
Start ptpython shell with redis object
License: GPLv3
Author: Bernardas Ališauskas <bernardas.alisauskas@pm.me>
"""
import inspect
import os
from pathlib import Path
import click
from ptpython.repl import embed
from redis import Redis
@click.command()
@click.option('-h', '--host', default=os.environ.get('REDIS_HOST'),
help='server address', show_default=True)
@click.option('-n', '--database', default=0,
help='database number', show_default=True)
@click.option('-p', '--port', default=os.environ.get('REDIS_PORT'),
help='server port', show_default=True)
@click.option('-a', '--pass', 'password', default=os.environ.get('REDIS_PASS'),
help='server password')
@click.option('-h', '--history', default=Path(__file__).parent / 'ptredis.history',
help='command history file location', show_default=True)
@click.option('-b', '--as_bytes', is_flag=True, help='do not decode responses')
@click.option('-s', '--silent', is_flag=True, help='do not print stuff')
def main(host, port, password, as_bytes, silent, history, database):
"""start ptpython shell with redis object and expanded commands"""
if silent:
echo = lambda v: None
else:
echo = click.echo
redis = Redis(
host=host,
port=os.environ.get('REDIS_PORT'),
password=os.environ.get('REDIS_PASS'),
decode_responses=not as_bytes,
db=database,
)
r = redis
for attr_name in dir(redis):
attr = getattr(redis, attr_name)
if inspect.ismethod(attr) and not attr_name.startswith('_'):
locals()[attr_name] = attr
echo(f'connected to: {redis}')
embed(
vi_mode=True,
history_filename=history,
locals=locals(),
globals=globals(),
title=f"Python Redis @ {host}",
)
if __name__ == '__main__':
main()