def gen_infolist_get(infolist_name, arguments, pointer=""): """ Same as infolist_get(), but yields it's elements. Be sure to iterate through the whole list, to ensure weechat.infolist_free() is called. """ infolist = weechat.infolist_get(infolist_name, pointer, arguments) if infolist: while weechat.infolist_next(infolist): fields = weechat.infolist_fields(infolist).split(',') field_names = [] list_element = {} for field in fields: field_type, field_name = field.split(':') field_names.append(field_name) # decide which function to use info_func = { 'i': weechat.infolist_integer, 's': weechat.infolist_string, 'p': weechat.infolist_pointer, # 'b': weechat.infolist_buffer, 't': weechat.infolist_time, }[field_type] value = info_func(infolist, field_name) list_element[field_name] = value # create a temporary namedtuple type using field_names item_tpl = namedtuple('InfolistItem', field_names) yield item_tpl(**list_element) weechat.infolist_free(infolist)
def get_option_list_and_desc(option, displayname): """Get list of options and description for option(s).""" global cmdhelp_settings, cmdhelp_option_infolist, cmdhelp_option_infolist_fields options = [] description = '' cmdhelp_option_infolist = weechat.infolist_get('option', '', option) if cmdhelp_option_infolist: cmdhelp_option_infolist_fields = {} while weechat.infolist_next(cmdhelp_option_infolist): options.append( weechat.infolist_string(cmdhelp_option_infolist, 'full_name')) if not description: fields = weechat.infolist_fields(cmdhelp_option_infolist) for field in fields.split(','): items = field.split(':', 1) if len(items) == 2: cmdhelp_option_infolist_fields[items[1]] = items[0] description = re.compile(r'\$\{[^\}]+\}').sub( format_option, cmdhelp_settings['format_option']) if displayname: description = '%s%s%s: %s' % ( weechat.color(cmdhelp_settings['color_option_name']), weechat.infolist_string(cmdhelp_option_infolist, 'full_name'), weechat.color(cmdhelp_settings['color_option_help']), description) weechat.infolist_free(cmdhelp_option_infolist) cmdhelp_option_infolist = '' cmdhelp_option_infolist_fields = {} return options, description
def get_option_list_and_desc(option, displayname): """Get list of options and description for option(s).""" global cmdhelp_settings, cmdhelp_option_infolist global cmdhelp_option_infolist_fields options = [] description = '' cmdhelp_option_infolist = weechat.infolist_get('option', '', option) if cmdhelp_option_infolist: cmdhelp_option_infolist_fields = {} while weechat.infolist_next(cmdhelp_option_infolist): options.append(weechat.infolist_string(cmdhelp_option_infolist, 'full_name')) if not description: fields = weechat.infolist_fields(cmdhelp_option_infolist) for field in fields.split(','): items = field.split(':', 1) if len(items) == 2: cmdhelp_option_infolist_fields[items[1]] = items[0] description = re.compile(r'\$\{[^\}]+\}').sub( format_option, cmdhelp_settings['format_option']) if displayname: description = '%s%s%s: %s' % ( weechat.color(cmdhelp_settings['color_option_name']), weechat.infolist_string(cmdhelp_option_infolist, 'full_name'), weechat.color(cmdhelp_settings['color_option_help']), description) weechat.infolist_free(cmdhelp_option_infolist) cmdhelp_option_infolist = '' cmdhelp_option_infolist_fields = {} return options, description
def dev_fields(ptr): """ list fields of an infolist """ fields = w.infolist_fields(ptr) if not fields: w.prnt("", f"no fields for infolist {ptr}, maybe you need to move the cursor") return w.prnt("", f"fields for infolist {ptr}: {fields}")
def next(self): """ Return the next item in an InfoList, or raise StopIteration """ self.assert_open() if not infolist_next(self._infolist): raise StopIteration() self._old_item.close() new_item = InfoListItem(self, infolist_fields(self._infolist)) self._old_item = new_item return new_item
def dev_fields(ptr): """ list fields of an infolist """ fields = w.infolist_fields(ptr) if not fields: w.prnt( "", f"no fields for infolist {ptr}, maybe you need to move the cursor") return w.prnt("", f"fields for infolist {ptr}: {fields}")
def conga(data, buffer, command): il = w.infolist_get("buffer", "", "") pronted = False if il: while w.infolist_next(il): if not pronted: w.prnt("", w.infolist_fields(il)) pronted = True w.prnt("", "2. %i - %i - %i - %i - %s" % (w.infolist_integer(il, "first_line_not_read"), w.infolist_integer(il, "num_displayed"), w.infolist_integer(il, "lines_hidden"), w.infolist_integer(il, "num_history"), w.infolist_string(il, "name"))) w.infolist_free(il) return w.WEECHAT_RC_OK
def get_fields(self): """Return a dict of the fields in the current infolist position.""" fields = {} for field in weechat.infolist_fields(self._infolist).split(","): (field_type, field_name) = field.split(":") try: field_value = FIELD_TYPE_FUNC[field_type]( self._infolist, field_name) except KeyError: continue fields[field_name] = field_value return fields
def dev_iter(ptr): """ iterate all fields of all items of an infolist pointer """ buf = w.buffer_new(f"infolist {ptr}", "", "", "", "") infos = [] # type: List[Dict[str, Tuple]] while w.infolist_next(ptr): this = {} for field in w.infolist_fields(ptr).split(","): ftype, name = field.split(":", 1) if ftype == "i": this[ name] = ftype, f"{w.color('*lightgreen') + str(w.infolist_integer(ptr, name))}" elif ftype == "s": this[name] = ftype, w.color( '*214') + f"{w.infolist_string(ptr, name)!r}" elif ftype == "p": this[ name] = ftype, f"{w.color('*yellow') + w.infolist_pointer(ptr, name)}" elif ftype == "t": this[ name] = ftype, f"{w.color('magenta') + w.infolist_time(ptr, name)}" else: this[name] = "(not available in scripting API)" infos.append(this) for item in infos: pad = 0 for key in item: if len(key) > pad: pad = len(key) for name, (ftype, value) in item.items(): w.prnt( buf, w.color('blue') + f"{name: <{pad}}" + w.color('reset') + f" = ({ftype}) {value}") w.prnt(buf, f"{w.color('red')}---") w.buffer_set(buf, "title", f"infolist {ptr}, iterated {len(infos)} item(s)")
def test_infolist(): """Test infolist functions.""" hook_infolist = weechat.hook_infolist('infolist_test_script', 'description', '', '', 'infolist_cb', '') check(weechat.infolist_get('infolist_does_not_exist', '', '') == '') ptr_infolist = weechat.infolist_get('infolist_test_script', '', '') check(ptr_infolist != '') check(weechat.infolist_next(ptr_infolist) == 1) check(weechat.infolist_integer(ptr_infolist, 'integer') == 123) check(weechat.infolist_string(ptr_infolist, 'string') == 'test string') check(weechat.infolist_pointer(ptr_infolist, 'pointer') == '0xabcdef') check(weechat.infolist_time(ptr_infolist, 'time') == 1231231230) check(weechat.infolist_fields(ptr_infolist) == 'i:integer,s:string,p:pointer,t:time') check(weechat.infolist_next(ptr_infolist) == 0) weechat.infolist_free(ptr_infolist) weechat.unhook(hook_infolist)
def infolist_display(buffer, args): items = args.split(" ", 1) infolist_args = "" infolist_pointer = "" if len(items) >= 2: infolist_args = items[1] if infolist_args[:2] == "0x": infolist_pointer, sep, infolist_args = infolist_args.partition(" ") elif infolist_args[:3] == "\"\" ": infolist_args = infolist_args[3:] infolist = weechat.infolist_get(items[0], infolist_pointer, infolist_args) if infolist == "": weechat.prnt_date_tags(buffer, 0, "no_filter", "%sInfolist '%s' not found." % (weechat.prefix("error"), items[0])) return weechat.WEECHAT_RC_OK item_count = 0 weechat.buffer_clear(buffer) weechat.prnt(buffer, "") count = 0 while weechat.infolist_next(infolist): item_count += 1 if item_count > 1: weechat.prnt(buffer, "") fields = weechat.infolist_fields(infolist).split(",") for field in fields: (type, name) = field.split(":", 1) if name != 'host': continue value = weechat.infolist_string(infolist, name) name_end = "." * (30 - len(name)) weechat.prnt(buffer, "%s: %s%s" % (name, weechat.color("cyan"), value)) prefix = "" count += 1 if count == 0: weechat.prnt_date_tags(buffer, 0, "no_filter", "Empty infolist.") weechat.infolist_free(infolist) return weechat.WEECHAT_RC_OK
def dev_iter(ptr): """ iterate all fields of all items of an infolist pointer """ buf = w.buffer_new(f"infolist {ptr}", "", "", "", "") infos = [] # type: List[Dict[str, Tuple]] while w.infolist_next(ptr): this = {} for field in w.infolist_fields(ptr).split(","): ftype, name = field.split(":", 1) if ftype == "i": this[name] = ftype, f"{w.color('*lightgreen') + str(w.infolist_integer(ptr, name))}" elif ftype == "s": this[name] = ftype, w.color('*214') + f"{w.infolist_string(ptr, name)!r}" elif ftype == "p": this[name] = ftype, f"{w.color('*yellow') + w.infolist_pointer(ptr, name)}" elif ftype == "t": this[name] = ftype, f"{w.color('magenta') + w.infolist_time(ptr, name)}" else: this[name] = "(not available in scripting API)" infos.append(this) for item in infos: pad = 0 for key in item: if len(key) > pad: pad = len(key) for name, (ftype, value) in item.items(): w.prnt(buf, w.color('blue') + f"{name: <{pad}}" + w.color('reset') + f" = ({ftype}) {value}") w.prnt(buf, f"{w.color('red')}---") w.buffer_set(buf, "title", f"infolist {ptr}, iterated {len(infos)} item(s)")
# buffer2 = weechat.info_get("irc_buffer", "undernet,#clinkclank") weechat.command(buffer2, "hello IM A BOT") weechat.prnt( buffer2, "Hello, from python script! AND F**K OFF - printed to buffer, ocal only") weechat.prnt(buffer2, buffer2) #weechat.hook_signal_send("irc_input_send", weechat.WEECHAT_HOOK_SIGNAL_STRING, "freenode;;priority_low;;/whois FlashCode") buffer2 = weechat.info_get("irc_buffer", "undernet,#rriiyh7h7d8fg") weechat.command(buffer2, "hello IM A BOT") weechat.prnt( buffer2, "Hello, from python script! AND F**K OFF - printed to buffer, ocal only") weechat.prnt(buffer2, buffer2) buffer2 = weechat.info_get("irc_buffer", "undernet,#bookz") weechat.prnt(buffer2, buffer2) chans = weechat.infolist_get("irc_channel", "", "") weechat.prnt(buffer, weechat.infolist_fields(chans)) """ if chans: while weechat.infolist_next(chans): name = weechat.infolist_string(chans, "name") weechat.prnt(buffer, "buffer: %s" % name) weechat.infolist_free(chans) """
def infolist_display(buffer, args): global infolist_var_type items = args.split(" ", 1) infolist_args = "" infolist_pointer = "" if len(items) >= 2: infolist_args = items[1] if infolist_args[:2] == "0x": infolist_pointer, sep, infolist_args = infolist_args.partition(" ") elif infolist_args[:3] == "\"\" ": infolist_args = infolist_args[3:] infolist = weechat.infolist_get(items[0], infolist_pointer, infolist_args) if infolist == "": weechat.prnt_date_tags(buffer, 0, "no_filter", "%sInfolist '%s' not found." % (weechat.prefix("error"), items[0])) return weechat.WEECHAT_RC_OK item_count = 0 weechat.buffer_clear(buffer) weechat.prnt_date_tags(buffer, 0, "no_filter", "Infolist '%s', with pointer '%s' and arguments '%s':" % (items[0], infolist_pointer, infolist_args)) weechat.prnt(buffer, "") count = 0 while weechat.infolist_next(infolist): item_count += 1 if item_count > 1: weechat.prnt(buffer, "") fields = weechat.infolist_fields(infolist).split(",") prefix = "%s[%s%d%s]\t" % (weechat.color("chat_delimiters"), weechat.color("chat_buffer"), item_count, weechat.color("chat_delimiters")) for field in fields: (type, name) = field.split(":", 1) value = "" quote = "" if type == "i": value = weechat.infolist_integer(infolist, name) elif type == "s": value = weechat.infolist_string(infolist, name) quote = "'" elif type == "p": value = weechat.infolist_pointer(infolist, name) elif type == "t": value = weechat.infolist_time(infolist, name) name_end = "." * (30 - len(name)) weechat.prnt_date_tags(buffer, 0, "no_filter", "%s%s%s: %s%s%s %s%s%s%s%s%s" % (prefix, name, name_end, weechat.color("brown"), infolist_var_type[type], weechat.color("chat"), weechat.color("chat"), quote, weechat.color("cyan"), value, weechat.color("chat"), quote)) prefix = "" count += 1 if count == 0: weechat.prnt_date_tags(buffer, 0, "no_filter", "Empty infolist.") weechat.infolist_free(infolist) return weechat.WEECHAT_RC_OK
def cmd_help_cb(data, buffer, args): """Callback for /cmd_help command.""" cmd_help_toggle() return weechat.WEECHAT_RC_OK if __name__ == '__main__' and import_ok: if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', ''): # set allowed fields in option "format_option" fields = [] infolist = weechat.infolist_get('option', '', 'weechat.plugin.*') if infolist: if weechat.infolist_next(infolist): strfields = weechat.infolist_fields(infolist) for field in strfields.split(','): items = field.split(':', 1) if len(items) == 2: fields.append(items[1]) weechat.infolist_free(infolist) if fields: cmdhelp_settings_default['format_option'][1] += ( ': %s' % ', '.join(fields)) # set default settings version = weechat.info_get("version_number", "") or 0 for option, value in cmdhelp_settings_default.items(): if weechat.config_is_set_plugin(option): cmdhelp_settings[option] = weechat.config_get_plugin(option) else:
def infolist_display(buffer, args): global infolist_var_type items = args.split(" ", 1) infolist_args = "" infolist_pointer = "" if len(items) >= 2: infolist_args = items[1] if infolist_args[:2] == "0x": infolist_pointer, sep, infolist_args = infolist_args.partition(" ") elif infolist_args[:3] == "\"\" ": infolist_args = infolist_args[3:] infolist = weechat.infolist_get(items[0], infolist_pointer, infolist_args) if infolist == "": weechat.prnt_date_tags( buffer, 0, "no_filter", "%sInfolist '%s' not found." % (weechat.prefix("error"), items[0])) return weechat.WEECHAT_RC_OK item_count = 0 weechat.buffer_clear(buffer) weechat.prnt_date_tags( buffer, 0, "no_filter", "Infolist '%s', with pointer '%s' and arguments '%s':" % (items[0], infolist_pointer, infolist_args)) weechat.prnt(buffer, "") count = 0 while weechat.infolist_next(infolist): item_count += 1 if item_count > 1: weechat.prnt(buffer, "") fields = weechat.infolist_fields(infolist).split(",") prefix = "%s[%s%d%s]\t" % (weechat.color("chat_delimiters"), weechat.color("chat_buffer"), item_count, weechat.color("chat_delimiters")) for field in fields: (type, name) = field.split(":", 1) value = "" quote = "" if type == "i": value = weechat.infolist_integer(infolist, name) elif type == "s": value = weechat.infolist_string(infolist, name) quote = "'" elif type == "p": value = weechat.infolist_pointer(infolist, name) elif type == "t": value = weechat.infolist_time(infolist, name) # since WeeChat 2.2, infolist_time returns a long integer # instead of a string if not isinstance(value, str): str_date = time.strftime('%F %T', time.localtime(int(value))) value = '%d (%s)' % (value, str_date) name_end = "." * (30 - len(name)) weechat.prnt_date_tags( buffer, 0, "no_filter", "%s%s%s: %s%s%s %s%s%s%s%s%s" % (prefix, name, name_end, weechat.color("brown"), infolist_var_type[type], weechat.color("chat"), weechat.color("chat"), quote, weechat.color("cyan"), value, weechat.color("chat"), quote)) prefix = "" count += 1 if count == 0: weechat.prnt_date_tags(buffer, 0, "no_filter", "Empty infolist.") weechat.infolist_free(infolist) return weechat.WEECHAT_RC_OK
def cmd_help_cb(data, buffer, args): """Callback for /cmd_help command.""" cmd_help_toggle() return weechat.WEECHAT_RC_OK if __name__ == '__main__' and import_ok: if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', ''): # set allowed fields in option "format_option" fields = [] infolist = weechat.infolist_get('option', '', 'weechat.plugin.*') if infolist: if weechat.infolist_next(infolist): strfields = weechat.infolist_fields(infolist) for field in strfields.split(','): items = field.split(':', 1) if len(items) == 2: fields.append(items[1]) weechat.infolist_free(infolist) if fields: cmdhelp_settings_default['format_option'][1] += ': %s' % ', '.join( fields) # set default settings version = weechat.info_get("version_number", "") or 0 for option, value in cmdhelp_settings_default.items(): if weechat.config_is_set_plugin(option): cmdhelp_settings[option] = weechat.config_get_plugin(option) else: