From 7b01c62e07612a123f3ffe94583f51099e470c3b Mon Sep 17 00:00:00 2001 From: mathieui Date: Mon, 20 Oct 2014 20:03:16 +0200 Subject: Change the API of Config.get_by_tabname Make the "default" parameter optional and thus move it to the end of the command with the other optional parameters. And change all the calls. --- src/bookmark.py | 2 +- src/config.py | 7 ++-- src/core/commands.py | 5 +-- src/core/handlers.py | 34 +++++++++---------- src/logger.py | 10 +++--- src/tabs/basetabs.py | 9 ++--- src/tabs/conversationtab.py | 15 ++++++--- src/tabs/muctab.py | 81 +++++++++++++++++++++------------------------ src/tabs/privatetab.py | 18 +++++----- 9 files changed, 90 insertions(+), 91 deletions(-) (limited to 'src') diff --git a/src/bookmark.py b/src/bookmark.py index aa710d24..1807c45e 100644 --- a/src/bookmark.py +++ b/src/bookmark.py @@ -226,7 +226,7 @@ def get_local(): nick = jid.resource else: nick = None - passwd = config.get_by_tabname('password', '', jid.bare, fallback=False) or None + passwd = config.get_by_tabname('password', jid.bare, fallback=False) or None b = Bookmark(jid.bare, autojoin=True, nick=nick, password=passwd, method='local') if not get_by_jid(b.jid): bookmarks.append(b) diff --git a/src/config.py b/src/config.py index 79c7381d..533838e1 100644 --- a/src/config.py +++ b/src/config.py @@ -181,15 +181,16 @@ class Config(RawConfigParser): return default return res - def get_by_tabname( - self, option, default, tabname, - fallback=True, fallback_server=True): + def get_by_tabname(self, option, tabname, + fallback=True, fallback_server=True, default=''): """ Try to get the value for the option. First we look in a section named `tabname`, if the option is not present in the section, we search for the global option if fallback is True. And we return `default` as a fallback as a last resort. """ + if self.default and (not default) and fallback: + default = self.default.get(DEFSECTION, {}).get(option, '') if tabname in self.sections(): if option in self.options(tabname): # We go the tab-specific option diff --git a/src/core/commands.py b/src/core/commands.py index 7c0f56fa..ab441ae9 100644 --- a/src/core/commands.py +++ b/src/core/commands.py @@ -375,10 +375,7 @@ def command_join(self, arg, histo_length=None): if histo_length is not None: histo_length = str(histo_length) if password is None: # try to use a saved password - password = config.get_by_tabname('password', - None, - room, - fallback=False) + password = config.get_by_tabname('password', room, fallback=False) if tab and not tab.joined: if tab.last_connection: if tab.last_connection is not None: diff --git a/src/core/handlers.py b/src/core/handlers.py index 87aaecd5..4853c804 100644 --- a/src/core/handlers.py +++ b/src/core/handlers.py @@ -235,8 +235,8 @@ def on_normal_message(self, message): def try_modify(): replaced_id = message['replace']['id'] - if replaced_id and (config.get_by_tabname('group_corrections', - True, conv_jid.bare)): + if replaced_id and config.get_by_tabname('group_corrections', + conv_jid.bare): try: conversation.modify_message(body, replaced_id, message['id'], jid=jid, nickname=remote_nick) @@ -260,7 +260,7 @@ def on_normal_message(self, message): else: conversation.remote_wants_chatstates = False if 'private' in config.get('beep_on', 'highlight private').split(): - if not config.get_by_tabname('disable_beep', False, conv_jid.bare, False): + if not config.get_by_tabname('disable_beep', conv_jid.bare): curses.beep() if self.current_tab() is not conversation: conversation.state = 'private' @@ -310,7 +310,7 @@ def on_gaming_event(self, message): if contact.gaming: logger.log_roster_change(contact.bare_jid, 'is playing %s' % (common.format_gaming_string(contact.gaming))) - if old_gaming != contact.gaming and config.get_by_tabname('display_gaming_notifications', False, contact.bare_jid): + if old_gaming != contact.gaming and config.get_by_tabname('display_gaming_notifications', contact.bare_jid): if contact.gaming: self.information('%s is playing %s' % (contact.bare_jid, common.format_gaming_string(contact.gaming)), 'Gaming') else: @@ -343,7 +343,7 @@ def on_mood_event(self, message): if contact.mood: logger.log_roster_change(contact.bare_jid, 'has now the mood: %s' % contact.mood) - if old_mood != contact.mood and config.get_by_tabname('display_mood_notifications', False, contact.bare_jid): + if old_mood != contact.mood and config.get_by_tabname('display_mood_notifications', contact.bare_jid): if contact.mood: self.information('Mood from '+ contact.bare_jid + ': ' + contact.mood, 'Mood') else: @@ -382,7 +382,7 @@ def on_activity_event(self, message): if contact.activity: logger.log_roster_change(contact.bare_jid, 'has now the activity %s' % contact.activity) - if old_activity != contact.activity and config.get_by_tabname('display_activity_notifications', False, contact.bare_jid): + if old_activity != contact.activity and config.get_by_tabname('display_activity_notifications', contact.bare_jid): if contact.activity: self.information('Activity from '+ contact.bare_jid + ': ' + contact.activity, 'Activity') else: @@ -416,7 +416,7 @@ def on_tune_event(self, message): if contact.tune: logger.log_roster_change(message['from'].bare, 'is now listening to %s' % common.format_tune_string(contact.tune)) - if old_tune != contact.tune and config.get_by_tabname('display_tune_notifications', False, contact.bare_jid): + if old_tune != contact.tune and config.get_by_tabname('display_tune_notifications', contact.bare_jid): if contact.tune: self.information( 'Tune from '+ message['from'].bare + ': ' + common.format_tune_string(contact.tune), @@ -460,8 +460,8 @@ def on_groupchat_message(self, message): delayed, date = common.find_delayed_tag(message) replaced_id = message['replace']['id'] replaced = False - if replaced_id is not '' and (config.get_by_tabname( - 'group_corrections', True, message['from'].bare)): + if replaced_id is not '' and config.get_by_tabname('group_corrections', + message['from'].bare): try: if tab.modify_message(body, replaced_id, message['id'], time=date, nickname=nick_from, user=user): @@ -488,7 +488,7 @@ def on_groupchat_message(self, message): self.doupdate() if 'message' in config.get('beep_on', 'highlight private').split(): - if (not config.get_by_tabname('disable_beep', False, room_from, False) + if (not config.get_by_tabname('disable_beep', room_from) and self.own_nick != message['from'].resource): curses.beep() @@ -515,13 +515,13 @@ def on_groupchat_private_message(self, message): tmp_dir=tmp_dir, extract_images=extract_images) tab = self.get_tab_by_name(jid.full, tabs.PrivateTab) # get the tab with the private conversation - ignore = config.get_by_tabname('ignore_private', False, room_from) + ignore = config.get_by_tabname('ignore_private', room_from) if not tab: # It's the first message we receive: create the tab if body and not ignore: tab = self.open_private_window(room_from, nick_from, False) if ignore: self.events.trigger('ignored_private', message, tab) - msg = config.get_by_tabname('private_auto_response', None, room_from) + msg = config.get_by_tabname('private_auto_response', room_from) if msg and body: self.xmpp.send_message(mto=jid.full, mbody=msg, mtype='chat') return @@ -534,8 +534,8 @@ def on_groupchat_private_message(self, message): replaced_id = message['replace']['id'] replaced = False user = tab.parent_muc.get_user_by_name(nick_from) - if replaced_id is not '' and (config.get_by_tabname( - 'group_corrections', True, room_from)): + if replaced_id is not '' and config.get_by_tabname('group_corrections', + room_from): try: tab.modify_message(body, replaced_id, message['id'], user=user, jid=message['from'], nickname=nick_from) @@ -555,7 +555,7 @@ def on_groupchat_private_message(self, message): else: tab.remote_wants_chatstates = False if 'private' in config.get('beep_on', 'highlight private').split(): - if not config.get_by_tabname('disable_beep', False, jid.full, False): + if not config.get_by_tabname('disable_beep', jid.full): curses.beep() if tab is self.current_tab(): self.refresh_window() @@ -1050,8 +1050,8 @@ def room_error(self, error, room_name): msg = _('To provide a password in order to join the room, type "/join / password" (replace "password" by the real password)') tab.add_message(msg, typ=2) if code == '409': - if config.get('alternative_nickname', '') != '': - self.command_join('%s/%s'% (tab.name, tab.own_nick+config.get('alternative_nickname', ''))) + if config.get('alternative_nickname') != '': + self.command_join('%s/%s'% (tab.name, tab.own_nick+config.get('alternative_nickname'))) else: if not tab.joined: tab.add_message(_('You can join the room with an other nick, by typing "/join /other_nick"'), typ=2) diff --git a/src/logger.py b/src/logger.py index 7ed0692f..53ec4311 100644 --- a/src/logger.py +++ b/src/logger.py @@ -78,7 +78,7 @@ class Logger(object): Check that the directory where we want to log the messages exists. if not, create it """ - if not config.get_by_tabname('use_log', True, room): + if not config.get_by_tabname('use_log', room): return try: makedirs(log_dir) @@ -106,10 +106,10 @@ class Logger(object): this function is a little bit more complicated than “read the last nb lines”. """ - if config.get_by_tabname('load_log', 10, jid) <= 0: + if config.get_by_tabname('load_log', jid) <= 0: return - if not config.get_by_tabname('use_log', True, jid): + if not config.get_by_tabname('use_log', jid): return if nb <= 0: @@ -197,7 +197,7 @@ class Logger(object): return True jid = str(jid).replace('/', '\\') - if not config.get_by_tabname('use_log', False, jid): + if not config.get_by_tabname('use_log', jid): return True if jid in self.fds.keys(): fd = self.fds[jid] @@ -245,7 +245,7 @@ class Logger(object): """ Log a roster change """ - if not config.get_by_tabname('use_log', False, jid): + if not config.get_by_tabname('use_log', jid): return True self.check_and_create_log_dir('', open_fd=False) if not self.roster_logfile: diff --git a/src/tabs/basetabs.py b/src/tabs/basetabs.py index c2efc9bc..0fde624c 100644 --- a/src/tabs/basetabs.py +++ b/src/tabs/basetabs.py @@ -592,8 +592,8 @@ class ChatTab(Tab): if not self.is_muc or self.joined: if state in ('active', 'inactive', 'gone') and self.inactive and not always_send: return - if config.get_by_tabname('send_chat_states', True, self.general_jid, True) and \ - self.remote_wants_chatstates is not False: + if (config.get_by_tabname('send_chat_states', self.general_jid) + and self.remote_wants_chatstates is not False): msg = self.core.xmpp.make_message(self.get_dest_jid()) msg['type'] = self.message_type msg['chat_state'] = state @@ -607,7 +607,8 @@ class ChatTab(Tab): on the the current status of the input """ name = self.general_jid - if config.get_by_tabname('send_chat_states', True, name, True) and self.remote_wants_chatstates: + if (config.get_by_tabname('send_chat_states', name) + and self.remote_wants_chatstates): needed = 'inactive' if self.inactive else 'active' self.cancel_paused_delay() if not empty_after: @@ -622,7 +623,7 @@ class ChatTab(Tab): we create a timed event that will put us to paused in a few seconds """ - if not config.get_by_tabname('send_chat_states', True, self.general_jid, True): + if not config.get_by_tabname('send_chat_states', self.general_jid): return if self.timed_event_paused: # check the weakref diff --git a/src/tabs/conversationtab.py b/src/tabs/conversationtab.py index 94cfd305..b2d526de 100644 --- a/src/tabs/conversationtab.py +++ b/src/tabs/conversationtab.py @@ -108,7 +108,7 @@ class ConversationTab(OneToOneTab): replaced = False if correct or msg['replace']['id']: msg['replace']['id'] = self.last_sent_message['id'] - if config.get_by_tabname('group_corrections', True, self.name): + if config.get_by_tabname('group_corrections', self.name): try: self.modify_message(msg['body'], self.last_sent_message['id'], msg['id'], jid=self.core.xmpp.boundjid, nickname=self.core.own_nick) @@ -121,7 +121,8 @@ class ConversationTab(OneToOneTab): msg.enable('html') msg['html']['body'] = xhtml.poezio_colors_to_html(msg['body']) msg['body'] = xhtml.clean_text(msg['body']) - if config.get_by_tabname('send_chat_states', True, self.general_jid, True) and self.remote_wants_chatstates is not False: + if (config.get_by_tabname('send_chat_states', self.general_jid) and + self.remote_wants_chatstates is not False): needed = 'inactive' if self.inactive else 'active' msg['chat_state'] = needed if attention and self.remote_supports_attention: @@ -316,7 +317,9 @@ class ConversationTab(OneToOneTab): self.state = 'normal' self.text_win.remove_line_separator() self.text_win.add_line_separator(self._text_buffer) - if config.get_by_tabname('send_chat_states', True, self.general_jid, True) and (not self.input.get_text() or not self.input.get_text().startswith('//')): + if (config.get_by_tabname('send_chat_states', self.general_jid) + and (not self.input.get_text() + or not self.input.get_text().startswith('//'))): if resource: self.send_chat_state('inactive') self.check_scrolled() @@ -334,7 +337,9 @@ class ConversationTab(OneToOneTab): self.state = 'current' curses.curs_set(1) - if config.get_by_tabname('send_chat_states', True, self.general_jid, True) and (not self.input.get_text() or not self.input.get_text().startswith('//')): + if (config.get_by_tabname('send_chat_states', self.general_jid) + and (not self.input.get_text() + or not self.input.get_text().startswith('//'))): if resource: self.send_chat_state('active') @@ -349,7 +354,7 @@ class ConversationTab(OneToOneTab): def on_close(self): Tab.on_close(self) - if config.get_by_tabname('send_chat_states', True, self.general_jid, True): + if config.get_by_tabname('send_chat_states', self.general_jid): self.send_chat_state('gone') def matching_names(self): diff --git a/src/tabs/muctab.py b/src/tabs/muctab.py index c36a533d..bd8ed312 100644 --- a/src/tabs/muctab.py +++ b/src/tabs/muctab.py @@ -469,8 +469,8 @@ class MucTab(ChatTab): char_quit = get_theme().CHAR_QUIT spec_col = dump_tuple(get_theme().COLOR_QUIT_CHAR) - if config.get_by_tabname('display_user_color_in_join_part', True, - self.general_jid, True): + if config.get_by_tabname('display_user_color_in_join_part', + self.general_jid): color = dump_tuple(get_theme().COLOR_OWN_NICK) else: color = 3 @@ -735,8 +735,8 @@ class MucTab(ChatTab): msg.enable('html') msg['html']['body'] = xhtml.poezio_colors_to_html(msg['body']) msg['body'] = xhtml.clean_text(msg['body']) - if (config.get_by_tabname('send_chat_states', True, self.general_jid, - True) and self.remote_wants_chatstates is not False): + if (config.get_by_tabname('send_chat_states', self.general_jid) + and self.remote_wants_chatstates is not False): msg['chat_state'] = needed if correct: msg['replace']['id'] = self.last_sent_message['id'] @@ -922,8 +922,8 @@ class MucTab(ChatTab): self.state = 'disconnected' self.text_win.remove_line_separator() self.text_win.add_line_separator(self._text_buffer) - if config.get_by_tabname('send_chat_states', True, - self.general_jid, True) and not self.input.get_text(): + if (config.get_by_tabname('send_chat_states', self.general_jid) and + not self.input.get_text()): self.send_chat_state('inactive') self.check_scrolled() @@ -933,8 +933,8 @@ class MucTab(ChatTab): and not config.get('show_useless_separator', False)): self.text_win.remove_line_separator() curses.curs_set(1) - if self.joined and config.get_by_tabname('send_chat_states', True, - self.general_jid, True) and not self.input.get_text(): + if self.joined and config.get_by_tabname('send_chat_states', + self.general_jid) and not self.input.get_text(): self.send_chat_state('active') def on_info_win_size_changed(self): @@ -1001,7 +1001,7 @@ class MucTab(ChatTab): new_user.color = get_theme().COLOR_OWN_NICK if config.get_by_tabname('display_user_color_in_join_part', - True, self.general_jid, True): + self.general_jid): color = dump_tuple(new_user.color) else: color = 3 @@ -1120,11 +1120,11 @@ class MucTab(ChatTab): user = User(from_nick, affiliation, show, status, role, jid) self.users.append(user) - hide_exit_join = config.get_by_tabname('hide_exit_join', -1, - self.general_jid, True) + hide_exit_join = config.get_by_tabname('hide_exit_join', + self.general_jid) if hide_exit_join != 0: - if config.get_by_tabname('display_user_color_in_join_part', True, - self.general_jid, True): + if config.get_by_tabname('display_user_color_in_join_part', + self.general_jid): color = dump_tuple(user.color) else: color = 3 @@ -1161,8 +1161,8 @@ class MucTab(ChatTab): self.core.on_muc_own_nickchange(self) user.change_nick(new_nick) - if config.get_by_tabname('display_user_color_in_join_part', True, - self.general_jid, True): + if config.get_by_tabname('display_user_color_in_join_part', + self.general_jid): color = dump_tuple(user.color) else: color = 3 @@ -1204,10 +1204,9 @@ class MucTab(ChatTab): self.refresh_tab_win() self.core.current_tab().input.refresh() self.core.doupdate() - if config.get_by_tabname('autorejoin', False, - self.general_jid, True): - delay = config.get_by_tabname('autorejoin_delay', '5', - self.general_jid, True) + if config.get_by_tabname('autorejoin', self.general_jid): + delay = config.get_by_tabname('autorejoin_delay', + self.general_jid) delay = common.parse_str_to_secs(delay) if delay <= 0: muc.join_groupchat(self.core, self.name, self.own_nick) @@ -1221,7 +1220,7 @@ class MucTab(ChatTab): else: if config.get_by_tabname('display_user_color_in_join_part', - True, self.general_jid, True): + self.general_jid): color = dump_tuple(user.color) else: color = 3 @@ -1276,10 +1275,9 @@ class MucTab(ChatTab): self.core.current_tab().input.refresh() self.core.doupdate() # try to auto-rejoin - if config.get_by_tabname('autorejoin', False, - self.general_jid, True): - delay = config.get_by_tabname('autorejoin_delay', "5", - self.general_jid, True) + if config.get_by_tabname('autorejoin', self.general_jid): + delay = config.get_by_tabname('autorejoin_delay', + self.general_jid) delay = common.parse_str_to_secs(delay) if delay <= 0: muc.join_groupchat(self.core, self.name, self.own_nick) @@ -1291,8 +1289,8 @@ class MucTab(ChatTab): self.name, self.own_nick)) else: - if config.get_by_tabname('display_user_color_in_join_part', True, - self.general_jid, True): + if config.get_by_tabname('display_user_color_in_join_part', + self.general_jid): color = dump_tuple(user.color) else: color = 3 @@ -1325,13 +1323,12 @@ class MucTab(ChatTab): self.core.disable_private_tabs(from_room) self.refresh_tab_win() - hide_exit_join = max(config.get_by_tabname('hide_exit_join', -1, - self.general_jid, True), - -1) + hide_exit_join = config.get_by_tabname('hide_exit_join', + self.general_jid) - if hide_exit_join == -1 or user.has_talked_since(hide_exit_join): - if config.get_by_tabname('display_user_color_in_join_part', True, - self.general_jid, True): + if hide_exit_join <= -1 or user.has_talked_since(hide_exit_join): + if config.get_by_tabname('display_user_color_in_join_part', + self.general_jid): color = dump_tuple(user.color) else: color = 3 @@ -1371,8 +1368,8 @@ class MucTab(ChatTab): # build the message display_message = False # flag to know if something significant enough # to be displayed has changed - if config.get_by_tabname('display_user_color_in_join_part', True, - self.general_jid, True): + if config.get_by_tabname('display_user_color_in_join_part', + self.general_jid): color = dump_tuple(user.color) else: color = 3 @@ -1408,8 +1405,8 @@ class MucTab(ChatTab): if not display_message: return msg = msg[:-2] # remove the last ", " - hide_status_change = config.get_by_tabname('hide_status_change', -1, - self.general_jid, True) + hide_status_change = config.get_by_tabname('hide_status_change', + self.general_jid) if hide_status_change < -1: hide_status_change = -1 if ((hide_status_change == -1 or \ @@ -1469,9 +1466,9 @@ class MucTab(ChatTab): self.state = 'highlight' highlighted = True else: - highlight_words = config.get_by_tabname('highlight_on', '', - self.general_jid, - True).split(':') + highlight_words = config.get_by_tabname('highlight_on', + self.general_jid) + highlight_words = highlight_words.split(':') for word in highlight_words: if word and word.lower() in txt.lower(): if self.state != 'current': @@ -1481,8 +1478,7 @@ class MucTab(ChatTab): if highlighted: beep_on = config.get('beep_on', 'highlight private').split() if 'highlight' in beep_on and 'message' not in beep_on: - if not config.get_by_tabname('disable_beep', False, - self.name, False): + if not config.get_by_tabname('disable_beep', self.name): curses.beep() return highlighted @@ -1521,8 +1517,7 @@ class MucTab(ChatTab): if (not time and nickname and nickname != self.own_nick and self.state != 'current'): if (self.state != 'highlight' and - config.get_by_tabname('notify_messages', - True, self.name)): + config.get_by_tabname('notify_messages', self.name)): self.state = 'message' if time and not txt.startswith('/me'): txt = '\x19%(info_col)s}%(txt)s' % { diff --git a/src/tabs/privatetab.py b/src/tabs/privatetab.py index c1e8c8e5..f4fa10cf 100644 --- a/src/tabs/privatetab.py +++ b/src/tabs/privatetab.py @@ -139,7 +139,7 @@ class PrivateTab(OneToOneTab): replaced = False if correct or msg['replace']['id']: msg['replace']['id'] = self.last_sent_message['id'] - if config.get_by_tabname('group_corrections', True, self.name): + if config.get_by_tabname('group_corrections', self.name): try: self.modify_message(msg['body'], self.last_sent_message['id'], msg['id'], user=user, jid=self.core.xmpp.boundjid, nickname=self.own_nick) @@ -153,7 +153,8 @@ class PrivateTab(OneToOneTab): msg.enable('html') msg['html']['body'] = xhtml.poezio_colors_to_html(msg['body']) msg['body'] = xhtml.clean_text(msg['body']) - if config.get_by_tabname('send_chat_states', True, self.general_jid, True) and self.remote_wants_chatstates is not False: + if (config.get_by_tabname('send_chat_states', self.general_jid) and + self.remote_wants_chatstates is not False): needed = 'inactive' if self.inactive else 'active' msg['chat_state'] = needed if attention and self.remote_supports_attention: @@ -278,9 +279,8 @@ class PrivateTab(OneToOneTab): self.text_win.remove_line_separator() self.text_win.add_line_separator(self._text_buffer) tab = self.core.get_tab_by_name(safeJID(self.name).bare, MucTab) - if tab and tab.joined and config.get_by_tabname( - 'send_chat_states', True, self.general_jid, True) and\ - not self.input.get_text() and self.on: + if tab and tab.joined and config.get_by_tabname('send_chat_states', + self.general_jid) and not self.input.get_text() and self.on: self.send_chat_state('inactive') self.check_scrolled() @@ -288,9 +288,8 @@ class PrivateTab(OneToOneTab): self.state = 'current' curses.curs_set(1) tab = self.core.get_tab_by_name(safeJID(self.name).bare, MucTab) - if tab and tab.joined and config.get_by_tabname( - 'send_chat_states', True, self.general_jid, True) and\ - not self.input.get_text() and self.on: + if tab and tab.joined and config.get_by_tabname('send_chat_states', + self.general_jid,) and not self.input.get_text() and self.on: self.send_chat_state('active') def on_info_win_size_changed(self): @@ -334,7 +333,8 @@ class PrivateTab(OneToOneTab): self.check_features() tab = self.core.get_tab_by_name(safeJID(self.name).bare, MucTab) color = 3 - if tab and config.get_by_tabname('display_user_color_in_join_part', '', self.general_jid, True): + if tab and config.get_by_tabname('display_user_color_in_join_part', + self.general_jid): user = tab.get_user_by_name(nick) if user: color = dump_tuple(user.color) -- cgit v1.2.3