sites = []<EOL>if running_under_virtualenv():<EOL><INDENT>if virtualenv_no_global():<EOL><INDENT>sites.append(site_packages)<EOL><DEDENT>else:<EOL><INDENT>sites.append(site_packages)<EOL>if user_site:<EOL><INDENT>sites.append(user_site)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>if user_site:<EOL><INDENT>sites.append(user_site)<EOL><DEDENT>sites.append(site_packages)<EOL><DEDENT>for site in sites:<EOL><INDENT>egglink = os.path.join(site, dist.project_name) + '<STR_LIT>'<EOL>if os.path.isfile(egglink):<EOL><INDENT>return egglink<EOL><DEDENT><DEDENT>

Return the path for the .egg-link file if it exists, otherwise, None.

There's 3 scenarios:
1) not in a virtualenv
   try to find in site.USER_SITE, then site_packages
2) in a no-global virtualenv
   try to find in site_packages
3) in a yes-global virtualenv
   try to find in site_packages, then site.USER_SITE
   (don't look in global location)

For #1 and #3, there could be odd cases, where there's an egg-link in 2
locations.

This method will just return the first one found.


egg_link = egg_link_path(dist)<EOL>if egg_link:<EOL><INDENT>return egg_link<EOL><DEDENT>return dist.location<EOL>

Get the site-packages location of this distribution. Generally
this is dist.location, except in the case of develop-installed
packages, where dist.location is the source code location, and we
want to know where the egg-link file is.


def ioctl_GWINSZ(fd):<EOL><INDENT>try:<EOL><INDENT>import fcntl<EOL>import termios<EOL>import struct<EOL>cr = struct.unpack(<EOL>'<STR_LIT>',<EOL>fcntl.ioctl(fd, termios.TIOCGWINSZ, '<STR_LIT>')<EOL>)<EOL><DEDENT>except:<EOL><INDENT>return None<EOL><DEDENT>if cr == (<NUM_LIT:0>, <NUM_LIT:0>):<EOL><INDENT>return None<EOL><DEDENT>return cr<EOL><DEDENT>cr = ioctl_GWINSZ(<NUM_LIT:0>) or ioctl_GWINSZ(<NUM_LIT:1>) or ioctl_GWINSZ(<NUM_LIT:2>)<EOL>if not cr:<EOL><INDENT>try:<EOL><INDENT>fd = os.open(os.ctermid(), os.O_RDONLY)<EOL>cr = ioctl_GWINSZ(fd)<EOL>os.close(fd)<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if not cr:<EOL><INDENT>cr = (os.environ.get('<STR_LIT>', <NUM_LIT>), os.environ.get('<STR_LIT>', <NUM_LIT>))<EOL><DEDENT>return int(cr[<NUM_LIT:1>]), int(cr[<NUM_LIT:0>])<EOL>

Returns a tuple (x, y) representing the width(x) and the height(x)
    in characters of the terminal window.


mask = os.umask(<NUM_LIT:0>)<EOL>os.umask(mask)<EOL>return mask<EOL>

Get the current umask which involves having to set it temporarily.


def unzip_file(filename, location, flatten=True):

if not os.path.exists(location):<EOL><INDENT>os.makedirs(location)<EOL><DEDENT>zipfp = open(filename, '<STR_LIT:rb>')<EOL>try:<EOL><INDENT>zip = zipfile.ZipFile(zipfp, allowZip64=True)<EOL>leading = has_leading_dir(zip.namelist()) and flatten<EOL>for info in zip.infolist():<EOL><INDENT>name = info.filename<EOL>data = zip.read(name)<EOL>fn = name<EOL>if leading:<EOL><INDENT>fn = split_leading_dir(name)[<NUM_LIT:1>]<EOL><DEDENT>fn = os.path.join(location, fn)<EOL>dir = os.path.dirname(fn)<EOL>if not os.path.exists(dir):<EOL><INDENT>os.makedirs(dir)<EOL><DEDENT>if fn.endswith('<STR_LIT:/>') or fn.endswith('<STR_LIT:\\>'):<EOL><INDENT>if not os.path.exists(fn):<EOL><INDENT>os.makedirs(fn)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>fp = open(fn, '<STR_LIT:wb>')<EOL>try:<EOL><INDENT>fp.write(data)<EOL><DEDENT>finally:<EOL><INDENT>fp.close()<EOL>mode = info.external_attr >> <NUM_LIT:16><EOL>if mode and stat.S_ISREG(mode) and mode & <NUM_LIT>:<EOL><INDENT>os.chmod(fn, (<NUM_LIT> - current_umask() | <NUM_LIT>))<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>zipfp.close()<EOL><DEDENT>

Unzip the file (with path `filename`) to the destination `location`.  All
files are written based on system defaults and umask (i.e. permissions are
not preserved), except that regular file members with any execute
permissions (user, group, or world) have "chmod +x" applied after being
written. Note that for windows, any execute changes using os.chmod are
no-ops per the python docs.


if not os.path.exists(location):<EOL><INDENT>os.makedirs(location)<EOL><DEDENT>if filename.lower().endswith('<STR_LIT>') or filename.lower().endswith('<STR_LIT>'):<EOL><INDENT>mode = '<STR_LIT>'<EOL><DEDENT>elif (filename.lower().endswith('<STR_LIT>')<EOL>or filename.lower().endswith('<STR_LIT>')):<EOL><INDENT>mode = '<STR_LIT>'<EOL><DEDENT>elif filename.lower().endswith('<STR_LIT>'):<EOL><INDENT>mode = '<STR_LIT:r>'<EOL><DEDENT>else:<EOL><INDENT>logger.warning(<EOL>'<STR_LIT>', filename,<EOL>)<EOL>mode = '<STR_LIT>'<EOL><DEDENT>tar = tarfile.open(filename, mode)<EOL>try:<EOL><INDENT>leading = has_leading_dir([<EOL>member.name for member in tar.getmembers()<EOL>if member.name != '<STR_LIT>'<EOL>])<EOL>for member in tar.getmembers():<EOL><INDENT>fn = member.name<EOL>if fn == '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>if leading:<EOL><INDENT>fn = split_leading_dir(fn)[<NUM_LIT:1>]<EOL><DEDENT>path = os.path.join(location, fn)<EOL>if member.isdir():<EOL><INDENT>if not os.path.exists(path):<EOL><INDENT>os.makedirs(path)<EOL><DEDENT><DEDENT>elif member.issym():<EOL><INDENT>try:<EOL><INDENT>tar._extract_member(member, path)<EOL><DEDENT>except Exception as exc:<EOL><INDENT>logger.warning(<EOL>'<STR_LIT>',<EOL>filename, member.name, exc,<EOL>)<EOL>continue<EOL><DEDENT><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>fp = tar.extractfile(member)<EOL><DEDENT>except (KeyError, AttributeError) as exc:<EOL><INDENT>logger.warning(<EOL>'<STR_LIT>',<EOL>filename, member.name, exc,<EOL>)<EOL>continue<EOL><DEDENT>if not os.path.exists(os.path.dirname(path)):<EOL><INDENT>os.makedirs(os.path.dirname(path))<EOL><DEDENT>destfp = open(path, '<STR_LIT:wb>')<EOL>try:<EOL><INDENT>shutil.copyfileobj(fp, destfp)<EOL><DEDENT>finally:<EOL><INDENT>destfp.close()<EOL><DEDENT>fp.close()<EOL>if member.mode & <NUM_LIT>:<EOL><INDENT>os.chmod(path, (<NUM_LIT> - current_umask() | <NUM_LIT>))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>tar.close()<EOL><DEDENT>

Untar the file (with path `filename`) to the destination `location`.
All files are written based on system defaults and umask (i.e. permissions
are not preserved), except that regular file members with any execute
permissions (user, group, or world) have "chmod +x" applied after being
written.  Note that for windows, any execute changes using os.chmod are
no-ops per the python docs.


with open(filename, '<STR_LIT:rb>') as fp:<EOL><INDENT>data = fp.read()<EOL><DEDENT>encodings = ['<STR_LIT:utf-8>', locale.getpreferredencoding(False), '<STR_LIT>']<EOL>for enc in encodings:<EOL><INDENT>try:<EOL><INDENT>data = data.decode(enc)<EOL><DEDENT>except UnicodeDecodeError:<EOL><INDENT>continue<EOL><DEDENT>break<EOL><DEDENT>assert type(data) != bytes  <EOL>return data<EOL>

Return the contents of *filename*.

    Try to decode the file contents with utf-8, the preferred system encoding
    (e.g., cp1252 on some Windows machines), and latin1, in that order.
    Decoding a byte string with latin1 will never raise an error. In the worst
    case, the returned string will contain some garbage characters.


orig_stdout = getattr(sys, stream_name)<EOL>setattr(sys, stream_name, StreamWrapper.from_stream(orig_stdout))<EOL>try:<EOL><INDENT>yield getattr(sys, stream_name)<EOL><DEDENT>finally:<EOL><INDENT>setattr(sys, stream_name, orig_stdout)<EOL><DEDENT>

Return a context manager used by captured_stdout/stdin/stderr
    that temporarily replaces the sys stream *stream_name* with a StringIO.

    Taken from Lib/support/__init__.py in the CPython repo.


return captured_output('<STR_LIT>')<EOL>

Capture the output of sys.stdout:

       with captured_stdout() as stdout:
           print('hello')
       self.assertEqual(stdout.getvalue(), 'hello\n')

    Taken from Lib/support/__init__.py in the CPython repo.


if WINDOWS:<EOL><INDENT>path = os.path.normpath(_get_win_folder("<STR_LIT>"))<EOL>path = os.path.join(path, appname, "<STR_LIT>")<EOL><DEDENT>elif sys.platform == "<STR_LIT>":<EOL><INDENT>path = os.path.expanduser("<STR_LIT>")<EOL>path = os.path.join(path, appname)<EOL><DEDENT>else:<EOL><INDENT>path = os.getenv("<STR_LIT>", os.path.expanduser("<STR_LIT>"))<EOL>path = os.path.join(path, appname)<EOL><DEDENT>return path<EOL>

r"""
    Return full path to the user-specific cache dir for this application.

        "appname" is the name of application.

    Typical user cache directories are:
        Mac OS X:   ~/Library/Caches/<AppName>
        Unix:       ~/.cache/<AppName> (XDG default)
        Windows:      C:\Users\<username>\AppData\Local\<AppName>\Cache

    On Windows the only suggestion in the MSDN docs is that local settings go
    in the `CSIDL_LOCAL_APPDATA` directory. This is identical to the
    non-roaming app data dir (the default returned by `user_data_dir`). Apps
    typically put cache data somewhere *under* the given dir here. Some
    examples:
        ...\Mozilla\Firefox\Profiles\<ProfileName>\Cache
        ...\Acme\SuperApp\Cache\1.0

    OPINION: This function appends "Cache" to the `CSIDL_LOCAL_APPDATA` value.


if WINDOWS:<EOL><INDENT>const = roaming and "<STR_LIT>" or "<STR_LIT>"<EOL>path = os.path.join(os.path.normpath(_get_win_folder(const)), appname)<EOL><DEDENT>elif sys.platform == "<STR_LIT>":<EOL><INDENT>path = os.path.join(<EOL>os.path.expanduser('<STR_LIT>'),<EOL>appname,<EOL>)<EOL><DEDENT>else:<EOL><INDENT>path = os.path.join(<EOL>os.getenv('<STR_LIT>', os.path.expanduser("<STR_LIT>")),<EOL>appname,<EOL>)<EOL><DEDENT>return path<EOL>

Return full path to the user-specific data dir for this application.

    "appname" is the name of application.
        If None, just the system directory is returned.
    "roaming" (boolean, default False) can be set True to use the Windows
        roaming appdata directory. That means that for users on a Windows
        network setup for roaming profiles, this user data will be
        sync'd on login. See
        <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
        for a discussion of issues.

Typical user data directories are:
    Mac OS X:               ~/Library/Application Support/<AppName>
    Unix:                   ~/.local/share/<AppName>    # or in
                            $XDG_DATA_HOME, if defined
    Win XP (not roaming):   C:\Documents and Settings\<username>\ ...
                            ...Application Data\<AppName>
    Win XP (roaming):       C:\Documents and Settings\<username>\Local ...
                            ...Settings\Application Data\<AppName>
    Win 7  (not roaming):   C:\\Users\<username>\AppData\Local\<AppName>
    Win 7  (roaming):       C:\\Users\<username>\AppData\Roaming\<AppName>

For Unix, we follow the XDG spec and support $XDG_DATA_HOME.
That means, by default "~/.local/share/<AppName>".


if WINDOWS:<EOL><INDENT>path = os.path.join(user_data_dir(appname), "<STR_LIT>")<EOL><DEDENT>elif sys.platform == "<STR_LIT>":<EOL><INDENT>path = os.path.join(os.path.expanduser('<STR_LIT>'), appname)<EOL><DEDENT>else:<EOL><INDENT>path = os.path.join(user_cache_dir(appname), "<STR_LIT>")<EOL><DEDENT>return path<EOL>

Return full path to the user-specific log dir for this application.

    "appname" is the name of application.
        If None, just the system directory is returned.

Typical user cache directories are:
    Mac OS X:   ~/Library/Logs/<AppName>
    Unix:       ~/.cache/<AppName>/log  # or under $XDG_CACHE_HOME if
                defined
    Win XP:     C:\Documents and Settings\<username>\Local Settings\ ...
                ...Application Data\<AppName>\Logs
    Vista:      C:\\Users\<username>\AppData\Local\<AppName>\Logs

On Windows the only suggestion in the MSDN docs is that local settings
go in the `CSIDL_LOCAL_APPDATA` directory. (Note: I'm interested in
examples of what some windows apps use for a logs dir.)

OPINION: This function appends "Logs" to the `CSIDL_LOCAL_APPDATA`
value for Windows and appends "log" to the user cache dir for Unix.


if WINDOWS:<EOL><INDENT>path = user_data_dir(appname, roaming=roaming)<EOL><DEDENT>elif sys.platform == "<STR_LIT>":<EOL><INDENT>path = user_data_dir(appname)<EOL><DEDENT>else:<EOL><INDENT>path = os.getenv('<STR_LIT>', os.path.expanduser("<STR_LIT>"))<EOL>path = os.path.join(path, appname)<EOL><DEDENT>return path<EOL>

Return full path to the user-specific config dir for this application.

        "appname" is the name of application.
            If None, just the system directory is returned.
        "roaming" (boolean, default True) can be set False to not use the
            Windows roaming appdata directory. That means that for users on a
            Windows network setup for roaming profiles, this user data will be
            sync'd on login. See
            <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
            for a discussion of issues.

    Typical user data directories are:
        Mac OS X:               same as user_data_dir
        Unix:                   ~/.config/<AppName>
        Win *:                  same as user_data_dir

    For Unix, we follow the XDG spec and support $XDG_CONFIG_HOME.
    That means, by deafult "~/.config/<AppName>".


if WINDOWS:<EOL><INDENT>path = os.path.normpath(_get_win_folder("<STR_LIT>"))<EOL>pathlist = [os.path.join(path, appname)]<EOL><DEDENT>elif sys.platform == '<STR_LIT>':<EOL><INDENT>pathlist = [os.path.join('<STR_LIT>', appname)]<EOL><DEDENT>else:<EOL><INDENT>xdg_config_dirs = os.getenv('<STR_LIT>', '<STR_LIT>')<EOL>if xdg_config_dirs:<EOL><INDENT>pathlist = [<EOL>os.sep.join([os.path.expanduser(x), appname])<EOL>for x in xdg_config_dirs.split(os.pathsep)<EOL>]<EOL><DEDENT>else:<EOL><INDENT>pathlist = []<EOL><DEDENT>pathlist.append('<STR_LIT>')<EOL><DEDENT>return pathlist<EOL>

Return a list of potential user-shared config dirs for this application.

        "appname" is the name of application.

    Typical user config directories are:
        Mac OS X:   /Library/Application Support/<AppName>/
        Unix:       /etc or $XDG_CONFIG_DIRS[i]/<AppName>/ for each value in
                    $XDG_CONFIG_DIRS
        Win XP:     C:\Documents and Settings\All Users\Application ...
                    ...Data\<AppName>\
        Vista:      (Fail! "C:\ProgramData" is a hidden *system* directory
                    on Vista.)
        Win 7:      Hidden, but writeable on Win 7:
                    C:\ProgramData\<AppName>\


import _winreg<EOL>shell_folder_name = {<EOL>"<STR_LIT>": "<STR_LIT>",<EOL>"<STR_LIT>": "<STR_LIT>",<EOL>"<STR_LIT>": "<STR_LIT>",<EOL>}[csidl_name]<EOL>key = _winreg.OpenKey(<EOL>_winreg.HKEY_CURRENT_USER,<EOL>r"<STR_LIT>"<EOL>)<EOL>directory, _type = _winreg.QueryValueEx(key, shell_folder_name)<EOL>return directory<EOL>

This is a fallback technique at best. I'm not sure if using the
registry for this guarantees us the correct answer for all CSIDL_*
names.


shells = COMPLETION_SCRIPTS.keys()<EOL>shell_options = ['<STR_LIT>' + shell for shell in sorted(shells)]<EOL>if options.shell in shells:<EOL><INDENT>script = COMPLETION_SCRIPTS.get(options.shell, '<STR_LIT>')<EOL>print(BASE_COMPLETION % {'<STR_LIT>': script, '<STR_LIT>': options.shell})<EOL><DEDENT>else:<EOL><INDENT>sys.stderr.write(<EOL>'<STR_LIT>' % '<STR_LIT>'.join(shell_options)<EOL>)<EOL><DEDENT>

Prints the completion code of the given shell


def _build_package_finder(self, options, index_urls, session):

Create a package finder appropriate to this list command.


packages = {}<EOL>for hit in hits:<EOL><INDENT>name = hit['<STR_LIT:name>']<EOL>summary = hit['<STR_LIT>']<EOL>version = hit['<STR_LIT:version>']<EOL>score = hit['<STR_LIT>']<EOL>if score is None:<EOL><INDENT>score = <NUM_LIT:0><EOL><DEDENT>if name not in packages.keys():<EOL><INDENT>packages[name] = {<EOL>'<STR_LIT:name>': name,<EOL>'<STR_LIT>': summary,<EOL>'<STR_LIT>': [version],<EOL>'<STR_LIT>': score,<EOL>}<EOL><DEDENT>else:<EOL><INDENT>packages[name]['<STR_LIT>'].append(version)<EOL>if version == highest_version(packages[name]['<STR_LIT>']):<EOL><INDENT>packages[name]['<STR_LIT>'] = summary<EOL>packages[name]['<STR_LIT>'] = score<EOL><DEDENT><DEDENT><DEDENT>package_list = sorted(<EOL>packages.values(),<EOL>key=lambda x: x['<STR_LIT>'],<EOL>reverse=True,<EOL>)<EOL>return package_list<EOL>

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.


Create a package finder appropriate to this install command.
This method is meant to be overridden by subclasses, not
called directly.


if ordered:<EOL><INDENT>cmditems = _sort_commands(commands_dict, commands_order)<EOL><DEDENT>else:<EOL><INDENT>cmditems = commands_dict.items()<EOL><DEDENT>for name, command_class in cmditems:<EOL><INDENT>if ignore_hidden and command_class.hidden:<EOL><INDENT>continue<EOL><DEDENT>yield (name, command_class.summary)<EOL><DEDENT>

Yields sorted (command name, command summary) tuples.


from difflib import get_close_matches<EOL>name = name.lower()<EOL>close_commands = get_close_matches(name, commands_dict.keys())<EOL>if close_commands:<EOL><INDENT>return close_commands[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT>


if not self.select_paths:<EOL><INDENT>return sys.path<EOL><DEDENT>result = []<EOL>match_any = set()<EOL>for path in sys.path:<EOL><INDENT>path = os.path.normcase(os.path.abspath(path))<EOL>for match in self.select_paths:<EOL><INDENT>match = os.path.normcase(os.path.abspath(match))<EOL>if '<STR_LIT:*>' in match:<EOL><INDENT>if re.search(fnmatch.translate(match + '<STR_LIT:*>'), path):<EOL><INDENT>result.append(path)<EOL>match_any.add(match)<EOL>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if path.startswith(match):<EOL><INDENT>result.append(path)<EOL>match_any.add(match)<EOL>break<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>logger.debug(<EOL>"<STR_LIT>",<EOL>path,<EOL>'<STR_LIT:U+002CU+0020>'.join(self.select_paths),<EOL>)<EOL><DEDENT><DEDENT>for match in self.select_paths:<EOL><INDENT>if match not in match_any and '<STR_LIT:*>' not in match:<EOL><INDENT>result.append(match)<EOL>logger.debug(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>",<EOL>match,<EOL>)<EOL><DEDENT><DEDENT>return result<EOL>

All the entries of sys.path, possibly restricted by --path


installed = dict(<EOL>[(p.project_name.lower(), p) for p in pkg_resources.working_set])<EOL>query_names = [name.lower() for name in query]<EOL>for dist in [installed[pkg] for pkg in query_names if pkg in installed]:<EOL><INDENT>package = {<EOL>'<STR_LIT:name>': dist.project_name,<EOL>'<STR_LIT:version>': dist.version,<EOL>'<STR_LIT:location>': dist.location,<EOL>'<STR_LIT>': [dep.project_name for dep in dist.requires()],<EOL>}<EOL>file_list = None<EOL>if isinstance(dist, pkg_resources.DistInfoDistribution):<EOL><INDENT>if dist.has_metadata('<STR_LIT>'):<EOL><INDENT>lines = dist.get_metadata_lines('<STR_LIT>')<EOL>paths = [l.split('<STR_LIT:U+002C>')[<NUM_LIT:0>] for l in lines]<EOL>paths = [os.path.join(dist.location, p) for p in paths]<EOL>file_list = [os.path.relpath(p, dist.location) for p in paths]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if dist.has_metadata('<STR_LIT>'):<EOL><INDENT>paths = dist.get_metadata_lines('<STR_LIT>')<EOL>paths = [os.path.join(dist.egg_info, p) for p in paths]<EOL>file_list = [os.path.relpath(p, dist.location) for p in paths]<EOL><DEDENT><DEDENT>package['<STR_LIT>'] = file_list and sorted(file_list)<EOL>yield package<EOL><DEDENT>

Gather details from installed distributions. Print distribution name,
version, location, and installed files. Installed files requires a
pip generated 'installed-files.txt' in the distributions '.egg-info'
directory.


results_printed = False<EOL>for dist in distributions:<EOL><INDENT>results_printed = True<EOL>logger.info("<STR_LIT>")<EOL>logger.info("<STR_LIT>" % dist['<STR_LIT:name>'])<EOL>logger.info("<STR_LIT>" % dist['<STR_LIT:version>'])<EOL>logger.info("<STR_LIT>" % dist['<STR_LIT:location>'])<EOL>logger.info("<STR_LIT>" % '<STR_LIT:U+002CU+0020>'.join(dist['<STR_LIT>']))<EOL>if list_all_files:<EOL><INDENT>logger.info("<STR_LIT>")<EOL>if dist['<STR_LIT>'] is not None:<EOL><INDENT>for line in dist['<STR_LIT>']:<EOL><INDENT>logger.info("<STR_LIT>" % line.strip())<EOL><DEDENT><DEDENT>else:<EOL><INDENT>logger.info("<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>return results_printed<EOL>

Print the informations from installed distributions found.


return self.convert or self.strip or self.autoreset<EOL>

True if this class is actually needed. If false, then the output
stream will not be affected, nor will win32 calls be issued, so
wrapping stdout is not actually required. This will generally be
False on non-Windows platforms, unless optional functionality like
autoreset has been requested using kwargs to init()


cursor = <NUM_LIT:0><EOL>text = self.convert_osc(text)<EOL>for match in self.ANSI_CSI_RE.finditer(text):<EOL><INDENT>start, end = match.span()<EOL>self.write_plain_text(text, cursor, start)<EOL>self.convert_ansi(*match.groups())<EOL>cursor = end<EOL><DEDENT>self.write_plain_text(text, cursor, len(text))<EOL>

Write the given text to our wrapped stream, stripping any ANSI
sequences from the text, and optionally converting them into win32
calls.


handler.startDocument()<EOL>for prefix, namespace in prefix_mapping.items():<EOL><INDENT>handler.startPrefixMapping(prefix, namespace)<EOL><DEDENT>for token in walker:<EOL><INDENT>type = token["<STR_LIT:type>"]<EOL>if type == "<STR_LIT>":<EOL><INDENT>continue<EOL><DEDENT>elif type in ("<STR_LIT>", "<STR_LIT>"):<EOL><INDENT>attrs = AttributesNSImpl(token["<STR_LIT:data>"],<EOL>unadjustForeignAttributes)<EOL>handler.startElementNS((token["<STR_LIT>"], token["<STR_LIT:name>"]),<EOL>token["<STR_LIT:name>"],<EOL>attrs)<EOL>if type == "<STR_LIT>":<EOL><INDENT>handler.endElementNS((token["<STR_LIT>"], token["<STR_LIT:name>"]),<EOL>token["<STR_LIT:name>"])<EOL><DEDENT><DEDENT>elif type == "<STR_LIT>":<EOL><INDENT>handler.endElementNS((token["<STR_LIT>"], token["<STR_LIT:name>"]),<EOL>token["<STR_LIT:name>"])<EOL><DEDENT>elif type in ("<STR_LIT>", "<STR_LIT>"):<EOL><INDENT>handler.characters(token["<STR_LIT:data>"])<EOL><DEDENT>elif type == "<STR_LIT>":<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>assert False, "<STR_LIT>"<EOL><DEDENT><DEDENT>for prefix, namespace in prefix_mapping.items():<EOL><INDENT>handler.endPrefixMapping(prefix)<EOL><DEDENT>handler.endDocument()<EOL>

Call SAX-like content handler based on treewalker walker


def parse(doc, treebuilder="<STR_LIT>", encoding=None,<EOL>namespaceHTMLElements=True):

tb = treebuilders.getTreeBuilder(treebuilder)<EOL>p = HTMLParser(tb, namespaceHTMLElements=namespaceHTMLElements)<EOL>return p.parse(doc, encoding=encoding)<EOL>

Parse a string or file-like object into a tree


def __init__(self, tree=None, tokenizer=tokenizer.HTMLTokenizer,<EOL>strict=False, namespaceHTMLElements=True, debug=False):

<EOL>self.strict = strict<EOL>if tree is None:<EOL><INDENT>tree = treebuilders.getTreeBuilder("<STR_LIT>")<EOL><DEDENT>self.tree = tree(namespaceHTMLElements)<EOL>self.tokenizer_class = tokenizer<EOL>self.errors = []<EOL>self.phases = dict([(name, cls(self, self.tree)) for name, cls in<EOL>getPhases(debug).items()])<EOL>

strict - raise an exception when a parse error is encountered

tree - a treebuilder class controlling the type of tree that will be
returned. Built in treebuilders can be accessed through
html5lib.treebuilders.getTreeBuilder(treeType)

tokenizer - a class that provides a stream of tokens to the treebuilder.
This may be replaced for e.g. a sanitizer which converts some tags to
text


def parse(self, stream, encoding=None, parseMeta=True, useChardet=True):

self._parse(stream, innerHTML=False, encoding=encoding,<EOL>parseMeta=parseMeta, useChardet=useChardet)<EOL>return self.tree.getDocument()<EOL>

Parse a HTML document into a well-formed tree

        stream - a filelike object or string containing the HTML to be parsed

        The optional encoding parameter must be a string that indicates
        the encoding.  If specified, that encoding will be used,
        regardless of any BOM or later declaration (such as in a meta
        element)


def parseFragment(self, stream, container="<STR_LIT>", encoding=None,<EOL>parseMeta=False, useChardet=True):

self._parse(stream, True, container=container, encoding=encoding)<EOL>return self.tree.getFragment()<EOL>

Parse a HTML fragment into a well-formed tree fragment

        container - name of the element we're setting the innerHTML property
        if set to None, default to 'div'

        stream - a filelike object or string containing the HTML to be parsed

        The optional encoding parameter must be a string that indicates
        the encoding.  If specified, that encoding will be used,
        regardless of any BOM or later declaration (such as in a meta
        element)


if token["<STR_LIT:type>"] == tokenTypes["<STR_LIT>"]:<EOL><INDENT>token["<STR_LIT:data>"] = dict(token["<STR_LIT:data>"][::-<NUM_LIT:1>])<EOL><DEDENT>return token<EOL>

HTML5 specific normalizations to the token stream


def parseRCDataRawtext(self, token, contentType):

assert contentType in ("<STR_LIT>", "<STR_LIT>")<EOL>self.tree.insertElement(token)<EOL>if contentType == "<STR_LIT>":<EOL><INDENT>self.tokenizer.state = self.tokenizer.rawtextState<EOL><DEDENT>else:<EOL><INDENT>self.tokenizer.state = self.tokenizer.rcdataState<EOL><DEDENT>self.originalPhase = self.phase<EOL>self.phase = self.phases["<STR_LIT:text>"]<EOL>

Generic RCDATA/RAWTEXT Parsing algorithm
        contentType - RCDATA or RAWTEXT


self.tokenQueue = deque([])<EOL>while self.state():<EOL><INDENT>while self.stream.errors:<EOL><INDENT>yield {"<STR_LIT:type>": tokenTypes["<STR_LIT>"], "<STR_LIT:data>": self.stream.errors.pop(<NUM_LIT:0>)}<EOL><DEDENT>while self.tokenQueue:<EOL><INDENT>yield self.tokenQueue.popleft()<EOL><DEDENT><DEDENT>

This is where the magic happens.

        We do our usually processing through the states and when we have a token
        to return we yield the token which pauses processing until the next token
        is requested.


allowed = digits<EOL>radix = <NUM_LIT:10><EOL>if isHex:<EOL><INDENT>allowed = hexDigits<EOL>radix = <NUM_LIT:16><EOL><DEDENT>charStack = []<EOL>c = self.stream.char()<EOL>while c in allowed and c is not EOF:<EOL><INDENT>charStack.append(c)<EOL>c = self.stream.char()<EOL><DEDENT>charAsInt = int("<STR_LIT>".join(charStack), radix)<EOL>if charAsInt in replacementCharacters:<EOL><INDENT>char = replacementCharacters[charAsInt]<EOL>self.tokenQueue.append({"<STR_LIT:type>": tokenTypes["<STR_LIT>"], "<STR_LIT:data>":<EOL>"<STR_LIT>",<EOL>"<STR_LIT>": {"<STR_LIT>": charAsInt}})<EOL><DEDENT>elif ((<NUM_LIT> <= charAsInt <= <NUM_LIT>) or<EOL>(charAsInt > <NUM_LIT>)):<EOL><INDENT>char = "<STR_LIT>"<EOL>self.tokenQueue.append({"<STR_LIT:type>": tokenTypes["<STR_LIT>"], "<STR_LIT:data>":<EOL>"<STR_LIT>",<EOL>"<STR_LIT>": {"<STR_LIT>": charAsInt}})<EOL><DEDENT>else:<EOL><INDENT>if ((<NUM_LIT> <= charAsInt <= <NUM_LIT>) or<EOL>(<NUM_LIT> <= charAsInt <= <NUM_LIT>) or<EOL>(<NUM_LIT> <= charAsInt <= <NUM_LIT>) or<EOL>(<NUM_LIT> <= charAsInt <= <NUM_LIT>) or<EOL>charAsInt in frozenset([<NUM_LIT>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT>,<EOL><NUM_LIT>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT>,<EOL><NUM_LIT>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT>,<EOL><NUM_LIT>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT>,<EOL><NUM_LIT>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT>,<EOL><NUM_LIT>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT>,<EOL><NUM_LIT>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT>,<EOL><NUM_LIT>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT>,<EOL><NUM_LIT>, <NUM_LIT>, <NUM_LIT>])):<EOL><INDENT>self.tokenQueue.append({"<STR_LIT:type>": tokenTypes["<STR_LIT>"],<EOL>"<STR_LIT:data>":<EOL>"<STR_LIT>",<EOL>"<STR_LIT>": {"<STR_LIT>": charAsInt}})<EOL><DEDENT>try:<EOL><INDENT>char = chr(charAsInt)<EOL><DEDENT>except ValueError:<EOL><INDENT>v = charAsInt - <NUM_LIT><EOL>char = chr(<NUM_LIT> | (v >> <NUM_LIT:10>)) + chr(<NUM_LIT> | (v & <NUM_LIT>))<EOL><DEDENT><DEDENT>if c != "<STR_LIT:;>":<EOL><INDENT>self.tokenQueue.append({"<STR_LIT:type>": tokenTypes["<STR_LIT>"], "<STR_LIT:data>":<EOL>"<STR_LIT>"})<EOL>self.stream.unget(c)<EOL><DEDENT>return char<EOL>

This function returns either U+FFFD or the character based on the
        decimal or hexadecimal representation. It also discards ";" if present.
        If not present self.tokenQueue.append({"type": tokenTypes["ParseError"]}) is invoked.


self.consumeEntity(allowedChar=allowedChar, fromAttribute=True)<EOL>

This method replaces the need for "entityInAttributeValueState".


token = self.currentToken<EOL>if (token["<STR_LIT:type>"] in tagTokenTypes):<EOL><INDENT>if self.lowercaseElementName:<EOL><INDENT>token["<STR_LIT:name>"] = token["<STR_LIT:name>"].translate(asciiUpper2Lower)<EOL><DEDENT>if token["<STR_LIT:type>"] == tokenTypes["<STR_LIT>"]:<EOL><INDENT>if token["<STR_LIT:data>"]:<EOL><INDENT>self.tokenQueue.append({"<STR_LIT:type>": tokenTypes["<STR_LIT>"],<EOL>"<STR_LIT:data>": "<STR_LIT>"})<EOL><DEDENT>if token["<STR_LIT>"]:<EOL><INDENT>self.tokenQueue.append({"<STR_LIT:type>": tokenTypes["<STR_LIT>"],<EOL>"<STR_LIT:data>": "<STR_LIT>"})<EOL><DEDENT><DEDENT><DEDENT>self.tokenQueue.append(token)<EOL>self.state = self.dataState<EOL>

This method is a generic handler for emitting the tags. It also sets
        the state to "data" because that's what's needed after a token has been
        emitted.


self.name = name<EOL>self.parent = None<EOL>self.value = None<EOL>self.attributes = {}<EOL>self.childNodes = []<EOL>self._flags = []<EOL>

Node representing an item in the tree.
        name - The tag name associated with the node
        parent - The parent of the current node (or None for the document node)
        value - The value of the current node (applies to text nodes and
        comments
        attributes - a dict holding name, value pairs for attributes of the node
        childNodes - a list of child nodes of the current node. This must
        include all elements but not necessarily other node types
        _flags - A list of miscellaneous flags that can be set on the node


raise NotImplementedError<EOL>

Insert node as a child of the current node


def insertText(self, data, insertBefore=None):

raise NotImplementedError<EOL>

Insert data as text in the current node, positioned before the
        start of node insertBefore or to the end of the node's text.


raise NotImplementedError<EOL>

Insert node as a child of the current node, before refNode in the
        list of child nodes. Raises ValueError if refNode is not a child of
        the current node


raise NotImplementedError<EOL>

Remove node from the children of the current node


<EOL>for child in self.childNodes:<EOL><INDENT>newParent.appendChild(child)<EOL><DEDENT>self.childNodes = []<EOL>

Move all the children of the current node to newParent.
        This is needed so that trees that don't store text as nodes move the
        text in the correct way


raise NotImplementedError<EOL>

Return a shallow copy of the current node i.e. a node with the same
        name and attributes but with no parent or child nodes


raise NotImplementedError<EOL>

Return true if the node has children or text, false otherwise


for item in self.activeFormattingElements[::-<NUM_LIT:1>]:<EOL><INDENT>if item == Marker:<EOL><INDENT>break<EOL><DEDENT>elif item.name == name:<EOL><INDENT>return item<EOL><DEDENT><DEDENT>return False<EOL>

Check if an element exists between the end of the active
        formatting elements and the last marker. If it does, return it, else
        return false


name = token["<STR_LIT:name>"]<EOL>namespace = token.get("<STR_LIT>", self.defaultNamespace)<EOL>element = self.elementClass(name, namespace)<EOL>element.attributes = token["<STR_LIT:data>"]<EOL>return element<EOL>

Create an element but don't insert it anywhere


self._insertFromTable = value<EOL>if value:<EOL><INDENT>self.insertElement = self.insertElementTable<EOL><DEDENT>else:<EOL><INDENT>self.insertElement = self.insertElementNormal<EOL><DEDENT>

Switch the function used to insert an element from the
        normal one to the misnested table one and back again


element = self.createElement(token)<EOL>if self.openElements[-<NUM_LIT:1>].name not in tableInsertModeElements:<EOL><INDENT>return self.insertElementNormal(token)<EOL><DEDENT>else:<EOL><INDENT>parent, insertBefore = self.getTableMisnestedNodePosition()<EOL>if insertBefore is None:<EOL><INDENT>parent.appendChild(element)<EOL><DEDENT>else:<EOL><INDENT>parent.insertBefore(element, insertBefore)<EOL><DEDENT>self.openElements.append(element)<EOL><DEDENT>return element<EOL>

Create an element and insert it into the tree


def insertText(self, data, parent=None):

if parent is None:<EOL><INDENT>parent = self.openElements[-<NUM_LIT:1>]<EOL><DEDENT>if (not self.insertFromTable or (self.insertFromTable and<EOL>self.openElements[-<NUM_LIT:1>].name<EOL>not in tableInsertModeElements)):<EOL><INDENT>parent.insertText(data)<EOL><DEDENT>else:<EOL><INDENT>parent, insertBefore = self.getTableMisnestedNodePosition()<EOL>parent.insertText(data, insertBefore)<EOL><DEDENT>


<EOL>lastTable = None<EOL>fosterParent = None<EOL>insertBefore = None<EOL>for elm in self.openElements[::-<NUM_LIT:1>]:<EOL><INDENT>if elm.name == "<STR_LIT>":<EOL><INDENT>lastTable = elm<EOL>break<EOL><DEDENT><DEDENT>if lastTable:<EOL><INDENT>if lastTable.parent:<EOL><INDENT>fosterParent = lastTable.parent<EOL>insertBefore = lastTable<EOL><DEDENT>else:<EOL><INDENT>fosterParent = self.openElements[<EOL>self.openElements.index(lastTable) - <NUM_LIT:1>]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>fosterParent = self.openElements[<NUM_LIT:0>]<EOL><DEDENT>return fosterParent, insertBefore<EOL>

Get the foster parent element, and sibling to insert before
        (or None) when inserting a misnested table node


return self.document<EOL>


<EOL>fragment = self.fragmentClass()<EOL>self.openElements[<NUM_LIT:0>].reparentChildren(fragment)<EOL>return fragment<EOL>


rv = []<EOL>finalText = None<EOL>def serializeElement(element):<EOL><INDENT>if not hasattr(element, "<STR_LIT>"):<EOL><INDENT>if element.docinfo.internalDTD:<EOL><INDENT>if element.docinfo.doctype:<EOL><INDENT>dtd_str = element.docinfo.doctype<EOL><DEDENT>else:<EOL><INDENT>dtd_str = "<STR_LIT>" % element.docinfo.root_name<EOL><DEDENT>rv.append(dtd_str)<EOL><DEDENT>serializeElement(element.getroot())<EOL><DEDENT>elif element.tag == comment_type:<EOL><INDENT>rv.append("<STR_LIT>" % (element.text,))<EOL><DEDENT>else:<EOL><INDENT>if not element.attrib:<EOL><INDENT>rv.append("<STR_LIT>" % (element.tag,))<EOL><DEDENT>else:<EOL><INDENT>attr = "<STR_LIT:U+0020>".join(["<STR_LIT>" % (name, value)<EOL>for name, value in element.attrib.items()])<EOL>rv.append("<STR_LIT>" % (element.tag, attr))<EOL><DEDENT>if element.text:<EOL><INDENT>rv.append(element.text)<EOL><DEDENT>for child in element:<EOL><INDENT>serializeElement(child)<EOL><DEDENT>rv.append("<STR_LIT>" % (element.tag,))<EOL><DEDENT>if hasattr(element, "<STR_LIT>") and element.tail:<EOL><INDENT>rv.append(element.tail)<EOL><DEDENT><DEDENT>serializeElement(element)<EOL>if finalText is not None:<EOL><INDENT>rv.append("<STR_LIT>" % ('<STR_LIT:U+0020>' * <NUM_LIT:2>, finalText))<EOL><DEDENT>return "<STR_LIT>".join(rv)<EOL>

Serialize an element and its child nodes to a string


<EOL>docStr = "<STR_LIT>"<EOL>if self.doctype:<EOL><INDENT>assert self.doctype.name<EOL>docStr += "<STR_LIT>" % self.doctype.name<EOL>if (self.doctype.publicId is not None or<EOL>self.doctype.systemId is not None):<EOL><INDENT>docStr += ('<STR_LIT>' %<EOL>(self.infosetFilter.coercePubid(self.doctype.publicId or "<STR_LIT>")))<EOL>if self.doctype.systemId:<EOL><INDENT>sysid = self.doctype.systemId<EOL>if sysid.find("<STR_LIT:'>") >= <NUM_LIT:0> and sysid.find('<STR_LIT:">') >= <NUM_LIT:0>:<EOL><INDENT>warnings.warn("<STR_LIT>", DataLossWarning)<EOL>sysid = sysid.replace("<STR_LIT:'>", '<STR_LIT>')<EOL><DEDENT>if sysid.find("<STR_LIT:'>") >= <NUM_LIT:0>:<EOL><INDENT>docStr += '<STR_LIT>' % sysid<EOL><DEDENT>else:<EOL><INDENT>docStr += "<STR_LIT>" % sysid<EOL><DEDENT><DEDENT>else:<EOL><INDENT>docStr += "<STR_LIT>"<EOL><DEDENT><DEDENT>docStr += "<STR_LIT:>>"<EOL>if self.doctype.name != token["<STR_LIT:name>"]:<EOL><INDENT>warnings.warn("<STR_LIT>", DataLossWarning)<EOL><DEDENT><DEDENT>docStr += "<STR_LIT>"<EOL>root = etree.fromstring(docStr)<EOL>for comment_token in self.initial_comments:<EOL><INDENT>root.addprevious(etree.Comment(comment_token["<STR_LIT:data>"]))<EOL><DEDENT>self.document = self.documentClass()<EOL>self.document._elementTree = root.getroottree()<EOL>name = token["<STR_LIT:name>"]<EOL>namespace = token.get("<STR_LIT>", self.defaultNamespace)<EOL>if namespace is None:<EOL><INDENT>etree_tag = name<EOL><DEDENT>else:<EOL><INDENT>etree_tag = "<STR_LIT>" % (namespace, name)<EOL><DEDENT>root.tag = etree_tag<EOL>root_element = self.elementClass(name, namespace)<EOL>root_element._element = root<EOL>self.document._childNodes.append(root_element)<EOL>self.openElements.append(root_element)<EOL>self.insertComment = self.insertCommentMain<EOL>


def getTreeBuilder(treeType, implementation=None, **kwargs):

treeType = treeType.lower()<EOL>if treeType not in treeBuilderCache:<EOL><INDENT>if treeType == "<STR_LIT>":<EOL><INDENT>from . import dom<EOL>if implementation is None:<EOL><INDENT>from xml.dom import minidom<EOL>implementation = minidom<EOL><DEDENT>return dom.getDomModule(implementation, **kwargs).TreeBuilder<EOL><DEDENT>elif treeType == "<STR_LIT>":<EOL><INDENT>from . import etree_lxml<EOL>treeBuilderCache[treeType] = etree_lxml.TreeBuilder<EOL><DEDENT>elif treeType == "<STR_LIT>":<EOL><INDENT>from . import etree<EOL>if implementation is None:<EOL><INDENT>implementation = default_etree<EOL><DEDENT>return etree.getETreeModule(implementation, **kwargs).TreeBuilder<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("""<STR_LIT>""" % treeType)<EOL><DEDENT><DEDENT>return treeBuilderCache.get(treeType)<EOL>

Get a TreeBuilder class for various types of tree with built-in support

    treeType - the name of the tree type required (case-insensitive). Supported
               values are:

               "dom" - A generic builder for DOM implementations, defaulting to
                       a xml.dom.minidom based implementation.
               "etree" - A generic builder for tree implementations exposing an
                         ElementTree-like interface, defaulting to
                         xml.etree.cElementTree if available and
                         xml.etree.ElementTree if not.
               "lxml" - A etree-based builder for lxml.etree, handling
                        limitations of lxml's implementation.

    implementation - (Currently applies to the "etree" and "dom" tree types). A
                      module implementing the tree type e.g.
                      xml.etree.ElementTree or xml.etree.cElementTree.


if s is None:<EOL><INDENT>if blank_if_none:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT><DEDENT>elif isinstance(s, text_type):<EOL><INDENT>return s<EOL><DEDENT>else:<EOL><INDENT>return text_type(s)<EOL><DEDENT>

Wrapper around six.text_type to convert None to empty string


return string is None or isinstance(string, string_types)<EOL>

Wrapper around isinstance(string_types) or is None


def getTreeWalker(treeType, implementation=None, **kwargs):

treeType = treeType.lower()<EOL>if treeType not in treeWalkerCache:<EOL><INDENT>if treeType in ("<STR_LIT>", "<STR_LIT>"):<EOL><INDENT>name = "<STR_LIT>" % (__name__, treeType)<EOL>__import__(name)<EOL>mod = sys.modules[name]<EOL>treeWalkerCache[treeType] = mod.TreeWalker<EOL><DEDENT>elif treeType == "<STR_LIT>":<EOL><INDENT>from . import genshistream<EOL>treeWalkerCache[treeType] = genshistream.TreeWalker<EOL><DEDENT>elif treeType == "<STR_LIT>":<EOL><INDENT>from . import lxmletree<EOL>treeWalkerCache[treeType] = lxmletree.TreeWalker<EOL><DEDENT>elif treeType == "<STR_LIT>":<EOL><INDENT>from . import etree<EOL>if implementation is None:<EOL><INDENT>implementation = default_etree<EOL><DEDENT>return etree.getETreeModule(implementation, **kwargs).TreeWalker<EOL><DEDENT><DEDENT>return treeWalkerCache.get(treeType)<EOL>

Get a TreeWalker class for various types of tree with built-in support

    treeType - the name of the tree type required (case-insensitive). Supported
               values are:

                "dom" - The xml.dom.minidom DOM implementation
                "pulldom" - The xml.dom.pulldom event stream
                "etree" - A generic walker for tree implementations exposing an
                          elementtree-like interface (known to work with
                          ElementTree, cElementTree and lxml.etree).
                "lxml" - Optimized walker for lxml.etree
                "genshi" - a Genshi stream

    implementation - (Currently applies to the "etree" tree type only). A module
                      implementing the tree type e.g. xml.etree.ElementTree or
                      cElementTree.


if isinstance(encoding, bytes):<EOL><INDENT>try:<EOL><INDENT>encoding = encoding.decode("<STR_LIT:ascii>")<EOL><DEDENT>except UnicodeDecodeError:<EOL><INDENT>return None<EOL><DEDENT><DEDENT>if encoding:<EOL><INDENT>canonicalName = ascii_punctuation_re.sub("<STR_LIT>", encoding).lower()<EOL>return encodings.get(canonicalName, None)<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

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


<EOL>if len("<STR_LIT>") == <NUM_LIT:1>:<EOL><INDENT>self.reportCharacterErrors = self.characterErrorsUCS4<EOL>self.replaceCharactersRegexp = re.compile("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>self.reportCharacterErrors = self.characterErrorsUCS2<EOL>self.replaceCharactersRegexp = re.compile("<STR_LIT>")<EOL><DEDENT>self.newLines = [<NUM_LIT:0>]<EOL>self.charEncoding = ("<STR_LIT:utf-8>", "<STR_LIT>")<EOL>self.dataStream = self.openStream(source)<EOL>self.reset()<EOL>

Initialises the HTMLInputStream.

        HTMLInputStream(source, [encoding]) -> Normalized stream from source
        for use by html5lib.

        source can be either a file-object, local filename or a string.

        The optional encoding parameter must be a string that indicates
        the encoding.  If specified, that encoding will be used,
        regardless of any BOM or later declaration (such as in a meta
        element)

        parseMeta - Look for a <meta> element containing encoding information


<EOL>if hasattr(source, '<STR_LIT>'):<EOL><INDENT>stream = source<EOL><DEDENT>else:<EOL><INDENT>stream = StringIO(source)<EOL><DEDENT>return stream<EOL>

Produces a file object from source.

        source can be either a file object, local filename or a string.


line, col = self._position(self.chunkOffset)<EOL>return (line + <NUM_LIT:1>, col)<EOL>

Returns (line, col) of the current position in the stream.


<EOL>if self.chunkOffset >= self.chunkSize:<EOL><INDENT>if not self.readChunk():<EOL><INDENT>return EOF<EOL><DEDENT><DEDENT>chunkOffset = self.chunkOffset<EOL>char = self.chunk[chunkOffset]<EOL>self.chunkOffset = chunkOffset + <NUM_LIT:1><EOL>return char<EOL>

Read one character from the stream or queue if available. Return
            EOF when EOF is reached.


def charsUntil(self, characters, opposite=False):

<EOL>try:<EOL><INDENT>chars = charsUntilRegEx[(characters, opposite)]<EOL><DEDENT>except KeyError:<EOL><INDENT>if __debug__:<EOL><INDENT>for c in characters:<EOL><INDENT>assert(ord(c) < <NUM_LIT>)<EOL><DEDENT><DEDENT>regex = "<STR_LIT>".join(["<STR_LIT>" % ord(c) for c in characters])<EOL>if not opposite:<EOL><INDENT>regex = "<STR_LIT>" % regex<EOL><DEDENT>chars = charsUntilRegEx[(characters, opposite)] = re.compile("<STR_LIT>" % regex)<EOL><DEDENT>rv = []<EOL>while True:<EOL><INDENT>m = chars.match(self.chunk, self.chunkOffset)<EOL>if m is None:<EOL><INDENT>if self.chunkOffset != self.chunkSize:<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>end = m.end()<EOL>if end != self.chunkSize:<EOL><INDENT>rv.append(self.chunk[self.chunkOffset:end])<EOL>self.chunkOffset = end<EOL>break<EOL><DEDENT><DEDENT>rv.append(self.chunk[self.chunkOffset:])<EOL>if not self.readChunk():<EOL><INDENT>break<EOL><DEDENT><DEDENT>r = "<STR_LIT>".join(rv)<EOL>return r<EOL>

Returns a string of characters from the stream up to but not
        including any character in 'characters' or EOF. 'characters' must be
        a container that supports the 'in' method and iteration over its
        characters.


def __init__(self, source, encoding=None, parseMeta=True, chardet=True):

<EOL>self.rawStream = self.openStream(source)<EOL>HTMLUnicodeInputStream.__init__(self, self.rawStream)<EOL>self.charEncoding = (codecName(encoding), "<STR_LIT>")<EOL>self.numBytesMeta = <NUM_LIT><EOL>self.numBytesChardet = <NUM_LIT:100><EOL>self.defaultEncoding = "<STR_LIT>"<EOL>if (self.charEncoding[<NUM_LIT:0>] is None):<EOL><INDENT>self.charEncoding = self.detectEncoding(parseMeta, chardet)<EOL><DEDENT>self.reset()<EOL>


<EOL>if hasattr(source, '<STR_LIT>'):<EOL><INDENT>stream = source<EOL><DEDENT>else:<EOL><INDENT>stream = BytesIO(source)<EOL><DEDENT>try:<EOL><INDENT>stream.seek(stream.tell())<EOL><DEDENT>except:<EOL><INDENT>stream = BufferedStream(stream)<EOL><DEDENT>return stream<EOL>


bomDict = {<EOL>codecs.BOM_UTF8: '<STR_LIT:utf-8>',<EOL>codecs.BOM_UTF16_LE: '<STR_LIT>', codecs.BOM_UTF16_BE: '<STR_LIT>',<EOL>codecs.BOM_UTF32_LE: '<STR_LIT>', codecs.BOM_UTF32_BE: '<STR_LIT>'<EOL>}<EOL>string = self.rawStream.read(<NUM_LIT:4>)<EOL>assert isinstance(string, bytes)<EOL>encoding = bomDict.get(string[:<NUM_LIT:3>])         <EOL>seek = <NUM_LIT:3><EOL>if not encoding:<EOL><INDENT>encoding = bomDict.get(string)         <EOL>seek = <NUM_LIT:4><EOL>if not encoding:<EOL><INDENT>encoding = bomDict.get(string[:<NUM_LIT:2>])  <EOL>seek = <NUM_LIT:2><EOL><DEDENT><DEDENT>self.rawStream.seek(encoding and seek or <NUM_LIT:0>)<EOL>return encoding<EOL>

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


buffer = self.rawStream.read(self.numBytesMeta)<EOL>assert isinstance(buffer, bytes)<EOL>parser = EncodingParser(buffer)<EOL>self.rawStream.seek(<NUM_LIT:0>)<EOL>encoding = parser.getEncoding()<EOL>if encoding in ("<STR_LIT>", "<STR_LIT>", "<STR_LIT>"):<EOL><INDENT>encoding = "<STR_LIT:utf-8>"<EOL><DEDENT>return encoding<EOL>

Report the encoding declared by the meta element


p = self.position               <EOL>while p < len(self):<EOL><INDENT>c = self[p:p + <NUM_LIT:1>]<EOL>if c not in chars:<EOL><INDENT>self._position = p<EOL>return c<EOL><DEDENT>p += <NUM_LIT:1><EOL><DEDENT>self._position = p<EOL>return None<EOL>


p = self.position<EOL>data = self[p:p + len(bytes)]<EOL>rv = data.startswith(bytes)<EOL>if rv:<EOL><INDENT>self.position += len(bytes)<EOL><DEDENT>return rv<EOL>

Look for a sequence of bytes at the start of a string. If the bytes
        are found return True and advance the position to the byte after the
        match. Otherwise return False and leave the position alone


newPosition = self[self.position:].find(bytes)<EOL>if newPosition > -<NUM_LIT:1>:<EOL><INDENT>if self._position == -<NUM_LIT:1>:<EOL><INDENT>self._position = <NUM_LIT:0><EOL><DEDENT>self._position += (newPosition + len(bytes) - <NUM_LIT:1>)<EOL>return True<EOL><DEDENT>else:<EOL><INDENT>raise StopIteration<EOL><DEDENT>

Look for the next sequence of bytes matching a given sequence. If
        a match is found advance the position to the last byte of the match


self.data = EncodingBytes(data)<EOL>self.encoding = None<EOL>

string - the data to work on for encoding detection


return self.data.jumpTo(b"<STR_LIT>")<EOL>


data = self.data<EOL>c = data.skip(spaceCharactersBytes | frozenset([b"<STR_LIT:/>"]))<EOL>assert c is None or len(c) == <NUM_LIT:1><EOL>if c in (b"<STR_LIT:>>", None):<EOL><INDENT>return None<EOL><DEDENT>attrName = []<EOL>attrValue = []<EOL>while True:<EOL><INDENT>if c == b"<STR_LIT:=>" and attrName:<EOL><INDENT>break<EOL><DEDENT>elif c in spaceCharactersBytes:<EOL><INDENT>c = data.skip()<EOL>break<EOL><DEDENT>elif c in (b"<STR_LIT:/>", b"<STR_LIT:>>"):<EOL><INDENT>return b"<STR_LIT>".join(attrName), b"<STR_LIT>"<EOL><DEDENT>elif c in asciiUppercaseBytes:<EOL><INDENT>attrName.append(c.lower())<EOL><DEDENT>elif c is None:<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>attrName.append(c)<EOL><DEDENT>c = next(data)<EOL><DEDENT>if c != b"<STR_LIT:=>":<EOL><INDENT>data.previous()<EOL>return b"<STR_LIT>".join(attrName), b"<STR_LIT>"<EOL><DEDENT>next(data)<EOL>c = data.skip()<EOL>if c in (b"<STR_LIT:'>", b'<STR_LIT:">'):<EOL><INDENT>quoteChar = c<EOL>while True:<EOL><INDENT>c = next(data)<EOL>if c == quoteChar:<EOL><INDENT>next(data)<EOL>return b"<STR_LIT>".join(attrName), b"<STR_LIT>".join(attrValue)<EOL><DEDENT>elif c in asciiUppercaseBytes:<EOL><INDENT>attrValue.append(c.lower())<EOL><DEDENT>else:<EOL><INDENT>attrValue.append(c)<EOL><DEDENT><DEDENT><DEDENT>elif c == b"<STR_LIT:>>":<EOL><INDENT>return b"<STR_LIT>".join(attrName), b"<STR_LIT>"<EOL><DEDENT>elif c in asciiUppercaseBytes:<EOL><INDENT>attrValue.append(c.lower())<EOL><DEDENT>elif c is None:<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>attrValue.append(c)<EOL><DEDENT>while True:<EOL><INDENT>c = next(data)<EOL>if c in spacesAngleBrackets:<EOL><INDENT>return b"<STR_LIT>".join(attrName), b"<STR_LIT>".join(attrValue)<EOL><DEDENT>elif c in asciiUppercaseBytes:<EOL><INDENT>attrValue.append(c.lower())<EOL><DEDENT>elif c is None:<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>attrValue.append(c)<EOL><DEDENT><DEDENT>

Return a name,value pair for the next attribute in the stream,
        if one is found, or None


pass<EOL>


if '<STR_LIT>' in kwargs:<EOL><INDENT>self.use_best_quote_char = False<EOL><DEDENT>for attr in self.options:<EOL><INDENT>setattr(self, attr, kwargs.get(attr, getattr(self, attr)))<EOL><DEDENT>self.errors = []<EOL>self.strict = False<EOL>

Initialize HTMLSerializer.

        Keyword options (default given first unless specified) include:

        inject_meta_charset=True|False
          Whether it insert a meta element to define the character set of the
          document.
        quote_attr_values=True|False
          Whether to quote attribute values that don't require quoting
          per HTML5 parsing rules.
        quote_char=u'"'|u"'"
          Use given quote character for attribute quoting. Default is to
          use double quote unless attribute value contains a double quote,
          in which case single quotes are used instead.
        escape_lt_in_attrs=False|True
          Whether to escape < in attribute values.
        escape_rcdata=False|True
          Whether to escape characters that need to be escaped within normal
          elements within rcdata elements such as style.
        resolve_entities=True|False
          Whether to resolve named character entities that appear in the
          source tree. The XML predefined entities < > & " '
          are unaffected by this setting.
        strip_whitespace=False|True
          Whether to remove semantically meaningless whitespace. (This
          compresses all whitespace to a single space except within pre.)
        minimize_boolean_attributes=True|False
          Shortens boolean attributes to give just the attribute value,
          for example <input disabled="disabled"> becomes <input disabled>.
        use_trailing_solidus=False|True
          Includes a close-tag slash at the end of the start tag of void
          elements (empty elements whose end tag is forbidden). E.g.
.
        space_before_trailing_solidus=True|False
          Places a space immediately before the closing slash in a tag
          using a trailing solidus. E.g.
. Requires use_trailing_solidus.
        sanitize=False|True
          Strip all unsafe or unknown constructs from output.
          See `html5lib user documentation`_
        omit_optional_tags=True|False
          Omit start/end tags that are optional.
        alphabetical_attributes=False|True
          Reorder attributes to be in alphabetical order.

        .. _html5lib user documentation: http://code.google.com/p/html5lib/wiki/UserDocumentation


plat = get_build_platform()<EOL>m = macosVersionString.match(plat)<EOL>if m is not None and sys.platform == "<STR_LIT>":<EOL><INDENT>try:<EOL><INDENT>plat = '<STR_LIT>' % ('<STR_LIT:.>'.join(_macosx_vers()[:<NUM_LIT:2>]), m.group(<NUM_LIT:3>))<EOL><DEDENT>except ValueError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>return plat<EOL>

Return this platform's maximum compatible version.

    distutils.util.get_platform() normally reports the minimum version
    of Mac OS X that would be required to *use* extensions produced by
    distutils.  But what we want when checking compatibility is to know the
    version of Mac OS X that we are *running*.  To allow usage of packages that
    explicitly require a newer version of Mac OS X, we must also know the
    current version of the OS.

    If this condition occurs for any other platform with a version in its
    platform strings, this function should be extended accordingly.


_provider_factories[loader_type] = provider_factory<EOL>

Register `provider_factory` to make providers for `loader_type`

    `loader_type` is the type or class of a PEP 302 ``module.__loader__``,
    and `provider_factory` is a function that, passed a *module* object,
    returns an ``IResourceProvider`` for that module.


if isinstance(moduleOrReq, Requirement):<EOL><INDENT>return working_set.find(moduleOrReq) or require(str(moduleOrReq))[<NUM_LIT:0>]<EOL><DEDENT>try:<EOL><INDENT>module = sys.modules[moduleOrReq]<EOL><DEDENT>except KeyError:<EOL><INDENT>__import__(moduleOrReq)<EOL>module = sys.modules[moduleOrReq]<EOL><DEDENT>loader = getattr(module, '<STR_LIT>', None)<EOL>return _find_adapter(_provider_factories, loader)(module)<EOL>

Return an IResourceProvider for the named module or requirement


try:<EOL><INDENT>from sysconfig import get_platform<EOL><DEDENT>except ImportError:<EOL><INDENT>from distutils.util import get_platform<EOL><DEDENT>plat = get_platform()<EOL>if sys.platform == "<STR_LIT>" and not plat.startswith('<STR_LIT>'):<EOL><INDENT>try:<EOL><INDENT>version = _macosx_vers()<EOL>machine = os.uname()[<NUM_LIT:4>].replace("<STR_LIT:U+0020>", "<STR_LIT:_>")<EOL>return "<STR_LIT>" % (int(version[<NUM_LIT:0>]), int(version[<NUM_LIT:1>]),<EOL>_macosx_arch(machine))<EOL><DEDENT>except ValueError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>return plat<EOL>

Return this platform's string for platform-specific distributions

    XXX Currently this is the same as ``distutils.util.get_platform()``, but it
    needs some hacks for Linux and Mac OS X.


if provided is None or required is None or provided==required:<EOL><INDENT>return True<EOL><DEDENT>reqMac = macosVersionString.match(required)<EOL>if reqMac:<EOL><INDENT>provMac = macosVersionString.match(provided)<EOL>if not provMac:<EOL><INDENT>provDarwin = darwinVersionString.match(provided)<EOL>if provDarwin:<EOL><INDENT>dversion = int(provDarwin.group(<NUM_LIT:1>))<EOL>macosversion = "<STR_LIT>" % (reqMac.group(<NUM_LIT:1>), reqMac.group(<NUM_LIT:2>))<EOL>if dversion == <NUM_LIT:7> and macosversion >= "<STR_LIT>" ordversion == <NUM_LIT:8> and macosversion >= "<STR_LIT>":<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL><DEDENT>if provMac.group(<NUM_LIT:1>) != reqMac.group(<NUM_LIT:1>) orprovMac.group(<NUM_LIT:3>) != reqMac.group(<NUM_LIT:3>):<EOL><INDENT>return False<EOL><DEDENT>if int(provMac.group(<NUM_LIT:2>)) > int(reqMac.group(<NUM_LIT:2>)):<EOL><INDENT>return False<EOL><DEDENT>return True<EOL><DEDENT>return False<EOL>

Can code for the `provided` platform run on the `required` platform?

    Returns true if either platform is ``None``, or the platforms are equal.

    XXX Needs compatibility checks for Linux and other unixy OSes.


ns = sys._getframe(<NUM_LIT:1>).f_globals<EOL>name = ns['<STR_LIT>']<EOL>ns.clear()<EOL>ns['<STR_LIT>'] = name<EOL>require(dist_spec)[<NUM_LIT:0>].run_script(script_name, ns)<EOL>

Locate distribution `dist_spec` and run its `script_name` script


if isinstance(dist, string_types):<EOL><INDENT>dist = Requirement.parse(dist)<EOL><DEDENT>if isinstance(dist, Requirement):<EOL><INDENT>dist = get_provider(dist)<EOL><DEDENT>if not isinstance(dist, Distribution):<EOL><INDENT>raise TypeError("<STR_LIT>", dist)<EOL><DEDENT>return dist<EOL>

Return a current distribution object for a Requirement or string


def load_entry_point(dist, group, name):

return get_distribution(dist).load_entry_point(group, name)<EOL>

Return `name` entry point of `group` for `dist` or raise ImportError


return get_distribution(dist).get_entry_map(group)<EOL>

Return the entry point map for `group`, or the full entry map


return get_distribution(dist).get_entry_info(group, name)<EOL>

Return the EntryPoint object for `group`+`name`, or ``None``


try:<EOL><INDENT>return os.environ['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>if os.name!='<STR_LIT>':<EOL><INDENT>return os.path.expanduser('<STR_LIT>')<EOL><DEDENT>app_data = '<STR_LIT>'<EOL>app_homes = [<EOL>(('<STR_LIT>',), None),<EOL>(('<STR_LIT>',), app_data),<EOL>(('<STR_LIT>','<STR_LIT>'), app_data),<EOL>(('<STR_LIT>',), app_data),<EOL>(('<STR_LIT>',), None),<EOL>(('<STR_LIT>',), app_data),<EOL>]<EOL>for keys, subdir in app_homes:<EOL><INDENT>dirname = '<STR_LIT>'<EOL>for key in keys:<EOL><INDENT>if key in os.environ:<EOL><INDENT>dirname = os.path.join(dirname, os.environ[key])<EOL><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if subdir:<EOL><INDENT>dirname = os.path.join(dirname, subdir)<EOL><DEDENT>return os.path.join(dirname, '<STR_LIT>')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise RuntimeError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>

Determine the default cache location

    This returns the ``PYTHON_EGG_CACHE`` environment variable, if set.
    Otherwise, on Windows, it returns a "Python-Eggs" subdirectory of the
    "Application Data" directory.  On all other systems, it's "~/.python-eggs".


return re.sub('<STR_LIT>', '<STR_LIT:->', name)<EOL>

Convert an arbitrary string to a standard distribution name

    Any runs of non-alphanumeric/. characters are replaced with a single '-'.


try:<EOL><INDENT>return str(packaging.version.Version(version))<EOL><DEDENT>except packaging.version.InvalidVersion:<EOL><INDENT>version = version.replace('<STR_LIT:U+0020>','<STR_LIT:.>')<EOL>return re.sub('<STR_LIT>', '<STR_LIT:->', version)<EOL><DEDENT>

Convert an arbitrary string to a standard version string


return re.sub('<STR_LIT>', '<STR_LIT:_>', extra).lower()<EOL>

Convert an arbitrary string to a standard 'extra' name

    Any runs of non-alphanumeric characters are replaced with a single '_',
    and the result is always lowercased.


return name.replace('<STR_LIT:->','<STR_LIT:_>')<EOL>

Convert a project or version name to its filename-escaped form

    Any '-' characters are currently replaced with '_'.


_distribution_finders[importer_type] = distribution_finder<EOL>

Register `distribution_finder` to find distributions in sys.path items

    `importer_type` is the type or class of a PEP 302 "Importer" (sys.path item
    handler), and `distribution_finder` is a callable that, passed a path
    item and the importer instance, yields ``Distribution`` instances found on
    that path item.  See ``pkg_resources.find_on_path`` for an example.


importer = get_importer(path_item)<EOL>finder = _find_adapter(_distribution_finders, importer)<EOL>return finder(importer, path_item, only)<EOL>

Yield distributions accessible via `path_item`


def find_eggs_in_zip(importer, path_item, only=False):

if importer.archive.endswith('<STR_LIT>'):<EOL><INDENT>return<EOL><DEDENT>metadata = EggMetadata(importer)<EOL>if metadata.has_metadata('<STR_LIT>'):<EOL><INDENT>yield Distribution.from_filename(path_item, metadata=metadata)<EOL><DEDENT>if only:<EOL><INDENT>return<EOL><DEDENT>for subitem in metadata.resource_listdir('<STR_LIT:/>'):<EOL><INDENT>if subitem.endswith('<STR_LIT>'):<EOL><INDENT>subpath = os.path.join(path_item, subitem)<EOL>for dist in find_eggs_in_zip(zipimport.zipimporter(subpath), subpath):<EOL><INDENT>yield dist<EOL><DEDENT><DEDENT><DEDENT>

Find eggs in zip files; possibly multiple nested eggs.


def find_on_path(importer, path_item, only=False):

path_item = _normalize_cached(path_item)<EOL>if os.path.isdir(path_item) and os.access(path_item, os.R_OK):<EOL><INDENT>if path_item.lower().endswith('<STR_LIT>'):<EOL><INDENT>yield Distribution.from_filename(<EOL>path_item, metadata=PathMetadata(<EOL>path_item, os.path.join(path_item,'<STR_LIT>')<EOL>)<EOL>)<EOL><DEDENT>else:<EOL><INDENT>for entry in os.listdir(path_item):<EOL><INDENT>lower = entry.lower()<EOL>if lower.endswith('<STR_LIT>') or lower.endswith('<STR_LIT>'):<EOL><INDENT>fullpath = os.path.join(path_item, entry)<EOL>if os.path.isdir(fullpath):<EOL><INDENT>metadata = PathMetadata(path_item, fullpath)<EOL><DEDENT>else:<EOL><INDENT>metadata = FileMetadata(fullpath)<EOL><DEDENT>yield Distribution.from_location(<EOL>path_item, entry, metadata, precedence=DEVELOP_DIST<EOL>)<EOL><DEDENT>elif not only and lower.endswith('<STR_LIT>'):<EOL><INDENT>dists = find_distributions(os.path.join(path_item, entry))<EOL>for dist in dists:<EOL><INDENT>yield dist<EOL><DEDENT><DEDENT>elif not only and lower.endswith('<STR_LIT>'):<EOL><INDENT>with open(os.path.join(path_item, entry)) as entry_file:<EOL><INDENT>entry_lines = entry_file.readlines()<EOL><DEDENT>for line in entry_lines:<EOL><INDENT>if not line.strip():<EOL><INDENT>continue<EOL><DEDENT>path = os.path.join(path_item, line.rstrip())<EOL>dists = find_distributions(path)<EOL>for item in dists:<EOL><INDENT>yield item<EOL><DEDENT>break<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>

Yield distributions accessible on a sys.path directory


_namespace_handlers[importer_type] = namespace_handler<EOL>

Register `namespace_handler` to declare namespace packages

    `importer_type` is the type or class of a PEP 302 "Importer" (sys.path item
    handler), and `namespace_handler` is a callable like this::

        def namespace_handler(importer, path_entry, moduleName, module):
            # return a path_entry to use for child packages

    Namespace handlers are only called if the importer object has already
    agreed that it can handle the relevant path item, and they should only
    return a subpath if the module __path__ does not already contain an
    equivalent subpath.  For an example namespace handler, see
    ``pkg_resources.file_ns_handler``.


importer = get_importer(path_item)<EOL>if importer is None:<EOL><INDENT>return None<EOL><DEDENT>loader = importer.find_module(packageName)<EOL>if loader is None:<EOL><INDENT>return None<EOL><DEDENT>module = sys.modules.get(packageName)<EOL>if module is None:<EOL><INDENT>module = sys.modules[packageName] = imp.new_module(packageName)<EOL>module.__path__ = []<EOL>_set_parent_ns(packageName)<EOL><DEDENT>elif not hasattr(module,'<STR_LIT>'):<EOL><INDENT>raise TypeError("<STR_LIT>", packageName)<EOL><DEDENT>handler = _find_adapter(_namespace_handlers, importer)<EOL>subpath = handler(importer, path_item, packageName, module)<EOL>if subpath is not None:<EOL><INDENT>path = module.__path__<EOL>path.append(subpath)<EOL>loader.load_module(packageName)<EOL>for path_item in path:<EOL><INDENT>if path_item not in module.__path__:<EOL><INDENT>module.__path__.append(path_item)<EOL><DEDENT><DEDENT><DEDENT>return subpath<EOL>

Ensure that named package includes a subpath of path_item (if needed)


imp.acquire_lock()<EOL>try:<EOL><INDENT>if packageName in _namespace_packages:<EOL><INDENT>return<EOL><DEDENT>path, parent = sys.path, None<EOL>if '<STR_LIT:.>' in packageName:<EOL><INDENT>parent = '<STR_LIT:.>'.join(packageName.split('<STR_LIT:.>')[:-<NUM_LIT:1>])<EOL>declare_namespace(parent)<EOL>if parent not in _namespace_packages:<EOL><INDENT>__import__(parent)<EOL><DEDENT>try:<EOL><INDENT>path = sys.modules[parent].__path__<EOL><DEDENT>except AttributeError:<EOL><INDENT>raise TypeError("<STR_LIT>", parent)<EOL><DEDENT><DEDENT>_namespace_packages.setdefault(parent,[]).append(packageName)<EOL>_namespace_packages.setdefault(packageName,[])<EOL>for path_item in path:<EOL><INDENT>_handle_ns(packageName, path_item)<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>imp.release_lock()<EOL><DEDENT>

Declare that package 'packageName' is a namespace package


imp.acquire_lock()<EOL>try:<EOL><INDENT>for package in _namespace_packages.get(parent,()):<EOL><INDENT>subpath = _handle_ns(package, path_item)<EOL>if subpath:<EOL><INDENT>fixup_namespace_packages(subpath, package)<EOL><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>imp.release_lock()<EOL><DEDENT>

Ensure that previously-declared namespace packages include path_item


def file_ns_handler(importer, path_item, packageName, module):

subpath = os.path.join(path_item, packageName.split('<STR_LIT:.>')[-<NUM_LIT:1>])<EOL>normalized = _normalize_cached(subpath)<EOL>for item in module.__path__:<EOL><INDENT>if _normalize_cached(item)==normalized:<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return subpath<EOL><DEDENT>

Compute an ns-package subpath for a filesystem or zipfile importer


return os.path.normcase(os.path.realpath(filename))<EOL>

Normalize a file/dir name for comparison purposes


if isinstance(strs, string_types):<EOL><INDENT>for s in strs.splitlines():<EOL><INDENT>s = s.strip()<EOL>if s and not s.startswith('<STR_LIT:#>'):<EOL><INDENT>yield s<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>for ss in strs:<EOL><INDENT>for s in yield_lines(ss):<EOL><INDENT>yield s<EOL><DEDENT><DEDENT><DEDENT>

Yield non-empty/non-comment lines of a string or sequence


<EOL>lines = iter(yield_lines(strs))<EOL>def scan_list(ITEM, TERMINATOR, line, p, groups, item_name):<EOL><INDENT>items = []<EOL>while not TERMINATOR(line, p):<EOL><INDENT>if CONTINUE(line, p):<EOL><INDENT>try:<EOL><INDENT>line = next(lines)<EOL>p = <NUM_LIT:0><EOL><DEDENT>except StopIteration:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT><DEDENT>match = ITEM(line, p)<EOL>if not match:<EOL><INDENT>msg = "<STR_LIT>" + item_name + "<STR_LIT>"<EOL>raise ValueError(msg, line, "<STR_LIT>", line[p:])<EOL><DEDENT>items.append(match.group(*groups))<EOL>p = match.end()<EOL>match = COMMA(line, p)<EOL>if match:<EOL><INDENT>p = match.end()<EOL><DEDENT>elif not TERMINATOR(line, p):<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg, line, "<STR_LIT>", line[p:])<EOL><DEDENT><DEDENT>match = TERMINATOR(line, p)<EOL>if match:<EOL><INDENT>p = match.end()<EOL><DEDENT>return line, p, items<EOL><DEDENT>for line in lines:<EOL><INDENT>match = DISTRO(line)<EOL>if not match:<EOL><INDENT>raise ValueError("<STR_LIT>", line)<EOL><DEDENT>project_name = match.group(<NUM_LIT:1>)<EOL>p = match.end()<EOL>extras = []<EOL>match = OBRACKET(line, p)<EOL>if match:<EOL><INDENT>p = match.end()<EOL>line, p, extras = scan_list(<EOL>DISTRO, CBRACKET, line, p, (<NUM_LIT:1>,), "<STR_LIT>"<EOL>)<EOL><DEDENT>line, p, specs = scan_list(VERSION, LINE_END, line, p, (<NUM_LIT:1>, <NUM_LIT:2>),<EOL>"<STR_LIT>")<EOL>specs = [(op, val) for op, val in specs]<EOL>yield Requirement(project_name, specs, extras)<EOL><DEDENT>

Yield ``Requirement`` objects for each specification in `strs`

    `strs` must be a string, or a (possibly-nested) iterable thereof.


if not isinstance(cls, type):<EOL><INDENT>class cls(cls, object): pass<EOL>return cls.__mro__[<NUM_LIT:1>:]<EOL><DEDENT>return cls.__mro__<EOL>


for t in _get_mro(getattr(ob, '<STR_LIT>', type(ob))):<EOL><INDENT>if t in registry:<EOL><INDENT>return registry[t]<EOL><DEDENT><DEDENT>

Return an adapter factory for `ob` from `registry`


dirname = os.path.dirname(path)<EOL>if not os.path.isdir(dirname):<EOL><INDENT>os.makedirs(dirname)<EOL><DEDENT>

Ensure that the parent directory of `path` exists


if not WRITE_SUPPORT:<EOL><INDENT>raise IOError('<STR_LIT>')<EOL><DEDENT>dirname, filename = split(path)<EOL>if dirname and filename and not isdir(dirname):<EOL><INDENT>_bypass_ensure_directory(dirname)<EOL>mkdir(dirname, mode)<EOL><DEDENT>

Sandbox-bypassing version of ensure_directory()


section = None<EOL>content = []<EOL>for line in yield_lines(s):<EOL><INDENT>if line.startswith("<STR_LIT:[>"):<EOL><INDENT>if line.endswith("<STR_LIT:]>"):<EOL><INDENT>if section or content:<EOL><INDENT>yield section, content<EOL><DEDENT>section = line[<NUM_LIT:1>:-<NUM_LIT:1>].strip()<EOL>content = []<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>", line)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>content.append(line)<EOL><DEDENT><DEDENT>yield section, content<EOL>

Split a string or iterable thereof into (section, content) pairs

    Each ``section`` is a stripped version of the section header ("[section]")
    and each ``content`` is a list of stripped lines excluding blank lines and
    comment-only lines.  If there are any such lines before the first section
    header, they're returned in a first ``section`` of ``None``.


Does the package's distribution contain the named metadata?


Yield named metadata resource as list of non-blank non-comment lines

       Leading and trailing whitespace is stripped from each line, and lines
       with ``#`` as the first non-blank character are omitted.


Is the named metadata a directory?  (like ``os.path.isdir()``)


List of metadata names in the directory (like ``os.listdir()``)


Execute the named script in the supplied namespace dictionary


Return a true filesystem path for `resource_name`

        `manager` must be an ``IResourceManager``


Return a readable file-like object for `resource_name`

        `manager` must be an ``IResourceManager``


Return a string containing the contents of `resource_name`

        `manager` must be an ``IResourceManager``


Does the package contain the named resource?


Is the named resource a directory?  (like ``os.path.isdir()``)


List of resource names in the directory (like ``os.listdir()``)


self.entries = []<EOL>self.entry_keys = {}<EOL>self.by_key = {}<EOL>self.callbacks = []<EOL>if entries is None:<EOL><INDENT>entries = sys.path<EOL><DEDENT>for entry in entries:<EOL><INDENT>self.add_entry(entry)<EOL><DEDENT>

Create working set from list of path entries (default=sys.path)


ws = cls()<EOL>try:<EOL><INDENT>from __main__ import __requires__<EOL><DEDENT>except ImportError:<EOL><INDENT>return ws<EOL><DEDENT>try:<EOL><INDENT>ws.require(__requires__)<EOL><DEDENT>except VersionConflict:<EOL><INDENT>return cls._build_from_requirements(__requires__)<EOL><DEDENT>return ws<EOL>


<EOL>ws = cls([])<EOL>reqs = parse_requirements(req_spec)<EOL>dists = ws.resolve(reqs, Environment())<EOL>for dist in dists:<EOL><INDENT>ws.add(dist)<EOL><DEDENT>for entry in sys.path:<EOL><INDENT>if entry not in ws.entries:<EOL><INDENT>ws.add_entry(entry)<EOL><DEDENT><DEDENT>sys.path[:] = ws.entries<EOL>return ws<EOL>

Build a working set from a requirement spec. Rewrites sys.path.


self.entry_keys.setdefault(entry, [])<EOL>self.entries.append(entry)<EOL>for dist in find_distributions(entry, True):<EOL><INDENT>self.add(dist, entry, False)<EOL><DEDENT>

Add a path item to ``.entries``, finding any distributions on it

        ``find_distributions(entry, True)`` is used to find distributions
        corresponding to the path entry, and they are added.  `entry` is
        always appended to ``.entries``, even if it is already present.
        (This is because ``sys.path`` can contain the same value more than
        once, and the ``.entries`` of the ``sys.path`` WorkingSet should always
        equal ``sys.path``.)


return self.by_key.get(dist.key) == dist<EOL>

True if `dist` is the active distribution for its project


dist = self.by_key.get(req.key)<EOL>if dist is not None and dist not in req:<EOL><INDENT>raise VersionConflict(dist, req)<EOL><DEDENT>else:<EOL><INDENT>return dist<EOL><DEDENT>

Find a distribution matching requirement `req`

        If there is an active distribution for the requested project, this
        returns it as long as it meets the version requirement specified by
        `req`.  But, if there is an active distribution for the project and it
        does *not* meet the `req` requirement, ``VersionConflict`` is raised.
        If there is no active distribution for the requested project, ``None``
        is returned.


def iter_entry_points(self, group, name=None):

for dist in self:<EOL><INDENT>entries = dist.get_entry_map(group)<EOL>if name is None:<EOL><INDENT>for ep in entries.values():<EOL><INDENT>yield ep<EOL><DEDENT><DEDENT>elif name in entries:<EOL><INDENT>yield entries[name]<EOL><DEDENT><DEDENT>

Yield entry point objects from `group` matching `name`

        If `name` is None, yields all entry points in `group` from all
        distributions in the working set, otherwise only ones matching
        both `group` and `name` are yielded (in distribution order).


def run_script(self, requires, script_name):

ns = sys._getframe(<NUM_LIT:1>).f_globals<EOL>name = ns['<STR_LIT>']<EOL>ns.clear()<EOL>ns['<STR_LIT>'] = name<EOL>self.require(requires)[<NUM_LIT:0>].run_script(script_name, ns)<EOL>

Locate distribution for `requires` and run `script_name` script


seen = {}<EOL>for item in self.entries:<EOL><INDENT>if item not in self.entry_keys:<EOL><INDENT>continue<EOL><DEDENT>for key in self.entry_keys[item]:<EOL><INDENT>if key not in seen:<EOL><INDENT>seen[key]=<NUM_LIT:1><EOL>yield self.by_key[key]<EOL><DEDENT><DEDENT><DEDENT>

Yield distributions for non-duplicate projects in the working set

        The yield order is the order in which the items' path entries were
        added to the working set.


def add(self, dist, entry=None, insert=True, replace=False):

if insert:<EOL><INDENT>dist.insert_on(self.entries, entry)<EOL><DEDENT>if entry is None:<EOL><INDENT>entry = dist.location<EOL><DEDENT>keys = self.entry_keys.setdefault(entry,[])<EOL>keys2 = self.entry_keys.setdefault(dist.location,[])<EOL>if not replace and dist.key in self.by_key:<EOL><INDENT>return<EOL><DEDENT>self.by_key[dist.key] = dist<EOL>if dist.key not in keys:<EOL><INDENT>keys.append(dist.key)<EOL><DEDENT>if dist.key not in keys2:<EOL><INDENT>keys2.append(dist.key)<EOL><DEDENT>self._added_new(dist)<EOL>

Add `dist` to working set, associated with `entry`

        If `entry` is unspecified, it defaults to the ``.location`` of `dist`.
        On exit from this routine, `entry` is added to the end of the working
        set's ``.entries`` (if it wasn't already present).

        `dist` is only added to the working set if it's for a project that
        doesn't already have a distribution in the set, unless `replace=True`.
        If it's added, any callbacks registered with the ``subscribe()`` method
        will be called.


def resolve(self, requirements, env=None, installer=None,<EOL>replace_conflicting=False):

<EOL>requirements = list(requirements)[::-<NUM_LIT:1>]<EOL>processed = {}<EOL>best = {}<EOL>to_activate = []<EOL>required_by = collections.defaultdict(set)<EOL>while requirements:<EOL><INDENT>req = requirements.pop(<NUM_LIT:0>)<EOL>if req in processed:<EOL><INDENT>continue<EOL><DEDENT>dist = best.get(req.key)<EOL>if dist is None:<EOL><INDENT>dist = self.by_key.get(req.key)<EOL>if dist is None or (dist not in req and replace_conflicting):<EOL><INDENT>ws = self<EOL>if env is None:<EOL><INDENT>if dist is None:<EOL><INDENT>env = Environment(self.entries)<EOL><DEDENT>else:<EOL><INDENT>env = Environment([])<EOL>ws = WorkingSet([])<EOL><DEDENT><DEDENT>dist = best[req.key] = env.best_match(req, ws, installer)<EOL>if dist is None:<EOL><INDENT>raise DistributionNotFound(req)<EOL><DEDENT><DEDENT>to_activate.append(dist)<EOL><DEDENT>if dist not in req:<EOL><INDENT>tmpl = "<STR_LIT>"<EOL>args = dist, req, list(required_by.get(req, []))<EOL>raise VersionConflict(tmpl % args)<EOL><DEDENT>new_requirements = dist.requires(req.extras)[::-<NUM_LIT:1>]<EOL>requirements.extend(new_requirements)<EOL>for new_requirement in new_requirements:<EOL><INDENT>required_by[new_requirement].add(req.project_name)<EOL><DEDENT>processed[req] = True<EOL><DEDENT>return to_activate<EOL>

List all distributions needed to (recursively) meet `requirements`

        `requirements` must be a sequence of ``Requirement`` objects.  `env`,
        if supplied, should be an ``Environment`` instance.  If
        not supplied, it defaults to all distributions available within any
        entry or distribution in the working set.  `installer`, if supplied,
        will be invoked with each requirement that cannot be met by an
        already-installed distribution; it should return a ``Distribution`` or
        ``None``.

        Unless `replace_conflicting=True`, raises a VersionConflict exception if
        any requirements are found on the path that have the correct name but
        the wrong version.  Otherwise, if an `installer` is supplied it will be
        invoked to obtain the correct version of the requirement and activate
        it.


def find_plugins(self, plugin_env, full_env=None, installer=None,<EOL>fallback=True):

plugin_projects = list(plugin_env)<EOL>plugin_projects.sort()<EOL>error_info = {}<EOL>distributions = {}<EOL>if full_env is None:<EOL><INDENT>env = Environment(self.entries)<EOL>env += plugin_env<EOL><DEDENT>else:<EOL><INDENT>env = full_env + plugin_env<EOL><DEDENT>shadow_set = self.__class__([])<EOL>list(map(shadow_set.add, self))<EOL>for project_name in plugin_projects:<EOL><INDENT>for dist in plugin_env[project_name]:<EOL><INDENT>req = [dist.as_requirement()]<EOL>try:<EOL><INDENT>resolvees = shadow_set.resolve(req, env, installer)<EOL><DEDENT>except ResolutionError:<EOL><INDENT>v = sys.exc_info()[<NUM_LIT:1>]<EOL>error_info[dist] = v<EOL>if fallback:<EOL><INDENT>continue<EOL><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>list(map(shadow_set.add, resolvees))<EOL>distributions.update(dict.fromkeys(resolvees))<EOL>break<EOL><DEDENT><DEDENT><DEDENT>distributions = list(distributions)<EOL>distributions.sort()<EOL>return distributions, error_info<EOL>

Find all activatable distributions in `plugin_env`

        Example usage::

            distributions, errors = working_set.find_plugins(
                Environment(plugin_dirlist)
            )
            # add plugins+libs to sys.path
            map(working_set.add, distributions)
            # display errors
            print('Could not load', errors)

        The `plugin_env` should be an ``Environment`` instance that contains
        only distributions that are in the project's "plugin directory" or
        directories. The `full_env`, if supplied, should be an ``Environment``
        contains all currently-available distributions.  If `full_env` is not
        supplied, one is created automatically from the ``WorkingSet`` this
        method is called on, which will typically mean that every directory on
        ``sys.path`` will be scanned for distributions.

        `installer` is a standard installer callback as used by the
        ``resolve()`` method. The `fallback` flag indicates whether we should
        attempt to resolve older versions of a plugin if the newest version
        cannot be resolved.

        This method returns a 2-tuple: (`distributions`, `error_info`), where
        `distributions` is a list of the distributions found in `plugin_env`
        that were loadable, along with any other distributions that are needed
        to resolve their dependencies.  `error_info` is a dictionary mapping
        unloadable plugin distributions to an exception instance describing the
        error that occurred. Usually this will be a ``DistributionNotFound`` or
        ``VersionConflict`` instance.


needed = self.resolve(parse_requirements(requirements))<EOL>for dist in needed:<EOL><INDENT>self.add(dist)<EOL><DEDENT>return needed<EOL>

Ensure that distributions matching `requirements` are activated

        `requirements` must be a string or a (possibly-nested) sequence
        thereof, specifying the distributions and versions required.  The
        return value is a sequence of the distributions that needed to be
        activated to fulfill the requirements; all relevant distributions are
        included, even if they were already activated in this working set.


if callback in self.callbacks:<EOL><INDENT>return<EOL><DEDENT>self.callbacks.append(callback)<EOL>for dist in self:<EOL><INDENT>callback(dist)<EOL><DEDENT>

Invoke `callback` for all distributions (including existing ones)


def __init__(self, search_path=None, platform=get_supported_platform(),<EOL>python=PY_MAJOR):

self._distmap = {}<EOL>self.platform = platform<EOL>self.python = python<EOL>self.scan(search_path)<EOL>

Snapshot distributions available on a search path

        Any distributions found on `search_path` are added to the environment.
        `search_path` should be a sequence of ``sys.path`` items.  If not
        supplied, ``sys.path`` is used.

        `platform` is an optional string specifying the name of the platform
        that platform-specific distributions must be compatible with.  If
        unspecified, it defaults to the current platform.  `python` is an
        optional string naming the desired version of Python (e.g. ``'3.3'``);
        it defaults to the current version.

        You may explicitly set `platform` (and/or `python`) to ``None`` if you
        wish to map *all* distributions, not just those compatible with the
        running platform or Python version.


return (self.python is None or dist.py_version is None<EOL>or dist.py_version==self.python)and compatible_platforms(dist.platform, self.platform)<EOL>

Is distribution `dist` acceptable for this environment?

        The distribution must match the platform and python version
        requirements specified when this environment was created, or False
        is returned.


self._distmap[dist.key].remove(dist)<EOL>


if search_path is None:<EOL><INDENT>search_path = sys.path<EOL><DEDENT>for item in search_path:<EOL><INDENT>for dist in find_distributions(item):<EOL><INDENT>self.add(dist)<EOL><DEDENT><DEDENT>

Scan `search_path` for distributions usable in this environment

        Any distributions found are added to the environment.
        `search_path` should be a sequence of ``sys.path`` items.  If not
        supplied, ``sys.path`` is used.  Only distributions conforming to
        the platform/python version defined at initialization are added.


distribution_key = project_name.lower()<EOL>return self._distmap.get(distribution_key, [])<EOL>

Return a newest-to-oldest list of distributions for `project_name`

        Uses case-insensitive `project_name` comparison, assuming all the
        project's distributions use their project's name converted to all
        lowercase as their key.


if self.can_add(dist) and dist.has_version():<EOL><INDENT>dists = self._distmap.setdefault(dist.key, [])<EOL>if dist not in dists:<EOL><INDENT>dists.append(dist)<EOL>dists.sort(key=operator.attrgetter('<STR_LIT>'), reverse=True)<EOL><DEDENT><DEDENT>

Add `dist` if we ``can_add()`` it and it has not already been added


def best_match(self, req, working_set, installer=None):

dist = working_set.find(req)<EOL>if dist is not None:<EOL><INDENT>return dist<EOL><DEDENT>for dist in self[req.key]:<EOL><INDENT>if dist in req:<EOL><INDENT>return dist<EOL><DEDENT><DEDENT>return self.obtain(req, installer)<EOL>

Find distribution best matching `req` and usable on `working_set`

        This calls the ``find(req)`` method of the `working_set` to see if a
        suitable distribution is already active.  (This may raise
        ``VersionConflict`` if an unsuitable version of the project is already
        active in the specified `working_set`.)  If a suitable distribution
        isn't active, this method returns the newest distribution in the
        environment that meets the ``Requirement`` in `req`.  If no suitable
        distribution is found, and `installer` is supplied, then the result of
        calling the environment's ``obtain(req, installer)`` method will be
        returned.


def obtain(self, requirement, installer=None):

if installer is not None:<EOL><INDENT>return installer(requirement)<EOL><DEDENT>

Obtain a distribution matching `requirement` (e.g. via download)

        Obtain a distro that matches requirement (e.g. via download).  In the
        base ``Environment`` class, this routine just returns
        ``installer(requirement)``, unless `installer` is None, in which case
        None is returned instead.  This method is a hook that allows subclasses
        to attempt other ways of obtaining a distribution before falling back
        to the `installer` argument.


for key in self._distmap.keys():<EOL><INDENT>if self[key]:<EOL><INDENT>yield key<EOL><DEDENT><DEDENT>

Yield the unique project names of the available distributions


if isinstance(other, Distribution):<EOL><INDENT>self.add(other)<EOL><DEDENT>elif isinstance(other, Environment):<EOL><INDENT>for project in other:<EOL><INDENT>for dist in other[project]:<EOL><INDENT>self.add(dist)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>raise TypeError("<STR_LIT>" % (other,))<EOL><DEDENT>return self<EOL>

In-place addition of a distribution or environment


new = self.__class__([], platform=None, python=None)<EOL>for env in self, other:<EOL><INDENT>new += env<EOL><DEDENT>return new<EOL>

Add an environment or distribution to an environment


def resource_exists(self, package_or_requirement, resource_name):

return get_provider(package_or_requirement).has_resource(resource_name)<EOL>


def resource_isdir(self, package_or_requirement, resource_name):

return get_provider(package_or_requirement).resource_isdir(<EOL>resource_name<EOL>)<EOL>

Is the named resource an existing directory?


def resource_filename(self, package_or_requirement, resource_name):

return get_provider(package_or_requirement).get_resource_filename(<EOL>self, resource_name<EOL>)<EOL>

Return a true filesystem path for specified resource


def resource_stream(self, package_or_requirement, resource_name):

return get_provider(package_or_requirement).get_resource_stream(<EOL>self, resource_name<EOL>)<EOL>

Return a readable file-like object for specified resource


def resource_string(self, package_or_requirement, resource_name):

return get_provider(package_or_requirement).get_resource_string(<EOL>self, resource_name<EOL>)<EOL>


def resource_listdir(self, package_or_requirement, resource_name):

return get_provider(package_or_requirement).resource_listdir(<EOL>resource_name<EOL>)<EOL>

List the contents of the named resource directory


old_exc = sys.exc_info()[<NUM_LIT:1>]<EOL>cache_path = self.extraction_path or get_default_cache()<EOL>err = ExtractionError(

Give an error message for problems extracting file(s)


def get_cache_path(self, archive_name, names=()):

extract_path = self.extraction_path or get_default_cache()<EOL>target_path = os.path.join(extract_path, archive_name+'<STR_LIT>', *names)<EOL>try:<EOL><INDENT>_bypass_ensure_directory(target_path)<EOL><DEDENT>except:<EOL><INDENT>self.extraction_error()<EOL><DEDENT>self._warn_unsafe_extraction_path(extract_path)<EOL>self.cached_files[target_path] = <NUM_LIT:1><EOL>return target_path<EOL>

Return absolute location in cache for `archive_name` and `names`

        The parent directory of the resulting path will be created if it does
        not already exist.  `archive_name` should be the base filename of the
        enclosing egg (which may not be the name of the enclosing zipfile!),
        including its ".egg" extension.  `names`, if provided, should be a
        sequence of path name parts "under" the egg's extraction location.

        This method should only be called by resource providers that need to
        obtain an extraction location, and only for names they intend to
        extract, as it tracks the generated names for possible cleanup later.


if os.name == '<STR_LIT>' and not path.startswith(os.environ['<STR_LIT>']):<EOL><INDENT>return<EOL><DEDENT>mode = os.stat(path).st_mode<EOL>if mode & stat.S_IWOTH or mode & stat.S_IWGRP:<EOL><INDENT>msg = ("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % path)<EOL>warnings.warn(msg, UserWarning)<EOL><DEDENT>

If the default extraction path is overridden and set to an insecure
location, such as /tmp, it opens up an opportunity for an attacker to
replace an extracted file with an unauthorized payload. Warn the user
if a known insecure location is used.

See Distribute #375 for more details.


def postprocess(self, tempname, filename):

if os.name == '<STR_LIT>':<EOL><INDENT>mode = ((os.stat(tempname).st_mode) | <NUM_LIT>) & <NUM_LIT><EOL>os.chmod(tempname, mode)<EOL><DEDENT>

Perform any platform-specific postprocessing of `tempname`

        This is where Mac header rewrites should be done; other platforms don't
        have anything special they should do.

        Resource providers should call this method ONLY after successfully
        extracting a compressed resource.  They must NOT call it on resources
        that are already in the filesystem.

        `tempname` is the current (temporary) name of the file, and `filename`
        is the name it will be renamed to by the caller after this routine
        returns.


if self.cached_files:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>self.extraction_path = path<EOL>

Set the base path where resources will be extracted to, if needed.

        If you do not call this routine before any extractions take place, the
        path defaults to the return value of ``get_default_cache()``.  (Which
        is based on the ``PYTHON_EGG_CACHE`` environment variable, with various
        platform-specific fallbacks.  See that routine's documentation for more
        details.)

        Resources are extracted to subdirectories of this path based upon
        information given by the ``IResourceProvider``.  You may set this to a
        temporary directory, but then you must call ``cleanup_resources()`` to
        delete the extracted files when done.  There is no guarantee that
        ``cleanup_resources()`` will be able to remove all extracted files.

        (Note: you may not change the extraction path for a given resource
        manager once resources have been extracted, unless you first call
        ``cleanup_resources()``.)


<EOL>

Delete all extracted resource files and directories, returning a list
of the file and directory names that could not be successfully removed.
This function does not have any concurrency protection, so it should
generally only be called when the extraction path is a temporary
directory exclusive to a single process.  This method is not
automatically called; you must call it explicitly or register it as an
``atexit`` function if you wish to ensure cleanup of a temporary
directory used for extractions.


try:<EOL><INDENT>cls.evaluate_marker(text)<EOL><DEDENT>except SyntaxError:<EOL><INDENT>return cls.normalize_exception(sys.exc_info()[<NUM_LIT:1>])<EOL><DEDENT>return False<EOL>

Validate text as a PEP 426 environment marker; return an exception
if invalid or False otherwise.


subs = {<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>}<EOL>exc.filename = None<EOL>exc.lineno = None<EOL>exc.msg = subs.get(exc.msg, exc.msg)<EOL>return exc<EOL>

Given a SyntaxError from a marker evaluation, normalize the error
message:
 - Remove indications of filename and line number.
 - Replace platform-specific error messages with standard error
   messages.


@classmethod<EOL><INDENT>def evaluate_marker(cls, text, extra=None):<DEDENT>

return cls.interpret(parser.expr(text).totuple(<NUM_LIT:1>)[<NUM_LIT:1>])<EOL>

Evaluate a PEP 426 environment marker on CPython 2.4+.
Return a boolean indicating the marker result in this environment.
Raise SyntaxError if marker is invalid.

This implementation uses the 'parser' module, which is not implemented
on
Jython and has been superseded by the 'ast' module in Python 2.6 and
later.


from pip._vendor import _markerlib<EOL>env = _markerlib.default_environment()<EOL>for key in env.keys():<EOL><INDENT>new_key = key.replace('<STR_LIT:.>', '<STR_LIT:_>')<EOL>env[new_key] = env.pop(key)<EOL><DEDENT>try:<EOL><INDENT>result = _markerlib.interpret(text, env)<EOL><DEDENT>except NameError:<EOL><INDENT>e = sys.exc_info()[<NUM_LIT:1>]<EOL>raise SyntaxError(e.args[<NUM_LIT:0>])<EOL><DEDENT>return result<EOL>

Evaluate a PEP 426 environment marker using markerlib.
Return a boolean indicating the marker result in this environment.
Raise SyntaxError if marker is invalid.


with ContextualZipFile(path) as zfile:<EOL><INDENT>items = (<EOL>(<EOL>name.replace('<STR_LIT:/>', os.sep),<EOL>zfile.getinfo(name),<EOL>)<EOL>for name in zfile.namelist()<EOL>)<EOL>return dict(items)<EOL><DEDENT>

Build a dictionary similar to the zipimport directory
caches, except instead of tuples, store ZipInfo objects.

Use a platform-specific path separator (os.sep) for the path keys
for compatibility with pypy on Windows.


path = os.path.normpath(path)<EOL>mtime = os.stat(path).st_mtime<EOL>if path not in self or self[path].mtime != mtime:<EOL><INDENT>manifest = self.build(path)<EOL>self[path] = self.manifest_mod(manifest, mtime)<EOL><DEDENT>return self[path].manifest<EOL>

Load a manifest at path or return a suitable manifest already loaded.


if hasattr(zipfile.ZipFile, '<STR_LIT>'):<EOL><INDENT>return zipfile.ZipFile(*args, **kwargs)<EOL><DEDENT>return super(ContextualZipFile, cls).__new__(cls)<EOL>

Construct a ZipFile or ContextualZipFile as appropriate


def _is_current(self, file_path, zip_path):

timestamp, size = self._get_date_and_size(self.zipinfo[zip_path])<EOL>if not os.path.isfile(file_path):<EOL><INDENT>return False<EOL><DEDENT>stat = os.stat(file_path)<EOL>if stat.st_size!=size or stat.st_mtime!=timestamp:<EOL><INDENT>return False<EOL><DEDENT>zip_contents = self.loader.get_data(zip_path)<EOL>with open(file_path, '<STR_LIT:rb>') as f:<EOL><INDENT>file_contents = f.read()<EOL><DEDENT>return zip_contents == file_contents<EOL>

Return True if the file_path is current for this zip_path


self.zip_pre = importer.archive+os.sep<EOL>self.loader = importer<EOL>if importer.prefix:<EOL><INDENT>self.module_path = os.path.join(importer.archive, importer.prefix)<EOL><DEDENT>else:<EOL><INDENT>self.module_path = importer.archive<EOL><DEDENT>self._setup_prefix()<EOL>

Create a metadata provider from a zipimporter


@classmethod<EOL><INDENT>def parse(cls, src, dist=None):<DEDENT>

m = cls.pattern.match(src)<EOL>if not m:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg, src)<EOL><DEDENT>res = m.groupdict()<EOL>extras = cls._parse_extras(res['<STR_LIT>'])<EOL>attrs = res['<STR_LIT>'].split('<STR_LIT:.>') if res['<STR_LIT>'] else ()<EOL>return cls(res['<STR_LIT:name>'], res['<STR_LIT>'], attrs, extras, dist)<EOL>

Parse a single entry point from string `src`

        Entry point syntax follows the form::

            name = some.module:some.attr [extra1, extra2]

        The entry name and module name are required, but the ``:attrs`` and
        ``[extras]`` parts are optional


@classmethod<EOL><INDENT>def parse_group(cls, group, lines, dist=None):<DEDENT>

if not MODULE(group):<EOL><INDENT>raise ValueError("<STR_LIT>", group)<EOL><DEDENT>this = {}<EOL>for line in yield_lines(lines):<EOL><INDENT>ep = cls.parse(line, dist)<EOL>if ep.name in this:<EOL><INDENT>raise ValueError("<STR_LIT>", group, ep.name)<EOL><DEDENT>this[ep.name]=ep<EOL><DEDENT>return this<EOL>


@classmethod<EOL><INDENT>def parse_map(cls, data, dist=None):<DEDENT>

if isinstance(data, dict):<EOL><INDENT>data = data.items()<EOL><DEDENT>else:<EOL><INDENT>data = split_sections(data)<EOL><DEDENT>maps = {}<EOL>for group, lines in data:<EOL><INDENT>if group is None:<EOL><INDENT>if not lines:<EOL><INDENT>continue<EOL><DEDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>group = group.strip()<EOL>if group in maps:<EOL><INDENT>raise ValueError("<STR_LIT>", group)<EOL><DEDENT>maps[group] = cls.parse_group(group, lines, dist)<EOL><DEDENT>return maps<EOL>


dm = self._dep_map<EOL>deps = []<EOL>deps.extend(dm.get(None, ()))<EOL>for ext in extras:<EOL><INDENT>try:<EOL><INDENT>deps.extend(dm[safe_extra(ext)])<EOL><DEDENT>except KeyError:<EOL><INDENT>raise UnknownExtra(<EOL>"<STR_LIT>" % (self, ext)<EOL>)<EOL><DEDENT><DEDENT>return deps<EOL>

List of Requirements needed for this distro if `extras` are used


if path is None:<EOL><INDENT>path = sys.path<EOL><DEDENT>self.insert_on(path)<EOL>if path is sys.path:<EOL><INDENT>fixup_namespace_packages(self.location)<EOL>for pkg in self._get_metadata('<STR_LIT>'):<EOL><INDENT>if pkg in sys.modules:<EOL><INDENT>declare_namespace(pkg)<EOL><DEDENT><DEDENT><DEDENT>

Ensure distribution is importable on `path` (default=sys.path)


filename = "<STR_LIT>" % (<EOL>to_filename(self.project_name), to_filename(self.version),<EOL>self.py_version or PY_MAJOR<EOL>)<EOL>if self.platform:<EOL><INDENT>filename += '<STR_LIT:->' + self.platform<EOL><DEDENT>return filename<EOL>

Return what this distribution's standard .egg filename should be


if attr.startswith('<STR_LIT:_>'):<EOL><INDENT>raise AttributeError(attr)<EOL><DEDENT>return getattr(self._provider, attr)<EOL>

Delegate all unrecognized public attributes to .metadata provider


if isinstance(self.parsed_version, packaging.version.Version):<EOL><INDENT>spec = "<STR_LIT>" % (self.project_name, self.parsed_version)<EOL><DEDENT>else:<EOL><INDENT>spec = "<STR_LIT>" % (self.project_name, self.parsed_version)<EOL><DEDENT>return Requirement.parse(spec)<EOL>

Return a ``Requirement`` that matches this distribution exactly


def load_entry_point(self, group, name):

ep = self.get_entry_info(group, name)<EOL>if ep is None:<EOL><INDENT>raise ImportError("<STR_LIT>" % ((group, name),))<EOL><DEDENT>return ep.load()<EOL>

Return the `name` entry point of `group` or raise ImportError


try:<EOL><INDENT>ep_map = self._ep_map<EOL><DEDENT>except AttributeError:<EOL><INDENT>ep_map = self._ep_map = EntryPoint.parse_map(<EOL>self._get_metadata('<STR_LIT>'), self<EOL>)<EOL><DEDENT>if group is not None:<EOL><INDENT>return ep_map.get(group,{})<EOL><DEDENT>return ep_map<EOL>

Return the entry point map for `group`, or the full entry map


return self.get_entry_map(group).get(name)<EOL>

Return the EntryPoint object for `group`+`name`, or ``None``


loc = loc or self.location<EOL>if not loc:<EOL><INDENT>return<EOL><DEDENT>nloc = _normalize_cached(loc)<EOL>bdir = os.path.dirname(nloc)<EOL>npath= [(p and _normalize_cached(p) or p) for p in path]<EOL>for p, item in enumerate(npath):<EOL><INDENT>if item == nloc:<EOL><INDENT>break<EOL><DEDENT>elif item == bdir and self.precedence == EGG_DIST:<EOL><INDENT>if path is sys.path:<EOL><INDENT>self.check_version_conflict()<EOL><DEDENT>path.insert(p, loc)<EOL>npath.insert(p, nloc)<EOL>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if path is sys.path:<EOL><INDENT>self.check_version_conflict()<EOL><DEDENT>path.append(loc)<EOL>return<EOL><DEDENT>while True:<EOL><INDENT>try:<EOL><INDENT>np = npath.index(nloc, p+<NUM_LIT:1>)<EOL><DEDENT>except ValueError:<EOL><INDENT>break<EOL><DEDENT>else:<EOL><INDENT>del npath[np], path[np]<EOL>p = np<EOL><DEDENT><DEDENT>return<EOL>

Insert self.location in path before its nearest parent directory


names = '<STR_LIT>'<EOL>for attr in names.split():<EOL><INDENT>kw.setdefault(attr, getattr(self, attr, None))<EOL><DEDENT>kw.setdefault('<STR_LIT>', self._provider)<EOL>return self.__class__(**kw)<EOL>

Copy this distribution, substituting in any changed keyword args