From 2e54939345aeb940f2106cd644da8942bce7b2bd Mon Sep 17 00:00:00 2001 From: magical Date: Sat, 6 Aug 2022 06:01:25 +0000 Subject: [PATCH] remove global network value --- clients/urwid/main.py | 79 ++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/clients/urwid/main.py b/clients/urwid/main.py index d4542b6..e3da6c6 100644 --- a/clients/urwid/main.py +++ b/clients/urwid/main.py @@ -309,7 +309,8 @@ markpath = os.path.join(os.getenv("HOME"), ".bbjmarks") pinpath = os.path.join(os.getenv("HOME"), ".bbjpins") class App(object): - def __init__(self): + def __init__(self, network): + self.network = network self.prefs = bbjrc("load") self.client_pinned_threads = load_client_pins() self.usermap = {} @@ -368,7 +369,7 @@ class App(object): bar formatting to it. """ header = ("{}@bbj | " + text).format( - (network.user_name or "anonymous"), + (self.network.user_name or "anonymous"), *format_specs ) self.loop.widget.header = urwid.AttrMap(urwid.Text(header), "bar") @@ -574,7 +575,7 @@ class App(object): # first we need to get the server's version of the message # instead of our formatted one try: - message = network.edit_query(thread_id, post_id) + message = self.network.edit_query(thread_id, post_id) except UserWarning as e: self.remove_overlays() return self.temp_footer_message(e.description) @@ -597,7 +598,7 @@ class App(object): urwid.Text(("bold", "Delete this %s?" % ("whole thread" if op else "post"))), urwid.Divider(), cute_button(("10" , ">> Yes"), lambda _: [ - network.message_delete(message["thread_id"], message["post_id"]), + self.network.message_delete(message["thread_id"], message["post_id"]), self.remove_overlays(), self.index() if op else self.refresh() ]), @@ -619,7 +620,7 @@ class App(object): def toggle_formatting(self, button, message): self.remove_overlays() raw = not message["send_raw"] - network.set_post_raw(message["thread_id"], message["post_id"], raw) + self.network.set_post_raw(message["thread_id"], message["post_id"], raw) return self.refresh() @@ -636,7 +637,7 @@ class App(object): "View %sQuote" % ("a " if len(quotes) != 1 else ""), self.quote_view_menu, quotes)) - if network.can_edit(message["thread_id"], message["post_id"]) \ + if self.network.can_edit(message["thread_id"], message["post_id"]) \ and not self.window_split: if message["post_id"] == 0: @@ -649,7 +650,7 @@ class App(object): "Enable Formatting" if raw else "Disable Formatting", self.toggle_formatting, message)) buttons.insert(0, urwid.Button("Edit Post", self.edit_post, message)) - if network.user["is_admin"]: + if self.network.user["is_admin"]: buttons.insert(0, urwid.Text(("20", "Reminder: You're an admin!"))) if not buttons: @@ -802,7 +803,7 @@ class App(object): # narrowed selection of content, so we dont want to resume last_index_pos self.last_index_pos = False else: - threads, usermap = network.thread_index() + threads, usermap = self.network.thread_index() self.usermap.update(usermap) self.walker.clear() @@ -848,7 +849,7 @@ class App(object): self.body.attr_map = {None: "default"} self.mode = "thread" - thread, usermap = network.thread_load(thread_id, format="sequential") + thread, usermap = self.network.thread_load(thread_id, format="sequential") self.usermap.update(usermap) self.thread = thread self.match_data["matches"].clear() @@ -869,16 +870,16 @@ class App(object): def toggle_server_pin(self): - if self.mode != "index" or not network.user["is_admin"]: + if self.mode != "index" or not self.network.user["is_admin"]: return thread = self.walker.get_focus()[0].thread - network.thread_set_pin(thread["thread_id"], not thread["pinned"]) + self.network.thread_set_pin(thread["thread_id"], not thread["pinned"]) self.index() def search_index_callback(self, query): simple_query = query.lower().strip() - threads, usermap = network.thread_index() + threads, usermap = self.network.thread_index() self.usermap.update(usermap) results = [ thread for thread in threads @@ -1156,7 +1157,7 @@ class App(object): call("clear", shell=True) print(welcome) try: - log_in() + log_in(self.network) except (KeyboardInterrupt, InterruptedError): pass self.loop.start() @@ -1169,8 +1170,9 @@ class App(object): Options menu callback to anonymize the user and then redisplay the options menu. """ - network.user_name = network.user_auth = None - network.user = network("get_me")["data"] + self.network.user_name = None + self.network.user_auth = None + self.network.user = self.network("get_me")["data"] self.loop.widget = self.loop.widget[0] self.set_default_header() self.options_menu() @@ -1212,7 +1214,7 @@ class App(object): """ # we can "recycle" the server's formatting abilities to # use the same syntax for the help text itself - message = network.fake_message( + message = self.network.fake_message( "\n\n".join(format_help), format="sequential") widget = OptionsMenu( @@ -1234,7 +1236,7 @@ class App(object): def set_color(self, button, value, color): if value == False: return - network.user_update(color=color) + self.network.user_update(color=color) def toggle_exit(self, button, value): @@ -1263,8 +1265,8 @@ class App(object): self.loop.stop() call("clear", shell=True) try: - name = nameloop("Choose a new username", True) - network.user_update(user_name=name) + name = nameloop(self.network, "Choose a new username", True) + self.network.user_update(user_name=name) motherfucking_rainbows("~~hello there %s~~" % name) sleep(0.8) self.loop.start() @@ -1280,7 +1282,7 @@ class App(object): call("clear", shell=True) try: password = password_loop("Choose a new password. Can be empty", True) - network.user_update(auth_hash=network._hash(password)) + self.network.user_update(auth_hash=self.network._hash(password)) motherfucking_rainbows("SET NEW PASSWORD") sleep(0.8) self.loop.start() @@ -1376,8 +1378,8 @@ class App(object): editor_buttons = [] edit_mode = [] - if network.user_auth: - account_message = "Logged in as %s." % network.user_name + if self.network.user_auth: + account_message = "Logged in as %s." % self.network.user_name account_stuff = [ urwid.Button("Relog", on_press=self.relog), urwid.Button("Go anonymous", on_press=self.unlog), @@ -1394,7 +1396,7 @@ class App(object): for index, color in enumerate(colornames): urwid.RadioButton( user_colors, color.title(), - network.user["color"] == index, + self.network.user["color"] == index, self.set_color, index) for item in user_colors: @@ -1616,7 +1618,7 @@ class App(object): return self.footer_prompt("Title", self.compose) elif title: - try: network.validate("title", title) + try: self.network.validate("title", title) except AssertionError as e: return self.footer_prompt( "Title", self.compose, extra_text=e.description) @@ -1642,7 +1644,7 @@ class App(object): endpoint = "thread_create" params.update({"title": title}) - network.request(endpoint, **params) + self.network.request(endpoint, **params) self.refresh() if edit: self.goto_post(edit["post_id"]) @@ -1751,7 +1753,7 @@ class MessageBody(urwid.Text): if _c != 0: color = str(_c) - if user != "anonymous" and user["user_name"] == network.user_name: + if user != "anonymous" and user["user_name"] == app.network.user_name: display = "[You]" # bold it color += "0" @@ -1978,7 +1980,7 @@ class ExternalEditor(urwid.Terminal): def exterminate(self, *_, anon_confirmed=False): if app.prefs["confirm_anon"] \ and not anon_confirmed \ - and network.user["user_name"] == "anonymous": + and app.network.user["user_name"] == "anonymous": # TODO fixoverlay: urwid terminal widgets have been mucking # up overlay dialogs since the wee days of bbj, i really # need to find a real solution instead of dodging the issue @@ -2021,7 +2023,7 @@ class ExternalEditor(urwid.Terminal): if body and not re.search("^>>[0-9]+$", body): self.params.update({"body": body}) - network.request(self.endpoint, **self.params) + app.network.request(self.endpoint, **self.params) if self.endpoint == "edit_post": app.refresh() app.goto_post(self.params["post_id"]) @@ -2366,13 +2368,13 @@ def paren_prompt(text, positive=True, choices=[], function=input, default=None): return "" -def sane_value(key, prompt, positive=True, return_empty=False): +def sane_value(network, key, prompt, positive=True, return_empty=False): response = paren_prompt(prompt, positive) if return_empty and response == "": return response try: network.validate(key, response) except AssertionError as e: - return sane_value(key, e.description, False) + return sane_value(network, key, e.description, False) return response @@ -2388,14 +2390,14 @@ def password_loop(prompt, positive=True): return response1 -def nameloop(prompt, positive): - name = sane_value("user_name", prompt, positive) +def nameloop(network, prompt, positive): + name = sane_value(network, "user_name", prompt, positive) if network.user_is_registered(name): - return nameloop("%s is already registered" % name, False) + return nameloop(network, "%s is already registered" % name, False) return name -def log_in(name="", password=""): +def log_in(network, name="", password=""): """ Handles login or registration. If name and/or password are not provided, the user is prompted for them using an oldschool @@ -2403,7 +2405,7 @@ def log_in(name="", password=""): curses app. """ if not name: - name = sane_value("user_name", "Username", return_empty=True) + name = sane_value(network, "user_name", "Username", return_empty=True) if name == "": motherfucking_rainbows("~~W3 4R3 4n0nYm0u5~~") else: @@ -2436,7 +2438,7 @@ def log_in(name="", password=""): ) if response == "c": - name = nameloop("Pick a new name", True) + name = nameloop(network, "Pick a new name", True) elif response == "n": raise InterruptedError @@ -2565,7 +2567,6 @@ def main(): print(help_text) exit() - global network try: network = BBJ(get_arg("host", "127.0.0.1"), get_arg("port", 7099), @@ -2575,14 +2576,14 @@ def main(): exit("\033[0;31m%s\033[0m" % repr(e)) global app - app = App() + app = App(network) call("clear", shell=True) motherfucking_rainbows(obnoxious_logo) print(welcome) try: name = get_arg("user") or os.getenv("BBJ_USER") password = os.getenv("BBJ_PASSWORD", default="") - log_in(name, password) + log_in(network, name, password) app.index() app.loop.run() except (InterruptedError, KeyboardInterrupt):