Aug-30-2022, 05:25 AM
(This post was last modified: Aug-30-2022, 03:03 PM by deanhystad.)
Context helps when the question doesn't make any sense (my issue, not yours). I'm still a little fuzzy about this entire thread. But if you do want to save information in a file, and you like Gribouillis' post, I think you will like json even better.
An advantage of json over csv is that there are no intermediate steps required to translate the Python object you want to save to something that can be written to a file. The json dump command does this for you. The same goes in the reverse direction using json load. The result of reading (loading) a json file is a Python object that you can use without any further processing.
Here I translate Gribouillis' csv code to similar code that uses json. I changed the channels datatype from list to dictionary, but json can also read/write a list of lists.
I'm still not seeing the reason for a file unless you need some kind of persistence.
I did take a shot at modifying your "new" code to be more like the old. Instead of a static network and list of channels, that information is loaded from configuration information the first time limiter_cb runs. From then on it looks exactly like your old code.
An advantage of json over csv is that there are no intermediate steps required to translate the Python object you want to save to something that can be written to a file. The json dump command does this for you. The same goes in the reverse direction using json load. The result of reading (loading) a json file is a Python object that you can use without any further processing.
Here I translate Gribouillis' csv code to similar code that uses json. I changed the channels datatype from list to dictionary, but json can also read/write a list of lists.
import json from pathlib import Path source = "plugins.var.python.limiter.limiter_chans.ircnet #python,#forum" # extract the list ['#python', '#forum'] ichan = [x.strip() for x in source.split(' ', 1)[1].strip().split(',')] # create the dictionary channels = {x:0 for x in ichan} # optional: save content to file filename = Path(__file__).parent/'channels.json' with open(filename, "w") as file: json.dump(channels, file) # optional: read file to get content with open(filename, "r") as file: channels = json.load(channels, file) print(channels) # prints {'#python':0, '#forum':0}The disadvantage of json is that a csv file is prettier when you print it out. Json files are human readable but look more like html than a table.
I'm still not seeing the reason for a file unless you need some kind of persistence.
I did take a shot at modifying your "new" code to be more like the old. Instead of a static network and list of channels, that information is loaded from configuration information the first time limiter_cb runs. From then on it looks exactly like your old code.
# Simple Channel limiter version 0.1. # Protection script against mass joining from clones/botnets on your channel. ''' /set plugins.var.python.limiter.limiter_chans.ircnet #python,#forum ''' from weechat import ( WEECHAT_RC_OK, register, infolist_get, infolist_string, buffer_search, config_get_plugin, infolist_next, string_eval_expression, command, hook_timer, infolist_free) NAME = 'limiter' AUTHOR = 'fozz <[email protected]>' VERSION = '0.1' LICENSE = 'GPL3' DESCRIPTION = 'Channel limiter, protection against mass joining from clones/botnets' register(NAME, AUTHOR, VERSION, LICENSE, DESCRIPTION, '', '') # Configuration limit_ = 5 min_offset_ = 2 #lowest diff max_offset_ = 2 #highest diff interval_ = 1 channels_ = [] class Channel: def __init__(self, network, channel, offset=0): self.network = network self.channel = channel self.offset = offset def initialize_channel_list(): networks = infolist_get('irc_server', '', '') while infolist_next(networks): network = infolist_string(networks, 'name') limiter_chans = config_get_plugin('limiter_chans.{}'.format(network)) if limiter_chans: for channel in limiter_chans.split(','): channels_.append(Channel(network, channel)) infolist_free(networks) def limiter_cb(data, signal): if len(channels_) == 0: initialize_channel_list() for channel in channels_: buffer = buffer_search('irc', '{}.{}'.format(channel.network, channel.channel)) if buffer: nc = int(string_eval_expression('${buffer.nicklist_visible_count}', {'buffer': buffer}, {}, {})) nl = nc + limit_ minoff = nl - min_offset_ maxoff = nl + max_offset_ if channel.offset <= minoff or channel.offset >= maxoff: command(buffer, '/mode ' + channel + ' +l ' + f'{nl}') channel.offset = nl # save value for next time return WEECHAT_RC_OK hook_timer(interval_ * 60000, 0, 0, 'limiter_cb', '')