-
Notifications
You must be signed in to change notification settings - Fork 11
/
service.py
executable file
·100 lines (71 loc) · 2.91 KB
/
service.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
import argparse
import asyncio
import signal
import logbook
import uvloop
import sys
from logbook import Logger, StreamHandler
from wallabag_kindle_consumer import models
from wallabag_kindle_consumer.config import Config
from wallabag_kindle_consumer.consumer import Consumer
from wallabag_kindle_consumer.interface import App
from wallabag_kindle_consumer.refresher import Refresher
from wallabag_kindle_consumer.sender import Sender
from wallabag_kindle_consumer.wallabag import Wallabag
logger = Logger("kindle-consumer")
def parse_args():
parser = argparse.ArgumentParser(description="Wallabag-Kindle-Consumer")
parser.add_argument("--cfg", help="config file", required=False)
parser.add_argument("--env", help="Read config from env", action="store_true")
parser.add_argument("--refresher", help="Start token refresher", action="store_true")
parser.add_argument("--interface", help="Start web interface", action="store_true")
parser.add_argument("--consumer", help="Start article consumer", action="store_true")
parser.add_argument("--create_db", help="Try to create the db", action="store_true")
parser.add_argument("--debug", help="Enable debug logging", action="store_true")
return parser.parse_args()
if __name__ == "__main__":
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
loop = asyncio.get_event_loop()
args = parse_args()
level = logbook.INFO
if args.debug:
level = logbook.DEBUG
StreamHandler(sys.stdout, level=level).push_application()
config = Config.from_file("config.ini")
if 'cfg' in args and args.cfg is not None:
new = Config.from_file(args.cfg)
if new is not None:
config = new
if 'env' in args and args.env:
new = Config.from_env()
if new is not None:
config = new
if args.create_db:
models.create_db(config)
logger.info("Database created.")
on_stop = []
def _stop():
for cb in on_stop:
cb()
loop.stop()
loop.add_signal_handler(signal.SIGTERM, _stop)
loop.add_signal_handler(signal.SIGINT, _stop)
wallabag = Wallabag(config)
sender = Sender(loop, config.smtp_from, config.smtp_host, config.smtp_port, config.smtp_user, config.smtp_passwd)
if args.refresher:
logger.info("Create Refresher")
refresher = Refresher(config, wallabag, sender)
loop.create_task(refresher.refresh())
on_stop.append(lambda: refresher.stop())
if args.consumer:
logger.info("Create Consumer")
consumer = Consumer(wallabag, config, sender)
loop.create_task(consumer.consume())
on_stop.append(lambda: consumer.stop())
if args.interface:
logger.info("Create Interface")
webapp = App(config, wallabag)
loop.create_task(webapp.register_server())
on_stop.append(lambda: webapp.stop())
loop.run_forever()