<commit_before>from __future__ import print_function
import abc
import logging
import time
log = logging.getLogger("rfm_ecomanager_logger")
from sensor import Sensor                   
from input_with_cancel import input_with_cancel

class TransmitterError(Exception):
    """For errors from Transmitter objects"""

class NeedToPickle(TransmitterError):
    """Need to pickle!"""

class Transmitter(object):
    """Abstract base class for representing a single transmitter.
    A transmitter may have multiple sensors.
    """
    
    __metaclass__ = abc.ABCMeta
    
    def __init__(self, rf_id, manager):
        self.id = rf_id
        self.manager = manager
    
    @abc.abstractmethod
    def update_name(self, sensors=None):
        print("\nEditing transmitter {}. Press c to cancel.\n".format(self.id))      

    def accept_pair_request(self):
        print("Pairing with", self.id)
        self.manager.nanode.send_command("p", self.id)
        success = False
        DEADLINE = time.time() + 5
        while time.time() < DEADLINE and not success:
            data = self.manager.nanode.read_sensor_data()
            if data.tx_id == self.id:
                if data.pair_ack:
                    print("Successfully paired with", self.id)
                    self.update_name()
                    success = True
                else:
                    success = False
                    break
            else:
                log.debug("Ignoring {} while waiting for pair ack from {}"
                          .format(data.tx_id, self.id))                
        if not success:
            raise TransmitterError("Failed to pair with {}".format(self.id))            
    
    @abc.abstractmethod
    def reject_pair_request(self, pr):
        pass
    
    def unpickle(self, manager):
        self.manager = manager
        for _, sensor in self.sensors.iteritems():
            sensor.update_filename(self)
            sensor.last_logged_timecode = 0
        
    def add_to_nanode(self):
        self.manager.nanode.send_command(self.ADD_COMMAND, self.id)
        
    def delete_from_nanode(self):
        self.manager.nanode.send_command(self.DEL_COMMAND, self.id)

    def new_reading(self, data):
        for s_id, watts in data.sensors.iteritems():
            s_id = int(s_id)
            if s_id in self.sensors.keys():
                self.sensors[s_id].log_data_to_disk(data.timecode, watts,
                                                    self.get_power_state())
            else:
                log.error("Transmitter {:d} reports a sensor is connected to "
                      "port {:d} but we don't have any info for that sensor id."
                      .format(self.id, s_id))

    def get_power_state(self):
        """Overridden by child classes, if necessary."""
        return None

    def __getstate__(self):
        """Used by pickle()"""
        odict = self.__dict__.copy() # copy the dict since we change it
        del odict['manager']
        return odict
    
    def print_sensors(self):
        string = ""
        first = True
        for sensor_id, sensor in self.sensors.iteritems():
            if first:
                first = False
            else:
                string += "\n" + " "*23
                
            string += "{:>8d}{:^5s}{:>10d}{:>20s}" \
                      .format(sensor_id,
                              "agg" if sensor.agg_chan else "iam", 
                              sensor.log_chan, 
                              sensor.name)<commit_msg>Indent second line of TX correctly when listing transmitters in edit mode<commit_after>from __future__ import print_function
import abc
import logging
import time
log = logging.getLogger("rfm_ecomanager_logger")
from sensor import Sensor                   
from input_with_cancel import input_with_cancel

class TransmitterError(Exception):
    """For errors from Transmitter objects"""

class NeedToPickle(TransmitterError):
    """Need to pickle!"""

class Transmitter(object):
    """Abstract base class for representing a single transmitter.
    A transmitter may have multiple sensors.
    """
    
    __metaclass__ = abc.ABCMeta
    
    def __init__(self, rf_id, manager):
        self.id = rf_id
        self.manager = manager
    
    @abc.abstractmethod
    def update_name(self, sensors=None):
        print("\nEditing transmitter {}. Press c to cancel.\n".format(self.id))      

    def accept_pair_request(self):
        print("Pairing with", self.id)
        self.manager.nanode.send_command("p", self.id)
        success = False
        DEADLINE = time.time() + 5
        while time.time() < DEADLINE and not success:
            data = self.manager.nanode.read_sensor_data()
            if data.tx_id == self.id:
                if data.pair_ack:
                    print("Successfully paired with", self.id)
                    self.update_name()
                    success = True
                else:
                    success = False
                    break
            else:
                log.debug("Ignoring {} while waiting for pair ack from {}"
                          .format(data.tx_id, self.id))                
        if not success:
            raise TransmitterError("Failed to pair with {}".format(self.id))            
    
    @abc.abstractmethod
    def reject_pair_request(self, pr):
        pass
    
    def unpickle(self, manager):
        self.manager = manager
        for _, sensor in self.sensors.iteritems():
            sensor.update_filename(self)
            sensor.last_logged_timecode = 0
        
    def add_to_nanode(self):
        self.manager.nanode.send_command(self.ADD_COMMAND, self.id)
        
    def delete_from_nanode(self):
        self.manager.nanode.send_command(self.DEL_COMMAND, self.id)

    def new_reading(self, data):
        for s_id, watts in data.sensors.iteritems():
            s_id = int(s_id)
            if s_id in self.sensors.keys():
                self.sensors[s_id].log_data_to_disk(data.timecode, watts,
                                                    self.get_power_state())
            else:
                log.error("Transmitter {:d} reports a sensor is connected to "
                      "port {:d} but we don't have any info for that sensor id."
                      .format(self.id, s_id))

    def get_power_state(self):
        """Overridden by child classes, if necessary."""
        return None

    def __getstate__(self):
        """Used by pickle()"""
        odict = self.__dict__.copy() # copy the dict since we change it
        del odict['manager']
        return odict
    
    def print_sensors(self):
        string = ""
        first = True
        for sensor_id, sensor in self.sensors.iteritems():
            if first:
                first = False
            else:
                string += "\n" + " "*26
                
            string += "{:>8d}{:^5s}{:>10d}{:>20s}" \
                      .format(sensor_id,
                              "agg" if sensor.agg_chan else "iam", 
                              sensor.log_chan, 
                              sensor.name)
{'fraction_non_alphanumeric': 0.09482517482517483, 'fraction_numerical': 0.002797202797202797, 'mean_word_length': 1.6702763256161315, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2215846', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1924, 'n_words': 498}