<commit_before>import argparse                             # For parsing command line arguments
import sys                                  # For importing from project directory
import os                                   # For importing from project directory
import Queue                                # For communicating between datasinks and devices
#import threading                            # For making device communication threads
import subprocess                           # For launching each device's processes
import logging                              # For logging each of the devices
import time                                 # For waiting between syncing stdout's of device processes
import configReader                         # For reading the configuration

import fcntl

class LabSenseMain(object):

    class DeviceClass(object):
        """ Stores a device's name, process, and logger """
        def __init__(self, name, device_type, process, logger, queue):
            self.name = name
            self.device_type = device_type
            self.process = process
            self.logger = logger
            self.queue = queue

    def __init__(self, config_file):
        self.config_file = config_file
        self.config = configReader.readConfiguration(config_file)
        self.running_devices = []

        # Set up logging for debugging
        logging.basicConfig(format="%(asctime)s %(levelname)s %(message)s",\
                level=logging.DEBUG)

    def startDevices(self):
        """ Parse devices in Configuration file """
        for device, config in self.config.iteritems():
            # Sinks
            if device == "SensorAct":
                required_fields = ["IP", "PORT", "API_KEY"]
                for field in required_fields:
                    if not config[field]:
                        sys.exit("SensorAct requires the field " + field)

            elif device == "Cosm":
                required_fields = ["API_KEY", "user_name"]
                for field in required_fields:
                    if not config[field]:
                        sys.exit("Cosm requires the field " + field)

            elif device == "Stdout":
                pass

            # Devices 
            elif device in ["Eaton", "Veris", "Raritan", "SmartSwitch",
                          "LightSensor", "TemperatureSensor", "LabSenseServer"]:
                # For each device, start the device process, create a logger for
                # it, and store into running_devices list.
                process = self.__startDevice(device, config["name"])
                name = config["name"]
                logger = logging.getLogger(name)
                logger.propagate = False            # Don't propagate to console
                formatter = logging.Formatter("%(asctime)s %(message)s")
                filehandler = logging.FileHandler("logs/%s.log" % name, "w")
                filehandler.setLevel(logging.DEBUG)
                filehandler.setFormatter(formatter)
                logger.addHandler(filehandler)
                device_queue = Queue.Queue()
                device_class = self.DeviceClass(config["name"], device, process, logger, device_queue)
                self.running_devices.append(device_class)

            # Unrecognized
            else:
                raise KeyError("Unrecognized device: " + device)

    def monitorDevices(self, timeout):
        """ This variable helps increase timeout for everytime the error
        persists"""

        i = 0

        while True:
            for device in self.running_devices:
                device_process = device.process.poll()

                # If child process dies, restart it
                if device_process is not None:
                    print "CHILD DIED"
                    print "linearly increasing timeout = "+str(i)
                    i = i+5
                    if i == 500:
                        i = 0
                        print "timeout backoff at 500 seconds, resetting to 0"
                    device.process = self.__startDevice(device.device_type, device.name)

                std_out_line = None
                std_err_line = None
                try:<commit_msg>Support list of devices in config file<commit_after>import argparse                             # For parsing command line arguments
import sys                                  # For importing from project directory
import os                                   # For importing from project directory
import Queue                                # For communicating between datasinks and devices
#import threading                            # For making device communication threads
import subprocess                           # For launching each device's processes
import logging                              # For logging each of the devices
import time                                 # For waiting between syncing stdout's of device processes
import configReader                         # For reading the configuration

import fcntl

class LabSenseMain(object):

    class DeviceClass(object):
        """ Stores a device's name, process, and logger """
        def __init__(self, name, device_type, process, logger, queue):
            self.name = name
            self.device_type = device_type
            self.process = process
            self.logger = logger
            self.queue = queue

    def __init__(self, config_file):
        self.config_file = config_file
        self.config = configReader.readConfiguration(config_file)
        self.running_devices = []

        # Set up logging for debugging
        logging.basicConfig(format="%(asctime)s %(levelname)s %(message)s",\
                level=logging.DEBUG)

    def startDevices(self):
        """ Parse devices in Configuration file """
        for device, configs in self.config.iteritems():
			if (type(configs)!=list):
				configs = [configs]
			for config in configs.iteritems():
            	# Sinks
	            if device == "SensorAct":
	                required_fields = ["IP", "PORT", "API_KEY"]
	                for field in required_fields:
	                    if not config[field]:
	                        sys.exit("SensorAct requires the field " + field)

	            elif device == "Cosm":
	                required_fields = ["API_KEY", "user_name"]
	                for field in required_fields:
	                    if not config[field]:
	                        sys.exit("Cosm requires the field " + field)

	            elif device == "Stdout":
	                pass

	            # Devices 
	            elif device in ["Eaton", "Veris", "Raritan", "SmartSwitch",
	                          "LightSensor", "TemperatureSensor", "LabSenseServer"]:
	                # For each device, start the device process, create a logger for
	                # it, and store into running_devices list.
	                process = self.__startDevice(device, config["name"])
	                name = config["name"]
	                logger = logging.getLogger(name)
	                logger.propagate = False            # Don't propagate to console
	                formatter = logging.Formatter("%(asctime)s %(message)s")
	                filehandler = logging.FileHandler("logs/%s.log" % name, "w")
	                filehandler.setLevel(logging.DEBUG)
	                filehandler.setFormatter(formatter)
	                logger.addHandler(filehandler)
	                device_queue = Queue.Queue()
	                device_class = self.DeviceClass(config["name"], device, process, logger, device_queue)
	                self.running_devices.append(device_class)

	            # Unrecognized
	            else:
	                raise KeyError("Unrecognized device: " + device)

    def monitorDevices(self, timeout):
        """ This variable helps increase timeout for everytime the error
        persists"""

        i = 0

        while True:
            for device in self.running_devices:
                device_process = device.process.poll()

                # If child process dies, restart it
                if device_process is not None:
                    print "CHILD DIED"
                    print "linearly increasing timeout = "+str(i)
                    i = i+5
                    if i == 500:
                        i = 0
                        print "timeout backoff at 500 seconds, resetting to 0"
                    device.process = self.__startDevice(device.device_type, device.name)

                std_out_line = None
                std_err_line = None
                try:
{'fraction_non_alphanumeric': 0.07545855583933132, 'fraction_numerical': 0.0023218017181332712, 'mean_word_length': 1.5323339212228102, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '80839', 'n_tokens_mistral': 2110, 'n_tokens_neox': 1900, 'n_words': 613}