Move main logic to larigira.py

This commit is contained in:
boyska 2015-02-01 00:08:13 +01:00
parent 05144d0c2f
commit 24cda62c5f
No known key found for this signature in database
GPG key ID: 7395DCAE58289CA9
5 changed files with 65 additions and 68 deletions

View file

@ -7,7 +7,6 @@ logging.basicConfig(level=logging.INFO,
format=FORMAT,
datefmt='%H:%M:%S')
logging.getLogger('mpd').setLevel(logging.WARNING)
import signal
from datetime import datetime, timedelta
import gevent
@ -135,25 +134,3 @@ class Monitor(ParentedLet):
raise NotImplementedError()
else:
self.log.warning("Unknown message: %s" % str(value))
def on_player_crash(*args, **kwargs):
print('A crash occurred in "main" greenlet. Aborting...')
import sys
sys.exit(1)
def main():
conf = dict(CACHING_TIME=10, DB_URI='larigira.db')
monitor = Monitor(Queue(), conf)
monitor.start()
monitor.link_exception(on_player_crash)
def sig(*args):
print('invoked sig', args)
monitor.q.put('signal', *args)
gevent.signal(signal.SIGHUP, sig, signal.SIGHUP)
gevent.wait()
if __name__ == '__main__':
main()

62
larigira/larigira.py Normal file
View file

@ -0,0 +1,62 @@
'''
This module is for the main application logic
'''
from __future__ import print_function
from gevent import monkey
monkey.patch_all(subprocess=True)
import sys
import signal
import logging
FORMAT = '%(asctime)s|%(levelname)s[%(name)s:%(lineno)d] %(message)s'
logging.basicConfig(level=logging.INFO,
format=FORMAT,
datefmt='%H:%M:%S')
import gevent
from gevent.wsgi import WSGIServer
from .mpc import Player
from .event import Monitor
from .config import get_conf
from .rpc import create_app
def on_player_crash(*args, **kwargs):
print('A crash occurred in "main" greenlet. Aborting...')
sys.exit(1)
class Larigira(object):
def __init__(self):
self.log = logging.getLogger('larigira')
self.conf = get_conf()
self.player = Player(self.conf)
if 'DB_URI' in self.conf:
self.monitor = Monitor(self.player.q, self.conf)
else:
self.monitor = None
self.player.link_exception(on_player_crash)
self.http_server = WSGIServer(('', 5000),
create_app(self.player.q, self))
def start(self):
self.player.start()
if self.monitor is not None:
self.monitor.start()
self.http_server.start()
def main():
logging.basicConfig(level=logging.DEBUG)
larigira = Larigira()
larigira.start()
def sig(*args):
print('invoked sig', args)
larigira.player.q.put('signal', *args)
gevent.signal(signal.SIGHUP, sig, signal.SIGHUP)
gevent.wait()
if __name__ == '__main__':
main()

View file

@ -1,24 +1,12 @@
from __future__ import print_function
from gevent import monkey
monkey.patch_all(subprocess=True)
import logging
FORMAT = '%(asctime)s|%(levelname)s[%(name)s:%(lineno)d] %(message)s'
logging.basicConfig(level=logging.INFO,
format=FORMAT,
datefmt='%H:%M:%S')
import signal
import gevent
from gevent.queue import Queue
from gevent.wsgi import WSGIServer
from mpd import MPDClient
from eventutils import ParentedLet, Timer
import rpc
from audiogen import audiogenerate
from event import Monitor
from .config import get_conf
class MpcWatcher(ParentedLet):
@ -50,7 +38,6 @@ class Player(gevent.Greenlet):
def _get_mpd(self):
mpd_client = MPDClient()
# TODO: use config values
mpd_client.connect(self.conf['MPD_HOST'], self.conf['MPD_PORT'])
return mpd_client
@ -69,15 +56,12 @@ class Player(gevent.Greenlet):
mpd_client = self._get_mpd()
for song in songs:
self.log.info('Adding {} to playlist'.format(song))
print(mpd_client.playlistid())
pos = min(1, len(mpd_client.playlistid()))
mpd_client.addid(song, pos)
def _run(self):
MpcWatcher(self.q, self.conf, client=None).start()
Timer(60 * 1000, self.q).start()
http_server = WSGIServer(('', 5000), rpc.create_app(self.q))
http_server.start()
while True:
value = self.q.get()
self.log.debug('<- %s' % str(value))
@ -92,30 +76,3 @@ class Player(gevent.Greenlet):
self.enqueue(args[0])
else:
self.log.warning("Unknown message: %s" % str(value))
def on_player_crash(*args, **kwargs):
print('A crash occurred in "main" greenlet. Aborting...')
import sys
sys.exit(1)
def main():
conf = get_conf()
logging.basicConfig(level=logging.DEBUG)
p = Player(conf)
p.start()
# TODO: if <someoption> create Monitor(p.q)
if 'DB_URI' in conf:
m = Monitor(p.q, conf)
m.start()
p.link_exception(on_player_crash)
def sig(*args):
print('invoked sig', args)
p.q.put('signal', *args)
gevent.signal(signal.SIGHUP, sig, signal.SIGHUP)
gevent.wait()
if __name__ == '__main__':
main()

View file

@ -47,8 +47,9 @@ def rpc_wip():
return jsonify(dict(greenlets=greenlets))
def create_app(queue):
def create_app(queue, larigira):
app = Flask(__name__)
app.register_blueprint(rpc)
app.queue = queue
app.larigira = larigira
return app

View file

@ -46,7 +46,7 @@ setup(name='larigira',
cmdclass={'test': PyTest},
zip_safe=False,
entry_points={
'console_scripts': ['larigira=larigira.mpc:main',
'console_scripts': ['larigira=larigira.larigira:main',
'larigira-timegen=larigira.timegen:main',
'larigira-audiogen=larigira.audiogen:main',
'larigira-dbmanage=larigira.event_manage:main'],