-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
65 lines (50 loc) · 1.81 KB
/
app.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
63
64
65
import os
import asyncio
import aiomisc
import yaml
import logging
import logging.config
import importlib
from datetime import datetime
from pprint import pformat
class Moneybot:
"""An aggregator for the main routine"""
def __init__(self, config):
self._config = config
self.pullers = []
for p in ('privat',):
cc = config['pullers'][p]
pp = importlib.import_module(f'moneybot.pullers.{p}').Puller(
cc, prefix=config['db_prefix'],
monthly_donate=config['monthly_donate'])
self.pullers.append(pp)
self.pushers = []
for p in ('slack', 'csv'):
cc = config['pushers'][p]
pp = importlib.import_module(f'moneybot.pushers.{p}').Pusher(
cc, prefix=config['db_prefix'],
monthly_donate=config['monthly_donate'])
self.pushers.append(pp)
async def flow(self):
"""The main loop"""
while True:
await asyncio.gather(*[p.pull() for p in self.pullers])
now = datetime.now()
await asyncio.gather(*[
ps.push(pl.stats, now=now, provider=str(pl))
for pl in self.pullers for ps in self.pushers
])
await asyncio.sleep(self._config['update_interval'])
async def main():
config = yaml.safe_load(open('config.yml', 'r'))
logging_config = yaml.safe_load(open('logging.yml', 'r'))
logging_config['root']['level'] = config['log_level']
logging.config.dictConfig(logging_config)
logging.debug('Config: {}'.format(pformat(config)))
db_prefix = config['db_prefix']
if not os.path.exists(f'./{db_prefix}'):
os.mkdir(db_prefix)
mb = Moneybot(config)
await mb.flow()
with aiomisc.entrypoint() as loop:
loop.run_until_complete(main())