forked from rc/aircox
		
	make the controllers' manager; fix errors, make it working
This commit is contained in:
		@ -56,7 +56,6 @@ class Connector:
 | 
			
		||||
            if data:
 | 
			
		||||
                data = reg.sub(r'\1', data)
 | 
			
		||||
                data = data.strip()
 | 
			
		||||
 | 
			
		||||
                if parse:
 | 
			
		||||
                    data = self.parse(data)
 | 
			
		||||
                elif parse_json:
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,6 @@
 | 
			
		||||
import os
 | 
			
		||||
import subprocess
 | 
			
		||||
import atexit
 | 
			
		||||
 | 
			
		||||
import aircox.controllers.plugins.plugins as plugins
 | 
			
		||||
from aircox.controllers.plugins.connector import Connector
 | 
			
		||||
@ -29,7 +31,10 @@ class StationController(plugins.StationController):
 | 
			
		||||
        self.connector = Connector(self.socket_path)
 | 
			
		||||
 | 
			
		||||
    def _send(self, *args, **kwargs):
 | 
			
		||||
        self.connector.send(*args, **kwargs)
 | 
			
		||||
        return self.connector.send(*args, **kwargs)
 | 
			
		||||
 | 
			
		||||
    def __get_process_args(self):
 | 
			
		||||
        return ['liquidsoap', '-v', self.path]
 | 
			
		||||
 | 
			
		||||
    def fetch(self):
 | 
			
		||||
        super().fetch()
 | 
			
		||||
@ -38,19 +43,22 @@ class StationController(plugins.StationController):
 | 
			
		||||
        if not rid:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        data = self._send('request.metadata', rid, parse = True)
 | 
			
		||||
        data = self._send('request.metadata ', rid, parse = True)
 | 
			
		||||
        if not data:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        self.current_sound = data.get('initial_uri')
 | 
			
		||||
        self.current_source = [
 | 
			
		||||
            # we assume sound is always from a registered source
 | 
			
		||||
            source for source in self.station.get_sources()
 | 
			
		||||
            if source.rid == rid
 | 
			
		||||
        ][0]
 | 
			
		||||
        try:
 | 
			
		||||
            self.current_source = next(
 | 
			
		||||
                source for source in self.station.get_sources()
 | 
			
		||||
                if source.controller.rid == rid
 | 
			
		||||
            )
 | 
			
		||||
        except:
 | 
			
		||||
            self.current_source = None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SourceController(plugins.SourceController):
 | 
			
		||||
    rid = None
 | 
			
		||||
    connector = None
 | 
			
		||||
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
@ -58,7 +66,7 @@ class SourceController(plugins.SourceController):
 | 
			
		||||
        self.connector = self.source.station.controller.connector
 | 
			
		||||
 | 
			
		||||
    def _send(self, *args, **kwargs):
 | 
			
		||||
        self.connector.send(*args, **kwargs)
 | 
			
		||||
        return self.connector.send(*args, **kwargs)
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def active(self):
 | 
			
		||||
@ -76,7 +84,7 @@ class SourceController(plugins.SourceController):
 | 
			
		||||
        self._send(self.source.slug, '.skip')
 | 
			
		||||
 | 
			
		||||
    def fetch(self):
 | 
			
		||||
        data = self._send(self.source.slug, '.get', parse = True)
 | 
			
		||||
        data = self._send(self.source.id_, '.get', parse = True)
 | 
			
		||||
        if not data:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,7 @@
 | 
			
		||||
import os
 | 
			
		||||
import re
 | 
			
		||||
import subprocess
 | 
			
		||||
import atexit
 | 
			
		||||
 | 
			
		||||
from django.template.loader import render_to_string
 | 
			
		||||
 | 
			
		||||
@ -57,8 +59,7 @@ class StationController:
 | 
			
		||||
    """
 | 
			
		||||
    Current source object that is responsible of self.current_sound
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    # TODO: add function to launch external program?
 | 
			
		||||
    process = None
 | 
			
		||||
 | 
			
		||||
    def __init__(self, **kwargs):
 | 
			
		||||
        self.__dict__.update(kwargs)
 | 
			
		||||
@ -76,6 +77,46 @@ class StationController:
 | 
			
		||||
            if source.controller:
 | 
			
		||||
                source.controller.fetch()
 | 
			
		||||
 | 
			
		||||
    def __get_process_args(self):
 | 
			
		||||
        """
 | 
			
		||||
        Get arguments for the executed application. Called by exec, to be
 | 
			
		||||
        used as subprocess.Popen(__get_process_args()).
 | 
			
		||||
        If no value is returned, abort the execution.
 | 
			
		||||
 | 
			
		||||
        Must be implemented by the plugin
 | 
			
		||||
        """
 | 
			
		||||
        return []
 | 
			
		||||
 | 
			
		||||
    def process_run(self):
 | 
			
		||||
        """
 | 
			
		||||
        Execute the external application with corresponding informations.
 | 
			
		||||
 | 
			
		||||
        This function must make sure that all needed files have been generated.
 | 
			
		||||
        """
 | 
			
		||||
        if self.process:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        self.push()
 | 
			
		||||
 | 
			
		||||
        args = self.__get_process_args()
 | 
			
		||||
        if not args:
 | 
			
		||||
            return
 | 
			
		||||
        self.process = subprocess.Popen(args, stderr=subprocess.STDOUT)
 | 
			
		||||
        atexit.register(self.process.terminate)
 | 
			
		||||
 | 
			
		||||
    def process_terminate(self):
 | 
			
		||||
        if self.process:
 | 
			
		||||
            self.process.terminate()
 | 
			
		||||
            self.process = None
 | 
			
		||||
 | 
			
		||||
    def process_wait(self):
 | 
			
		||||
        """
 | 
			
		||||
        Wait for the process to terminate if there is a process
 | 
			
		||||
        """
 | 
			
		||||
        if self.process:
 | 
			
		||||
            self.process.wait()
 | 
			
		||||
            self.process = None
 | 
			
		||||
 | 
			
		||||
    def push(self, config = True):
 | 
			
		||||
        """
 | 
			
		||||
        Update configuration and children's info.
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user