Example #1
0
    def parse_models(self):
        '''
        Parse the c++ reference implementation
        of the custom instruction.
        '''

        logger.info('Determine if modelpath is a folder or a single file')
        if os.path.isdir(self._modelpath):
            # restore the toolchain to its defaults
            self.restore()
            logger.info('Traverse over directory')
            self.treewalk(self._modelpath)
        else:
            logger.info('Single file, start parsing')
            model = Model(self._modelpath)
            self._models.append(model)

        # add model for read function
        self._models.append(Model(read=True))
        # add model for write function
        self._models.append(Model(write=True))

        self._exts = Extensions(self._models)
        self._compiler = Compiler(self._exts, self._regs, self._tcpath)
        self._gem5 = Gem5(self._exts, self._regs)
Example #2
0
    def __init__(self, socket, tls_wrapper=None):
        self.io = IO(socket, tls_wrapper)
        self.reply_queue = []

        #: :class:`Extensions` object of the client, populated once the EHLO
        #: command returns its response.
        self.extensions = Extensions()
    def generate_response(self, request):
        """
        Makes response for request.
        Two main actions is possible:
        - response : returns saved response
        - forward : makes request to 3rd resource

        :param request: Any request into mock
        :return: custom response with result
        """
        self.log_container.add({'request': request})
        if self.logs_url is None:
            self._logger.info("Log id %s for request %s %s headers: %s" % (
                self.log_container.get_latest_id(),
                request['method'],
                request['path'],
                request['headers']))
        else:
            self._logger.info("Log %s/%s for request %s %s headers: %s" % (
                self.logs_url,
                self.log_container.get_latest_id(),
                request['method'],
                request['path'],
                request['headers']))

        if len(self.host_whitelist) > 0:
            request_headers = request['headers'] if 'headers' in request else []
            request_host = request_headers['Host'] if len(request_headers) > 0 and 'Host' in request['headers'] else ""
            has_wl_match = False
            for wl_host in self.host_whitelist:
                has_wl_match = has_wl_match or ExpectationMatcher.value_matcher(wl_host, request_host)

            if not has_wl_match:
                self._logger.warning("Request's host '%s' not in a white list!" % request_host)
                response = CustomResponse(status_code=codes.not_allowed)
                self.log_container.update_last_with_kv('response', response)
                return response

        list_matched_expectations = self._expectation_manager.get_matched_expectations_for_request(request)

        if len(list_matched_expectations) > 0:
            expectation = Extensions.order_by_priority(list_matched_expectations)[0]
            self._logger.debug("Matched expectation: %s" % expectation)
            response = self.apply_action_from_expectation_to_request(expectation, request)
        else:
            self._logger.warning("List of expectations is empty!")
            response = CustomResponse("No expectation for request: " + str(request))
        self.log_container.update_last_with_kv('response', response.to_dict())
        self._logger.debug("Response: %s" % response)
        return response
Example #4
0
	def accumulateOnColumns(self, function, columns):
		
		length = len(self.featureNames)
		accumulator = self.sc.accumulator([0] * length, Extensions.ListAccumulatorParam())

		def f(row):
			increment = [0] * length
			for col in columns:
				increment[col] = function(row[col])
			accumulator.add(increment)
			
		self.rdd.foreach(f)
		
		return accumulator.value
Example #5
0
	def stochasticGradientDescent(self, data):
		integerShape = self.weights.integers.shape
		categoricalShape = self.weights.categoricals.shape
		
		accumIntegerWeights = data.sc.accumulator(numpy.zeros(integerShape), Extensions.ArrayAccumulatorParam())
		accumCategoricalWeights = data.sc.accumulator(numpy.zeros(categoricalShape), Extensions.ArrayAccumulatorParam())

		def accumulateWeight(row):
			integerDelta = numpy.ones(integerShape)
			categoricalDelta = numpy.ones(categoricalShape)
			
			#for array in [zeroIntegers, zeroCategoricals]:
			#	for element in numpy.nditer(array, op_flags=['readwrite']):
			#		element[...] = 1
			
			accumIntegerWeights.add(integerDelta)
			accumCategoricalWeights.add(categoricalDelta)
		
		data.rdd.foreach(accumulateWeight)
		
		self.weights.integer = accumIntegerWeights.value
		self.weights.integer = accumCategoricalWeights.value
		
		return 
Example #6
0
class Client(object):
    """Class whose methods perform various SMTP commands on a given socket. The
    return value from each command is a |Reply| object. Commands that are
    pipelined may not have their replies filled until subsequent commands are
    executed.

    The ``extensions`` attribute contains the |Extensions| object that are made
    available by the server.

    :param socket: Connected socket to use for the client.
    :param tls_wrapper: Optional function that takes a socket and the ``tls``
                        dictionary, creates a new encrypted socket, performs
                        the TLS handshake, and returns it. The default uses
                        :class:`~gevent.ssl.SSLSocket`.

    """

    def __init__(self, socket, tls_wrapper=None):
        self.io = IO(socket, tls_wrapper)
        self.reply_queue = []

        #: :class:`Extensions` object of the client, populated once the EHLO
        #: command returns its response.
        self.extensions = Extensions()

    def _flush_pipeline(self):
        self.io.flush_send()
        while True:
            try:
                reply = self.reply_queue.pop(0)
            except IndexError:
                return None
            reply.recv(self.io)

    def has_reply_waiting(self):
        """Checks if the underlying socket has data waiting to be received,
        which means a reply is waiting to be read.

        :rtype: True or False

        """
        sock_fd = self.io.socket.fileno()
        try:
            wait_read(sock_fd, 0.1, Timeout())
        except Timeout:
            return False
        else:
            return True

    def get_reply(self, command='[TIMEOUT]'):
        """Gets a reply from the server that was not triggered by the client
        sending a command. This is most useful for receiving timeout
        notifications.

        :param command: Optional command name to associate with the reply.
        :returns: |Reply| object populated with the response.

        """
        reply = Reply(command=command)
        self.reply_queue.append(reply)

        self._flush_pipeline()

        return reply

    def get_banner(self):
        """Waits for the SMTP banner at the beginning of the connection.

        :returns: |Reply| object populated with the response.

        """
        banner = Reply(command='[BANNER]')
        banner.enhanced_status_code = False
        self.reply_queue.append(banner)

        self._flush_pipeline()

        return banner

    def custom_command(self, command, arg=None):
        """Sends a custom command to the SMTP server and waits for the reply.

        :param command: The command to send.
        :param arg: Optonal argument string to send with the command.
        :returns: |Reply| object populated with the response.

        """
        custom = Reply(command=command.upper())
        self.reply_queue.append(custom)

        if arg:
            command = ' '.join((command, arg))
        self.io.send_command(command)

        self._flush_pipeline()

        return custom

    def ehlo(self, ehlo_as):
        """Sends the EHLO command with identifier string and waits for the
        reply. When this method returns, the ``self.extensions`` object will
        also be populated with the SMTP extensions the server supports.

        :param ehlo_as: EHLO identifier string, usually an FQDN.
        :returns: |Reply| object populated with the response.

        """
        ehlo = Reply(command='EHLO')
        ehlo.enhanced_status_code = False
        self.reply_queue.append(ehlo)

        command = 'EHLO '+ehlo_as
        self.io.send_command(command)

        self._flush_pipeline()
        if ehlo.code == '250':
            self.extensions.reset()
            ehlo.message = self.extensions.parse_string(ehlo.message)

        return ehlo

    def helo(self, helo_as):
        """Sends the HELO command with identifier string and waits for the
        reply.

        :param helo_as: HELO identifier string, usually an FQDN.
        :returns: |Reply| object populated with the response.

        """
        helo = Reply(command='HELO')
        helo.enhanced_status_code = False
        self.reply_queue.append(helo)

        command = 'HELO '+helo_as
        self.io.send_command(command)

        self._flush_pipeline()

        return helo

    def encrypt(self, tls):
        """Encrypts the underlying socket with the information given by ``tls``.
        This call should only be used directly against servers that expect to be
        immediately encrypted. If encryption is negotiated with
        :meth:`starttls()` there is no need to call this method.

        :param tls: Dictionary of keyword arguments for
                    :class:`~gevent.ssl.SSLSocket`.

        """
        self.io.encrypt_socket(tls)

    def starttls(self, tls):
        """Sends the STARTTLS command with identifier string and waits for the
        reply. When the reply is received and the code is 220, the socket is
        encrypted with the parameters in ``tls``. This should be followed by a
        another call to :meth:`ehlo()`.

        :param tls: Dictionary of keyword arguments for
                    :class:`~gevent.ssl.SSLSocket`.
        :returns: |Reply| object populated with the response.

        """
        reply = self.custom_command('STARTTLS')
        if reply.code == '220':
            self.encrypt(tls)
        return reply

    def mailfrom(self, address, data_size=None):
        """Sends the MAIL command with the ``address`` and possibly the message
        size. The message size is sent if the server supports the SIZE
        extension. If the server does *not* support PIPELINING, the returned
        reply object is populated immediately.

        :param address: The sender address to send.
        :param data_size: Optional size of the message body.
        :returns: |Reply| object that will be populated with the response
                  once a non-pipelined command is called, or if the server does
                  not support PIPELINING.

        """
        mailfrom = Reply(command='MAIL')
        self.reply_queue.append(mailfrom)

        command = 'MAIL FROM:<{0}>'.format(address)
        if data_size is not None and 'SIZE' in self.extensions:
            command += ' SIZE='+str(data_size)
        self.io.send_command(command)

        if 'PIPELINING' not in self.extensions:
            self._flush_pipeline()

        return mailfrom

    def rcptto(self, address):
        """Sends the RCPT command with the ``address``. If the server
        does *not* support PIPELINING, the returned reply object is
        populated immediately.

        :param address: The sender address to send.
        :param data_size: Optional size of the message body.
        :returns: |Reply| object that will be populated with the response
                  once a non-pipelined command is called, or if the server does
                  not support PIPELINING.

        """
        rcptto = Reply(command='RCPT')
        self.reply_queue.append(rcptto)

        command = 'RCPT TO:<{0}>'.format(address)
        self.io.send_command(command)

        if 'PIPELINING' not in self.extensions:
            self._flush_pipeline()

        return rcptto

    def data(self):
        """Sends the DATA command and waits for the response. If the response
        from the server is a 354, the server is respecting message data and
        should be sent :meth:`send_data` or :meth:`send_empty_data`.

        :returns: |Reply| object populated with the response.

        """
        return self.custom_command('DATA')

    def send_data(self, *data):
        """Processes and sends message data. At the end of the message data,
        the client will send a line with a single ``.`` to indicate the end of
        the message.  If the server does *not* support PIPELINING, the returned
        reply object is populated immediately.

        :param data: The message data parts.
        :type data: string or unicode
        :returns: |Reply| object that will be populated with the response
                  once a non-pipelined command is called, or if the server does
                  not support PIPELINING.

        """
        send_data = Reply(command='[SEND_DATA]')
        self.reply_queue.append(send_data)

        data_sender = DataSender(*data)
        data_sender.send(self.io)

        if 'PIPELINING' not in self.extensions:
            self._flush_pipeline()

        return send_data

    def send_empty_data(self):
        """Sends a line with a single ``.`` to indicate an empty message. If
        the server does *not* support PIPELINING, the returned reply object is
        populated immediately.

        :param data: The message data.
        :type data: string or unicode
        :returns: |Reply| object that will be populated with the response
                  once a non-pipelined command is called, or if the server does
                  not support PIPELINING.

        """
        send_data = Reply(command='[SEND_DATA]')
        self.reply_queue.append(send_data)

        self.io.send_command('.')

        if 'PIPELINING' not in self.extensions:
            self._flush_pipeline()

        return send_data

    def rset(self):
        """Sends a RSET command and waits for the response. The intent of the
        RSET command is to reset any :meth:`mail` or :meth:`rcpt` commands that
        are pending.

        :returns: |Reply| object populated with the response.

        """
        return self.custom_command('RSET')

    def quit(self):
        """Sends the QUIT command and waits for the response. After the response
        is received (should be 221) the socket should be closed.

        :returns: |Reply| object populated with the response.

        """
        return self.custom_command('QUIT')
Example #7
0
 def __init__(self, root="."):
     self.root = os.path.abspath(root)
     self.paths = Paths(root=self.root)
     self.extensions = Extensions()
     self.aliases = {}
Example #8
0
class Crawl:
    def __init__(self, root="."):
        self.root = os.path.abspath(root)
        self.paths = Paths(root=self.root)
        self.extensions = Extensions()
        self.aliases = {}

    def prepend_paths(self, *paths):
        new = Paths(paths)
        new.extend(self.paths)
        self.paths = new

    def prepend_path(self, *paths):
        self.prepend_paths(*paths)

    def append_paths(self, *paths):
        for path in paths:
            self.paths.append(path)

    def append_path(self, *paths):
        self.append_paths(*paths)

    def remove_path(self, path):
        if path in self.paths:
            self.paths.remove(path)

    def prepend_extensions(self, *extensions):
        new = Extensions(extensions)
        new.extend(self.extensions)
        self.extensions = new

    def prepend_extension(self, *extensions):
        self.prepend_extensions(*extensions)

    def append_extensions(self, *extensions):
        for extension in extensions:
            self.extensions.append(extension)

    def append_extension(self, *extensions):
        self.append_extensions(*extensions)

    def remove_extension(self, extension):
        if extension in self.extensions:
            self.extensions.remove(extension)

    def alias_extension(self, new_extension, old_extension):
        new_extension = self.extensions.normalize_element(new_extension)

        self.aliases[new_extension] = self.extensions.normalize_element(
            old_extension)

    def unalias_extension(self, extension):
        del self.aliases[self.extensions.normalize_element(extension)]

    def find(self, *args, **kwargs):
        return self.index().find(*args, **kwargs)

    def index(self):
        return Index(self.root, self.paths, self.extensions, self.aliases)

    def entries(self, *args):
        return self.index().entries(*args)

    def stat(self, *args):
        return self.index().stat(*args)
Example #9
0
 def prepend_extensions(self, *extensions):
     new = Extensions(extensions)
     new.extend(self.extensions)
     self.extensions = new
Example #10
0
    def plansza3(self):
        """Rysuje trzecią planszę."""

        for i in range(2):
            self.add_speed = Extensions(
                random.randrange(30, self.screen_width - 30),
                random.randrange(30, self.screen_height / 2),
                "data/add_speed.png")
            self.AddSpeedSprite.add(self.add_speed)

        for i in range(2):
            self.add_speed = Extensions(
                random.randrange(30, self.screen_width - 30),
                random.randrange(30, self.screen_height / 2),
                "data/substract_speed.png")
            self.SubstractSpeedSprite.add(self.add_speed)

        i = 10
        j = 35
        while i < self.screen_width - 10:
            block = Blocks(i, j, 'data/gray_brick.png')
            self.BlocksSprite.add(block)
            i = i + 87.5

        i = 97.5
        j = 70
        while i < self.screen_width - 97.5:
            block = Blocks(i, j, 'data/wood_brick.png')
            self.BlocksSprite.add(block)
            i = i + 87.5

        i = 185
        j = 105
        while i < self.screen_width - 185:
            block = Blocks(i, j, 'data/gray_brick.png')
            self.BlocksSprite.add(block)
            i = i + 87.5

        i = 272.5
        j = 140
        while i < self.screen_width - 272.5:
            block = Blocks(i, j, 'data/wood_brick.png')
            self.BlocksSprite.add(block)
            i = i + 87.5

        ####

        block = Blocks(360, 175, 'data/gray_brick.png')
        self.BlocksSprite.add(block)

        ####

        i = 272.5
        j = 210
        while i < self.screen_width - 272.5:
            block = Blocks(i, j, 'data/wood_brick.png')
            self.BlocksSprite.add(block)
            i = i + 175

        i = 185
        j = 245
        while i < self.screen_width - 185:
            block = Blocks(i, j, 'data/gray_brick.png')
            self.BlocksSprite.add(block)
            i = i + 175

        i = 97.5
        j = 280
        while i < self.screen_width - 97.5:
            block = Blocks(i, j, 'data/wood_brick.png')
            self.BlocksSprite.add(block)
            i = i + 175

        i = 10
        j = 315
        while i < self.screen_width - 10:
            block = Blocks(i, j, 'data/gray_brick.png')
            self.BlocksSprite.add(block)
            i = i + 175

        ####

        i = 10
        for k in range(3):
            block = Blocks(i, 175, 'data/light_wood_brick.png')
            self.BlocksSprite.add(block)
            i = i + 87.5

        j = 140
        for k in range(2):
            block = Blocks(97.5, j, 'data/light_wood_brick.png')
            self.BlocksSprite.add(block)
            j = j + 70

        i = 535
        for k in range(3):
            block = Blocks(i, 175, 'data/light_wood_brick.png')
            self.BlocksSprite.add(block)
            i = i + 87.5

        j = 140
        for k in range(2):
            block = Blocks(622.5, j, 'data/light_wood_brick.png')
            self.BlocksSprite.add(block)
            j = j + 70

        self.counter = len(self.BlocksSprite)
Example #11
0
# -*- coding: utf-8 -*-

# Copyright (C) 2013 Sylvain Boily <*****@*****.**>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from flask import Blueprint
from extensions import Extensions
import wtforms_json

bp_extensions = Blueprint('extensions',
                          __name__,
                          template_folder='templates/extensions')

extensions = Extensions()
wtforms_json.init()
 def func_wrapper(cls, message):
     kwargs = {'ts': datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3],
               'level': logging.getLevelName(level),
               'message': Extensions.remove_linebreaks(message)}
     s = cls.encoder.encode(kwargs)
     return func(cls, s)
Example #13
0
	def __init__(self,root="."):
		self.root = os.path.abspath(root)
		self.paths = Paths(root=self.root)
		self.extensions = Extensions()
		self.aliases = {}
Example #14
0
class Crawl:

	def __init__(self,root="."):
		self.root = os.path.abspath(root)
		self.paths = Paths(root=self.root)
		self.extensions = Extensions()
		self.aliases = {}

	def prepend_paths(self,*paths):
		new = Paths(paths)
		new.extend(self.paths)
		self.paths = new

	def prepend_path(self,*paths):
		self.prepend_paths(*paths)

	def append_paths(self,*paths):
		for path in paths:
			self.paths.append(path)

	def append_path(self,*paths):
		self.append_paths(*paths)

	def remove_path(self,path):
		if path in self.paths:
			self.paths.remove(path)

	def prepend_extensions(self,*extensions):
		new = Extensions(extensions)
		new.extend(self.extensions)
		self.extensions = new

	def prepend_extension(self,*extensions):
		self.prepend_extensions(*extensions)

	def append_extensions(self,*extensions):
		for extension in extensions:
			self.extensions.append(extension)

	def append_extension(self,*extensions):
		self.append_extensions(*extensions)

	def remove_extension(self,extension):
		if extension in self.extensions:
			self.extensions.remove(extension)

	def alias_extension(self,new_extension,old_extension):
		new_extension = self.extensions.normalize_element(new_extension)

		self.aliases[new_extension] = self.extensions.normalize_element(old_extension)

	def unalias_extension(self,extension):
		del self.aliases[self.extensions.normalize_element(extension)]

	def find(self,*args,**kwargs):
		return self.index().find(*args,**kwargs)

	def index(self):
		return Index(self.root,self.paths,self.extensions,self.aliases)

	def entries(self,*args):
		return self.index().entries(*args)

	def stat(self,*args):
		return self.index().stat(*args)
Example #15
0
	def prepend_extensions(self,*extensions):
		new = Extensions(extensions)
		new.extend(self.extensions)
		self.extensions = new