Exemplo n.º 1
0
    def _handle_attempt(self):
        """
        This method is called when the socket is not
        connected, once every loop of the IOManager.
        """
        self.handle_loop_disconnected()
        if time.time() < self.__last_attempt + self.__timeout:
            return False

        self.__last_attempt = time.time()
        if self.__listener:
            result = self.__listen()
        else:
            result = self.__connect()

        if not result:
            self.__attempt += 1
            action = "bind to port" if self.__listener else "connect"
            if self.__giveup >= 0 and self.__attempt >= self.__giveup:
                self.__logger.critical("Failed to %s for %d times. Giving up." \
                                       % (action, self.__attempt))
                mgr = IOManager()
                mgr.unregister_unconnected(self)
            elif self.__giveup >= 0:
                self.__logger.critical("Failed to %s for %d/%d times. " \
                                       "Retrying in %d seconds."        \
                                       % (action, self.__attempt,       \
                                          self.__giveup, self.__timeout))
            else:
                self.__logger.critical("Failed to %s for %d times. " \
                                       "Retrying in %d seconds."     \
                                       % (action, self.__attempt,    \
                                          self.__timeout))
        else:
            self.__attempt = 0
Exemplo n.º 2
0
    def _handle_attempt(self):
        """
        This method is called when the socket is not
        connected, once every loop of the IOManager.
        """
        self.handle_loop_disconnected()
        if time.time() < self.__last_attempt + self.__timeout:
            return False

        self.__last_attempt = time.time()
        if self.__listener:
            result = self.__listen()
        else:
            result = self.__connect()

        if not result:
            self.__attempt += 1
            action = "bind to port" if self.__listener else "connect"
            if self.__giveup >= 0 and self.__attempt >= self.__giveup:
                self.__logger.critical("Failed to %s for %d times. Giving up." \
                                       % (action, self.__attempt))
                mgr = IOManager()
                mgr.unregister_unconnected(self)
            elif self.__giveup >= 0:
                self.__logger.critical("Failed to %s for %d/%d times. " \
                                       "Retrying in %d seconds."        \
                                       % (action, self.__attempt,       \
                                          self.__giveup, self.__timeout))
            else:
                self.__logger.critical("Failed to %s for %d times. " \
                                       "Retrying in %d seconds."     \
                                       % (action, self.__attempt,    \
                                          self.__timeout))
        else:
            self.__attempt = 0
Exemplo n.º 3
0
    def __listen(self):
        """
        This method is called from the connection manager when the socket is a
        server socket and should bind to the port specified.
        """
        if self.__socket or not self.__listener:
            return False

        if self.__unix_socket:
            if os.path.exists(self.__port):
                try:
                    os.unlink(self.__port)
                except:
                    self.__logger.error("Socket %s exists and cannot be " \
                                        "removed" % self.__port)
                    return False

            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            address = (self.__port)
            to_what = "Unix Domain Socket %s" % self.__port
        else:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            address = (self.__host, self.__port)
            to_what = "TCP port %d" % self.__port

        try:
            sock.bind(address)
            sock.listen(5)
            self.__set_socket_options(sock)
        except socket.error as e:
            self.__logger.warning("Could not bind to %s: %s" \
                                  % (to_what, str(e)))
            return False
        except Exception as e:
            # Other exceptions usually mean bad settings. Try one more time but
            # do not keep on trying forever..
            self.__logger.error("Could not connect to %s: %s" \
                                % (to_what, str(e)))
            self.__giveup = 2
            return False

        self.__logger.info("Socket listening on %s initialized" % to_what)

        self.__socket = sock
        self.__fileno = self.__socket.fileno()
        self.__connected = True
        mgr = IOManager()
        mgr.register(self)
        return True
Exemplo n.º 4
0
    def __listen(self):
        """
        This method is called from the connection manager when the socket is a
        server socket and should bind to the port specified.
        """
        if self.__socket or not self.__listener:
            return False

        if self.__unix_socket:
            if os.path.exists(self.__port):
                try:
                    os.unlink(self.__port)
                except:
                    self.__logger.error("Socket %s exists and cannot be " \
                                        "removed" % self.__port)
                    return False

            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            address = (self.__port)
            to_what = "Unix Domain Socket %s" % self.__port
        else:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            address = (self.__host, self.__port)
            to_what = "TCP port %d" % self.__port

        try:
            sock.bind(address)
            sock.listen(5)
            self.__set_socket_options(sock)
        except socket.error as e:
            self.__logger.warning("Could not bind to %s: %s" \
                                  % (to_what, str(e)))
            return False
        except Exception as e:
            # Other exceptions usually mean bad settings. Try one more time but
            # do not keep on trying forever..
            self.__logger.error("Could not connect to %s: %s" \
                                % (to_what, str(e)))
            self.__giveup = 2
            return False

        self.__logger.info("Socket listening on %s initialized" % to_what)

        self.__socket = sock
        self.__fileno = self.__socket.fileno()
        self.__connected = True
        mgr = IOManager()
        mgr.register(self)
        return True
Exemplo n.º 5
0
    def _handle_write(self):
        """
        This method is called from the IOManager thread when there the socket
        is writable and marked writable in the IO poller. It uses the stringio
        object of the current send buffer to get the next data to send and
        makes sure that the stringio object is set at the correct position,
        based on the amount of bytes actually sent.
        """
        with self.__send_lock:
            if not self.__send_buffer:
                # No data to write, make sure that this
                # socket is marked not writable in the
                # IOManager
                if self.connected():
                    mgr = IOManager()
                    mgr.set_writable(self, False)
                raise IOWrapperEnd()

            cur_id, cur_buffer = self.__send_buffer[0]
            cur_buffer.seek(self.__buffer_start_pos)
            data = cur_buffer.read(self.__buffer_size)
            if not data:
                # If the buffer ran out of data,
                # remove it from the queue and return.
                # The next attempt to write will set
                # the socket non-writable if there is
                # no next send buffer.
                cur_buffer.close()
                self.__send_buffer.pop(0)
                self.__send_buffer_ids.remove(cur_id)
                self.__buffer_start_pos = 0

                return

            sent_bytes = 0
            try:
                sent_bytes = self.__socket.send(data)
                self.__buffer_start_pos += sent_bytes
            except socket.error as err:
                if err.errno == errno.EAGAIN:  # Resource temporarily unavailable
                    raise IOWrapperEnd()
                raise

            if self.__log_output:
                self.__logger.debug("Sent data: %s" % repr(data[:sent_bytes]))

            self.__total_bytes += sent_bytes
            return sent_bytes
Exemplo n.º 6
0
    def close(self, active=True):
        """
        This method will close the socket. The active parameter will specify
        whether the socket is closed on this side or was closed on the other
        side. If the socket was closed on this side, no attempt to reconnect
        will be attempted. Otherwise, a reconnect will be attempted.
        """
        if not self.__socket or not self.__connected:
            return

        if self.__unix_socket:
            if self.__listener:
                close_what = "Unix Domain Socket listening on %s" \
                             % (self.__port)
            else:
                close_what = "Unix Domain Socket connection to %s" \
                             % (self.__port)
        else:
            if self.__listener:
                close_what = "server socket on port %d" % (self.__port)
            else:
                close_what = "TCP connection to %s:%d" \
                             % (self.__host, self.__port)

        self.__connected = False
        mgr = IOManager()
        mgr.unregister(self)

        if not self.__socket is None:
            try:
                self.__socket.close()
                self.__socket.shutdown()
            except socket.error as e:
                pass

        if active:
            self.__logger.info("Closed %s" % close_what)
        else:
            self.__logger.info("%s closed." % close_what)

        if self.__unix_socket and self.__listener:
            try:
                os.unlink(self.__port)
            except:
                pass

        self.__socket = None
        self.__fileno = None

        # If remote end closed connection, attempt to reconnect
        if not active and           \
           not self.__listener and  \
           not self.__handler and   \
           self.__giveup != 0:
            mgr = IOManager()
            mgr.register_unconnected(self)
        else:
            self.handle_shutdown()
Exemplo n.º 7
0
 def error_test(self, method_kind, phase, error_class):
     kwargs_key = phase + '_kwargs'
     manager = IOManager(**{kwargs_key: {'required': CustomType}})
     method_name = '_'.join([method_kind, phase])
     method = getattr(manager, method_name)
     with pytest.raises(error_class):
         method(iovalue=object())
Exemplo n.º 8
0
    def _handle_write(self):
        """
        This method is called from the IOManager thread when there the socket
        is writable and marked writable in the IO poller. It uses the stringio
        object of the current send buffer to get the next data to send and
        makes sure that the stringio object is set at the correct position,
        based on the amount of bytes actually sent.
        """
        if not self.__send_buffer:
            # No data to write, make sure that this
            # socket is marked not writable in the
            # IOManager
            if self.connected():
                mgr = IOManager()
                mgr.set_writable(self, False)
            raise IOWrapperEnd()

        with self.__send_lock:
            cur_id, cur_buffer = self.__send_buffer[0]
            cur_buffer.seek(self.__buffer_start_pos)
            data = cur_buffer.read(self.__buffer_size)
            if not data:
                # If the buffer ran out of data,
                # remove it from the queue and return.
                # The next attempt to write will set
                # the socket non-writable if there is
                # no next send buffer.
                cur_buffer.close()
                self.__send_buffer.pop(0)
                self.__send_buffer_ids.remove(cur_id)
                self.__buffer_start_pos = 0
                return

            sent_bytes = 0
            try:
                sent_bytes = self.__socket.send(data)
                self.__buffer_start_pos += sent_bytes
            except socket.error as err:
                if err.errno == errno.EAGAIN: # Resource temporarily unavailable
                    raise IOWrapperEnd()
                raise
            if self.__log_output:
                self.__logger.debug("Sent data: %s" % repr(data[:sent_bytes]))
                
            self.__total_bytes += sent_bytes
            return sent_bytes
Exemplo n.º 9
0
    def __init__(self, name):
        self.name = name

        self.regex_name = '^(' + re.escape(name) + "|" + re.escape(
            unidecode(name)) + "),? ?"

        logger.info("%s is initializing. Running version %s" %
                    (name, self.version))

        # Create input manager
        self.io_manager = IOManager(self)

        # 1st. Setup Inputs
        self.io_manager.setup()

        # 2nd. Setup Skills
        self.setup_skills()

        # 3rd. Setup default skill
        self.default_skill = Default(self)
Exemplo n.º 10
0
    def __connect(self):
        """
        This method is called by the _do_attempt method when the socket is a
        connecting socket that should try to connect
        """
        if self.__socket:
            return False

        if self.__unix_socket:
            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            address = (self.__port)
            to_what = "Unix Domain Socket %s" % self.__port
        else:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            address = (self.__host, self.__port)
            to_what = "TCP port %d" % self.__port
        try:
            sock.connect(address)
            self.__set_socket_options(sock)
        except socket.error as e:
            self.__logger.error("Could not connect to %s: %s" \
                                % (to_what, str(e)))
            return False
        except Exception as e:
            # Other exceptions mean bad settings. Try one more time but do not
            # keep on trying forever..
            self.__logger.error("Could not connect to %s: %s" \
                                % (to_what, str(e)))
            self.__giveup = 2
            return False

        self.__socket = sock
        self.__connected = True
        self.__fileno = self.__socket.fileno()

        mgr = IOManager()
        mgr.register(self)
        if not self.send_buffer_empty():
            mgr.set_writable(self, True)

        return True
Exemplo n.º 11
0
    def close(self, active=True):
        """
        This method will close the socket. The active parameter will specify
        whether the socket is closed on this side or was closed on the other
        side. If the socket was closed on this side, no attempt to reconnect
        will be attempted. Otherwise, a reconnect will be attempted.
        """
        if not self.__socket or not self.__connected:
            return

        if self.__unix_socket:
            if self.__listener:
                close_what = "Unix Domain Socket listening on %s" \
                             % (self.__port)
            else:
                close_what = "Unix Domain Socket connection to %s" \
                             % (self.__port)
        else:
            if self.__listener:
                close_what = "server socket on port %d" % (self.__port)
            else:
                close_what = "TCP connection to %s:%d" \
                             % (self.__host, self.__port)

        self.__connected = False
        mgr = IOManager()
        mgr.unregister(self)

        if not self.__socket is None:
            try:
                self.__socket.close()
                self.__socket.shutdown()
            except socket.error as e:
                pass

        if active:
            self.__logger.info("Closed %s" % close_what)
        else:
            self.__logger.info("%s closed." % close_what)

        if self.__unix_socket and self.__listener:
            try:
                os.unlink(self.__port)
            except:
                pass

        self.__socket = None
        self.__fileno = None

        # If remote end closed connection, attempt to reconnect
        if not active and           \
           not self.__listener and  \
           not self.__handler and   \
           self.__giveup != 0:
            mgr = IOManager()
            mgr.register_unconnected(self)
        else:
            self.handle_shutdown()
Exemplo n.º 12
0
    def __init__(self, argv):

        self.iom = IOManager(argv)
        self.keys = {}

        self.IF = ImageFactory()

        # get keys from rootfile, iterate over the enum
        # and see what's in the root file
        for i in xrange(larcv.kProductUnknown):
            product = larcv.ProductName(i)

            self.keys[product] = []

            producers = self.iom.iom.producer_list(i)

            for p in producers:
                self.keys[product].append(p)

        # run subrun and event start at zero
        self.run = -1
        self.subrun = -1
        self.event = -1
Exemplo n.º 13
0
    def send(self, data, optional=False):
        """
        This method enqueues new data to be send. It wraps the data by pickling
        it if configured to do so and creates a new StringIO buffer to store the
        new data.
        """
        if self.__listener:
            if not self.__client_sockets:
                self.__logger.warning("No clients connected to send data")
                return False
            with self.__receive_lock:
                for wrapper in self.__client_sockets:
                    wrapper.send(data)
            return True 

        if self.__use_pickle:
            try:
                data = pickle.dumps(data)
            except pickle.PicklingError:
                self.__logger.error("Cannot pickle data %s" % repr(data))
                return False

        with self.__send_lock:
            set_writable = False
            if not self.__send_buffer:
                set_writable = True

            id = self.__send_id
            self.__send_id += 1
            buffer = stringio.StringIO(data)
            self.__send_buffer.append((id, buffer))
            self.__send_buffer_ids.add(id)

            if set_writable and self.connected():
                mgr = IOManager()
                mgr.set_writable(self, True)
            return id
Exemplo n.º 14
0
    def send(self, data, optional=False):
        """
        This method enqueues new data to be send. It wraps the data by pickling
        it if configured to do so and creates a new StringIO buffer to store the
        new data.
        """
        if self.__listener:
            if not self.__client_sockets:
                self.__logger.warning("No clients connected to send data")
                return False
            with self.__receive_lock:
                for wrapper in self.__client_sockets:
                    wrapper.send(data)
            return True

        if self.__use_pickle:
            try:
                data = pickle.dumps(data)
            except pickle.PicklingError:
                self.__logger.error("Cannot pickle data %s" % repr(data))
                return False

        with self.__send_lock:
            set_writable = False
            if not self.__send_buffer:
                set_writable = True

            id = self.__send_id
            self.__send_id += 1
            buffer = stringio.StringIO(data)
            self.__send_buffer.append((id, buffer))
            self.__send_buffer_ids.add(id)

            if set_writable and self.connected():
                mgr = IOManager()
                mgr.set_writable(self, True)
            return id
Exemplo n.º 15
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--graph', help='File name for graph', required=True)
    parser.add_argument('--goals',
                        nargs='+',
                        help='Goal node ids for drones',
                        required=True)
    args = parser.parse_args()
    goals = [int(g) for g in args.goals]
    graph = IOManager.import_graph(args.graph)

    # IOManager.export_graph(graph, '../aco/waterloo.ecegraph')
    # graph = IOManager.import_graph('../aco/waterloo.ecegraph')

    paths = []
    costs = []
    removed = {}
    start_time = time.time()
    for goal in goals:
        end = goal
        population = Population(graph[START_NODE], end, removed)
        path = population.evolve()
        path = remove_cycle(path)
        new_cost = Chromosome.calc_costs(path, graph, goal, removed)
        costs.append(new_cost)
        paths.append(path)
        # remove path from graph
        for p in path:
            removed[p] = p
        # s = path[0]
        # for i in range(1, len(path)):
        #   e = path[i]
        #   if e in graph[s].neighbours:
        #     del graph[s].neighbours[e]
        #   s = e
    end_time = time.time()
    print 'path: ' + str(paths)
    print 'costs: ' + str(costs)
    print('Overall time: {}'.format(end_time - start_time))
Exemplo n.º 16
0
    def __init__(self,argv):
        
        self.iom = IOManager(argv)
        self.keys ={}

        self.IF = WhiteImageFactory()
        
        # get keys from rootfile, iterate over the enum
        # and see what's in the root file
        for i in xrange(larcv.kProductUnknown):
            product = larcv.ProductName(i)

            self.keys[product] = []

            producers=self.iom.iom.producer_list(i)
            
            for p in producers:
                self.keys[product].append(p)

        # run subrun and event start at zero
        self.run    = -1
        self.subrun = -1
        self.event  = -1
Exemplo n.º 17
0
    def __connect(self):
        """
        This method is called by the _do_attempt method when the socket is a
        connecting socket that should try to connect
        """
        if self.__socket:
            return False

        if self.__unix_socket:
            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            address = (self.__port)
            to_what = "Unix Domain Socket %s" % self.__port
        else:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            address = (self.__host, self.__port)
            to_what = "TCP port %d" % self.__port
        try:
            sock.connect(address)
            self.__set_socket_options(sock)
        except socket.error as e:
            self.__logger.error("Could not connect to %s: %s" \
                                % (to_what, str(e)))
            return False
        except Exception as e:
            # Other exceptions mean bad settings. Try one more time but do not
            # keep on trying forever..
            self.__logger.error("Could not connect to %s: %s" \
                                % (to_what, str(e)))
            self.__giveup = 2
            return False

        self.__socket = sock
        self.__connected = True
        self.__fileno = self.__socket.fileno()

        mgr = IOManager()
        mgr.register(self)
        if not self.send_buffer_empty():
            mgr.set_writable(self, True)

        return True
Exemplo n.º 18
0
 def make_iomanager(self, process_kind, iospec, **kwargs):
     key = process_kind + '_kwargs'
     kwargs.setdefault(key, {})
     kwargs[key].update({'required': iospec})
     return IOManager(**kwargs)
Exemplo n.º 19
0
    def __init__(
        self,
        host="localhost",  # The host to connect to or serve
        port=49152,  # The port to connect to or serve
        giveup=5,  # Amount of reconnect attempts
        use_socket=None,  # Pass in a connected socket object to use
        retry_timeout=1,  # Timeout between connection attempts
        server=None,  # True for a server, False for clients
        use_pickle=True,  # Whether to pickle/unpickle 
        #    send/received data
        bufsize="auto",  # Number of bytes to send/receive at once
        linger=.1,  # Whether to set the SO_LINGER option
        handler=False,  # Set to true to make this a incoming 
        #    connection handler
        logger=None  # Pass in a logger to use
    ):
        """
        Initialize the socket. The parameters define the behavior of the socket.
        
        Host should be an IP address or hostname to which to connect or on which
        to listen. If the server argument is not specifically set, the hostname
        will also define whether this will be a server or client socket: if host
        is empty, it will be a server socket listening on all interfaces,
        otherwise it will be a client socket. 

        port should be the port to listen on. When the port is numeric it is
        assumed to be a TCP port. When it is a string, it will be cast to an int
        specifying a TCP port. If the conversion failed, it is assumed to be a
        Unix Socket, located in /tmp. This will raise an exception if the socket
        is trying to connect to a remove Unix Socket, which is impractical and
        defeats the purpose.

        giveup is the amount of retries performed to connect to a socket or bind
        to a port.

        use_socket can be used to pass in an already connected socket. The same
        socket options will be set on this socket as newly created ones.

        retry_timeout defines the number of seconds to wait between connection/
        binding attempts of unconnected sockets.

        server is a boolean that specifies whether this will be a server or a
        client socket. A server socket will bind and listen to a port, a client
        socket will attempt to connect to a port. The default value decides this
        automatically, based on the value of the host parameter as described
        above.

        use_pickle is a boolean specifying whether communication on this socket
        will proceed using the Pickle protocol. If set to true, all outgoing
        data will be pickled and all incoming data will be unpickled.

        bufsize specifies the amount of data to send or receive at once. The
        default is auto, meaning it will be set depending on other options. When
        the socket to use is a Unix Domain Socket, it will use 128 KiB. If it is
        a client socket connecting to a local socket, it will be a 16 KiB and if
        it is a server socket or a client socket connecting to a non-local host,
        a buffer of 8 KiB is used.

        linger is a number specifying whether the socket will be set in
        linger mode (see man page for setsockopt(2)). When in linger mode, the
        call to close on the socket will block for at most a set number of
        seconds and will then forcefully close the socket. Otherwise, the close
        call is non-blocking and the socket will get in a TIME_WAIT state, which
        effectively blocks the port for up to 4 minutes, possibly resulting in
        'Port already bound' messages. If the linger parameter is 0, LINGER will
        not be used; otherwise this is the amount of seconds to set.

        handler can be set to True to activate incoming connection handler 
        behavior. This basically makes sure that no reconnetion attempt is 
        performed when the connection is closed by the other end.
        """

        self.__socket = None
        self.__fileno = None
        self.__client_sockets = []
        self.__connected = False
        self.__listener = False
        self.__handler = handler
        self.__use_pickle = use_pickle

        self.__send_lock = threading.RLock()
        self.__receive_lock = threading.RLock()
        self.__receive_buffer = []
        self.__recv_buffer = ""
        self.__send_buffer = []
        self.__send_buffer_ids = set([])
        self.__buffer_start_pos = 0
        self.__send_id = 0

        self.__set_port(host, port)

        self.__set_buffer_size(bufsize)
        self.__linger = float(linger)
        self.__total_bytes = 0

        self.__log_input = False
        self.__log_output = False

        if server is None and self.__host == "":
            self.__listener = True
        else:
            self.__listener = server

        if logger:
            self.__logger = logger
        else:
            self.__logger = logging.getLogger('Borg.Brain.Util.ThreadedSocket')

        self.__logger.addHandler(nullhandler.NullHandler())

        self.__last_attempt = 0
        self.__timeout = retry_timeout
        self.__giveup = giveup
        self.__attempt = 0

        if use_socket:
            self.__socket = use_socket
            self.__fileno = self.__socket.fileno()
            self.__port = port
            self.__connected = True
            self.__set_socket_options(self.__socket)
            mgr = IOManager()
            mgr.register(self)
        else:
            mgr = IOManager()
            mgr.register_unconnected(self)
Exemplo n.º 20
0
import sys

#python doesn't allow fo importing modules from other
#directories, so I'm bypassing this.
curpth = os.path.dirname(os.path.abspath(__file__))
targetpth = curpth + '/../../src/'
sys.path.insert(0, targetpth)

from iomanager import IOManager

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('csv_path')
    parser.add_argument('roster_path')
    args = parser.parse_args()
    csv_pth = args.csv_path
    roster_pth = args.roster_path
    roster = []

    #grab the roster file and create a roster list
    with open(roster_pth) as r_file:
        for line in r_file:
            roster.append(line)

    manager = IOManager(roster)
    students = manager.read(csv_pth)

    print('students:')
    for s in students:
        print(s)
Exemplo n.º 21
0
#!/usr/bin/env python3
'''This module is the main body of the emulator.'''

from sys import argv
from vm import Chip8
from iomanager import IOManager


def load_rom(input_file):
    '''Load ROM file from the command line.'''
    try:
        with open(input_file, 'rb') as rom_file:
            return rom_file.read().hex().upper()
    except IOError:
        print("Error: file not found")
        exit(1)


if __name__ == "__main__":
    GAME_ROM = load_rom(argv[1])

    chip8 = Chip8(GAME_ROM)
    io_manager = IOManager(chip8)
Exemplo n.º 22
0
class DataManager(object):

    def __init__(self,argv):
        
        self.iom = IOManager(argv)
        self.keys ={}

        self.IF = ImageFactory()
        
        # get keys from rootfile, iterate over the enum
        # and see what's in the root file
        for i in xrange(larcv.kProductUnknown):
            product = larcv.ProductName(i)

            self.keys[product] = []

            producers=self.iom.iom.producer_list(i)
            
            for p in producers:
                self.keys[product].append(p)

        # run subrun and event start at zero
        self.run    = -1
        self.subrun = -1
        self.event  = -1
        

    def get_nchannels(self,ii,imgprod) :
        # Sorry Vic I hacked this
        # --> it's ok
        self.iom.read_entry(ii)
        imdata  = self.iom.get_data(larcv.kProductImage2D,imgprod)
        return imdata.Image2DArray().size()

    def get_event_image(self,ii,imgprod,roiprod,planes, refresh=True) :

        #Load data in TChain
        self.iom.read_entry(ii)

        # there may be no ROI
        hasroi = False
        roidata = None
        if roiprod is not None:
            roidata = self.iom.iom.get_data(larcv.kProductROI,roiprod)
            roidata = roidata.ROIArray()
            hasroi = True

        # get the EventImage2D
        imdata  = self.iom.get_data(larcv.kProductImage2D,imgprod) # goes to disk

        self.run    = imdata.run()
        self.subrun = imdata.subrun()
        self.event  = imdata.event()

        # get the std::vector<larcv::Image2D>
        imdata  = imdata.Image2DArray()
        if imdata.size() == 0 : return (None, False)

        # hand it off to the factory, the producer name should query the
        # the correct subclass of PlotImage
        image = self.IF.get(imdata,roidata,planes,imgprod) # returns PlotImgae


        # return it to rgbviewer
        return ( image, hasroi )         
    
    
    
    # -----------------------------------------------------------------------------
    # Erez, July-21, 2016 - get an image using R/S/E navigation
    # -----------------------------------------------------------------------------
    def get_all_images(self,imgprod,event_base_and_images,rse_map) :
        
        for entry in range(self.iom.get_n_entries()):
            read_entry = self.iom.read_entry(entry)
            event_base = self.iom.get_data(larcv.kProductImage2D,imgprod)
            event_base_and_images[entry] = event_base
            rse = ( int(event_base.run()),int(event_base.subrun()),int(event_base.event()) )
            #print rse
            #rse_map[entry] = [event_base.run(),event_base.subrun(),event_base.event()]
            rse_map[ rse ] = entry
#            print rse_map[entry]
        print "collected %d images...\nready for RSE navigation"%len(event_base_and_images)

        return
    



    # -----------------------------------------------------------------------------
    # Erez, July-21, 2016 - get an image using R/S/E navigation
    # -----------------------------------------------------------------------------
    def get_rse_image(self,event_base_and_images,rse_map,wanted_rse,imgprod,roiprod,planes, refresh=True) :
        if wanted_rse in rse_map:
            return self.get_event_image(rse_map[wanted_rse],imgprod,roiprod,planes,refresh)
        else:
            print "i couldn't find this R/S/E..."
            return None, False
 
        ii = -1
        for i in range(len(event_base_and_images)):
            
            if rse_map[i] == wanted_rse:
                ii = i
                break
    
        if (ii==-1):
            print "i couldn't find this R/S/E..."
                        
        return self.get_event_image(ii,imgprod,roiprod,planes,refresh)
Exemplo n.º 23
0
 def startManager(self):
     '''
     This starts the IOManager
     '''
     self.c_data = ConfigData()
     self.manager = IOManager(self.c_data, self.roster)
Exemplo n.º 24
0
class GuiInterface():
    '''
    The interface class
    '''
    def __init__(self):
        '''
        Initializes all items used by the
        interface to none
        '''
        self.teams = None
        self.roster = None
        self.c_data = None
        self.manager = None
        self.students = None
        self.algorithm = None
        self.outputpath = None
        self.email = ""
        self.password = ""
        self.saved = False

    def loadRoster(self, rostertext):
        '''
        This loads the roster text file
        into a list of student names

        @param:
            rostertext: string
        '''
        self.roster = []
        with open(rostertext) as r_file:
            for line in r_file:
                self.roster.append(line)

    def startManager(self):
        '''
        This starts the IOManager
        '''
        self.c_data = ConfigData()
        self.manager = IOManager(self.c_data, self.roster)

    def readCsv(self, intext):
        '''
        This reads the csv file using
        the IOManager

        @param:
            intext: string
        '''
        self.students = self.manager.read(intext)

    def startAlgorithm(self, teamsize):
        '''
        This sets the teamsize for the algorithm
        and runs the algorithm
        
        @param:
            teamsize: int
        '''
        self.algorithm = AlgorithmManager(self.c_data, teamsize)
        self.teams = self.algorithm.runMain(self.students)

    def runGeneral(self, rostertext, intext, teamsize):
        '''
        This sets and runs the guiinterface quickly

        @param:
            rostertext: string
            intext:     string
            teamsize:   int
        '''
        self.loadRoster(rostertext)
        self.startManager()
        self.readCsv(intext)
        self.startAlgorithm(teamsize)

    def setOutputPath(self, outputpath):
        '''
        This sets the outputpath

        @param:
            outputpath: string
        '''
        self.outputpath = outputpath

    def writeFile(self):
        '''
        This writes the file
        using the IOManager
        
        If file already exists,
        appends number to the end
        '''
        filenumber = 1
        newoutputpath = self.outputpath + "/out.txt"
        newpath = Path(newoutputpath)
        while (newpath.exists()):
            newoutputpath = self.outputpath + "/out" + str(filenumber) + ".txt"
            newpath = Path(newoutputpath)
            filenumber += 1
        self.manager.write(newoutputpath, self.teams)

    def reShuffleAll(self):
        '''
        This re-runs the algorithm fully to shuffle
        all of the teams
        '''
        self.teams = self.algorithm.runMain(self.students)

    def reShuffleTeams(self):
        '''
        This shuffles the teams using the swapMember
        algorithm from the Algorithm class
        '''
        self.teams = self.algorithm.swapMembers(self.teams)

    def reShuffleSelectedTeams(self, indexes):
        '''
        This runs a teamswap on the selected teams.
        The selected teams are represented as indexes
        from the team object in the class

        @param:
            indexes: [int]
        '''
        swapping = []
        tempteam = [i for i in self.teams]

        for index in indexes:
            swapping.append(self.teams[index])
            tempteam.remove(self.teams[index])

        newteams = self.algorithm.swapMembers(swapping)
        for i in newteams:
            tempteam.append(i)

        swapping = []
        self.teams = tempteam

    def sendEmail(self, teams, email="", password="", save=False):
        '''
        This emails the selected teams from the gui and 
        invokes the send_email method, the selected teams
        are represented as indexes corresponding to
        the teams within the self.teams list

        @param:
            teams    - [indexes] represents the teams at each index
            email    - string this is the email address given
            password - string that is the password
            save     - bool if true, will save the email and password until 
                           the end of the program     

        @returns:
            bool - True if success, False if failure 
        '''
        success = True

        if self.saved:
            for team in teams:
                success = send_email(self.teams[team], self.email,
                                     self.password)
                if not success:
                    return False
            return True

        for team in teams:
            success = send_email(self.teams[team], email, password)
            if not success:
                return False

        if (save):
            self.email = email
            self.password = password
            self.saved = True

        return True
Exemplo n.º 25
0
targetpth1 = curpth + '/../../src/'
targetpth2 = curpth + '/../../config/'
sys.path.insert(0, targetpth1)
sys.path.insert(0, targetpth2)

from iomanager import IOManager
from config_data import *

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('csv_path')
    parser.add_argument('roster_path')
    args = parser.parse_args()
    csv_pth = args.csv_path
    roster_pth = args.roster_path
    roster = []

    #grab the roster file and create a roster list
    with open(roster_pth) as r_file:
        for line in r_file:
            roster.append(line)

    c_data = ConfigData()

    manager = IOManager(c_data, roster)
    students = manager.read(csv_pth)

    print('students:')
    for s in students:
        print(s)
Exemplo n.º 26
0
class DataManager(object):
    def __init__(self, argv):

        self.iom = IOManager(argv)
        self.keys = {}

        self.IF = ImageFactory()

        # get keys from rootfile, iterate over the enum
        # and see what's in the root file
        for i in xrange(larcv.kProductUnknown):
            product = larcv.ProductName(i)

            self.keys[product] = []

            producers = self.iom.iom.producer_list(i)

            for p in producers:
                self.keys[product].append(p)

        # run subrun and event start at zero
        self.run = -1
        self.subrun = -1
        self.event = -1

    def get_nchannels(self, ii, imgprod):
        # Sorry Vic I hacked this
        # --> it's ok
        self.iom.read_entry(ii)
        imdata = self.iom.get_data(larcv.kProductImage2D, imgprod)
        return imdata.Image2DArray().size()

    def get_event_image(self, ii, imgprod, roiprod, planes, refresh=True):

        #Load data in TChain
        self.iom.read_entry(ii)

        # there may be no ROI
        hasroi = False
        roidata = None
        if roiprod is not None:
            roidata = self.iom.iom.get_data(larcv.kProductROI, roiprod)
            roidata = roidata.ROIArray()
            hasroi = True

        # get the EventImage2D
        imdata = self.iom.get_data(larcv.kProductImage2D,
                                   imgprod)  # goes to disk

        self.run = imdata.run()
        self.subrun = imdata.subrun()
        self.event = imdata.event()

        # get the std::vector<larcv::Image2D>
        imdata = imdata.Image2DArray()
        if imdata.size() == 0: return (None, False)

        # hand it off to the factory, the producer name should query the
        # the correct subclass of PlotImage
        image = self.IF.get(imdata, roidata, planes,
                            imgprod)  # returns PlotImgae

        # return it to rgbviewer
        return (image, hasroi)
Exemplo n.º 27
0
    def __init__(self,
                 host="localhost", # The host to connect to or serve
                 port=49152,       # The port to connect to or serve
                 giveup=5,         # Amount of reconnect attempts
                 use_socket=None,  # Pass in a connected socket object to use
                 retry_timeout=1,  # Timeout between connection attempts
                 server=None,      # True for a server, False for clients
                 use_pickle=True,  # Whether to pickle/unpickle 
                                   #    send/received data
                 bufsize="auto",   # Number of bytes to send/receive at once
                 linger=.1,        # Whether to set the SO_LINGER option
                 handler=False,    # Set to true to make this a incoming 
                                   #    connection handler
                 logger=None       # Pass in a logger to use
                ):
        """
        Initialize the socket. The parameters define the behavior of the socket.
        
        Host should be an IP address or hostname to which to connect or on which
        to listen. If the server argument is not specifically set, the hostname
        will also define whether this will be a server or client socket: if host
        is empty, it will be a server socket listening on all interfaces,
        otherwise it will be a client socket. 

        port should be the port to listen on. When the port is numeric it is
        assumed to be a TCP port. When it is a string, it will be cast to an int
        specifying a TCP port. If the conversion failed, it is assumed to be a
        Unix Socket, located in /tmp. This will raise an exception if the socket
        is trying to connect to a remove Unix Socket, which is impractical and
        defeats the purpose.

        giveup is the amount of retries performed to connect to a socket or bind
        to a port.

        use_socket can be used to pass in an already connected socket. The same
        socket options will be set on this socket as newly created ones.

        retry_timeout defines the number of seconds to wait between connection/
        binding attempts of unconnected sockets.

        server is a boolean that specifies whether this will be a server or a
        client socket. A server socket will bind and listen to a port, a client
        socket will attempt to connect to a port. The default value decides this
        automatically, based on the value of the host parameter as described
        above.

        use_pickle is a boolean specifying whether communication on this socket
        will proceed using the Pickle protocol. If set to true, all outgoing
        data will be pickled and all incoming data will be unpickled.

        bufsize specifies the amount of data to send or receive at once. The
        default is auto, meaning it will be set depending on other options. When
        the socket to use is a Unix Domain Socket, it will use 128 KiB. If it is
        a client socket connecting to a local socket, it will be a 16 KiB and if
        it is a server socket or a client socket connecting to a non-local host,
        a buffer of 8 KiB is used.

        linger is a number specifying whether the socket will be set in
        linger mode (see man page for setsockopt(2)). When in linger mode, the
        call to close on the socket will block for at most a set number of
        seconds and will then forcefully close the socket. Otherwise, the close
        call is non-blocking and the socket will get in a TIME_WAIT state, which
        effectively blocks the port for up to 4 minutes, possibly resulting in
        'Port already bound' messages. If the linger parameter is 0, LINGER will
        not be used; otherwise this is the amount of seconds to set.

        handler can be set to True to activate incoming connection handler 
        behavior. This basically makes sure that no reconnetion attempt is 
        performed when the connection is closed by the other end.
        """
        self.__socket = None
        self.__fileno = None
        self.__client_sockets = []
        self.__connected = False
        self.__listener = False
        self.__handler = handler
        self.__use_pickle = use_pickle

        self.__send_lock = threading.RLock()
        self.__receive_lock = threading.RLock()
        self.__receive_buffer = []
        self.__recv_buffer = ""
        self.__send_buffer = []
        self.__send_buffer_ids = set([])
        self.__buffer_start_pos = 0
        self.__send_id = 0

        self.__set_port(host, port)

        self.__set_buffer_size(bufsize)
        self.__linger = float(linger)
        self.__total_bytes = 0

        self.__log_input = False
        self.__log_output = False

        if server is None and self.__host == "":
            self.__listener = True
        else:
            self.__listener = server

        if logger:
            self.__logger = logger
        else:
            self.__logger = logging.getLogger('Borg.Brain.Util.ThreadedSocket')

        self.__logger.addHandler(nullhandler.NullHandler())

        self.__last_attempt = 0
        self.__timeout = retry_timeout
        self.__giveup = giveup
        self.__attempt = 0

        if use_socket:
            self.__socket = use_socket
            self.__fileno = self.__socket.fileno()
            self.__port = port
            self.__connected = True
            self.__set_socket_options(self.__socket)
            mgr = IOManager()
            mgr.register(self)
        else:
            mgr = IOManager()
            mgr.register_unconnected(self)
Exemplo n.º 28
0
if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('csv_path')
    parser.add_argument('roster_path')
    parser.add_argument('output_path')
    args       = parser.parse_args()
    csv_pth    = args.csv_path
    roster_pth = args.roster_path
    out_path   = args.output_path
    roster     = []

    #grab the roster file and create a roster list
    with open(roster_pth) as r_file:
        for line in r_file:
            roster.append(line)

    c_data = ConfigData()

    manager  = IOManager(c_data, roster)
    students = manager.read(csv_pth)
    alg      = AlgorithmManager()
    teams    = alg.runMain(students)
    manager.write(out_path, teams)
   




    
Exemplo n.º 29
0
class WhiteDataManager(object):

    def __init__(self,argv):
        
        self.iom = IOManager(argv)
        self.keys ={}

        self.IF = WhiteImageFactory()
        
        # get keys from rootfile, iterate over the enum
        # and see what's in the root file
        for i in xrange(larcv.kProductUnknown):
            product = larcv.ProductName(i)

            self.keys[product] = []

            producers=self.iom.iom.producer_list(i)
            
            for p in producers:
                self.keys[product].append(p)

        # run subrun and event start at zero
        self.run    = -1
        self.subrun = -1
        self.event  = -1
        

    def get_nchannels(self,ii,imgprod) :
        # Sorry Vic I hacked this
        # --> it's ok
        self.iom.read_entry(ii)
        imdata  = self.iom.get_data(larcv.kProductImage2D,imgprod)
        return imdata.Image2DArray().size()

    def get_event_image(self,ii,imgprod,roiprod,planes, refresh=True) :

        #Load data in TChain
        self.iom.read_entry(ii)

        # there may be no ROI
        hasroi = False
        roidata = None
        if roiprod is not None:
            roidata = self.iom.iom.get_data(larcv.kProductROI,roiprod)
            roidata = roidata.ROIArray()
            hasroi = True

        # get the EventImage2D
        imdata  = self.iom.get_data(larcv.kProductImage2D,imgprod) # goes to disk

        self.run    = imdata.run()
        self.subrun = imdata.subrun()
        self.event  = imdata.event()

        # get the std::vector<larcv::Image2D>
        imdata  = imdata.Image2DArray()
        if imdata.size() == 0 : return (None, False)

        # hand it off to the factory, the producer name should query the
        # the correct subclass of PlotImage
        image = self.IF.get(imdata,roidata,planes,imgprod) # returns PlotImgae

        # return it to rgbviewer
        return ( image, hasroi )         
Exemplo n.º 30
0
        loadUi('../ui/tbs_window.ui', self.tbs_window)
        self.tbs_window.setWindowTitle('Tarım Bilgi Sistemi')

        OceanViewGui.setButtonIcon(self.tbs_window.backtohomeButton,
                                   '../ui/icon/back.png')
        self.tbs_window.backtohomeButton.clicked.connect(
            self.on_backtohomeButton_clicked)

        webView = QtWebEngineWidgets.QWebEngineView(self.tbs_window.webWidget)
        webView.setUrl(
            QtCore.QUrl("https://tbs.tarbil.gov.tr/Authentication.aspx"))
        webView.setObjectName("webView")


if __name__ == '__main__':
    config = Config('../config.ini')
    engine = Engine(
        iomanager=IOManager(),
        analyzer=Analyzer(config=config, database=Database(config)),
        calibrator=Calibrator(input_dir=config.calibration_input_dir,
                              output_dir=config.calibration_output_dir,
                              calibration_eqns=config.calibration_equation),
        config=config)

    app = QApplication(sys.argv)

    widget = OceanViewGui(engine=engine, config=config)
    widget.show()

    sys.exit(app.exec_())
Exemplo n.º 31
0
class Bot:
    """
	Creates a new bot instance
	"""

    # Bot name
    name = "Mr. Nobody"

    version = "2.0"

    # List of Skills
    skills = defaultdict(list)

    io_manager = None

    threads = []

    def __init__(self, name):
        self.name = name

        self.regex_name = '^(' + re.escape(name) + "|" + re.escape(
            unidecode(name)) + "),? ?"

        logger.info("%s is initializing. Running version %s" %
                    (name, self.version))

        # Create input manager
        self.io_manager = IOManager(self)

        # 1st. Setup Inputs
        self.io_manager.setup()

        # 2nd. Setup Skills
        self.setup_skills()

        # 3rd. Setup default skill
        self.default_skill = Default(self)

    def start(self):

        # Start input manager worker
        self.io_manager.start()

    def shutdown(self):
        logger.info("Shutting down %s threads " % len(self.threads))

        # Shutdown all the bot instantiated threads
        for t in self.threads:
            t.kill_received = True
            t.join()
            #self.threads.remove(t)
            self.threads = [
                thread for thread in self.threads if thread is not t
            ]
            logger.info("Killed %s " % t)

        # Shutdown the input manager
        self.io_manager.shutdown()

    def run(self):

        self.start()

        # Check if threads are running
        while True:
            try:
                time.sleep(60)
            except KeyboardInterrupt:
                logger.info("Ctrl-c received! Shutting down...")
                self.shutdown()
                return

    def setup_skills(self):
        from ambrosio import config

        # Fetch skills from config
        skills = config['skills']

        logger.info("Initializing Skills")

        for skill in skills:

            level = skill['level'] if 'level' in skill else 0
            name = skill['skill']
            is_custom = skill['custom'] if 'custom' in skill else False

            try:
                if is_custom:
                    module = importlib.import_module('config.skills.' +
                                                     name.lower())
                else:
                    module = importlib.import_module('skills.' + name.lower())

                module_obj = getattr(module, name)

                self.register_skill(module_obj(self), level)
            except ImportError as e:
                logger.error(
                    "Impossible to load skill %s: Module not found (%s)" %
                    (name, e.args[0]))
            except Exception:
                logger.error("Error loading skill. " +
                             str(traceback.format_exc()))

    def register_skill(self, skill, level=0):
        logger.debug("Registering Skill %s in level %s" % (skill.name, level))
        self.skills[level].append(skill)
        return True
Exemplo n.º 32
0
    d1 = Day("Tuesday")
    d1.insertTime(10)
    d1.insertTime(13)
    d2 = Day("Wednesday")
    d2.insertTime(10)
    d2.insertTime(14)

    s1 = Student("Streisand, Barbara", "")
    days = [d1, d2]
    filters3 = {}
    filters3['Meeting Times'] = days
    s1.setFilters(filters3)

    s2 = Student("Cobain, Curt", "")
    days = [d2]
    filters4 = {}
    filters4['Meeting Times'] = days
    s2.setFilters(filters4)

    t2 = Team(2, 4)

    t2.insertStudent(s1)
    t2.insertStudent(s2)

    team_lst = [t1, t2]

    manager = IOManager(roster)
    students = manager.read(csv_pth)
    manager.write(out_path, team_lst)
Exemplo n.º 33
0
 def method_test(self, method_name):
     iomanager = IOManager()
     method_callable = getattr(iomanager, method_name)
     method_callable(iovalue=object())