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