<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Minimal path in a 80x80 matrix, from top left node to bottom right node.
Moving up, down, left, or right directions.
'''

import timeit
import os

start = timeit.default_timer()

global edges
global traveled
global heap

path = os.getcwd().strip('py_solutions_81-90')

with open(path + 'euler_txt/matrix.txt') as f:
    grid = f.read()

edges = [[int(i) for i in v.split(',')] for v in grid.split('\r\n') if v]

traveled = [['inf'] * 80 for i in xrange(80)]

x = y = 0
heap = [[y, x]]

def euler_83(y, x):
    bounds = 80
    r_vertex = d_vertex = u_vertex = l_vertex = False
    if traveled[y][x] == 'inf':
        traveled[y][x] = curr = edges[y][x]
    else:
        curr = traveled[y][x]
    if x + 1 >= bounds and y + 1 >= bounds:
        return
    if y + 1 < bounds:
        d_vertex = d_edge(y, x, curr)
    if x + 1 < bounds:
        r_vertex = r_edge(y, x, curr)
    if y - 1 >= 0:
        u_vertex = u_edge(y, x, curr)
    if x - 1 >= 0:
        l_vertex = l_edge(y, x, curr)
    mvs = {d_vertex:'d_vertex',
           r_vertex:'r_vertex',
           u_vertex:'u_vertex',
           l_vertex:'l_vertex'
          }
    if any(mvs):
        mvs = {k:v for k,v in mvs.items() if k}
        next_mv = min(mvs)
        heap_mv = [mv for mv in mvs.values() if mv != mvs[next_mv]]
        push_heap(y, x, heap_mv)
        if mvs[next_mv] == 'd_vertex':
            euler_83(y+1, x)
        elif mvs[next_mv] == 'r_vertex':
            euler_83(y, x+1)
        elif mvs[next_mv] == 'u_vertex':
            euler_83(y-1,x)
        else:
            euler_83(y,x-1)
    else:
        return

def d_edge(y, x, curr):
    d_vertex = curr + edges[y+1][x]
    if traveled[y+1][x] == 'inf':
        traveled[y+1][x] = d_vertex
    elif d_vertex < traveled[y+1][x]:
        traveled[y+1][x] = d_vertex
    else:
        d_vertex = False
    return d_vertex

def r_edge(y, x, curr):
    r_vertex = curr + edges[y][x+1]
    if traveled[y][x+1] == 'inf':
        traveled[y][x+1] = r_vertex
    elif r_vertex < traveled[y][x+1]:
        traveled[y][x+1] = r_vertex
    else:
        r_vertex = False
    return r_vertex

def u_edge(y, x, curr):
    u_vertex = curr + edges[y-1][x]
    if traveled[y-1][x] == 'inf':
        traveled[y-1][x] = u_vertex
    elif u_vertex < traveled[y-1][x]:
        traveled[y-1][x] = u_vertex
    else:
        u_vertex = False
    return u_vertex

def l_edge(y, x, curr):
    l_vertex = curr + edges[y][x-1]
    if traveled[y][x-1] == 'inf':
        traveled[y][x-1] = l_vertex
    elif l_vertex < traveled[y][x-1]:
        traveled[y][x-1] = l_vertex
    else:
        l_vertex = False
    return l_vertex

def push_heap(y, x, heap_mv):
    mv_coor = {'d_vertex':[y+1,x],
               'r_vertex':[y,x+1],
               'u_vertex':[y-1,x],
               'l_vertex':[y,x-1]
              }
    heap.extend([mv_coor[i] for i in heap_mv])
    return

while heap:
    y, x = heap.pop(0)
    euler_83(y, x)

stop = timeit.default_timer()
print "Answer: %d" % traveled[79][79]
print "Time: %f" % (stop - start)

<commit_msg>mistaken about global thinking<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Minimal path in a 80x80 matrix, from top left node to bottom right node.
Moving up, down, left, or right directions.
'''

import timeit
import os

start = timeit.default_timer()

path = os.getcwd().strip('py_solutions_81-90')

with open(path + 'euler_txt/matrix.txt') as f:
    grid = f.read()

edges = [[int(i) for i in v.split(',')] for v in grid.split('\r\n') if v]

traveled = [['inf'] * 80 for i in xrange(80)]

x = y = 0
heap = [[y, x]]

def euler_83(y, x):
    bounds = 80
    r_vertex = d_vertex = u_vertex = l_vertex = False
    if traveled[y][x] == 'inf':
        traveled[y][x] = curr = edges[y][x]
    else:
        curr = traveled[y][x]
    if x + 1 >= bounds and y + 1 >= bounds:
        return
    if y + 1 < bounds:
        d_vertex = d_edge(y, x, curr)
    if x + 1 < bounds:
        r_vertex = r_edge(y, x, curr)
    if y - 1 >= 0:
        u_vertex = u_edge(y, x, curr)
    if x - 1 >= 0:
        l_vertex = l_edge(y, x, curr)
    mvs = {d_vertex:'d_vertex',
           r_vertex:'r_vertex',
           u_vertex:'u_vertex',
           l_vertex:'l_vertex'
          }
    if any(mvs):
        mvs = {k:v for k,v in mvs.items() if k}
        next_mv = min(mvs)
        heap_mv = [mv for mv in mvs.values() if mv != mvs[next_mv]]
        push_heap(y, x, heap_mv)
        if mvs[next_mv] == 'd_vertex':
            euler_83(y+1, x)
        elif mvs[next_mv] == 'r_vertex':
            euler_83(y, x+1)
        elif mvs[next_mv] == 'u_vertex':
            euler_83(y-1,x)
        else:
            euler_83(y,x-1)
    else:
        return

def d_edge(y, x, curr):
    d_vertex = curr + edges[y+1][x]
    if traveled[y+1][x] == 'inf':
        traveled[y+1][x] = d_vertex
    elif d_vertex < traveled[y+1][x]:
        traveled[y+1][x] = d_vertex
    else:
        d_vertex = False
    return d_vertex

def r_edge(y, x, curr):
    r_vertex = curr + edges[y][x+1]
    if traveled[y][x+1] == 'inf':
        traveled[y][x+1] = r_vertex
    elif r_vertex < traveled[y][x+1]:
        traveled[y][x+1] = r_vertex
    else:
        r_vertex = False
    return r_vertex

def u_edge(y, x, curr):
    u_vertex = curr + edges[y-1][x]
    if traveled[y-1][x] == 'inf':
        traveled[y-1][x] = u_vertex
    elif u_vertex < traveled[y-1][x]:
        traveled[y-1][x] = u_vertex
    else:
        u_vertex = False
    return u_vertex

def l_edge(y, x, curr):
    l_vertex = curr + edges[y][x-1]
    if traveled[y][x-1] == 'inf':
        traveled[y][x-1] = l_vertex
    elif l_vertex < traveled[y][x-1]:
        traveled[y][x-1] = l_vertex
    else:
        l_vertex = False
    return l_vertex

def push_heap(y, x, heap_mv):
    mv_coor = {'d_vertex':[y+1,x],
               'r_vertex':[y,x+1],
               'u_vertex':[y-1,x],
               'l_vertex':[y,x-1]
              }
    heap.extend([mv_coor[i] for i in heap_mv])
    return

while heap:
    y, x = heap.pop(0)
    euler_83(y, x)

stop = timeit.default_timer()
print "Answer: %d" % traveled[79][79]
print "Time: %f" % (stop - start)

<|endoftext|>


<commit_before>import re
import select
import threading
import socket
import sys
import time

# Helper module
try:
    from .helper import H
except:
    from helper import H

# Settings variables
try:
    from . import settings as S
except:
    import settings as S

# Config module
from .config import get_value

# Log module
from .log import debug

# HTML entities
try:
    from html.entities import name2codepoint
except ImportError:
    from htmlentitydefs import name2codepoint

ILLEGAL_XML_UNICODE_CHARACTERS = [
    (0x00, 0x08), (0x0B, 0x0C), (0x0E, 0x1F), (0x7F, 0x84),
    (0x86, 0x9F), (0xD800, 0xDFFF), (0xFDD0, 0xFDDF),
    (0xFFFE, 0xFFFF),
    (0x1FFFE, 0x1FFFF), (0x2FFFE, 0x2FFFF), (0x3FFFE, 0x3FFFF),
    (0x4FFFE, 0x4FFFF), (0x5FFFE, 0x5FFFF), (0x6FFFE, 0x6FFFF),
    (0x7FFFE, 0x7FFFF), (0x8FFFE, 0x8FFFF), (0x9FFFE, 0x9FFFF),
    (0xAFFFE, 0xAFFFF), (0xBFFFE, 0xBFFFF), (0xCFFFE, 0xCFFFF),
    (0xDFFFE, 0xDFFFF), (0xEFFFE, 0xEFFFF), (0xFFFFE, 0xFFFFF),
    (0x10FFFE, 0x10FFFF) ]

ILLEGAL_XML_RANGES = ["%s-%s" % (H.unicode_chr(low), H.unicode_chr(high))
                  for (low, high) in ILLEGAL_XML_UNICODE_CHARACTERS
                  if low < sys.maxunicode]

ILLEGAL_XML_RE = re.compile(H.unicode_string('[%s]') % H.unicode_string('').join(ILLEGAL_XML_RANGES))

infStr = "function() return math.huge end"
negInfStr = "function() return -math.huge end"
nanStr = "function() return 0/0 end"

def serialize( value ):
    t = type( value )
    if t == int or t == float:
        if value == float('inf'):
            res = infStr
        elif value == -float('inf'):
            res = negInfStr
        elif value == float('nan'):
            res = nanStr
        else:
            res = str( value )

        return res
    elif t == bool:
        return str(value).lower()
    elif t == str:
        return '"'+value.replace('"', '\\"')+'"'
    elif t == type(None):
        return "nil"
    elif t == dict:
        res = "{ "
        for k, v in value.items():
            res = res+"["+serialize( k )+"] = "+serialize( v )+", "
        res = res+" }"
        return res
    else:
        error( "Can't serialize a value of type "+str(t) )

def convert_lua_table_str_to_python_dict_str(s):
    subbed_s = re.sub(r"\[(.+?)\]\s*=\s*([\w\W]*?[,{])", r"\g<1>: \g<2>", s) # NOTE: does not support keys with newlines, also expects all keys to be wrapped in []
    return re.sub(r":\s*\"([\w\W]*?)\"([,{])", r': """\g<1>"""\g<2>', subbed_s) # replace '= "X"' with '= """X"""'

def deserialize( s ):
    ds = s.replace(infStr, "float('inf')")
    ds = ds.replace(negInfStr, "-float('inf')")
    ds = ds.replace(nanStr, "float('nan')")

    ds = ds.replace('true', 'True')
    ds = ds.replace('false', 'False')

    ds = ds.replace('nil', 'None')
    ds = ds.replace('\\\n\9', '\\\n')

    try:
        ds = convert_lua_table_str_to_python_dict_str(ds)
        result = eval(ds)
        return result
    except BaseException:
        e = sys.exc_info()[1]
        raise ProtocolException("Error deserializing message \n{}\n{}".format(s, e))

def assert_locked(func):
    def wrapper(self, *args, **kwargs):
        assert self.is_locked(), "Cannot access Protocol methods outside of a with statement! This is to ensure thread safety."
        return func(self, *args, **kwargs)

    return wrapper

class Protocol(object):
    """
    Class for connecting with debugger engine  ####which uses DBGp protocol.#### no longer true
    """

    # Maximum amount of data to be received at once by socket
    read_size = 1024

    def __init__(self, on_connection_lost_cb=None):
        # Set port number to listen for response
        self.port = get_value(S.KEY_PORT, S.DEFAULT_PORT)

        self.messages = [] # pulled from the lua client (a response to a message we sent)

        self.command_cbs = {}

        self.lock = threading.RLock()
        self.locked = 0
        self.listening_canceled_event = threading.Event()

        self.on_connection_lost_cb = on_connection_lost_cb

        with self as s:
            s.clear()

    def __enter__(self):
        self.lock.acquire()
        self.locked += 1
        return self

    def __exit__(self, err_type, error_obj, traceback):
        self.lock.release()
        self.locked = max(self.locked - 1, 0)

    def is_locked(self):
        return self.locked > 0

    def register_command_cb(self, cmd_name, cb):
        cbs = self.command_cbs.setdefault(cmd_name, [])
        cbs.append(cb)

    def transaction_id():
        """
        Standard argument for sending commands, an unique numerical ID.
        """
        def fget(self):
            self._transaction_id += 1
            return self._transaction_id

        def fset(self, value):
            self._transaction_id = value

        def fdel(self):
            self._transaction_id = 0
        return locals()

    # Transaction ID property
    transaction_id = property(**transaction_id())

    @assert_locked
    def clear(self):
        """
        Clear variables, reset transaction_id, close socket connection.
        """
        self.buffer = ''
        self.connected = False
        self.listening = False
        del self.transaction_id
        try:
            self.socket.close()
        except:
            pass
        self.socket = None

        #self.lock = threading.RLock()
        #self.locked = 0

        self.listening_canceled_event.clear()

    def stop_listening_for_incoming_connections(self):
        self.listening_canceled_event.set()

    def unescape(self, string):
        """
        Convert HTML entities and character references to ordinary characters.
        """
        def convert(matches):
            text = matches.group(0)
            # Character reference
            if text[:2] == "&#":
                try:
                    if text[:3] == "&#x":
                        return H.unicode_chr(int(text[3:-1], 16))
                    else:
                        return H.unicode_chr(int(text[2:-1]))
                except ValueError:
                    pass
            # Named entity
            else:
                try:
                    # Following are not needed to be converted for XML
                    if text[1:-1] == "amp" or text[1:-1] == "gt" or text[1:-1] == "lt":
                        pass
                    else:
                        text = H.unicode_chr(name2codepoint[text[1:-1]])
                except KeyError:
                    pass
            return text
        return re.sub("&#?\w+;", convert, string)

    @assert_locked
    def is_command(self, message):
        if not message:
            return False

        deserialized_message = deserialize(message)

        return deserialized_message in ('break', 'synchronize')

    @assert_locked
    def handle_command(self, message):
        command_name = deserialize(message)

        if command_name == 'break':
            filename = deserialize(self.read_next_message())
            line = deserialize(self.read_next_message())

            cbargs = (filename, line)

        elif command_name == 'synchronize':
            cbargs = tuple()

        cbs = self.command_cbs.get(command_name)
        if cbs and len(cbs) > 0:
            for cb in cbs:
                cb(*cbargs)

    @assert_locked
    def check_connection(self):
        t = time.clock()
        if not hasattr(self, 'last_check_time') or self.last_check_time - t >= 1: # need to rate-limit this so we don't flood the network
            self.last_check_time = t
            self.send('', 'ka') # keep-alive channel

    @assert_locked
    def update(self):
        self.check_connection()

        # read in messages (these might be commands which will be handled immediately)
        message = self.read_next_message(async=True)

        # if it's not a command, we need to keep it around for the next read request
        if message:
            self.messages.append(message)

    @assert_locked
    def parse_grld_message(self, message):
        """
        returns parsed_message, remaining_buffer_data
        """
        if len(message) == 0:
            return '', ''

        if message.count("\n") < 2:
            raise ProtocolException("Tried to parse malformed GRLD data")

        channel, messageSize, remaining = message.split("\n", 2)
        data = remaining[:int(messageSize)]
        remaining = remaining[int(messageSize):]

        return data, remaining

    @assert_locked
    def read_socket_into_buffer(self, async=False):
        """
        Get response data from debugger engine.
        """
        # Check socket connection
        if self.connected:
            # Get result data from debugger engine
            try:
                if async:
                    r, _, _ = select.select([self.socket], [], [], 0)
                    if not r:
                        return

                rawSockData = ''
                while True:
                    rawSockData = self.socket.recv(self.read_size)
                    self.buffer += H.data_read(rawSockData)

                    r, _, _ = select.select([self.socket], [], [], 0)
                    if not r or not rawSockData:
                        break

            except:
                e = sys.exc_info()[1]
                raise ProtocolConnectionException(e)
        else:
            raise ProtocolConnectionException("GRLD is not connected")

    @assert_locked
    def read_next_message(self, async=False):
        """
        Update buffer from socket if buffer is empty.

        Parse out buffer data and return the next message.
        """
        # Verify length of response data
        # length = self.read_socket_into_buffer()

        if len(self.buffer) <= 0:
            self.read_socket_into_buffer(async)

        if len(self.buffer) <= 0:
            return None

        while True:
            message, self.buffer = self.parse_grld_message(self.buffer)

            if self.is_command(message):
                # kind of gross to do this here (heavy coupling, poor concern encapsulation), but it needs to happen immediately because the GRLD client might expect a response
                self.handle_command(message)
            else:
                break

        return message

    @assert_locked
    def read(self, return_string=False):
        if len(self.messages):
            message = self.messages.pop(0)
        else:
            message = self.read_next_message()

        if return_string:
            return message
        else:
            return deserialize(message)

    @assert_locked
    def send(self, data, channel='default'):
        s_data = serialize(data)
        formatted_data = channel + '\n' + str(len(s_data)) + '\n' + s_data

        try:
            self.socket.send(H.data_write(formatted_data))
        except BaseException as e:
            self.socket.close()
            if self.on_connection_lost_cb:
                self.on_connection_lost_cb(e)

    @assert_locked
    def listen(self):
        """
        Create socket server which listens for connection on configured port.
        """
        # Create socket server
        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        if server:
            # Configure socket server
            try:
                server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                server.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
                server.settimeout(1)
                server.bind(('', self.port))
                server.listen(1)
                self.listening = True
                self.socket = None
            except:
                e = sys.exc_info()[1]
                raise ProtocolConnectionException(e)

            # Accept incoming connection on configured port
            while not self.listening_canceled_event.is_set() and self.listening:
                try:
                    self.socket, address = server.accept()
                    self.listening = False
                except socket.timeout:
                    pass

            # Check if a connection has been made
            if self.socket:
                self.connected = True
                self.socket.settimeout(None)
                self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
            else:
                self.connected = False
                self.listening = False

            # Close socket server
            try:
                server.close()
            except:
                pass
            server = None

            # Return socket connection
            return self.socket
        else:
            raise ProtocolConnectionException('Could not create socket server.')

class ProtocolException(Exception):
    pass

class ProtocolConnectionException(ProtocolException):
    pass<commit_msg>Actually rate-limit correctly<commit_after>import re
import select
import threading
import socket
import sys
import time

# Helper module
try:
    from .helper import H
except:
    from helper import H

# Settings variables
try:
    from . import settings as S
except:
    import settings as S

# Config module
from .config import get_value

# Log module
from .log import debug

# HTML entities
try:
    from html.entities import name2codepoint
except ImportError:
    from htmlentitydefs import name2codepoint

ILLEGAL_XML_UNICODE_CHARACTERS = [
    (0x00, 0x08), (0x0B, 0x0C), (0x0E, 0x1F), (0x7F, 0x84),
    (0x86, 0x9F), (0xD800, 0xDFFF), (0xFDD0, 0xFDDF),
    (0xFFFE, 0xFFFF),
    (0x1FFFE, 0x1FFFF), (0x2FFFE, 0x2FFFF), (0x3FFFE, 0x3FFFF),
    (0x4FFFE, 0x4FFFF), (0x5FFFE, 0x5FFFF), (0x6FFFE, 0x6FFFF),
    (0x7FFFE, 0x7FFFF), (0x8FFFE, 0x8FFFF), (0x9FFFE, 0x9FFFF),
    (0xAFFFE, 0xAFFFF), (0xBFFFE, 0xBFFFF), (0xCFFFE, 0xCFFFF),
    (0xDFFFE, 0xDFFFF), (0xEFFFE, 0xEFFFF), (0xFFFFE, 0xFFFFF),
    (0x10FFFE, 0x10FFFF) ]

ILLEGAL_XML_RANGES = ["%s-%s" % (H.unicode_chr(low), H.unicode_chr(high))
                  for (low, high) in ILLEGAL_XML_UNICODE_CHARACTERS
                  if low < sys.maxunicode]

ILLEGAL_XML_RE = re.compile(H.unicode_string('[%s]') % H.unicode_string('').join(ILLEGAL_XML_RANGES))

infStr = "function() return math.huge end"
negInfStr = "function() return -math.huge end"
nanStr = "function() return 0/0 end"

def serialize( value ):
    t = type( value )
    if t == int or t == float:
        if value == float('inf'):
            res = infStr
        elif value == -float('inf'):
            res = negInfStr
        elif value == float('nan'):
            res = nanStr
        else:
            res = str( value )

        return res
    elif t == bool:
        return str(value).lower()
    elif t == str:
        return '"'+value.replace('"', '\\"')+'"'
    elif t == type(None):
        return "nil"
    elif t == dict:
        res = "{ "
        for k, v in value.items():
            res = res+"["+serialize( k )+"] = "+serialize( v )+", "
        res = res+" }"
        return res
    else:
        error( "Can't serialize a value of type "+str(t) )

def convert_lua_table_str_to_python_dict_str(s):
    subbed_s = re.sub(r"\[(.+?)\]\s*=\s*([\w\W]*?[,{])", r"\g<1>: \g<2>", s) # NOTE: does not support keys with newlines, also expects all keys to be wrapped in []
    return re.sub(r":\s*\"([\w\W]*?)\"([,{])", r': """\g<1>"""\g<2>', subbed_s) # replace '= "X"' with '= """X"""'

def deserialize( s ):
    ds = s.replace(infStr, "float('inf')")
    ds = ds.replace(negInfStr, "-float('inf')")
    ds = ds.replace(nanStr, "float('nan')")

    ds = ds.replace('true', 'True')
    ds = ds.replace('false', 'False')

    ds = ds.replace('nil', 'None')
    ds = ds.replace('\\\n\9', '\\\n')

    try:
        ds = convert_lua_table_str_to_python_dict_str(ds)
        result = eval(ds)
        return result
    except BaseException:
        e = sys.exc_info()[1]
        raise ProtocolException("Error deserializing message \n{}\n{}".format(s, e))

def assert_locked(func):
    def wrapper(self, *args, **kwargs):
        assert self.is_locked(), "Cannot access Protocol methods outside of a with statement! This is to ensure thread safety."
        return func(self, *args, **kwargs)

    return wrapper

class Protocol(object):
    """
    Class for connecting with debugger engine  ####which uses DBGp protocol.#### no longer true
    """

    # Maximum amount of data to be received at once by socket
    read_size = 1024

    def __init__(self, on_connection_lost_cb=None):
        # Set port number to listen for response
        self.port = get_value(S.KEY_PORT, S.DEFAULT_PORT)

        self.messages = [] # pulled from the lua client (a response to a message we sent)

        self.command_cbs = {}

        self.lock = threading.RLock()
        self.locked = 0
        self.listening_canceled_event = threading.Event()

        self.on_connection_lost_cb = on_connection_lost_cb

        with self as s:
            s.clear()

    def __enter__(self):
        self.lock.acquire()
        self.locked += 1
        return self

    def __exit__(self, err_type, error_obj, traceback):
        self.lock.release()
        self.locked = max(self.locked - 1, 0)

    def is_locked(self):
        return self.locked > 0

    def register_command_cb(self, cmd_name, cb):
        cbs = self.command_cbs.setdefault(cmd_name, [])
        cbs.append(cb)

    def transaction_id():
        """
        Standard argument for sending commands, an unique numerical ID.
        """
        def fget(self):
            self._transaction_id += 1
            return self._transaction_id

        def fset(self, value):
            self._transaction_id = value

        def fdel(self):
            self._transaction_id = 0
        return locals()

    # Transaction ID property
    transaction_id = property(**transaction_id())

    @assert_locked
    def clear(self):
        """
        Clear variables, reset transaction_id, close socket connection.
        """
        self.buffer = ''
        self.connected = False
        self.listening = False
        del self.transaction_id
        try:
            self.socket.close()
        except:
            pass
        self.socket = None

        #self.lock = threading.RLock()
        #self.locked = 0

        self.listening_canceled_event.clear()

    def stop_listening_for_incoming_connections(self):
        self.listening_canceled_event.set()

    def unescape(self, string):
        """
        Convert HTML entities and character references to ordinary characters.
        """
        def convert(matches):
            text = matches.group(0)
            # Character reference
            if text[:2] == "&#":
                try:
                    if text[:3] == "&#x":
                        return H.unicode_chr(int(text[3:-1], 16))
                    else:
                        return H.unicode_chr(int(text[2:-1]))
                except ValueError:
                    pass
            # Named entity
            else:
                try:
                    # Following are not needed to be converted for XML
                    if text[1:-1] == "amp" or text[1:-1] == "gt" or text[1:-1] == "lt":
                        pass
                    else:
                        text = H.unicode_chr(name2codepoint[text[1:-1]])
                except KeyError:
                    pass
            return text
        return re.sub("&#?\w+;", convert, string)

    @assert_locked
    def is_command(self, message):
        if not message:
            return False

        deserialized_message = deserialize(message)

        return deserialized_message in ('break', 'synchronize')

    @assert_locked
    def handle_command(self, message):
        command_name = deserialize(message)

        if command_name == 'break':
            filename = deserialize(self.read_next_message())
            line = deserialize(self.read_next_message())

            cbargs = (filename, line)

        elif command_name == 'synchronize':
            cbargs = tuple()

        cbs = self.command_cbs.get(command_name)
        if cbs and len(cbs) > 0:
            for cb in cbs:
                cb(*cbargs)

    @assert_locked
    def check_connection(self):
        t = time.clock()
        if not hasattr(self, 'last_check_time') or (t - self.last_check_time) >= 1: # need to rate-limit this so we don't flood the network
            self.last_check_time = t
            self.send('', 'ka') # keep-alive channel

    @assert_locked
    def update(self):
        self.check_connection()

        # read in messages (these might be commands which will be handled immediately)
        message = self.read_next_message(async=True)

        # if it's not a command, we need to keep it around for the next read request
        if message:
            self.messages.append(message)

    @assert_locked
    def parse_grld_message(self, message):
        """
        returns parsed_message, remaining_buffer_data
        """
        if len(message) == 0:
            return '', ''

        if message.count("\n") < 2:
            raise ProtocolException("Tried to parse malformed GRLD data")

        channel, messageSize, remaining = message.split("\n", 2)
        data = remaining[:int(messageSize)]
        remaining = remaining[int(messageSize):]

        return data, remaining

    @assert_locked
    def read_socket_into_buffer(self, async=False):
        """
        Get response data from debugger engine.
        """
        # Check socket connection
        if self.connected:
            # Get result data from debugger engine
            try:
                if async:
                    r, _, _ = select.select([self.socket], [], [], 0)
                    if not r:
                        return

                rawSockData = ''
                while True:
                    rawSockData = self.socket.recv(self.read_size)
                    self.buffer += H.data_read(rawSockData)

                    r, _, _ = select.select([self.socket], [], [], 0)
                    if not r or not rawSockData:
                        break

            except:
                e = sys.exc_info()[1]
                raise ProtocolConnectionException(e)
        else:
            raise ProtocolConnectionException("GRLD is not connected")

    @assert_locked
    def read_next_message(self, async=False):
        """
        Update buffer from socket if buffer is empty.

        Parse out buffer data and return the next message.
        """
        # Verify length of response data
        # length = self.read_socket_into_buffer()

        if len(self.buffer) <= 0:
            self.read_socket_into_buffer(async)

        if len(self.buffer) <= 0:
            return None

        while True:
            message, self.buffer = self.parse_grld_message(self.buffer)

            if self.is_command(message):
                # kind of gross to do this here (heavy coupling, poor concern encapsulation), but it needs to happen immediately because the GRLD client might expect a response
                self.handle_command(message)
            else:
                break

        return message

    @assert_locked
    def read(self, return_string=False):
        if len(self.messages):
            message = self.messages.pop(0)
        else:
            message = self.read_next_message()

        if return_string:
            return message
        else:
            return deserialize(message)

    @assert_locked
    def send(self, data, channel='default'):
        s_data = serialize(data)
        formatted_data = channel + '\n' + str(len(s_data)) + '\n' + s_data

        try:
            self.socket.send(H.data_write(formatted_data))
        except BaseException as e:
            self.socket.close()
            if self.on_connection_lost_cb:
                self.on_connection_lost_cb(e)

    @assert_locked
    def listen(self):
        """
        Create socket server which listens for connection on configured port.
        """
        # Create socket server
        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        if server:
            # Configure socket server
            try:
                server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                server.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
                server.settimeout(1)
                server.bind(('', self.port))
                server.listen(1)
                self.listening = True
                self.socket = None
            except:
                e = sys.exc_info()[1]
                raise ProtocolConnectionException(e)

            # Accept incoming connection on configured port
            while not self.listening_canceled_event.is_set() and self.listening:
                try:
                    self.socket, address = server.accept()
                    self.listening = False
                except socket.timeout:
                    pass

            # Check if a connection has been made
            if self.socket:
                self.connected = True
                self.socket.settimeout(None)
                self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
            else:
                self.connected = False
                self.listening = False

            # Close socket server
            try:
                server.close()
            except:
                pass
            server = None

            # Return socket connection
            return self.socket
        else:
            raise ProtocolConnectionException('Could not create socket server.')

class ProtocolException(Exception):
    pass

class ProtocolConnectionException(ProtocolException):
    pass<|endoftext|>


<commit_before># encoding=utf-8
# The Python Imaging Library (PIL) is

#    Copyright © 1997-2011 by Secret Labs AB
#    Copyright © 1995-2011 by Fredrik Lundh

# By obtaining, using, and/or copying this software and/or its associated
# documentation, you agree that you have read, understood, and will comply
# with the following terms and conditions:

# Permission to use, copy, modify, and distribute this software and its
# associated documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appears in all copies, and that
# both that copyright notice and this permission notice appear in supporting
# documentation, and that the name of Secret Labs AB or the author not be used
# in advertising or publicity pertaining to distribution of the software
# without specific, written prior permission.

# SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
# IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR ANY SPECIAL,
# INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
"""Plugin for using PurePNG with PIL"""
from PIL import Image, ImageFile
import array
import png
__version__ = "0.1.0"

try:
    from itertools import izip_longest as zip_l
except ImportError:
    from itertools import zip_longest as zip_l

try:
    bytes
except NameError:
    bytes = str

def buf_emu(not_buffer):
    if hasattr(not_buffer, 'tostring'):
        return not_buffer.tostring()
    else:
        return bytes(not_buffer)

try:
    buffer
except NameError:
    try:
        buffer = memoryview
    except NameError:
        buffer = buf_emu

def group(s, n):
    """Repack iterator items into groups"""
    # See http://www.python.org/doc/2.6/library/functions.html#zip
    return list(zip(*[iter(s)] * n))

class PngImageFile(ImageFile.ImageFile):
    format = "PNG"
    format_description = "Portable network graphics"

    def _open(self):
        self.png = png.Reader(file=self.fp)
        self.png.preamble()
        direct = self.png.asDirect
        bitdepth = self.png.bitdepth
        if self.png.sbit:
            import struct
            sbit = struct.unpack('%dB' % len(self.png.sbit), self.png.sbit)
            bitdepth = max(sbit)
        if bitdepth < 8:
            x, y, pixels, meta = self.png._as_rescale(direct, 8)
        elif 16 > bitdepth > 8 and self.png.greyscale:
            x, y, pixels, meta = self.png._as_rescale(direct, 16)
        # PIL does not support RGB 16bit/channel
        elif bitdepth != 8 and not self.png.greyscale:
            x, y, pixels, meta = self.png._as_rescale(direct, 8)
        else:
            x, y, pixels, meta = direct()
        self.size = x, y
        self.mode = "L" if meta['greyscale'] else "RGB"
        if meta['alpha']:
            self.mode = self.mode + 'A'

        if meta['bitdepth'] == 16:
            if self.mode == 'L':
                self.mode = 'I'
                self.rawmode = 'I;16'
            else:
                # 16 bit per pixel decoders are big endian byte order
                self.rawmode = self.mode + ';' + str(meta['bitdepth']) + 'B'
        else:
            self.rawmode = self.mode
        if self.rawmode == 'LA;16B':
            self.mode = 'RGBA'  # No native LA 16 bit in PIL
        self.pixels = pixels
        # image data
        self.tile = [("raw", (0, 0) + self.size, 0, self.rawmode)]
        if 'gamma' in meta:
            self.info['gamma'] = meta['gamma']
        if 'icc_profile' in meta:
            self.info['icc_profile'] = meta['icc_profile']
        if 'physical' in meta:
            if meta['physical'][1] == 1:
                self.info['dpi'] = (int(meta['physical'][0][0] * 0.0254 + 0.5),
                                    int(meta['physical'][0][1] * 0.0254 + 0.5))
            elif meta['physical'][1] == 0:
                self.info['aspect'] = (meta['physical'][0][0],
                                       meta['physical'][0][1])
        self.info.update(meta['text'])  # experimental

    def verify(self):
        "Verify PNG file"
        pass

    def load_read(self, n_bytes):
        """internal: read more image data"""
        row = next(self.pixels)
        if isinstance(row, array.array):
            if row.typecode == 'H':
                if self.rawmode.endswith('B'):
                    row.byteswap()
        return bytes(buffer(row))

    def load_seek(self, pos=0):
        pass

# --------------------------------------------------------------------
# PNG writer
def get_palette(im):
    """Prepare palete for saving. Return (palette, bitdepth) tuple."""
    palette_bytes = im.palette.getdata()[1]
    # attempt to minimize storage requirements for palette images
    if "bits" in im.encoderinfo:
        # number of bits specified by user
        colors = 1 << im.encoderinfo["bits"]
    else:
        # check palette contents
        if im.palette:
            # For now palette only RGB
            colors = max(min(len(palette_bytes) // 3, 256), 2)
        else:
            colors = 256

    if colors <= 2:
        bits = 1
    elif colors <= 4:
        bits = 2
    elif colors <= 16:
        bits = 4
    else:
        bits = 8

    # For now palette only RGB
    palette_byte_number = (2 ** bits) * 3
    # Match to declared palette size
    palette_bytes = bytearray(palette_bytes[:palette_byte_number])
    if len(palette_bytes) < palette_byte_number:
        palette_bytes.extend((0,) * (palette_byte_number - len(palette_bytes)))
    return (group(palette_bytes, 3), bits)

def _save(im, fp, filename):
    """save an image to disk (called by the save method)"""

    def rows(im):
        """Rows generator from image"""
        for i in range(im.size[1]):
            row = []
            for j in range(im.size[0]):
                px = im.getpixel((j, i))
                if hasattr(px, '__iter__'):
                    # Multi-channel image
                    row.extend(px)
                else:
                    # Single channel image
                    row.append(px)
            yield row
    # Default values
    meta = dict(im.info)
    if im.mode == 'P':
        meta['palette'], bits = get_palette(im)
    else:
        parsed_mode = png.parse_mode(im.mode, 8)
        (meta['greyscale'], meta['alpha'], bits) = parsed_mode
        if bits == 1:
            fullrows = rows

            def rows(im):
                """Wrapper rows to strictly bool value"""
                for row in fullrows(im):
                    yield [bool(it) for it in row]

    transparency = im.encoderinfo.get('transparency',
                        im.info.get('transparency',
                            None))
    if (transparency or transparency == 0):
        if im.mode == "P":
            # "Patch" palette with transparency
            if isinstance(transparency, bytes):
                transparency = bytearray(transparency)
            else:  # not sure about this
                transparency = max(0, min(255, transparency))
                transparency = bytearray((255,) * transparency)

            # limit to actual palette size
            alpha_bytes = 2 ** bits
            palette = zip_l(meta['palette'],
                            transparency[:alpha_bytes],
                            fillvalue=255)
            meta['palette'] = list(map(lambda it: it[0] + (it[1],), palette))
            transparency = None
        elif im.mode == "L":
            transparency = max(0, min(65535, transparency))
        elif im.mode == "RGB":
            # as max value per channel in RGB mode is 255
            # higher trasparency is a bug
            if max(transparency) > 255:
                im = im.convert('RGBA')
                meta['alpha'] = True
                transparency = None
            pass  # triade will pass to writer
        else:
            if "transparency" in im.encoderinfo:
                # don't bother with transparency if it's an RGBA
                # and it's in the info dict. It's probably just stale.
                raise IOError("cannot use transparency for this mode")

    if 'aspect' in im.info:
        meta['physical'] = (meta.pop('aspect'), 0)
    if 'dpi' in im.info:
        meta['physical'] = (meta.pop('dpi'), 'i')
    meta.pop('transparency', None)

    writer = png.Writer(size=im.size,
                        bitdepth=bits,
                        transparent=transparency,
                        compression=im.encoderinfo.get("compress_level", -1),
                        **meta)

    writer.write(fp, rows(im))

    #  TODO: pnginfo (?)

    try:
        fp.flush()
    except:
        pass

# --------------------------------------------------------------------
# Registry
Image.register_open("PNG", PngImageFile,
                    lambda header: header[:8] == png.png_signature)
Image.register_save("PNG", _save)

Image.register_extension("PNG", ".png")

Image.register_mime("PNG", "image/png")
<commit_msg>Encoderinfo is pretty strange...<commit_after># encoding=utf-8
# The Python Imaging Library (PIL) is

#    Copyright © 1997-2011 by Secret Labs AB
#    Copyright © 1995-2011 by Fredrik Lundh

# By obtaining, using, and/or copying this software and/or its associated
# documentation, you agree that you have read, understood, and will comply
# with the following terms and conditions:

# Permission to use, copy, modify, and distribute this software and its
# associated documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appears in all copies, and that
# both that copyright notice and this permission notice appear in supporting
# documentation, and that the name of Secret Labs AB or the author not be used
# in advertising or publicity pertaining to distribution of the software
# without specific, written prior permission.

# SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
# IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR ANY SPECIAL,
# INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
"""Plugin for using PurePNG with PIL"""
from PIL import Image, ImageFile
import array
import png
__version__ = "0.1.0"

try:
    from itertools import izip_longest as zip_l
except ImportError:
    from itertools import zip_longest as zip_l

try:
    bytes
except NameError:
    bytes = str

def buf_emu(not_buffer):
    if hasattr(not_buffer, 'tostring'):
        return not_buffer.tostring()
    else:
        return bytes(not_buffer)

try:
    buffer
except NameError:
    try:
        buffer = memoryview
    except NameError:
        buffer = buf_emu

def group(s, n):
    """Repack iterator items into groups"""
    # See http://www.python.org/doc/2.6/library/functions.html#zip
    return list(zip(*[iter(s)] * n))

class PngImageFile(ImageFile.ImageFile):
    format = "PNG"
    format_description = "Portable network graphics"

    def _open(self):
        self.png = png.Reader(file=self.fp)
        self.png.preamble()
        direct = self.png.asDirect
        bitdepth = self.png.bitdepth
        if self.png.sbit:
            import struct
            sbit = struct.unpack('%dB' % len(self.png.sbit), self.png.sbit)
            bitdepth = max(sbit)
        if bitdepth < 8:
            x, y, pixels, meta = self.png._as_rescale(direct, 8)
        elif 16 > bitdepth > 8 and self.png.greyscale:
            x, y, pixels, meta = self.png._as_rescale(direct, 16)
        # PIL does not support RGB 16bit/channel
        elif bitdepth != 8 and not self.png.greyscale:
            x, y, pixels, meta = self.png._as_rescale(direct, 8)
        else:
            x, y, pixels, meta = direct()
        self.size = x, y
        self.mode = "L" if meta['greyscale'] else "RGB"
        if meta['alpha']:
            self.mode = self.mode + 'A'

        if meta['bitdepth'] == 16:
            if self.mode == 'L':
                self.mode = 'I'
                self.rawmode = 'I;16'
            else:
                # 16 bit per pixel decoders are big endian byte order
                self.rawmode = self.mode + ';' + str(meta['bitdepth']) + 'B'
        else:
            self.rawmode = self.mode
        if self.rawmode == 'LA;16B':
            self.mode = 'RGBA'  # No native LA 16 bit in PIL
        self.pixels = pixels
        # image data
        self.tile = [("raw", (0, 0) + self.size, 0, self.rawmode)]
        if 'gamma' in meta:
            self.info['gamma'] = meta['gamma']
        if 'icc_profile' in meta:
            self.info['icc_profile'] = meta['icc_profile']
        if 'physical' in meta:
            if meta['physical'][1] == 1:
                self.info['dpi'] = (int(meta['physical'][0][0] * 0.0254 + 0.5),
                                    int(meta['physical'][0][1] * 0.0254 + 0.5))
            elif meta['physical'][1] == 0:
                self.info['aspect'] = (meta['physical'][0][0],
                                       meta['physical'][0][1])
        self.info.update(meta['text'])  # experimental

    def verify(self):
        "Verify PNG file"
        pass

    def load_read(self, n_bytes):
        """internal: read more image data"""
        row = next(self.pixels)
        if isinstance(row, array.array):
            if row.typecode == 'H':
                if self.rawmode.endswith('B'):
                    row.byteswap()
        return bytes(buffer(row))

    def load_seek(self, pos=0):
        pass

# --------------------------------------------------------------------
# PNG writer
def get_palette(im):
    """Prepare palete for saving. Return (palette, bitdepth) tuple."""
    palette_bytes = im.palette.getdata()[1]
    # attempt to minimize storage requirements for palette images
    if "bits" in im.encoderinfo:
        # number of bits specified by user
        colors = 1 << im.encoderinfo["bits"]
    else:
        # check palette contents
        if im.palette:
            # For now palette only RGB
            colors = max(min(len(palette_bytes) // 3, 256), 2)
        else:
            colors = 256

    if colors <= 2:
        bits = 1
    elif colors <= 4:
        bits = 2
    elif colors <= 16:
        bits = 4
    else:
        bits = 8

    # For now palette only RGB
    palette_byte_number = (2 ** bits) * 3
    # Match to declared palette size
    palette_bytes = bytearray(palette_bytes[:palette_byte_number])
    if len(palette_bytes) < palette_byte_number:
        palette_bytes.extend((0,) * (palette_byte_number - len(palette_bytes)))
    return (group(palette_bytes, 3), bits)

def _save(im, fp, filename):
    """save an image to disk (called by the save method)"""
    encoderinfo = im.encoderinfo

    def rows(im):
        """Rows generator from image"""
        for i in range(im.size[1]):
            row = []
            for j in range(im.size[0]):
                px = im.getpixel((j, i))
                if hasattr(px, '__iter__'):
                    # Multi-channel image
                    row.extend(px)
                else:
                    # Single channel image
                    row.append(px)
            yield row
    # Default values
    meta = dict(im.info)
    if im.mode == 'P':
        meta['palette'], bits = get_palette(im)
    else:
        parsed_mode = png.parse_mode(im.mode, 8)
        (meta['greyscale'], meta['alpha'], bits) = parsed_mode
        if bits == 1:
            fullrows = rows

            def rows(im):
                """Wrapper rows to strictly bool value"""
                for row in fullrows(im):
                    yield [bool(it) for it in row]

    transparency = im.encoderinfo.get('transparency',
                        im.info.get('transparency',
                            None))
    if (transparency or transparency == 0):
        if im.mode == "P":
            # "Patch" palette with transparency
            if isinstance(transparency, bytes):
                transparency = bytearray(transparency)
            else:  # not sure about this
                transparency = max(0, min(255, transparency))
                transparency = bytearray((255,) * transparency)

            # limit to actual palette size
            alpha_bytes = 2 ** bits
            palette = zip_l(meta['palette'],
                            transparency[:alpha_bytes],
                            fillvalue=255)
            meta['palette'] = list(map(lambda it: it[0] + (it[1],), palette))
            transparency = None
        elif im.mode == "L":
            transparency = max(0, min(65535, transparency))
        elif im.mode == "RGB":
            # as max value per channel in RGB mode is 255
            # higher trasparency is a bug
            if max(transparency) > 255:
                im = im.convert('RGBA')
                meta['alpha'] = True
                transparency = None
            pass  # triade will pass to writer
        else:
            if "transparency" in encoderinfo:
                # don't bother with transparency if it's an RGBA
                # and it's in the info dict. It's probably just stale.
                raise IOError("cannot use transparency for this mode")

    if 'aspect' in im.info:
        meta['physical'] = (meta.pop('aspect'), 0)
    if 'dpi' in im.info:
        meta['physical'] = (meta.pop('dpi'), 'i')
    meta.pop('transparency', None)

    writer = png.Writer(size=im.size,
                        bitdepth=bits,
                        transparent=transparency,
                        compression=encoderinfo.get("compress_level", -1),
                        **meta)

    writer.write(fp, rows(im))

    #  TODO: pnginfo (?)

    try:
        fp.flush()
    except:
        pass

# --------------------------------------------------------------------
# Registry
Image.register_open("PNG", PngImageFile,
                    lambda header: header[:8] == png.png_signature)
Image.register_save("PNG", _save)

Image.register_extension("PNG", ".png")

Image.register_mime("PNG", "image/png")
<|endoftext|>


<commit_before><commit_msg>added in some enhancements<commit_after><|endoftext|>


<commit_before>"""Integrate guild with Qt.

The QtActorMixin mixin class allows you to add actor functionality to
any PyQt class. Your class runs in a Qt thread, unless it is a QWidget
(or derived from QWidget) in which case it runs in the main Qt thread.
Queued method invocations (@actor_method, @actor_function) are handled
as Qt events.

The ActorSignal class implements a simple bridge between guild and
PyQt. Any object received by its guild input is emitted on its Qt
signal. This allows guild and Qt components to be connected without
modifying either.

Qt signals can be connected directly to guild components' actor
methods. Their queued invocation allows them to be called safely from
a Qt thread.

"""

from PyQt4 import QtCore, QtGui

from actor import ActorMixin, ActorMetaclass, actor_method

class _QtActorMixinMetaclass(QtCore.pyqtWrapperType, ActorMetaclass):
    pass

class QtActorMixin(ActorMixin):
    __metaclass__ = _QtActorMixinMetaclass
    # create unique event types
    _qtactor_queue_event = QtCore.QEvent.registerEventType()
    _qtactor_step_event  = QtCore.QEvent.registerEventType()
    _qtactor_stop_event  = QtCore.QEvent.registerEventType()

    def __init__(self, *argv, **argd):
        super(QtActorMixin, self).__init__(*argv, **argd)
        self._qtactor_dispatch = {
            self._qtactor_queue_event : self._actor_do_queued,
            self._qtactor_step_event  : self._qtactor_step,
            self._qtactor_stop_event  : self._qtactor_stop,
            }
        # if not a Widget, move to a Qt thread
        if isinstance(self, QtGui.QWidget):
            # widgets can't be moved to another thread
            self._qtactor_thread = None
        else:
            # create a thread and move to it
            self._qtactor_thread = QtCore.QThread()
            self.moveToThread(self._qtactor_thread)
            self._qtactor_thread.started.connect(self._qtactor_run)

    def start(self):
        if self._qtactor_thread:
            self._qtactor_thread.start()
        else:
            self._qtactor_run()

    def _qtactor_run(self):
        self.process_start()
        self.process()
        # get gen_process generator
        try:
            self._qtactor_gen = self.gen_process()
        except AttributeError:
            self._qtactor_gen = None
        # do first step
        if self._qtactor_gen:
            self._qtactor_step()

    def _qtactor_step(self):
        try:
            self._qtactor_gen.next()
        except StopIteration:
            self._qtactor_gen = None
            return
        # trigger next step
        QtCore.QCoreApplication.postEvent(
            self, QtCore.QEvent(self._qtactor_step_event),
            QtCore.Qt.LowEventPriority)

    def _qtactor_stop(self):
        self._qtactor_dispatch = {}
        if self._qtactor_gen:
            self._qtactor_gen.close()
            self.onStop()
        if self._qtactor_thread:
            self._qtactor_thread.quit()

    def _actor_notify(self):
        QtCore.QCoreApplication.postEvent(
            self, QtCore.QEvent(self._qtactor_queue_event),
            QtCore.Qt.LowEventPriority)

    def event(self, event):
        event_type = event.type()
        if event_type in self._qtactor_dispatch:
            event.accept()
            self._qtactor_dispatch[event_type]()
            return True
        return super(QtActorMixin, self).event(event)

    def stop(self):
        QtCore.QCoreApplication.postEvent(
            self, QtCore.QEvent(self._qtactor_stop_event),
            QtCore.Qt.HighEventPriority)

    def join(self):
        if self._qtactor_thread:
            self._qtactor_thread.wait()

class ActorSignal(QtActorMixin, QtCore.QObject):
    signal = QtCore.pyqtSignal(object)

    @actor_method
    def input(self, msg):
        self.signal.emit(msg)
<commit_msg>Ensure onStop is called, even if no gen_process<commit_after>"""Integrate guild with Qt.

The QtActorMixin mixin class allows you to add actor functionality to
any PyQt class. Your class runs in a Qt thread, unless it is a QWidget
(or derived from QWidget) in which case it runs in the main Qt thread.
Queued method invocations (@actor_method, @actor_function) are handled
as Qt events.

The ActorSignal class implements a simple bridge between guild and
PyQt. Any object received by its guild input is emitted on its Qt
signal. This allows guild and Qt components to be connected without
modifying either.

Qt signals can be connected directly to guild components' actor
methods. Their queued invocation allows them to be called safely from
a Qt thread.

"""

from PyQt4 import QtCore, QtGui

from actor import ActorMixin, ActorMetaclass, actor_method

class _QtActorMixinMetaclass(QtCore.pyqtWrapperType, ActorMetaclass):
    pass

class QtActorMixin(ActorMixin):
    __metaclass__ = _QtActorMixinMetaclass
    # create unique event types
    _qtactor_queue_event = QtCore.QEvent.registerEventType()
    _qtactor_step_event  = QtCore.QEvent.registerEventType()
    _qtactor_stop_event  = QtCore.QEvent.registerEventType()

    def __init__(self, *argv, **argd):
        super(QtActorMixin, self).__init__(*argv, **argd)
        self._qtactor_dispatch = {
            self._qtactor_queue_event : self._actor_do_queued,
            self._qtactor_step_event  : self._qtactor_step,
            self._qtactor_stop_event  : self._qtactor_stop,
            }
        # if not a Widget, move to a Qt thread
        if isinstance(self, QtGui.QWidget):
            # widgets can't be moved to another thread
            self._qtactor_thread = None
        else:
            # create a thread and move to it
            self._qtactor_thread = QtCore.QThread()
            self.moveToThread(self._qtactor_thread)
            self._qtactor_thread.started.connect(self._qtactor_run)

    def start(self):
        if self._qtactor_thread:
            self._qtactor_thread.start()
        else:
            self._qtactor_run()

    def _qtactor_run(self):
        self.process_start()
        self.process()
        # get gen_process generator
        try:
            self._qtactor_gen = self.gen_process()
        except AttributeError:
            self._qtactor_gen = None
        # do first step
        if self._qtactor_gen:
            self._qtactor_step()

    def _qtactor_step(self):
        try:
            self._qtactor_gen.next()
        except StopIteration:
            self._qtactor_gen = None
            return
        # trigger next step
        QtCore.QCoreApplication.postEvent(
            self, QtCore.QEvent(self._qtactor_step_event),
            QtCore.Qt.LowEventPriority)

    def _qtactor_stop(self):
        self._qtactor_dispatch = {}
        if self._qtactor_gen:
            self._qtactor_gen.close()
        self.onStop()
        if self._qtactor_thread:
            self._qtactor_thread.quit()

    def _actor_notify(self):
        QtCore.QCoreApplication.postEvent(
            self, QtCore.QEvent(self._qtactor_queue_event),
            QtCore.Qt.LowEventPriority)

    def event(self, event):
        event_type = event.type()
        if event_type in self._qtactor_dispatch:
            event.accept()
            self._qtactor_dispatch[event_type]()
            return True
        return super(QtActorMixin, self).event(event)

    def stop(self):
        QtCore.QCoreApplication.postEvent(
            self, QtCore.QEvent(self._qtactor_stop_event),
            QtCore.Qt.HighEventPriority)

    def join(self):
        if self._qtactor_thread:
            self._qtactor_thread.wait()

class ActorSignal(QtActorMixin, QtCore.QObject):
    signal = QtCore.pyqtSignal(object)

    @actor_method
    def input(self, msg):
        self.signal.emit(msg)
<|endoftext|>


<commit_before>#!/usr/bin/env python

# Copyright (c) 2001, MetaSlash Inc.  All rights reserved.

"""
Check python source code files for possible errors and print warnings

Contact Info:
  http://pychecker.sourceforge.net/
  pychecker@metaslash.com
"""

import string
import types
import sys
import imp
import os
import glob

def setupNamespace(path) :
    # remove pychecker if it's the first component, it needs to be last
    if sys.path[0][-9:] == 'pychecker' :
        del sys.path[0]

    # make sure pychecker is last in path, so we can import
    checker_path = os.path.dirname(os.path.dirname(path))
    if checker_path not in sys.path :
        sys.path.append(checker_path)

if __name__ == '__main__' :
    setupNamespace(sys.argv[0])

from pychecker import printer
from pychecker import warn
from pychecker import OP
from pychecker import Config
from pychecker import function

# Globals for storing a dictionary of info about modules and classes
_allModules = {}
_cfg = None

# Constants
_DEFAULT_MODULE_TOKENS = [ '__builtins__', '__doc__', '__file__', '__name__',
                           '__path__', ]
_DEFAULT_CLASS_TOKENS = [ '__doc__', '__name__', '__module__', ]

def _flattenList(list) :
    "Returns a list which contains no lists"

    new_list = []
    for element in list :
        if type(element) == types.ListType :
            new_list.extend(_flattenList(element))
        else :
            new_list.append(element)

    return new_list

def getModules(arg_list) :
    "Returns a list of module names that can be imported"

    new_arguments = []
    for arg in arg_list :
        # is this a wildcard filespec? (necessary for windows)
        if '*' in arg or '?' in arg or '[' in arg :
            arg = glob.glob(arg)
        new_arguments.append(arg)

    PY_SUFFIX = '.py'
    PY_SUFFIX_LEN = len(PY_SUFFIX)

    modules = []
    for arg in _flattenList(new_arguments) :
        # is it a .py file?
        if len(arg) > PY_SUFFIX_LEN and arg[-PY_SUFFIX_LEN:] == PY_SUFFIX:
            arg_dir = os.path.dirname(arg)
            if arg_dir and not os.path.exists(arg) :
                print 'File or pathname element does not exist: "%s"' % arg
                continue

            module_name = os.path.basename(arg)[:-PY_SUFFIX_LEN]
            if arg_dir not in sys.path :
                sys.path.insert(1, arg_dir)
	    arg = module_name
        modules.append(arg)

    return modules

def _findModule(name, path = sys.path) :
    """Returns the result of an imp.find_module(), ie, (file, filename, smt)
       name can be a module or a package name.  It is *not* a filename."""

    packages = string.split(name, '.')
    for p in packages :
        # smt = (suffix, mode, type)
        file, filename, smt = imp.find_module(p, path)
        if smt[-1] == imp.PKG_DIRECTORY :
            try :
                # package found - read path info from init file
                m = imp.load_module(p, file, filename, smt)
            finally :
                if file is not None :
                    file.close()

            # importing xml plays a trick, which replaces itself with _xmlplus
            # both have subdirs w/same name, but different modules in them
            # we need to choose the real (replaced) version
            if m.__name__ != p :
                try :
                    file, filename, smt = imp.find_module(m.__name__, path)
                    m = imp.load_module(p, file, filename, smt)
                finally :
                    if file is not None :
                        file.close()

	    new_path = m.__path__
	    if type(new_path) == types.ListType :
	        new_path = filename
            if new_path not in path :
                path.insert(1, new_path)
        else:
            if p is not packages[-1] :
                if file is not None :
                    file.close()
                raise ImportError, "No module named %s" % packages[-1]
            return file, filename, smt

    # in case we have been given a package to check
    return file, filename, smt

class Variable :
    "Class to hold all information about a variable"

    def __init__(self, name, type):
        self.name = name
        self.type = type
        self.value = None

def _filterDir(object, ignoreList) :
    "Return a list of tokens (attributes) in a class, except for ignoreList"

    tokens = dir(object)
    for token in ignoreList :
        if token in tokens :
            tokens.remove(token)
    return tokens

def _getClassTokens(c) :
    return _filterDir(c, _DEFAULT_CLASS_TOKENS)

class Class :
    "Class to hold all information about a class"

    def __init__(self, name, module) :
        self.name = name
        self.module = module
        self.classObject = getattr(module, name)
        self.ignoreAttrs = 0
        self.methods = {}
        self.members = { '__class__': types.ClassType,
                         '__doc__': types.StringType,
                         '__dict__': types.DictType, }

    def getFirstLine(self) :
        "Return first line we can find in THIS class, not any base classes"

        lineNums = []
        classDir = dir(self.classObject)
        for m in self.methods.values() :
            if m != None and m.function.func_code.co_name in classDir:
                lineNums.append(m.function.func_code.co_firstlineno)
        if lineNums :
            return min(lineNums)
        return 0

    def allBaseClasses(self, c = None) :
        "Return a list of all base classes for this class and it's subclasses"

        baseClasses = []
        if c == None :
            c = self.classObject
        for base in c.__bases__ :
            baseClasses = baseClasses + [ base ] + self.allBaseClasses(base)
        return baseClasses

    def __getMethodName(self, func_name, className = None) :
        if func_name[0:2] == '__' and func_name[-2:] != '__' :
            if className == None :
                className = self.name
            if className[0] != '_' :
                className = '_' + className
            func_name = className + func_name
        return func_name

    def addMethod(self, method, className = None, methodName = None) :
        if type(method) == types.StringType :
            self.methods[method] = None
            return
        if not hasattr(method, "func_name") :
            return

        if not methodName :
            methodName = self.__getMethodName(method.func_name, className)
        self.methods[methodName] = function.Function(method)

    def addMethods(self, classObject) :
        for classToken in _getClassTokens(classObject) :
            token = getattr(classObject, classToken)
            if type(token) == types.MethodType :
                self.addMethod(token.im_func, classObject.__name__, classToken)
            else :
                self.members[classToken] = type(token)

        # add standard methods
        for methodName in [ '__class__', ] :
            self.addMethod(methodName, classObject.__name__)

    def addMembers(self, classObject) :
        if not _cfg.onlyCheckInitForMembers :
            for classToken in _getClassTokens(classObject) :
                method = getattr(classObject, classToken)
                if type(method) == types.MethodType :
                    self.addMembersFromMethod(method.im_func)
        elif hasattr(classObject, "__init__") :
            self.addMembersFromMethod(classObject.__init__.im_func)

    def addMembersFromMethod(self, method) :
        if not hasattr(method, 'func_code') :
            return

        func_code, code, i, maxCode, extended_arg = OP.initFuncCode(method)
        stack = []
        while i < maxCode :
            op, oparg, i, extended_arg = OP.getInfo(code, i, extended_arg)
            if op >= OP.HAVE_ARGUMENT :
                operand = OP.getOperand(op, func_code, oparg)
                if OP.LOAD_CONST(op) or OP.LOAD_FAST(op) :
                    stack.append(operand)
                elif OP.STORE_ATTR(op) :
                    if len(stack) > 0 :
                        if stack[-1] == _cfg.methodArgName :
                            value = None
                            if len(stack) > 1 :
                                value = type(stack[-2])
                            self.members[operand] = value
                        stack = []

def importError(moduleName, info):
    # detail may contain a newline replace with -
    # use str to avoid undestanding the tuple structure in the exception
    info = string.join(string.split(str(info), '\n' ), ' - ')
    sys.stderr.write("  Problem importing module %s - %s\n" % (moduleName, info))

class Module :
    "Class to hold all information for a module"

    def __init__(self, moduleName, check = 1) :
        self.moduleName = moduleName
        self.variables = {}
        self.functions = {}
        self.classes = {}
        self.modules = {}
        self.moduleLineNums = {}
        self.attributes = [ '__dict__' ]
        self.main_code = None
        self.module = None
        self.check = check
        global _allModules
        _allModules[moduleName] = self

    def addVariable(self, var, varType) :
        self.variables[var] = Variable(var, varType)

    def addFunction(self, func) :
        self.functions[func.__name__] = function.Function(func)

    def __addAttributes(self, c, classObject) :
        for base in classObject.__bases__ :
            self.__addAttributes(c, base)
        c.addMethods(classObject)
        c.addMembers(classObject)

    def addClass(self, name) :
        self.classes[name] = c = Class(name, self.module)
        packages = string.split(str(c.classObject), '.')
        c.ignoreAttrs = packages[0] in _cfg.blacklist
        if not c.ignoreAttrs :
            self.__addAttributes(c, c.classObject)

    def addModule(self, name) :
        module = _allModules.get(name, None)
        if module is None :
            self.modules[name] = module = Module(name, 0)
            if imp.is_builtin(name) == 0 :
                module.load()
            else :
                globalModule = globals().get(name)
                if globalModule :
                    module.attributes.extend(dir(globalModule))
        else :
            self.modules[name] = module

    def filename(self) :
        if not self.module :
            return self.moduleName
        filename = self.module.__file__
        if string.lower(filename[-4:]) == '.pyc' :
            filename = filename[:-4] + '.py'
        return filename

    def load(self) :
        try :
            # there's no need to reload modules we already have
            if sys.modules.has_key(self.moduleName) :
                if not _allModules[self.moduleName].module :
                    return self.initModule(sys.modules[self.moduleName])
                return 1

	    file, filename, smt = _findModule(self.moduleName)
            # FIXME: if the smt[-1] == imp.PKG_DIRECTORY : load __all__
            try :
                module = imp.load_module(self.moduleName, file, filename, smt)
                self.setupMainCode(file, filename, module)
            finally :
                if file != None :
                    file.close()
            return self.initModule(module)
        except (SystemExit, KeyboardInterrupt) :
            exc_type, exc_value, exc_tb = sys.exc_info()
            raise exc_type, exc_value
        except :
            return importError(self.moduleName, sys.exc_info()[1])

    def initModule(self, module) :
        self.module = module
        self.attributes = dir(self.module)
        for tokenName in _filterDir(self.module, _DEFAULT_MODULE_TOKENS) :
            token = getattr(self.module, tokenName)
            tokenType = type(token)
            if tokenType == types.ModuleType :
                # get the real module name, tokenName could be an alias
                moduleName = getattr(self.module, tokenName).__name__
                self.addModule(moduleName)
            elif tokenType == types.FunctionType :
                self.addFunction(token)
            elif tokenType == types.ClassType :
                self.addClass(tokenName)
            else :
                self.addVariable(tokenName, tokenType)

        return 1

    def setupMainCode(self, file, filename, module) :
        if file :
            self.main_code = function.create_from_file(file, filename, module)

def getAllModules() :
    "Returns a list of all modules that should be checked."
    modules = []
    for module in _allModules.values() :
        if module.check :
            modules.append(module)
    return modules

_BUILTIN_MODULE_ATTRS = { 'sys': [ 'ps1', 'ps2', 'tracebacklimit',
                                   'exc_type', 'exc_value', 'exc_traceback',
                                   'last_type', 'last_value', 'last_traceback',
                                 ],
                        }

def fixupBuiltinModules() :
    for moduleName in sys.builtin_module_names :
        module = _allModules.get(moduleName, None)
        if module is not None :
            try :
                m = imp.init_builtin(moduleName)
            except ImportError :
                pass
            else :
                extra_attrs = _BUILTIN_MODULE_ATTRS.get(moduleName, [])
                module.attributes = [ '__dict__' ] + dir(m) + extra_attrs

def _printWarnings(warnings) :
    warnings.sort()
    lastWarning = None
    for warning in warnings :
        if lastWarning != None :
            # ignore duplicate warnings
            if cmp(lastWarning, warning) == 0 :
                continue
            # print blank line between files
            if lastWarning.file != warning.file :
                print ""

        lastWarning = warning
        warning.output()

def processFiles(files, cfg = None, pre_process_cb = None) :
    # insert this here, so we find files in the local dir before std library
    if sys.path[0] != '' :
        sys.path.insert(0, '')

    # ensure we have a config object, it's necessary
    global _cfg
    if cfg is not None :
        _cfg = cfg
    elif _cfg is None :
        _cfg = Config.Config()

    warnings = []
    for moduleName in getModules(files) :
        if callable(pre_process_cb) :
            pre_process_cb(moduleName)
        module = Module(moduleName)
        if not module.load() :
            w = warn.Warning(module.filename(), 1, "NOT PROCESSED UNABLE TO IMPORT")
            warnings.append(w)
    return warnings

def getWarnings(files, cfg = None, suppressions = {}):
    warnings = processFiles(files, cfg)
    fixupBuiltinModules()
    return warnings + warn.find(getAllModules(), _cfg, suppressions)

def _print_processing(name) :
    if not _cfg.quiet :
        sys.stderr.write("Processing %s...\n" % name)

def main(argv) :
    global _cfg
    _cfg, files, suppressions = Config.setupFromArgs(argv[1:])
    if not files :
        return 0

    # insert this here, so we find files in the local dir before std library
    sys.path.insert(0, '')

    importWarnings = processFiles(files, _cfg, _print_processing)
    fixupBuiltinModules()
    if _cfg.printParse :
        for module in getAllModules() :
            printer.module(module)

    warnings = warn.find(getAllModules(), _cfg, suppressions)
    if not _cfg.quiet :
        print "\nWarnings...\n"
    if warnings or importWarnings :
        _printWarnings(importWarnings + warnings)
        return 1

    if not _cfg.quiet :
        print "None"
    return 0

if __name__ == '__main__' :
    try :
        sys.exit(main(sys.argv))
    except Config.UsageError :
        sys.exit(127)
<commit_msg>use Warning from Warning, not warn.Warning<commit_after>#!/usr/bin/env python

# Copyright (c) 2001, MetaSlash Inc.  All rights reserved.

"""
Check python source code files for possible errors and print warnings

Contact Info:
  http://pychecker.sourceforge.net/
  pychecker@metaslash.com
"""

import string
import types
import sys
import imp
import os
import glob

def setupNamespace(path) :
    # remove pychecker if it's the first component, it needs to be last
    if sys.path[0][-9:] == 'pychecker' :
        del sys.path[0]

    # make sure pychecker is last in path, so we can import
    checker_path = os.path.dirname(os.path.dirname(path))
    if checker_path not in sys.path :
        sys.path.append(checker_path)

if __name__ == '__main__' :
    setupNamespace(sys.argv[0])

from pychecker import printer
from pychecker import warn
from pychecker import OP
from pychecker import Config
from pychecker import function
from pychecker.Warning import Warning

# Globals for storing a dictionary of info about modules and classes
_allModules = {}
_cfg = None

# Constants
_DEFAULT_MODULE_TOKENS = [ '__builtins__', '__doc__', '__file__', '__name__',
                           '__path__', ]
_DEFAULT_CLASS_TOKENS = [ '__doc__', '__name__', '__module__', ]

def _flattenList(list) :
    "Returns a list which contains no lists"

    new_list = []
    for element in list :
        if type(element) == types.ListType :
            new_list.extend(_flattenList(element))
        else :
            new_list.append(element)

    return new_list

def getModules(arg_list) :
    "Returns a list of module names that can be imported"

    new_arguments = []
    for arg in arg_list :
        # is this a wildcard filespec? (necessary for windows)
        if '*' in arg or '?' in arg or '[' in arg :
            arg = glob.glob(arg)
        new_arguments.append(arg)

    PY_SUFFIX = '.py'
    PY_SUFFIX_LEN = len(PY_SUFFIX)

    modules = []
    for arg in _flattenList(new_arguments) :
        # is it a .py file?
        if len(arg) > PY_SUFFIX_LEN and arg[-PY_SUFFIX_LEN:] == PY_SUFFIX:
            arg_dir = os.path.dirname(arg)
            if arg_dir and not os.path.exists(arg) :
                print 'File or pathname element does not exist: "%s"' % arg
                continue

            module_name = os.path.basename(arg)[:-PY_SUFFIX_LEN]
            if arg_dir not in sys.path :
                sys.path.insert(1, arg_dir)
	    arg = module_name
        modules.append(arg)

    return modules

def _findModule(name, path = sys.path) :
    """Returns the result of an imp.find_module(), ie, (file, filename, smt)
       name can be a module or a package name.  It is *not* a filename."""

    packages = string.split(name, '.')
    for p in packages :
        # smt = (suffix, mode, type)
        file, filename, smt = imp.find_module(p, path)
        if smt[-1] == imp.PKG_DIRECTORY :
            try :
                # package found - read path info from init file
                m = imp.load_module(p, file, filename, smt)
            finally :
                if file is not None :
                    file.close()

            # importing xml plays a trick, which replaces itself with _xmlplus
            # both have subdirs w/same name, but different modules in them
            # we need to choose the real (replaced) version
            if m.__name__ != p :
                try :
                    file, filename, smt = imp.find_module(m.__name__, path)
                    m = imp.load_module(p, file, filename, smt)
                finally :
                    if file is not None :
                        file.close()

	    new_path = m.__path__
	    if type(new_path) == types.ListType :
	        new_path = filename
            if new_path not in path :
                path.insert(1, new_path)
        else:
            if p is not packages[-1] :
                if file is not None :
                    file.close()
                raise ImportError, "No module named %s" % packages[-1]
            return file, filename, smt

    # in case we have been given a package to check
    return file, filename, smt

class Variable :
    "Class to hold all information about a variable"

    def __init__(self, name, type):
        self.name = name
        self.type = type
        self.value = None

def _filterDir(object, ignoreList) :
    "Return a list of tokens (attributes) in a class, except for ignoreList"

    tokens = dir(object)
    for token in ignoreList :
        if token in tokens :
            tokens.remove(token)
    return tokens

def _getClassTokens(c) :
    return _filterDir(c, _DEFAULT_CLASS_TOKENS)

class Class :
    "Class to hold all information about a class"

    def __init__(self, name, module) :
        self.name = name
        self.module = module
        self.classObject = getattr(module, name)
        self.ignoreAttrs = 0
        self.methods = {}
        self.members = { '__class__': types.ClassType,
                         '__doc__': types.StringType,
                         '__dict__': types.DictType, }

    def getFirstLine(self) :
        "Return first line we can find in THIS class, not any base classes"

        lineNums = []
        classDir = dir(self.classObject)
        for m in self.methods.values() :
            if m != None and m.function.func_code.co_name in classDir:
                lineNums.append(m.function.func_code.co_firstlineno)
        if lineNums :
            return min(lineNums)
        return 0

    def allBaseClasses(self, c = None) :
        "Return a list of all base classes for this class and it's subclasses"

        baseClasses = []
        if c == None :
            c = self.classObject
        for base in c.__bases__ :
            baseClasses = baseClasses + [ base ] + self.allBaseClasses(base)
        return baseClasses

    def __getMethodName(self, func_name, className = None) :
        if func_name[0:2] == '__' and func_name[-2:] != '__' :
            if className == None :
                className = self.name
            if className[0] != '_' :
                className = '_' + className
            func_name = className + func_name
        return func_name

    def addMethod(self, method, className = None, methodName = None) :
        if type(method) == types.StringType :
            self.methods[method] = None
            return
        if not hasattr(method, "func_name") :
            return

        if not methodName :
            methodName = self.__getMethodName(method.func_name, className)
        self.methods[methodName] = function.Function(method)

    def addMethods(self, classObject) :
        for classToken in _getClassTokens(classObject) :
            token = getattr(classObject, classToken)
            if type(token) == types.MethodType :
                self.addMethod(token.im_func, classObject.__name__, classToken)
            else :
                self.members[classToken] = type(token)

        # add standard methods
        for methodName in [ '__class__', ] :
            self.addMethod(methodName, classObject.__name__)

    def addMembers(self, classObject) :
        if not _cfg.onlyCheckInitForMembers :
            for classToken in _getClassTokens(classObject) :
                method = getattr(classObject, classToken)
                if type(method) == types.MethodType :
                    self.addMembersFromMethod(method.im_func)
        elif hasattr(classObject, "__init__") :
            self.addMembersFromMethod(classObject.__init__.im_func)

    def addMembersFromMethod(self, method) :
        if not hasattr(method, 'func_code') :
            return

        func_code, code, i, maxCode, extended_arg = OP.initFuncCode(method)
        stack = []
        while i < maxCode :
            op, oparg, i, extended_arg = OP.getInfo(code, i, extended_arg)
            if op >= OP.HAVE_ARGUMENT :
                operand = OP.getOperand(op, func_code, oparg)
                if OP.LOAD_CONST(op) or OP.LOAD_FAST(op) :
                    stack.append(operand)
                elif OP.STORE_ATTR(op) :
                    if len(stack) > 0 :
                        if stack[-1] == _cfg.methodArgName :
                            value = None
                            if len(stack) > 1 :
                                value = type(stack[-2])
                            self.members[operand] = value
                        stack = []

def importError(moduleName, info):
    # detail may contain a newline replace with -
    # use str to avoid undestanding the tuple structure in the exception
    info = string.join(string.split(str(info), '\n' ), ' - ')
    sys.stderr.write("  Problem importing module %s - %s\n" % (moduleName, info))

class Module :
    "Class to hold all information for a module"

    def __init__(self, moduleName, check = 1) :
        self.moduleName = moduleName
        self.variables = {}
        self.functions = {}
        self.classes = {}
        self.modules = {}
        self.moduleLineNums = {}
        self.attributes = [ '__dict__' ]
        self.main_code = None
        self.module = None
        self.check = check
        global _allModules
        _allModules[moduleName] = self

    def addVariable(self, var, varType) :
        self.variables[var] = Variable(var, varType)

    def addFunction(self, func) :
        self.functions[func.__name__] = function.Function(func)

    def __addAttributes(self, c, classObject) :
        for base in classObject.__bases__ :
            self.__addAttributes(c, base)
        c.addMethods(classObject)
        c.addMembers(classObject)

    def addClass(self, name) :
        self.classes[name] = c = Class(name, self.module)
        packages = string.split(str(c.classObject), '.')
        c.ignoreAttrs = packages[0] in _cfg.blacklist
        if not c.ignoreAttrs :
            self.__addAttributes(c, c.classObject)

    def addModule(self, name) :
        module = _allModules.get(name, None)
        if module is None :
            self.modules[name] = module = Module(name, 0)
            if imp.is_builtin(name) == 0 :
                module.load()
            else :
                globalModule = globals().get(name)
                if globalModule :
                    module.attributes.extend(dir(globalModule))
        else :
            self.modules[name] = module

    def filename(self) :
        if not self.module :
            return self.moduleName
        filename = self.module.__file__
        if string.lower(filename[-4:]) == '.pyc' :
            filename = filename[:-4] + '.py'
        return filename

    def load(self) :
        try :
            # there's no need to reload modules we already have
            if sys.modules.has_key(self.moduleName) :
                if not _allModules[self.moduleName].module :
                    return self.initModule(sys.modules[self.moduleName])
                return 1

	    file, filename, smt = _findModule(self.moduleName)
            # FIXME: if the smt[-1] == imp.PKG_DIRECTORY : load __all__
            try :
                module = imp.load_module(self.moduleName, file, filename, smt)
                self.setupMainCode(file, filename, module)
            finally :
                if file != None :
                    file.close()
            return self.initModule(module)
        except (SystemExit, KeyboardInterrupt) :
            exc_type, exc_value, exc_tb = sys.exc_info()
            raise exc_type, exc_value
        except :
            return importError(self.moduleName, sys.exc_info()[1])

    def initModule(self, module) :
        self.module = module
        self.attributes = dir(self.module)
        for tokenName in _filterDir(self.module, _DEFAULT_MODULE_TOKENS) :
            token = getattr(self.module, tokenName)
            tokenType = type(token)
            if tokenType == types.ModuleType :
                # get the real module name, tokenName could be an alias
                moduleName = getattr(self.module, tokenName).__name__
                self.addModule(moduleName)
            elif tokenType == types.FunctionType :
                self.addFunction(token)
            elif tokenType == types.ClassType :
                self.addClass(tokenName)
            else :
                self.addVariable(tokenName, tokenType)

        return 1

    def setupMainCode(self, file, filename, module) :
        if file :
            self.main_code = function.create_from_file(file, filename, module)

def getAllModules() :
    "Returns a list of all modules that should be checked."
    modules = []
    for module in _allModules.values() :
        if module.check :
            modules.append(module)
    return modules

_BUILTIN_MODULE_ATTRS = { 'sys': [ 'ps1', 'ps2', 'tracebacklimit',
                                   'exc_type', 'exc_value', 'exc_traceback',
                                   'last_type', 'last_value', 'last_traceback',
                                 ],
                        }

def fixupBuiltinModules() :
    for moduleName in sys.builtin_module_names :
        module = _allModules.get(moduleName, None)
        if module is not None :
            try :
                m = imp.init_builtin(moduleName)
            except ImportError :
                pass
            else :
                extra_attrs = _BUILTIN_MODULE_ATTRS.get(moduleName, [])
                module.attributes = [ '__dict__' ] + dir(m) + extra_attrs

def _printWarnings(warnings) :
    warnings.sort()
    lastWarning = None
    for warning in warnings :
        if lastWarning != None :
            # ignore duplicate warnings
            if cmp(lastWarning, warning) == 0 :
                continue
            # print blank line between files
            if lastWarning.file != warning.file :
                print ""

        lastWarning = warning
        warning.output()

def processFiles(files, cfg = None, pre_process_cb = None) :
    # insert this here, so we find files in the local dir before std library
    if sys.path[0] != '' :
        sys.path.insert(0, '')

    # ensure we have a config object, it's necessary
    global _cfg
    if cfg is not None :
        _cfg = cfg
    elif _cfg is None :
        _cfg = Config.Config()

    warnings = []
    for moduleName in getModules(files) :
        if callable(pre_process_cb) :
            pre_process_cb(moduleName)
        module = Module(moduleName)
        if not module.load() :
            w = Warning(module.filename(), 1, "NOT PROCESSED UNABLE TO IMPORT")
            warnings.append(w)
    return warnings

def getWarnings(files, cfg = None, suppressions = None):
    warnings = processFiles(files, cfg)
    fixupBuiltinModules()
    return warnings + warn.find(getAllModules(), _cfg, suppressions)

def _print_processing(name) :
    if not _cfg.quiet :
        sys.stderr.write("Processing %s...\n" % name)

def main(argv) :
    global _cfg
    _cfg, files, suppressions = Config.setupFromArgs(argv[1:])
    if not files :
        return 0

    # insert this here, so we find files in the local dir before std library
    sys.path.insert(0, '')

    importWarnings = processFiles(files, _cfg, _print_processing)
    fixupBuiltinModules()
    if _cfg.printParse :
        for module in getAllModules() :
            printer.module(module)

    warnings = warn.find(getAllModules(), _cfg, suppressions)
    if not _cfg.quiet :
        print "\nWarnings...\n"
    if warnings or importWarnings :
        _printWarnings(importWarnings + warnings)
        return 1

    if not _cfg.quiet :
        print "None"
    return 0

if __name__ == '__main__' :
    try :
        sys.exit(main(sys.argv))
    except Config.UsageError :
        sys.exit(127)
<|endoftext|>


<commit_before>
import errno
import logging
import os
import socket
import sys
import time

log = logging.getLogger(__name__)

class BaseSocket(object):

    def __init__(self, addr, fd=None):
        self.address = addr
        if fd is None:
            sock = socket.socket(self.FAMILY, socket.SOCK_STREAM)
        else:
            print "%r" % fd
            sock = socket.fromfd(fd, self.FAMILY, socket.SOCK_STREAM)
        self.sock = self.set_options(sock, bound=(fd is not None))

    def __str__(self, name):
        return "<socket %d>" % self.sock.fileno()

    def __getattr__(self, name):
        return getattr(self.sock, name)

    def set_options(self, sock, bound=False):
        if not bound:
            sock.bind(self.address)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.setblocking(0)
        sock.listen(2048)
        return sock

class TCPSocket(BaseSocket):

    FAMILY = socket.AF_INET

    def __str__(self):
        return "http://%s:%d" % self.sock.getsockname()

    def set_options(self, sock, bound=False):
        if hasattr(socket, "TCP_CORK"):
            sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_CORK, 1)
        elif hasattr(socket, "TCP_NOPUSH"):
            sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NOPUSH, 1)
        sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        return super(TCPSocket, self).set_options(sock, bound=bound)

class UnixSocket(BaseSocket):

    FAMILY = socket.AF_UNIX

    def __init__(self, addr, fd=None):
        if fd is None:
            try:
                os.remove(addr)
            except OSError:
                pass
        super(UnixSocket, self).__init__(addr, fd=fd)

    def __str__(self):
        return "unix://%s" % self.address

def create_socket(addr):
    """\
    Create a new socket for the given address. If the
    address is a tuple, a TCP socket is created. If it
    is a string, a Unix socket is created. Otherwise
    a TypeError is raised.
    """
    if isinstance(addr, tuple):
        sock_type = TCPSocket
    elif isinstance(addr, basestring):
        sock_type = UnixSocket
    else:
        raise TypeError("Unable to create socket from: %r" % addr)

    if 'GUNICORN_FD' in os.environ:
        fd = int(os.environ.pop('GUNICORN_FD'))
        try:
            return sock_type(addr, fd=fd)
        except socket.error, e:
            if e[0] == errno.ENOTCONN:
                log.error("GUNICORN_FD should refer to an open socket.")
            else:
                raise

    # If we fail to create a socket from GUNICORN_FD
    # we fall through and try and open the socket
    # normally.

    for i in range(5):
        try:
            return sock_type(addr)
        except socket.error, e:
            if e[0] == errno.EADDRINUSE:
                log.error("Connection in use: %s" % str(addr))
            if e[0] == errno.EADDRNOTAVAIL:
                log.error("Invalid address: %s" % str(addr))
                sys.exit(1)
            if i < 5:
                log.error("Retrying in 1 second.")
                time.sleep(1)

    log.error("Can't connect to %s" % str(addr))
    sys.exit(1)
<commit_msg>license header<commit_after># -*- coding: utf-8 -
#
# This file is part of gunicorn released under the MIT license.
# See the NOTICE for more information.

import errno
import logging
import os
import socket
import sys
import time

log = logging.getLogger(__name__)

class BaseSocket(object):

    def __init__(self, addr, fd=None):
        self.address = addr
        if fd is None:
            sock = socket.socket(self.FAMILY, socket.SOCK_STREAM)
        else:
            print "%r" % fd
            sock = socket.fromfd(fd, self.FAMILY, socket.SOCK_STREAM)
        self.sock = self.set_options(sock, bound=(fd is not None))

    def __str__(self, name):
        return "<socket %d>" % self.sock.fileno()

    def __getattr__(self, name):
        return getattr(self.sock, name)

    def set_options(self, sock, bound=False):
        if not bound:
            sock.bind(self.address)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.setblocking(0)
        sock.listen(2048)
        return sock

class TCPSocket(BaseSocket):

    FAMILY = socket.AF_INET

    def __str__(self):
        return "http://%s:%d" % self.sock.getsockname()

    def set_options(self, sock, bound=False):
        if hasattr(socket, "TCP_CORK"):
            sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_CORK, 1)
        elif hasattr(socket, "TCP_NOPUSH"):
            sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NOPUSH, 1)
        sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        return super(TCPSocket, self).set_options(sock, bound=bound)

class UnixSocket(BaseSocket):

    FAMILY = socket.AF_UNIX

    def __init__(self, addr, fd=None):
        if fd is None:
            try:
                os.remove(addr)
            except OSError:
                pass
        super(UnixSocket, self).__init__(addr, fd=fd)

    def __str__(self):
        return "unix://%s" % self.address

def create_socket(addr):
    """\
    Create a new socket for the given address. If the
    address is a tuple, a TCP socket is created. If it
    is a string, a Unix socket is created. Otherwise
    a TypeError is raised.
    """
    if isinstance(addr, tuple):
        sock_type = TCPSocket
    elif isinstance(addr, basestring):
        sock_type = UnixSocket
    else:
        raise TypeError("Unable to create socket from: %r" % addr)

    if 'GUNICORN_FD' in os.environ:
        fd = int(os.environ.pop('GUNICORN_FD'))
        try:
            return sock_type(addr, fd=fd)
        except socket.error, e:
            if e[0] == errno.ENOTCONN:
                log.error("GUNICORN_FD should refer to an open socket.")
            else:
                raise

    # If we fail to create a socket from GUNICORN_FD
    # we fall through and try and open the socket
    # normally.

    for i in range(5):
        try:
            return sock_type(addr)
        except socket.error, e:
            if e[0] == errno.EADDRINUSE:
                log.error("Connection in use: %s" % str(addr))
            if e[0] == errno.EADDRNOTAVAIL:
                log.error("Invalid address: %s" % str(addr))
                sys.exit(1)
            if i < 5:
                log.error("Retrying in 1 second.")
                time.sleep(1)

    log.error("Can't connect to %s" % str(addr))
    sys.exit(1)
<|endoftext|>


<commit_before>from multiprocessing import Pool
from time import sleep
from random import randint
import os

class AsyncFactory:
    def __init__(self, func, cb_func):
        self.func = func
        self.cb_func = cb_func
        self.pool = Pool(maxtasksperchild=2)

    def call(self,*args, **kwargs):
        self.pool.apply_async(self.func, args, kwargs, self.cb_func)

    def wait(self):
        self.pool.close()
        self.pool.join()

def square(pos, x):
    sleep_duration = randint(1,5)
    print "PID: %d \t Value: %d \t Sleep: %d" % (os.getpid(), x ,sleep_duration)
    sleep(sleep_duration)
    return pos, x*x

def cb_func((pos, x)):
    print pos, x
    results.append(x)

results = []

async_square = AsyncFactory(square, cb_func)

for i in range(25):
    async_square.call(i,i)

async_square.wait()
<commit_msg>tweaks to undestand MP.<commit_after>from multiprocessing import Pool
from time import sleep
from random import randint
import os

class AsyncFactory:
    def __init__(self, func, cb_func):
        self.func = func
        self.cb_func = cb_func
        self.pool = Pool(maxtasksperchild=2)

    def call(self,*args, **kwargs):
        return self.pool.apply_async(self.func, args, kwargs, self.cb_func)

    def wait(self):
        self.pool.close()
        self.pool.join()

def square(pos, x):
    sleep_duration = randint(1,5)
    print "PID: %d \t Value: %d \t Sleep: %d" % (os.getpid(), x ,sleep_duration)
    sleep(sleep_duration)
    return pos, x*x

def cb_func((pos, x)):
    print pos, x
    results.append(x)

results = []
x = []
async_square = AsyncFactory(square, cb_func)

for i in range(25):
    x.append(async_square.call(i,i))

async_square.wait()
<|endoftext|>