while True:<EOL><INDENT>rawdata = self.get_server_msg()<EOL>if not rawdata:<EOL><INDENT>continue  <EOL><DEDENT>try:<EOL><INDENT>pkt = packet.decode(rawdata, self.json_loads)<EOL><DEDENT>except (ValueError, KeyError, Exception) as e:<EOL><INDENT>self.error('<STR_LIT>',<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % (rawdata[:<NUM_LIT:20>], e))<EOL>continue<EOL><DEDENT>if pkt['<STR_LIT:type>'] == '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>if pkt['<STR_LIT:type>'] == '<STR_LIT>' and pkt['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>self.kill(detach=True)<EOL>continue<EOL><DEDENT>endpoint = pkt['<STR_LIT>']<EOL>if endpoint not in self.namespaces:<EOL><INDENT>self.error("<STR_LIT>",<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % endpoint, endpoint=endpoint)<EOL>continue<EOL><DEDENT>elif endpoint in self.active_ns:<EOL><INDENT>pkt_ns = self.active_ns[endpoint]<EOL><DEDENT>else:<EOL><INDENT>new_ns_class = self.namespaces[endpoint]<EOL>pkt_ns = new_ns_class(self.environ, endpoint,<EOL>request=self.request)<EOL>for cls in type(pkt_ns).__mro__:<EOL><INDENT>if hasattr(cls, '<STR_LIT>'):<EOL><INDENT>cls.initialize(pkt_ns)  <EOL><DEDENT><DEDENT>self.active_ns[endpoint] = pkt_ns<EOL><DEDENT>retval = pkt_ns.process_packet(pkt)<EOL>if pkt.get('<STR_LIT>') == "<STR_LIT:data>" and pkt.get('<STR_LIT:id>'):<EOL><INDENT>if type(retval) is tuple:<EOL><INDENT>args = list(retval)<EOL><DEDENT>else:<EOL><INDENT>args = [retval]<EOL><DEDENT>returning_ack = dict(type='<STR_LIT>', ackId=pkt['<STR_LIT:id>'],<EOL>args=args,<EOL>endpoint=pkt.get('<STR_LIT>', '<STR_LIT>'))<EOL>self.send_packet(returning_ack)<EOL><DEDENT>if not self.connected:<EOL><INDENT>self.kill(detach=True)  <EOL>return<EOL><DEDENT><DEDENT>

This is the loop that takes messages from the queue for the server
        to consume, decodes them and dispatches them.

        It is the main loop for a socket.  We join on this process before
        returning control to the web framework.

        This process is not tracked by the socket itself, it is not going
        to be killed by the ``gevent.killall(socket.jobs)``, so it must
        exit gracefully itself.


job = gevent.spawn(self._receiver_loop)<EOL>self.jobs.append(job)<EOL>return job<EOL>

Spawns the reader loop.  This is called internall by
        socketio_manage().


while True:<EOL><INDENT>gevent.sleep(<NUM_LIT:1.0>)<EOL>if not self.connected:<EOL><INDENT>for ns_name, ns in list(six.iteritems(self.active_ns)):<EOL><INDENT>ns.recv_disconnect()<EOL><DEDENT>gevent.killall(self.jobs)<EOL>break<EOL><DEDENT><DEDENT>

Watch out if we've been disconnected, in that case, kill
        all the jobs.


job = gevent.spawn(self._watcher)<EOL>return job<EOL>

This one is not waited for with joinall(socket.jobs), as it
        is an external watcher, to clean up when everything is done.


interval = self.config['<STR_LIT>']<EOL>while self.connected:<EOL><INDENT>gevent.sleep(interval)<EOL>self.put_client_msg("<STR_LIT>")<EOL><DEDENT>

Start the heartbeat Greenlet to check connection health.


self.spawn(self._heartbeat)<EOL>self.spawn(self._heartbeat_timeout)<EOL>


def serve_paste(app, global_conf, **kw):

serve(app, **kw)<EOL>return <NUM_LIT:0><EOL>

pserve / paster serve / waitress replacement / integration

    You can pass as parameters:

    transports = websockets, xhr-multipart, xhr-longpolling, etc...
    policy_server = True


self.sockets = {}<EOL>if '<STR_LIT>' in kwargs:<EOL><INDENT>print("<STR_LIT>")<EOL>self.resource = kwargs.pop('<STR_LIT>', '<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>self.resource = kwargs.pop('<STR_LIT>', '<STR_LIT>')<EOL><DEDENT>self.transports = kwargs.pop('<STR_LIT>', None)<EOL>if kwargs.pop('<STR_LIT>', True):<EOL><INDENT>try:<EOL><INDENT>address = args[<NUM_LIT:0>][<NUM_LIT:0>]<EOL><DEDENT>except TypeError:<EOL><INDENT>try:<EOL><INDENT>address = args[<NUM_LIT:0>].address[<NUM_LIT:0>]<EOL><DEDENT>except AttributeError:<EOL><INDENT>address = args[<NUM_LIT:0>].cfg_addr[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>policylistener = kwargs.pop('<STR_LIT>', (address, <NUM_LIT>))<EOL>self.policy_server = FlashPolicyServer(policylistener)<EOL><DEDENT>else:<EOL><INDENT>self.policy_server = None<EOL><DEDENT>self.config = {<EOL>'<STR_LIT>': <NUM_LIT>,<EOL>'<STR_LIT>': <NUM_LIT>,<EOL>'<STR_LIT>': <NUM_LIT>,<EOL>}<EOL>for f in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>if f in kwargs:<EOL><INDENT>self.config[f] = int(kwargs.pop(f))<EOL><DEDENT><DEDENT>if not '<STR_LIT>' in kwargs:<EOL><INDENT>kwargs['<STR_LIT>'] = SocketIOHandler<EOL><DEDENT>if not '<STR_LIT>' in kwargs:<EOL><INDENT>self.ws_handler_class = WebSocketHandler<EOL><DEDENT>else:<EOL><INDENT>self.ws_handler_class = kwargs.pop('<STR_LIT>')<EOL><DEDENT>log_file = kwargs.pop('<STR_LIT>', None)<EOL>if log_file:<EOL><INDENT>kwargs['<STR_LIT>'] = open(log_file, '<STR_LIT:a>')<EOL><DEDENT>super(SocketIOServer, self).__init__(*args, **kwargs)<EOL>

This is just like the standard WSGIServer __init__, except with a
        few additional ``kwargs``:

        :param resource: The URL which has to be identified as a
            socket.io request.  Defaults to the /socket.io/ URL.

        :param transports: Optional list of transports to allow. List of
            strings, each string should be one of
            handler.SocketIOHandler.handler_types.

        :param policy_server: Boolean describing whether or not to use the
            Flash policy server.  Default True.

        :param policy_listener: A tuple containing (host, port) for the
            policy server.  This is optional and used only if policy server
            is set to true.  The default value is 0.0.0.0:843

        :param heartbeat_interval: int The timeout for the server, we
            should receive a heartbeat from the client within this
            interval. This should be less than the
            ``heartbeat_timeout``.

        :param heartbeat_timeout: int The timeout for the client when
            it should send a new heartbeat to the server. This value
            is sent to the client after a successful handshake.

        :param close_timeout: int The timeout for the client, when it
            closes the connection it still X amounts of seconds to do
            re open of the connection. This value is sent to the
            client after a successful handshake.

        :param log_file: str The file in which you want the PyWSGI
            server to write its access log.  If not specified, it
            is sent to `stderr` (with gevent 0.13).


socket = self.sockets.get(sessid)<EOL>if sessid and not socket:<EOL><INDENT>return None  <EOL><DEDENT>if socket is None:<EOL><INDENT>socket = Socket(self, self.config)<EOL>self.sockets[socket.sessid] = socket<EOL><DEDENT>else:<EOL><INDENT>socket.incr_hits()<EOL><DEDENT>return socket<EOL>

Return an existing or new client Socket.


def __init__(self, handler, config, **kwargs):

self.content_type = ("<STR_LIT:Content-Type>", "<STR_LIT>")<EOL>self.headers = [<EOL>("<STR_LIT>", "<STR_LIT:*>"),<EOL>("<STR_LIT>", "<STR_LIT:true>"),<EOL>("<STR_LIT>", "<STR_LIT>"),<EOL>("<STR_LIT>", "<STR_LIT>"),<EOL>]<EOL>self.handler = handler<EOL>self.config = config<EOL>

Base transport class.

        :param config: dict Should contain the config keys, like
          ``heartbeat_interval``, ``heartbeat_timeout`` and
          ``close_timeout``.


def get_messages_payload(self, socket, timeout=None):

try:<EOL><INDENT>msgs = socket.get_multiple_client_msgs(timeout=timeout)<EOL>data = self.encode_payload(msgs)<EOL><DEDENT>except Empty:<EOL><INDENT>data = "<STR_LIT>"<EOL><DEDENT>return data<EOL>

This will fetch the messages from the Socket's queue, and if
        there are many messes, pack multiple messages in one payload and return


if not messages or messages[<NUM_LIT:0>] is None:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>if len(messages) == <NUM_LIT:1>:<EOL><INDENT>return messages[<NUM_LIT:0>].encode('<STR_LIT:utf-8>')<EOL><DEDENT>payload = u'<STR_LIT>'.join([(u'<STR_LIT>' % (len(p), p))<EOL>for p in messages if p is not None])<EOL>return payload.encode('<STR_LIT:utf-8>')<EOL>

Encode list of messages. Expects messages to be unicode.

        ``messages`` - List of raw messages to encode, if necessary


payload = payload.decode('<STR_LIT:utf-8>')<EOL>if payload[<NUM_LIT:0>] == u"<STR_LIT>":<EOL><INDENT>ret = []<EOL>while len(payload) != <NUM_LIT:0>:<EOL><INDENT>len_end = payload.find(u"<STR_LIT>", <NUM_LIT:1>)<EOL>length = int(payload[<NUM_LIT:1>:len_end])<EOL>msg_start = len_end + <NUM_LIT:1><EOL>msg_end = length + msg_start<EOL>message = payload[msg_start:msg_end]<EOL>ret.append(message)<EOL>payload = payload[msg_end:]<EOL><DEDENT>return ret<EOL><DEDENT>return [payload]<EOL>

This function can extract multiple messages from one HTTP payload.
        Some times, the XHR/JSONP/.. transports can pack more than one message
        on a single packet.  They are encoding following the WebSocket
        semantics, which need to be reproduced here to unwrap the messages.

        The semantics are:

          \ufffd + [length as a string] + \ufffd + [payload as a unicode string]

        This function returns a list of messages, even though there is only
        one.

        Inspired by socket.io/lib/transports/http.js


args = parse_qs(self.handler.environ.get("<STR_LIT>"))<EOL>if "<STR_LIT:i>" in args:<EOL><INDENT>i = args["<STR_LIT:i>"]<EOL><DEDENT>else:<EOL><INDENT>i = "<STR_LIT:0>"<EOL><DEDENT>super(JSONPolling, self).write("<STR_LIT>" % (i, data))<EOL>

Just quote out stuff before sending it out


def socketio_manage(environ, namespaces, request=None, error_handler=None,<EOL>json_loads=None, json_dumps=None):

Main SocketIO management function, call from within your Framework of
    choice's view.

    The ``environ`` variable is the WSGI ``environ``.  It is used to extract
    Socket object from the underlying server (as the 'socketio' key), and will
    be attached to both the ``Socket`` and ``Namespace`` objects.

    The ``namespaces`` parameter is a dictionary of the namespace string
    representation as key, and the BaseNamespace namespace class descendant as
    a value.  The empty string ('') namespace is the global namespace.  You can
    use Socket.GLOBAL_NS to be more explicit. So it would look like:

    .. code-block:: python

      namespaces={'': GlobalNamespace,
                  '/chat': ChatNamespace}

    The ``request`` object is not required, but will probably be useful to pass
    framework-specific things into your Socket and Namespace functions. It will
    simply be attached to the Socket and Namespace object (accessible through
    ``self.request`` in both cases), and it is not accessed in any case by the
    ``gevent-socketio`` library.

    Pass in an ``error_handler`` if you want to override the default
    error_handler (which is :func:`socketio.virtsocket.default_error_handler`.
    The callable you pass in should have the same signature as the default
    error handler.

    The ``json_loads`` and ``json_dumps`` are overrides for the default
    ``json.loads`` and ``json.dumps`` function calls.  Override these at
    the top-most level here.  This will affect all sockets created by this
    socketio manager, and all namespaces inside.

    This function will block the current "view" or "controller" in your
    framework to do the recv/send on the socket, and dispatch incoming messages
    to your namespaces.

    This is a simple example using Pyramid:

    .. code-block:: python

      def my_view(request):
          socketio_manage(request.environ, {'': GlobalNamespace}, request)

    NOTE: You must understand that this function is going to be called
    *only once* per socket opening, *even though* you are using a long
    polling mechanism.  The subsequent calls (for long polling) will
    be hooked directly at the server-level, to interact with the
    active ``Socket`` instance.  This means you will *not* get access
    to the future ``request`` or ``environ`` objects.  This is of
    particular importance regarding sessions (like Beaker).  The
    session will be opened once at the opening of the Socket, and not
    closed until the socket is closed.  You are responsible for
    opening and closing the cookie-based session yourself if you want
    to keep its data in sync with the rest of your GET/POST calls.


def __init__(self, config, *args, **kwargs):

self.socketio_connection = False<EOL>self.allowed_paths = None<EOL>self.config = config<EOL>super(SocketIOHandler, self).__init__(*args, **kwargs)<EOL>self.transports = list(self.handler_types.keys())<EOL>if self.server.transports:<EOL><INDENT>self.transports = self.server.transports<EOL>if not set(self.transports).issubset(set(self.handler_types)):<EOL><INDENT>raise ValueError("<STR_LIT>" %<EOL>list(self.handler_types.keys()))<EOL><DEDENT><DEDENT>

Create a new SocketIOHandler.

        :param config: dict Configuration for timeouts and intervals
          that will go down to the other components, transports, etc..


path = self.environ.get('<STR_LIT>')<EOL>if not path.lstrip('<STR_LIT:/>').startswith(self.server.resource + '<STR_LIT:/>'):<EOL><INDENT>return super(SocketIOHandler, self).handle_one_response()<EOL><DEDENT>self.status = None<EOL>self.headers_sent = False<EOL>self.result = None<EOL>self.response_length = <NUM_LIT:0><EOL>self.response_use_chunked = False<EOL>request_method = self.environ.get("<STR_LIT>")<EOL>request_tokens = self.RE_REQUEST_URL.match(path)<EOL>handshake_tokens = self.RE_HANDSHAKE_URL.match(path)<EOL>disconnect_tokens = self.RE_DISCONNECT_URL.match(path)<EOL>if handshake_tokens:<EOL><INDENT>return self._do_handshake(handshake_tokens.groupdict())<EOL><DEDENT>elif disconnect_tokens:<EOL><INDENT>tokens = disconnect_tokens.groupdict()<EOL><DEDENT>elif request_tokens:<EOL><INDENT>tokens = request_tokens.groupdict()<EOL><DEDENT>else:<EOL><INDENT>return super(SocketIOHandler, self).handle_one_response()<EOL><DEDENT>sessid = tokens["<STR_LIT>"]<EOL>socket = self.server.get_socket(sessid)<EOL>if not socket:<EOL><INDENT>self.handle_bad_request()<EOL>return []  <EOL><DEDENT>if self.environ['<STR_LIT>'].startswith('<STR_LIT>'):<EOL><INDENT>socket.disconnect()<EOL>self.handle_disconnect_request()<EOL>return []<EOL><DEDENT>transport = self.handler_types.get(tokens["<STR_LIT>"])<EOL>old_class = None<EOL>if issubclass(transport, (transports.WebsocketTransport,<EOL>transports.FlashSocketTransport)):<EOL><INDENT>old_class = self.__class__<EOL>self.__class__ = self.server.ws_handler_class<EOL>self.prevent_wsgi_call = True  <EOL>self.handle_one_response()  <EOL><DEDENT>self.environ['<STR_LIT>'] = socket<EOL>self.transport = transport(self, self.config)<EOL>self.transport.do_exchange(socket, request_method)<EOL>if not socket.connection_established:<EOL><INDENT>socket.connection_established = True<EOL>socket.state = socket.STATE_CONNECTED<EOL>socket._spawn_heartbeat()<EOL>socket._spawn_watcher()<EOL>try:<EOL><INDENT>if socket.wsgi_app_greenlet is None:<EOL><INDENT>start_response = lambda status, headers, exc=None: None<EOL>socket.wsgi_app_greenlet = gevent.spawn(self.application,<EOL>self.environ,<EOL>start_response)<EOL><DEDENT><DEDENT>except:<EOL><INDENT>self.handle_error(*sys.exc_info())<EOL><DEDENT><DEDENT>if tokens['<STR_LIT>'] in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>gevent.joinall(socket.jobs)<EOL><DEDENT>if old_class:<EOL><INDENT>self.__class__ = old_class<EOL><DEDENT>if hasattr(self, '<STR_LIT>') and self.websocket:<EOL><INDENT>if hasattr(self.websocket, '<STR_LIT>'):<EOL><INDENT>del self.websocket.environ<EOL><DEDENT>del self.websocket<EOL><DEDENT>if self.environ:<EOL><INDENT>del self.environ<EOL><DEDENT>

This function deals with *ONE INCOMING REQUEST* from the web.

        It will wire and exchange message to the queues for long-polling
        methods, otherwise, will stay alive for websockets.


if self.allowed_methods is None:<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>return method_name in self.allowed_methods<EOL><DEDENT>

ACL system: this checks if you have access to that method_name,
        according to the set ACLs


if isinstance(self.allowed_methods, set):<EOL><INDENT>self.allowed_methods.add(method_name)<EOL><DEDENT>else:<EOL><INDENT>self.allowed_methods = set([method_name])<EOL><DEDENT>

ACL system: make the method_name accessible to the current socket


if self.allowed_methods is None:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>)<EOL><DEDENT>self.allowed_methods.remove(method_name)<EOL>

ACL system: ensure the user will not have access to that method.


self.allowed_methods = None<EOL>

ACL system: This removes restrictions on the Namespace's methods, so
        that all the ``on_*()`` and ``recv_*()`` can be accessed.


return None<EOL>

ACL system: If you define this function, you must return
        all the 'event' names that you want your User (the established
        virtual Socket) to have access to.

        If you do not define this function, the user will have free
        access to all of the ``on_*()`` and ``recv_*()`` functions,
        etc.. methods.

        Return something like: ``set(['recv_connect', 'on_public_method'])``

        You can later modify this list dynamically (inside
        ``on_connect()`` for example) using:

        .. code-block:: python

           self.add_acl_method('on_secure_method')

        ``self.request`` is available in here, if you're already ready to
        do some auth. check.

        The ACLs are checked by the :meth:`process_packet` and/or
        :meth:`process_event` default implementations, before calling
        the class's methods.

        **Beware**, returning ``None`` leaves the namespace completely
        accessible.

        The methods that are open are stored in the ``allowed_methods``
        attribute of the ``Namespace`` instance.


self.allowed_methods = self.get_initial_acl()<EOL>

Resets ACL to its initial value (calling
        :meth:`get_initial_acl`` and applying that again).


packet_type = packet['<STR_LIT:type>']<EOL>if packet_type == '<STR_LIT>':<EOL><INDENT>return self.process_event(packet)<EOL><DEDENT>elif packet_type == '<STR_LIT:message>':<EOL><INDENT>return self.call_method_with_acl('<STR_LIT>', packet,<EOL>packet['<STR_LIT:data>'])<EOL><DEDENT>elif packet_type == '<STR_LIT>':<EOL><INDENT>return self.call_method_with_acl('<STR_LIT>', packet,<EOL>packet['<STR_LIT:data>'])<EOL><DEDENT>elif packet_type == '<STR_LIT>':<EOL><INDENT>self.socket.send_packet(packet)<EOL>return self.call_method_with_acl('<STR_LIT>', packet)<EOL><DEDENT>elif packet_type == '<STR_LIT:error>':<EOL><INDENT>return self.call_method_with_acl('<STR_LIT>', packet)<EOL><DEDENT>elif packet_type == '<STR_LIT>':<EOL><INDENT>callback = self.socket._pop_ack_callback(packet['<STR_LIT>'])<EOL>if not callback:<EOL><INDENT>print("<STR_LIT>" % packet['<STR_LIT>'])<EOL>return<EOL><DEDENT>return callback(*(packet['<STR_LIT:args>']))<EOL><DEDENT>elif packet_type == '<STR_LIT>':<EOL><INDENT>return self.call_method_with_acl('<STR_LIT>', packet)<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>", packet)<EOL><DEDENT>

If you override this, NONE of the functions in this class
        will be called.  It is responsible for dispatching to
        :meth:`process_event` (which in turn calls ``on_*()`` and
        ``recv_*()`` methods).

        If the packet arrived here, it is because it belongs to this endpoint.

        For each packet arriving, the only possible path of execution, that is,
        the only methods that *can* be called are the following:

        * recv_connect()
        * recv_message()
        * recv_json()
        * recv_error()
        * recv_disconnect()
        * on_*()


args = packet['<STR_LIT:args>']<EOL>name = packet['<STR_LIT:name>']<EOL>if not allowed_event_name_regex.match(name):<EOL><INDENT>self.error("<STR_LIT>",<EOL>"<STR_LIT>")<EOL>return<EOL><DEDENT>method_name = '<STR_LIT>' + name.replace('<STR_LIT:U+0020>', '<STR_LIT:_>')<EOL>return self.call_method_with_acl(method_name, packet, *args)<EOL>

This function dispatches ``event`` messages to the correct
        functions. You should override this method only if you are not
        satisfied with the automatic dispatching to
        ``on_``-prefixed methods.  You could then implement your own dispatch.
        See the source code for inspiration.

        There are two ways to deal with callbacks from the client side
        (meaning, the browser has a callback waiting for data that this
        server will be sending back):

        The first one is simply to return an object.  If the incoming
        packet requested has an 'ack' field set, meaning the browser is
        waiting for callback data, it will automatically be packaged
        and sent, associated with the 'ackId' from the browser. The
        return value must be a *sequence* of elements, that will be
        mapped to the positional parameters of the callback function
        on the browser side.

        If you want to *know* that you're dealing with a packet
        that requires a return value, you can do those things manually
        by inspecting the ``ack`` and ``id`` keys from the ``packet``
        object.  Your callback will behave specially if the name of
        the argument to your method is ``packet``.  It will fill it
        with the unprocessed ``packet`` object for your inspection,
        like this:

        .. code-block:: python

          def on_my_callback(self, packet):
              if 'ack' in packet:
                  self.emit('go_back', 'param1', id=packet['id'])


def call_method_with_acl(self, method_name, packet, *args):

if not self.is_method_allowed(method_name):<EOL><INDENT>self.error('<STR_LIT>',<EOL>'<STR_LIT>' % method_name)<EOL>return<EOL><DEDENT>return self.call_method(method_name, packet, *args)<EOL>

You should always use this function to call the methods,
        as it checks if the user is allowed according to the ACLs.

        If you override :meth:`process_packet` or
        :meth:`process_event`, you should definitely want to use this
        instead of ``getattr(self, 'my_method')()``


def call_method(self, method_name, packet, *args):

method = getattr(self, method_name, None)<EOL>if method is None:<EOL><INDENT>self.error('<STR_LIT>',<EOL>'<STR_LIT>' % method_name)<EOL>return<EOL><DEDENT>specs = inspect.getargspec(method)<EOL>func_args = specs.args<EOL>if not len(func_args) or func_args[<NUM_LIT:0>] != '<STR_LIT>':<EOL><INDENT>self.error("<STR_LIT>",<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL>return<EOL><DEDENT>if hasattr(self, '<STR_LIT>'):<EOL><INDENT>method = self.exception_handler_decorator(method)<EOL><DEDENT>if len(func_args) == <NUM_LIT:2> and func_args[<NUM_LIT:1>] == '<STR_LIT>':<EOL><INDENT>return method(packet)<EOL><DEDENT>else:<EOL><INDENT>return method(*args)<EOL><DEDENT>

This function is used to implement the two behaviors on dispatched
        ``on_*()`` and ``recv_*()`` method calls.

        Those are the two behaviors:

        * If there is only one parameter on the dispatched method and
          it is named ``packet``, then pass in the packet dict as the
          sole parameter.

        * Otherwise, pass in the arguments as specified by the
          different ``recv_*()`` methods args specs, or the
          :meth:`process_event` documentation.

        This method will also consider the
        ``exception_handler_decorator``.  See Namespace documentation
        for details and examples.


pass<EOL>

This is called right after ``__init__``, on the initial
        creation of a namespace so you may handle any setup job you
        need.

        Namespaces are created only when some packets arrive that ask
        for the namespace.  They are not created altogether when a new
        :class:`~socketio.virtsocket.Socket` connection is established,
        so you can have many many namespaces assigned (when calling
        :func:`~socketio.socketio_manage`) without clogging the
        memory.

        If you override this method, you probably want to initialize
        the variables you're going to use in the events handled by this
        namespace, setup ACLs, etc..

        This method is called on all base classes following the _`method resolution order <http://docs.python.org/library/stdtypes.html?highlight=mro#class.__mro__>`
        so you don't need to call super() to initialize the mixins or
        other derived classes.


return data<EOL>

This is more of a backwards compatibility hack. This will be
        called for messages sent with the original send() call on the client
        side. This is NOT the 'message' event, which you will catch with
        'on_message()'. The data arriving here is a simple string, with no
        other info.

        If you want to handle those messages, you should override this method.


return data<EOL>

This is more of a backwards compatibility hack. This will be
        called for JSON packets sent with the original json() call on the
        JavaScript side. This is NOT the 'json' event, which you will catch
        with 'on_json()'. The data arriving here is a python dict, with no
        event name.

        If you want to handle those messages, you should override this method.


self.disconnect(silent=True)<EOL>

Override this function if you want to do something when you get a
        *force disconnect* packet.

        By default, this function calls the :meth:`disconnect` clean-up
        function.  You probably want to call it yourself also, and put
        your clean-up routines in :meth:`disconnect` rather than here,
        because that :meth:`disconnect` function gets called
        automatically upon disconnection.  This function is a
        pre-handle for when you get the `disconnect packet`.


pass<EOL>

Called the first time a client connection is open on a
        Namespace. This *does not* fire on the global namespace.

        This allows you to do boilerplate stuff for
        the namespace like connecting to rooms, broadcasting events
        to others, doing authorization work, and tweaking the ACLs to open
        up the rest of the namespace (if it was closed at the
        beginning by having :meth:`get_initial_acl` return only
        ['recv_connect'])

        Also see the different :ref:`mixins <mixins_module>` (like
        `RoomsMixin`, `BroadcastMixin`).


pass<EOL>

Override this function to handle the errors we get from the client.

        :param packet: the full packet.


def error(self, error_name, error_message, msg_id=None, quiet=False):

self.socket.error(error_name, error_message, endpoint=self.ns_name,<EOL>msg_id=msg_id, quiet=quiet)<EOL>

Use this to use the configured ``error_handler`` yield an
        error message to your application.

        :param error_name: is a short string, to associate messages to recovery
                           methods
        :param error_message: is some human-readable text, describing the error
        :param msg_id: is used to associate with a request
        :param quiet: specific to error_handlers. The default doesn't send a
                      message to the user, but shows a debug message on the
                      developer console.


def send(self, message, json=False, callback=None):

pkt = dict(type="<STR_LIT:message>", data=message, endpoint=self.ns_name)<EOL>if json:<EOL><INDENT>pkt['<STR_LIT:type>'] = "<STR_LIT>"<EOL><DEDENT>if callback:<EOL><INDENT>pkt['<STR_LIT>'] = True<EOL>pkt['<STR_LIT:id>'] = msgid = self.socket._get_next_msgid()<EOL>self.socket._save_ack_callback(msgid, callback)<EOL><DEDENT>self.socket.send_packet(pkt)<EOL>

Use send to send a simple string message.

        If ``json`` is True, the message will be encoded as a JSON object
        on the wire, and decoded on the other side.

        This is mostly for backwards compatibility.  ``emit()`` is more fun.

        :param callback: This is a callback function that will be
                         called automatically by the client upon
                         reception.  It does not verify that the
                         listener over there was completed with
                         success.  It just tells you that the browser
                         got a hold of the packet.
        :type callback: callable


callback = kwargs.pop('<STR_LIT>', None)<EOL>if kwargs:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>pkt = dict(type="<STR_LIT>", name=event, args=args,<EOL>endpoint=self.ns_name)<EOL>if callback:<EOL><INDENT>pkt['<STR_LIT>'] = '<STR_LIT:data>'<EOL>pkt['<STR_LIT:id>'] = msgid = self.socket._get_next_msgid()<EOL>self.socket._save_ack_callback(msgid, callback)<EOL><DEDENT>self.socket.send_packet(pkt)<EOL>

Use this to send a structured event, with a name and arguments, to
        the client.

        By default, it uses this namespace's endpoint. You can send messages on
        other endpoints with something like:

            ``self.socket['/other_endpoint'].emit()``.

        However, it is possible that the ``'/other_endpoint'`` was not
        initialized yet, and that would yield a ``KeyError``.

        The only supported ``kwargs`` is ``callback``.  All other parameters
        must be passed positionally.

        :param event: The name of the event to trigger on the other end.
        :param callback: Pass in the callback keyword argument to define a
                         call-back that will be called when the client acks.

                         This callback is slightly different from the one from
                         ``send()``, as this callback will receive parameters
                         from the explicit call of the ``ack()`` function
                         passed to the listener on the client side.

                         The remote listener will need to explicitly ack (by
                         calling its last argument, a function which is
                         usually called 'ack') with some parameters indicating
                         success or error.  The 'ack' packet coming back here
                         will then trigger the callback function with the
                         returned values.
        :type callback: callable


<EOL>if hasattr(self, '<STR_LIT>'):<EOL><INDENT>fn = self.exception_handler_decorator(fn)<EOL><DEDENT>new = gevent.spawn(fn, *args, **kwargs)<EOL>self.jobs.append(new)<EOL>return new<EOL>

Spawn a new process, attached to this Namespace.

        It will be monitored by the "watcher" process in the Socket. If the
        socket disconnects, all these greenlets are going to be killed, after
        calling BaseNamespace.disconnect()

        This method uses the ``exception_handler_decorator``.  See
        Namespace documentation for more information.


if not silent:<EOL><INDENT>packet = {"<STR_LIT:type>": "<STR_LIT>",<EOL>"<STR_LIT>": self.ns_name}<EOL>self.socket.send_packet(packet)<EOL><DEDENT>try:<EOL><INDENT>self.socket.remove_namespace(self.ns_name)<EOL><DEDENT>finally:<EOL><INDENT>self.kill_local_jobs()<EOL><DEDENT>

Send a 'disconnect' packet, so that the user knows it has been
        disconnected (booted actually).  This will trigger an onDisconnect()
        call on the client side.

        Over here, we will kill all ``spawn``ed processes and remove the
        namespace from the Socket object.

        :param silent: do not actually send the packet (if they asked for a
                       disconnect for example), but just kill all jobs spawned
                       by this Namespace, and remove it from the Socket.


gevent.killall(self.jobs)<EOL>self.jobs = []<EOL>

Kills all the jobs spawned with BaseNamespace.spawn() on a namespace
        object.

        This will be called automatically if the ``watcher`` process detects
        that the Socket was closed.


self.args = self.parse_and_process_args(args)<EOL>if self.args.version:<EOL><INDENT>print(__version__)<EOL>return <NUM_LIT:0><EOL><DEDENT>if self.args.rulefile:<EOL><INDENT>for filename in self.args.rulefile:<EOL><INDENT>self._load_rule_file(filename)<EOL><DEDENT><DEDENT>if self.args.list:<EOL><INDENT>self.list_rules()<EOL>return <NUM_LIT:0><EOL><DEDENT>if self.args.describe:<EOL><INDENT>self._describe_rules(self.args.args)<EOL>return <NUM_LIT:0><EOL><DEDENT>self.counts = { ERROR: <NUM_LIT:0>, WARNING: <NUM_LIT:0>, "<STR_LIT>": <NUM_LIT:0>}<EOL>for filename in self.args.args:<EOL><INDENT>if not (os.path.exists(filename)):<EOL><INDENT>sys.stderr.write("<STR_LIT>" % filename)<EOL>continue<EOL><DEDENT>if os.path.isdir(filename):<EOL><INDENT>self._process_folder(filename)<EOL><DEDENT>else:<EOL><INDENT>self._process_file(filename)<EOL><DEDENT><DEDENT>if self.counts[ERROR] > <NUM_LIT:0>:<EOL><INDENT>return self.counts[ERROR] if self.counts[ERROR] < <NUM_LIT> else <NUM_LIT:255><EOL><DEDENT>return <NUM_LIT:0><EOL>

Parse command line arguments, and run rflint


for rule in sorted(self.all_rules, key=lambda rule: rule.name):<EOL><INDENT>print(rule)<EOL>if self.args.verbose:<EOL><INDENT>for line in rule.doc.split("<STR_LIT:\n>"):<EOL><INDENT>print("<STR_LIT:U+0020>", line)<EOL><DEDENT><DEDENT><DEDENT>


def report(self, linenumber, filename, severity, message, rulename, char):

if self._print_filename is not None:<EOL><INDENT>print("<STR_LIT>" + self._print_filename)<EOL>self._print_filename = None<EOL><DEDENT>if severity in (WARNING, ERROR):<EOL><INDENT>self.counts[severity] += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>self.counts["<STR_LIT>"] += <NUM_LIT:1><EOL><DEDENT>print(self.args.format.format(linenumber=linenumber, filename=filename,<EOL>severity=severity, message=message.encode('<STR_LIT:utf-8>'),<EOL>rulename=rulename, char=char))<EOL>


result = []<EOL>for rule_class in cls.__subclasses__():<EOL><INDENT>rule_name = rule_class.__name__.lower()<EOL>if rule_name not in self._rules:<EOL><INDENT>rule = rule_class(self)<EOL>self._rules[rule_name] = rule<EOL><DEDENT>result.append(self._rules[rule_name])<EOL><DEDENT>return result<EOL>

Returns a list of rules of a given class

        Rules are treated as singletons - we only instantiate each
        rule once.


if not (os.path.exists(filename)):<EOL><INDENT>sys.stderr.write("<STR_LIT>" % filename)<EOL>return<EOL><DEDENT>try:<EOL><INDENT>basename = os.path.basename(filename)<EOL>(name, ext) = os.path.splitext(basename)<EOL>imp.load_source(name, filename)<EOL><DEDENT>except Exception as e:<EOL><INDENT>sys.stderr.write("<STR_LIT>" % (filename, str(e)))<EOL><DEDENT>


parser = argparse.ArgumentParser(<EOL>prog="<STR_LIT>",<EOL>description="<STR_LIT>",<EOL>formatter_class=argparse.RawDescriptionHelpFormatter,<EOL>epilog = (<EOL>"<STR_LIT>"<EOL>"<STR_LIT:\n>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT:\n>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT:\n>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT:\n>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT:\n>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL>)<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", metavar="<STR_LIT>", action=SetErrorAction,<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", metavar="<STR_LIT>", action=SetIgnoreAction,<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", metavar="<STR_LIT>", action=SetWarningAction,<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store_true>",<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store_true>",<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", action="<STR_LIT>", dest="<STR_LIT>", <EOL>default=True,<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", <EOL>help="<STR_LIT>",<EOL>default='<STR_LIT>')<EOL>parser.add_argument("<STR_LIT>", action="<STR_LIT:store_true>", default=False,<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store_true>", default=False,<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT:-c>", action=ConfigureAction,<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store_true>", default=False,<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", action=RulefileAction,<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", action=ArgfileLoader,<EOL>help="<STR_LIT>")<EOL>parser.add_argument('<STR_LIT:args>', metavar="<STR_LIT:file>", nargs=argparse.REMAINDER)<EOL>ns = argparse.Namespace()<EOL>setattr(ns, "<STR_LIT>", self)<EOL>args = parser.parse_args(args, ns)<EOL>Rule.output_format = args.format<EOL>return args<EOL>

Handle the parsing of command line arguments.


def report(self, obj, message, linenum, char_offset=<NUM_LIT:0>):

self.controller.report(linenumber=linenum, filename=obj.path,<EOL>severity=self.severity, message=message,<EOL>rulename = self.__class__.__name__,<EOL>char=char_offset)<EOL>


if not self.__doc__:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>lines = self.__doc__.expandtabs().splitlines()<EOL>indent = sys.maxsize<EOL>for line in lines[<NUM_LIT:1>:]:<EOL><INDENT>stripped = line.lstrip()<EOL>if stripped:<EOL><INDENT>indent = min(indent, len(line) - len(stripped))<EOL><DEDENT><DEDENT>trimmed = [lines[<NUM_LIT:0>].strip()]<EOL>if indent < sys.maxsize:<EOL><INDENT>for line in lines[<NUM_LIT:1>:]:<EOL><INDENT>trimmed.append(line[indent:].rstrip())<EOL><DEDENT><DEDENT>while trimmed and not trimmed[-<NUM_LIT:1>]:<EOL><INDENT>trimmed.pop()<EOL><DEDENT>while trimmed and not trimmed[<NUM_LIT:0>]:<EOL><INDENT>trimmed.pop(<NUM_LIT:0>)<EOL><DEDENT>return '<STR_LIT:\n>'.join(trimmed)<EOL>


return string.replace("<STR_LIT:U+0020>", "<STR_LIT>").replace("<STR_LIT:_>", "<STR_LIT>").lower()<EOL>

convert to lowercase, remove spaces and underscores


if os.path.isdir(path):<EOL><INDENT>return SuiteFolder(path, parent)<EOL><DEDENT>else:<EOL><INDENT>rf = RobotFile(path, parent)<EOL>for table in rf.tables:<EOL><INDENT>if isinstance(table, TestcaseTable):<EOL><INDENT>rf.__class__ = SuiteFile<EOL>return rf<EOL><DEDENT><DEDENT>rf.__class__ = ResourceFile<EOL>return rf<EOL><DEDENT>

Return an instance of SuiteFile, ResourceFile, SuiteFolder

    Exactly which is returned depends on whether it's a file or
    folder, and if a file, the contents of the file. If there is a
    testcase table, this will return an instance of SuiteFile,
    otherwise it will return an instance of ResourceFile.


requested = types if len(types) > <NUM_LIT:0> else [SuiteFile, ResourceFile, SuiteFolder, Testcase, Keyword]<EOL>for thing in self.robot_files:<EOL><INDENT>if thing.__class__ in requested:<EOL><INDENT>yield thing<EOL><DEDENT>if isinstance(thing, SuiteFolder):<EOL><INDENT>for child in thing.walk():<EOL><INDENT>if child.__class__ in requested:<EOL><INDENT>yield child<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>for child in thing.walk(*types):<EOL><INDENT>yield child<EOL><DEDENT><DEDENT><DEDENT>

Iterator which visits all suites and suite files,
yielding test cases and keywords


result = []<EOL>for name in os.listdir(self.path):<EOL><INDENT>fullpath = os.path.join(self.path, name)<EOL>if os.path.isdir(fullpath):<EOL><INDENT>result.append(RobotFactory(fullpath, parent=self))<EOL><DEDENT>else:<EOL><INDENT>if ((name.endswith("<STR_LIT>") or name.endswith("<STR_LIT>")) and<EOL>(name not in ("<STR_LIT>", "<STR_LIT>"))):<EOL><INDENT>result.append(RobotFactory(fullpath, parent=self))<EOL><DEDENT><DEDENT><DEDENT>return result<EOL>

Return a list of all folders, and test suite files (.txt, .robot)


requested = types if len(types) > <NUM_LIT:0> else [Testcase, Keyword]<EOL>if Testcase in requested:<EOL><INDENT>for testcase in self.testcases:<EOL><INDENT>yield testcase<EOL><DEDENT><DEDENT>if Keyword in requested:<EOL><INDENT>for keyword in self.keywords:<EOL><INDENT>yield keyword<EOL><DEDENT><DEDENT>

Iterator which can return all test cases and/or keywords

You can specify with objects to return as parameters; if
no parameters are given, both tests and keywords will
be returned.

For example, to get only test cases, you could call it
like this:

    robot_file = RobotFactory(...)
    for testcase in robot_file.walk(Testcase): ...


self.tables = []<EOL>current_table = DefaultTable(self)<EOL>with Utf8Reader(path) as f:<EOL><INDENT>self.raw_text = f.read()<EOL>f._file.seek(<NUM_LIT:0>) <EOL>matcher = Matcher(re.IGNORECASE)<EOL>for linenumber, raw_text in enumerate(f.readlines()):<EOL><INDENT>linenumber += <NUM_LIT:1>; <EOL>raw_text = raw_text.replace(u'<STR_LIT>', '<STR_LIT:U+0020>')<EOL>raw_text = raw_text.rstrip()<EOL>cells = TxtReader.split_row(raw_text)<EOL>_heading_regex = r'<STR_LIT>'<EOL>if matcher(_heading_regex, cells[<NUM_LIT:0>]):<EOL><INDENT>table_name = matcher.group(<NUM_LIT:1>)<EOL>current_table = tableFactory(self, linenumber, table_name, raw_text)<EOL>self.tables.append(current_table)<EOL><DEDENT>else:<EOL><INDENT>current_table.append(Row(linenumber, raw_text, cells))<EOL><DEDENT><DEDENT><DEDENT>

The general idea is to do a quick parse, creating a list of
tables. Each table is nothing more than a list of rows, with
each row being a list of cells. Additional parsing such as
combining rows into statements is done on demand. This first
pass is solely to read in the plain text and organize it by table.


robot_tables = [table for table in self.tables if not isinstance(table, UnknownTable)]<EOL>if len(robot_tables) == <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>for table in self.tables:<EOL><INDENT>if isinstance(table, TestcaseTable):<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT><DEDENT>return "<STR_LIT>"<EOL>

Return 'suite' or 'resource' or None

        This will return 'suite' if a testcase table is found;
        It will return 'resource' if at least one robot table
        is found. If no tables are found it will return None


for table in self.tables:<EOL><INDENT>if isinstance(table, KeywordTable):<EOL><INDENT>for keyword in table.keywords:<EOL><INDENT>yield keyword<EOL><DEDENT><DEDENT><DEDENT>

Generator which returns all keywords in the suite


for table in self.tables:<EOL><INDENT>print("<STR_LIT>" % table.name)<EOL>table.dump()<EOL><DEDENT>


for table in self.tables:<EOL><INDENT>if isinstance(table, SettingTable):<EOL><INDENT>for statement in table.statements:<EOL><INDENT>yield statement<EOL><DEDENT><DEDENT><DEDENT>

Generator which returns all of the statements in all of the settings tables


for table in self.tables:<EOL><INDENT>if isinstance(table, VariableTable):<EOL><INDENT>for statement in table.rows:<EOL><INDENT>if statement[<NUM_LIT:0>] != "<STR_LIT>":<EOL><INDENT>yield statement<EOL><DEDENT><DEDENT><DEDENT><DEDENT>

Generator which returns all of the statements in all of the variables tables


<EOL>if len(self.rows) == <NUM_LIT:0>:<EOL><INDENT>return []<EOL><DEDENT>current_statement = Statement(self.rows[<NUM_LIT:0>])<EOL>current_statement.startline = self.rows[<NUM_LIT:0>].linenumber<EOL>current_statement.endline = self.rows[<NUM_LIT:0>].linenumber<EOL>statements = []<EOL>for row in self.rows[<NUM_LIT:1>:]:<EOL><INDENT>if len(row) > <NUM_LIT:0> and row[<NUM_LIT:0>] == "<STR_LIT>":<EOL><INDENT>current_statement += row[<NUM_LIT:1>:]<EOL>current_statement.endline = row.linenumber<EOL><DEDENT>else:<EOL><INDENT>if len(current_statement) > <NUM_LIT:0>:<EOL><INDENT>statements.append(current_statement)<EOL><DEDENT>current_statement = Statement(row)<EOL>current_statement.startline = row.linenumber<EOL>current_statement.endline = row.linenumber<EOL><DEDENT><DEDENT>if len(current_statement) > <NUM_LIT:0>:<EOL><INDENT>statements.append(current_statement)<EOL><DEDENT>while (len(statements[-<NUM_LIT:1>]) == <NUM_LIT:0> or <EOL>((len(statements[-<NUM_LIT:1>]) == <NUM_LIT:1>) and len(statements[-<NUM_LIT:1>][<NUM_LIT:0>]) == <NUM_LIT:0>)):<EOL><INDENT>statements.pop()<EOL><DEDENT>return statements<EOL>

Return a list of statements

        This is done by joining together any rows that
        have continuations


if len(row) == <NUM_LIT:0>:<EOL><INDENT>return<EOL><DEDENT>if (row[<NUM_LIT:0>] != "<STR_LIT>" and <EOL>(not row[<NUM_LIT:0>].lstrip().startswith("<STR_LIT:#>"))):<EOL><INDENT>self._children.append(self._childClass(self.parent, row.linenumber, row[<NUM_LIT:0>]))<EOL>if len(row.cells) > <NUM_LIT:1>:<EOL><INDENT>row[<NUM_LIT:0>] = "<STR_LIT>"<EOL>self._children[-<NUM_LIT:1>].append(row.linenumber, row.raw_text, row.cells)<EOL><DEDENT><DEDENT>elif len(self._children) == <NUM_LIT:0>:<EOL><INDENT>self.comments.append(row)<EOL><DEDENT>else:<EOL><INDENT>if len(row.cells) > <NUM_LIT:0>:<EOL><INDENT>self._children[-<NUM_LIT:1>].append(row.linenumber, row.raw_text, row.cells)<EOL><DEDENT><DEDENT>

The idea is, we recognize when we have a new testcase by
checking the first cell. If it's not empty and not a comment,
we have a new test case.


for table in self.parent.tables:<EOL><INDENT>if isinstance(table, SettingTable):<EOL><INDENT>for row in table.rows:<EOL><INDENT>if row[<NUM_LIT:0>].lower() == "<STR_LIT>":<EOL><INDENT>return True<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return False<EOL>

Return True if the test is part of a suite that uses a Test Template


def append(self, linenumber, raw_text, cells):

self.rows.append(Row(linenumber, raw_text, cells))<EOL>

Add another row of data from a test suite


steps = []<EOL>for statement in self.statements:<EOL><INDENT>if ((not statement.is_comment()) and<EOL>(not statement.is_setting())):<EOL><INDENT>steps.append(statement)<EOL><DEDENT><DEDENT>return steps<EOL>

Return a list of steps (statements that are not settings or comments)


return [statement for statement in self.statements if statement.is_setting()]<EOL>

Return a list of settings (statements with cell[1] matching \[.*?\])

        Note: this returns any statement that *looks* like a setting. If you have
        a misspelled or completely bogus setting, it'll return that too
        (eg: | | [Blockumentation] | hello, world)


<EOL>if len(self.rows) == <NUM_LIT:0>:<EOL><INDENT>return []<EOL><DEDENT>current_statement = Statement(self.rows[<NUM_LIT:0>])<EOL>current_statement.startline = self.rows[<NUM_LIT:0>].linenumber<EOL>current_statement.endline = self.rows[<NUM_LIT:0>].linenumber<EOL>statements = []<EOL>for row in self.rows[<NUM_LIT:1>:]:<EOL><INDENT>if len(row) > <NUM_LIT:1> and row[<NUM_LIT:0>] == "<STR_LIT>" and row[<NUM_LIT:1>] == "<STR_LIT>":<EOL><INDENT>current_statement += row[<NUM_LIT:2>:]<EOL>current_statement.endline = row.linenumber<EOL><DEDENT>else:<EOL><INDENT>if len(current_statement) > <NUM_LIT:0>:<EOL><INDENT>statements.append(current_statement)<EOL><DEDENT>current_statement = Statement(row)<EOL>current_statement.startline = row.linenumber<EOL>current_statement.endline = row.linenumber<EOL><DEDENT><DEDENT>if len(current_statement) > <NUM_LIT:0>:<EOL><INDENT>statements.append(current_statement)<EOL><DEDENT>return statements<EOL>


for cell in self[:]:<EOL><INDENT>if cell == "<STR_LIT>":<EOL><INDENT>continue<EOL><DEDENT>if cell.lstrip().startswith("<STR_LIT:#>"):<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>return False<EOL>

Return True if the first non-empty cell starts with "#


<EOL>old_hpp = index.settings['<STR_LIT>'] if '<STR_LIT>' in index.settings else None<EOL>index.settings['<STR_LIT>'] = <NUM_LIT><EOL>index.reindex_all()<EOL>index.settings['<STR_LIT>'] = old_hpp<EOL>time.sleep(<NUM_LIT:10>)  <EOL>index_settings = index.get_settings()<EOL>self.assertEqual(index_settings['<STR_LIT>'], <NUM_LIT>,<EOL>"<STR_LIT>")<EOL>return index_settings<EOL>

Applies a sample setting to the index.

:param index: an AlgoliaIndex that will be updated
:return: the new settings


try:<EOL><INDENT>app_id = settings['<STR_LIT>']<EOL>api_key = settings['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>raise AlgoliaEngineError(<EOL>'<STR_LIT>')<EOL><DEDENT>self.__auto_indexing = settings.get('<STR_LIT>', True)<EOL>self.__settings = settings<EOL>self.__registered_models = {}<EOL>self.client = algoliasearch.Client(app_id, api_key)<EOL>self.client.set_extra_header('<STR_LIT>',<EOL>'<STR_LIT>'<EOL>% (CLIENT_VERSION, python_version(), VERSION, django_version))<EOL>


return model in self.__registered_models<EOL>

Checks whether the given models is registered with Algolia engine


def register(self, model, index_cls=AlgoliaIndex, auto_indexing=None):

<EOL>if self.is_registered(model):<EOL><INDENT>raise RegistrationError(<EOL>'<STR_LIT>'.format(model))<EOL><DEDENT>if not issubclass(index_cls, AlgoliaIndex):<EOL><INDENT>raise RegistrationError(<EOL>'<STR_LIT>'.format(index_cls))<EOL><DEDENT>index_obj = index_cls(model, self.client, self.__settings)<EOL>self.__registered_models[model] = index_obj<EOL>if (isinstance(auto_indexing, bool) and<EOL>auto_indexing) or self.__auto_indexing:<EOL><INDENT>post_save.connect(self.__post_save_receiver, model)<EOL>pre_delete.connect(self.__pre_delete_receiver, model)<EOL>logger.info('<STR_LIT>', model)<EOL><DEDENT>

Registers the given model with Algolia engine.

If the given model is already registered with Algolia engine, a
RegistrationError will be raised.


if not self.is_registered(model):<EOL><INDENT>raise RegistrationError(<EOL>'<STR_LIT>'.format(model))<EOL><DEDENT>del self.__registered_models[model]<EOL>post_save.disconnect(self.__post_save_receiver, model)<EOL>pre_delete.disconnect(self.__pre_delete_receiver, model)<EOL>logger.info('<STR_LIT>', model)<EOL>

Unregisters the given model with Algolia engine.

If the given model is not registered with Algolia engine, a
RegistrationError will be raised.


return list(self.__registered_models.keys())<EOL>

Returns a list of models that have been registered with Algolia
engine.


if not self.is_registered(model):<EOL><INDENT>raise RegistrationError(<EOL>'<STR_LIT>'.format(model))<EOL><DEDENT>return self.__registered_models[model]<EOL>

Returns the adapter associated with the given model.


model = instance.__class__<EOL>return self.get_adapter(model)<EOL>

Returns the adapter associated with the given instance.


def save_record(self, instance, **kwargs):

adapter = self.get_adapter_from_instance(instance)<EOL>adapter.save_record(instance, **kwargs)<EOL>

Saves the record.

        If `update_fields` is set, this method will use partial_update_object()
        and will update only the given fields (never `_geoloc` and `_tags`).

        For more information about partial_update_object:
        https://github.com/algolia/algoliasearch-client-python#update-an-existing-object-in-the-index


adapter = self.get_adapter_from_instance(instance)<EOL>adapter.delete_record(instance)<EOL>


def update_records(self, model, qs, batch_size=<NUM_LIT:1000>, **kwargs):

adapter = self.get_adapter(model)<EOL>adapter.update_records(qs, batch_size=batch_size, **kwargs)<EOL>

Updates multiple records.

This method is optimized for speed. It takes a QuerySet and the same
arguments as QuerySet.update(). Optionally, you can specify the size
of the batch send to Algolia with batch_size (default to 1000).

>>> from algoliasearch_django import update_records
>>> qs = MyModel.objects.filter(myField=False)
>>> update_records(MyModel, qs, myField=True)
>>> qs.update(myField=True)


def raw_search(self, model, query='<STR_LIT>', params=None):

if params is None:<EOL><INDENT>params = {}<EOL><DEDENT>adapter = self.get_adapter(model)<EOL>return adapter.raw_search(query, params)<EOL>

Performs a search query and returns the parsed JSON.


adapter = self.get_adapter(model)<EOL>adapter.clear_index()<EOL>


def reindex_all(self, model, batch_size=<NUM_LIT:1000>):

adapter = self.get_adapter(model)<EOL>return adapter.reindex_all(batch_size)<EOL>

Reindex all the records.

By default, this method use Model.objects.all() but you can implement
a method `get_queryset` in your subclass. This can be used to optimize
the performance (for example with select_related or prefetch_related).


self.__init__(settings=settings if settings is not None else SETTINGS)<EOL>

Reinitializes the Algolia engine and its client.
        :param settings: settings to use instead of the default django.conf.settings.algolia


def __post_save_receiver(self, instance, **kwargs):

logger.debug('<STR_LIT>', instance.__class__)<EOL>self.save_record(instance, **kwargs)<EOL>

Signal handler for when a registered model has been saved.


def __pre_delete_receiver(self, instance, **kwargs):

logger.debug('<STR_LIT>', instance.__class__)<EOL>self.delete_record(instance)<EOL>

Signal handler for when a registered model has been deleted.


batch_size = options.get('<STR_LIT>', None)<EOL>if not batch_size:<EOL><INDENT>batch_size = <NUM_LIT:1000><EOL><DEDENT>self.stdout.write('<STR_LIT>')<EOL>for model in get_registered_model():<EOL><INDENT>if options.get('<STR_LIT>', None) and not (model.__name__ in<EOL>options['<STR_LIT>']):<EOL><INDENT>continue<EOL><DEDENT>counts = reindex_all(model, batch_size=batch_size)<EOL>self.stdout.write('<STR_LIT>'.format(model.__name__, counts))<EOL><DEDENT>


self.stdout.write('<STR_LIT>')<EOL>for model in get_registered_model():<EOL><INDENT>if options.get('<STR_LIT>', None) and not (model.__name__ in<EOL>options['<STR_LIT>']):<EOL><INDENT>continue<EOL><DEDENT>get_adapter(model).set_settings()<EOL>self.stdout.write('<STR_LIT>'.format(model.__name__))<EOL><DEDENT>


self.stdout.write('<STR_LIT>')<EOL>for model in get_registered_model():<EOL><INDENT>if options.get('<STR_LIT>', None) and not (model.__name__ in<EOL>options['<STR_LIT>']):<EOL><INDENT>continue<EOL><DEDENT>clear_index(model)<EOL>self.stdout.write('<STR_LIT>'.format(model.__name__))<EOL><DEDENT>


def __init__(self, model, client, settings):

self.__init_index(client, model, settings)<EOL>self.model = model<EOL>self.__client = client<EOL>self.__named_fields = {}<EOL>self.__translate_fields = {}<EOL>if self.settings is None:  <EOL><INDENT>self.settings = {}<EOL><DEDENT>try:<EOL><INDENT>all_model_fields = [f.name for f in model._meta.get_fields() if not f.is_relation]<EOL><DEDENT>except AttributeError:  <EOL><INDENT>all_model_fields = [f.name for f in model._meta.local_fields]<EOL><DEDENT>if isinstance(self.fields, str):<EOL><INDENT>self.fields = (self.fields,)<EOL><DEDENT>elif isinstance(self.fields, (list, tuple, set)):<EOL><INDENT>self.fields = tuple(self.fields)<EOL><DEDENT>else:<EOL><INDENT>raise AlgoliaIndexError('<STR_LIT>')<EOL><DEDENT>for field in self.fields:<EOL><INDENT>if sys.version_info < (<NUM_LIT:3>, <NUM_LIT:0>) and isinstance(field, unicode) or isinstance(field, str):<EOL><INDENT>attr = field<EOL>name = field<EOL><DEDENT>elif isinstance(field, (list, tuple)) and len(field) == <NUM_LIT:2>:<EOL><INDENT>attr = field[<NUM_LIT:0>]<EOL>name = field[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>raise AlgoliaIndexError(<EOL>'<STR_LIT>'.format(field, type(field)))<EOL><DEDENT>self.__translate_fields[attr] = name<EOL>if attr in all_model_fields:<EOL><INDENT>self.__named_fields[name] = get_model_attr(attr)<EOL><DEDENT>else:<EOL><INDENT>self.__named_fields[name] = check_and_get_attr(model, attr)<EOL><DEDENT><DEDENT>if not self.fields:<EOL><INDENT>self.fields = set(all_model_fields)<EOL>for elt in ('<STR_LIT>', '<STR_LIT:id>', '<STR_LIT>'):<EOL><INDENT>try:<EOL><INDENT>self.fields.remove(elt)<EOL><DEDENT>except KeyError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>self.__translate_fields = dict(zip(self.fields, self.fields))<EOL>self.__named_fields = dict(zip(self.fields, map(get_model_attr,<EOL>self.fields)))<EOL><DEDENT>if self.custom_objectID in chain(['<STR_LIT>'], all_model_fields) or hasattr(model, self.custom_objectID):<EOL><INDENT>self.objectID = get_model_attr(self.custom_objectID)<EOL><DEDENT>else:<EOL><INDENT>raise AlgoliaIndexError('<STR_LIT>'.format(<EOL>self.custom_objectID, model))<EOL><DEDENT>if self.tags:<EOL><INDENT>if self.tags in all_model_fields:<EOL><INDENT>self.tags = get_model_attr(self.tags)<EOL><DEDENT>else:<EOL><INDENT>self.tags = check_and_get_attr(model, self.tags)<EOL><DEDENT><DEDENT>if self.geo_field:<EOL><INDENT>self.geo_field = check_and_get_attr(model, self.geo_field)<EOL><DEDENT>if self.should_index:<EOL><INDENT>if hasattr(model, self.should_index):<EOL><INDENT>attr = getattr(model, self.should_index)<EOL>if type(attr) is not bool:  <EOL><INDENT>self.should_index = attr<EOL><DEDENT>if callable(self.should_index):<EOL><INDENT>self._should_index_is_method = True<EOL><DEDENT><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>model._meta.get_field_by_name(self.should_index)<EOL><DEDENT>except:<EOL><INDENT>raise AlgoliaIndexError('<STR_LIT>'.format(<EOL>self.should_index, model))<EOL><DEDENT><DEDENT><DEDENT>


if set(geolocation) != {'<STR_LIT>', '<STR_LIT>'}:<EOL><INDENT>raise AlgoliaIndexError(<EOL>'<STR_LIT>'.format(<EOL>geolocation<EOL>)<EOL>)<EOL><DEDENT>

Make sure we have the proper geolocation format.


def get_raw_record(self, instance, update_fields=None):

tmp = {'<STR_LIT>': self.objectID(instance)}<EOL>if update_fields:<EOL><INDENT>if isinstance(update_fields, str):<EOL><INDENT>update_fields = (update_fields,)<EOL><DEDENT>for elt in update_fields:<EOL><INDENT>key = self.__translate_fields.get(elt, None)<EOL>if key:<EOL><INDENT>tmp[key] = self.__named_fields[key](instance)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>for key, value in self.__named_fields.items():<EOL><INDENT>tmp[key] = value(instance)<EOL><DEDENT>if self.geo_field:<EOL><INDENT>loc = self.geo_field(instance)<EOL>if isinstance(loc, tuple):<EOL><INDENT>tmp['<STR_LIT>'] = {'<STR_LIT>': loc[<NUM_LIT:0>], '<STR_LIT>': loc[<NUM_LIT:1>]}<EOL><DEDENT>elif isinstance(loc, dict):<EOL><INDENT>self._validate_geolocation(loc)<EOL>tmp['<STR_LIT>'] = loc<EOL><DEDENT>elif isinstance(loc, list):<EOL><INDENT>[self._validate_geolocation(geo) for geo in loc]<EOL>tmp['<STR_LIT>'] = loc<EOL><DEDENT><DEDENT>if self.tags:<EOL><INDENT>if callable(self.tags):<EOL><INDENT>tmp['<STR_LIT>'] = self.tags(instance)<EOL><DEDENT>if not isinstance(tmp['<STR_LIT>'], list):<EOL><INDENT>tmp['<STR_LIT>'] = list(tmp['<STR_LIT>'])<EOL><DEDENT><DEDENT><DEDENT>logger.debug('<STR_LIT>', tmp['<STR_LIT>'], self.model)<EOL>return tmp<EOL>

Gets the raw record.

If `update_fields` is set, the raw record will be build with only
the objectID and the given fields. Also, `_geoloc` and `_tags` will
not be included.


return self.should_index is not None<EOL>

Return True if this AlgoliaIndex has a should_index method or attribute


if self._has_should_index():<EOL><INDENT>return self._should_really_index(instance)<EOL><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT>

Return True if the object should be indexed (including when self.should_index is not set).


if self._should_index_is_method:<EOL><INDENT>is_method = inspect.ismethod(self.should_index)<EOL>try:<EOL><INDENT>count_args = len(inspect.signature(self.should_index).parameters)<EOL><DEDENT>except AttributeError:<EOL><INDENT>count_args = len(inspect.getargspec(self.should_index).args)<EOL><DEDENT>if is_method or count_args is <NUM_LIT:1>:<EOL><INDENT>return self.should_index(instance)<EOL><DEDENT>else:<EOL><INDENT>return self.should_index()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>attr_type = type(self.should_index)<EOL>if attr_type is DeferredAttribute:<EOL><INDENT>attr_value = self.should_index.__get__(instance, None)<EOL><DEDENT>elif attr_type is str:<EOL><INDENT>attr_value = getattr(instance, self.should_index)<EOL><DEDENT>elif attr_type is property:<EOL><INDENT>attr_value = self.should_index.__get__(instance)<EOL><DEDENT>else:<EOL><INDENT>raise AlgoliaIndexError('<STR_LIT>'.format(<EOL>self.should_index))<EOL><DEDENT>if type(attr_value) is not bool:<EOL><INDENT>raise AlgoliaIndexError("<STR_LIT>" % (<EOL>instance.__class__.__name__, self.should_index))<EOL><DEDENT>return attr_value<EOL><DEDENT>

Return True if according to should_index the object should be indexed.


def save_record(self, instance, update_fields=None, **kwargs):

if not self._should_index(instance):<EOL><INDENT>self.delete_record(instance)<EOL>return<EOL><DEDENT>try:<EOL><INDENT>if update_fields:<EOL><INDENT>obj = self.get_raw_record(instance,<EOL>update_fields=update_fields)<EOL>result = self.__index.partial_update_object(obj)<EOL><DEDENT>else:<EOL><INDENT>obj = self.get_raw_record(instance)<EOL>result = self.__index.save_object(obj)<EOL><DEDENT>logger.info('<STR_LIT>', obj['<STR_LIT>'], self.model)<EOL>return result<EOL><DEDENT>except AlgoliaException as e:<EOL><INDENT>if DEBUG:<EOL><INDENT>raise e<EOL><DEDENT>else:<EOL><INDENT>logger.warning('<STR_LIT>', obj['<STR_LIT>'],<EOL>self.model, e)<EOL><DEDENT><DEDENT>


objectID = self.objectID(instance)<EOL>try:<EOL><INDENT>self.__index.delete_object(objectID)<EOL>logger.info('<STR_LIT>', objectID, self.model)<EOL><DEDENT>except AlgoliaException as e:<EOL><INDENT>if DEBUG:<EOL><INDENT>raise e<EOL><DEDENT>else:<EOL><INDENT>logger.warning('<STR_LIT>', objectID,<EOL>self.model, e)<EOL><DEDENT><DEDENT>


def update_records(self, qs, batch_size=<NUM_LIT:1000>, **kwargs):

tmp = {}<EOL>for key, value in kwargs.items():<EOL><INDENT>name = self.__translate_fields.get(key, None)<EOL>if name:<EOL><INDENT>tmp[name] = value<EOL><DEDENT><DEDENT>batch = []<EOL>objectsIDs = qs.only(self.custom_objectID).values_list(<EOL>self.custom_objectID, flat=True)<EOL>for elt in objectsIDs:<EOL><INDENT>tmp['<STR_LIT>'] = elt<EOL>batch.append(dict(tmp))<EOL>if len(batch) >= batch_size:<EOL><INDENT>self.__index.partial_update_objects(batch)<EOL>batch = []<EOL><DEDENT><DEDENT>if len(batch) > <NUM_LIT:0>:<EOL><INDENT>self.__index.partial_update_objects(batch)<EOL><DEDENT>

Updates multiple records.

This method is optimized for speed. It takes a QuerySet and the same
arguments as QuerySet.update(). Optionnaly, you can specify the size
of the batch send to Algolia with batch_size (default to 1000).

>>> from algoliasearch_django import update_records
>>> qs = MyModel.objects.filter(myField=False)
>>> update_records(MyModel, qs, myField=True)
>>> qs.update(myField=True)


def raw_search(self, query='<STR_LIT>', params=None):

if params is None:<EOL><INDENT>params = {}<EOL><DEDENT>try:<EOL><INDENT>return self.__index.search(query, params)<EOL><DEDENT>except AlgoliaException as e:<EOL><INDENT>if DEBUG:<EOL><INDENT>raise e<EOL><DEDENT>else:<EOL><INDENT>logger.warning('<STR_LIT>', self.index_name, e)<EOL><DEDENT><DEDENT>


try:<EOL><INDENT>logger.info('<STR_LIT>', self.index_name)<EOL>return self.__index.get_settings()<EOL><DEDENT>except AlgoliaException as e:<EOL><INDENT>if DEBUG:<EOL><INDENT>raise e<EOL><DEDENT>else:<EOL><INDENT>logger.warning('<STR_LIT>',<EOL>self.model, e)<EOL><DEDENT><DEDENT>


if not self.settings:<EOL><INDENT>return<EOL><DEDENT>try:<EOL><INDENT>self.__index.set_settings(self.settings)<EOL>logger.info('<STR_LIT>', self.index_name)<EOL><DEDENT>except AlgoliaException as e:<EOL><INDENT>if DEBUG:<EOL><INDENT>raise e<EOL><DEDENT>else:<EOL><INDENT>logger.warning('<STR_LIT>',<EOL>self.model, e)<EOL><DEDENT><DEDENT>


try:<EOL><INDENT>self.__index.clear_index()<EOL>logger.info('<STR_LIT>', self.index_name)<EOL><DEDENT>except AlgoliaException as e:<EOL><INDENT>if DEBUG:<EOL><INDENT>raise e<EOL><DEDENT>else:<EOL><INDENT>logger.warning('<STR_LIT>', self.model, e)<EOL><DEDENT><DEDENT>


should_keep_synonyms = False<EOL>should_keep_rules = False<EOL>try:<EOL><INDENT>if not self.settings:<EOL><INDENT>self.settings = self.get_settings()<EOL>logger.debug('<STR_LIT>', self.index_name, self.settings)<EOL><DEDENT>else:<EOL><INDENT>logger.debug("<STR_LIT>", self.index_name, self.settings)<EOL><DEDENT><DEDENT>except AlgoliaException as e:<EOL><INDENT>if any("<STR_LIT>" in arg for arg in e.args):<EOL><INDENT>pass  <EOL><DEDENT>else:<EOL><INDENT>raise e  <EOL><DEDENT><DEDENT>try:<EOL><INDENT>if self.settings:<EOL><INDENT>replicas = self.settings.get('<STR_LIT>', None)<EOL>slaves = self.settings.get('<STR_LIT>', None)<EOL>should_keep_replicas = replicas is not None<EOL>should_keep_slaves = slaves is not None<EOL>if should_keep_replicas:<EOL><INDENT>self.settings['<STR_LIT>'] = []<EOL>logger.debug("<STR_LIT>")<EOL><DEDENT>if should_keep_slaves:<EOL><INDENT>self.settings['<STR_LIT>'] = []<EOL>logger.debug("<STR_LIT>")<EOL><DEDENT>self.__tmp_index.wait_task(self.__tmp_index.set_settings(self.settings)['<STR_LIT>'])<EOL>logger.debug('<STR_LIT>', self.index_name)<EOL><DEDENT>rules = []<EOL>synonyms = []<EOL>for r in self.__index.iter_rules():<EOL><INDENT>rules.append(r)<EOL><DEDENT>for s in self.__index.iter_synonyms():<EOL><INDENT>synonyms.append(s)<EOL><DEDENT>if len(rules):<EOL><INDENT>logger.debug('<STR_LIT>', self.index_name, rules)<EOL>should_keep_rules = True<EOL><DEDENT>if len(synonyms):<EOL><INDENT>logger.debug('<STR_LIT>', self.index_name, rules)<EOL>should_keep_synonyms = True<EOL><DEDENT>self.__tmp_index.clear_index()<EOL>logger.debug('<STR_LIT>', self.index_name)<EOL>counts = <NUM_LIT:0><EOL>batch = []<EOL>if hasattr(self, '<STR_LIT>'):<EOL><INDENT>qs = self.get_queryset()<EOL><DEDENT>else:<EOL><INDENT>qs = self.model.objects.all()<EOL><DEDENT>for instance in qs:<EOL><INDENT>if not self._should_index(instance):<EOL><INDENT>continue  <EOL><DEDENT>batch.append(self.get_raw_record(instance))<EOL>if len(batch) >= batch_size:<EOL><INDENT>self.__tmp_index.save_objects(batch)<EOL>logger.info('<STR_LIT>', len(batch),<EOL>self.index_name)<EOL>batch = []<EOL><DEDENT>counts += <NUM_LIT:1><EOL><DEDENT>if len(batch) > <NUM_LIT:0>:<EOL><INDENT>self.__tmp_index.save_objects(batch)<EOL>logger.info('<STR_LIT>', len(batch),<EOL>self.index_name)<EOL><DEDENT>self.__client.move_index(self.__tmp_index.index_name,<EOL>self.__index.index_name)<EOL>logger.info('<STR_LIT>', self.index_name,<EOL>self.index_name)<EOL>if self.settings:<EOL><INDENT>if should_keep_replicas:<EOL><INDENT>self.settings['<STR_LIT>'] = replicas<EOL>logger.debug("<STR_LIT>")<EOL><DEDENT>if should_keep_slaves:<EOL><INDENT>self.settings['<STR_LIT>'] = slaves<EOL>logger.debug("<STR_LIT>")<EOL><DEDENT>if should_keep_replicas or should_keep_slaves:<EOL><INDENT>self.__index.set_settings(self.settings)<EOL><DEDENT>if should_keep_rules:<EOL><INDENT>response = self.__index.batch_rules(rules, forward_to_replicas=True)<EOL>self.__index.wait_task(response['<STR_LIT>'])<EOL>logger.info("<STR_LIT>".format(response), self.index_name)<EOL><DEDENT>if should_keep_synonyms:<EOL><INDENT>response = self.__index.batch_synonyms(synonyms, forward_to_replicas=True)<EOL>self.__index.wait_task(response['<STR_LIT>'])<EOL>logger.info("<STR_LIT>".format(response), self.index_name)<EOL><DEDENT><DEDENT>return counts<EOL><DEDENT>except AlgoliaException as e:<EOL><INDENT>if DEBUG:<EOL><INDENT>raise e<EOL><DEDENT>else:<EOL><INDENT>logger.warning('<STR_LIT>', self.model,<EOL>e)<EOL><DEDENT><DEDENT>


return WRAPPER_ASSIGNMENTS<EOL>

Return the list of functools-wrappable attributes on a callable.
This was required as a workaround for http://bugs.python.org/issue3445
under Python 2.


from algoliasearch_django import AlgoliaIndex, register<EOL>def _algolia_engine_wrapper(index_class):<EOL><INDENT>if not issubclass(index_class, AlgoliaIndex):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>register(model, index_class)<EOL>return index_class<EOL><DEDENT>return _algolia_engine_wrapper<EOL>

Register the given model class and wrapped AlgoliaIndex class with the Algolia engine:

@register(Author)
class AuthorIndex(AlgoliaIndex):
    pass


text = self.render(files=False)<EOL>vpn_instances = vpn_pattern.split(text)<EOL>if '<STR_LIT>' in vpn_instances:<EOL><INDENT>vpn_instances.remove('<STR_LIT>')<EOL><DEDENT>for vpn in vpn_instances:<EOL><INDENT>lines = vpn.split('<STR_LIT:\n>')<EOL>vpn_name = lines[<NUM_LIT:0>]<EOL>text_contents = '<STR_LIT:\n>'.join(lines[<NUM_LIT:2>:])<EOL>if text_contents.endswith('<STR_LIT>'):<EOL><INDENT>text_contents = text_contents[<NUM_LIT:0>:-<NUM_LIT:1>]<EOL><DEDENT>self._add_file(tar=tar,<EOL>name='<STR_LIT>'.format(vpn_name, config_suffix),<EOL>contents=text_contents)<EOL><DEDENT>

Adds configuration files to tarfile instance.

:param tar: tarfile instance
:returns: None


@classmethod<EOL><INDENT>def auto_client(cls, host, server, ca_path=None, ca_contents=None,<EOL>cert_path=None, cert_contents=None, key_path=None,<EOL>key_contents=None):<DEDENT>

<EOL>client = {<EOL>"<STR_LIT>": "<STR_LIT>",<EOL>"<STR_LIT>": True,<EOL>"<STR_LIT>": "<STR_LIT>",<EOL>"<STR_LIT>": True<EOL>}<EOL>port = server.get('<STR_LIT:port>') or <NUM_LIT><EOL>client['<STR_LIT>'] = [{'<STR_LIT:host>': host, '<STR_LIT:port>': port}]<EOL>if server.get('<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>client['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>client['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>if '<STR_LIT>' in server or '<STR_LIT>' in server:<EOL><INDENT>client['<STR_LIT>'] = True<EOL><DEDENT>if '<STR_LIT>' not in server or not server['<STR_LIT>']:<EOL><INDENT>client['<STR_LIT>'] = False<EOL><DEDENT>ns_cert_type = {None: '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>'}<EOL>client['<STR_LIT>'] = ns_cert_type[server.get('<STR_LIT>')]<EOL>remote_cert_tls = {None: '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>'}<EOL>client['<STR_LIT>'] = remote_cert_tls[server.get('<STR_LIT>')]<EOL>copy_keys = ['<STR_LIT:name>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT:key>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT:user>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT:float>', '<STR_LIT>', '<STR_LIT>']<EOL>for key in copy_keys:<EOL><INDENT>if key in server:<EOL><INDENT>client[key] = server[key]<EOL><DEDENT><DEDENT>files = cls._auto_client_files(client, ca_path, ca_contents,<EOL>cert_path, cert_contents,<EOL>key_path, key_contents)<EOL>return {<EOL>'<STR_LIT>': [client],<EOL>'<STR_LIT>': files<EOL>}<EOL>

Returns a configuration dictionary representing an OpenVPN client configuration
that is compatible with the passed server configuration.

:param host: remote VPN server
:param server: dictionary representing a single OpenVPN server configuration
:param ca_path: optional string representing path to CA, will consequently add
                a file in the resulting configuration dictionary
:param ca_contents: optional string representing contents of CA file
:param cert_path: optional string representing path to certificate, will consequently add
                a file in the resulting configuration dictionary
:param cert_contents: optional string representing contents of cert file
:param key_path: optional string representing path to key, will consequently add
                a file in the resulting configuration dictionary
:param key_contents: optional string representing contents of key file
:returns: dictionary representing a single OpenVPN client configuration


@classmethod<EOL><INDENT>def _auto_client_files(cls, client, ca_path=None, ca_contents=None, cert_path=None,<EOL>cert_contents=None, key_path=None, key_contents=None):<DEDENT>

files = []<EOL>if ca_path and ca_contents:<EOL><INDENT>client['<STR_LIT>'] = ca_path<EOL>files.append(dict(path=ca_path,<EOL>contents=ca_contents,<EOL>mode=DEFAULT_FILE_MODE))<EOL><DEDENT>if cert_path and cert_contents:<EOL><INDENT>client['<STR_LIT>'] = cert_path<EOL>files.append(dict(path=cert_path,<EOL>contents=cert_contents,<EOL>mode=DEFAULT_FILE_MODE))<EOL><DEDENT>if key_path and key_contents:<EOL><INDENT>client['<STR_LIT:key>'] = key_path<EOL>files.append(dict(path=key_path,<EOL>contents=key_contents,<EOL>mode=DEFAULT_FILE_MODE,))<EOL><DEDENT>return files<EOL>

returns a list of NetJSON extra files for automatically generated clients
produces side effects in ``client`` dictionary


return str(cls.__name__).replace('<STR_LIT>', '<STR_LIT>').lower()<EOL>

Returns the name of the render class without its prefix


return output<EOL>

Performs cleanup of output (indentation, new lines)

:param output: string representation of the client configuration


<EOL>template_name = '<STR_LIT>'.format(self.get_name())<EOL>template = self.template_env.get_template(template_name)<EOL>context = getattr(self.backend, '<STR_LIT>', {})<EOL>output = template.render(data=context)<EOL>return self.cleanup(output)<EOL>

Renders configuration by using the jinja2 templating engine


def __init__(self, config=None, native=None, templates=None, context=None):

<EOL>self.config = None<EOL>self.intermediate_data = None<EOL>if config is not None:<EOL><INDENT>config = deepcopy(self._load(config))<EOL>self.config = self._merge_config(config, templates)<EOL>self.config = self._evaluate_vars(self.config, context)<EOL><DEDENT>elif native is not None:<EOL><INDENT>self.parse(native)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>

:param config: ``dict`` containing a valid **NetJSON** configuration dictionary
:param native: ``str`` or file object representing a native configuration that will
               be parsed and converted to a **NetJSON** configuration dictionary
:param templates: ``list`` containing **NetJSON** configuration dictionaries that
                  will be used as a base for the main config
:param context: ``dict`` containing configuration variables
:raises TypeError: raised if ``config`` is not of type ``dict`` or if
                   ``templates`` is not of type ``list``


if isinstance(config, six.string_types):<EOL><INDENT>try:<EOL><INDENT>config = json.loads(config)<EOL><DEDENT>except ValueError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if not isinstance(config, dict):<EOL><INDENT>raise TypeError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>return config<EOL>


def _merge_config(self, config, templates):

if not templates:<EOL><INDENT>return config<EOL><DEDENT>if not isinstance(templates, list):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>result = {}<EOL>config_list = templates + [config]<EOL>for merging in config_list:<EOL><INDENT>result = merge_config(result, self._load(merging), self.list_identifiers)<EOL><DEDENT>return result<EOL>


def _evaluate_vars(self, config, context):

<EOL>if not context:<EOL><INDENT>return config<EOL><DEDENT>return evaluate_vars(config, context)<EOL>


output = '<STR_LIT>'<EOL>files = self.config.get('<STR_LIT>', [])<EOL>if files:<EOL><INDENT>output += '<STR_LIT>'.format(self.FILE_SECTION_DELIMITER)<EOL><DEDENT>for f in files:<EOL><INDENT>mode = f.get('<STR_LIT>', DEFAULT_FILE_MODE)<EOL>file_output = '<STR_LIT>''<STR_LIT>''<STR_LIT>'.format(f['<STR_LIT:path>'], mode, f['<STR_LIT>'])<EOL>output += file_output<EOL><DEDENT>return output<EOL>

Renders additional files specified in ``self.config['files']``


self.validate()<EOL>if self.intermediate_data is None:<EOL><INDENT>self.to_intermediate()<EOL><DEDENT>renderers = getattr(self, '<STR_LIT>', None) or [self.renderer]<EOL>output = '<STR_LIT>'<EOL>for renderer_class in renderers:<EOL><INDENT>renderer = renderer_class(self)<EOL>output += renderer.render()<EOL>del renderer<EOL><DEDENT>if files:<EOL><INDENT>files_output = self._render_files()<EOL>if files_output:<EOL><INDENT>output += files_output.replace('<STR_LIT>', '<STR_LIT>')<EOL><DEDENT><DEDENT>return output<EOL>

Converts the configuration dictionary into the corresponding configuration format

:param files: whether to include "additional files" in the output or not;
              defaults to ``True``
:returns: string with output


def json(self, validate=True, *args, **kwargs):

if validate:<EOL><INDENT>self.validate()<EOL><DEDENT>config = deepcopy(self.config)<EOL>config.update({'<STR_LIT:type>': '<STR_LIT>'})<EOL>return json.dumps(config, *args, **kwargs)<EOL>

returns a string formatted as **NetJSON DeviceConfiguration**;
performs validation before returning output;

``*args`` and ``*kwargs`` will be passed to ``json.dumps``;

:returns: string


tar_bytes = BytesIO()<EOL>tar = tarfile.open(fileobj=tar_bytes, mode='<STR_LIT:w>')<EOL>self._generate_contents(tar)<EOL>self._process_files(tar)<EOL>tar.close()<EOL>tar_bytes.seek(<NUM_LIT:0>)  <EOL>gzip_bytes = BytesIO()<EOL>gz = gzip.GzipFile(fileobj=gzip_bytes, mode='<STR_LIT:wb>', mtime=<NUM_LIT:0>)<EOL>gz.write(tar_bytes.getvalue())<EOL>gz.close()<EOL>gzip_bytes.seek(<NUM_LIT:0>)  <EOL>return gzip_bytes<EOL>

Returns a ``BytesIO`` instance representing an in-memory tar.gz archive
containing the native router configuration.

:returns: in-memory tar.gz archive, instance of ``BytesIO``


def write(self, name, path='<STR_LIT>'):

byte_object = self.generate()<EOL>file_name = '<STR_LIT>'.format(name)<EOL>if not path.endswith('<STR_LIT:/>'):<EOL><INDENT>path += '<STR_LIT:/>'<EOL><DEDENT>f = open('<STR_LIT>'.format(path, file_name), '<STR_LIT:wb>')<EOL>f.write(byte_object.getvalue())<EOL>f.close()<EOL>

Like ``generate`` but writes to disk.

:param name: file name, the tar.gz extension will be added automatically
:param path: directory where the file will be written to, defaults to ``./``
:returns: None


<EOL>for file_item in self.config.get('<STR_LIT>', []):<EOL><INDENT>path = file_item['<STR_LIT:path>']<EOL>if path.startswith('<STR_LIT:/>'):<EOL><INDENT>path = path[<NUM_LIT:1>:]<EOL><DEDENT>self._add_file(tar=tar,<EOL>name=path,<EOL>contents=file_item['<STR_LIT>'],<EOL>mode=file_item.get('<STR_LIT>', DEFAULT_FILE_MODE))<EOL><DEDENT>

Adds files specified in self.config['files'] to tarfile instance.

:param tar: tarfile instance
:returns: None


def _add_file(self, tar, name, contents, mode=DEFAULT_FILE_MODE):

byte_contents = BytesIO(contents.encode('<STR_LIT:utf8>'))<EOL>info = tarfile.TarInfo(name=name)<EOL>info.size = len(contents)<EOL>info.mtime = <NUM_LIT:0><EOL>info.type = tarfile.REGTYPE<EOL>info.mode = int(mode, <NUM_LIT:8>)  <EOL>tar.addfile(tarinfo=info, fileobj=byte_contents)<EOL>

Adds a single file in tarfile instance.

:param tar: tarfile instance
:param name: string representing filename or path
:param contents: string representing file contents
:param mode: string representing file mode, defaults to 644
:returns: None


self.validate()<EOL>self.intermediate_data = OrderedDict()<EOL>for converter_class in self.converters:<EOL><INDENT>if not converter_class.should_run_forward(self.config):<EOL><INDENT>continue<EOL><DEDENT>converter = converter_class(self)<EOL>value = converter.to_intermediate()<EOL>if value and isinstance(value, (tuple, list)):  <EOL><INDENT>value = OrderedDict(value)<EOL><DEDENT>if value:<EOL><INDENT>self.intermediate_data = merge_config(self.intermediate_data,<EOL>value,<EOL>list_identifiers=['<STR_LIT>'])<EOL><DEDENT><DEDENT>

Converts the NetJSON configuration dictionary (self.config)
to the intermediate data structure (self.intermediate_data) that will
be then used by the renderer class to generate the router configuration


if not hasattr(self, '<STR_LIT>') or not self.parser:<EOL><INDENT>raise NotImplementedError('<STR_LIT>')<EOL><DEDENT>parser = self.parser(native)<EOL>self.intermediate_data = parser.intermediate_data<EOL>del parser<EOL>self.to_netjson()<EOL>

Parses a native configuration and converts
it to a NetJSON configuration dictionary


self.__backup_intermediate_data()<EOL>self.config = OrderedDict()<EOL>for converter_class in self.converters:<EOL><INDENT>if not converter_class.should_run_backward(self.intermediate_data):<EOL><INDENT>continue<EOL><DEDENT>converter = converter_class(self)<EOL>value = converter.to_netjson()<EOL>if value:<EOL><INDENT>self.config = merge_config(self.config,<EOL>value,<EOL>list_identifiers=self.list_identifiers)<EOL><DEDENT><DEDENT>self.__restore_intermediate_data()<EOL>self.validate()<EOL>

Converts the intermediate data structure (self.intermediate_data)
to the NetJSON configuration dictionary (self.config)


return cls.netjson_key in config<EOL>

Returns True if Converter should be instantiated and run
during the forward conversion process (NetJSON to native)


return cls.intermediate_key in intermediate_data<EOL>

Returns True if Converter should be instantiated and run
during the backward conversion process (native to NetJSON)


if schema is None:<EOL><INDENT>schema = self._schema<EOL><DEDENT>properties = schema['<STR_LIT>']<EOL>for key, value in item.items():<EOL><INDENT>if key not in properties:<EOL><INDENT>continue<EOL><DEDENT>try:<EOL><INDENT>json_type = properties[key]['<STR_LIT:type>']<EOL><DEDENT>except KeyError:<EOL><INDENT>json_type = None<EOL><DEDENT>if json_type == '<STR_LIT>' and not isinstance(value, int):<EOL><INDENT>value = int(value)<EOL><DEDENT>elif json_type == '<STR_LIT>' and not isinstance(value, bool):<EOL><INDENT>value = value == '<STR_LIT:1>'<EOL><DEDENT>item[key] = value<EOL><DEDENT>return item<EOL>

Loops over item and performs type casting
according to supplied schema fragment


result = OrderedDict()<EOL>netjson = get_copy(self.netjson, self.netjson_key)<EOL>if isinstance(netjson, list):<EOL><INDENT>for index, block in enumerate(netjson):<EOL><INDENT>result = self.to_intermediate_loop(block, result, index + <NUM_LIT:1>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>result = self.to_intermediate_loop(netjson, result)<EOL><DEDENT>return result<EOL>

Converts the NetJSON configuration dictionary (``self.config``)
to intermediate data structure (``self.intermediate_datra``)


def to_intermediate_loop(self, block, result, index=None):

raise NotImplementedError()<EOL>

Utility method called in the loop of ``to_intermediate``


result = OrderedDict()<EOL>intermediate_data = list(self.intermediate_data[self.intermediate_key])<EOL>for index, block in enumerate(intermediate_data):<EOL><INDENT>if self.should_skip_block(block):<EOL><INDENT>continue<EOL><DEDENT>if remove_block:<EOL><INDENT>self.intermediate_data[self.intermediate_key].remove(block)<EOL><DEDENT>result = self.to_netjson_loop(block, result, index + <NUM_LIT:1>)<EOL><DEDENT>return result<EOL>

Converts the intermediate data structure (``self.intermediate_datra``)
to a NetJSON configuration dictionary (``self.config``)


def to_netjson_loop(self, block, result, index=None):

raise NotImplementedError()<EOL>

Utility method called in the loop of ``to_netjson``


for radio in self.config.get('<STR_LIT>', []):<EOL><INDENT>radio.setdefault('<STR_LIT>', False)<EOL><DEDENT>

OpenWisp 1.x requires the following explicit entry
in the radio sections of /uci/wireless.conf:
    option disabled '0'


if item not in self.config['<STR_LIT>']:<EOL><INDENT>self.config['<STR_LIT>'].append(item)<EOL><DEDENT>

adds a file in self.config['files'] only if not present already


config = self.config<EOL>l2vpn = []<EOL>for vpn in self.config.get('<STR_LIT>', []):<EOL><INDENT>if vpn.get('<STR_LIT>') != '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>tap = vpn.copy()<EOL>l2vpn.append(tap)<EOL><DEDENT>bridges = []<EOL>for interface in self.config.get('<STR_LIT>', []):<EOL><INDENT>if interface['<STR_LIT:type>'] != '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>bridge = interface.copy()<EOL>if bridge.get('<STR_LIT>'):<EOL><INDENT>bridge['<STR_LIT>'] = interface['<STR_LIT>'][<NUM_LIT:0>].get('<STR_LIT>')<EOL>bridge['<STR_LIT>'] = interface['<STR_LIT>'][<NUM_LIT:0>].get('<STR_LIT:address>')<EOL><DEDENT>bridges.append(bridge)<EOL><DEDENT>cron = False<EOL>for _file in config.get('<STR_LIT>', []):<EOL><INDENT>path = _file['<STR_LIT:path>']<EOL>if path.startswith('<STR_LIT>') or path.startswith('<STR_LIT>'):<EOL><INDENT>cron = True<EOL>break<EOL><DEDENT><DEDENT>return dict(hostname=config['<STR_LIT>']['<STR_LIT>'],  <EOL>l2vpn=l2vpn,<EOL>bridges=bridges,<EOL>radios=config.get('<STR_LIT>', []),  <EOL>cron=cron)<EOL>

returns the template context for install.sh and uninstall.sh


contents = self._render_template('<STR_LIT>', context)<EOL>self.config.setdefault('<STR_LIT>', [])  <EOL>self._add_unique_file({<EOL>"<STR_LIT:path>": "<STR_LIT>",<EOL>"<STR_LIT>": contents,<EOL>"<STR_LIT>": "<STR_LIT>"<EOL>})<EOL>

generates install.sh and adds it to included files


generates uninstall.sh and adds it to included files


<EOL>context = dict(tc_options=self.config.get('<STR_LIT>', []))<EOL>contents = self._render_template('<STR_LIT>', context)<EOL>self.config.setdefault('<STR_LIT>', [])  <EOL>self._add_unique_file({<EOL>"<STR_LIT:path>": "<STR_LIT>",<EOL>"<STR_LIT>": contents,<EOL>"<STR_LIT>": "<STR_LIT>"<EOL>})<EOL>

generates tc_script.sh and adds it to included files


uci = self.render(files=False)<EOL>packages = re.split('<STR_LIT>', uci)<EOL>if '<STR_LIT>' in packages:<EOL><INDENT>packages.remove('<STR_LIT>')<EOL><DEDENT>for package in packages:<EOL><INDENT>lines = package.split('<STR_LIT:\n>')<EOL>package_name = lines[<NUM_LIT:0>]<EOL>text_contents = '<STR_LIT:\n>'.join(lines[<NUM_LIT:2>:])<EOL>text_contents = '<STR_LIT>'.format(package_name, text_contents)<EOL>self._add_file(tar=tar,<EOL>name='<STR_LIT>'.format(package_name),<EOL>contents=text_contents)<EOL><DEDENT>template_context = self._get_install_context()<EOL>self._add_install(template_context)<EOL>self._add_uninstall(template_context)<EOL>self._add_openvpn_scripts()<EOL>self._add_tc_script()<EOL>

Adds configuration files to tarfile instance.

:param tar: tarfile instance
:returns: None


<EOL>output = output.replace('<STR_LIT:U+0020>', '<STR_LIT>').replace('<STR_LIT>', '<STR_LIT>').replace('<STR_LIT>', '<STR_LIT>')<EOL>output = output.replace('<STR_LIT:True>', '<STR_LIT:1>').replace('<STR_LIT:False>', '<STR_LIT:0>')<EOL>output = output.replace('<STR_LIT>', '<STR_LIT>')<EOL>if output.endswith('<STR_LIT>'):<EOL><INDENT>return output[<NUM_LIT:0>:-<NUM_LIT:1>]<EOL><DEDENT>return output<EOL>

Generates consistent OpenWRT/LEDE UCI output


return True<EOL>


address_list = self.get_copy(interface, '<STR_LIT>')<EOL>if not address_list:<EOL><INDENT>return [{'<STR_LIT>': '<STR_LIT:none>'}]<EOL><DEDENT>result = []<EOL>static = {}<EOL>dhcp = []<EOL>for address in address_list:<EOL><INDENT>family = address.get('<STR_LIT>')<EOL>if address['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>address['<STR_LIT>'] = '<STR_LIT>' if family == '<STR_LIT>' else '<STR_LIT>'<EOL>dhcp.append(self.__intermediate_address(address))<EOL>continue<EOL><DEDENT>if '<STR_LIT>' in address:<EOL><INDENT>uci_key = '<STR_LIT>' if family == '<STR_LIT>' else '<STR_LIT>'<EOL>interface[uci_key] = address['<STR_LIT>']<EOL><DEDENT>address_key = '<STR_LIT>' if family == '<STR_LIT>' else '<STR_LIT>'<EOL>static.setdefault(address_key, [])<EOL>static[address_key].append('<STR_LIT>'.format(**address))<EOL>static.update(self.__intermediate_address(address))<EOL><DEDENT>if static:<EOL><INDENT>if len(static.get('<STR_LIT>', [])) == <NUM_LIT:1>:<EOL><INDENT>network = ip_interface(six.text_type(static['<STR_LIT>'][<NUM_LIT:0>]))<EOL>static['<STR_LIT>'] = str(network.ip)<EOL>static['<STR_LIT>'] = str(network.netmask)<EOL><DEDENT>if len(static.get('<STR_LIT>', [])) == <NUM_LIT:1>:<EOL><INDENT>static['<STR_LIT>'] = static['<STR_LIT>'][<NUM_LIT:0>]<EOL><DEDENT>result.append(static)<EOL><DEDENT>if dhcp:<EOL><INDENT>result += dhcp<EOL><DEDENT>return result<EOL>

converts NetJSON address to
UCI intermediate data structure


def __intermediate_interface(self, interface, uci_name):

interface.update({<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': uci_name,<EOL>'<STR_LIT>': interface.pop('<STR_LIT:name>')<EOL>})<EOL>if '<STR_LIT>' in interface:<EOL><INDENT>del interface['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in interface:<EOL><INDENT>if interface.get('<STR_LIT:type>') != '<STR_LIT>':<EOL><INDENT>interface['<STR_LIT>'] = interface['<STR_LIT>']<EOL><DEDENT>del interface['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in interface:<EOL><INDENT>interface['<STR_LIT>'] = interface['<STR_LIT>']<EOL>del interface['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in interface:<EOL><INDENT>interface['<STR_LIT>'] = not interface['<STR_LIT>']<EOL>del interface['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in interface:<EOL><INDENT>del interface['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in interface:<EOL><INDENT>del interface['<STR_LIT>']<EOL><DEDENT>return interface<EOL>

converts NetJSON interface to
UCI intermediate data structure


for key in self._address_keys:<EOL><INDENT>if key in address:<EOL><INDENT>del address[key]<EOL><DEDENT><DEDENT>return address<EOL>


def __intermediate_bridge(self, interface, i):

<EOL>if interface['<STR_LIT:type>'] == '<STR_LIT>' and i < <NUM_LIT:2>:<EOL><INDENT>bridge_members = '<STR_LIT:U+0020>'.join(interface.pop('<STR_LIT>'))<EOL>if bridge_members:<EOL><INDENT>interface['<STR_LIT>'] = bridge_members<EOL><DEDENT>else:<EOL><INDENT>interface['<STR_LIT>'] = True<EOL>del interface['<STR_LIT>']<EOL><DEDENT><DEDENT>elif interface['<STR_LIT:type>'] == '<STR_LIT>' and i >= <NUM_LIT:2>:<EOL><INDENT>if '<STR_LIT>' not in interface['<STR_LIT>']:<EOL><INDENT>interface['<STR_LIT>'] = '<STR_LIT>'.format(**interface)<EOL><DEDENT>for attr in ['<STR_LIT:type>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>if attr in interface:<EOL><INDENT>del interface[attr]<EOL><DEDENT><DEDENT><DEDENT>elif interface['<STR_LIT:type>'] != '<STR_LIT>':<EOL><INDENT>del interface['<STR_LIT:type>']<EOL><DEDENT>return interface<EOL>

converts NetJSON bridge to
UCI intermediate data structure


def __intermediate_proto(self, interface, address):

<EOL>address_proto = address.pop('<STR_LIT>', '<STR_LIT>')<EOL>if '<STR_LIT>' not in interface:<EOL><INDENT>return address_proto<EOL><DEDENT>else:<EOL><INDENT>return interface.pop('<STR_LIT>')<EOL><DEDENT>


def __intermediate_dns_servers(self, uci, address):

<EOL>if '<STR_LIT>' in uci:<EOL><INDENT>return uci['<STR_LIT>']<EOL><DEDENT>if address['<STR_LIT>'] in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT:none>']:<EOL><INDENT>return None<EOL><DEDENT>dns = self.netjson.get('<STR_LIT>', None)<EOL>if dns:<EOL><INDENT>return '<STR_LIT:U+0020>'.join(dns)<EOL><DEDENT>


def __intermediate_dns_search(self, uci, address):

<EOL>if '<STR_LIT>' in uci:<EOL><INDENT>return uci['<STR_LIT>']<EOL><DEDENT>if address['<STR_LIT>'] == '<STR_LIT:none>':<EOL><INDENT>return None<EOL><DEDENT>dns_search = self.netjson.get('<STR_LIT>', None)<EOL>if dns_search:<EOL><INDENT>return '<STR_LIT:U+0020>'.join(dns_search)<EOL><DEDENT>

determines UCI interface "dns_search" option


protocol = radio['<STR_LIT>']<EOL>if protocol in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>return protocol[<NUM_LIT:4>:]<EOL><DEDENT>if radio['<STR_LIT>'] is <NUM_LIT:0>:<EOL><INDENT>return radio.get('<STR_LIT>')<EOL><DEDENT>elif radio['<STR_LIT>'] <= <NUM_LIT>:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>

possible return values are: 11a, 11b, 11g


protocol = radio.pop('<STR_LIT>')<EOL>channel_width = radio.pop('<STR_LIT>')<EOL>if '<STR_LIT>' in radio:<EOL><INDENT>return radio['<STR_LIT>']<EOL><DEDENT>if protocol == '<STR_LIT>':<EOL><INDENT>return '<STR_LIT>'.format(channel_width)<EOL><DEDENT>elif protocol == '<STR_LIT>':<EOL><INDENT>return '<STR_LIT>'.format(channel_width)<EOL><DEDENT>return '<STR_LIT>'<EOL>


htmode = radio.get('<STR_LIT>')<EOL>hwmode = radio.get('<STR_LIT>', None)<EOL>if htmode.startswith('<STR_LIT>'):<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>elif htmode.startswith('<STR_LIT>'):<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>return '<STR_LIT>'.format(hwmode)<EOL>

determines NetJSON protocol radio attribute


if radio['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>del radio['<STR_LIT>']<EOL>return int(radio['<STR_LIT>'])<EOL>

determines NetJSON channel radio attribute


htmode = radio.pop('<STR_LIT>')<EOL>if htmode == '<STR_LIT>':<EOL><INDENT>return <NUM_LIT:20><EOL><DEDENT>channel_width = htmode.replace('<STR_LIT>', '<STR_LIT>').replace('<STR_LIT>', '<STR_LIT>')<EOL>if '<STR_LIT:+>' in channel_width or '<STR_LIT:->' in channel_width:<EOL><INDENT>radio['<STR_LIT>'] = htmode<EOL>channel_width = channel_width[<NUM_LIT:0>:-<NUM_LIT:1>]<EOL><DEDENT>return int(channel_width)<EOL>

determines NetJSON channel_width radio attribute


uci = self.render(files=False)<EOL>packages = packages_pattern.split(uci)<EOL>if '<STR_LIT>' in packages:<EOL><INDENT>packages.remove('<STR_LIT>')<EOL><DEDENT>for package in packages:<EOL><INDENT>lines = package.split('<STR_LIT:\n>')<EOL>package_name = lines[<NUM_LIT:0>]<EOL>text_contents = '<STR_LIT:\n>'.join(lines[<NUM_LIT:2>:])<EOL>self._add_file(tar=tar,<EOL>name='<STR_LIT>'.format(config_path, package_name),<EOL>contents=text_contents)<EOL><DEDENT>


def merge_config(template, config, list_identifiers=None):

result = template.copy()<EOL>for key, value in config.items():<EOL><INDENT>if isinstance(value, dict):<EOL><INDENT>node = result.get(key, OrderedDict())<EOL>result[key] = merge_config(node, value)<EOL><DEDENT>elif isinstance(value, list) and isinstance(result.get(key), list):<EOL><INDENT>result[key] = merge_list(result[key], value, list_identifiers)<EOL><DEDENT>else:<EOL><INDENT>result[key] = value<EOL><DEDENT><DEDENT>return result<EOL>

Merges ``config`` on top of ``template``.

Conflicting keys are handled in the following way:

* simple values (eg: ``str``, ``int``, ``float``, ecc) in ``config`` will
  overwrite the ones in ``template``
* values of type ``list`` in both ``config`` and ``template`` will be
  merged using to the ``merge_list`` function
* values of type ``dict`` will be merged recursively

:param template: template ``dict``
:param config: config ``dict``
:param list_identifiers: ``list`` or ``None``
:returns: merged ``dict``


def merge_list(list1, list2, identifiers=None):

identifiers = identifiers or []<EOL>dict_map = {'<STR_LIT>': OrderedDict(), '<STR_LIT>': OrderedDict()}<EOL>counter = <NUM_LIT:1><EOL>for list_ in [list1, list2]:<EOL><INDENT>container = dict_map['<STR_LIT>'.format(counter)]<EOL>for el in list_:<EOL><INDENT>key = id(el)<EOL>if isinstance(el, dict):<EOL><INDENT>for id_key in identifiers:<EOL><INDENT>if id_key in el:<EOL><INDENT>key = el[id_key]<EOL>break<EOL><DEDENT><DEDENT><DEDENT>container[key] = deepcopy(el)<EOL><DEDENT>counter += <NUM_LIT:1><EOL><DEDENT>merged = merge_config(dict_map['<STR_LIT>'], dict_map['<STR_LIT>'])<EOL>return list(merged.values())<EOL>

Merges ``list2`` on top of ``list1``.

If both lists contain dictionaries which have keys specified
in ``identifiers`` which have equal values, those dicts will
be merged (dicts in ``list2`` will override dicts in ``list1``).
The remaining elements will be summed in order to create a list
which contains elements of both lists.

:param list1: ``list`` from template
:param list2: ``list`` from config
:param identifiers: ``list`` or ``None``
:returns: merged ``list``


context = context or {}<EOL>if isinstance(data, (dict, list)):<EOL><INDENT>if isinstance(data, dict):<EOL><INDENT>loop_items = data.items()<EOL><DEDENT>elif isinstance(data, list):<EOL><INDENT>loop_items = enumerate(data)<EOL><DEDENT>for key, value in loop_items:<EOL><INDENT>data[key] = evaluate_vars(value, context)<EOL><DEDENT><DEDENT>elif isinstance(data, six.string_types):<EOL><INDENT>vars_found = var_pattern.findall(data)<EOL>for var in vars_found:<EOL><INDENT>var = var.strip()<EOL>if len(vars_found) > <NUM_LIT:1>:<EOL><INDENT>pattern = r'<STR_LIT>' % var<EOL><DEDENT>else:<EOL><INDENT>pattern = var_pattern<EOL><DEDENT>if var in context:<EOL><INDENT>data = re.sub(pattern, context[var], data)<EOL><DEDENT><DEDENT><DEDENT>return data<EOL>

Evaluates variables in ``data``

:param data: data structure containing variables, may be
             ``str``, ``dict`` or ``list``
:param context: ``dict`` containing variables
:returns: modified data structure


value = dict_.get(key, default)<EOL>if value:<EOL><INDENT>return deepcopy(value)<EOL><DEDENT>return value<EOL>

Looks for a key in a dictionary, if found returns
a deepcopied value, otherwise returns default value


return string.replace('<STR_LIT:U+0020>', '<STR_LIT:\t>')<EOL>


error_list = []<EOL>for value, error in zip(e.validator_value, e.context):<EOL><INDENT>error_list.append((value, error.message))<EOL>if error.context:<EOL><INDENT>error_list += _list_errors(error)<EOL><DEDENT><DEDENT>return error_list<EOL>

Returns a list of violated schema fragments and related error messages
:param e: ``jsonschema.exceptions.ValidationError`` instance


self.message = e.message<EOL>self.details = e<EOL>

preserve jsonschema exception attributes
in self.details


requirements = []<EOL>for line in open('<STR_LIT>').readlines():<EOL><INDENT>if line.startswith('<STR_LIT:#>') or line == '<STR_LIT>' or line.startswith('<STR_LIT:http>') or line.startswith('<STR_LIT>'):<EOL><INDENT>continue<EOL><DEDENT>requirements.append(line.replace('<STR_LIT:\n>', '<STR_LIT>'))<EOL><DEDENT>if sys.version_info.major < <NUM_LIT:3>:<EOL><INDENT>requirements.append('<STR_LIT>')<EOL><DEDENT>return requirements<EOL>

parse requirements.txt, ignore links, exclude comments


s = list(iterable)<EOL>return itertools.chain.from_iterable(itertools.combinations(s, r) for r in range(len(s)+<NUM_LIT:1>))<EOL>


if (a != b) == out:<EOL><INDENT>return fault == <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>return fault == <NUM_LIT:1><EOL><DEDENT>

Returns True if XOR(a, b) == out and fault == 0 or XOR(a, b) != out and fault == 1.


if (a and b) == out:<EOL><INDENT>return fault == <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>return fault == <NUM_LIT:1><EOL><DEDENT>

Returns True if AND(a, b) == out and fault == 0 or AND(a, b) != out and fault == 1.


if (a or b) == out:<EOL><INDENT>return fault == <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>return fault == <NUM_LIT:1><EOL><DEDENT>

Returns True if OR(a, b) == out and fault == 0 or OR(a, b) != out and fault == 1.


<EOL>return _irreducible_components(constraint.configurations, constraint.variables)<EOL>

Determine the sets of variables that are irreducible.

    Let V(C) denote the variables of constraint C. For a configuration x, let x|A denote the
    restriction of the configuration to the variables of A. Constraint C is reducible if there
    is a partition of V(C) into nonempty subsets A and B, and two constraints C_A and C_B, with
    V(C_A) = A and C_B V(C_B) = B, such that a configuration x is feasible in C if and only if x|A
    is feasible in C_A and x|B is feasible in C_B. A constraint is irreducible if it is not
    reducible.

    Args:
        constraint (:obj:`.Constraint`):
            Constraint to attempt to reduce.

    Returns:
        list[tuple]: List of tuples in which each tuple is a set of variables that is irreducible.

    Examples:
        This example reduces a constraint, created by specifying its valid configurations, to two
        constraints. The original constraint, that valid configurations for a,b,c are 0,0,1 and
        1,1,1, can be represented by two reduced constraints, for example, (c=1) & (a=b). For
        comparison, an attempt to reduce a constraint representing an AND gate fails to find a
        valid reduction.

        >>> import dwavebinarycsp
        >>> const = dwavebinarycsp.Constraint.from_configurations([(0, 0, 1), (1, 1, 1)],
        ...                                                       ['a', 'b', 'c'], dwavebinarycsp.BINARY)
        >>> dwavebinarycsp.irreducible_components(const)
        [('c',), ('a', 'b')]
        >>> const_and = dwavebinarycsp.Constraint.from_configurations([(0, 0, 0), (0, 1, 0), (1, 0, 0), (1, 1, 1)],
        ...                                                           ['a', 'b', 'c'], dwavebinarycsp.BINARY)
        >>> dwavebinarycsp.irreducible_components(const_and)
        [('a', 'b', 'c')]