def parameters(self):
        '''Population parameter vals == average member parameter vals'''

        if len(self.__members) != 0:
            if self.__num_processes > 1:
                members = [m.get() for m in self.__members]
            else:
                members = self.__members
            params = {}
            for p in self.__parameters:
                params[p.name] = sum(
                    m.parameters[p.name] for m in members
                ) / len(members)
            return params
        else:
            return None

def parameters ( self ) : if len ( self . __members ) != 0 : if self . __num_processes > 1 : members = [ m . get ( ) for m in self . __members ] else : members = self . __members params = { } for p in self . __parameters : params [ p . name ] = sum ( m . parameters [ p . name ] for m in members ) / len ( members ) return params else : return None


def members(self):
        '''Returns Member objects of population'''

        if self.__num_processes > 1:
            return [m.get() for m in self.__members]
        else:
            return self.__members

def members ( self ) : if self . __num_processes > 1 : return [ m . get ( ) for m in self . __members ] else : return self . __members


def add_parameter(self, name, min_val, max_val):
        '''Adds a paramber to the Population

        Args:
            name (str): name of the parameter
            min_val (int or float): minimum value for the parameter
            max_val (int or float): maximum value for the parameter
        '''

        self.__parameters.append(Parameter(name, min_val, max_val))

def add_parameter ( self , name , min_val , max_val ) : self . __parameters . append ( Parameter ( name , min_val , max_val ) )


def next_generation(self, mut_rate=0, max_mut_amt=0, log_base=10):
        '''Generates the next population from a previously evaluated generation

        Args:
            mut_rate (float): mutation rate for new members (0.0 - 1.0)
            max_mut_amt (float): how much the member is allowed to mutate
                (0.0 - 1.0, proportion change of mutated parameter)
            log_base (int): the higher this number, the more likely the first
                Members (chosen with supplied selection function) are chosen
                as parents for the next generation
        '''

        if self.__num_processes > 1:
            process_pool = Pool(processes=self.__num_processes)
            members = [m.get() for m in self.__members]
        else:
            members = self.__members

        if len(members) == 0:
            raise Exception(
                'Generation 0 not found: use generate_population() first'
            )

        selected_members = self.__select_fn(members)
        reproduction_probs = list(reversed(logspace(0.0, 1.0,
                                  num=len(selected_members), base=log_base)))
        reproduction_probs = reproduction_probs / sum(reproduction_probs)

        self.__members = []

        for _ in range(self.__pop_size):
            parent_1 = nrandom.choice(selected_members, p=reproduction_probs)
            parent_2 = nrandom.choice(selected_members, p=reproduction_probs)

            feed_dict = {}
            for param in self.__parameters:
                which_parent = uniform(0, 1)
                if which_parent < 0.5:
                    feed_dict[param.name] = parent_1.parameters[param.name]
                else:
                    feed_dict[param.name] = parent_2.parameters[param.name]
                feed_dict[param.name] = self.__mutate_parameter(
                    feed_dict[param.name], param, mut_rate, max_mut_amt
                )

            if self.__num_processes > 1:
                self.__members.append(process_pool.apply_async(
                    self._start_process,
                    [self.__cost_fn, feed_dict, self.__cost_fn_args])
                )
            else:
                self.__members.append(
                    Member(
                        feed_dict,
                        self.__cost_fn(feed_dict, self.__cost_fn_args)
                    )
                )

        if self.__num_processes > 1:
            process_pool.close()
            process_pool.join()

        self.__determine_best_member()


def normalize_keys(self, items):
        """Return a config dictionary with normalized keys regardless of
        whether the keys were specified in environment variables or in config
        files"""
        normalized = {}
        for key, val in items:
            key = key.replace('_', '-')
            if not key.startswith('--'):
                key = '--%s' % key  # only prefer long opts
            normalized[key] = val
        return normalized

def normalize_keys ( self , items ) : normalized = { } for key , val in items : key = key . replace ( '_' , '-' ) if not key . startswith ( '--' ) : key = '--%s' % key normalized [ key ] = val return normalized


def get_environ_vars(self):
        """Returns a generator with all environmental vars with prefix PIP_"""
        for key, val in os.environ.items():
            if _environ_prefix_re.search(key):
                yield (_environ_prefix_re.sub("", key).lower(), val)

def get_environ_vars ( self ) : for key , val in os . environ . items ( ) : if _environ_prefix_re . search ( key ) : yield ( _environ_prefix_re . sub ( "" , key ) . lower ( ) , val )


def throws_exception(callable, *exceptions):
	"""
	Return True if the callable throws the specified exception

	>>> throws_exception(lambda: int('3'))
	False
	>>> throws_exception(lambda: int('a'))
	True
	>>> throws_exception(lambda: int('a'), KeyError)
	False
	"""
	with context.ExceptionTrap():
		with context.ExceptionTrap(*exceptions) as exc:
			callable()
	return bool(exc)

def throws_exception ( callable , * exceptions ) : with context . ExceptionTrap ( ) : with context . ExceptionTrap ( * exceptions ) as exc : callable ( ) return bool ( exc )


def transform_hits(hits):
    """
    The list from pypi is really a list of versions. We want a list of
    packages with the list of versions stored inline. This converts the
    list from pypi into one we can use.
    """
    packages = {}
    for hit in hits:
        name = hit['name']
        summary = hit['summary']
        version = hit['version']
        score = hit['_pypi_ordering']
        if score is None:
            score = 0

        if name not in packages.keys():
            packages[name] = {
                'name': name,
                'summary': summary,
                'versions': [version],
                'score': score,
            }
        else:
            packages[name]['versions'].append(version)

            # if this is the highest version, replace summary and score
            if version == highest_version(packages[name]['versions']):
                packages[name]['summary'] = summary
                packages[name]['score'] = score

    # each record has a unique name now, so we will convert the dict into a
    # list sorted by score
    package_list = sorted(
        packages.values(),
        key=lambda x: x['score'],
        reverse=True,
    )
    return package_list

def transform_hits ( hits ) : packages = { } for hit in hits : name = hit [ 'name' ] summary = hit [ 'summary' ] version = hit [ 'version' ] score = hit [ '_pypi_ordering' ] if score is None : score = 0 if name not in packages . keys ( ) : packages [ name ] = { 'name' : name , 'summary' : summary , 'versions' : [ version ] , 'score' : score , } else : packages [ name ] [ 'versions' ] . append ( version ) if version == highest_version ( packages [ name ] [ 'versions' ] ) : packages [ name ] [ 'summary' ] = summary packages [ name ] [ 'score' ] = score package_list = sorted ( packages . values ( ) , key = lambda x : x [ 'score' ] , reverse = True , ) return package_list

The list from pypi is really a list of versions . We want a list of packages with the list of versions stored inline . This converts the list from pypi into one we can use .


def _transform_result(typ, result):
    """Convert the result back into the input type.
    """
    if issubclass(typ, bytes):
        return tostring(result, encoding='utf-8')
    elif issubclass(typ, unicode):
        return tostring(result, encoding='unicode')
    else:
        return result

def _transform_result ( typ , result ) : if issubclass ( typ , bytes ) : return tostring ( result , encoding = 'utf-8' ) elif issubclass ( typ , unicode ) : return tostring ( result , encoding = 'unicode' ) else : return result

Convert the result back into the input type .


def html_to_xhtml(html):
    """Convert all tags in an HTML tree to XHTML by moving them to the
    XHTML namespace.
    """
    try:
        html = html.getroot()
    except AttributeError:
        pass
    prefix = "{%s}" % XHTML_NAMESPACE
    for el in html.iter(etree.Element):
        tag = el.tag
        if tag[0] != '{':
            el.tag = prefix + tag

def html_to_xhtml ( html ) : try : html = html . getroot ( ) except AttributeError : pass prefix = "{%s}" % XHTML_NAMESPACE for el in html . iter ( etree . Element ) : tag = el . tag if tag [ 0 ] != '{' : el . tag = prefix + tag

Convert all tags in an HTML tree to XHTML by moving them to the XHTML namespace .


def xhtml_to_html(xhtml):
    """Convert all tags in an XHTML tree to HTML by removing their
    XHTML namespace.
    """
    try:
        xhtml = xhtml.getroot()
    except AttributeError:
        pass
    prefix = "{%s}" % XHTML_NAMESPACE
    prefix_len = len(prefix)
    for el in xhtml.iter(prefix + "*"):
        el.tag = el.tag[prefix_len:]

def xhtml_to_html ( xhtml ) : try : xhtml = xhtml . getroot ( ) except AttributeError : pass prefix = "{%s}" % XHTML_NAMESPACE prefix_len = len ( prefix ) for el in xhtml . iter ( prefix + "*" ) : el . tag = el . tag [ prefix_len : ]

Convert all tags in an XHTML tree to HTML by removing their XHTML namespace .


def tostring(doc, pretty_print=False, include_meta_content_type=False,
             encoding=None, method="html", with_tail=True, doctype=None):
    """Return an HTML string representation of the document.

    Note: if include_meta_content_type is true this will create a
    ``<meta http-equiv="Content-Type" ...>`` tag in the head;
    regardless of the value of include_meta_content_type any existing
    ``<meta http-equiv="Content-Type" ...>`` tag will be removed

    The ``encoding`` argument controls the output encoding (defauts to
    ASCII, with &#...; character references for any characters outside
    of ASCII).  Note that you can pass the name ``'unicode'`` as
    ``encoding`` argument to serialise to a Unicode string.

    The ``method`` argument defines the output method.  It defaults to
    'html', but can also be 'xml' for xhtml output, or 'text' to
    serialise to plain text without markup.

    To leave out the tail text of the top-level element that is being
    serialised, pass ``with_tail=False``.

    The ``doctype`` option allows passing in a plain string that will
    be serialised before the XML tree.  Note that passing in non
    well-formed content here will make the XML output non well-formed.
    Also, an existing doctype in the document tree will not be removed
    when serialising an ElementTree instance.

    Example::

        >>> from lxml import html
        >>> root = html.fragment_fromstring('
Hello
world!
')

        >>> html.tostring(root)
        b'
Hello
world!
'
        >>> html.tostring(root, method='html')
        b'
Hello
world!
'

        >>> html.tostring(root, method='xml')
        b'
Hello
world!
'

        >>> html.tostring(root, method='text')
        b'Helloworld!'

        >>> html.tostring(root, method='text', encoding='unicode')
        u'Helloworld!'

        >>> root = html.fragment_fromstring('

Hello
world!
TAIL
')
        >>> html.tostring(root[0], method='text', encoding='unicode')
        u'Helloworld!TAIL'

        >>> html.tostring(root[0], method='text', encoding='unicode', with_tail=False)
        u'Helloworld!'

        >>> doc = html.document_fromstring('
Hello
world!
')
        >>> html.tostring(doc, method='html', encoding='unicode')
        u'
Hello
world!
'

        >>> print(html.tostring(doc, method='html', encoding='unicode',
        ...          doctype='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'
        ...                  ' "http://www.w3.org/TR/html4/strict.dtd">'))
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

Hello
world!

    """
    html = etree.tostring(doc, method=method, pretty_print=pretty_print,
                          encoding=encoding, with_tail=with_tail,
                          doctype=doctype)
    if method == 'html' and not include_meta_content_type:
        if isinstance(html, str):
            html = __str_replace_meta_content_type('', html)
        else:
            html = __bytes_replace_meta_content_type(bytes(), html)
    return html

def tostring ( doc , pretty_print = False , include_meta_content_type = False , encoding = None , method = "html" , with_tail = True , doctype = None ) : html = etree . tostring ( doc , method = method , pretty_print = pretty_print , encoding = encoding , with_tail = with_tail , doctype = doctype ) if method == 'html' and not include_meta_content_type : if isinstance ( html , str ) : html = __str_replace_meta_content_type ( '' , html ) else : html = __bytes_replace_meta_content_type ( bytes ( ) , html ) return html

Return an HTML string representation of the document .


def open_in_browser(doc, encoding=None):
    """
    Open the HTML document in a web browser, saving it to a temporary
    file to open it.  Note that this does not delete the file after
    use.  This is mainly meant for debugging.
    """
    import os
    import webbrowser
    import tempfile
    if not isinstance(doc, etree._ElementTree):
        doc = etree.ElementTree(doc)
    handle, fn = tempfile.mkstemp(suffix='.html')
    f = os.fdopen(handle, 'wb')
    try:
        doc.write(f, method="html", encoding=encoding or doc.docinfo.encoding or "UTF-8")
    finally:
        # we leak the file itself here, but we should at least close it
        f.close()
    url = 'file://' + fn.replace(os.path.sep, '/')
    print(url)
    webbrowser.open(url)

def open_in_browser ( doc , encoding = None ) : import os import webbrowser import tempfile if not isinstance ( doc , etree . _ElementTree ) : doc = etree . ElementTree ( doc ) handle , fn = tempfile . mkstemp ( suffix = '.html' ) f = os . fdopen ( handle , 'wb' ) try : doc . write ( f , method = "html" , encoding = encoding or doc . docinfo . encoding or "UTF-8" ) finally : f . close ( ) url = 'file://' + fn . replace ( os . path . sep , '/' ) print ( url ) webbrowser . open ( url )

Open the HTML document in a web browser saving it to a temporary file to open it . Note that this does not delete the file after use . This is mainly meant for debugging .


def drop_tree(self):
        """
        Removes this element from the tree, including its children and
        text.  The tail text is joined to the previous element or
        parent.
        """
        parent = self.getparent()
        assert parent is not None
        if self.tail:
            previous = self.getprevious()
            if previous is None:
                parent.text = (parent.text or '') + self.tail
            else:
                previous.tail = (previous.tail or '') + self.tail
        parent.remove(self)

def drop_tree ( self ) : parent = self . getparent ( ) assert parent is not None if self . tail : previous = self . getprevious ( ) if previous is None : parent . text = ( parent . text or '' ) + self . tail else : previous . tail = ( previous . tail or '' ) + self . tail parent . remove ( self )

Removes this element from the tree including its children and text . The tail text is joined to the previous element or parent .


def drop_tag(self):
        """
        Remove the tag, but not its children or text.  The children and text
        are merged into the parent.

        Example::

            >>> h = fragment_fromstring('
Hello World!
')
            >>> h.find('.//b').drop_tag()
            >>> print(tostring(h, encoding='unicode'))

Hello World!

        """
        parent = self.getparent()
        assert parent is not None
        previous = self.getprevious()
        if self.text and isinstance(self.tag, basestring):
            # not a Comment, etc.
            if previous is None:
                parent.text = (parent.text or '') + self.text
            else:
                previous.tail = (previous.tail or '') + self.text
        if self.tail:
            if len(self):
                last = self[-1]
                last.tail = (last.tail or '') + self.tail
            elif previous is None:
                parent.text = (parent.text or '') + self.tail
            else:
                previous.tail = (previous.tail or '') + self.tail
        index = parent.index(self)
        parent[index:index+1] = self[:]

def drop_tag ( self ) : parent = self . getparent ( ) assert parent is not None previous = self . getprevious ( ) if self . text and isinstance ( self . tag , basestring ) : if previous is None : parent . text = ( parent . text or '' ) + self . text else : previous . tail = ( previous . tail or '' ) + self . text if self . tail : if len ( self ) : last = self [ - 1 ] last . tail = ( last . tail or '' ) + self . tail elif previous is None : parent . text = ( parent . text or '' ) + self . tail else : previous . tail = ( previous . tail or '' ) + self . tail index = parent . index ( self ) parent [ index : index + 1 ] = self [ : ]

Remove the tag but not its children or text . The children and text are merged into the parent .


def get_element_by_id(self, id, *default):
        """
        Get the first element in a document with the given id.  If none is
        found, return the default argument if provided or raise KeyError
        otherwise.

        Note that there can be more than one element with the same id,
        and this isn't uncommon in HTML documents found in the wild.
        Browsers return only the first match, and this function does
        the same.
        """
        try:
            # FIXME: should this check for multiple matches?
            # browsers just return the first one
            return _id_xpath(self, id=id)[0]
        except IndexError:
            if default:
                return default[0]
            else:
                raise KeyError(id)

def get_element_by_id ( self , id , * default ) : try : return _id_xpath ( self , id = id ) [ 0 ] except IndexError : if default : return default [ 0 ] else : raise KeyError ( id )

Get the first element in a document with the given id . If none is found return the default argument if provided or raise KeyError otherwise .


def cssselect(self, expr, translator='html'):
        """
        Run the CSS expression on this element and its children,
        returning a list of the results.

        Equivalent to lxml.cssselect.CSSSelect(expr, translator='html')(self)
        -- note that pre-compiling the expression can provide a substantial
        speedup.
        """
        # Do the import here to make the dependency optional.
        from lxml.cssselect import CSSSelector
        return CSSSelector(expr, translator=translator)(self)

def cssselect ( self , expr , translator = 'html' ) : from lxml . cssselect import CSSSelector return CSSSelector ( expr , translator = translator ) ( self )

Run the CSS expression on this element and its children returning a list of the results .


def loghandler_members():
    """iterate through the attributes of every logger's handler

    this is used to switch out stderr and stdout in tests when buffer is True

    :returns: generator of tuples, each tuple has (name, handler, member_name, member_val)
    """
    Members = namedtuple("Members", ["name", "handler", "member_name", "member"])
    log_manager = logging.Logger.manager
    loggers = []
    ignore = set([modname()])
    if log_manager.root:
        loggers = list(log_manager.loggerDict.items())
        loggers.append(("root", log_manager.root))

    for logger_name, logger in loggers:
        if logger_name in ignore: continue

        for handler in getattr(logger, "handlers", []):
            members = inspect.getmembers(handler)
            for member_name, member in members:
                yield Members(logger_name, handler, member_name, member)

def loghandler_members ( ) : Members = namedtuple ( "Members" , [ "name" , "handler" , "member_name" , "member" ] ) log_manager = logging . Logger . manager loggers = [ ] ignore = set ( [ modname ( ) ] ) if log_manager . root : loggers = list ( log_manager . loggerDict . items ( ) ) loggers . append ( ( "root" , log_manager . root ) ) for logger_name , logger in loggers : if logger_name in ignore : continue for handler in getattr ( logger , "handlers" , [ ] ) : members = inspect . getmembers ( handler ) for member_name , member in members : yield Members ( logger_name , handler , member_name , member )

iterate through the attributes of every logger s handler


def get_counts():
    """return test counts that are set via pyt environment variables when pyt
    runs the test

    :returns: dict, 3 keys (classes, tests, modules) and how many tests of each
        were found by pyt
    """
    counts = {}
    ks = [
        ('PYT_TEST_CLASS_COUNT', "classes"),
        ('PYT_TEST_COUNT', "tests"),
        ('PYT_TEST_MODULE_COUNT', "modules"),
    ]

    for ek, cn in ks:
        counts[cn] = int(os.environ.get(ek, 0))

    return counts

def get_counts ( ) : counts = { } ks = [ ( 'PYT_TEST_CLASS_COUNT' , "classes" ) , ( 'PYT_TEST_COUNT' , "tests" ) , ( 'PYT_TEST_MODULE_COUNT' , "modules" ) , ] for ek , cn in ks : counts [ cn ] = int ( os . environ . get ( ek , 0 ) ) return counts


def is_single_class():
    """Returns True if only a single class is being run or some tests within a single class"""
    ret = False
    counts = get_counts()
    if counts["classes"] < 1 and counts["modules"] < 1:
        ret = counts["tests"] > 0
    else:
        ret = counts["classes"] <= 1 and counts["modules"] <= 1
    return ret

def is_single_class ( ) : ret = False counts = get_counts ( ) if counts [ "classes" ] < 1 and counts [ "modules" ] < 1 : ret = counts [ "tests" ] > 0 else : ret = counts [ "classes" ] <= 1 and counts [ "modules" ] <= 1 return ret


def is_single_module():
    """Returns True if only a module is being run"""
    ret = False
    counts = get_counts()
    if counts["modules"] == 1:
        ret = True

    elif counts["modules"] < 1:
        ret = is_single_class()

    return ret

def is_single_module ( ) : ret = False counts = get_counts ( ) if counts [ "modules" ] == 1 : ret = True elif counts [ "modules" ] < 1 : ret = is_single_class ( ) return ret


def validate_params(request):
    """Validate request params."""

    if 'params' in request:
        correct_params = isinstance(request['params'], (list, dict))
        error = 'Incorrect parameter values'
        assert correct_params, error

def
validate_params
(
request
)
:
if
'params'
in
request
:
correct_params
=
isinstance
(
request
[
'params'
]
,
(
list
,
dict
)
)
error
=
'Incorrect parameter values'
assert
correct_params
,
error


def validate_id(request):
    """Validate request id."""

    if 'id' in request:
        correct_id = isinstance(
            request['id'],
            (string_types, int, None),
        )
        error = 'Incorrect identifier'
        assert correct_id, error

def
validate_id
(
request
)
:
if
'id'
in
request
:
correct_id
=
isinstance
(
request
[
'id'
]
,
(
string_types
,
int
,
)
,
)
error
=
'Incorrect identifier'
assert
correct_id
,
error


def filesys_decode(path):
    """
    Ensure that the given path is decoded,
    NONE when no expected encoding works
    """

    fs_enc = sys.getfilesystemencoding()
    if isinstance(path, decoded_string):
        return path

    for enc in (fs_enc, "utf-8"):
        try:
            return path.decode(enc)
        except UnicodeDecodeError:
            continue

def filesys_decode ( path ) : fs_enc = sys . getfilesystemencoding ( ) if isinstance ( path , decoded_string ) : return path for enc in ( fs_enc , "utf-8" ) : try : return path . decode ( enc ) except UnicodeDecodeError : continue

Ensure that the given path is decoded NONE when no expected encoding works


def _escape_argspec(obj, iterable, escape):
    """Helper for various string-wrapped functions."""
    for key, value in iterable:
        if hasattr(value, '__html__') or isinstance(value, string_types):
            obj[key] = escape(value)
    return obj

def _escape_argspec ( obj , iterable , escape ) : for key , value in iterable : if hasattr ( value , '__html__' ) or isinstance ( value , string_types ) : obj [ key ] = escape ( value ) return obj

Helper for various string - wrapped functions .


def codecName(encoding):
    """Return the python codec name corresponding to an encoding or None if the
    string doesn't correspond to a valid encoding."""
    if isinstance(encoding, bytes):
        try:
            encoding = encoding.decode("ascii")
        except UnicodeDecodeError:
            return None
    if encoding:
        canonicalName = ascii_punctuation_re.sub("", encoding).lower()
        return encodings.get(canonicalName, None)
    else:
        return None

def codecName ( encoding ) : if isinstance ( encoding , bytes ) : try : encoding = encoding . decode ( "ascii" ) except UnicodeDecodeError : return None if encoding : canonicalName = ascii_punctuation_re . sub ( "" , encoding ) . lower ( ) return encodings . get ( canonicalName , None ) else : return None

Return the python codec name corresponding to an encoding or None if the string doesn t correspond to a valid encoding .


def detectBOM(self):
        """Attempts to detect at BOM at the start of the stream. If
        an encoding can be determined from the BOM return the name of the
        encoding otherwise return None"""
        bomDict = {
            codecs.BOM_UTF8: 'utf-8',
            codecs.BOM_UTF16_LE: 'utf-16-le', codecs.BOM_UTF16_BE: 'utf-16-be',
            codecs.BOM_UTF32_LE: 'utf-32-le', codecs.BOM_UTF32_BE: 'utf-32-be'
        }

        # Go to beginning of file and read in 4 bytes
        string = self.rawStream.read(4)
        assert isinstance(string, bytes)

        # Try detecting the BOM using bytes from the string
        encoding = bomDict.get(string[:3])         # UTF-8
        seek = 3
        if not encoding:
            # Need to detect UTF-32 before UTF-16
            encoding = bomDict.get(string)         # UTF-32
            seek = 4
            if not encoding:
                encoding = bomDict.get(string[:2])  # UTF-16
                seek = 2

        # Set the read position past the BOM if one was found, otherwise
        # set it to the start of the stream
        self.rawStream.seek(encoding and seek or 0)

        return encoding

def detectBOM ( self ) : bomDict = { codecs . BOM_UTF8 : 'utf-8' , codecs . BOM_UTF16_LE : 'utf-16-le' , codecs . BOM_UTF16_BE : 'utf-16-be' , codecs . BOM_UTF32_LE : 'utf-32-le' , codecs . BOM_UTF32_BE : 'utf-32-be' } string = self . rawStream . read ( 4 ) assert isinstance ( string , bytes ) encoding = bomDict . get ( string [ : 3 ] ) seek = 3 if not encoding : encoding = bomDict . get ( string ) seek = 4 if not encoding : encoding = bomDict . get ( string [ : 2 ] ) seek = 2 self . rawStream . seek ( encoding and seek or 0 ) return encoding

Attempts to detect at BOM at the start of the stream . If an encoding can be determined from the BOM return the name of the encoding otherwise return None


def get_remote_addr(self, forwarded_for):
        """Selects the new remote addr from the given list of ips in
        X-Forwarded-For.  By default it picks the one that the `num_proxies`
        proxy server provides.  Before 0.9 it would always pick the first.

        .. versionadded:: 0.8
        """
        if len(forwarded_for) >= self.num_proxies:
            return forwarded_for[-1 * self.num_proxies]

def get_remote_addr ( self , forwarded_for ) : if len ( forwarded_for ) >= self . num_proxies : return forwarded_for [ - 1 * self . num_proxies ]

Selects the new remote addr from the given list of ips in X - Forwarded - For . By default it picks the one that the num_proxies proxy server provides . Before 0 . 9 it would always pick the first .


def amount_converter(obj):
    """Converts amount value from several types into Decimal."""
    if isinstance(obj, Decimal):
        return obj
    elif isinstance(obj, (str, int, float)):
        return Decimal(str(obj))
    else:
        raise ValueError('do not know how to convert: {}'.format(type(obj)))

def
amount_converter
(
obj
)
:
if
isinstance
(
obj
,
Decimal
)
:
return
obj
elif
isinstance
(
obj
,
(
str
,
int
,
float
)
)
:
return
Decimal
(
str
(
obj
)
)
else
:
raise
ValueError
(
'do not know how to convert: {}'
.
format
(
type
(
obj
)
)
)

Converts amount value from several types into Decimal .


def fromstring(data, beautifulsoup=None, makeelement=None, **bsargs):
    """Parse a string of HTML data into an Element tree using the
    BeautifulSoup parser.

    Returns the root ``<html>`` Element of the tree.

    You can pass a different BeautifulSoup parser through the
    `beautifulsoup` keyword, and a diffent Element factory function
    through the `makeelement` keyword.  By default, the standard
    ``BeautifulSoup`` class and the default factory of `lxml.html` are
    used.
    """
    return _parse(data, beautifulsoup, makeelement, **bsargs)

def fromstring ( data , beautifulsoup = None , makeelement = None , ** bsargs ) : return _parse ( data , beautifulsoup , makeelement , ** bsargs )

Parse a string of HTML data into an Element tree using the BeautifulSoup parser .


def parse(file, beautifulsoup=None, makeelement=None, **bsargs):
    """Parse a file into an ElemenTree using the BeautifulSoup parser.

    You can pass a different BeautifulSoup parser through the
    `beautifulsoup` keyword, and a diffent Element factory function
    through the `makeelement` keyword.  By default, the standard
    ``BeautifulSoup`` class and the default factory of `lxml.html` are
    used.
    """
    if not hasattr(file, 'read'):
        file = open(file)
    root = _parse(file, beautifulsoup, makeelement, **bsargs)
    return etree.ElementTree(root)

def parse ( file , beautifulsoup = None , makeelement = None , ** bsargs ) : if not hasattr ( file , 'read' ) : file = open ( file ) root = _parse ( file , beautifulsoup , makeelement , ** bsargs ) return etree . ElementTree ( root )

Parse a file into an ElemenTree using the BeautifulSoup parser .


def convert_tree(beautiful_soup_tree, makeelement=None):
    """Convert a BeautifulSoup tree to a list of Element trees.

    Returns a list instead of a single root Element to support
    HTML-like soup with more than one root element.

    You can pass a different Element factory through the `makeelement`
    keyword.
    """
    if makeelement is None:
        makeelement = html.html_parser.makeelement
    root = _convert_tree(beautiful_soup_tree, makeelement)
    children = root.getchildren()
    for child in children:
        root.remove(child)
    return children

def convert_tree ( beautiful_soup_tree , makeelement = None ) : if makeelement is None : makeelement = html . html_parser . makeelement root = _convert_tree ( beautiful_soup_tree , makeelement ) children = root . getchildren ( ) for child in children : root . remove ( child ) return children

Convert a BeautifulSoup tree to a list of Element trees .


def get_current_traceback(ignore_system_exceptions=False,
                          show_hidden_frames=False, skip=0):
    """Get the current exception info as `Traceback` object.  Per default
    calling this method will reraise system exceptions such as generator exit,
    system exit or others.  This behavior can be disabled by passing `False`
    to the function as first parameter.
    """
    exc_type, exc_value, tb = sys.exc_info()
    if ignore_system_exceptions and exc_type in system_exceptions:
        raise
    for x in range_type(skip):
        if tb.tb_next is None:
            break
        tb = tb.tb_next
    tb = Traceback(exc_type, exc_value, tb)
    if not show_hidden_frames:
        tb.filter_hidden_frames()
    return tb

def get_current_traceback ( ignore_system_exceptions = False , show_hidden_frames = False , skip = 0 ) : exc_type , exc_value , tb = sys . exc_info ( ) if ignore_system_exceptions and exc_type in system_exceptions : raise for x in range_type ( skip ) : if tb . tb_next is None : break tb = tb . tb_next tb = Traceback ( exc_type , exc_value , tb ) if not show_hidden_frames : tb . filter_hidden_frames ( ) return tb

Get the current exception info as Traceback object . Per default calling this method will reraise system exceptions such as generator exit system exit or others . This behavior can be disabled by passing False to the function as first parameter .


def exception(self):
        """String representation of the exception."""
        buf = traceback.format_exception_only(self.exc_type, self.exc_value)
        rv = ''.join(buf).strip()
        return rv.decode('utf-8', 'replace') if PY2 else rv

def exception ( self ) : buf = traceback . format_exception_only ( self . exc_type , self . exc_value ) rv = '' . join ( buf ) . strip ( ) return rv . decode ( 'utf-8' , 'replace' ) if PY2 else rv

String representation of the exception .


def render_summary(self, include_title=True):
        """Render the traceback for the interactive console."""
        title = ''
        frames = []
        classes = ['traceback']
        if not self.frames:
            classes.append('noframe-traceback')

        if include_title:
            if self.is_syntax_error:
                title = u'Syntax Error'
            else:
                title = u'Traceback (most recent call last):'

        for frame in self.frames:
            frames.append(u'
%s' % (
                frame.info and u' title="%s"' % escape(frame.info) or u'',
                frame.render()
            ))

        if self.is_syntax_error:
            description_wrapper = u'
%s
'
        else:
            description_wrapper = u'
%s
'

        return SUMMARY_HTML % {
            'classes':      u' '.join(classes),
            'title':        title and u'
%s
' % title or u'',
            'frames':       u'\n'.join(frames),
            'description':  description_wrapper % escape(self.exception)
        }

u'Traceback <em>(most recent call last)</em>:'

Render the traceback for the interactive console .


def generate_plaintext_traceback(self):
        """Like the plaintext attribute but returns a generator"""
        yield u'Traceback (most recent call last):'
        for frame in self.frames:
            yield u'  File "%s", line %s, in %s' % (
                frame.filename,
                frame.lineno,
                frame.function_name
            )
            yield u'    ' + frame.current_line.strip()
        yield self.exception

def
generate_plaintext_traceback
(
self
)
:
yield
u'Traceback (most recent call last):'
for
frame
in
self
.
frames
:
yield
u'  File "%s", line %s, in %s'
%
(
frame
.
filename
,
frame
.
lineno
,
frame
.
function_name
)
yield
u'    '
+
frame
.
current_line
.
strip
(
)
yield
self
.
exception


def get_annotated_lines(self):
        """Helper function that returns lines with extra information."""
        lines = [Line(idx + 1, x) for idx, x in enumerate(self.sourcelines)]

        # find function definition and mark lines
        if hasattr(self.code, 'co_firstlineno'):
            lineno = self.code.co_firstlineno - 1
            while lineno > 0:
                if _funcdef_re.match(lines[lineno].code):
                    break
                lineno -= 1
            try:
                offset = len(inspect.getblock([x.code + '\n' for x
                                               in lines[lineno:]]))
            except TokenError:
                offset = 0
            for line in lines[lineno:lineno + offset]:
                line.in_frame = True

        # mark current line
        try:
            lines[self.lineno - 1].current = True
        except IndexError:
            pass

        return lines

def get_annotated_lines ( self ) : lines = [ Line ( idx + 1 , x ) for idx , x in enumerate ( self . sourcelines ) ] if hasattr ( self . code , 'co_firstlineno' ) : lineno = self . code . co_firstlineno - 1 while lineno > 0 : if _funcdef_re . match ( lines [ lineno ] . code ) : break lineno -= 1 try : offset = len ( inspect . getblock ( [ x . code + '\n' for x in lines [ lineno : ] ] ) ) except TokenError : offset = 0 for line in lines [ lineno : lineno + offset ] : line . in_frame = True try : lines [ self . lineno - 1 ] . current = True except IndexError : pass return lines

Helper function that returns lines with extra information .


def render_source(self):
        """Render the sourcecode."""
        return SOURCE_TABLE_HTML % u'\n'.join(line.render() for line in
                                              self.get_annotated_lines())

def render_source ( self ) : return SOURCE_TABLE_HTML % u'\n' . join ( line . render ( ) for line in self . get_annotated_lines ( ) )


def egg_info_matches(
        egg_info, search_name, link,
        _egg_info_re=re.compile(r'([a-z0-9_.]+)-([a-z0-9_.!+-]+)', re.I)):
    """Pull the version part out of a string.

    :param egg_info: The string to parse. E.g. foo-2.1
    :param search_name: The name of the package this belongs to. None to
        infer the name. Note that this cannot unambiguously parse strings
        like foo-2-2 which might be foo, 2-2 or foo-2, 2.
    :param link: The link the string came from, for logging on failure.
    """
    match = _egg_info_re.search(egg_info)
    if not match:
        logger.debug('Could not parse version from link: %s', link)
        return None
    if search_name is None:
        full_match = match.group(0)
        return full_match[full_match.index('-'):]
    name = match.group(0).lower()
    # To match the "safe" name that pkg_resources creates:
    name = name.replace('_', '-')
    # project name and version must be separated by a dash
    look_for = search_name.lower() + "-"
    if name.startswith(look_for):
        return match.group(0)[len(look_for):]
    else:
        return None

def
egg_info_matches
(
egg_info
,
search_name
,
link
,
_egg_info_re
=
re
.
compile
(
r'([a-z0-9_.]+)-([a-z0-9_.!+-]+)'
,
re
.
I
)
)
:
match
=
_egg_info_re
.
search
(
egg_info
)
if
not
match
:
logger
.
debug
(
'Could not parse version from link: %s'
,
link
)
return
if
search_name
is
:
full_match
=
match
.
group
(
0
)
return
full_match
[
full_match
.
index
(
'-'
)
:
]
name
=
match
.
group
(
0
)
.
lower
(
)
name
=
name
.
replace
(
'_'
,
'-'
)
look_for
=
search_name
.
lower
(
)
+
"-"
if
name
.
startswith
(
look_for
)
:
return
match
.
group
(
0
)
[
len
(
look_for
)
:
]
else
:
return


def _get_index_urls_locations(self, project_name):
        """Returns the locations found via self.index_urls

        Checks the url_name on the main (first in the list) index and
        use this url_name to produce all locations
        """

        def mkurl_pypi_url(url):
            loc = posixpath.join(url, project_url_name)
            # For maximum compatibility with easy_install, ensure the path
            # ends in a trailing slash.  Although this isn't in the spec
            # (and PyPI can handle it without the slash) some other index
            # implementations might break if they relied on easy_install's
            # behavior.
            if not loc.endswith('/'):
                loc = loc + '/'
            return loc

        project_url_name = urllib_parse.quote(project_name.lower())

        if self.index_urls:
            # Check that we have the url_name correctly spelled:

            # Only check main index if index URL is given
            main_index_url = Link(
                mkurl_pypi_url(self.index_urls[0]),
                trusted=True,
            )

            page = self._get_page(main_index_url)
            if page is None and PyPI.netloc not in str(main_index_url):
                warnings.warn(
                    "Failed to find %r at %s. It is suggested to upgrade "
                    "your index to support normalized names as the name in "
                    "/simple/{name}." % (project_name, main_index_url),
                    RemovedInPip8Warning,
                )

                project_url_name = self._find_url_name(
                    Link(self.index_urls[0], trusted=True),
                    project_url_name,
                ) or project_url_name

        if project_url_name is not None:
            return [mkurl_pypi_url(url) for url in self.index_urls]
        return []

Returns the locations found via self . index_urls


def _find_all_versions(self, project_name):
        """Find all available versions for project_name

        This checks index_urls, find_links and dependency_links
        All versions found are returned

        See _link_package_versions for details on which files are accepted
        """
        index_locations = self._get_index_urls_locations(project_name)
        index_file_loc, index_url_loc = self._sort_locations(index_locations)
        fl_file_loc, fl_url_loc = self._sort_locations(
            self.find_links, expand_dir=True)
        dep_file_loc, dep_url_loc = self._sort_locations(self.dependency_links)

        file_locations = (
            Link(url) for url in itertools.chain(
                index_file_loc, fl_file_loc, dep_file_loc)
        )

        # We trust every url that the user has given us whether it was given
        #   via --index-url or --find-links
        # We explicitly do not trust links that came from dependency_links
        # We want to filter out any thing which does not have a secure origin.
        url_locations = [
            link for link in itertools.chain(
                (Link(url, trusted=True) for url in index_url_loc),
                (Link(url, trusted=True) for url in fl_url_loc),
                (Link(url) for url in dep_url_loc),
            )
            if self._validate_secure_origin(logger, link)
        ]

        logger.debug('%d location(s) to search for versions of %s:',
                     len(url_locations), project_name)

        for location in url_locations:
            logger.debug('* %s', location)

        canonical_name = pkg_resources.safe_name(project_name).lower()
        formats = fmt_ctl_formats(self.format_control, canonical_name)
        search = Search(project_name.lower(), canonical_name, formats)
        find_links_versions = self._package_versions(
            # We trust every directly linked archive in find_links
            (Link(url, '-f', trusted=True) for url in self.find_links),
            search
        )

        page_versions = []
        for page in self._get_pages(url_locations, project_name):
            logger.debug('Analyzing links from page %s', page.url)
            with indent_log():
                page_versions.extend(
                    self._package_versions(page.links, search)
                )

        dependency_versions = self._package_versions(
            (Link(url) for url in self.dependency_links), search
        )
        if dependency_versions:
            logger.debug(
                'dependency_links found: %s',
                ', '.join([
                    version.location.url for version in dependency_versions
                ])
            )

        file_versions = self._package_versions(file_locations, search)
        if file_versions:
            file_versions.sort(reverse=True)
            logger.debug(
                'Local files found: %s',
                ', '.join([
                    url_to_path(candidate.location.url)
                    for candidate in file_versions
                ])
            )

        # This is an intentional priority ordering
        return (
            file_versions + find_links_versions + page_versions +
            dependency_versions
        )


def find_requirement(self, req, upgrade):
        """Try to find an InstallationCandidate for req

        Expects req, an InstallRequirement and upgrade, a boolean
        Returns an InstallationCandidate or None
        May raise DistributionNotFound or BestVersionAlreadyInstalled
        """
        all_versions = self._find_all_versions(req.name)
        # Filter out anything which doesn't match our specifier

        _versions = set(
            req.specifier.filter(
                [x.version for x in all_versions],
                prereleases=(
                    self.allow_all_prereleases
                    if self.allow_all_prereleases else None
                ),
            )
        )
        applicable_versions = [
            x for x in all_versions if x.version in _versions
        ]

        if req.satisfied_by is not None:
            # Finally add our existing versions to the front of our versions.
            applicable_versions.insert(
                0,
                InstallationCandidate(
                    req.name,
                    req.satisfied_by.version,
                    INSTALLED_VERSION,
                )
            )
            existing_applicable = True
        else:
            existing_applicable = False

        applicable_versions = self._sort_versions(applicable_versions)

        if not upgrade and existing_applicable:
            if applicable_versions[0].location is INSTALLED_VERSION:
                logger.debug(
                    'Existing installed version (%s) is most up-to-date and '
                    'satisfies requirement',
                    req.satisfied_by.version,
                )
            else:
                logger.debug(
                    'Existing installed version (%s) satisfies requirement '
                    '(most up-to-date version is %s)',
                    req.satisfied_by.version,
                    applicable_versions[0][2],
                )
            return None

        if not applicable_versions:
            logger.critical(
                'Could not find a version that satisfies the requirement %s '
                '(from versions: %s)',
                req,
                ', '.join(
                    sorted(
                        set(str(i.version) for i in all_versions),
                        key=parse_version,
                    )
                )
            )

            if self.need_warn_external:
                logger.warning(
                    "Some externally hosted files were ignored as access to "
                    "them may be unreliable (use --allow-external %s to "
                    "allow).",
                    req.name,
                )

            if self.need_warn_unverified:
                logger.warning(
                    "Some insecure and unverifiable files were ignored"
                    " (use --allow-unverified %s to allow).",
                    req.name,
                )

            raise DistributionNotFound(
                'No matching distribution found for %s' % req
            )

        if applicable_versions[0].location is INSTALLED_VERSION:
            # We have an existing version, and its the best version
            logger.debug(
                'Installed version (%s) is most up-to-date (past versions: '
                '%s)',
                req.satisfied_by.version,
                ', '.join(str(i.version) for i in applicable_versions[1:]) or
                "none",
            )
            raise BestVersionAlreadyInstalled

        if len(applicable_versions) > 1:
            logger.debug(
                'Using version %s (newest of versions: %s)',
                applicable_versions[0].version,
                ', '.join(str(i.version) for i in applicable_versions)
            )

        selected_version = applicable_versions[0].location

        if (selected_version.verifiable is not None and not
                selected_version.verifiable):
            logger.warning(
                "%s is potentially insecure and unverifiable.", req.name,
            )

        return selected_version


def _sort_links(self, links):
        """
        Returns elements of links in order, non-egg links first, egg links
        second, while eliminating duplicates
        """
        eggs, no_eggs = [], []
        seen = set()
        for link in links:
            if link not in seen:
                seen.add(link)
                if link.egg_fragment:
                    eggs.append(link)
                else:
                    no_eggs.append(link)
        return no_eggs + eggs

def _sort_links ( self , links ) : eggs , no_eggs = [ ] , [ ] seen = set ( ) for link in links : if link not in seen : seen . add ( link ) if link . egg_fragment : eggs . append ( link ) else : no_eggs . append ( link ) return no_eggs + eggs

Returns elements of links in order non - egg links first egg links second while eliminating duplicates


def _get_content_type(url, session):
        """Get the Content-Type of the given url, using a HEAD request"""
        scheme, netloc, path, query, fragment = urllib_parse.urlsplit(url)
        if scheme not in ('http', 'https'):
            # FIXME: some warning or something?
            # assertion error?
            return ''

        resp = session.head(url, allow_redirects=True)
        resp.raise_for_status()

        return resp.headers.get("Content-Type", "")

def _get_content_type ( url , session ) : scheme , netloc , path , query , fragment = urllib_parse . urlsplit ( url ) if scheme not in ( 'http' , 'https' ) : return '' resp = session . head ( url , allow_redirects = True ) resp . raise_for_status ( ) return resp . headers . get ( "Content-Type" , "" )

Get the Content - Type of the given url using a HEAD request


def links(self):
        """Yields all links in the page"""
        for anchor in self.parsed.findall(".//a"):
            if anchor.get("href"):
                href = anchor.get("href")
                url = self.clean_link(
                    urllib_parse.urljoin(self.base_url, href)
                )

                # Determine if this link is internal. If that distinction
                #   doesn't make sense in this context, then we don't make
                #   any distinction.
                internal = None
                if self.api_version and self.api_version >= 2:
                    # Only api_versions >= 2 have a distinction between
                    #   external and internal links
                    internal = bool(
                        anchor.get("rel") and
                        "internal" in anchor.get("rel").split()
                    )

                yield Link(url, self, internal=internal)

def links ( self ) : for anchor in self . parsed . findall ( ".//a" ) : if anchor . get ( "href" ) : href = anchor . get ( "href" ) url = self . clean_link ( urllib_parse . urljoin ( self . base_url , href ) ) internal = None if self . api_version and self . api_version >= 2 : internal = bool ( anchor . get ( "rel" ) and "internal" in anchor . get ( "rel" ) . split ( ) ) yield Link ( url , self , internal = internal )


def verifiable(self):
        """
        Returns True if this link can be verified after download, False if it
        cannot, and None if we cannot determine.
        """
        trusted = self.trusted or getattr(self.comes_from, "trusted", None)
        if trusted is not None and trusted:
            # This link came from a trusted source. It *may* be verifiable but
            #   first we need to see if this page is operating under the new
            #   API version.
            try:
                api_version = getattr(self.comes_from, "api_version", None)
                api_version = int(api_version)
            except (ValueError, TypeError):
                api_version = None

            if api_version is None or api_version <= 1:
                # This link is either trusted, or it came from a trusted,
                #   however it is not operating under the API version 2 so
                #   we can't make any claims about if it's safe or not
                return

            if self.hash:
                # This link came from a trusted source and it has a hash, so we
                #   can consider it safe.
                return True
            else:
                # This link came from a trusted source, using the new API
                #   version, and it does not have a hash. It is NOT verifiable
                return False
        elif trusted is not None:
            # This link came from an untrusted source and we cannot trust it
            return False

def verifiable ( self ) : trusted = self . trusted or getattr ( self . comes_from , "trusted" , None ) if trusted is not None and trusted : try : api_version = getattr ( self . comes_from , "api_version" , None ) api_version = int ( api_version ) except ( ValueError , TypeError ) : api_version = None if api_version is None or api_version <= 1 : return if self . hash : return True else : return False elif trusted is not None : return False

Returns True if this link can be verified after download False if it cannot and None if we cannot determine .


def find_data_files(self, package, src_dir):
        """Return filenames for package's data files in 'src_dir'"""
        globs = (self.package_data.get('', [])
                 + self.package_data.get(package, []))
        files = self.manifest_files.get(package, [])[:]
        for pattern in globs:
            # Each pattern has to be converted to a platform-specific path
            files.extend(glob(os.path.join(src_dir, convert_path(pattern))))
        return self.exclude_data_files(package, src_dir, files)

def find_data_files ( self , package , src_dir ) : globs = ( self . package_data . get ( '' , [ ] ) + self . package_data . get ( package , [ ] ) ) files = self . manifest_files . get ( package , [ ] ) [ : ] for pattern in globs : files . extend ( glob ( os . path . join ( src_dir , convert_path ( pattern ) ) ) ) return self . exclude_data_files ( package , src_dir , files )

Return filenames for package s data files in src_dir


def exclude_data_files(self, package, src_dir, files):
        """Filter filenames for package's data files in 'src_dir'"""
        globs = (self.exclude_package_data.get('', [])
                 + self.exclude_package_data.get(package, []))
        bad = []
        for pattern in globs:
            bad.extend(
                fnmatch.filter(
                    files, os.path.join(src_dir, convert_path(pattern))
                )
            )
        bad = dict.fromkeys(bad)
        seen = {}
        return [
            f for f in files if f not in bad
            and f not in seen and seen.setdefault(f, 1)  # ditch dupes
        ]

def exclude_data_files ( self , package , src_dir , files ) : globs = ( self . exclude_package_data . get ( '' , [ ] ) + self . exclude_package_data . get ( package , [ ] ) ) bad = [ ] for pattern in globs : bad . extend ( fnmatch . filter ( files , os . path . join ( src_dir , convert_path ( pattern ) ) ) ) bad = dict . fromkeys ( bad ) seen = { } return [ f for f in files if f not in bad and f not in seen and seen . setdefault ( f , 1 ) ]

Filter filenames for package s data files in src_dir


def parse_requirements(filename, finder=None, comes_from=None, options=None,
                       session=None, wheel_cache=None):
    """
    Parse a requirements file and yield InstallRequirement instances.

    :param filename:    Path or url of requirements file.
    :param finder:      Instance of pip.index.PackageFinder.
    :param comes_from:  Origin description of requirements.
    :param options:     Global options.
    :param session:     Instance of pip.download.PipSession.
    :param wheel_cache: Instance of pip.wheel.WheelCache
    """
    if session is None:
        raise TypeError(
            "parse_requirements() missing 1 required keyword argument: "
            "'session'"
        )

    _, content = get_file_content(
        filename, comes_from=comes_from, session=session
    )

    lines = content.splitlines()
    lines = ignore_comments(lines)
    lines = join_lines(lines)
    lines = skip_regex(lines, options)

    for line_number, line in enumerate(lines, 1):
        req_iter = process_line(line, filename, line_number, finder,
                                comes_from, options, session, wheel_cache)
        for req in req_iter:
            yield req

Parse a requirements file and yield InstallRequirement instances .


def join_lines(iterator):
    """
    Joins a line ending in '\' with the previous line.
    """
    lines = []
    for line in iterator:
        if not line.endswith('\\'):
            if lines:
                lines.append(line)
                yield ''.join(lines)
                lines = []
            else:
                yield line
        else:
            lines.append(line.strip('\\'))

def join_lines ( iterator ) : lines = [ ] for line in iterator : if not line . endswith ( '\\' ) : if lines : lines . append ( line ) yield '' . join ( lines ) lines = [ ] else : yield line else : lines . append ( line . strip ( '\\' ) )

Joins a line ending in \ with the previous line .


def ignore_comments(iterator):
    """
    Strips and filters empty or commented lines.
    """
    for line in iterator:
        line = COMMENT_RE.sub('', line)
        line = line.strip()
        if line:
            yield line

def ignore_comments ( iterator ) : for line in iterator : line = COMMENT_RE . sub ( '' , line ) line = line . strip ( ) if line : yield line

Strips and filters empty or commented lines .


def compile(marker):
    """Return compiled marker as a function accepting an environment dict."""
    try:
        return _cache[marker]
    except KeyError:
        pass
    if not marker.strip():
        def marker_fn(environment=None, override=None):
            """"""
            return True
    else:
        compiled_marker = compile_marker(parse_marker(marker))
        def marker_fn(environment=None, override=None):
            """override updates environment"""
            if override is None:
                override = {}
            if environment is None:
                environment = default_environment()
            environment.update(override)
            return eval(compiled_marker, environment)
    marker_fn.__doc__ = marker
    _cache[marker] = marker_fn
    return _cache[marker]

def compile ( marker ) : try : return _cache [ marker ] except KeyError : pass if not marker . strip ( ) : def marker_fn ( environment = None , override = None ) : return True else : compiled_marker = compile_marker ( parse_marker ( marker ) ) def marker_fn ( environment = None , override = None ) : if override is None : override = { } if environment is None : environment = default_environment ( ) environment . update ( override ) return eval ( compiled_marker , environment ) marker_fn . __doc__ = marker _cache [ marker ] = marker_fn return _cache [ marker ]

Return compiled marker as a function accepting an environment dict .


def visit(self, node):
        """Ensure statement only contains allowed nodes."""
        if not isinstance(node, self.ALLOWED):
            raise SyntaxError('Not allowed in environment markers.\n%s\n%s' %
                               (self.statement,
                               (' ' * node.col_offset) + '^'))
        return ast.NodeTransformer.visit(self, node)

Ensure statement only contains allowed nodes .


def visit_Attribute(self, node):
        """Flatten one level of attribute access."""
        new_node = ast.Name("%s.%s" % (node.value.id, node.attr), node.ctx)
        return ast.copy_location(new_node, node)

def visit_Attribute ( self , node ) : new_node = ast . Name ( "%s.%s" % ( node . value . id , node . attr ) , node . ctx ) return ast . copy_location ( new_node , node )


def coerce(value):
	"""
	coerce takes a value and attempts to convert it to a float,
	or int.

	If none of the conversions are successful, the original value is
	returned.

	>>> coerce('3')
	3

	>>> coerce('3.0')
	3.0

	>>> coerce('foo')
	'foo'

	>>> coerce({})
	{}

	>>> coerce('{}')
	'{}'
	"""
	with contextlib2.suppress(Exception):
		loaded = json.loads(value)
		assert isinstance(loaded, numbers.Number)
		return loaded
	return value

def coerce ( value ) : with contextlib2 . suppress ( Exception ) : loaded = json . loads ( value ) assert isinstance ( loaded , numbers . Number ) return loaded return value

coerce takes a value and attempts to convert it to a float or int .


def copy_current_request_context(f):
    """A helper function that decorates a function to retain the current
    request context.  This is useful when working with greenlets.  The moment
    the function is decorated a copy of the request context is created and
    then pushed when the function is called.

    Example::

        import gevent
        from flask import copy_current_request_context

        @app.route('/')
        def index():
            @copy_current_request_context
            def do_some_work():
                # do some work here, it can access flask.request like you
                # would otherwise in the view function.
                ...
            gevent.spawn(do_some_work)
            return 'Regular response'

    .. versionadded:: 0.10
    """
    top = _request_ctx_stack.top
    if top is None:
        raise RuntimeError('This decorator can only be used at local scopes '
            'when a request context is on the stack.  For instance within '
            'view functions.')
    reqctx = top.copy()
    def wrapper(*args, **kwargs):
        with reqctx:
            return f(*args, **kwargs)
    return update_wrapper(wrapper, f)

A helper function that decorates a function to retain the current request context . This is useful when working with greenlets . The moment the function is decorated a copy of the request context is created and then pushed when the function is called .


def push(self):
        """Binds the app context to the current context."""
        self._refcnt += 1
        _app_ctx_stack.push(self)
        appcontext_pushed.send(self.app)

def push ( self ) : self . _refcnt += 1 _app_ctx_stack . push ( self ) appcontext_pushed . send ( self . app )

Binds the app context to the current context .


def pop(self, exc=None):
        """Pops the app context."""
        self._refcnt -= 1
        if self._refcnt <= 0:
            if exc is None:
                exc = sys.exc_info()[1]
            self.app.do_teardown_appcontext(exc)
        rv = _app_ctx_stack.pop()
        assert rv is self, 'Popped wrong app context.  (%r instead of %r)' \
            % (rv, self)
        appcontext_popped.send(self.app)


def copy(self):
        """Creates a copy of this request context with the same request object.
        This can be used to move a request context to a different greenlet.
        Because the actual request object is the same this cannot be used to
        move a request context to a different thread unless access to the
        request object is locked.

        .. versionadded:: 0.10
        """
        return self.__class__(self.app,
            environ=self.request.environ,
            request=self.request
        )

def copy ( self ) : return self . __class__ ( self . app , environ = self . request . environ , request = self . request )

Creates a copy of this request context with the same request object . This can be used to move a request context to a different greenlet . Because the actual request object is the same this cannot be used to move a request context to a different thread unless access to the request object is locked .


def match_request(self):
        """Can be overridden by a subclass to hook into the matching
        of the request.
        """
        try:
            url_rule, self.request.view_args = \
                self.url_adapter.match(return_rule=True)
            self.request.url_rule = url_rule
        except HTTPException as e:
            self.request.routing_exception = e

def match_request ( self ) : try : url_rule , self . request . view_args = self . url_adapter . match ( return_rule = True ) self . request . url_rule = url_rule except HTTPException as e : self . request . routing_exception = e

Can be overridden by a subclass to hook into the matching of the request .


def push(self):
        """Binds the request context to the current context."""
        # If an exception occurs in debug mode or if context preservation is
        # activated under exception situations exactly one context stays
        # on the stack.  The rationale is that you want to access that
        # information under debug situations.  However if someone forgets to
        # pop that context again we want to make sure that on the next push
        # it's invalidated, otherwise we run at risk that something leaks
        # memory.  This is usually only a problem in testsuite since this
        # functionality is not active in production environments.
        top = _request_ctx_stack.top
        if top is not None and top.preserved:
            top.pop(top._preserved_exc)

        # Before we push the request context we have to ensure that there
        # is an application context.
        app_ctx = _app_ctx_stack.top
        if app_ctx is None or app_ctx.app != self.app:
            app_ctx = self.app.app_context()
            app_ctx.push()
            self._implicit_app_ctx_stack.append(app_ctx)
        else:
            self._implicit_app_ctx_stack.append(None)

        _request_ctx_stack.push(self)

        # Open the session at the moment that the request context is
        # available. This allows a custom open_session method to use the
        # request context (e.g. code that access database information
        # stored on `g` instead of the appcontext).
        self.session = self.app.open_session(self.request)
        if self.session is None:
            self.session = self.app.make_null_session()

def push ( self ) : top = _request_ctx_stack . top if top is not None and top . preserved : top . pop ( top . _preserved_exc ) app_ctx = _app_ctx_stack . top if app_ctx is None or app_ctx . app != self . app : app_ctx = self . app . app_context ( ) app_ctx . push ( ) self . _implicit_app_ctx_stack . append ( app_ctx ) else : self . _implicit_app_ctx_stack . append ( None ) _request_ctx_stack . push ( self ) self . session = self . app . open_session ( self . request ) if self . session is None : self . session = self . app . make_null_session ( )

Binds the request context to the current context .


def make_path_relative(path, rel_to):
    """
    Make a filename relative, where the filename path, and it is
    relative to rel_to

        >>> make_path_relative('/usr/share/something/a-file.pth',
        ...                    '/usr/share/another-place/src/Directory')
        '../../../something/a-file.pth'
        >>> make_path_relative('/usr/share/something/a-file.pth',
        ...                    '/home/user/src/Directory')
        '../../../usr/share/something/a-file.pth'
        >>> make_path_relative('/usr/share/a-file.pth', '/usr/share/')
        'a-file.pth'
    """
    path_filename = os.path.basename(path)
    path = os.path.dirname(path)
    path = os.path.normpath(os.path.abspath(path))
    rel_to = os.path.normpath(os.path.abspath(rel_to))
    path_parts = path.strip(os.path.sep).split(os.path.sep)
    rel_to_parts = rel_to.strip(os.path.sep).split(os.path.sep)
    while path_parts and rel_to_parts and path_parts[0] == rel_to_parts[0]:
        path_parts.pop(0)
        rel_to_parts.pop(0)
    full_parts = ['..'] * len(rel_to_parts) + path_parts + [path_filename]
    if full_parts == ['']:
        return '.' + os.path.sep
    return os.path.sep.join(full_parts)

def make_path_relative ( path , rel_to ) : path_filename = os . path . basename ( path ) path = os . path . dirname ( path ) path = os . path . normpath ( os . path . abspath ( path ) ) rel_to = os . path . normpath ( os . path . abspath ( rel_to ) ) path_parts = path . strip ( os . path . sep ) . split ( os . path . sep ) rel_to_parts = rel_to . strip ( os . path . sep ) . split ( os . path . sep ) while path_parts and rel_to_parts and path_parts [ 0 ] == rel_to_parts [ 0 ] : path_parts . pop ( 0 ) rel_to_parts . pop ( 0 ) full_parts = [ '..' ] * len ( rel_to_parts ) + path_parts + [ path_filename ] if full_parts == [ '' ] : return '.' + os . path . sep return os . path . sep . join ( full_parts )

Make a filename relative where the filename path and it is relative to rel_to


def dist_is_editable(dist):
    """Is distribution an editable install?"""
    # TODO: factor out determining editableness out of FrozenRequirement
    from pip import FrozenRequirement
    req = FrozenRequirement.from_dist(dist, [])
    return req.editable

def dist_is_editable ( dist ) : from pip import FrozenRequirement req = FrozenRequirement . from_dist ( dist , [ ] ) return req . editable


def url_value_preprocessor(self, f):
        """Registers a function as URL value preprocessor for this
        blueprint.  It's called before the view functions are called and
        can modify the url values provided.
        """
        self.record_once(lambda s: s.app.url_value_preprocessors
            .setdefault(self.name, []).append(f))
        return f

def url_value_preprocessor ( self , f ) : self . record_once ( lambda s : s . app . url_value_preprocessors . setdefault ( self . name , [ ] ) . append ( f ) ) return f

Registers a function as URL value preprocessor for this blueprint . It s called before the view functions are called and can modify the url values provided .


def url_defaults(self, f):
        """Callback function for URL defaults for this blueprint.  It's called
        with the endpoint and values and should update the values passed
        in place.
        """
        self.record_once(lambda s: s.app.url_default_functions
            .setdefault(self.name, []).append(f))
        return f

def url_defaults ( self , f ) : self . record_once ( lambda s : s . app . url_default_functions . setdefault ( self . name , [ ] ) . append ( f ) ) return f

Callback function for URL defaults for this blueprint . It s called with the endpoint and values and should update the values passed in place .


def errorhandler(self, code_or_exception):
        """Registers an error handler that becomes active for this blueprint
        only.  Please be aware that routing does not happen local to a
        blueprint so an error handler for 404 usually is not handled by
        a blueprint unless it is caused inside a view function.  Another
        special case is the 500 internal server error which is always looked
        up from the application.

        Otherwise works as the :meth:`~flask.Flask.errorhandler` decorator
        of the :class:`~flask.Flask` object.
        """
        def decorator(f):
            self.record_once(lambda s: s.app._register_error_handler(
                self.name, code_or_exception, f))
            return f
        return decorator

def errorhandler ( self , code_or_exception ) : def decorator ( f ) : self . record_once ( lambda s : s . app . _register_error_handler ( self . name , code_or_exception , f ) ) return f return decorator

Registers an error handler that becomes active for this blueprint only . Please be aware that routing does not happen local to a blueprint so an error handler for 404 usually is not handled by a blueprint unless it is caused inside a view function . Another special case is the 500 internal server error which is always looked up from the application .


def stream_with_context(generator_or_function):
    """Request contexts disappear when the response is started on the server.
    This is done for efficiency reasons and to make it less likely to encounter
    memory leaks with badly written WSGI middlewares.  The downside is that if
    you are using streamed responses, the generator cannot access request bound
    information any more.

    This function however can help you keep the context around for longer::

        from flask import stream_with_context, request, Response

        @app.route('/stream')
        def streamed_response():
            @stream_with_context
            def generate():
                yield 'Hello '
                yield request.args['name']
                yield '!'
            return Response(generate())

    Alternatively it can also be used around a specific generator::

        from flask import stream_with_context, request, Response

        @app.route('/stream')
        def streamed_response():
            def generate():
                yield 'Hello '
                yield request.args['name']
                yield '!'
            return Response(stream_with_context(generate()))

    .. versionadded:: 0.9
    """
    try:
        gen = iter(generator_or_function)
    except TypeError:
        def decorator(*args, **kwargs):
            gen = generator_or_function()
            return stream_with_context(gen)
        return update_wrapper(decorator, generator_or_function)

    def generator():
        ctx = _request_ctx_stack.top
        if ctx is None:
            raise RuntimeError('Attempted to stream with context but '
                'there was no context in the first place to keep around.')
        with ctx:
            # Dummy sentinel.  Has to be inside the context block or we're
            # not actually keeping the context around.
            yield None

            # The try/finally is here so that if someone passes a WSGI level
            # iterator in we're still running the cleanup logic.  Generators
            # don't need that because they are closed on their destruction
            # automatically.
            try:
                for item in gen:
                    yield item
            finally:
                if hasattr(gen, 'close'):
                    gen.close()

    # The trick is to start the generator.  Then the code execution runs until
    # the first dummy None is yielded at which point the context was already
    # pushed.  This item is discarded.  Then when the iteration continues the
    # real generator is executed.
    wrapped_g = generator()
    next(wrapped_g)
    return wrapped_g

Request contexts disappear when the response is started on the server . This is done for efficiency reasons and to make it less likely to encounter memory leaks with badly written WSGI middlewares . The downside is that if you are using streamed responses the generator cannot access request bound information any more .


def make_response(*args):
    """Sometimes it is necessary to set additional headers in a view.  Because
    views do not have to return response objects but can return a value that
    is converted into a response object by Flask itself, it becomes tricky to
    add headers to it.  This function can be called instead of using a return
    and you will get a response object which you can use to attach headers.

    If view looked like this and you want to add a new header::

        def index():
            return render_template('index.html', foo=42)

    You can now do something like this::

        def index():
            response = make_response(render_template('index.html', foo=42))
            response.headers['X-Parachutes'] = 'parachutes are cool'
            return response

    This function accepts the very same arguments you can return from a
    view function.  This for example creates a response with a 404 error
    code::

        response = make_response(render_template('not_found.html'), 404)

    The other use case of this function is to force the return value of a
    view function into a response which is helpful with view
    decorators::

        response = make_response(view_function())
        response.headers['X-Parachutes'] = 'parachutes are cool'

    Internally this function does the following things:

    -   if no arguments are passed, it creates a new response argument
    -   if one argument is passed, :meth:`flask.Flask.make_response`
        is invoked with it.
    -   if more than one argument is passed, the arguments are passed
        to the :meth:`flask.Flask.make_response` function as tuple.

    .. versionadded:: 0.6
    """
    if not args:
        return current_app.response_class()
    if len(args) == 1:
        args = args[0]
    return current_app.make_response(args)

def make_response ( * args ) : if not args : return current_app . response_class ( ) if len ( args ) == 1 : args = args [ 0 ] return current_app . make_response ( args )

Sometimes it is necessary to set additional headers in a view . Because views do not have to return response objects but can return a value that is converted into a response object by Flask itself it becomes tricky to add headers to it . This function can be called instead of using a return and you will get a response object which you can use to attach headers .


def url_for(endpoint, **values):
    """Generates a URL to the given endpoint with the method provided.

    Variable arguments that are unknown to the target endpoint are appended
    to the generated URL as query arguments.  If the value of a query argument
    is `None`, the whole pair is skipped.  In case blueprints are active
    you can shortcut references to the same blueprint by prefixing the
    local endpoint with a dot (``.``).

    This will reference the index function local to the current blueprint::

        url_for('.index')

    For more information, head over to the :ref:`Quickstart <url-building>`.

    To integrate applications, :class:`Flask` has a hook to intercept URL build
    errors through :attr:`Flask.build_error_handler`.  The `url_for` function
    results in a :exc:`~werkzeug.routing.BuildError` when the current app does
    not have a URL for the given endpoint and values.  When it does, the
    :data:`~flask.current_app` calls its :attr:`~Flask.build_error_handler` if
    it is not `None`, which can return a string to use as the result of
    `url_for` (instead of `url_for`'s default to raise the
    :exc:`~werkzeug.routing.BuildError` exception) or re-raise the exception.
    An example::

        def external_url_handler(error, endpoint, **values):
            "Looks up an external URL when `url_for` cannot build a URL."
            # This is an example of hooking the build_error_handler.
            # Here, lookup_url is some utility function you've built
            # which looks up the endpoint in some external URL registry.
            url = lookup_url(endpoint, **values)
            if url is None:
                # External lookup did not have a URL.
                # Re-raise the BuildError, in context of original traceback.
                exc_type, exc_value, tb = sys.exc_info()
                if exc_value is error:
                    raise exc_type, exc_value, tb
                else:
                    raise error
            # url_for will use this result, instead of raising BuildError.
            return url

        app.build_error_handler = external_url_handler

    Here, `error` is the instance of :exc:`~werkzeug.routing.BuildError`, and
    `endpoint` and `**values` are the arguments passed into `url_for`.  Note
    that this is for building URLs outside the current application, and not for
    handling 404 NotFound errors.

    .. versionadded:: 0.10
       The `_scheme` parameter was added.

    .. versionadded:: 0.9
       The `_anchor` and `_method` parameters were added.

    .. versionadded:: 0.9
       Calls :meth:`Flask.handle_build_error` on
       :exc:`~werkzeug.routing.BuildError`.

    :param endpoint: the endpoint of the URL (name of the function)
    :param values: the variable arguments of the URL rule
    :param _external: if set to `True`, an absolute URL is generated. Server
      address can be changed via `SERVER_NAME` configuration variable which
      defaults to `localhost`.
    :param _scheme: a string specifying the desired URL scheme. The `_external`
      parameter must be set to `True` or a `ValueError` is raised.
    :param _anchor: if provided this is added as anchor to the URL.
    :param _method: if provided this explicitly specifies an HTTP method.
    """
    appctx = _app_ctx_stack.top
    reqctx = _request_ctx_stack.top
    if appctx is None:
        raise RuntimeError('Attempted to generate a URL without the '
                           'application context being pushed. This has to be '
                           'executed when application context is available.')

    # If request specific information is available we have some extra
    # features that support "relative" urls.
    if reqctx is not None:
        url_adapter = reqctx.url_adapter
        blueprint_name = request.blueprint
        if not reqctx.request._is_old_module:
            if endpoint[:1] == '.':
                if blueprint_name is not None:
                    endpoint = blueprint_name + endpoint
                else:
                    endpoint = endpoint[1:]
        else:
            # TODO: get rid of this deprecated functionality in 1.0
            if '.' not in endpoint:
                if blueprint_name is not None:
                    endpoint = blueprint_name + '.' + endpoint
            elif endpoint.startswith('.'):
                endpoint = endpoint[1:]
        external = values.pop('_external', False)

    # Otherwise go with the url adapter from the appctx and make
    # the urls external by default.
    else:
        url_adapter = appctx.url_adapter
        if url_adapter is None:
            raise RuntimeError('Application was not able to create a URL '
                               'adapter for request independent URL generation. '
                               'You might be able to fix this by setting '
                               'the SERVER_NAME config variable.')
        external = values.pop('_external', True)

    anchor = values.pop('_anchor', None)
    method = values.pop('_method', None)
    scheme = values.pop('_scheme', None)
    appctx.app.inject_url_defaults(endpoint, values)

    if scheme is not None:
        if not external:
            raise ValueError('When specifying _scheme, _external must be True')
        url_adapter.url_scheme = scheme

    try:
        rv = url_adapter.build(endpoint, values, method=method,
                               force_external=external)
    except BuildError as error:
        # We need to inject the values again so that the app callback can
        # deal with that sort of stuff.
        values['_external'] = external
        values['_anchor'] = anchor
        values['_method'] = method
        return appctx.app.handle_url_build_error(error, endpoint, values)

    if anchor is not None:
        rv += '#' + url_quote(anchor)
    return rv

Generates a URL to the given endpoint with the method provided .


def safe_join(directory, filename):
    """Safely join `directory` and `filename`.

    Example usage::

        @app.route('/wiki/<path:filename>')
        def wiki_page(filename):
            filename = safe_join(app.config['WIKI_FOLDER'], filename)
            with open(filename, 'rb') as fd:
                content = fd.read() # Read and process the file content...

    :param directory: the base directory.
    :param filename: the untrusted filename relative to that directory.
    :raises: :class:`~werkzeug.exceptions.NotFound` if the resulting path
             would fall out of `directory`.
    """
    filename = posixpath.normpath(filename)
    for sep in _os_alt_seps:
        if sep in filename:
            raise NotFound()
    if os.path.isabs(filename) or \
       filename == '..' or \
       filename.startswith('../'):
        raise NotFound()
    return os.path.join(directory, filename)

def safe_join ( directory , filename ) : filename = posixpath . normpath ( filename ) for sep in _os_alt_seps : if sep in filename : raise NotFound ( ) if os . path . isabs ( filename ) or filename == '..' or filename . startswith ( '../' ) : raise NotFound ( ) return os . path . join ( directory , filename )


def get_root_path(import_name):
    """Returns the path to a package or cwd if that cannot be found.  This
    returns the path of a package or the folder that contains a module.

    Not to be confused with the package path returned by :func:`find_package`.
    """
    # Module already imported and has a file attribute.  Use that first.
    mod = sys.modules.get(import_name)
    if mod is not None and hasattr(mod, '__file__'):
        return os.path.dirname(os.path.abspath(mod.__file__))

    # Next attempt: check the loader.
    loader = pkgutil.get_loader(import_name)

    # Loader does not exist or we're referring to an unloaded main module
    # or a main module without path (interactive sessions), go with the
    # current working directory.
    if loader is None or import_name == '__main__':
        return os.getcwd()

    # For .egg, zipimporter does not have get_filename until Python 2.7.
    # Some other loaders might exhibit the same behavior.
    if hasattr(loader, 'get_filename'):
        filepath = loader.get_filename(import_name)
    else:
        # Fall back to imports.
        __import__(import_name)
        filepath = sys.modules[import_name].__file__

    # filepath is import_name.py for a module, or __init__.py for a package.
    return os.path.dirname(os.path.abspath(filepath))

def get_root_path ( import_name ) : mod = sys . modules . get ( import_name ) if mod is not None and hasattr ( mod , '__file__' ) : return os . path . dirname ( os . path . abspath ( mod . __file__ ) ) loader = pkgutil . get_loader ( import_name ) if loader is None or import_name == '__main__' : return os . getcwd ( ) if hasattr ( loader , 'get_filename' ) : filepath = loader . get_filename ( import_name ) else : __import__ ( import_name ) filepath = sys . modules [ import_name ] . __file__ return os . path . dirname ( os . path . abspath ( filepath ) )

Returns the path to a package or cwd if that cannot be found . This returns the path of a package or the folder that contains a module .


def jinja_loader(self):
        """The Jinja loader for this package bound object.

        .. versionadded:: 0.5
        """
        if self.template_folder is not None:
            return FileSystemLoader(os.path.join(self.root_path,
                                                 self.template_folder))

def jinja_loader ( self ) : if self . template_folder is not None : return FileSystemLoader ( os . path . join ( self . root_path , self . template_folder ) )

The Jinja loader for this package bound object .


def run(self, options, args):
        """Prints the completion code of the given shell"""
        shells = COMPLETION_SCRIPTS.keys()
        shell_options = ['--' + shell for shell in sorted(shells)]
        if options.shell in shells:
            script = COMPLETION_SCRIPTS.get(options.shell, '')
            print(BASE_COMPLETION % {'script': script, 'shell': options.shell})
        else:
            sys.stderr.write(
                'ERROR: You must pass %s\n' % ' or '.join(shell_options)
            )

def
run
(
self
,
options
,
args
)
:
shells
=
COMPLETION_SCRIPTS
.
keys
(
)
shell_options
=
[
'--'
+
shell
for
shell
in
sorted
(
shells
)
]
if
options
.
shell
in
shells
:
script
=
COMPLETION_SCRIPTS
.
get
(
options
.
shell
,
''
)
print
(
BASE_COMPLETION
%
{
'script'
:
script
,
'shell'
:
options
.
shell
}
)
else
:
sys
.
stderr
.
write
(
'ERROR: You must pass %s\n'
%
' or '
.
join
(
shell_options
)
)


def get_cookie_domain(self, app):
        """Helpful helper method that returns the cookie domain that should
        be used for the session cookie if session cookies are used.
        """
        if app.config['SESSION_COOKIE_DOMAIN'] is not None:
            return app.config['SESSION_COOKIE_DOMAIN']
        if app.config['SERVER_NAME'] is not None:
            # chop of the port which is usually not supported by browsers
            rv = '.' + app.config['SERVER_NAME'].rsplit(':', 1)[0]

            # Google chrome does not like cookies set to .localhost, so
            # we just go with no domain then.  Flask documents anyways that
            # cross domain cookies need a fully qualified domain name
            if rv == '.localhost':
                rv = None

            # If we infer the cookie domain from the server name we need
            # to check if we are in a subpath.  In that case we can't
            # set a cross domain cookie.
            if rv is not None:
                path = self.get_cookie_path(app)
                if path != '/':
                    rv = rv.lstrip('.')

            return rv

def get_cookie_domain ( self , app ) : if app . config [ 'SESSION_COOKIE_DOMAIN' ] is not None : return app . config [ 'SESSION_COOKIE_DOMAIN' ] if app . config [ 'SERVER_NAME' ] is not None : rv = '.' + app . config [ 'SERVER_NAME' ] . rsplit ( ':' , 1 ) [ 0 ] if rv == '.localhost' : rv = None if rv is not None : path = self . get_cookie_path ( app ) if path != '/' : rv = rv . lstrip ( '.' ) return rv

Helpful helper method that returns the cookie domain that should be used for the session cookie if session cookies are used .


def _cache_for_link(cache_dir, link):
    """
    Return a directory to store cached wheels in for link.

    Because there are M wheels for any one sdist, we provide a directory
    to cache them in, and then consult that directory when looking up
    cache hits.

    We only insert things into the cache if they have plausible version
    numbers, so that we don't contaminate the cache with things that were not
    unique. E.g. ./package might have dozens of installs done for it and build
    a version of 0.0...and if we built and cached a wheel, we'd end up using
    the same wheel even if the source has been edited.

    :param cache_dir: The cache_dir being used by pip.
    :param link: The link of the sdist for which this will cache wheels.
    """

    # We want to generate an url to use as our cache key, we don't want to just
    # re-use the URL because it might have other items in the fragment and we
    # don't care about those.
    key_parts = [link.url_without_fragment]
    if link.hash_name is not None and link.hash is not None:
        key_parts.append("=".join([link.hash_name, link.hash]))
    key_url = "#".join(key_parts)

    # Encode our key url with sha224, we'll use this because it has similar
    # security properties to sha256, but with a shorter total output (and thus
    # less secure). However the differences don't make a lot of difference for
    # our use case here.
    hashed = hashlib.sha224(key_url.encode()).hexdigest()

    # We want to nest the directories some to prevent having a ton of top level
    # directories where we might run out of sub directories on some FS.
    parts = [hashed[:2], hashed[2:4], hashed[4:6], hashed[6:]]

    # Inside of the base location for cached wheels, expand our parts and join
    # them all together.
    return os.path.join(cache_dir, "wheels", *parts)

def _cache_for_link ( cache_dir , link ) : key_parts = [ link . url_without_fragment ] if link . hash_name is not None and link . hash is not None : key_parts . append ( "=" . join ( [ link . hash_name , link . hash ] ) ) key_url = "#" . join ( key_parts ) hashed = hashlib . sha224 ( key_url . encode ( ) ) . hexdigest ( ) parts = [ hashed [ : 2 ] , hashed [ 2 : 4 ] , hashed [ 4 : 6 ] , hashed [ 6 : ] ] return os . path . join ( cache_dir , "wheels" , * parts )

Return a directory to store cached wheels in for link .


def root_is_purelib(name, wheeldir):
    """
    Return True if the extracted wheel in wheeldir should go into purelib.
    """
    name_folded = name.replace("-", "_")
    for item in os.listdir(wheeldir):
        match = dist_info_re.match(item)
        if match and match.group('name') == name_folded:
            with open(os.path.join(wheeldir, item, 'WHEEL')) as wheel:
                for line in wheel:
                    line = line.lower().rstrip()
                    if line == "root-is-purelib: true":
                        return True
    return False

def
root_is_purelib
(
name
,
wheeldir
)
:
name_folded
=
name
.
replace
(
"-"
,
"_"
)
for
item
in
os
.
listdir
(
wheeldir
)
:
match
=
dist_info_re
.
match
(
item
)
if
match
and
match
.
group
(
'name'
)
==
name_folded
:
with
open
(
os
.
path
.
join
(
wheeldir
,
item
,
'WHEEL'
)
)
as
wheel
:
for
line
in
wheel
:
line
=
line
.
lower
(
)
.
rstrip
(
)
if
line
==
"root-is-purelib: true"
:
return
True
return
False

Return True if the extracted wheel in wheeldir should go into purelib .


def uninstallation_paths(dist):
    """
    Yield all the uninstallation paths for dist based on RECORD-without-.pyc

    Yield paths to all the files in RECORD. For each .py file in RECORD, add
    the .pyc in the same directory.

    UninstallPathSet.add() takes care of the __pycache__ .pyc.
    """
    from pip.utils import FakeFile  # circular import
    r = csv.reader(FakeFile(dist.get_metadata_lines('RECORD')))
    for row in r:
        path = os.path.join(dist.location, row[0])
        yield path
        if path.endswith('.py'):
            dn, fn = os.path.split(path)
            base = fn[:-3]
            path = os.path.join(dn, base + '.pyc')
            yield path

def uninstallation_paths ( dist ) : from pip . utils import FakeFile r = csv . reader ( FakeFile ( dist . get_metadata_lines ( 'RECORD' ) ) ) for row in r : path = os . path . join ( dist . location , row [ 0 ] ) yield path if path . endswith ( '.py' ) : dn , fn = os . path . split ( path ) base = fn [ : - 3 ] path = os . path . join ( dn , base + '.pyc' ) yield path

Yield all the uninstallation paths for dist based on RECORD - without - . pyc


def check_compatibility(version, name):
    """
    Raises errors or warns if called with an incompatible Wheel-Version.

    Pip should refuse to install a Wheel-Version that's a major series
    ahead of what it's compatible with (e.g 2.0 > 1.1); and warn when
    installing a version only minor version ahead (e.g 1.2 > 1.1).

    version: a 2-tuple representing a Wheel-Version (Major, Minor)
    name: name of wheel or package to raise exception about

    :raises UnsupportedWheel: when an incompatible Wheel-Version is given
    """
    if not version:
        raise UnsupportedWheel(
            "%s is in an unsupported or invalid wheel" % name
        )
    if version[0] > VERSION_COMPATIBLE[0]:
        raise UnsupportedWheel(
            "%s's Wheel-Version (%s) is not compatible with this version "
            "of pip" % (name, '.'.join(map(str, version)))
        )
    elif version > VERSION_COMPATIBLE:
        logger.warning(
            'Installing from a newer Wheel-Version (%s)',
            '.'.join(map(str, version)),
        )

Raises errors or warns if called with an incompatible Wheel - Version .


def _build_one(self, req, output_dir):
        """Build one wheel.

        :return: The filename of the built wheel, or None if the build failed.
        """
        tempd = tempfile.mkdtemp('pip-wheel-')
        try:
            if self.__build_one(req, tempd):
                try:
                    wheel_name = os.listdir(tempd)[0]
                    wheel_path = os.path.join(output_dir, wheel_name)
                    shutil.move(os.path.join(tempd, wheel_name), wheel_path)
                    logger.info('Stored in directory: %s', output_dir)
                    return wheel_path
                except:
                    return None
            return None
        finally:
            rmtree(tempd)

def
_build_one
(
self
,
req
,
output_dir
)
:
tempd
=
tempfile
.
mkdtemp
(
'pip-wheel-'
)
try
:
if
self
.
__build_one
(
req
,
tempd
)
:
try
:
wheel_name
=
os
.
listdir
(
tempd
)
[
0
]
wheel_path
=
os
.
path
.
join
(
output_dir
,
wheel_name
)
shutil
.
move
(
os
.
path
.
join
(
tempd
,
wheel_name
)
,
wheel_path
)
logger
.
info
(
'Stored in directory: %s'
,
output_dir
)
return
wheel_path
except
:
return
return
finally
:
rmtree
(
tempd
)


def iter_symbols(code):
    """Yield names and strings used by `code` and its nested code objects"""
    for name in code.co_names:
        yield name
    for const in code.co_consts:
        if isinstance(const, basestring):
            yield const
        elif isinstance(const, CodeType):
            for name in iter_symbols(const):
                yield name

def iter_symbols ( code ) : for name in code . co_names : yield name for const in code . co_consts : if isinstance ( const , basestring ) : yield const elif isinstance ( const , CodeType ) : for name in iter_symbols ( const ) : yield name

Yield names and strings used by code and its nested code objects


def ensure_fresh_rates(func):
    """Decorator for Backend that ensures rates are fresh within last 5 mins"""
    def wrapper(self, *args, **kwargs):
        if self.last_updated + timedelta(minutes=5) < zulu.now():
            self.refresh()
        return func(self, *args, **kwargs)
    return wrapper

def ensure_fresh_rates ( func ) : def wrapper ( self , * args , ** kwargs ) : if self . last_updated + timedelta ( minutes = 5 ) < zulu . now ( ) : self . refresh ( ) return func ( self , * args , ** kwargs ) return wrapper


def _add_egg_info(self, cmd):
        """
        Add paths for egg-info files for an external egg-base.

        The egg-info files are written to egg-base. If egg-base is
        outside the current working directory, this method
        searchs the egg-base directory for files to include
        in the manifest. Uses distutils.filelist.findall (which is
        really the version monkeypatched in by setuptools/__init__.py)
        to perform the search.

        Since findall records relative paths, prefix the returned
        paths with cmd.egg_base, so add_default's include_pattern call
        (which is looking for the absolute cmd.egg_info) will match
        them.
        """
        if cmd.egg_base == os.curdir:
            # egg-info files were already added by something else
            return

        discovered = distutils.filelist.findall(cmd.egg_base)
        resolved = (os.path.join(cmd.egg_base, path) for path in discovered)
        self.filelist.allfiles.extend(resolved)

def _add_egg_info ( self , cmd ) : if cmd . egg_base == os . curdir : return discovered = distutils . filelist . findall ( cmd . egg_base ) resolved = ( os . path . join ( cmd . egg_base , path ) for path in discovered ) self . filelist . allfiles . extend ( resolved )

Add paths for egg - info files for an external egg - base .


def write_delete_marker_file(directory):
    """
    Write the pip delete marker file into this directory.
    """
    filepath = os.path.join(directory, PIP_DELETE_MARKER_FILENAME)
    with open(filepath, 'w') as marker_fp:
        marker_fp.write(DELETE_MARKER_MESSAGE)

def write_delete_marker_file ( directory ) : filepath = os . path . join ( directory , PIP_DELETE_MARKER_FILENAME ) with open ( filepath , 'w' ) as marker_fp : marker_fp . write ( DELETE_MARKER_MESSAGE )

Write the pip delete marker file into this directory .