def indent(text, prefix, predicate=None):

if predicate is None:<EOL><INDENT>def predicate(line):<EOL><INDENT>return line.strip()<EOL><DEDENT><DEDENT>def prefixed_lines():<EOL><INDENT>for line in text.splitlines(True):<EOL><INDENT>yield (prefix + line if predicate(line) else line)<EOL><DEDENT><DEDENT>return '<STR_LIT>'.join(prefixed_lines())<EOL>

Adds 'prefix' to the beginning of selected lines in 'text'.

    If 'predicate' is provided, 'prefix' will only be added to the lines
    where 'predicate(line)' is True. If 'predicate' is not provided,
    it will default to adding 'prefix' to all non-empty lines that do not
    consist solely of whitespace characters.

    Borrowed from Py3 `textwrap` module.


<EOL>first_usage = [True]<EOL>@functools.wraps(func)<EOL>def wrapper(*args, **kwargs):<EOL><INDENT>if first_usage[<NUM_LIT:0>]:<EOL><INDENT>warnings.warn(<EOL>"<STR_LIT>".format(func.__name__),<EOL>DeprecationWarning,<EOL>)<EOL>first_usage[<NUM_LIT:0>] = False<EOL><DEDENT>return func(*args, **kwargs)<EOL><DEDENT>return wrapper<EOL>

Deprecates a function, printing a warning on the first usage.


bytecode_wnd = memoryview(bytecode)<EOL>while bytecode_wnd:<EOL><INDENT>opcode_id = byte2int(bytecode_wnd[<NUM_LIT:0>])<EOL>opcode = OPCODE_MAP[opcode_id]<EOL>if opcode.imm_struct is not None:<EOL><INDENT>offs, imm, _ = opcode.imm_struct.from_raw(None, bytecode_wnd[<NUM_LIT:1>:])<EOL><DEDENT>else:<EOL><INDENT>imm = None<EOL>offs = <NUM_LIT:0><EOL><DEDENT>insn_len = <NUM_LIT:1> + offs<EOL>yield Instruction(opcode, imm, insn_len)<EOL>bytecode_wnd = bytecode_wnd[insn_len:]<EOL><DEDENT>

Decodes raw bytecode, yielding `Instruction`s.


module_wnd = memoryview(module)<EOL>hdr = ModuleHeader()<EOL>hdr_len, hdr_data, _ = hdr.from_raw(None, module_wnd)<EOL>yield ModuleFragment(hdr, hdr_data)<EOL>module_wnd = module_wnd[hdr_len:]<EOL>while module_wnd:<EOL><INDENT>sec = Section()<EOL>sec_len, sec_data, _ = sec.from_raw(None, module_wnd)<EOL>if (<EOL>decode_name_subsections and<EOL>sec_data.id == SEC_UNK and<EOL>sec_data.name == SEC_NAME<EOL>):<EOL><INDENT>sec_wnd = sec_data.payload<EOL>while sec_wnd:<EOL><INDENT>subsec = NameSubSection()<EOL>subsec_len, subsec_data, _ = subsec.from_raw(None, sec_wnd)<EOL>yield ModuleFragment(subsec, subsec_data)<EOL>sec_wnd = sec_wnd[subsec_len:]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>yield ModuleFragment(sec, sec_data)<EOL><DEDENT>module_wnd = module_wnd[sec_len:]<EOL><DEDENT>

Decodes raw WASM modules, yielding `ModuleFragment`s.


return self._meta<EOL>

Obtains meta info for this object. The object returned is shared
between all objects of the same structure type.

A getter is utilized here instead of a property to allow strict
distinction of meta info from regular fields.


return self._decoder_meta<EOL>

Obtains meta info from the decoder, like byte length in raw format.
For objects not created through decoding, `None` is returned.


return self._decoder_meta<EOL>

Property emulating old name of `_decoder_meta`. Deprecated, do not use.


def process(thumbnail_file, size, **kwargs):

from . import conf<EOL>size_dict = conf.SIZES[size]<EOL>for processor in size_dict['<STR_LIT>']:<EOL><INDENT>processor['<STR_LIT>'](thumbnail_file, **processor['<STR_LIT>'])<EOL><DEDENT>return thumbnail_file<EOL>

Post processors are functions that receive file objects,
performs necessary operations and return the results as file objects.


def optimize(thumbnail_file, jpg_command=None, png_command=None,<EOL>gif_command=None):

temp_dir = get_or_create_temp_dir()<EOL>thumbnail_filename = os.path.join(temp_dir, "<STR_LIT:%s>" % shortuuid.uuid())<EOL>f = open(thumbnail_filename, '<STR_LIT:wb>')<EOL>f.write(thumbnail_file.read())<EOL>f.close()<EOL>filetype = imghdr.what(thumbnail_filename)<EOL>command = None<EOL>if filetype == "<STR_LIT>" or filetype == "<STR_LIT>":<EOL><INDENT>command = jpg_command<EOL><DEDENT>elif filetype == "<STR_LIT>":<EOL><INDENT>command = png_command<EOL><DEDENT>elif filetype == "<STR_LIT>":<EOL><INDENT>command = gif_command<EOL><DEDENT>if command:<EOL><INDENT>command = command % {'<STR_LIT:filename>': thumbnail_filename}<EOL>call(command, shell=True)<EOL><DEDENT>optimized_file = File(open(thumbnail_filename, '<STR_LIT:rb>'))<EOL>os.remove(thumbnail_filename)<EOL>return optimized_file<EOL>

A post processing function to optimize file size. Accepts commands
to optimize JPG, PNG and GIF images as arguments. Example:

THUMBNAILS = {
    # Other options...
    'POST_PROCESSORS': [
        {
            'processor': 'thumbnails.post_processors.optimize',
            'png_command': 'optipng -force -o3 "%(filename)s"',
            'jpg_command': 'jpegoptim -f --strip-all "%(filename)s"',
        },
    ],
}

Note: using output redirection in commands may cause unpredictable results.
For example 'optipng -force -o3 "%(filename)s" &> /dev/null' may cause
optimize command to fail on some systems.


def __init__(self, location=None, *args, **kwargs):

if location is None:<EOL><INDENT>location = tempfile.mkdtemp()<EOL>self.temporary_location = location<EOL><DEDENT>super(TemporaryStorage, self).__init__(location=location, *args,<EOL>**kwargs)<EOL>


temporary_location = getattr(self, '<STR_LIT>', None)<EOL>if temporary_location:<EOL><INDENT>shutil.rmtree(temporary_location)<EOL><DEDENT>

Delete the temporary directory created during initialisation.
This storage class should not be used again after this method is
called.


self._thumbnails = {}<EOL>metadatas = self.metadata_backend.get_thumbnails(self.source_image.name)<EOL>for metadata in metadatas:<EOL><INDENT>self._thumbnails[metadata.size] = Thumbnail(metadata=metadata, storage=self.storage)<EOL><DEDENT>


if self._thumbnails is not None:<EOL><INDENT>return self._thumbnails<EOL><DEDENT>self._refresh_cache()<EOL>return self._thumbnails<EOL>


if self._thumbnails is None:<EOL><INDENT>self._refresh_cache()<EOL><DEDENT>thumbnail = self._thumbnails.get(size)<EOL>if thumbnail is None:<EOL><INDENT>thumbnail = images.get(self.source_image.name, size,<EOL>self.metadata_backend, self.storage)<EOL>if thumbnail is None:<EOL><INDENT>thumbnail = self.create(size)<EOL><DEDENT>self._thumbnails[size] = thumbnail<EOL><DEDENT>return thumbnail<EOL>

Returns a Thumbnail instance.
First check whether thumbnail is already cached. If it doesn't:
1. Try to fetch the thumbnail
2. Create thumbnail if it's not present
3. Cache the thumbnail for future use


thumbnail = images.create(self.source_image.name, size,<EOL>self.metadata_backend, self.storage)<EOL>return thumbnail<EOL>

Creates and return a thumbnail of a given size.


images.delete(self.source_image.name, size,<EOL>self.metadata_backend, self.storage)<EOL>del(self._thumbnails[size])<EOL>


def create(source_name, size, metadata_backend=None, storage_backend=None):

if storage_backend is None:<EOL><INDENT>storage_backend = backends.storage.get_backend()<EOL><DEDENT>if metadata_backend is None:<EOL><INDENT>metadata_backend = backends.metadata.get_backend()<EOL><DEDENT>thumbnail_file = processors.process(storage_backend.open(source_name), size)<EOL>thumbnail_file = post_processors.process(thumbnail_file, size)<EOL>name = get_thumbnail_name(source_name, size)<EOL>name = storage_backend.save(name, thumbnail_file)<EOL>metadata = metadata_backend.add_thumbnail(source_name, size, name)<EOL>return Thumbnail(metadata=metadata, storage=storage_backend)<EOL>

Creates a thumbnail file and its relevant metadata. Returns a
Thumbnail instance.


def get(source_name, size, metadata_backend=None, storage_backend=None):

if storage_backend is None:<EOL><INDENT>storage_backend = backends.storage.get_backend()<EOL><DEDENT>if metadata_backend is None:<EOL><INDENT>metadata_backend = backends.metadata.get_backend()<EOL><DEDENT>metadata = metadata_backend.get_thumbnail(source_name, size)<EOL>if metadata is None:<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>return Thumbnail(metadata=metadata, storage=storage_backend)<EOL><DEDENT>

Returns a Thumbnail instance, or None if thumbnail does not yet exist.


def delete(source_name, size, metadata_backend=None, storage_backend=None):

if storage_backend is None:<EOL><INDENT>storage_backend = backends.storage.get_backend()<EOL><DEDENT>if metadata_backend is None:<EOL><INDENT>metadata_backend = backends.metadata.get_backend()<EOL><DEDENT>storage_backend.delete(get_thumbnail_name(source_name, size))<EOL>metadata_backend.delete_thumbnail(source_name, size)<EOL>

Deletes a thumbnail file and its relevant metadata.


def pre_save(self, model_instance, add):

file = getattr(model_instance, self.attname)<EOL>if file and not file._committed:<EOL><INDENT>image_file = file<EOL>if self.resize_source_to:<EOL><INDENT>file.seek(<NUM_LIT:0>)<EOL>image_file = processors.process(file, self.resize_source_to)<EOL>image_file = post_processors.process(image_file, self.resize_source_to)<EOL><DEDENT>filename = str(shortuuid.uuid()) + os.path.splitext(file.name)[<NUM_LIT:1>]<EOL>file.save(filename, image_file, save=False)<EOL><DEDENT>return file<EOL>

Process the source image through the defined processors.


from south.modelsinspector import introspector<EOL>field_class = '<STR_LIT>'<EOL>args, kwargs = introspector(self)<EOL>return (field_class, args, kwargs)<EOL>

Return a suitable description of this field for South.
Taken from smiley chris' easy_thumbnails


image.resize(**kwargs)<EOL>return image<EOL>

Resized an image based on given width and height


image.rotate(**kwargs)<EOL>return image<EOL>


image.flip(**kwargs)<EOL>return image<EOL>

Flips an image. Expects "horizontal" or "vertical" as argument.


image.crop(**kwargs)<EOL>return image<EOL>

Crops an image based on given width or height


image.quality = kwargs['<STR_LIT>']<EOL>return image<EOL>


from . import conf<EOL>raw_image = images.from_file(file)<EOL>size_dict = conf.SIZES[size]<EOL>for processor in size_dict['<STR_LIT>']:<EOL><INDENT>raw_image = processor['<STR_LIT>'](raw_image, **processor['<STR_LIT>'])<EOL><DEDENT>image_io = io.BytesIO()<EOL>raw_image.save(file=image_io)<EOL>image_file = ContentFile(image_io.getvalue())<EOL>return image_file<EOL>

Process an image through its defined processors
params :file: filename or file-like object
params :size: string for size defined in settings
return a ContentFile


if hasattr(name, '<STR_LIT>'):<EOL><INDENT>return name<EOL><DEDENT>module_name, attribute = name.rsplit('<STR_LIT:.>', <NUM_LIT:1>)<EOL>module = importlib.import_module(module_name)<EOL>return getattr(module, attribute)<EOL>

Return an attribute from a dotted path name (e.g. "path.to.func").
Copied from nvie's rq https://github.com/nvie/rq/blob/master/rq/utils.py


parsed_processors = []<EOL>for processor in processor_definition:<EOL><INDENT>processor_function = import_attribute(processor['<STR_LIT>'])<EOL>kwargs = deepcopy(processor)<EOL>kwargs.pop('<STR_LIT>')<EOL>parsed_processors.append({<EOL>'<STR_LIT>': processor_function,<EOL>'<STR_LIT>': kwargs<EOL>})<EOL><DEDENT>return parsed_processors<EOL>

Returns a dictionary that contains the imported processors and
kwargs. For example, passing in:

processors = [
    {'processor': 'thumbnails.processors.resize', 'width': 10, 'height': 10},
    {'processor': 'thumbnails.processors.crop', 'width': 10, 'height': 10},
]

Would return:

[
    {'processor': resize_function, kwargs: {'width': 10, 'height': 10}}
    {'processor': crop_function, kwargs: {'width': 10, 'height': 10}}
]


if not engine:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>mock_engine = engine(self)<EOL>methods = ('<STR_LIT>', '<STR_LIT>')<EOL>if not all([hasattr(mock_engine, method) for method in methods]):<EOL><INDENT>raise NotImplementedError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>self.mock_engine = mock_engine<EOL>if self.active:<EOL><INDENT>self.mock_engine.activate()<EOL><DEDENT>

Sets a custom mock engine, replacing the built-in one.

This is particularly useful if you want to replace the built-in
HTTP traffic mock interceptor engine with your custom one.

For mock engine implementation details, see `pook.MockEngine`.

Arguments:
    engine (pook.MockEngine): custom mock engine to use.


def hostname_filter(hostname, req):<EOL><INDENT>if isregex(hostname):<EOL><INDENT>return hostname.match(req.url.hostname)<EOL><DEDENT>return req.url.hostname == hostname<EOL><DEDENT>for hostname in hostnames:<EOL><INDENT>self.use_network_filter(partial(hostname_filter, hostname))<EOL><DEDENT>self.networking = True<EOL>

Enables real networking mode, optionally passing one or multiple
hostnames that would be used as filter.

If at least one hostname matches with the outgoing traffic, the
request will be executed via the real network.

Arguments:
    *hostnames: optional list of host names to enable real network
        against them. hostname value can be a regular expression.


self.networking = False<EOL>


self.network_filters = self.network_filters + fn<EOL>

Adds network filters to determine if certain outgoing unmatched
HTTP traffic can stablish real network connections.

Arguments:
    *fn (function): variadic function filter arguments to be used.


self.network_filters = []<EOL>

Flushes registered real networking filters in the current
mock engine.


<EOL>if kw.get('<STR_LIT>'):<EOL><INDENT>kw.pop('<STR_LIT>')<EOL>self.activate()<EOL><DEDENT>mock = Mock(url=url, **kw)<EOL>mock._engine = self<EOL>self.add_mock(mock)<EOL>return mock<EOL>

Creates and registers a new HTTP mock in the current engine.

Arguments:
    url (str): request URL to mock.
    activate (bool): force mock engine activation.
        Defaults to ``False``.
    **kw (mixed): variadic keyword arguments for ``Mock`` constructor.

Returns:
    pook.Mock: new mock instance.


self.mocks.append(mock)<EOL>

Adds a new mock instance to the current engine.

Arguments:
    mock (pook.Mock): mock instance to add.


self.mocks = [m for m in self.mocks if m is not mock]<EOL>

Removes a specific mock instance by object reference.

Arguments:
    mock (pook.Mock): mock instance to remove.


self.mocks = []<EOL>


self._engine_proxy('<STR_LIT>', *interceptors)<EOL>

Adds one or multiple HTTP traffic interceptors to the current
mocking engine.

Interceptors are typically HTTP client specific wrapper classes that
implements the pook interceptor interface.

Note: this method is may not be implemented if using a custom mock
engine.

Arguments:
    interceptors (pook.interceptors.BaseInterceptor)


Flushes registered interceptors in the current mocking engine.

This method is low-level. Only call it if you know what you are doing.

Note: this method is may not be implemented if using a custom mock
engine.


return self._engine_proxy('<STR_LIT>', name)<EOL>

Removes a specific interceptor by name.

Note: this method is may not be implemented if using a custom mock
engine.

Arguments:
    name (str): interceptor name to disable.

Returns:
    bool: `True` if the interceptor was disabled, otherwise `False`.


if self.active:<EOL><INDENT>return None<EOL><DEDENT>self.mock_engine.activate()<EOL>self.active = True<EOL>

Activates the registered interceptors in the mocking engine.

This means any HTTP traffic captures by those interceptors will
trigger the HTTP mock matching engine in order to determine if a given
HTTP transaction should be mocked out or not.


if not self.active:<EOL><INDENT>return None<EOL><DEDENT>self.mock_engine.disable()<EOL>self.active = False<EOL>

Disables interceptors and stops intercepting any outgoing HTTP traffic.


<EOL>Engine.__init__(self, network=self.networking)<EOL>

Resets and flushes engine state and mocks to defaults.


return [mock for mock in self.unmatched_reqs]<EOL>

Returns a ``tuple`` of unmatched requests.

Unmatched requests will be registered only if ``networking`` mode
has been enabled.

Returns:
    list: unmatched intercepted requests.


return len(self.unmatched_requests())<EOL>

Returns the total number of unmatched requests intercepted by pook.

Unmatched requests will be registered only if ``networking`` mode
has been enabled.

Returns:
    int: total number of unmatched requests.


return len(self.unmatched()) > <NUM_LIT:0><EOL>

Returns ``True`` if there are unmatched requests. Otherwise ``False``.

Unmatched requests will be registered only if ``networking`` mode
has been enabled.

Returns:
    bool


return len(self.pending_mocks())<EOL>

Returns the number of pending mocks to be matched.

Returns:
    int: number of pending mocks.


return [mock for mock in self.mocks if not mock.isdone()]<EOL>

Returns a ``tuple`` of pending mocks to be matched.

Returns:
    tuple: pending mock instances.


return len(self.pending_mocks())<EOL>

Returns the ``True`` if the engine has pending mocks to be matched.
Otherwise ``False``.

Returns:
    bool


return self.active<EOL>

Returns the current engine enabled/disabled status.

Returns:
    bool: ``True`` if the engine is active. Otherwise ``False``.


return all(mock.isdone() for mock in self.mocks)<EOL>

Returns True if all the registered mocks has been triggered.

Returns:
    bool: True is all the registered mocks are gone, otherwise False.


self._append(self.filters, *filters)<EOL>

Append engine-level HTTP request filter functions.

Arguments:
    filters*: variadic filter functions to be added.


self._append(self.mappers, *mappers)<EOL>

Append engine-level HTTP request mapper functions.

Arguments:
    filters*: variadic mapper functions to be added.


return (self.networking and<EOL>all((fn(request) for fn in self.network_filters)))<EOL>

Verifies if real networking mode should be used for the given
request, passing it to the registered network filters.

Arguments:
    request (pook.Request): outgoing HTTP request to test.

Returns:
    bool


<EOL>for test in self.filters:<EOL><INDENT>if not test(request, self):<EOL><INDENT>return False<EOL><DEDENT><DEDENT>for mapper in self.mappers:<EOL><INDENT>request = mapper(request, self)<EOL>if not request:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>match_errors = []<EOL>for mock in self.mocks[:]:<EOL><INDENT>try:<EOL><INDENT>matches, errors = mock.match(request.copy())<EOL>if len(errors):<EOL><INDENT>match_errors += errors<EOL><DEDENT>if matches:<EOL><INDENT>return mock<EOL><DEDENT><DEDENT>except PookExpiredMock:<EOL><INDENT>self.mocks.remove(mock)<EOL><DEDENT><DEDENT>if not self.should_use_network(request):<EOL><INDENT>msg = '<STR_LIT>'<EOL>msg += (<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(request)<EOL>)<EOL>if self.debug:<EOL><INDENT>err = '<STR_LIT>'.join([str(err) for err in match_errors])<EOL>if err:<EOL><INDENT>msg += '<STR_LIT>'.format(err)<EOL><DEDENT><DEDENT>raise PookNoMatches(msg)<EOL><DEDENT>self.unmatched_reqs.append(request)<EOL>

Matches a given Request instance contract against the registered mocks.

If a mock passes all the matchers, its response will be returned.

Arguments:
    request (pook.Request): Request contract to match.

Raises:
    pook.PookNoMatches: if networking is disabled and no mock matches
        with the given request contract.

Returns:
    pook.Response: the mock response to be used by the interceptor.


for interceptor in interceptors:<EOL><INDENT>self.interceptors.append(interceptor(self.engine))<EOL><DEDENT>

Adds one or multiple HTTP traffic interceptors to the current
mocking engine.

Interceptors are typically HTTP client specific wrapper classes that
implements the pook interceptor interface.

Arguments:
    interceptors (pook.interceptors.BaseInterceptor)


self.interceptors = []<EOL>

Flushes registered interceptors in the current mocking engine.

This method is low-level. Only call it if you know what you are doing.


for index, interceptor in enumerate(self.interceptors):<EOL><INDENT>matches = (<EOL>type(interceptor).__name__ == name or<EOL>getattr(interceptor, '<STR_LIT:name>') == name<EOL>)<EOL>if matches:<EOL><INDENT>self.interceptors.pop(index)<EOL>return True<EOL><DEDENT><DEDENT>return False<EOL>

Removes a specific interceptor by name.

Arguments:
    name (str): interceptor name to disable.

Returns:
    bool: `True` if the interceptor was disabled, otherwise `False`.


[interceptor.activate() for interceptor in self.interceptors]<EOL>

Activates the registered interceptors in the mocking engine.

This means any HTTP traffic captures by those interceptors will
trigger the HTTP mock matching engine in order to determine if a given
HTTP transaction should be mocked out or not.


<EOL>for interceptor in self.interceptors:<EOL><INDENT>try:<EOL><INDENT>interceptor.disable()<EOL><DEDENT>except RuntimeError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>

Disables interceptors and stops intercepting any outgoing HTTP traffic.


<EOL>try:<EOL><INDENT>value = self[key]<EOL><DEDENT>except KeyError:<EOL><INDENT>if default is self.__marker:<EOL><INDENT>raise<EOL><DEDENT>return default<EOL><DEDENT>else:<EOL><INDENT>del self[key]<EOL>return value<EOL><DEDENT>

D.pop(k[,d]) -> v, remove specified key and return the
corresponding value.
If key is not found, d is returned if given, otherwise KeyError
is raised.


key_lower = key.lower()<EOL>new_vals = key, val<EOL>vals = self._container.setdefault(key_lower, new_vals)<EOL>if new_vals is not vals:<EOL><INDENT>if isinstance(vals, list):<EOL><INDENT>vals.append(val)<EOL><DEDENT>else:<EOL><INDENT>self._container[key_lower] = [vals[<NUM_LIT:0>], vals[<NUM_LIT:1>], val]<EOL><DEDENT><DEDENT>

Adds a (name, value) pair, doesn't overwrite the value if it already
exists.

Usage::

    headers = HTTPHeaderDict(foo='bar')
    headers.add('Foo', 'baz')
    headers['foo']
    > 'bar, baz'


key_lower = key.lower()<EOL>new_vals = key, val<EOL>vals = self._container.setdefault(key_lower, new_vals)<EOL>if new_vals is not vals:<EOL><INDENT>self._container[key_lower] = [vals[<NUM_LIT:0>], vals[<NUM_LIT:1>], val]<EOL><DEDENT>

Sets a header field with the given value, removing
previous values.

Usage::

    headers = HTTPHeaderDict(foo='bar')
    headers.set('Foo', 'baz')
    headers['foo']
    > 'baz'


if len(args) > <NUM_LIT:1>:<EOL><INDENT>raise TypeError("<STR_LIT>"<EOL>"<STR_LIT>".format(len(args)))<EOL><DEDENT>other = args[<NUM_LIT:0>] if len(args) >= <NUM_LIT:1> else ()<EOL>if isinstance(other, HTTPHeaderDict):<EOL><INDENT>for key, val in other.iteritems():<EOL><INDENT>self.add(key, val)<EOL><DEDENT><DEDENT>elif isinstance(other, Mapping):<EOL><INDENT>for key in other:<EOL><INDENT>self.add(key, other[key])<EOL><DEDENT><DEDENT>elif hasattr(other, "<STR_LIT>"):<EOL><INDENT>for key in other.keys():<EOL><INDENT>self.add(key, other[key])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for key, value in other:<EOL><INDENT>self.add(key, value)<EOL><DEDENT><DEDENT>for key, value in kwargs.items():<EOL><INDENT>self.add(key, value)<EOL><DEDENT>

Generic import function for any type of header-like object.
Adapted version of MutableMapping.update in order to insert items
with self.add instead of self.__setitem__


try:<EOL><INDENT>vals = self._container[key.lower()]<EOL><DEDENT>except KeyError:<EOL><INDENT>return []<EOL><DEDENT>else:<EOL><INDENT>if isinstance(vals, tuple):<EOL><INDENT>return [vals[<NUM_LIT:1>]]<EOL><DEDENT>else:<EOL><INDENT>return vals[<NUM_LIT:1>:]<EOL><DEDENT><DEDENT>

Returns a list of all the values for the named field.
Returns an empty list if the key doesn't exist.


for key in self:<EOL><INDENT>vals = self._container[key.lower()]<EOL>for val in vals[<NUM_LIT:1>:]:<EOL><INDENT>yield vals[<NUM_LIT:0>], val<EOL><DEDENT><DEDENT>

Iterate over all header lines, including duplicate ones.


for key in self:<EOL><INDENT>val = self._container[key.lower()]<EOL>yield val[<NUM_LIT:0>], '<STR_LIT:U+002CU+0020>'.join(val[<NUM_LIT:1>:])<EOL><DEDENT>

Iterate over all headers, merging duplicate ones together.


@classmethod<EOL><INDENT>def from_httplib(cls, message):  <DEDENT>

<EOL>headers = []<EOL>for line in message.headers:<EOL><INDENT>if line.startswith(('<STR_LIT:U+0020>', '<STR_LIT:\t>')):<EOL><INDENT>key, value = headers[-<NUM_LIT:1>]<EOL>headers[-<NUM_LIT:1>] = (key, value + '<STR_LIT:\r\n>' + line.rstrip())<EOL>continue<EOL><DEDENT>key, value = line.split('<STR_LIT::>', <NUM_LIT:1>)<EOL>headers.append((key, value.strip()))<EOL><DEDENT>return cls(headers)<EOL>

Read headers from a Python 2 httplib message object.


@coroutine<EOL>@functools.wraps(fn)<EOL>def wrapper(*args, **kw):<EOL><INDENT>_engine.activate()<EOL>try:<EOL><INDENT>if iscoroutinefunction(fn):<EOL><INDENT>yield from fn(*args, **kw)  <EOL><DEDENT>else:<EOL><INDENT>fn(*args, **kw)<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>_engine.disable()<EOL><DEDENT><DEDENT>return wrapper<EOL>

Async version of activate decorator

Arguments:
    fn (function): function that be wrapped by decorator.
    _engine (Engine): pook engine instance

Returns:
    function: decorator wrapper function.


pass<EOL>

Match performs the value matching.
This is an abstract method that must be implemented by child classes.

Arguments:
    request (pook.Request): request object to match.


def compare(self, value, expectation, regex_expr=False):

return compare(value, expectation, regex_expr=regex_expr)<EOL>

Compares two values with regular expression matching support.

Arguments:
    value (mixed): value to compare.
    expectation (mixed): value to match.
    regex_expr (bool, optional): enables string based regex matching.

Returns:
    bool


return {self.name: deepcopy(self.expectation)}<EOL>

Returns the current matcher representation as dictionary.

Returns:
    dict


[matchers.append(m) for m in matcher]<EOL>

Registers one or multiple matchers to be used by default from
mocking engine.

Arguments:
    *matcher (list[pook.BaseMatcher]): variadic matchers to add.


for matcher in matchers:<EOL><INDENT>if matcher.__name__ == name or getattr(matcher, '<STR_LIT:name>', None) == name:<EOL><INDENT>return matcher<EOL><DEDENT><DEDENT>

Returns a matcher instance by class or alias name.

Arguments:
    name (str): matcher class name or alias.

Returns:
    matcher: found matcher instance, otherwise ``None``.


matcher = get(name)<EOL>if not matcher:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(name))<EOL><DEDENT>return matcher(*args)<EOL>

Initializes a matcher instance passing variadic arguments to
its constructor. Acts as a delegator proxy.

Arguments:
    name (str): matcher class name or alias to execute.
    *args (mixed): variadic argument

Returns:
    matcher: matcher instance.

Raises:
    ValueError: if matcher was not found.


if PY_3:<EOL><INDENT>return test_case().assertEqual(x, y) or True<EOL><DEDENT>assert x == y<EOL>

Shortcut function for ``unittest.TestCase.assertEqual()``.

Arguments:
    x (mixed)
    y (mixed)

Raises:
    AssertionError: in case of assertion error.

Returns:
    bool


<EOL>x = strip_regex(x) if regex_expr and isregex_expr(x) else x<EOL>if PY_3:<EOL><INDENT>x = x.pattern if isregex(x) else x<EOL>return test_case().assertRegex(y, x) or True<EOL><DEDENT>if isinstance(x, str):<EOL><INDENT>x = re.compile(x, re.IGNORECASE)<EOL><DEDENT>assert x.match(y) is not None<EOL>

Tries to match a regular expression value ``x`` against ``y``.
Aliast``unittest.TestCase.assertEqual()``

Arguments:
    x (regex|str): regular expression to test.
    y (str): value to match.
    regex_expr (bool): enables regex string based expression matching.

Raises:
    AssertionError: in case of mismatching.

Returns:
    bool


req = type(self)()<EOL>req.__dict__ = self.__dict__.copy()<EOL>req._headers = self.headers.copy()<EOL>return req<EOL>

Copies the current Request object instance for side-effects purposes.

Returns:
    pook.Request: copy of the current Request instance.


entries = []<EOL>entries.append('<STR_LIT>'.format(self._method))<EOL>entries.append('<STR_LIT>'.format(<EOL>self._url if isregex(self._url) else self.rawurl))<EOL>if self._query:<EOL><INDENT>entries.append('<STR_LIT>'.format(self._query))<EOL><DEDENT>if self._headers:<EOL><INDENT>entries.append('<STR_LIT>'.format(self._headers))<EOL><DEDENT>if self._body:<EOL><INDENT>entries.append('<STR_LIT>'.format(self._body))<EOL><DEDENT>separator = '<STR_LIT:=>' * <NUM_LIT:50><EOL>return (separator + '<STR_LIT>' + separator).format('<STR_LIT:\n>'.join(entries))<EOL>

Returns an human friendly readable instance data representation.

Returns:
    str


return type(self).__name__<EOL>


pass<EOL>

Activates the traffic interceptor.
This method must be implemented by any interceptor.


pass<EOL>

Disables the traffic interceptor.
This method must be implemented by any interceptor.


interceptors.append(*interceptors)<EOL>

Registers a new HTTP client interceptor.

Arguments:
    *interceptors (interceptor): interceptor(s) to be added.


for interceptor in interceptors:<EOL><INDENT>if interceptor.__name__ == name:<EOL><INDENT>return interceptor<EOL><DEDENT><DEDENT>

Returns an interceptor by class name.

Arguments:
    name (str): interceptor class name or alias.

Returns:
    interceptor: found interceptor instance, otherwise ``None``.


[self._patch(path) for path in PATCHES]<EOL>


[patch.stop() for patch in self.patchers]<EOL>


self.append(matcher)<EOL>

Adds a new matcher function to the current engine.

Arguments:
    matcher (function): matcher function to be added.


self.clear()<EOL>

Flushes the current matcher engine, removing all the registered
matcher functions.


errors = []<EOL>def match(matcher):<EOL><INDENT>try:<EOL><INDENT>return matcher.match(request)<EOL><DEDENT>except Exception as err:<EOL><INDENT>err = '<STR_LIT>'.format(type(matcher).__name__, err)<EOL>errors.append(err)<EOL>return False<EOL><DEDENT><DEDENT>return all([match(matcher) for matcher in self]), errors<EOL>

Match the given HTTP request instance against the registered
matcher functions in the current engine.

Arguments:
    request (pook.Request): outgoing request to match.

Returns:
    tuple(bool, list[Exception]): ``True`` if all matcher tests
        passes, otherwise ``False``. Also returns an optional list
        of error exceptions.


matchers = [repr(matcher) for matcher in self]<EOL>return '<STR_LIT>'.format('<STR_LIT>'.join(matchers))<EOL>


<EOL>for name in sorted(args):<EOL><INDENT>value = args[name]<EOL>target = instance<EOL>if name.startswith('<STR_LIT>') or name.startswith('<STR_LIT>'):<EOL><INDENT>name = name.replace('<STR_LIT>', '<STR_LIT>').replace('<STR_LIT>', '<STR_LIT>')<EOL>if hasattr(instance, '<STR_LIT>'):<EOL><INDENT>target = instance._response<EOL><DEDENT><DEDENT>member = getattr(target, name, None)<EOL>isattr = name in dir(target)<EOL>iscallable = ismethod(member) and not isfunction(member)<EOL>if not iscallable and not isattr:<EOL><INDENT>raise PookInvalidArgument('<STR_LIT>'.format(name))<EOL><DEDENT>if iscallable:<EOL><INDENT>member(value)<EOL><DEDENT>else:<EOL><INDENT>setattr(target, name, value)<EOL><DEDENT><DEDENT>

Triggers specific class methods using a simple reflection
mechanism based on the given input dictionary params.

Arguments:
    instance (object): target instance to dynamically trigger methods.
    args (iterable): input arguments to trigger objects to

Returns:
    None


[target.append(item) for item in items<EOL>if isfunction(item) or ismethod(item)]<EOL>

Helper function to append functions into a given list.

Arguments:
    target (list): receptor list to append functions.
    items (iterable): iterable that yields elements to append.


if not isinstance(request, Request):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>for key in request.keys:<EOL><INDENT>if hasattr(instance, key):<EOL><INDENT>getattr(instance, key)(getattr(request, key))<EOL><DEDENT><DEDENT>

Triggers request mock definition methods dynamically based on input
keyword arguments passed to `pook.Mock` constructor.

This is used to provide a more Pythonic interface vs chainable API
approach.


self._request.url = url<EOL>self.add_matcher(matcher('<STR_LIT>', url))<EOL>

Defines the mock URL to match.
It can be a full URL with path and query params.

Protocol schema is optional, defaults to ``http://``.

Arguments:
    url (str): mock URL to match. E.g: ``server.com/api``.

Returns:
    self: current Mock instance.


self._request.method = method<EOL>self.add_matcher(matcher('<STR_LIT>', method))<EOL>

Defines the HTTP method to match.
Use ``*`` to match any method.

Arguments:
    method (str): method value to match. E.g: ``GET``.

Returns:
    self: current Mock instance.


url = furl(self._request.rawurl)<EOL>url.path = path<EOL>self._request.url = url.url<EOL>self.add_matcher(matcher('<STR_LIT>', path))<EOL>

Defines a URL path to match.

Only call this method if the URL has no path already defined.

Arguments:
    path (str): URL path value to match. E.g: ``/api/users``.

Returns:
    self: current Mock instance.


@fluent<EOL><INDENT>def header(self, name, value):<DEDENT>

headers = {name: value}<EOL>self._request.headers = headers<EOL>self.add_matcher(matcher('<STR_LIT>', headers))<EOL>


@fluent<EOL><INDENT>def headers(self, headers=None, **kw):<DEDENT>

headers = kw if kw else headers<EOL>self._request.headers = headers<EOL>self.add_matcher(matcher('<STR_LIT>', headers))<EOL>

Defines a dictionary of arguments.

Header keys are case insensitive.

Arguments:
    headers (dict): headers to match.
    **headers (dict): headers to match as variadic keyword arguments.

Returns:
    self: current Mock instance.


for name in names:<EOL><INDENT>headers = {name: re.compile('<STR_LIT>')}<EOL>self.add_matcher(matcher('<STR_LIT>', headers))<EOL><DEDENT>

Defines a new header matcher expectation that must be present in the
outgoing request in order to be satisfied, no matter what value it
hosts.

Header keys are case insensitive.

Arguments:
    *names (str): header or headers names to match.

Returns:
    self: current Mock instance.

Example::

    (pook.get('server.com/api')
        .header_present('content-type'))


headers = {name: re.compile('<STR_LIT>') for name in headers}<EOL>self.add_matcher(matcher('<STR_LIT>', headers))<EOL>

Defines a list of headers that must be present in the
outgoing request in order to satisfy the matcher, no matter what value
the headers hosts.

Header keys are case insensitive.

Arguments:
    headers (list|tuple): header keys to match.

Returns:
    self: current Mock instance.

Example::

    (pook.get('server.com/api')
        .headers_present(['content-type', 'Authorization']))


self.content(value)<EOL>

Defines the request ``Content-Type`` header to match.

You can pass one of the following aliases instead of the full
MIME type representation:

- ``json`` = ``application/json``
- ``xml`` = ``application/xml``
- ``html`` = ``text/html``
- ``text`` = ``text/plain``
- ``urlencoded`` = ``application/x-www-form-urlencoded``
- ``form`` = ``application/x-www-form-urlencoded``
- ``form-data`` = ``application/x-www-form-urlencoded``

Arguments:
    value (str): type alias or header value to match.

Returns:
    self: current Mock instance.


header = {'<STR_LIT:Content-Type>': TYPES.get(value, value)}<EOL>self._request.headers = header<EOL>self.add_matcher(matcher('<STR_LIT>', header))<EOL>

Defines the ``Content-Type`` outgoing header value to match.

You can pass one of the following type aliases instead of the full
MIME type representation:

- ``json`` = ``application/json``
- ``xml`` = ``application/xml``
- ``html`` = ``text/html``
- ``text`` = ``text/plain``
- ``urlencoded`` = ``application/x-www-form-urlencoded``
- ``form`` = ``application/x-www-form-urlencoded``
- ``form-data`` = ``application/x-www-form-urlencoded``

Arguments:
    value (str): type alias or header value to match.

Returns:
    self: current Mock instance.


@fluent<EOL><INDENT>def param(self, name, value):<DEDENT>

self.params({name: value})<EOL>

Defines an URL param key and value to match.

Arguments:
    name (str): param name value to match.
    value (str): param name value to match.

Returns:
    self: current Mock instance.


self.params({name: re.compile('<STR_LIT>')})<EOL>

Checks if a given URL param name is present in the URL.

Arguments:
    name (str): param name to check existence.

Returns:
    self: current Mock instance.


url = furl(self._request.rawurl)<EOL>url = url.add(params)<EOL>self._request.url = url.url<EOL>self.add_matcher(matcher('<STR_LIT>', params))<EOL>

Defines a set of URL query params to match.

Arguments:
    params (dict): set of params to match.

Returns:
    self: current Mock instance.


self._request.body = body<EOL>self.add_matcher(matcher('<STR_LIT>', body))<EOL>

Defines the body data to match.

``body`` argument can be a ``str``, ``binary`` or a regular expression.

Arguments:
    body (str|binary|regex): body data to match.

Returns:
    self: current Mock instance.


self._request.json = json<EOL>self.add_matcher(matcher('<STR_LIT>', json))<EOL>

Defines the JSON body to match.

``json`` argument can be an JSON string, a JSON serializable
Python structure, such as a ``dict`` or ``list`` or it can be
a regular expression used to match the body.

Arguments:
    json (str|dict|list|regex): body JSON to match.

Returns:
    self: current Mock instance.


self.add_matcher(matcher('<STR_LIT>', schema))<EOL>

Defines a JSONSchema representation to be used for body matching.

Arguments:
    schema (str|dict): dict or JSONSchema string to use.

Returns:
    self: current Mock instance.


self._request.xml = xml<EOL>self.add_matcher(matcher('<STR_LIT>', xml))<EOL>

Defines a XML body value to match.

Arguments:
    xml (str|regex): body XML to match.

Returns:
    self: current Mock instance.


with open(path, '<STR_LIT:r>') as f:<EOL><INDENT>self.body(str(f.read()))<EOL><DEDENT>

Reads the body to match from a disk file.

Arguments:
    path (str): relative or absolute path to file to read from.

Returns:
    self: current Mock instance.


self.matchers.add(matcher)<EOL>

Adds one or multiple custom matchers instances.

Matchers must implement the following interface:

- ``.__init__(expectation)``
- ``.match(request)``
- ``.name = str``

Matchers can optionally inherit from ``pook.matchers.BaseMatcher``.

Arguments:
    *matchers (pook.matchers.BaseMatcher): matchers to add.

Returns:
    self: current Mock instance.


[self.add_matcher(matcher) for matcher in matchers]<EOL>


self._times = times<EOL>

Defines the TTL limit for the current mock.

The TTL number will determine the maximum number of times that the
current mock can be matched and therefore consumed.

Arguments:
    times (int): TTL number. Defaults to ``1``.

Returns:
    self: current Mock instance.


self._persist = status if type(status) is bool else True<EOL>

Enables persistent mode for the current mock.

Returns:
    self: current Mock instance.


_append_funcs(self.filters, filters)<EOL>

Registers one o multiple request filters used during the matching
phase.

Arguments:
    *mappers (function): variadic mapper functions.

Returns:
    self: current Mock instance.


_append_funcs(self.mappers, mappers)<EOL>

Registers one o multiple request mappers used during the mapping
phase.

Arguments:
    *mappers (function): variadic mapper functions.

Returns:
    self: current Mock instance.


_append_funcs(self.callbacks, callbacks)<EOL>

Registers one or multiple callback that will be called every time the
current mock matches an outgoing HTTP request.

Arguments:
    *callbacks (function): callback functions to call.

Returns:
    self: current Mock instance.


self._delay = int(delay)<EOL>

Delay network response with certain milliseconds.
Only supported by asynchronous HTTP clients, such as ``aiohttp``.

Arguments:
    delay (int): milliseconds to delay response.

Returns:
    self: current Mock instance.


self._error = RuntimeError(error) if isinstance(error, str) else error<EOL>

Defines a simulated exception error that will be raised.

Arguments:
    error (str|Exception): error to raise.

Returns:
    self: current Mock instance.


def reply(self, status=<NUM_LIT:200>, new_response=False, **kw):

<EOL>res = Response(**kw) if new_response else self._response<EOL>res.status(status or res._status)<EOL>res.mock = self<EOL>self._response = res<EOL>return res<EOL>

Defines the mock response.

Arguments:
    status (int, optional): response status code. Defaults to ``200``.
    **kw (dict): optional keyword arguments passed to ``pook.Response``
        constructor.

Returns:
    pook.Response: mock response definition instance.


return self.reply(status=code)<EOL>

Defines the response status code.
Equivalent to ``self.reply(code)``.

Arguments:
    code (int): response status code. Defaults to ``200``.

Returns:
    pook.Response: mock response definition instance.


def response(self, status=<NUM_LIT:200>, **kw):

return self.reply(status=status, **kw)<EOL>

Defines the mock response. Alias to ``.reply()``

Arguments:
    status (int): response status code. Defaults to ``200``.
    **kw (dict): optional keyword arguments passed to ``pook.Response``
        constructor.

Returns:
    pook.Response: mock response definition instance.


return (self._persist and self._matches > <NUM_LIT:0>) or self._times <= <NUM_LIT:0><EOL>

Returns ``True`` if the mock has been matched by outgoing HTTP traffic.

Returns:
    bool: ``True`` if the mock was matched succesfully.


return self._matches > <NUM_LIT:0><EOL>

Returns ``True`` if the mock has been matched at least once time.

Returns:
    bool


return self.isdone()<EOL>

Attribute accessor that would be ``True`` if the current mock
is done, and therefore have been matched multiple times.

Returns:
    bool


return self._matches > <NUM_LIT:0><EOL>

Accessor property that would be ``True`` if the current mock
have been matched at least once.

See ``Mock.total_matches`` for more information.

Returns:
    bool


return self._matches<EOL>

Accessor property to retrieve the total number of times that the
current mock has been matched.

Returns:
    int


return self._calls<EOL>

Accessor to retrieve the mock match calls registry.

Returns:
    list[MockCall]


return len(self.matches)<EOL>

Accessor to retrieve the amount of mock matched calls.

Returns:
    int


<EOL>if self._times <= <NUM_LIT:0>:<EOL><INDENT>raise PookExpiredMock('<STR_LIT>')<EOL><DEDENT>for test in self.filters:<EOL><INDENT>if not test(request, self):<EOL><INDENT>return False, []<EOL><DEDENT><DEDENT>for mapper in self.mappers:<EOL><INDENT>request = mapper(request, self)<EOL>if not request:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>matches, errors = self.matchers.match(request)<EOL>if not matches:<EOL><INDENT>return False, errors<EOL><DEDENT>self._calls.append(request)<EOL>self._matches += <NUM_LIT:1><EOL>if not self._persist:<EOL><INDENT>self._times -= <NUM_LIT:1><EOL><DEDENT>if self._error:<EOL><INDENT>raise self._error<EOL><DEDENT>for callback in self.callbacks:<EOL><INDENT>callback(request, self)<EOL><DEDENT>return True, []<EOL>

Matches an outgoing HTTP request against the current mock matchers.

This method acts like a delegator to `pook.MatcherEngine`.

Arguments:
    request (pook.Request): request instance to match.

Raises:
    Exception: if the mock has an exception defined.

Returns:
    tuple(bool, list[Exception]): ``True`` if the mock matches
        the outgoing HTTP request, otherwise ``False``. Also returns
        an optional list of error exceptions.


<EOL>if isinstance(fn, Response):<EOL><INDENT>return fn.mock<EOL><DEDENT>if isinstance(fn, Mock):<EOL><INDENT>return fn<EOL><DEDENT>if not isfunction(fn) and not ismethod(fn):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>self._engine.remove_mock(self)<EOL>@functools.wraps(fn)<EOL>def decorator(*args, **kw):<EOL><INDENT>self._engine.add_mock(self)<EOL>engine_active = self._engine.active<EOL>if not engine_active:<EOL><INDENT>self._engine.activate()<EOL><DEDENT>try:<EOL><INDENT>return fn(*args, **kw)<EOL><DEDENT>finally:<EOL><INDENT>self._engine.remove_mock(self)<EOL>if not engine_active:<EOL><INDENT>self._engine.disable()<EOL><DEDENT><DEDENT><DEDENT>return decorator<EOL>

Overload Mock instance as callable object in order to be used
as decorator definition syntax.

Arguments:
    fn (function): function to decorate.

Returns:
    function or pook.Mock


keys = ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>args = []<EOL>for key in keys:<EOL><INDENT>if key == '<STR_LIT>':<EOL><INDENT>value = repr(self.matchers).replace('<STR_LIT>', '<STR_LIT>')<EOL>value = value[:-<NUM_LIT:2>] + '<STR_LIT>'<EOL><DEDENT>elif key == '<STR_LIT>':<EOL><INDENT>value = repr(self._response)<EOL>value = value[:-<NUM_LIT:1>] + '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>value = repr(getattr(self, '<STR_LIT:_>' + key))<EOL><DEDENT>args.append('<STR_LIT>'.format(key, value))<EOL><DEDENT>args = '<STR_LIT>'.format('<STR_LIT>'.join(args))<EOL>return type(self).__name__ + args<EOL>

Returns an human friendly readable instance data representation.

Returns:
    str


<EOL>if self._times == <NUM_LIT:1>:<EOL><INDENT>self._persist = True<EOL><DEDENT>if not self._engine.active:<EOL><INDENT>self._engine.activate()<EOL>self._disable_engine = True<EOL><DEDENT>return self<EOL>

Implements context manager enter interface.


def __exit__(self, etype, value, traceback):

<EOL>self._times = <NUM_LIT:0><EOL>if getattr(self, '<STR_LIT>', False):<EOL><INDENT>self._disable_engine = False<EOL>self._engine.disable()<EOL><DEDENT>if etype is not None:<EOL><INDENT>raise value<EOL><DEDENT>

Implements context manager exit interface.


_engine.debug = enable<EOL>

Enables or disables debug mode in the current mock engine.

Arguments:
    enable (bool): ``True`` to enable debug mode. Otherwise ``False``.


return _engine<EOL>

Returns the current running mock engine.

Returns:
    pook.Engine: current used engine.


Sets a custom mock engine, replacing the built-in one.

This is particularly useful if you want to replace the built-in
HTTP traffic mock interceptor engine with your custom one.

For mock engine implementation details, see `pook.MockEngine`.

Arguments:
    engine (pook.MockEngine): custom mock engine to use.


<EOL>if not isfunction(fn):<EOL><INDENT>_engine.activate()<EOL>return None<EOL><DEDENT>if iscoroutinefunction is not None and iscoroutinefunction(fn):<EOL><INDENT>return activate_async(fn, _engine)<EOL><DEDENT>@functools.wraps(fn)<EOL>def wrapper(*args, **kw):<EOL><INDENT>_engine.activate()<EOL>try:<EOL><INDENT>fn(*args, **kw)<EOL><DEDENT>finally:<EOL><INDENT>_engine.disable()<EOL><DEDENT><DEDENT>return wrapper<EOL>

Enables the HTTP traffic interceptors.

This function can be used as decorator.

Arguments:
    fn (function|coroutinefunction): Optional function argument
        if used as decorator.

Returns:
    function: decorator wrapper function, only if called as decorator,
        otherwise ``None``.

Example::

    # Standard use case
    pook.activate()
    pook.mock('server.com/foo').reply(404)

    res = requests.get('server.com/foo')
    assert res.status_code == 404
    pook.disable()

    # Decorator use case
    @pook.activate
    def test_request():
        pook.mock('server.com/foo').reply(404)

        res = requests.get('server.com/foo')
        assert res.status_code == 404


return activate(fn)<EOL>

Enables the HTTP traffic interceptors.
Alias to ``pook.activate()``.

Arguments:
    fn (function|coroutinefunction): Optional function argument
        if used as decorator.

Returns:
    function: decorator wrapper function, only if called as decorator.

Example::

    # Standard usage
    pook.on()
    pook.mock('server.com/foo').reply(404)

    res = requests.get('server.com/foo')
    assert res.status_code == 404

    # Usage as decorator
    @pook.on
    def test_request():
        pook.mock('server.com/foo').reply(404)

        res = requests.get('server.com/foo')
        assert res.status_code == 404


disable()<EOL>reset()<EOL>

Disables mock engine, HTTP traffic interceptors and flushed all the
registered mocks.

Internally, it calls ``pook.disable()`` and ``pook.off()``.


Resets current mock engine state, flushing all the registered mocks.

This action will not disable the mock engine.


global _engine<EOL>__engine = _engine<EOL>activated = __engine.active<EOL>if activated:<EOL><INDENT>__engine.disable()<EOL><DEDENT>_engine = Engine(network=network)<EOL>_engine.activate()<EOL>yield _engine<EOL>_engine.disable()<EOL>if network:<EOL><INDENT>_engine.disable_network()<EOL><DEDENT>_engine = __engine<EOL>if activated:<EOL><INDENT>_engine.activate()<EOL><DEDENT>

Creates a new isolated mock engine to be used via context manager.

Example::

    with pook.use() as engine:
        pook.mock('server.com/foo').reply(404)

        res = requests.get('server.com/foo')
        assert res.status_code == 404


with use(network=network) as engine:<EOL><INDENT>yield engine<EOL><DEDENT>

Create a new isolated mock engine to be used via context manager.

Semantic alias to ``pook.context()``.

Example::

    with pook.use() as engine:
        pook.mock('server.com/foo').reply(404)

        res = requests.get('server.com/foo')
        assert res.status_code == 404


with use(network=True) as engine:<EOL><INDENT>yield engine<EOL><DEDENT>

Creates a new mock engine to be used as context manager

Example::

    with pook.use_network() as engine:
        pook.mock('server.com/foo').reply(404)

        res = requests.get('server.com/foo')
        assert res.status_code == 404


Enables real networking mode for unmatched mocks in the current
mock engine.


Disables real traffic networking mode in the current mock engine.


Adds network filters to determine if certain outgoing
unmatched HTTP traffic can stablish real network connections.

Arguments:
    *fn (function): variadic function filter arguments to be used.


Flushes registered real networking filters in the current
mock engine.


return _engine.mock(url, **kw)<EOL>

Creates and register a new HTTP mock.

Arguments:
    url (str): request URL to mock.
    activate (bool): force mock engine activation.
        Defaults to ``False``.
    **kw (mixed): variadic keyword arguments.

Returns:
    pook.Mock: mock instance


return mock(url, method='<STR_LIT:GET>', **kw)<EOL>

Registers a new mock HTTP request with GET method.

Arguments:
    url (str): request URL to mock.
    activate (bool): force mock engine activation.
        Defaults to ``False``.
    **kw (mixed): variadic arguments to ``pook.Mock`` constructor.

Returns:
    pook.Mock: mock instance


return mock(url, method='<STR_LIT:POST>', **kw)<EOL>

Registers a new mock HTTP request with POST method.

Arguments:
    url (str): request URL to mock.
    activate (bool): force mock engine activation.
        Defaults to ``False``.
    **kw (mixed): variadic arguments to ``pook.Mock`` constructor.

Returns:
    pook.Mock: mock instance


return mock(url, method='<STR_LIT>', **kw)<EOL>

Registers a new mock HTTP request with PUT method.

Arguments:
    url (str): request URL to mock.
    activate (bool): force mock engine activation.
        Defaults to ``False``.
    **kw (mixed): variadic arguments to ``pook.Mock`` constructor.

Returns:
    pook.Mock: mock instance


Registers a new mock HTTP request with DELETE method.

Arguments:
    url (str): request URL to mock.
    activate (bool): force mock engine activation.
        Defaults to ``False``.
    **kw (mixed): variadic arguments to ``pook.Mock`` constructor.

Returns:
    pook.Mock: mock instance


Registers a new mock HTTP request with HEAD method.

Arguments:
    url (str): request URL to mock.
    activate (bool): force mock engine activation.
        Defaults to ``False``.
    **kw (mixed): variadic arguments to ``pook.Mock`` constructor.

Returns:
    pook.Mock: mock instance


Registers a new mock HTTP request with PATCH method.

Arguments:
    url (str): request URL to mock.
    activate (bool): force mock engine activation.
        Defaults to ``False``.
    **kw (mixed): variadic arguments to ``pook.Mock`` constructor.

Returns:
    pook.Mock: new mock instance.


Registers a new mock HTTP request with OPTIONS method.

Arguments:
    url (str): request URL to mock.
    activate (bool): force mock engine activation.
        Defaults to ``False``.
    **kw (mixed): variadic arguments to ``pook.Mock`` constructor.

Returns:
    pook.Mock: new mock instance.


return _engine.pending()<EOL>

Returns the numbers of pending mocks to be matched.

Returns:
    int: number of pending mocks to match.


return _engine.ispending()<EOL>


return _engine.pending_mocks()<EOL>

Returns pending mocks to be matched.

Returns:
    list: pending mock instances.


return _engine.unmatched_requests()<EOL>

Returns a ``tuple`` of unmatched requests.

Unmatched requests will be registered only if ``networking``
mode has been enabled.

Returns:
    list: unmatched intercepted requests.


return _engine.unmatched()<EOL>

Returns the total number of unmatched requests intercepted by pook.

Unmatched requests will be registered only if ``networking``
mode has been enabled.

Returns:
    int: total number of unmatched requests.


return _engine.isunmatched()<EOL>

Returns ``True`` if there are unmatched requests. Otherwise ``False``.

Unmatched requests will be registered only if ``networking``
mode has been enabled.

Returns:
    bool


return _engine.isactive()<EOL>

Returns ``True`` if pook is active and intercepting traffic.
Otherwise ``False``.

Returns:
    bool: True is all the registered mocks are gone, otherwise False.


return _engine.isdone()<EOL>

Returns True if all the registered mocks has been triggered.

Returns:
    bool: True is all the registered mocks are gone, otherwise False.


return re.compile(expression, flags=flags)<EOL>

Convenient shortcut to ``re.compile()`` for fast, easy to use
regular expression compilation without an extra import statement.

Arguments:
    expression (str): regular expression value.
    flags (int): optional regular expression flags.
        Defaults to ``re.IGNORECASE``

Returns:
    expression (str): string based regular expression.

Raises:
    Exception: in case of regular expression compilation error

Example::

    (pook
        .get('api.com/foo')
        .header('Content-Type', pook.regex('[a-z]{1,4}')))


@functools.wraps(fn)<EOL>def wrapper(self, *args, **kw):<EOL><INDENT>result = fn(self, *args, **kw)<EOL>return self if result is None else result<EOL><DEDENT>return wrapper<EOL>

Simple function decorator allowing easy method chaining.

Arguments:
    fn (function): target function to decorate.


if not isinstance(expr, str):<EOL><INDENT>return False<EOL><DEDENT>return all([<EOL>len(expr) > <NUM_LIT:3>,<EOL>expr.startswith('<STR_LIT>'),<EOL>expr.endswith('<STR_LIT:/>')<EOL>])<EOL>

Returns ``True`` is the given expression value is a regular expression
like string with prefix ``re/`` and suffix ``/``, otherwise ``False``.

Arguments:
    expr (mixed): expression value to test.

Returns:
    bool


if not value:<EOL><INDENT>return False<EOL><DEDENT>return any((isregex_expr(value), isinstance(value, retype)))<EOL>

Returns ``True`` if the input argument object is a native
regular expression object, otherwise ``False``.

Arguments:
    value (mixed): input value to test.

Returns:
    bool


return expr.replace[<NUM_LIT:3>:-<NUM_LIT:1>] if isregex_expr(expr) else expr<EOL>

Strips regular expression notation syntax characters from the given
string expression.

Arguments:
    expr (str): regular expression expression to strip

Returns:
    str


def compare(expr, value, regex_expr=False):

<EOL>if expr == value:<EOL><INDENT>return True<EOL><DEDENT>negate = False<EOL>if isinstance(expr, str):<EOL><INDENT>negate = expr.startswith(NEGATE)<EOL>expr = strip_negate(expr) if negate else expr<EOL><DEDENT>try:<EOL><INDENT>test(expr, value, regex_expr=regex_expr)<EOL><DEDENT>except Exception as err:<EOL><INDENT>if negate:<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>raise err<EOL><DEDENT><DEDENT>return True<EOL>

Compares an string or regular expression againast a given value.

Arguments:
    expr (str|regex): string or regular expression value to compare.
    value (str): value to compare against to.
    regex_expr (bool, optional): enables string based regex matching.

Raises:
    AssertionError: in case of assertion error.

Returns:
    bool


self._status = int(code)<EOL>

Defines the response status code.

Arguments:
    code (int): response status code.

Returns:
    self: ``pook.Response`` current instance.


@fluent<EOL><INDENT>def header(self, key, value):<DEDENT>

if type(key) is tuple:<EOL><INDENT>key, value = str(key[<NUM_LIT:0>]), key[<NUM_LIT:1>]<EOL><DEDENT>headers = {key: value}<EOL>self._headers.extend(headers)<EOL>

Defines a new response header.
Alias to ``Response.header()``.

Arguments:
    header (str): header name.
    value (str): header value.

Returns:
    self: ``pook.Response`` current instance.


@fluent<EOL><INDENT>def set(self, header, value):<DEDENT>

self._headers[header] = value<EOL>


self.content(name)<EOL>

Defines the response ``Content-Type`` header.
Alias to ``Response.content(mime)``.

You can pass one of the following type aliases instead of the full
MIME type representation:

- ``json`` = ``application/json``
- ``xml`` = ``application/xml``
- ``html`` = ``text/html``
- ``text`` = ``text/plain``
- ``urlencoded`` = ``application/x-www-form-urlencoded``
- ``form`` = ``application/x-www-form-urlencoded``
- ``form-data`` = ``application/x-www-form-urlencoded``

Arguments:
    value (str): type alias or header value to match.

Returns:
    self: ``pook.Response`` current instance.


self._headers['<STR_LIT:Content-Type>'] = TYPES.get(name, name)<EOL>

Defines the response ``Content-Type`` header.

You can pass one of the following type aliases instead of the full
MIME type representation:

- ``json`` = ``application/json``
- ``xml`` = ``application/xml``
- ``html`` = ``text/html``
- ``text`` = ``text/plain``
- ``urlencoded`` = ``application/x-www-form-urlencoded``
- ``form`` = ``application/x-www-form-urlencoded``
- ``form-data`` = ``application/x-www-form-urlencoded``

Arguments:
    value (str): type alias or header value to match.

Returns:
    self: ``pook.Response`` current instance.


if isinstance(body, bytes):<EOL><INDENT>body = body.decode('<STR_LIT:utf-8>')<EOL><DEDENT>self._body = body<EOL>

Defines response body data.

Arguments:
    body (str|bytes): response body to use.

Returns:
    self: ``pook.Response`` current instance.


self._headers['<STR_LIT:Content-Type>'] = '<STR_LIT:application/json>'<EOL>if not isinstance(data, str):<EOL><INDENT>data = json.dumps(data, indent=<NUM_LIT:4>)<EOL><DEDENT>self._body = data<EOL>

Defines the mock response JSON body.

Arguments:
    data (dict|list|str): JSON body data.

Returns:
    self: ``pook.Response`` current instance.


self.body(xml)<EOL>

Defines the mock response XML body.

For not it only supports ``str`` as input type.

Arguments:
    xml (str): XML body data to use.

Returns:
    self: ``pook.Response`` current instance.


with open(path, '<STR_LIT:r>') as f:<EOL><INDENT>self.body = str(f.read())<EOL><DEDENT>

Defines the response body from file contents.

Arguments:
    path (str): disk file path to load.

Returns:
    self: ``pook.Response`` current instance.


return self._mock<EOL>


self._mock = mock<EOL>


args = []<EOL>for key in ('<STR_LIT>', '<STR_LIT:status>', '<STR_LIT:body>'):<EOL><INDENT>value = getattr(self, '<STR_LIT>'.format(key))<EOL>args.append('<STR_LIT>'.format(key, value))<EOL><DEDENT>return '<STR_LIT>'.format('<STR_LIT>'.join(args))<EOL>

Returns an human friendly readable instance data representation.

Returns:
    str


def _get_anagrams(word, sowpods=False, start="<STR_LIT>", end="<STR_LIT>"):

return [result for result in anagrams_in_word(word, sowpods, start, end)]<EOL>

Run the anagrams_in_word generator until its done, return as a list.


def pretty_print(input_word, anagrams, by_length=False):

scores = {}<EOL>if by_length:<EOL><INDENT>noun = "<STR_LIT>"<EOL>for word, score in anagrams:<EOL><INDENT>try:<EOL><INDENT>scores[len(word)].append("<STR_LIT>".format(word, score))<EOL><DEDENT>except KeyError:<EOL><INDENT>scores[len(word)] = ["<STR_LIT>".format(word, score)]<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>noun = "<STR_LIT>"<EOL>for word, score in anagrams:<EOL><INDENT>try:<EOL><INDENT>scores[score].append(word)<EOL><DEDENT>except KeyError:<EOL><INDENT>scores[score] = [word]<EOL><DEDENT><DEDENT><DEDENT>print("<STR_LIT>".format(input_word, "<STR_LIT>" * by_length))<EOL>if not valid_scrabble_word(input_word):<EOL><INDENT>print("<STR_LIT>".format(input_word))<EOL><DEDENT>for key, value in sorted(scores.items(), reverse=True):<EOL><INDENT>print("<STR_LIT>".format(key, noun, "<STR_LIT:U+002CU+0020>".join(value)))<EOL><DEDENT>

Prints the anagram results sorted by score to stdout.

    Args:
        input_word: the base word we searched on
        anagrams: generator of (word, score) from anagrams_in_word
        by_length: a boolean to declare printing by length instead of score


Argparse logic, command line options.

    Args:
        args: sys.argv[1:], everything passed to the program after its name

    Returns:
        A tuple of:
            a list of words/letters to search
            a boolean to declare if we want to use the sowpods words file
            a boolean to declare if we want to output anagrams by length
            a string of starting characters to find anagrams based on
            a string of ending characters to find anagrams based on

    Raises:
        SystemExit if the user passes invalid arguments, --version or --help


if not arguments:<EOL><INDENT>arguments = sys.argv[<NUM_LIT:1>:]<EOL><DEDENT>wordlist, sowpods, by_length, start, end = argument_parser(arguments)<EOL>for word in wordlist:<EOL><INDENT>pretty_print(<EOL>word,<EOL>anagrams_in_word(word, sowpods, start, end),<EOL>by_length,<EOL>)<EOL><DEDENT>


score_map = {<EOL><NUM_LIT:1>: ["<STR_LIT:a>", "<STR_LIT:e>", "<STR_LIT:i>", "<STR_LIT:o>", "<STR_LIT:u>", "<STR_LIT:l>", "<STR_LIT:n>", "<STR_LIT:r>", "<STR_LIT:s>", "<STR_LIT:t>"],<EOL><NUM_LIT:2>: ["<STR_LIT:d>", "<STR_LIT:g>"],<EOL><NUM_LIT:3>: ["<STR_LIT:b>", "<STR_LIT:c>", "<STR_LIT:m>", "<STR_LIT:p>"],<EOL><NUM_LIT:4>: ["<STR_LIT:f>", "<STR_LIT:h>", "<STR_LIT:v>", "<STR_LIT:w>", "<STR_LIT:y>"],<EOL><NUM_LIT:5>: ["<STR_LIT:k>"],<EOL><NUM_LIT:8>: ["<STR_LIT>", "<STR_LIT:x>"],<EOL><NUM_LIT:10>: ["<STR_LIT:q>", "<STR_LIT:z>"],<EOL>}<EOL>for score, letters in score_map.items():<EOL><INDENT>if letter.lower() in letters:<EOL><INDENT>return score<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise TypeError("<STR_LIT>", letter)<EOL><DEDENT>

Returns the Scrabble score of a letter.

    Args:
        letter: a single character string

    Raises:
        TypeError if a non-Scrabble character is supplied


def word_score(word, input_letters, questions=<NUM_LIT:0>):

score = <NUM_LIT:0><EOL>bingo = <NUM_LIT:0><EOL>filled_by_blanks = []<EOL>rack = list(input_letters)  <EOL>for letter in word:<EOL><INDENT>if letter in rack:<EOL><INDENT>bingo += <NUM_LIT:1><EOL>score += letter_score(letter)<EOL>rack.remove(letter)<EOL><DEDENT>else:<EOL><INDENT>filled_by_blanks.append(letter_score(letter))<EOL><DEDENT><DEDENT>for blank_score in sorted(filled_by_blanks, reverse=True):<EOL><INDENT>if questions > <NUM_LIT:0>:<EOL><INDENT>score += blank_score<EOL>questions -= <NUM_LIT:1><EOL><DEDENT><DEDENT>if bingo > <NUM_LIT:6>:<EOL><INDENT>score += <NUM_LIT:50><EOL><DEDENT>return score<EOL>

Checks the Scrabble score of a single word.

    Args:
        word: a string to check the Scrabble score of
        input_letters: the letters in our rack
        questions: integer of the tiles already on the board to build on

    Returns:
        an integer Scrabble score amount for the word


blanks = <NUM_LIT:0><EOL>questions = <NUM_LIT:0><EOL>input_letters = []<EOL>for letter in input_word:<EOL><INDENT>if letter == "<STR_LIT:_>":<EOL><INDENT>blanks += <NUM_LIT:1><EOL><DEDENT>elif letter == "<STR_LIT:?>":<EOL><INDENT>questions += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>input_letters.append(letter)<EOL><DEDENT><DEDENT>return input_letters, blanks, questions<EOL>

Searches a string for blank tile characters ("?" and "_").

    Args:
        input_word: the user supplied string to search through

    Returns:
        a tuple of:
            input_word without blanks
            integer number of blanks (no points)
            integer number of questions (points)


def word_list(sowpods=False, start="<STR_LIT>", end="<STR_LIT>"):

location = os.path.join(<EOL>os.path.dirname(os.path.realpath(__file__)),<EOL>"<STR_LIT>",<EOL>)<EOL>if sowpods:<EOL><INDENT>filename = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>filename = "<STR_LIT>"<EOL><DEDENT>filepath = os.path.join(location, filename)<EOL>with open(filepath) as wordfile:<EOL><INDENT>for word in wordfile.readlines():<EOL><INDENT>word = word.strip()<EOL>if start and end and word.startswith(start) and word.endswith(end):<EOL><INDENT>yield word<EOL><DEDENT>elif start and word.startswith(start) and not end:<EOL><INDENT>yield word<EOL><DEDENT>elif end and word.endswith(end) and not start:<EOL><INDENT>yield word<EOL><DEDENT>elif not start and not end:<EOL><INDENT>yield word<EOL><DEDENT><DEDENT><DEDENT>

Opens the word list file.

    Args:
        sowpods: a boolean to declare using the sowpods list or TWL (default)
        start: a string of starting characters to find anagrams based on
        end: a string of ending characters to find anagrams based on

    Yeilds:
        a word at a time out of 178691 words for TWL, 267751 for sowpods. Much
        less if either start or end are used (filtering is applied here)


letters_in_bag = {<EOL>"<STR_LIT:a>": <NUM_LIT:9>,<EOL>"<STR_LIT:b>": <NUM_LIT:2>,<EOL>"<STR_LIT:c>": <NUM_LIT:2>,<EOL>"<STR_LIT:d>": <NUM_LIT:4>,<EOL>"<STR_LIT:e>": <NUM_LIT:12>,<EOL>"<STR_LIT:f>": <NUM_LIT:2>,<EOL>"<STR_LIT:g>": <NUM_LIT:3>,<EOL>"<STR_LIT:h>": <NUM_LIT:2>,<EOL>"<STR_LIT:i>": <NUM_LIT:9>,<EOL>"<STR_LIT>": <NUM_LIT:1>,<EOL>"<STR_LIT:k>": <NUM_LIT:1>,<EOL>"<STR_LIT:l>": <NUM_LIT:4>,<EOL>"<STR_LIT:m>": <NUM_LIT:2>,<EOL>"<STR_LIT:n>": <NUM_LIT:6>,<EOL>"<STR_LIT:o>": <NUM_LIT:8>,<EOL>"<STR_LIT:p>": <NUM_LIT:2>,<EOL>"<STR_LIT:q>": <NUM_LIT:1>,<EOL>"<STR_LIT:r>": <NUM_LIT:6>,<EOL>"<STR_LIT:s>": <NUM_LIT:4>,<EOL>"<STR_LIT:t>": <NUM_LIT:6>,<EOL>"<STR_LIT:u>": <NUM_LIT:4>,<EOL>"<STR_LIT:v>": <NUM_LIT:2>,<EOL>"<STR_LIT:w>": <NUM_LIT:2>,<EOL>"<STR_LIT:x>": <NUM_LIT:1>,<EOL>"<STR_LIT:y>": <NUM_LIT:2>,<EOL>"<STR_LIT:z>": <NUM_LIT:1>,<EOL>"<STR_LIT:_>": <NUM_LIT:2>,<EOL>}<EOL>for letter in word:<EOL><INDENT>if letter == "<STR_LIT:?>":<EOL><INDENT>continue<EOL><DEDENT>try:<EOL><INDENT>letters_in_bag[letter] -= <NUM_LIT:1><EOL><DEDENT>except KeyError:<EOL><INDENT>return False<EOL><DEDENT>if letters_in_bag[letter] < <NUM_LIT:0>:<EOL><INDENT>letters_in_bag["<STR_LIT:_>"] -= <NUM_LIT:1><EOL>if letters_in_bag["<STR_LIT:_>"] < <NUM_LIT:0>:<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>return True<EOL>

Checks if the input word could be played with a full bag of tiles.

    Returns:
        True or false


lmap = {}<EOL>for letter in word:<EOL><INDENT>try:<EOL><INDENT>lmap[letter] += <NUM_LIT:1><EOL><DEDENT>except KeyError:<EOL><INDENT>lmap[letter] = <NUM_LIT:1><EOL><DEDENT><DEDENT>return lmap<EOL>

Creates a map of letter use in a word.

    Args:
        word: a string to create a letter map from

    Returns:
        a dictionary of {letter: integer count of letter in word}


def anagrams_in_word(word, sowpods=False, start="<STR_LIT>", end="<STR_LIT>"):

input_letters, blanks, questions = blank_tiles(word)<EOL>for tile in start + end:<EOL><INDENT>input_letters.append(tile)<EOL><DEDENT>for word in word_list(sowpods, start, end):<EOL><INDENT>lmap = _letter_map(input_letters)<EOL>used_blanks = <NUM_LIT:0><EOL>for letter in word:<EOL><INDENT>if letter in lmap:<EOL><INDENT>lmap[letter] -= <NUM_LIT:1><EOL>if lmap[letter] < <NUM_LIT:0>:<EOL><INDENT>used_blanks += <NUM_LIT:1><EOL>if used_blanks > (blanks + questions):<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>used_blanks += <NUM_LIT:1><EOL>if used_blanks > (blanks + questions):<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>yield (word, word_score(word, input_letters, questions))<EOL><DEDENT><DEDENT>

Finds anagrams in word.

    Args:
        word: the string to base our search off of
        sowpods: boolean to declare TWL or SOWPODS words file
        start: a string of starting characters to find anagrams based on
        end: a string of ending characters to find anagrams based on

    Yields:
        a tuple of (word, score) that can be made with the input_word


with io.open(filename, encoding="<STR_LIT:utf-8>") as version_file:<EOL><INDENT>version_match = re.search(r'<STR_LIT>',<EOL>version_file.read(), re.M)<EOL><DEDENT>if version_match:<EOL><INDENT>return version_match.group(<NUM_LIT:1>)<EOL><DEDENT>return "<STR_LIT>"<EOL>

Uses re to pull out the assigned value to __version__ in filename.


TestCommand.finalize_options(self)<EOL>self.test_args = (<EOL>"<STR_LIT>".split()<EOL>)<EOL>self.test_suite = True<EOL>


def askQuestion(question, required = True, answers = dict(), tries = <NUM_LIT:3>):

print(question)<EOL>if not answers:<EOL><INDENT>answer = getUtility(ISelectedChoice, '<STR_LIT>').selection()<EOL>_attempts = <NUM_LIT:0><EOL>while True:<EOL><INDENT>if tries and _attempts > tries:<EOL><INDENT>print ("<STR_LIT>")<EOL>return None<EOL><DEDENT>if not required or answer:<EOL><INDENT>return answer if answer else None<EOL><DEDENT>print ("<STR_LIT>")<EOL>answer = getUtility(ISelectedChoice, '<STR_LIT>').selection()<EOL>_attempts += <NUM_LIT:1><EOL><DEDENT><DEDENT>for selection_pair in answers:<EOL><INDENT>for key, potential_answer in selection_pair.items():<EOL><INDENT>print("<STR_LIT:(>" + key + "<STR_LIT>" + potential_answer)<EOL>break <EOL><DEDENT><DEDENT>_attempts = <NUM_LIT:0><EOL>while True:<EOL><INDENT>answer = getUtility(ISelectedChoice, '<STR_LIT>').selection()<EOL>if tries and _attempts > tries:<EOL><INDENT>print(_("<STR_LIT>"))<EOL>return None<EOL><DEDENT>if not answer and not required:<EOL><INDENT>return None<EOL><DEDENT>for selection_pair in answers:<EOL><INDENT>if answer in selection_pair:<EOL><INDENT>return answer<EOL><DEDENT><DEDENT>print(("<STR_LIT>".format(answer)))<EOL>answer = getUtility(ISelectedChoice, '<STR_LIT>').selection()<EOL>_attempts += <NUM_LIT:1><EOL><DEDENT>

Ask a question to STDOUT and return answer from STDIN

    Args:
        question: A string question that will be printed to stdout

    Kwargs:
        required: True indicates the question must be answered
        answers: A sequence of dicts with key value pairs where the key will
            be a user-selectable choice menu item with the value as its related
            description.
        tries: Integer value of maximum amount of attempts users may exercise
            to select a valid answer

    Returns:
        A user specified string when required is False and answers was not
        provided. A user selected key from answers when required is True and
        answers was provided.  None if required was false and user did not
        enter a answer.  None if required was True and user reached reached
        maximum limit of tries.


return input()<EOL>


tags = VcfRecord._EMPTY_SET<EOL>if self.sample_tag_values:<EOL><INDENT>first_sample = list(self.sample_tag_values.keys())[<NUM_LIT:0>]<EOL>tags = set(self.sample_tag_values[first_sample].keys())<EOL><DEDENT>return tags<EOL>


def vcf_records(self, format_tags=None, qualified=False):

if qualified:<EOL><INDENT>sample_names = self.qualified_sample_names<EOL><DEDENT>else:<EOL><INDENT>sample_names = self.sample_names<EOL><DEDENT>for line in self._file_reader.read_lines():<EOL><INDENT>if line.startswith("<STR_LIT:#>"):<EOL><INDENT>continue<EOL><DEDENT>vcf_record = vcf.VcfRecord.parse_record(line, sample_names)<EOL>if format_tags:<EOL><INDENT>vcf_record = self.modify_format_tag(vcf_record, format_tags)<EOL><DEDENT>yield vcf_record<EOL><DEDENT>

Generates parsed VcfRecord objects.

        Typically called in a for loop to process each vcf record in a
        VcfReader. VcfReader must be opened in advanced and closed when
        complete. Skips all headers.

        Args:
            qualified: When True, sample names are prefixed with file name

        Returns:
            Parsed VcfRecord

        Raises:
            StopIteration: when reader is exhausted.
            TypeError: if reader is closed.


if qualified:<EOL><INDENT>sample_names = self.qualified_sample_names<EOL><DEDENT>else:<EOL><INDENT>sample_names = self.sample_names<EOL><DEDENT>for line in self._file_reader.read_lines():<EOL><INDENT>if line.startswith("<STR_LIT:#>"):<EOL><INDENT>continue<EOL><DEDENT>yield VcfRecord.parse_record(line, sample_names)<EOL><DEDENT>


@classmethod<EOL><INDENT>def parse_record(cls, vcf_line, sample_names):<DEDENT>

vcf_fields = vcf_line.rstrip("<STR_LIT:\r\n>").split("<STR_LIT:\t>")<EOL>chrom, pos, rid, ref, alt, qual, rfilter, info= vcf_fields[<NUM_LIT:0>:<NUM_LIT:8>]<EOL>sample_fields = []<EOL>sample_tag_values = {}<EOL>if len(vcf_fields) > <NUM_LIT:9>:<EOL><INDENT>rformat = vcf_fields[<NUM_LIT:8>]<EOL>sample_fields = vcf_fields[<NUM_LIT:9>:]<EOL>sample_tag_values = VcfRecord._sample_tag_values(sample_names,<EOL>rformat,<EOL>sample_fields)<EOL><DEDENT>return VcfRecord(chrom, pos, ref, alt,<EOL>rid, qual, rfilter, info,<EOL>sample_tag_values)<EOL>

Alternative constructor that parses VcfRecord from VCF string.

        Aspire to parse/represent the data such that it could be reliably
        round-tripped. (This nicety means INFO fields and FORMAT tags should be
        treated as ordered to avoid shuffling.)

        Args:
            vcf_line: the VCF variant record as a string; tab separated fields,
                trailing newlines are ignored. Must have at least 8 fixed fields
                (through INFO)
            sample_names: a list of sample name strings; these should match
                the VCF header column
        Returns:
            A mutable VcfRecord.


@classmethod<EOL><INDENT>def _sample_tag_values(cls, sample_names, rformat, sample_fields):<DEDENT>

sample_tag_values = OrderedDict()<EOL>tag_names = VcfRecord._format_list(rformat)<EOL>for i, sample_field in enumerate(sample_fields):<EOL><INDENT>tag_values = sample_field.split("<STR_LIT::>") if sample_field else "<STR_LIT:.>"<EOL>sample_tag_values[sample_names[i]] = OrderedDict(zip(tag_names,<EOL>tag_values))<EOL><DEDENT>return sample_tag_values<EOL>

Creates a sample dict of tag-value dicts for a single variant record.

        Args:
            sample_names: list of sample name strings.
            rformat: record format string (from VCF record).
            sample_fields: list of strings where each string is the ';'
                seperated format values for an individual sample.

        Returns:
            An dict of samples, where each key is a sample and each value
                is an dict of format-values. See attribute below for example.
                Will return '.' if no values for sampe field.


def __init__(self, chrom, pos, ref, alt,<EOL>vcf_id="<STR_LIT:.>", qual="<STR_LIT:.>", vcf_filter="<STR_LIT:.>", info="<STR_LIT:.>",<EOL>sample_tag_values=None):

self.chrom = chrom<EOL>self.pos = pos<EOL>self.vcf_id = vcf_id<EOL>self.ref = ref<EOL>self.alt = alt<EOL>self.qual = qual<EOL>self.filter = vcf_filter<EOL>self.info = info<EOL>self.info_dict = self._init_info_dict()<EOL>if sample_tag_values is None:<EOL><INDENT>self.sample_tag_values = OrderedDict()<EOL><DEDENT>else:<EOL><INDENT>self.sample_tag_values = sample_tag_values<EOL><DEDENT>self._key = self._build_key()<EOL>

Builds a mutable VcfRecord from constituent VCF fields.

        If building from a VCF record line, see VcfRecord.parse_record.


if field in self.info_dict:<EOL><INDENT>msg = "<STR_LIT>".format(field)<EOL>raise KeyError(msg)<EOL><DEDENT>if "<STR_LIT:=>" in field:<EOL><INDENT>key, value = field.split("<STR_LIT:=>")<EOL>self.info_dict[key] = value<EOL><DEDENT>else:<EOL><INDENT>self.info_dict[field] = field<EOL><DEDENT>self._join_info_fields()<EOL>

Adds new info field (flag or key=value pair).

        Args:
            field: String flag (e.g. "SOMATIC") or key-value ("NEW_DP=42")

        Raises:
            KeyError: if info field already exists


if self.info_dict:<EOL><INDENT>info_fields = []<EOL>if len(self.info_dict) > <NUM_LIT:1>:<EOL><INDENT>self.info_dict.pop("<STR_LIT:.>", None)<EOL><DEDENT>for field, value in self.info_dict.items():<EOL><INDENT>if field == value:<EOL><INDENT>info_fields.append(value)<EOL><DEDENT>else:<EOL><INDENT>info_fields.append("<STR_LIT:=>".join([field, value]))<EOL><DEDENT><DEDENT>self.info = "<STR_LIT:;>".join(info_fields)<EOL><DEDENT>else:<EOL><INDENT>self.info = "<STR_LIT:.>"<EOL><DEDENT>


format_field = "<STR_LIT:.>"<EOL>if self.sample_tag_values:<EOL><INDENT>first_sample = list(self.sample_tag_values.keys())[<NUM_LIT:0>]<EOL>tag_names = self.sample_tag_values[first_sample].keys()<EOL>if tag_names:<EOL><INDENT>format_field = "<STR_LIT::>".join(tag_names)<EOL><DEDENT><DEDENT>return format_field<EOL>

Returns string representation of format field.


tag_values = self.sample_tag_values[sample].values()<EOL>if tag_values:<EOL><INDENT>return "<STR_LIT::>".join(tag_values)<EOL><DEDENT>else:<EOL><INDENT>return "<STR_LIT:.>"<EOL><DEDENT>

Returns string representation of sample-format values.

        Raises:
            KeyError: if requested sample is not defined.


stringifier = [self.chrom, self.pos, self.vcf_id, self.ref, self.alt,<EOL>self.qual, self.filter, self.info,<EOL>self._format_field()]<EOL>for sample in self.sample_tag_values:<EOL><INDENT>stringifier.append(self._sample_field(sample))<EOL><DEDENT>return "<STR_LIT:\t>".join(stringifier) + "<STR_LIT:\n>"<EOL>

Returns tab-delimited, newline terminated string of VcfRecord.


def add_sample_tag_value(self, tag_name, new_sample_values):

if tag_name in self.format_tags:<EOL><INDENT>msg = "<STR_LIT>".format(tag_name)<EOL>raise KeyError(msg)<EOL><DEDENT>if not self._samples_match(new_sample_values):<EOL><INDENT>raise KeyError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>for sample in self.sample_tag_values.keys():<EOL><INDENT>value = str(new_sample_values[sample])<EOL>self.sample_tag_values[sample][tag_name] = value<EOL><DEDENT>

Appends a new format tag-value for all samples.

        Args:
            tag_name: string tag name; must not already exist
            new_sample

        Raises:
            KeyError: if tag_name to be added already exists


if self.filter.lower() in self._FILTERS_TO_REPLACE:<EOL><INDENT>self.filter = new_filter<EOL><DEDENT>elif new_filter not in self.filter.split("<STR_LIT:;>"):<EOL><INDENT>self.filter = "<STR_LIT:;>".join([self.filter,<EOL>new_filter])<EOL><DEDENT>

Replaces null or blank filter or adds filter to existing list.


try:<EOL><INDENT>values = tag_values[dependent_tag_id].split("<STR_LIT:U+002C>")<EOL>return max([float(value) for value in values])<EOL><DEDENT>except KeyError:<EOL><INDENT>return None<EOL><DEDENT>except ValueError:<EOL><INDENT>return None<EOL><DEDENT>

Extract (float) value of dependent tag or None if absent.


def _init_population_stats(self, vcf_reader, dependent_tag_id):

<EOL>n = <NUM_LIT:0><EOL>mean = <NUM_LIT:0><EOL>M2 = <NUM_LIT:0><EOL>try:<EOL><INDENT>vcf_reader.open()<EOL>for vcf_record in vcf_reader.vcf_records():<EOL><INDENT>for tag_values in vcf_record.sample_tag_values.values():<EOL><INDENT>value = self._get_dependent_value(tag_values,<EOL>dependent_tag_id)<EOL>if value is not None:<EOL><INDENT>n += <NUM_LIT:1><EOL>delta = value - mean<EOL>mean += delta / n<EOL>M2 += delta * (value - mean)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>vcf_reader.close()<EOL><DEDENT>mean = round(mean, self._MAX_PRECISION)<EOL>stdev = <NUM_LIT:0><EOL>if n == <NUM_LIT:0>:<EOL><INDENT>mean = None<EOL>stdev = None<EOL><DEDENT>elif n >= <NUM_LIT:2>:<EOL><INDENT>variance = M2/n<EOL>stdev = round(math.sqrt(variance), self._MAX_PRECISION)<EOL><DEDENT>return mean, stdev<EOL>

Derive mean and stdev.

        Adapted from online variance algorithm from Knuth, The Art of Computer
        Programming, volume 2

        Returns: mean and stdev when len(values) > 1, otherwise (None, None)
            Values rounded to _MAX_PRECISION to ameliorate discrepancies between
            python versions.


global log_filename<EOL>log_filename = os.path.join(os.getcwd(), "<STR_LIT>")<EOL>if args.log_file:<EOL><INDENT>_validate_log_file(args.log_file)<EOL>log_filename = args.log_file<EOL><DEDENT>logging.basicConfig(format=_FILE_LOG_FORMAT,<EOL>level="<STR_LIT>",<EOL>datefmt=_DATE_FORMAT,<EOL>filename=log_filename)<EOL>global _verbose<EOL>if args.verbose:<EOL><INDENT>_verbose = args.verbose<EOL><DEDENT>start_time = datetime.now().strftime(_DATE_FORMAT)<EOL>global _logging_dict<EOL>_logging_dict = {'<STR_LIT:user>': getpass.getuser(),<EOL>'<STR_LIT:host>': socket.gethostname(),<EOL>'<STR_LIT>': start_time,<EOL>'<STR_LIT>': args.subparser_name}<EOL>

Sets command name and formatting for subsequent calls to logger


message = self._remessage_invalid_subparser(message)<EOL>raise utils.UsageError(message)<EOL>


(prefix_to_readers,<EOL>filter_files,<EOL>unclaimed_set) = self._find_varscan_files(file_readers)<EOL>prefix_by_patients = self._split_prefix_by_patient(prefix_to_readers)<EOL>self._validate_vcf_readers(prefix_by_patients)<EOL>vcf_hc_pairs = self._pair_files(prefix_to_readers, filter_files)<EOL>self._validate_vcf_hc_pairs(vcf_hc_pairs)<EOL>vcf_readers = self._create_vcf_readers(vcf_hc_pairs)<EOL>return list(unclaimed_set), vcf_readers<EOL>

Recognizes and claims VarScan VCFs form the set of all input VCFs.

        Each defined caller has a chance to evaluate and claim all the incoming
        files as something that it can process. Since VarScan can claim
        high-confidence files as well, this process is significantly more
        complex than for other callers.

        Args:
            file_readers: the collection of currently unclaimed files

        Returns:
            A tuple of unclaimed readers and VarScanVcfReaders.


(prefix_to_reader,<EOL>unclaimed_readers) = self._find_strelka_files(file_readers)<EOL>prefix_by_patients = self._split_prefix_by_patient(prefix_to_reader)<EOL>self._validate_vcf_readers(prefix_by_patients)<EOL>vcf_readers = self._create_vcf_readers(prefix_to_reader)<EOL>return (unclaimed_readers, vcf_readers)<EOL>

Recognizes and claims Strelka VCFs form the set of all input VCFs.

        Each defined caller has a chance to evaluate and claim all the incoming
        files as something that it can process.

        Args:
            file_readers: the collection of currently unclaimed files

        Returns:
            A tuple of unclaimed readers and StrelkaVcfReaders.


claimed_vcf_readers = []<EOL>for caller in self._callers:<EOL><INDENT>(unclaimed_file_readers,<EOL>translated_vcf_readers) = caller.claim(unclaimed_file_readers)<EOL>claimed_vcf_readers.extend(translated_vcf_readers)<EOL><DEDENT>return unclaimed_file_readers, claimed_vcf_readers<EOL>

Allows each caller to claim incoming files as they are recognized.

        Args:
            unclaimed_file_readers: Usually, all files in the input dir.

        Returns:
            A tuple of unclaimed file readers and claimed VcfReaders. The
            presence of any unclaimed file readers could indicate stray files
            in the input dir.


unclaimed_readers = []<EOL>vcf_readers = []<EOL>for file_reader in file_readers:<EOL><INDENT>if self._is_mutect_vcf(file_reader):<EOL><INDENT>vcf_reader = vcf.VcfReader(file_reader)<EOL>vcf_readers.append(_MutectVcfReader(vcf_reader))<EOL><DEDENT>else:<EOL><INDENT>unclaimed_readers.append(file_reader)<EOL><DEDENT><DEDENT>return (unclaimed_readers, vcf_readers)<EOL>

Recognizes and claims MuTect VCFs form the set of all input VCFs.

        Each defined caller has a chance to evaluate and claim all the incoming
        files as something that it can process.

        Args:
            file_readers: the collection of currently unclaimed files

        Returns:
            A tuple of unclaimed readers and MuTectVcfReaders.


mutect_dict = self._build_mutect_dict(vcf_reader.metaheaders)<EOL>new_header_list = []<EOL>required_keys = set([self._NORMAL_SAMPLE_KEY, self._TUMOR_SAMPLE_KEY])<EOL>mutect_keys = set(mutect_dict.keys())<EOL>if not required_keys.issubset(mutect_keys):<EOL><INDENT>raise utils.JQException("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>for field_name in vcf_reader.column_header.split("<STR_LIT:\t>"):<EOL><INDENT>if field_name == mutect_dict[self._NORMAL_SAMPLE_KEY]:<EOL><INDENT>field_name = "<STR_LIT>"<EOL><DEDENT>elif field_name == mutect_dict[self._TUMOR_SAMPLE_KEY]:<EOL><INDENT>field_name = "<STR_LIT>"<EOL><DEDENT>new_header_list.append(field_name)<EOL><DEDENT>return "<STR_LIT:\t>".join(new_header_list)<EOL>

Returns a standardized column header.

        MuTect sample headers include the name of input alignment, which is
        nice, but doesn't match up with the sample names reported in Strelka
        or VarScan. To fix this, we replace with NORMAL and TUMOR using the
        MuTect metadata command line to replace them correctly.


Returns a tuple of input and output types.

    Possible values are:
         'file'
         'directory'


returns a list of output file names be be created


Adds format tag and sample values to a VCF record


Recognizes and claims MuTect VCFs form the set of all input VCFs.

        Each defined caller has a chance to evaluate and claim all the incoming
        files as something that it can process.

        Args:
            file_readers: the collection of currently unclaimed files

        Returns:
            A tuple of unclaimed readers and NewVariantCallerReaders.


returns a list of file groupings (eg ['snp', 'indel']) to be used
        to later validate that the files are paired together correctly


with open(os.path.join(*paths), '<STR_LIT:r>') as filename:<EOL><INDENT>return filename.read()<EOL><DEDENT>

Build a file path from *paths* and return the contents.


rel_name = self.through._meta.get_field('<STR_LIT>').remote_field.get_accessor_name()<EOL>return getattr(self.instance, rel_name)<EOL>

Return the manager that handles the relation from this instance to the tagged_item class.
        If content_object on the tagged_item class is defined as a ParentalKey, this will be a
        DeferringRelatedManager which allows writing related objects without committing them
        to the database.


<EOL>all_unique_checks = set()<EOL>all_date_checks = set()<EOL>forms_to_delete = self.deleted_forms<EOL>valid_forms = [form for form in self.forms if form.is_valid() and form not in forms_to_delete]<EOL>for form in valid_forms:<EOL><INDENT>unique_checks, date_checks = form.instance._get_unique_checks()<EOL>all_unique_checks.update(unique_checks)<EOL>all_date_checks.update(date_checks)<EOL><DEDENT>errors = []<EOL>for uclass, unique_check in all_unique_checks:<EOL><INDENT>seen_data = set()<EOL>for form in valid_forms:<EOL><INDENT>row_data = (<EOL>field if field in self.unique_fields else form.cleaned_data[field]<EOL>for field in unique_check if field in form.cleaned_data<EOL>)<EOL>row_data = tuple(d._get_pk_val() if hasattr(d, '<STR_LIT>') else d<EOL>for d in row_data)<EOL>if row_data and None not in row_data:<EOL><INDENT>if row_data in seen_data:<EOL><INDENT>errors.append(self.get_unique_error_message(unique_check))<EOL>form._errors[NON_FIELD_ERRORS] = self.error_class([self.get_form_error()])<EOL>for field in unique_check:<EOL><INDENT>if field in form.cleaned_data:<EOL><INDENT>del form.cleaned_data[field]<EOL><DEDENT><DEDENT><DEDENT>seen_data.add(row_data)<EOL><DEDENT><DEDENT><DEDENT>if errors:<EOL><INDENT>raise ValidationError(errors)<EOL><DEDENT>

This clean method will check for unique_together condition


<EOL>if self.formsets:<EOL><INDENT>for formset in self.formsets.values():<EOL><INDENT>for form in formset.forms:<EOL><INDENT>if form.has_changed():<EOL><INDENT>return True<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return bool(self.changed_data)<EOL>

Return True if data differs from initial.


relation_name = related.get_accessor_name()<EOL>rel_field = related.field<EOL>rel_model = related.related_model<EOL>superclass = rel_model._default_manager.__class__<EOL>class DeferringRelatedManager(superclass):<EOL><INDENT>def __init__(self, instance):<EOL><INDENT>super(DeferringRelatedManager, self).__init__()<EOL>self.model = rel_model<EOL>self.instance = instance<EOL><DEDENT>def _get_cluster_related_objects(self):<EOL><INDENT>try:<EOL><INDENT>return self.instance._cluster_related_objects<EOL><DEDENT>except AttributeError:<EOL><INDENT>cluster_related_objects = {}<EOL>self.instance._cluster_related_objects = cluster_related_objects<EOL>return cluster_related_objects<EOL><DEDENT><DEDENT>def get_live_query_set(self):<EOL><INDENT>return self.get_live_queryset()<EOL><DEDENT>def get_live_queryset(self):<EOL><INDENT>"""<STR_LIT>"""<EOL>return original_manager_cls(self.instance).get_queryset()<EOL><DEDENT>def get_queryset(self):<EOL><INDENT>"""<STR_LIT>"""<EOL>try:<EOL><INDENT>results = self.instance._cluster_related_objects[relation_name]<EOL><DEDENT>except (AttributeError, KeyError):<EOL><INDENT>return self.get_live_queryset()<EOL><DEDENT>return FakeQuerySet(related.related_model, results)<EOL><DEDENT>def _apply_rel_filters(self, queryset):<EOL><INDENT>return queryset._next_is_sticky()<EOL><DEDENT>def get_prefetch_queryset(self, instances, queryset=None):<EOL><INDENT>if queryset is None:<EOL><INDENT>db = self._db or router.db_for_read(self.model, instance=instances[<NUM_LIT:0>])<EOL>queryset = super(DeferringRelatedManager, self).get_queryset().using(db)<EOL><DEDENT>rel_obj_attr = rel_field.get_local_related_value<EOL>instance_attr = rel_field.get_foreign_related_value<EOL>instances_dict = dict((instance_attr(inst), inst) for inst in instances)<EOL>query = {'<STR_LIT>' % rel_field.name: instances}<EOL>qs = queryset.filter(**query)<EOL>for rel_obj in qs:<EOL><INDENT>instance = instances_dict[rel_obj_attr(rel_obj)]<EOL>setattr(rel_obj, rel_field.name, instance)<EOL><DEDENT>cache_name = rel_field.related_query_name()<EOL>return qs, rel_obj_attr, instance_attr, False, cache_name, False<EOL><DEDENT>def get_object_list(self):<EOL><INDENT>"""<STR_LIT>"""<EOL>cluster_related_objects = self._get_cluster_related_objects()<EOL>try:<EOL><INDENT>object_list = cluster_related_objects[relation_name]<EOL><DEDENT>except KeyError:<EOL><INDENT>object_list = list(self.get_live_queryset())<EOL>cluster_related_objects[relation_name] = object_list<EOL><DEDENT>return object_list<EOL><DEDENT>def add(self, *new_items):<EOL><INDENT>"""<STR_LIT>"""<EOL>items = self.get_object_list()<EOL>for target in new_items:<EOL><INDENT>item_matched = False<EOL>for i, item in enumerate(items):<EOL><INDENT>if item == target:<EOL><INDENT>items[i] = target<EOL>item_matched = True<EOL>break<EOL><DEDENT><DEDENT>if not item_matched:<EOL><INDENT>items.append(target)<EOL><DEDENT>setattr(target, related.field.name, self.instance)<EOL><DEDENT>if rel_model._meta.ordering and len(items) > <NUM_LIT:1>:<EOL><INDENT>sort_by_fields(items, rel_model._meta.ordering)<EOL><DEDENT><DEDENT>def remove(self, *items_to_remove):<EOL><INDENT>"""<STR_LIT>"""<EOL>items = self.get_object_list()<EOL>items[:] = [item for item in items if item not in items_to_remove]<EOL><DEDENT>def create(self, **kwargs):<EOL><INDENT>items = self.get_object_list()<EOL>new_item = related.related_model(**kwargs)<EOL>items.append(new_item)<EOL>return new_item<EOL><DEDENT>def clear(self):<EOL><INDENT>"""<STR_LIT>"""<EOL>self.set([])<EOL><DEDENT>def set(self, objs, bulk=True, clear=False):<EOL><INDENT>objs = list(objs)<EOL>cluster_related_objects = self._get_cluster_related_objects()<EOL>for obj in objs:<EOL><INDENT>setattr(obj, related.field.name, self.instance)<EOL><DEDENT>if rel_model._meta.ordering and len(objs) > <NUM_LIT:1>:<EOL><INDENT>sort_by_fields(objs, rel_model._meta.ordering)<EOL><DEDENT>cluster_related_objects[relation_name] = objs<EOL><DEDENT>def commit(self):<EOL><INDENT>"""<STR_LIT>"""<EOL>if self.instance.pk is None:<EOL><INDENT>raise IntegrityError("<STR_LIT>" % relation_name)<EOL><DEDENT>try:<EOL><INDENT>final_items = self.instance._cluster_related_objects[relation_name]<EOL><DEDENT>except (AttributeError, KeyError):<EOL><INDENT>return<EOL><DEDENT>original_manager = original_manager_cls(self.instance)<EOL>live_items = list(original_manager.get_queryset())<EOL>for item in live_items:<EOL><INDENT>if item not in final_items:<EOL><INDENT>item.delete()<EOL><DEDENT><DEDENT>for item in final_items:<EOL><INDENT>original_manager.add(item, bulk=False)<EOL><DEDENT>del self.instance._cluster_related_objects[relation_name]<EOL><DEDENT><DEDENT>return DeferringRelatedManager<EOL>

Create a DeferringRelatedManager class that wraps an ordinary RelatedManager
with 'deferring' behaviour: any updates to the object set (via e.g. add() or clear())
are written to a holding area rather than committed to the database immediately.
Writing to the database is deferred until the model is saved.


<EOL>for key in reversed(fields):<EOL><INDENT>reverse = False<EOL>if key[<NUM_LIT:0>] == '<STR_LIT:->':<EOL><INDENT>reverse = True<EOL>key = key[<NUM_LIT:1>:]<EOL><DEDENT>items.sort(key=lambda x: (getattr(x, key) is not None, getattr(x, key)), reverse=reverse)<EOL><DEDENT>

Sort a list of objects on the given fields. The field list works analogously to
queryset.order_by(*fields): each field is either a property of the object,
or is prefixed by '-' (e.g. '-name') to indicate reverse ordering.


pk_field = model._meta.pk<EOL>while pk_field.remote_field and pk_field.remote_field.parent_link:<EOL><INDENT>pk_field = pk_field.remote_field.model._meta.pk<EOL><DEDENT>obj = {'<STR_LIT>': get_field_value(pk_field, model)}<EOL>for field in model._meta.fields:<EOL><INDENT>if field.serialize:<EOL><INDENT>obj[field.name] = get_field_value(field, model)<EOL><DEDENT><DEDENT>return obj<EOL>

Return a serialised version of the model's fields which exist as local database
columns (i.e. excluding m2m and incoming foreign key relations)


return [<EOL>field for field in model._meta.get_fields()<EOL>if isinstance(field.remote_field, ParentalKey)<EOL>]<EOL>

Return a list of RelatedObject records for child relations of the given model,
including ones attached to ancestors of the model


return [<EOL>field for field in model._meta.get_fields()<EOL>if isinstance(field, ParentalManyToManyField)<EOL>]<EOL>

Return a list of ParentalManyToManyFields on the given model,
including ones attached to ancestors of the model


child_relation_names = (<EOL>[rel.get_accessor_name() for rel in get_all_child_relations(self)] +<EOL>[field.name for field in get_all_child_m2m_relations(self)]<EOL>)<EOL>if any(name in kwargs for name in child_relation_names):<EOL><INDENT>kwargs_for_super = kwargs.copy()<EOL>relation_assignments = {}<EOL>for rel_name in child_relation_names:<EOL><INDENT>if rel_name in kwargs:<EOL><INDENT>relation_assignments[rel_name] = kwargs_for_super.pop(rel_name)<EOL><DEDENT><DEDENT>super(ClusterableModel, self).__init__(*args, **kwargs_for_super)<EOL>for (field_name, related_instances) in relation_assignments.items():<EOL><INDENT>setattr(self, field_name, related_instances)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>super(ClusterableModel, self).__init__(*args, **kwargs)<EOL><DEDENT>

Extend the standard model constructor to allow child object lists to be passed in
via kwargs


child_relation_names = [rel.get_accessor_name() for rel in get_all_child_relations(self)]<EOL>child_m2m_field_names = [field.name for field in get_all_child_m2m_relations(self)]<EOL>update_fields = kwargs.pop('<STR_LIT>', None)<EOL>if update_fields is None:<EOL><INDENT>real_update_fields = None<EOL>relations_to_commit = child_relation_names<EOL>m2m_fields_to_commit = child_m2m_field_names<EOL><DEDENT>else:<EOL><INDENT>real_update_fields = []<EOL>relations_to_commit = []<EOL>m2m_fields_to_commit = []<EOL>for field in update_fields:<EOL><INDENT>if field in child_relation_names:<EOL><INDENT>relations_to_commit.append(field)<EOL><DEDENT>elif field in child_m2m_field_names:<EOL><INDENT>m2m_fields_to_commit.append(field)<EOL><DEDENT>else:<EOL><INDENT>real_update_fields.append(field)<EOL><DEDENT><DEDENT><DEDENT>super(ClusterableModel, self).save(update_fields=real_update_fields, **kwargs)<EOL>for relation in relations_to_commit:<EOL><INDENT>getattr(self, relation).commit()<EOL><DEDENT>for field in m2m_fields_to_commit:<EOL><INDENT>getattr(self, field).commit()<EOL><DEDENT>

Save the model and commit all child relations.


@classmethod<EOL><INDENT>def from_serializable_data(cls, data, check_fks=True, strict_fks=False):<DEDENT>

obj = model_from_serializable_data(cls, data, check_fks=check_fks, strict_fks=strict_fks)<EOL>if obj is None:<EOL><INDENT>return None<EOL><DEDENT>child_relations = get_all_child_relations(cls)<EOL>for rel in child_relations:<EOL><INDENT>rel_name = rel.get_accessor_name()<EOL>try:<EOL><INDENT>child_data_list = data[rel_name]<EOL><DEDENT>except KeyError:<EOL><INDENT>continue<EOL><DEDENT>related_model = rel.related_model<EOL>if hasattr(related_model, '<STR_LIT>'):<EOL><INDENT>children = [<EOL>related_model.from_serializable_data(child_data, check_fks=check_fks, strict_fks=True)<EOL>for child_data in child_data_list<EOL>]<EOL><DEDENT>else:<EOL><INDENT>children = [<EOL>model_from_serializable_data(related_model, child_data, check_fks=check_fks, strict_fks=True)<EOL>for child_data in child_data_list<EOL>]<EOL><DEDENT>children = filter(lambda child: child is not None, children)<EOL>setattr(obj, rel_name, children)<EOL><DEDENT>return obj<EOL>

Build an instance of this model from the JSON-like structure passed in,
recursing into related objects as required.
If check_fks is true, it will check whether referenced foreign keys still
exist in the database.
- dangling foreign keys on related objects are dealt with by either nullifying the key or
dropping the related object, according to the 'on_delete' setting.
- dangling foreign keys on the base object will be nullified, unless strict_fks is true,
in which case any dangling foreign keys with on_delete=CASCADE will cause None to be
returned for the entire object.


raise NotImplementedError<EOL>

Subclasses have to override this method to implement the actual
        handler function code. This method is called with asset as argument.
        Depending on the type of the handler, this method must change asset
        state (as it does in :class:`~gears.processors.Directivesprocessor`)
        or return some value (in case of asset compressors).


@wraps(cls, updated=())<EOL>def handler(asset, *args, **kwargs):<EOL><INDENT>return handler.handler_class(**initkwargs)(asset, *args, **kwargs)<EOL><DEDENT>handler.handler_class = cls<EOL>handler.supports_check_mode = cls.supports_check_mode<EOL>return handler<EOL>

Converts the class into an actual handler function that can be used
        when registering different types of processors in
        :class:`~gears.environment.Environment` class instance.

        The arguments passed to :meth:`as_handler` are forwarded to the
        constructor of the class.


p = self.get_process()<EOL>output, errors = p.communicate(input=input.encode('<STR_LIT:utf-8>'))<EOL>if p.returncode != <NUM_LIT:0>:<EOL><INDENT>raise AssetHandlerError(errors)<EOL><DEDENT>return output.decode('<STR_LIT:utf-8>')<EOL>

Runs :attr:`executable` with ``input`` as stdin.
        :class:`AssetHandlerError` exception is raised, if execution is failed,
        otherwise stdout is returned.