On 3/4/20 6:26 AM, sean_sullivan@rhoworld.com wrote:
However, if I try and bring that in as a function that can be used with mailman withlist, I am consistently getting errors, and being that I am not very experienced in python, it has me scratching my head to say the least.
from mailmanclient import Client
You do not need to use mailmanclient with shell/withlist. mailmanclient
is Python bindings for REST API functions. When you are under mailman shell
(or mailman withlist
, a synonym for mailman shell
) you are
working within mailman core and it's just extra baggage to use
mailmanclient/REST.
from mailman.interfaces.member import MemberRole, AlreadySubscribedError from mailman.testing.helpers import subscribe
Here again, mailman.testing.helpers is to provide certain functions used in doctests. You don't need that with mailman shell.
addresses = ['test1@example.com', 'test2@example.com', 'test3@example.com']
client = Client('http://localhost:8001/3.1', 'restadmin', 'restpass')
def set_owners(mlist):
for email_addr in addresses: try: member = subscribe(mlist, '', email=email_addr, role=MemberRole.owner )
See below.
print (member) except AlreadySubscribedError as err: print (err)
def remove(mlist): my_list = client.get_list(mlist) # print(my_list.fqdn_listname) # for email_addr in addresses: # my_list.remove_owner(email_addr)
The errors I am getting are:
./bin/mailman withlist -r set_owners.remove -l it-test-b@lists.rhoworld.com Traceback (most recent call last): File "./bin/mailman", line 8, in <module> sys.exit(main()) File "/opt/mailman3/lib/python3.6/site-packages/click/core.py", line 764, in __call__ return self.main(*args, **kwargs) File "/opt/mailman3/lib/python3.6/site-packages/click/core.py", line 717, in main rv = self.invoke(ctx) File "/opt/mailman3/lib/python3.6/site-packages/mailman/bin/mailman.py", line 68, in invoke return super().invoke(ctx) File "/opt/mailman3/lib/python3.6/site-packages/click/core.py", line 1137, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/opt/mailman3/lib/python3.6/site-packages/click/core.py", line 956, in invoke return ctx.invoke(self.callback, **ctx.params) File "/opt/mailman3/lib/python3.6/site-packages/click/core.py", line 555, in invoke return callback(*args, **kwargs) File "/opt/mailman3/lib/python3.6/site-packages/click/decorators.py", line 17, in new_func return f(get_current_context(), *args, **kwargs) File "/opt/mailman3/lib/python3.6/site-packages/mailman/commands/cli_withlist.py", line 293, in shell r = call_name(dotted_name, m, *run_args) File "/opt/mailman3/lib/python3.6/site-packages/mailman/utilities/modules.py", line 70, in call_name return named_callable(*args, **kws) File "/opt/mailman3/bin/set_owners.py", line 24, in remove my_list = client.get_list(mlist) File "/opt/mailman3/lib/python3.6/site-packages/mailmanclient/client.py", line 347, in get_list 'lists/{0}'.format(fqdn_listname)) File "/opt/mailman3/lib/python3.6/site-packages/mailmanclient/restbase/connection.py", line 112, in call error_msg, response, None) urllib.error.HTTPError: HTTP Error 404: 404 Not Found
So, I am wondering, is it possible to do something like this or am I completely misunderstanding the documentation, and why do I get a 404 here, when in the shell everything works as expected. I know I must be missing something, but cannot for the life of me figure out what that is. Any help would be greatly appreciated.
Here's what you want with addresses as arguments so you can save this as set_owners.py and run things like.
$ bin/mailman shell --run set_owners -l list.example.com
test1@example.com test2@example.com test3@example.com'
and
$ bin/mailman shell --run set_owners.remove -l list.example.com
test1@example.com test2@example.com test3@example.com'
from mailman.app.membership import add_member, delete_member from mailman.interfaces.member import MemberRole, AlreadySubscribedError from mailman.interfaces.subscriptions import RequestRecord
def set_owners(mlist, *addresses): for email_addr in addresses: try: record = RequestRecord(email_addr) member = add_member(mlist, record, role=MemberRole.owner) print (member) except AlreadySubscribedError as err: print (err)
def remove(mlist, *addresses): for owner in mlist.owners.members: if owner.address.email in addresses: owner.unsubscribe()
-- Mark Sapiro <mark@msapiro.net> The highway is for gamblers, San Francisco Bay Area, California better use your sense - B. Dylan