-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.py
62 lines (46 loc) · 2.56 KB
/
notify.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os, sys, argparse, json, yaml
from notifications_python_client.notifications import NotificationsAPIClient
class GCNotifySourceController(object):
api_key: str
def __init__(self) -> None:
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('-k', '--key', help='Supply a GC Notify API key.')
arg_parser.add_argument('-p', '--pull', help='Flag to get all templates and parse them into yaml files.', action='store_true')
arg_parser.add_argument('-c', '--commit', help='Flag to commit the templates.', action='store_true')
args = arg_parser.parse_args()
if len(sys.argv) < 2:
arg_parser.print_help(sys.stderr)
return
if args.key:
self.set_key(args.key)
if args.pull:
self.pull()
if args.commit:
self.commit()
def set_key(self, api_key: str) -> str:
self.api_key = api_key
def pull(self) -> None:
notificactions_client = NotificationsAPIClient(self.api_key)
print('Retrieving all templates')
templates = notificactions_client.get_all_templates() # get all templates
if not templates: return # exit if there are no templates
with open('./dump.json', mode='w') as file: # write the templates json to dump file
print('Writing json dump file')
file.write(json.dumps(templates))
def commit(self) -> None:
with open('./dump.json', mode='r') as file: # read templates json dump file
print('Reading json dump file')
templates = json.loads(file.read()) # load the object from string
if not os.path.isdir('./templates'): # create templates directory if it does not exist
print('Creating templates directory')
os.mkdir('./templates')
if not templates['templates']: return # exit if dump object does not have templates
print('Deleting template yaml files')
for filename in os.listdir('./templates'): # delete all templates
os.remove('./templates/{}'.format(filename))
for template in templates['templates']:
with open('./templates/{}.yaml'.format(template['name']), mode='w') as file: # write template data to its yaml file
print('Writing yaml file templates/{}.yaml'.format(template['name']))
template['body'] = '\n' + template['body'].replace('\r\n','\n') + '\n'
yaml.dump(template, file, default_flow_style=False, default_style='', allow_unicode=True)
source_controller = GCNotifySourceController()