Created
March 1, 2020 05:42
-
-
Save gitcrtn/e7a5499eec5d76743794fe76099f863f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
dump UPnP actions | |
Requirements: | |
- upnpclient | |
- pyyaml | |
- click | |
Usage: | |
python dump_upnp_actions.py -d <output_dir> | |
""" | |
import os | |
import sys | |
from contextlib import contextmanager | |
from io import StringIO | |
import upnpclient | |
import yaml | |
import click | |
@contextmanager | |
def drop_err(): | |
sys.stderr = StringIO() | |
try: | |
yield | |
finally: | |
sys.stderr = sys.__stderr__ | |
def convert_argsdef(argsdef): | |
return { | |
argname: { | |
specname: str(specval) | |
for specname, specval in specs.items() | |
} | |
for argname, specs in argsdef | |
} | |
def dump(out_dir): | |
with drop_err(): | |
devices = upnpclient.discover() | |
for device in devices: | |
udn = device.udn | |
name = device.friendly_name | |
url = device.location | |
sresults = {} | |
services = device.services | |
for service in services: | |
sname = service.name | |
aresults = {} | |
actions = service.actions | |
for action in actions: | |
aname = action.name | |
aurl = action.url | |
args_in = convert_argsdef(action.argsdef_in) | |
args_out = convert_argsdef(action.argsdef_out) | |
aresults[aname] = { | |
'url': aurl, | |
'in': args_in, | |
'out': args_out, | |
} | |
sresults[sname] = { | |
'actions': aresults, | |
} | |
result = { | |
'name': name, | |
'url': url, | |
'udn': udn, | |
'services': sresults, | |
} | |
filename = '{0}.yml'.format(udn.replace(':','-')) | |
filepath = os.path.join(out_dir, filename) | |
with open(filename, 'w') as fp: | |
yaml.dump(result, fp) | |
@click.command() | |
@click.option('-d', 'out_dir', default=os.getcwd()) | |
def main(out_dir): | |
print('Output Dir: ' + out_dir) | |
if not os.path.isdir(out_dir): | |
print('Error: No such as directory: %s' % out_dir) | |
return | |
dump(out_dir) | |
if __name__ == '__main__': | |
main() | |
# EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment