-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
41 lines (37 loc) · 1.31 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Slack graphical user interface views.
This module loads every sibling JSON file from the file system and exposes
their contents in a more pythonic way.
>>> import interface
>>> interface.example
{"example": true}
"""
import collections
import pathlib
import json
class View(collections.UserDict):
"""Slack user interface view."""
def format(self, *arguments, function=None, **keyword_arguments):
"""Applies the `format()` method and, optionally, `function()` to
every string on the view object."""
def recurse(object):
if type(object) is dict:
return {
key.format(*arguments, **keyword_arguments):
recurse(value)
for key, value in object.items()
}
elif type(object) is list:
return [
recurse(item)
for item in object
]
elif type(object) is str:
formatted = object.format(*arguments, **keyword_arguments)
return function(formatted) if function else formatted
else:
return object
return recurse(dict(self))
views = {
path.stem: View(json.load(open(path)))
for path in pathlib.Path(__file__).parent.glob('*.json')
}