Refactor to better variable name

This commit is contained in:
Midgard 2021-06-30 13:17:19 +02:00
parent 544b6db885
commit 89bea07ae6
Signed by: midgard
GPG key ID: 511C112F1331BBB4

View file

@ -152,31 +152,31 @@ def resolve_team_channel(mm_api: mattermost.MMApi, query: str) -> Dict:
return team, channel
def login(mm_api, parsed):
def login(mm_api, cmdline_args):
print(
f"Logging in as {parsed.user}; password provided: {yes_no(parsed.password)}; "
f"TOTP token provided: {yes_no(parsed.totp)}",
f"Logging in as {cmdline_args.user}; password provided: {yes_no(cmdline_args.password)}; "
f"TOTP token provided: {yes_no(cmdline_args.totp)}",
file=sys.stderr)
mm_api.login(parsed.user, parsed.password, parsed.totp)
mm_api.login(cmdline_args.user, cmdline_args.password, cmdline_args.totp)
if parsed.format == "json":
if cmdline_args.format == "json":
print(json.dumps({"token": mm_api._bearer}))
elif parsed.format == "tsv":
elif cmdline_args.format == "tsv":
print(mm_api._bearer)
else:
assert False
def cat(mm_api: mattermost.MMApi, parsed):
def cat(mm_api: mattermost.MMApi, cmdline_args):
# channels = [
# resolve_team_channel(mm_api, query)
# for query in parsed.channels
# for query in cmdline_args.channels
# ]
team, channel = resolve_team_channel(mm_api, parsed.channel)
team, channel = resolve_team_channel(mm_api, cmdline_args.channel)
users = list(mm_api.get_users())
if not parsed.ids:
if not cmdline_args.ids:
def attribute(key_value):
key, value = key_value
if key == "channel_id":
@ -195,22 +195,22 @@ def cat(mm_api: mattermost.MMApi, parsed):
backlog_lock = threading.Lock()
def print_initial_messages():
posts = get_posts_for_channel(mm_api, channel["id"], after=parsed.after, since=parsed.since)
posts = get_posts_for_channel(mm_api, channel["id"], after=cmdline_args.after, since=cmdline_args.since)
for post in posts:
print(str_for_post(attribute, post, parsed))
print(str_for_post(attribute, post, cmdline_args))
with backlog_lock:
for post in backlog[0]:
print(str_for_post(attribute, post, parsed))
print(str_for_post(attribute, post, cmdline_args))
backlog[0] = None
if parsed.follow:
if cmdline_args.follow:
def simple_websocket_callback(_mmws, event_data):
if event_data.get("event") == "posted":
post = json.loads(event_data["data"]["post"])
if post["channel_id"] != channel["id"]:
return
print(str_for_post(attribute, post, parsed), flush=True)
print(str_for_post(attribute, post, cmdline_args), flush=True)
def initial_websocket_callback(mmws: MMws, event_data):
if event_data.get("event") == "posted":
@ -238,10 +238,10 @@ def cat(mm_api: mattermost.MMApi, parsed):
print_initial_messages()
def send(mm_api: mattermost.MMApi, parsed):
read_stdin = parsed.message is None or parsed.channel is None
def send(mm_api: mattermost.MMApi, cmdline_args):
read_stdin = cmdline_args.message is None or cmdline_args.channel is None
team, channel = resolve_team_channel(mm_api, parsed.channel) if parsed.channel is not None else (None, None)
team, channel = resolve_team_channel(mm_api, cmdline_args.channel) if cmdline_args.channel is not None else (None, None)
if read_stdin:
if sys.stdin.isatty():
@ -265,7 +265,7 @@ def send(mm_api: mattermost.MMApi, parsed):
print(sent)
else:
sent = mm_api.create_post(channel["id"], parsed.message, props={"from_mmcli": "true"}, filepaths=parsed.attach)
sent = mm_api.create_post(channel["id"], cmdline_args.message, props={"from_mmcli": "true"}, filepaths=cmdline_args.attach)
print(sent)
@ -273,16 +273,16 @@ def tsv_escape(text):
return text.replace("\\", "\\\\").replace("\t", r"\t").replace("\n", r"\n")
def str_for_post(attribute, post, parsed):
def str_for_post(attribute, post, cmdline_args):
obj = {
k: v
for k, v in map(attribute, post.items())
if (v or k == "message") and (k != "update_at" or post["update_at"] != post["create_at"])
}
if parsed.format == "json":
if cmdline_args.format == "json":
return json.dumps(obj)
if parsed.format == "tsv":
if cmdline_args.format == "tsv":
msg = tsv_escape(obj.get("message", ""))
return f"{obj['id']}\t{obj['create_at']}\t{obj.get('username') or obj['user_id']}\t{msg}"
assert False