python-plexapi/tools/plex-listdocattrs.py

52 lines
1.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Plex-ListDocAttrs is used during development of PlexAPI.
Example usage: AttDS(dict or object).write()
"""
2017-03-03 22:32:16 +00:00
import re
from collections import OrderedDict
2017-03-03 22:32:16 +00:00
def type_finder(s):
type_string = str(type(s))
x = re.search("'(.+)'", type_string)
if x:
return x.group(1)
return ''
class AttDS(object):
""" Helper that prints docstring attrs. """
2017-03-03 22:32:16 +00:00
def __init__(self, o, keys=None, style='google'):
self.__o = o
if not isinstance(o, dict):
self.o = o.__dict__.items()
self._as_dict = o.__dict__
else:
self.o = o.items()
self._as_dict = o
if keys is None:
self.keys = self._as_dict.keys()
else:
self.keys = keys
if style == 'google':
self.template = '%s (%s): %s'
self.res_dict = OrderedDict()
self.parse()
def parse(self):
for k, v in sorted(self.o, key=lambda k: k[0]):
if self.keys:
ds = ''
for key in self.keys:
ds += '%s=%s ' % (key, self._as_dict.get(key, ''))
else:
ds = ''
self.res_dict[k] = self.template % (k, type_finder(v), ds)
def write(self):
for k, v in self.res_dict.items():
print(v)