# Path: p2/scripts3/mcpi/util.py
# def flatten_parameters_to_string(l):
#     return ",".join(map(str, flatten(l)))


import socket
import select
import sys
import atexit
import os
import platform
import base64
from hashlib import md5
from .util import flatten_parameters_to_string

            if sys.version_info[0] >= 3:
                auth = md5((salt+":"+username+":"+password).encode("utf-8")).hexdigest()
            else:
                auth = md5(salt+":"+username+":"+password).hexdigest()
            self.send("security.authenticate", auth)

    def drain(self):
        """Drains the socket of incoming data"""
        while True:
            readable, _, _ = select.select([self.socket], [], [], 0.0)
            if not readable:
                break
            data = self.socket.recv(1500)
            if not data:
                self.socket.close()
                raise ValueError('Socket got closed')
            e =  "Drained Data: <%s>\n"%data.strip()
            e += "Last Message: <%s>\n"%self.lastSent.strip()
            sys.stderr.write(e)
                                             
    def send(self, f, *data):
        """Sends data. Note that a trailing newline '\n' is added here"""
        s = "%s(%s)\n"%(f, flatten_parameters_to_string(data))
        #print "s:"+s+":"
        self.drain()
        self.lastSent = s
        self.socket.sendall(s)

    def send_python3(self, f, *data):
        """Sends data. Note that a trailing newline '\n' is added here"""

# Path: p2/scripts3/mcpi/util.py
# def flatten_parameters_to_string(l):
#     return ",".join(map(str, flatten(l)))


# Path: p2/scripts3/mcpi/connection.py
import socket
import select
import sys
import atexit
import os
import platform
import base64
from hashlib import md5
from .util import flatten_parameters_to_string

            if sys.version_info[0] >= 3:
                auth = md5((salt+":"+username+":"+password).encode("utf-8")).hexdigest()
            else:
                auth = md5(salt+":"+username+":"+password).hexdigest()
            self.send("security.authenticate", auth)

    def drain(self):
        """Drains the socket of incoming data"""
        while True:
            readable, _, _ = select.select([self.socket], [], [], 0.0)
            if not readable:
                break
            data = self.socket.recv(1500)
            if not data:
                self.socket.close()
                raise ValueError('Socket got closed')
            e =  "Drained Data: <%s>\n"%data.strip()
            e += "Last Message: <%s>\n"%self.lastSent.strip()
            sys.stderr.write(e)
                                             
    def send(self, f, *data):
        """Sends data. Note that a trailing newline '\n' is added here"""
        s = "%s(%s)\n"%(f, flatten_parameters_to_string(data))
        #print "s:"+s+":"
        self.drain()
        self.lastSent = s
        self.socket.sendall(s)

    def send_python3(self, f, *data):
        """Sends data. Note that a trailing newline '\n' is added here"""

        s = "%s(%s)\n"%(f, flatten_parameters_to_string(data))
