示例#1
0
    def enqueue(self, task, priority):
        """
        Enqueues the given :class:`~retask.task.Task` object to the queue
        and returns a :class:`~retask.queue.Job` object.

        :arg task: ::class:`~retask.task.Task` object
        :arg priority:

        :return: :class:`~retask.queue.Job` object

        If the queue is not connected then it will raise
        :class:`retask.ConnectionError`.

        """
        if not self.connected:
            raise exceptions.ConnectionError('PriorityQueue is not connected')

        try:
            job = queue.Job(self.rdb)
            task.urn = job.urn
            text = json.dumps(task.__dict__)
            self.rdb.zadd(self._name, priority, text)

            self.rdb.publish(self._wc_name, _PRIORITY_QUEUE_READY_MSG)
            return job
        except redis.ConnectionError as e:
            raise exceptions.ConnectionError(str(e))
示例#2
0
    def length(self):
        if not self.connected:
            raise exceptions.ConnectionError('PriorityQueue is not connected')

        try:
            return self.rdb.zcard(self._name)
        except redis.exceptions.ConnectionError as e:
            raise exceptions.ConnectionError(str(e))
示例#3
0
def detect_common_color():
    request_json = request.get_json()

    if "url" not in request_json:
        raise exceptions.InvalidUsage("URL key missing from JSON post data")

    try:
        r = requests.get(request_json["url"], stream=True)
    except requests.exceptions.InvalidSchema:
        raise exceptions.InvalidUsage("URL has an invalid schema")
    except requests.exceptions.ConnectionError as e:
        raise exceptions.ConnectionError("Unable to connect to URL")
    except requests.exceptions.MissingSchema:
        raise exceptions.InvalidUsage(
            "No schema supplied. Perhaps you meant http://{}?".format(
                request_json["url"]
            )
        )

    if int(r.headers["Content-length"]) > config.max_file_size:
        raise exceptions.InvalidUsage(
            "File larger than allowed maximum ({})".format(config.max_file_size)
        )

    try:
        im = Image.open(r.raw)
    except UnidentifiedImageError:
        raise exceptions.InvalidUsage("URL is not an image")

    count, rgb = max(im.getcolors(im.size[0] * im.size[1]))
    distance, color_name = find_closest(rgb)
    if distance > config.max_color_distance:
        return jsonify({"result": None})
    return jsonify({"result": color_name})
示例#4
0
 def handle_response(self, response, content):
     """Validate HTTP response
     """
     status = response.status_code
     if status in (301, 302, 303, 307):
         raise exceptions.Redirection(response, content)
     elif 200 <= status <= 299:
         return json.loads(content) if content else {}
     elif status == 400:
         raise exceptions.BadRequest(response, content)
     elif status == 401:
         raise exceptions.UnauthorizedAccess(response, content)
     elif status == 403:
         raise exceptions.ForbiddenAccess(response, content)
     elif status == 404:
         raise exceptions.ResourceNotFound(response, content)
     elif status == 405:
         raise exceptions.MethodNotAllowed(response, content)
     elif status == 409:
         raise exceptions.ResourceConflict(response, content)
     elif status == 410:
         raise exceptions.ResourceGone(response, content)
     elif status == 422:
         raise exceptions.ResourceInvalid(response, content)
     elif 401 <= status <= 499:
         raise exceptions.ClientError(response, content)
     elif 500 <= status <= 599:
         raise exceptions.ServerError(response, content)
     else:
         raise exceptions.ConnectionError(
             response, content, "Unknown response code: #{response.code}")
示例#5
0
    def wait(self, wait_time=None):
        """
        Returns a :class:`~rtask.task.Task` object from the queue.
        Returns ``False`` if it timeouts.

        :arg wait_time: Time in seconds to wait, default is infinite.

        :return: :class:`~retask.task.Task` object from the queue or
                 False if it timeouts.

        .. doctest::

           >>> from retask import Queue
           >>> q = Queue('test')
           >>> q.connect()
           True
           >>> task = q.wait()
           >>> print task.data
           {u'name': u'kushal'}

        .. note::

            This is a blocking call, you can specity wait_time argument
            for timeout.

        """
        if not self.connected:
            raise exceptions.ConnectionError('PriorityQueue is not connected')

        ps = self.rdb.pubsub(ignore_subscribe_messages=True)
        ps.subscribe(self._wc_name)

        data = self._try_pop()
        timed_out = False

        while data is None and not timed_out:
            start_time = time.time()
            msg = ps.get_message(timeout=wait_time)
            elapsed = time.time() - start_time

            if wait_time is not None:
                wait_time -= elapsed
                timed_out = (wait_time <= 0)

            if not timed_out:
                if _is_priority_queue_ready_msg(msg):
                    data = self._try_pop()
                elif msg is not None:  # raise?
                    _log.error("PriorityQueue.wait: Unexpected message\n%s",
                               str(msg))

        ps.unsubscribe(self._wc_name)
        ps.close()

        if data:
            t = task.Task()
            t.__dict__ = json.loads(data)
            return t

        return False
def get_definitions(word, examples=False):
    if not word.strip():
        raise exceptions.WordNotFoundError(word)
    url = f'https://dlc.iec.cat/results.asp?txtEntrada={word}'
    try:
        soups = get_definitions_soup(url, word)
    except requests.exceptions.ConnectionError as e:
        raise exceptions.ConnectionError() from e
    definitions = scrap_definitions(soups, word, examples)
    return definitions
示例#7
0
    def get_service_document(self, refresh=False):
        if not refresh and self._service_document is not None:
            return self._service_document

        resp = requests.get(self.sd_uri, auth=self.auth)

        if resp.status_code == 403:
            raise exceptions.UnauthorizedError(
                'The credentials provided are invalid.')
        elif resp.status_code != 200:
            raise exceptions.ConnectionError(
                'Could not connect to the Dataverse')

        self._service_document = etree.XML(resp.content)
        return self._service_document
示例#8
0
    def find(self, obj):
        """Returns the index of the given object in the queue, it might
        be string which will be searched inside each task.

        :arg obj: object we are looking

        :return: -1 if the object is not found or else the location of the task
        """
        if not self.connected:
            raise exceptions.ConnectionError('Queue is not connected')

        data = self.rdb.zrange(self._name, 0, -1)
        for i, datum in enumerate(data):
            if datum.find(str(obj)) != -1:
                return i
        return -1
示例#9
0
文件: core.py 项目: scalp42/awslogs
class AWSConnection(object):
    """Wrapper on top of boto's ``connect_to_region`` which retry api
    calls if some well-known errors occur."""
    def __init__(self, aws_region, *args, **kwargs):

        if aws_region not in (r.name for r in botologs.regions()):
            raise exceptions.InvalidRegionError(aws_region)

        try:
            self.connection = botologs.connect_to_region(
                aws_region, *args, **kwargs)
        except boto.exception.NoAuthHandlerFound, exc:
            raise exceptions.NoAuthHandlerFoundError(*exc.args)

        if not self.connection:
            raise exceptions.ConnectionError()
示例#10
0
    def perform(self, request):
        url = self.base_url + request.path

        params = copy.deepcopy(request.params)
        params['ServiceId'] = self.service_id
        request_mac = calculate_mac(
            self.secret, *[params.get(f) for f in request.mac_fields])
        params['MAC'] = request_mac

        try:
            response = requests.request(
                request.method,
                url,
                data=params,
                timeout=(self.connect_timeout, self.read_timeout)
            )
        except requests_exceptions.RequestException as exc:
            msg = "Connection with url {} refused.".format(self.base_url)
            raise exceptions.ConnectionError(msg, exc)

        return self.check_response(response, request)
示例#11
0
    def dequeue(self):
        """
        Returns a :class:`~retask.task.Task` object from the queue. Returns
        ``None`` if the queue is empty.

        :return: :class:`~retask.task.Task` object from the queue

        If the queue is not connected then it will raise
        :class:`retask.ConnectionError`
        """
        if not self.connected:
            raise exceptions.ConnectionError('PriorityQueue is not connected')

        data = self._try_pop()

        if data:
            if isinstance(data, six.binary_type):
                data = six.text_type(data, 'utf-8', errors='replace')
            t = task.Task()
            t.__dict__ = json.loads(data)
            return t

        return None
示例#12
0
文件: wifi.py 项目: phvash/bello
def get_wifi_detail():

    if 'linux' in os.sys.platform:
        command = 'ifconfig'
    elif 'Windows' in os.sys.platform:
        command = 'ipconfig'
    else:
        return "Exception"

    raw_details = subprocess.check_output([command])

    try:
        response = [
            addrs[addrs.find(':') + 1:] for addrs in [
                line[line.find('inet') + 1:]
                for line in raw_details.split('\n')
                if 'inet addr' in line and '127' not in line
            ][0].split(' ')
            if addrs[addrs.find(':') + 1:].replace('.', '').isdigit()
        ]

        return response
    except:
        raise exceptions.ConnectionError("Please connect to a network")