repo
stringlengths 7
55
| path
stringlengths 4
223
| func_name
stringlengths 1
134
| original_string
stringlengths 75
104k
| language
stringclasses 1
value | code
stringlengths 75
104k
| code_tokens
listlengths 19
28.4k
| docstring
stringlengths 1
46.9k
| docstring_tokens
listlengths 1
1.97k
| sha
stringlengths 40
40
| url
stringlengths 87
315
| partition
stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|
pypa/pipenv
|
pipenv/patched/notpip/_internal/utils/misc.py
|
redact_netloc
|
def redact_netloc(netloc):
# type: (str) -> str
"""
Replace the password in a netloc with "****", if it exists.
For example, "user:pass@example.com" returns "user:****@example.com".
"""
netloc, (user, password) = split_auth_from_netloc(netloc)
if user is None:
return netloc
password = '' if password is None else ':****'
return '{user}{password}@{netloc}'.format(user=urllib_parse.quote(user),
password=password,
netloc=netloc)
|
python
|
def redact_netloc(netloc):
# type: (str) -> str
"""
Replace the password in a netloc with "****", if it exists.
For example, "user:pass@example.com" returns "user:****@example.com".
"""
netloc, (user, password) = split_auth_from_netloc(netloc)
if user is None:
return netloc
password = '' if password is None else ':****'
return '{user}{password}@{netloc}'.format(user=urllib_parse.quote(user),
password=password,
netloc=netloc)
|
[
"def",
"redact_netloc",
"(",
"netloc",
")",
":",
"# type: (str) -> str",
"netloc",
",",
"(",
"user",
",",
"password",
")",
"=",
"split_auth_from_netloc",
"(",
"netloc",
")",
"if",
"user",
"is",
"None",
":",
"return",
"netloc",
"password",
"=",
"''",
"if",
"password",
"is",
"None",
"else",
"':****'",
"return",
"'{user}{password}@{netloc}'",
".",
"format",
"(",
"user",
"=",
"urllib_parse",
".",
"quote",
"(",
"user",
")",
",",
"password",
"=",
"password",
",",
"netloc",
"=",
"netloc",
")"
] |
Replace the password in a netloc with "****", if it exists.
For example, "user:pass@example.com" returns "user:****@example.com".
|
[
"Replace",
"the",
"password",
"in",
"a",
"netloc",
"with",
"****",
"if",
"it",
"exists",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/utils/misc.py#L969-L982
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/utils/misc.py
|
protect_pip_from_modification_on_windows
|
def protect_pip_from_modification_on_windows(modifying_pip):
"""Protection of pip.exe from modification on Windows
On Windows, any operation modifying pip should be run as:
python -m pip ...
"""
pip_names = [
"pip.exe",
"pip{}.exe".format(sys.version_info[0]),
"pip{}.{}.exe".format(*sys.version_info[:2])
]
# See https://github.com/pypa/pip/issues/1299 for more discussion
should_show_use_python_msg = (
modifying_pip and
WINDOWS and
os.path.basename(sys.argv[0]) in pip_names
)
if should_show_use_python_msg:
new_command = [
sys.executable, "-m", "pip"
] + sys.argv[1:]
raise CommandError(
'To modify pip, please run the following command:\n{}'
.format(" ".join(new_command))
)
|
python
|
def protect_pip_from_modification_on_windows(modifying_pip):
"""Protection of pip.exe from modification on Windows
On Windows, any operation modifying pip should be run as:
python -m pip ...
"""
pip_names = [
"pip.exe",
"pip{}.exe".format(sys.version_info[0]),
"pip{}.{}.exe".format(*sys.version_info[:2])
]
# See https://github.com/pypa/pip/issues/1299 for more discussion
should_show_use_python_msg = (
modifying_pip and
WINDOWS and
os.path.basename(sys.argv[0]) in pip_names
)
if should_show_use_python_msg:
new_command = [
sys.executable, "-m", "pip"
] + sys.argv[1:]
raise CommandError(
'To modify pip, please run the following command:\n{}'
.format(" ".join(new_command))
)
|
[
"def",
"protect_pip_from_modification_on_windows",
"(",
"modifying_pip",
")",
":",
"pip_names",
"=",
"[",
"\"pip.exe\"",
",",
"\"pip{}.exe\"",
".",
"format",
"(",
"sys",
".",
"version_info",
"[",
"0",
"]",
")",
",",
"\"pip{}.{}.exe\"",
".",
"format",
"(",
"*",
"sys",
".",
"version_info",
"[",
":",
"2",
"]",
")",
"]",
"# See https://github.com/pypa/pip/issues/1299 for more discussion",
"should_show_use_python_msg",
"=",
"(",
"modifying_pip",
"and",
"WINDOWS",
"and",
"os",
".",
"path",
".",
"basename",
"(",
"sys",
".",
"argv",
"[",
"0",
"]",
")",
"in",
"pip_names",
")",
"if",
"should_show_use_python_msg",
":",
"new_command",
"=",
"[",
"sys",
".",
"executable",
",",
"\"-m\"",
",",
"\"pip\"",
"]",
"+",
"sys",
".",
"argv",
"[",
"1",
":",
"]",
"raise",
"CommandError",
"(",
"'To modify pip, please run the following command:\\n{}'",
".",
"format",
"(",
"\" \"",
".",
"join",
"(",
"new_command",
")",
")",
")"
] |
Protection of pip.exe from modification on Windows
On Windows, any operation modifying pip should be run as:
python -m pip ...
|
[
"Protection",
"of",
"pip",
".",
"exe",
"from",
"modification",
"on",
"Windows"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/utils/misc.py#L1014-L1040
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/utils/packaging.py
|
check_requires_python
|
def check_requires_python(requires_python):
# type: (Optional[str]) -> bool
"""
Check if the python version in use match the `requires_python` specifier.
Returns `True` if the version of python in use matches the requirement.
Returns `False` if the version of python in use does not matches the
requirement.
Raises an InvalidSpecifier if `requires_python` have an invalid format.
"""
if requires_python is None:
# The package provides no information
return True
requires_python_specifier = specifiers.SpecifierSet(requires_python)
# We only use major.minor.micro
python_version = version.parse('{0}.{1}.{2}'.format(*sys.version_info[:3]))
return python_version in requires_python_specifier
|
python
|
def check_requires_python(requires_python):
# type: (Optional[str]) -> bool
"""
Check if the python version in use match the `requires_python` specifier.
Returns `True` if the version of python in use matches the requirement.
Returns `False` if the version of python in use does not matches the
requirement.
Raises an InvalidSpecifier if `requires_python` have an invalid format.
"""
if requires_python is None:
# The package provides no information
return True
requires_python_specifier = specifiers.SpecifierSet(requires_python)
# We only use major.minor.micro
python_version = version.parse('{0}.{1}.{2}'.format(*sys.version_info[:3]))
return python_version in requires_python_specifier
|
[
"def",
"check_requires_python",
"(",
"requires_python",
")",
":",
"# type: (Optional[str]) -> bool",
"if",
"requires_python",
"is",
"None",
":",
"# The package provides no information",
"return",
"True",
"requires_python_specifier",
"=",
"specifiers",
".",
"SpecifierSet",
"(",
"requires_python",
")",
"# We only use major.minor.micro",
"python_version",
"=",
"version",
".",
"parse",
"(",
"'{0}.{1}.{2}'",
".",
"format",
"(",
"*",
"sys",
".",
"version_info",
"[",
":",
"3",
"]",
")",
")",
"return",
"python_version",
"in",
"requires_python_specifier"
] |
Check if the python version in use match the `requires_python` specifier.
Returns `True` if the version of python in use matches the requirement.
Returns `False` if the version of python in use does not matches the
requirement.
Raises an InvalidSpecifier if `requires_python` have an invalid format.
|
[
"Check",
"if",
"the",
"python",
"version",
"in",
"use",
"match",
"the",
"requires_python",
"specifier",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/utils/packaging.py#L23-L41
|
train
|
pypa/pipenv
|
pipenv/vendor/click_completion/__init__.py
|
init
|
def init(complete_options=False, match_incomplete=None):
"""Initialize the enhanced click completion
Parameters
----------
complete_options : bool
always complete the options, even when the user hasn't typed a first dash (Default value = False)
match_incomplete : func
a function with two parameters choice and incomplete. Must return True
if incomplete is a correct match for choice, False otherwise.
"""
global _initialized
if not _initialized:
_patch()
completion_configuration.complete_options = complete_options
if match_incomplete is not None:
completion_configuration.match_incomplete = match_incomplete
_initialized = True
|
python
|
def init(complete_options=False, match_incomplete=None):
"""Initialize the enhanced click completion
Parameters
----------
complete_options : bool
always complete the options, even when the user hasn't typed a first dash (Default value = False)
match_incomplete : func
a function with two parameters choice and incomplete. Must return True
if incomplete is a correct match for choice, False otherwise.
"""
global _initialized
if not _initialized:
_patch()
completion_configuration.complete_options = complete_options
if match_incomplete is not None:
completion_configuration.match_incomplete = match_incomplete
_initialized = True
|
[
"def",
"init",
"(",
"complete_options",
"=",
"False",
",",
"match_incomplete",
"=",
"None",
")",
":",
"global",
"_initialized",
"if",
"not",
"_initialized",
":",
"_patch",
"(",
")",
"completion_configuration",
".",
"complete_options",
"=",
"complete_options",
"if",
"match_incomplete",
"is",
"not",
"None",
":",
"completion_configuration",
".",
"match_incomplete",
"=",
"match_incomplete",
"_initialized",
"=",
"True"
] |
Initialize the enhanced click completion
Parameters
----------
complete_options : bool
always complete the options, even when the user hasn't typed a first dash (Default value = False)
match_incomplete : func
a function with two parameters choice and incomplete. Must return True
if incomplete is a correct match for choice, False otherwise.
|
[
"Initialize",
"the",
"enhanced",
"click",
"completion"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/click_completion/__init__.py#L27-L44
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/commands/search.py
|
transform_hits
|
def transform_hits(hits):
"""
The list from pypi is really a list of versions. We want a list of
packages with the list of versions stored inline. This converts the
list from pypi into one we can use.
"""
packages = OrderedDict()
for hit in hits:
name = hit['name']
summary = hit['summary']
version = hit['version']
if name not in packages.keys():
packages[name] = {
'name': name,
'summary': summary,
'versions': [version],
}
else:
packages[name]['versions'].append(version)
# if this is the highest version, replace summary and score
if version == highest_version(packages[name]['versions']):
packages[name]['summary'] = summary
return list(packages.values())
|
python
|
def transform_hits(hits):
"""
The list from pypi is really a list of versions. We want a list of
packages with the list of versions stored inline. This converts the
list from pypi into one we can use.
"""
packages = OrderedDict()
for hit in hits:
name = hit['name']
summary = hit['summary']
version = hit['version']
if name not in packages.keys():
packages[name] = {
'name': name,
'summary': summary,
'versions': [version],
}
else:
packages[name]['versions'].append(version)
# if this is the highest version, replace summary and score
if version == highest_version(packages[name]['versions']):
packages[name]['summary'] = summary
return list(packages.values())
|
[
"def",
"transform_hits",
"(",
"hits",
")",
":",
"packages",
"=",
"OrderedDict",
"(",
")",
"for",
"hit",
"in",
"hits",
":",
"name",
"=",
"hit",
"[",
"'name'",
"]",
"summary",
"=",
"hit",
"[",
"'summary'",
"]",
"version",
"=",
"hit",
"[",
"'version'",
"]",
"if",
"name",
"not",
"in",
"packages",
".",
"keys",
"(",
")",
":",
"packages",
"[",
"name",
"]",
"=",
"{",
"'name'",
":",
"name",
",",
"'summary'",
":",
"summary",
",",
"'versions'",
":",
"[",
"version",
"]",
",",
"}",
"else",
":",
"packages",
"[",
"name",
"]",
"[",
"'versions'",
"]",
".",
"append",
"(",
"version",
")",
"# if this is the highest version, replace summary and score",
"if",
"version",
"==",
"highest_version",
"(",
"packages",
"[",
"name",
"]",
"[",
"'versions'",
"]",
")",
":",
"packages",
"[",
"name",
"]",
"[",
"'summary'",
"]",
"=",
"summary",
"return",
"list",
"(",
"packages",
".",
"values",
"(",
")",
")"
] |
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.
|
[
"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",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/commands/search.py#L69-L94
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/req/req_set.py
|
RequirementSet.add_requirement
|
def add_requirement(
self,
install_req, # type: InstallRequirement
parent_req_name=None, # type: Optional[str]
extras_requested=None # type: Optional[Iterable[str]]
):
# type: (...) -> Tuple[List[InstallRequirement], Optional[InstallRequirement]] # noqa: E501
"""Add install_req as a requirement to install.
:param parent_req_name: The name of the requirement that needed this
added. The name is used because when multiple unnamed requirements
resolve to the same name, we could otherwise end up with dependency
links that point outside the Requirements set. parent_req must
already be added. Note that None implies that this is a user
supplied requirement, vs an inferred one.
:param extras_requested: an iterable of extras used to evaluate the
environment markers.
:return: Additional requirements to scan. That is either [] if
the requirement is not applicable, or [install_req] if the
requirement is applicable and has just been added.
"""
name = install_req.name
# If the markers do not match, ignore this requirement.
if not install_req.match_markers(extras_requested):
logger.info(
"Ignoring %s: markers '%s' don't match your environment",
name, install_req.markers,
)
return [], None
# If the wheel is not supported, raise an error.
# Should check this after filtering out based on environment markers to
# allow specifying different wheels based on the environment/OS, in a
# single requirements file.
if install_req.link and install_req.link.is_wheel:
wheel = Wheel(install_req.link.filename)
if self.check_supported_wheels and not wheel.supported():
raise InstallationError(
"%s is not a supported wheel on this platform." %
wheel.filename
)
# This next bit is really a sanity check.
assert install_req.is_direct == (parent_req_name is None), (
"a direct req shouldn't have a parent and also, "
"a non direct req should have a parent"
)
# Unnamed requirements are scanned again and the requirement won't be
# added as a dependency until after scanning.
if not name:
# url or path requirement w/o an egg fragment
self.unnamed_requirements.append(install_req)
return [install_req], None
try:
existing_req = self.get_requirement(name)
except KeyError:
existing_req = None
has_conflicting_requirement = (
parent_req_name is None and
existing_req and
not existing_req.constraint and
existing_req.extras == install_req.extras and
existing_req.req.specifier != install_req.req.specifier
)
if has_conflicting_requirement:
raise InstallationError(
"Double requirement given: %s (already in %s, name=%r)"
% (install_req, existing_req, name)
)
# When no existing requirement exists, add the requirement as a
# dependency and it will be scanned again after.
if not existing_req:
self.requirements[name] = install_req
# FIXME: what about other normalizations? E.g., _ vs. -?
if name.lower() != name:
self.requirement_aliases[name.lower()] = name
# We'd want to rescan this requirements later
return [install_req], install_req
# Assume there's no need to scan, and that we've already
# encountered this for scanning.
if install_req.constraint or not existing_req.constraint:
return [], existing_req
does_not_satisfy_constraint = (
install_req.link and
not (
existing_req.link and
install_req.link.path == existing_req.link.path
)
)
if does_not_satisfy_constraint:
self.reqs_to_cleanup.append(install_req)
raise InstallationError(
"Could not satisfy constraints for '%s': "
"installation from path or url cannot be "
"constrained to a version" % name,
)
# If we're now installing a constraint, mark the existing
# object for real installation.
existing_req.constraint = False
existing_req.extras = tuple(sorted(
set(existing_req.extras) | set(install_req.extras)
))
logger.debug(
"Setting %s extras to: %s",
existing_req, existing_req.extras,
)
# Return the existing requirement for addition to the parent and
# scanning again.
return [existing_req], existing_req
|
python
|
def add_requirement(
self,
install_req, # type: InstallRequirement
parent_req_name=None, # type: Optional[str]
extras_requested=None # type: Optional[Iterable[str]]
):
# type: (...) -> Tuple[List[InstallRequirement], Optional[InstallRequirement]] # noqa: E501
"""Add install_req as a requirement to install.
:param parent_req_name: The name of the requirement that needed this
added. The name is used because when multiple unnamed requirements
resolve to the same name, we could otherwise end up with dependency
links that point outside the Requirements set. parent_req must
already be added. Note that None implies that this is a user
supplied requirement, vs an inferred one.
:param extras_requested: an iterable of extras used to evaluate the
environment markers.
:return: Additional requirements to scan. That is either [] if
the requirement is not applicable, or [install_req] if the
requirement is applicable and has just been added.
"""
name = install_req.name
# If the markers do not match, ignore this requirement.
if not install_req.match_markers(extras_requested):
logger.info(
"Ignoring %s: markers '%s' don't match your environment",
name, install_req.markers,
)
return [], None
# If the wheel is not supported, raise an error.
# Should check this after filtering out based on environment markers to
# allow specifying different wheels based on the environment/OS, in a
# single requirements file.
if install_req.link and install_req.link.is_wheel:
wheel = Wheel(install_req.link.filename)
if self.check_supported_wheels and not wheel.supported():
raise InstallationError(
"%s is not a supported wheel on this platform." %
wheel.filename
)
# This next bit is really a sanity check.
assert install_req.is_direct == (parent_req_name is None), (
"a direct req shouldn't have a parent and also, "
"a non direct req should have a parent"
)
# Unnamed requirements are scanned again and the requirement won't be
# added as a dependency until after scanning.
if not name:
# url or path requirement w/o an egg fragment
self.unnamed_requirements.append(install_req)
return [install_req], None
try:
existing_req = self.get_requirement(name)
except KeyError:
existing_req = None
has_conflicting_requirement = (
parent_req_name is None and
existing_req and
not existing_req.constraint and
existing_req.extras == install_req.extras and
existing_req.req.specifier != install_req.req.specifier
)
if has_conflicting_requirement:
raise InstallationError(
"Double requirement given: %s (already in %s, name=%r)"
% (install_req, existing_req, name)
)
# When no existing requirement exists, add the requirement as a
# dependency and it will be scanned again after.
if not existing_req:
self.requirements[name] = install_req
# FIXME: what about other normalizations? E.g., _ vs. -?
if name.lower() != name:
self.requirement_aliases[name.lower()] = name
# We'd want to rescan this requirements later
return [install_req], install_req
# Assume there's no need to scan, and that we've already
# encountered this for scanning.
if install_req.constraint or not existing_req.constraint:
return [], existing_req
does_not_satisfy_constraint = (
install_req.link and
not (
existing_req.link and
install_req.link.path == existing_req.link.path
)
)
if does_not_satisfy_constraint:
self.reqs_to_cleanup.append(install_req)
raise InstallationError(
"Could not satisfy constraints for '%s': "
"installation from path or url cannot be "
"constrained to a version" % name,
)
# If we're now installing a constraint, mark the existing
# object for real installation.
existing_req.constraint = False
existing_req.extras = tuple(sorted(
set(existing_req.extras) | set(install_req.extras)
))
logger.debug(
"Setting %s extras to: %s",
existing_req, existing_req.extras,
)
# Return the existing requirement for addition to the parent and
# scanning again.
return [existing_req], existing_req
|
[
"def",
"add_requirement",
"(",
"self",
",",
"install_req",
",",
"# type: InstallRequirement",
"parent_req_name",
"=",
"None",
",",
"# type: Optional[str]",
"extras_requested",
"=",
"None",
"# type: Optional[Iterable[str]]",
")",
":",
"# type: (...) -> Tuple[List[InstallRequirement], Optional[InstallRequirement]] # noqa: E501",
"name",
"=",
"install_req",
".",
"name",
"# If the markers do not match, ignore this requirement.",
"if",
"not",
"install_req",
".",
"match_markers",
"(",
"extras_requested",
")",
":",
"logger",
".",
"info",
"(",
"\"Ignoring %s: markers '%s' don't match your environment\"",
",",
"name",
",",
"install_req",
".",
"markers",
",",
")",
"return",
"[",
"]",
",",
"None",
"# If the wheel is not supported, raise an error.",
"# Should check this after filtering out based on environment markers to",
"# allow specifying different wheels based on the environment/OS, in a",
"# single requirements file.",
"if",
"install_req",
".",
"link",
"and",
"install_req",
".",
"link",
".",
"is_wheel",
":",
"wheel",
"=",
"Wheel",
"(",
"install_req",
".",
"link",
".",
"filename",
")",
"if",
"self",
".",
"check_supported_wheels",
"and",
"not",
"wheel",
".",
"supported",
"(",
")",
":",
"raise",
"InstallationError",
"(",
"\"%s is not a supported wheel on this platform.\"",
"%",
"wheel",
".",
"filename",
")",
"# This next bit is really a sanity check.",
"assert",
"install_req",
".",
"is_direct",
"==",
"(",
"parent_req_name",
"is",
"None",
")",
",",
"(",
"\"a direct req shouldn't have a parent and also, \"",
"\"a non direct req should have a parent\"",
")",
"# Unnamed requirements are scanned again and the requirement won't be",
"# added as a dependency until after scanning.",
"if",
"not",
"name",
":",
"# url or path requirement w/o an egg fragment",
"self",
".",
"unnamed_requirements",
".",
"append",
"(",
"install_req",
")",
"return",
"[",
"install_req",
"]",
",",
"None",
"try",
":",
"existing_req",
"=",
"self",
".",
"get_requirement",
"(",
"name",
")",
"except",
"KeyError",
":",
"existing_req",
"=",
"None",
"has_conflicting_requirement",
"=",
"(",
"parent_req_name",
"is",
"None",
"and",
"existing_req",
"and",
"not",
"existing_req",
".",
"constraint",
"and",
"existing_req",
".",
"extras",
"==",
"install_req",
".",
"extras",
"and",
"existing_req",
".",
"req",
".",
"specifier",
"!=",
"install_req",
".",
"req",
".",
"specifier",
")",
"if",
"has_conflicting_requirement",
":",
"raise",
"InstallationError",
"(",
"\"Double requirement given: %s (already in %s, name=%r)\"",
"%",
"(",
"install_req",
",",
"existing_req",
",",
"name",
")",
")",
"# When no existing requirement exists, add the requirement as a",
"# dependency and it will be scanned again after.",
"if",
"not",
"existing_req",
":",
"self",
".",
"requirements",
"[",
"name",
"]",
"=",
"install_req",
"# FIXME: what about other normalizations? E.g., _ vs. -?",
"if",
"name",
".",
"lower",
"(",
")",
"!=",
"name",
":",
"self",
".",
"requirement_aliases",
"[",
"name",
".",
"lower",
"(",
")",
"]",
"=",
"name",
"# We'd want to rescan this requirements later",
"return",
"[",
"install_req",
"]",
",",
"install_req",
"# Assume there's no need to scan, and that we've already",
"# encountered this for scanning.",
"if",
"install_req",
".",
"constraint",
"or",
"not",
"existing_req",
".",
"constraint",
":",
"return",
"[",
"]",
",",
"existing_req",
"does_not_satisfy_constraint",
"=",
"(",
"install_req",
".",
"link",
"and",
"not",
"(",
"existing_req",
".",
"link",
"and",
"install_req",
".",
"link",
".",
"path",
"==",
"existing_req",
".",
"link",
".",
"path",
")",
")",
"if",
"does_not_satisfy_constraint",
":",
"self",
".",
"reqs_to_cleanup",
".",
"append",
"(",
"install_req",
")",
"raise",
"InstallationError",
"(",
"\"Could not satisfy constraints for '%s': \"",
"\"installation from path or url cannot be \"",
"\"constrained to a version\"",
"%",
"name",
",",
")",
"# If we're now installing a constraint, mark the existing",
"# object for real installation.",
"existing_req",
".",
"constraint",
"=",
"False",
"existing_req",
".",
"extras",
"=",
"tuple",
"(",
"sorted",
"(",
"set",
"(",
"existing_req",
".",
"extras",
")",
"|",
"set",
"(",
"install_req",
".",
"extras",
")",
")",
")",
"logger",
".",
"debug",
"(",
"\"Setting %s extras to: %s\"",
",",
"existing_req",
",",
"existing_req",
".",
"extras",
",",
")",
"# Return the existing requirement for addition to the parent and",
"# scanning again.",
"return",
"[",
"existing_req",
"]",
",",
"existing_req"
] |
Add install_req as a requirement to install.
:param parent_req_name: The name of the requirement that needed this
added. The name is used because when multiple unnamed requirements
resolve to the same name, we could otherwise end up with dependency
links that point outside the Requirements set. parent_req must
already be added. Note that None implies that this is a user
supplied requirement, vs an inferred one.
:param extras_requested: an iterable of extras used to evaluate the
environment markers.
:return: Additional requirements to scan. That is either [] if
the requirement is not applicable, or [install_req] if the
requirement is applicable and has just been added.
|
[
"Add",
"install_req",
"as",
"a",
"requirement",
"to",
"install",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/req/req_set.py#L52-L167
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/resolve.py
|
Resolver.resolve
|
def resolve(self, requirement_set):
# type: (RequirementSet) -> None
"""Resolve what operations need to be done
As a side-effect of this method, the packages (and their dependencies)
are downloaded, unpacked and prepared for installation. This
preparation is done by ``pip.operations.prepare``.
Once PyPI has static dependency metadata available, it would be
possible to move the preparation to become a step separated from
dependency resolution.
"""
# make the wheelhouse
if self.preparer.wheel_download_dir:
ensure_dir(self.preparer.wheel_download_dir)
# If any top-level requirement has a hash specified, enter
# hash-checking mode, which requires hashes from all.
root_reqs = (
requirement_set.unnamed_requirements +
list(requirement_set.requirements.values())
)
self.require_hashes = (
requirement_set.require_hashes or
any(req.has_hash_options for req in root_reqs)
)
# Display where finder is looking for packages
locations = self.finder.get_formatted_locations()
if locations:
logger.info(locations)
# Actually prepare the files, and collect any exceptions. Most hash
# exceptions cannot be checked ahead of time, because
# req.populate_link() needs to be called before we can make decisions
# based on link type.
discovered_reqs = [] # type: List[InstallRequirement]
hash_errors = HashErrors()
for req in chain(root_reqs, discovered_reqs):
try:
discovered_reqs.extend(
self._resolve_one(requirement_set, req)
)
except HashError as exc:
exc.req = req
hash_errors.append(exc)
if hash_errors:
raise hash_errors
|
python
|
def resolve(self, requirement_set):
# type: (RequirementSet) -> None
"""Resolve what operations need to be done
As a side-effect of this method, the packages (and their dependencies)
are downloaded, unpacked and prepared for installation. This
preparation is done by ``pip.operations.prepare``.
Once PyPI has static dependency metadata available, it would be
possible to move the preparation to become a step separated from
dependency resolution.
"""
# make the wheelhouse
if self.preparer.wheel_download_dir:
ensure_dir(self.preparer.wheel_download_dir)
# If any top-level requirement has a hash specified, enter
# hash-checking mode, which requires hashes from all.
root_reqs = (
requirement_set.unnamed_requirements +
list(requirement_set.requirements.values())
)
self.require_hashes = (
requirement_set.require_hashes or
any(req.has_hash_options for req in root_reqs)
)
# Display where finder is looking for packages
locations = self.finder.get_formatted_locations()
if locations:
logger.info(locations)
# Actually prepare the files, and collect any exceptions. Most hash
# exceptions cannot be checked ahead of time, because
# req.populate_link() needs to be called before we can make decisions
# based on link type.
discovered_reqs = [] # type: List[InstallRequirement]
hash_errors = HashErrors()
for req in chain(root_reqs, discovered_reqs):
try:
discovered_reqs.extend(
self._resolve_one(requirement_set, req)
)
except HashError as exc:
exc.req = req
hash_errors.append(exc)
if hash_errors:
raise hash_errors
|
[
"def",
"resolve",
"(",
"self",
",",
"requirement_set",
")",
":",
"# type: (RequirementSet) -> None",
"# make the wheelhouse",
"if",
"self",
".",
"preparer",
".",
"wheel_download_dir",
":",
"ensure_dir",
"(",
"self",
".",
"preparer",
".",
"wheel_download_dir",
")",
"# If any top-level requirement has a hash specified, enter",
"# hash-checking mode, which requires hashes from all.",
"root_reqs",
"=",
"(",
"requirement_set",
".",
"unnamed_requirements",
"+",
"list",
"(",
"requirement_set",
".",
"requirements",
".",
"values",
"(",
")",
")",
")",
"self",
".",
"require_hashes",
"=",
"(",
"requirement_set",
".",
"require_hashes",
"or",
"any",
"(",
"req",
".",
"has_hash_options",
"for",
"req",
"in",
"root_reqs",
")",
")",
"# Display where finder is looking for packages",
"locations",
"=",
"self",
".",
"finder",
".",
"get_formatted_locations",
"(",
")",
"if",
"locations",
":",
"logger",
".",
"info",
"(",
"locations",
")",
"# Actually prepare the files, and collect any exceptions. Most hash",
"# exceptions cannot be checked ahead of time, because",
"# req.populate_link() needs to be called before we can make decisions",
"# based on link type.",
"discovered_reqs",
"=",
"[",
"]",
"# type: List[InstallRequirement]",
"hash_errors",
"=",
"HashErrors",
"(",
")",
"for",
"req",
"in",
"chain",
"(",
"root_reqs",
",",
"discovered_reqs",
")",
":",
"try",
":",
"discovered_reqs",
".",
"extend",
"(",
"self",
".",
"_resolve_one",
"(",
"requirement_set",
",",
"req",
")",
")",
"except",
"HashError",
"as",
"exc",
":",
"exc",
".",
"req",
"=",
"req",
"hash_errors",
".",
"append",
"(",
"exc",
")",
"if",
"hash_errors",
":",
"raise",
"hash_errors"
] |
Resolve what operations need to be done
As a side-effect of this method, the packages (and their dependencies)
are downloaded, unpacked and prepared for installation. This
preparation is done by ``pip.operations.prepare``.
Once PyPI has static dependency metadata available, it would be
possible to move the preparation to become a step separated from
dependency resolution.
|
[
"Resolve",
"what",
"operations",
"need",
"to",
"be",
"done"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/resolve.py#L96-L144
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/resolve.py
|
Resolver._set_req_to_reinstall
|
def _set_req_to_reinstall(self, req):
# type: (InstallRequirement) -> None
"""
Set a requirement to be installed.
"""
# Don't uninstall the conflict if doing a user install and the
# conflict is not a user install.
if not self.use_user_site or dist_in_usersite(req.satisfied_by):
req.conflicts_with = req.satisfied_by
req.satisfied_by = None
|
python
|
def _set_req_to_reinstall(self, req):
# type: (InstallRequirement) -> None
"""
Set a requirement to be installed.
"""
# Don't uninstall the conflict if doing a user install and the
# conflict is not a user install.
if not self.use_user_site or dist_in_usersite(req.satisfied_by):
req.conflicts_with = req.satisfied_by
req.satisfied_by = None
|
[
"def",
"_set_req_to_reinstall",
"(",
"self",
",",
"req",
")",
":",
"# type: (InstallRequirement) -> None",
"# Don't uninstall the conflict if doing a user install and the",
"# conflict is not a user install.",
"if",
"not",
"self",
".",
"use_user_site",
"or",
"dist_in_usersite",
"(",
"req",
".",
"satisfied_by",
")",
":",
"req",
".",
"conflicts_with",
"=",
"req",
".",
"satisfied_by",
"req",
".",
"satisfied_by",
"=",
"None"
] |
Set a requirement to be installed.
|
[
"Set",
"a",
"requirement",
"to",
"be",
"installed",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/resolve.py#L156-L165
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/resolve.py
|
Resolver._check_skip_installed
|
def _check_skip_installed(self, req_to_install):
# type: (InstallRequirement) -> Optional[str]
"""Check if req_to_install should be skipped.
This will check if the req is installed, and whether we should upgrade
or reinstall it, taking into account all the relevant user options.
After calling this req_to_install will only have satisfied_by set to
None if the req_to_install is to be upgraded/reinstalled etc. Any
other value will be a dist recording the current thing installed that
satisfies the requirement.
Note that for vcs urls and the like we can't assess skipping in this
routine - we simply identify that we need to pull the thing down,
then later on it is pulled down and introspected to assess upgrade/
reinstalls etc.
:return: A text reason for why it was skipped, or None.
"""
if self.ignore_installed:
return None
req_to_install.check_if_exists(self.use_user_site)
if not req_to_install.satisfied_by:
return None
if self.force_reinstall:
self._set_req_to_reinstall(req_to_install)
return None
if not self._is_upgrade_allowed(req_to_install):
if self.upgrade_strategy == "only-if-needed":
return 'already satisfied, skipping upgrade'
return 'already satisfied'
# Check for the possibility of an upgrade. For link-based
# requirements we have to pull the tree down and inspect to assess
# the version #, so it's handled way down.
if not req_to_install.link:
try:
self.finder.find_requirement(req_to_install, upgrade=True)
except BestVersionAlreadyInstalled:
# Then the best version is installed.
return 'already up-to-date'
except DistributionNotFound:
# No distribution found, so we squash the error. It will
# be raised later when we re-try later to do the install.
# Why don't we just raise here?
pass
self._set_req_to_reinstall(req_to_install)
return None
|
python
|
def _check_skip_installed(self, req_to_install):
# type: (InstallRequirement) -> Optional[str]
"""Check if req_to_install should be skipped.
This will check if the req is installed, and whether we should upgrade
or reinstall it, taking into account all the relevant user options.
After calling this req_to_install will only have satisfied_by set to
None if the req_to_install is to be upgraded/reinstalled etc. Any
other value will be a dist recording the current thing installed that
satisfies the requirement.
Note that for vcs urls and the like we can't assess skipping in this
routine - we simply identify that we need to pull the thing down,
then later on it is pulled down and introspected to assess upgrade/
reinstalls etc.
:return: A text reason for why it was skipped, or None.
"""
if self.ignore_installed:
return None
req_to_install.check_if_exists(self.use_user_site)
if not req_to_install.satisfied_by:
return None
if self.force_reinstall:
self._set_req_to_reinstall(req_to_install)
return None
if not self._is_upgrade_allowed(req_to_install):
if self.upgrade_strategy == "only-if-needed":
return 'already satisfied, skipping upgrade'
return 'already satisfied'
# Check for the possibility of an upgrade. For link-based
# requirements we have to pull the tree down and inspect to assess
# the version #, so it's handled way down.
if not req_to_install.link:
try:
self.finder.find_requirement(req_to_install, upgrade=True)
except BestVersionAlreadyInstalled:
# Then the best version is installed.
return 'already up-to-date'
except DistributionNotFound:
# No distribution found, so we squash the error. It will
# be raised later when we re-try later to do the install.
# Why don't we just raise here?
pass
self._set_req_to_reinstall(req_to_install)
return None
|
[
"def",
"_check_skip_installed",
"(",
"self",
",",
"req_to_install",
")",
":",
"# type: (InstallRequirement) -> Optional[str]",
"if",
"self",
".",
"ignore_installed",
":",
"return",
"None",
"req_to_install",
".",
"check_if_exists",
"(",
"self",
".",
"use_user_site",
")",
"if",
"not",
"req_to_install",
".",
"satisfied_by",
":",
"return",
"None",
"if",
"self",
".",
"force_reinstall",
":",
"self",
".",
"_set_req_to_reinstall",
"(",
"req_to_install",
")",
"return",
"None",
"if",
"not",
"self",
".",
"_is_upgrade_allowed",
"(",
"req_to_install",
")",
":",
"if",
"self",
".",
"upgrade_strategy",
"==",
"\"only-if-needed\"",
":",
"return",
"'already satisfied, skipping upgrade'",
"return",
"'already satisfied'",
"# Check for the possibility of an upgrade. For link-based",
"# requirements we have to pull the tree down and inspect to assess",
"# the version #, so it's handled way down.",
"if",
"not",
"req_to_install",
".",
"link",
":",
"try",
":",
"self",
".",
"finder",
".",
"find_requirement",
"(",
"req_to_install",
",",
"upgrade",
"=",
"True",
")",
"except",
"BestVersionAlreadyInstalled",
":",
"# Then the best version is installed.",
"return",
"'already up-to-date'",
"except",
"DistributionNotFound",
":",
"# No distribution found, so we squash the error. It will",
"# be raised later when we re-try later to do the install.",
"# Why don't we just raise here?",
"pass",
"self",
".",
"_set_req_to_reinstall",
"(",
"req_to_install",
")",
"return",
"None"
] |
Check if req_to_install should be skipped.
This will check if the req is installed, and whether we should upgrade
or reinstall it, taking into account all the relevant user options.
After calling this req_to_install will only have satisfied_by set to
None if the req_to_install is to be upgraded/reinstalled etc. Any
other value will be a dist recording the current thing installed that
satisfies the requirement.
Note that for vcs urls and the like we can't assess skipping in this
routine - we simply identify that we need to pull the thing down,
then later on it is pulled down and introspected to assess upgrade/
reinstalls etc.
:return: A text reason for why it was skipped, or None.
|
[
"Check",
"if",
"req_to_install",
"should",
"be",
"skipped",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/resolve.py#L168-L219
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/resolve.py
|
Resolver._get_abstract_dist_for
|
def _get_abstract_dist_for(self, req):
# type: (InstallRequirement) -> DistAbstraction
"""Takes a InstallRequirement and returns a single AbstractDist \
representing a prepared variant of the same.
"""
assert self.require_hashes is not None, (
"require_hashes should have been set in Resolver.resolve()"
)
if req.editable:
return self.preparer.prepare_editable_requirement(
req, self.require_hashes, self.use_user_site, self.finder,
)
# satisfied_by is only evaluated by calling _check_skip_installed,
# so it must be None here.
assert req.satisfied_by is None
skip_reason = self._check_skip_installed(req)
if req.satisfied_by:
return self.preparer.prepare_installed_requirement(
req, self.require_hashes, skip_reason
)
upgrade_allowed = self._is_upgrade_allowed(req)
abstract_dist = self.preparer.prepare_linked_requirement(
req, self.session, self.finder, upgrade_allowed,
self.require_hashes
)
# NOTE
# The following portion is for determining if a certain package is
# going to be re-installed/upgraded or not and reporting to the user.
# This should probably get cleaned up in a future refactor.
# req.req is only avail after unpack for URL
# pkgs repeat check_if_exists to uninstall-on-upgrade
# (#14)
if not self.ignore_installed:
req.check_if_exists(self.use_user_site)
if req.satisfied_by:
should_modify = (
self.upgrade_strategy != "to-satisfy-only" or
self.force_reinstall or
self.ignore_installed or
req.link.scheme == 'file'
)
if should_modify:
self._set_req_to_reinstall(req)
else:
logger.info(
'Requirement already satisfied (use --upgrade to upgrade):'
' %s', req,
)
return abstract_dist
|
python
|
def _get_abstract_dist_for(self, req):
# type: (InstallRequirement) -> DistAbstraction
"""Takes a InstallRequirement and returns a single AbstractDist \
representing a prepared variant of the same.
"""
assert self.require_hashes is not None, (
"require_hashes should have been set in Resolver.resolve()"
)
if req.editable:
return self.preparer.prepare_editable_requirement(
req, self.require_hashes, self.use_user_site, self.finder,
)
# satisfied_by is only evaluated by calling _check_skip_installed,
# so it must be None here.
assert req.satisfied_by is None
skip_reason = self._check_skip_installed(req)
if req.satisfied_by:
return self.preparer.prepare_installed_requirement(
req, self.require_hashes, skip_reason
)
upgrade_allowed = self._is_upgrade_allowed(req)
abstract_dist = self.preparer.prepare_linked_requirement(
req, self.session, self.finder, upgrade_allowed,
self.require_hashes
)
# NOTE
# The following portion is for determining if a certain package is
# going to be re-installed/upgraded or not and reporting to the user.
# This should probably get cleaned up in a future refactor.
# req.req is only avail after unpack for URL
# pkgs repeat check_if_exists to uninstall-on-upgrade
# (#14)
if not self.ignore_installed:
req.check_if_exists(self.use_user_site)
if req.satisfied_by:
should_modify = (
self.upgrade_strategy != "to-satisfy-only" or
self.force_reinstall or
self.ignore_installed or
req.link.scheme == 'file'
)
if should_modify:
self._set_req_to_reinstall(req)
else:
logger.info(
'Requirement already satisfied (use --upgrade to upgrade):'
' %s', req,
)
return abstract_dist
|
[
"def",
"_get_abstract_dist_for",
"(",
"self",
",",
"req",
")",
":",
"# type: (InstallRequirement) -> DistAbstraction",
"assert",
"self",
".",
"require_hashes",
"is",
"not",
"None",
",",
"(",
"\"require_hashes should have been set in Resolver.resolve()\"",
")",
"if",
"req",
".",
"editable",
":",
"return",
"self",
".",
"preparer",
".",
"prepare_editable_requirement",
"(",
"req",
",",
"self",
".",
"require_hashes",
",",
"self",
".",
"use_user_site",
",",
"self",
".",
"finder",
",",
")",
"# satisfied_by is only evaluated by calling _check_skip_installed,",
"# so it must be None here.",
"assert",
"req",
".",
"satisfied_by",
"is",
"None",
"skip_reason",
"=",
"self",
".",
"_check_skip_installed",
"(",
"req",
")",
"if",
"req",
".",
"satisfied_by",
":",
"return",
"self",
".",
"preparer",
".",
"prepare_installed_requirement",
"(",
"req",
",",
"self",
".",
"require_hashes",
",",
"skip_reason",
")",
"upgrade_allowed",
"=",
"self",
".",
"_is_upgrade_allowed",
"(",
"req",
")",
"abstract_dist",
"=",
"self",
".",
"preparer",
".",
"prepare_linked_requirement",
"(",
"req",
",",
"self",
".",
"session",
",",
"self",
".",
"finder",
",",
"upgrade_allowed",
",",
"self",
".",
"require_hashes",
")",
"# NOTE",
"# The following portion is for determining if a certain package is",
"# going to be re-installed/upgraded or not and reporting to the user.",
"# This should probably get cleaned up in a future refactor.",
"# req.req is only avail after unpack for URL",
"# pkgs repeat check_if_exists to uninstall-on-upgrade",
"# (#14)",
"if",
"not",
"self",
".",
"ignore_installed",
":",
"req",
".",
"check_if_exists",
"(",
"self",
".",
"use_user_site",
")",
"if",
"req",
".",
"satisfied_by",
":",
"should_modify",
"=",
"(",
"self",
".",
"upgrade_strategy",
"!=",
"\"to-satisfy-only\"",
"or",
"self",
".",
"force_reinstall",
"or",
"self",
".",
"ignore_installed",
"or",
"req",
".",
"link",
".",
"scheme",
"==",
"'file'",
")",
"if",
"should_modify",
":",
"self",
".",
"_set_req_to_reinstall",
"(",
"req",
")",
"else",
":",
"logger",
".",
"info",
"(",
"'Requirement already satisfied (use --upgrade to upgrade):'",
"' %s'",
",",
"req",
",",
")",
"return",
"abstract_dist"
] |
Takes a InstallRequirement and returns a single AbstractDist \
representing a prepared variant of the same.
|
[
"Takes",
"a",
"InstallRequirement",
"and",
"returns",
"a",
"single",
"AbstractDist",
"\\",
"representing",
"a",
"prepared",
"variant",
"of",
"the",
"same",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/resolve.py#L221-L277
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/resolve.py
|
Resolver._resolve_one
|
def _resolve_one(
self,
requirement_set, # type: RequirementSet
req_to_install, # type: InstallRequirement
ignore_requires_python=False # type: bool
):
# type: (...) -> List[InstallRequirement]
"""Prepare a single requirements file.
:return: A list of additional InstallRequirements to also install.
"""
# Tell user what we are doing for this requirement:
# obtain (editable), skipping, processing (local url), collecting
# (remote url or package name)
if req_to_install.constraint or req_to_install.prepared:
return []
req_to_install.prepared = True
# register tmp src for cleanup in case something goes wrong
requirement_set.reqs_to_cleanup.append(req_to_install)
abstract_dist = self._get_abstract_dist_for(req_to_install)
# Parse and return dependencies
dist = abstract_dist.dist()
try:
check_dist_requires_python(dist)
except UnsupportedPythonVersion as err:
if self.ignore_requires_python or ignore_requires_python or self.ignore_compatibility:
logger.warning(err.args[0])
else:
raise
# A huge hack, by Kenneth Reitz.
try:
self.requires_python = check_dist_requires_python(dist, absorb=False)
except TypeError:
self.requires_python = None
more_reqs = [] # type: List[InstallRequirement]
def add_req(subreq, extras_requested):
sub_install_req = install_req_from_req_string(
str(subreq),
req_to_install,
isolated=self.isolated,
wheel_cache=self.wheel_cache,
use_pep517=self.use_pep517
)
parent_req_name = req_to_install.name
to_scan_again, add_to_parent = requirement_set.add_requirement(
sub_install_req,
parent_req_name=parent_req_name,
extras_requested=extras_requested,
)
if parent_req_name and add_to_parent:
self._discovered_dependencies[parent_req_name].append(
add_to_parent
)
more_reqs.extend(to_scan_again)
with indent_log():
# We add req_to_install before its dependencies, so that we
# can refer to it when adding dependencies.
if not requirement_set.has_requirement(req_to_install.name):
available_requested = sorted(
set(dist.extras) & set(req_to_install.extras)
)
# 'unnamed' requirements will get added here
req_to_install.is_direct = True
requirement_set.add_requirement(
req_to_install, parent_req_name=None,
extras_requested=available_requested,
)
if not self.ignore_dependencies:
if req_to_install.extras:
logger.debug(
"Installing extra requirements: %r",
','.join(req_to_install.extras),
)
missing_requested = sorted(
set(req_to_install.extras) - set(dist.extras)
)
for missing in missing_requested:
logger.warning(
'%s does not provide the extra \'%s\'',
dist, missing
)
available_requested = sorted(
set(dist.extras) & set(req_to_install.extras)
)
for subreq in dist.requires(available_requested):
add_req(subreq, extras_requested=available_requested)
# Hack for deep-resolving extras.
for available in available_requested:
if hasattr(dist, '_DistInfoDistribution__dep_map'):
for req in dist._DistInfoDistribution__dep_map[available]:
req = InstallRequirement(
req,
req_to_install,
isolated=self.isolated,
wheel_cache=self.wheel_cache,
use_pep517=None
)
more_reqs.append(req)
if not req_to_install.editable and not req_to_install.satisfied_by:
# XXX: --no-install leads this to report 'Successfully
# downloaded' for only non-editable reqs, even though we took
# action on them.
requirement_set.successfully_downloaded.append(req_to_install)
return more_reqs
|
python
|
def _resolve_one(
self,
requirement_set, # type: RequirementSet
req_to_install, # type: InstallRequirement
ignore_requires_python=False # type: bool
):
# type: (...) -> List[InstallRequirement]
"""Prepare a single requirements file.
:return: A list of additional InstallRequirements to also install.
"""
# Tell user what we are doing for this requirement:
# obtain (editable), skipping, processing (local url), collecting
# (remote url or package name)
if req_to_install.constraint or req_to_install.prepared:
return []
req_to_install.prepared = True
# register tmp src for cleanup in case something goes wrong
requirement_set.reqs_to_cleanup.append(req_to_install)
abstract_dist = self._get_abstract_dist_for(req_to_install)
# Parse and return dependencies
dist = abstract_dist.dist()
try:
check_dist_requires_python(dist)
except UnsupportedPythonVersion as err:
if self.ignore_requires_python or ignore_requires_python or self.ignore_compatibility:
logger.warning(err.args[0])
else:
raise
# A huge hack, by Kenneth Reitz.
try:
self.requires_python = check_dist_requires_python(dist, absorb=False)
except TypeError:
self.requires_python = None
more_reqs = [] # type: List[InstallRequirement]
def add_req(subreq, extras_requested):
sub_install_req = install_req_from_req_string(
str(subreq),
req_to_install,
isolated=self.isolated,
wheel_cache=self.wheel_cache,
use_pep517=self.use_pep517
)
parent_req_name = req_to_install.name
to_scan_again, add_to_parent = requirement_set.add_requirement(
sub_install_req,
parent_req_name=parent_req_name,
extras_requested=extras_requested,
)
if parent_req_name and add_to_parent:
self._discovered_dependencies[parent_req_name].append(
add_to_parent
)
more_reqs.extend(to_scan_again)
with indent_log():
# We add req_to_install before its dependencies, so that we
# can refer to it when adding dependencies.
if not requirement_set.has_requirement(req_to_install.name):
available_requested = sorted(
set(dist.extras) & set(req_to_install.extras)
)
# 'unnamed' requirements will get added here
req_to_install.is_direct = True
requirement_set.add_requirement(
req_to_install, parent_req_name=None,
extras_requested=available_requested,
)
if not self.ignore_dependencies:
if req_to_install.extras:
logger.debug(
"Installing extra requirements: %r",
','.join(req_to_install.extras),
)
missing_requested = sorted(
set(req_to_install.extras) - set(dist.extras)
)
for missing in missing_requested:
logger.warning(
'%s does not provide the extra \'%s\'',
dist, missing
)
available_requested = sorted(
set(dist.extras) & set(req_to_install.extras)
)
for subreq in dist.requires(available_requested):
add_req(subreq, extras_requested=available_requested)
# Hack for deep-resolving extras.
for available in available_requested:
if hasattr(dist, '_DistInfoDistribution__dep_map'):
for req in dist._DistInfoDistribution__dep_map[available]:
req = InstallRequirement(
req,
req_to_install,
isolated=self.isolated,
wheel_cache=self.wheel_cache,
use_pep517=None
)
more_reqs.append(req)
if not req_to_install.editable and not req_to_install.satisfied_by:
# XXX: --no-install leads this to report 'Successfully
# downloaded' for only non-editable reqs, even though we took
# action on them.
requirement_set.successfully_downloaded.append(req_to_install)
return more_reqs
|
[
"def",
"_resolve_one",
"(",
"self",
",",
"requirement_set",
",",
"# type: RequirementSet",
"req_to_install",
",",
"# type: InstallRequirement",
"ignore_requires_python",
"=",
"False",
"# type: bool",
")",
":",
"# type: (...) -> List[InstallRequirement]",
"# Tell user what we are doing for this requirement:",
"# obtain (editable), skipping, processing (local url), collecting",
"# (remote url or package name)",
"if",
"req_to_install",
".",
"constraint",
"or",
"req_to_install",
".",
"prepared",
":",
"return",
"[",
"]",
"req_to_install",
".",
"prepared",
"=",
"True",
"# register tmp src for cleanup in case something goes wrong",
"requirement_set",
".",
"reqs_to_cleanup",
".",
"append",
"(",
"req_to_install",
")",
"abstract_dist",
"=",
"self",
".",
"_get_abstract_dist_for",
"(",
"req_to_install",
")",
"# Parse and return dependencies",
"dist",
"=",
"abstract_dist",
".",
"dist",
"(",
")",
"try",
":",
"check_dist_requires_python",
"(",
"dist",
")",
"except",
"UnsupportedPythonVersion",
"as",
"err",
":",
"if",
"self",
".",
"ignore_requires_python",
"or",
"ignore_requires_python",
"or",
"self",
".",
"ignore_compatibility",
":",
"logger",
".",
"warning",
"(",
"err",
".",
"args",
"[",
"0",
"]",
")",
"else",
":",
"raise",
"# A huge hack, by Kenneth Reitz.",
"try",
":",
"self",
".",
"requires_python",
"=",
"check_dist_requires_python",
"(",
"dist",
",",
"absorb",
"=",
"False",
")",
"except",
"TypeError",
":",
"self",
".",
"requires_python",
"=",
"None",
"more_reqs",
"=",
"[",
"]",
"# type: List[InstallRequirement]",
"def",
"add_req",
"(",
"subreq",
",",
"extras_requested",
")",
":",
"sub_install_req",
"=",
"install_req_from_req_string",
"(",
"str",
"(",
"subreq",
")",
",",
"req_to_install",
",",
"isolated",
"=",
"self",
".",
"isolated",
",",
"wheel_cache",
"=",
"self",
".",
"wheel_cache",
",",
"use_pep517",
"=",
"self",
".",
"use_pep517",
")",
"parent_req_name",
"=",
"req_to_install",
".",
"name",
"to_scan_again",
",",
"add_to_parent",
"=",
"requirement_set",
".",
"add_requirement",
"(",
"sub_install_req",
",",
"parent_req_name",
"=",
"parent_req_name",
",",
"extras_requested",
"=",
"extras_requested",
",",
")",
"if",
"parent_req_name",
"and",
"add_to_parent",
":",
"self",
".",
"_discovered_dependencies",
"[",
"parent_req_name",
"]",
".",
"append",
"(",
"add_to_parent",
")",
"more_reqs",
".",
"extend",
"(",
"to_scan_again",
")",
"with",
"indent_log",
"(",
")",
":",
"# We add req_to_install before its dependencies, so that we",
"# can refer to it when adding dependencies.",
"if",
"not",
"requirement_set",
".",
"has_requirement",
"(",
"req_to_install",
".",
"name",
")",
":",
"available_requested",
"=",
"sorted",
"(",
"set",
"(",
"dist",
".",
"extras",
")",
"&",
"set",
"(",
"req_to_install",
".",
"extras",
")",
")",
"# 'unnamed' requirements will get added here",
"req_to_install",
".",
"is_direct",
"=",
"True",
"requirement_set",
".",
"add_requirement",
"(",
"req_to_install",
",",
"parent_req_name",
"=",
"None",
",",
"extras_requested",
"=",
"available_requested",
",",
")",
"if",
"not",
"self",
".",
"ignore_dependencies",
":",
"if",
"req_to_install",
".",
"extras",
":",
"logger",
".",
"debug",
"(",
"\"Installing extra requirements: %r\"",
",",
"','",
".",
"join",
"(",
"req_to_install",
".",
"extras",
")",
",",
")",
"missing_requested",
"=",
"sorted",
"(",
"set",
"(",
"req_to_install",
".",
"extras",
")",
"-",
"set",
"(",
"dist",
".",
"extras",
")",
")",
"for",
"missing",
"in",
"missing_requested",
":",
"logger",
".",
"warning",
"(",
"'%s does not provide the extra \\'%s\\''",
",",
"dist",
",",
"missing",
")",
"available_requested",
"=",
"sorted",
"(",
"set",
"(",
"dist",
".",
"extras",
")",
"&",
"set",
"(",
"req_to_install",
".",
"extras",
")",
")",
"for",
"subreq",
"in",
"dist",
".",
"requires",
"(",
"available_requested",
")",
":",
"add_req",
"(",
"subreq",
",",
"extras_requested",
"=",
"available_requested",
")",
"# Hack for deep-resolving extras.",
"for",
"available",
"in",
"available_requested",
":",
"if",
"hasattr",
"(",
"dist",
",",
"'_DistInfoDistribution__dep_map'",
")",
":",
"for",
"req",
"in",
"dist",
".",
"_DistInfoDistribution__dep_map",
"[",
"available",
"]",
":",
"req",
"=",
"InstallRequirement",
"(",
"req",
",",
"req_to_install",
",",
"isolated",
"=",
"self",
".",
"isolated",
",",
"wheel_cache",
"=",
"self",
".",
"wheel_cache",
",",
"use_pep517",
"=",
"None",
")",
"more_reqs",
".",
"append",
"(",
"req",
")",
"if",
"not",
"req_to_install",
".",
"editable",
"and",
"not",
"req_to_install",
".",
"satisfied_by",
":",
"# XXX: --no-install leads this to report 'Successfully",
"# downloaded' for only non-editable reqs, even though we took",
"# action on them.",
"requirement_set",
".",
"successfully_downloaded",
".",
"append",
"(",
"req_to_install",
")",
"return",
"more_reqs"
] |
Prepare a single requirements file.
:return: A list of additional InstallRequirements to also install.
|
[
"Prepare",
"a",
"single",
"requirements",
"file",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/resolve.py#L279-L397
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/resolve.py
|
Resolver.get_installation_order
|
def get_installation_order(self, req_set):
# type: (RequirementSet) -> List[InstallRequirement]
"""Create the installation order.
The installation order is topological - requirements are installed
before the requiring thing. We break cycles at an arbitrary point,
and make no other guarantees.
"""
# The current implementation, which we may change at any point
# installs the user specified things in the order given, except when
# dependencies must come earlier to achieve topological order.
order = []
ordered_reqs = set() # type: Set[InstallRequirement]
def schedule(req):
if req.satisfied_by or req in ordered_reqs:
return
if req.constraint:
return
ordered_reqs.add(req)
for dep in self._discovered_dependencies[req.name]:
schedule(dep)
order.append(req)
for install_req in req_set.requirements.values():
schedule(install_req)
return order
|
python
|
def get_installation_order(self, req_set):
# type: (RequirementSet) -> List[InstallRequirement]
"""Create the installation order.
The installation order is topological - requirements are installed
before the requiring thing. We break cycles at an arbitrary point,
and make no other guarantees.
"""
# The current implementation, which we may change at any point
# installs the user specified things in the order given, except when
# dependencies must come earlier to achieve topological order.
order = []
ordered_reqs = set() # type: Set[InstallRequirement]
def schedule(req):
if req.satisfied_by or req in ordered_reqs:
return
if req.constraint:
return
ordered_reqs.add(req)
for dep in self._discovered_dependencies[req.name]:
schedule(dep)
order.append(req)
for install_req in req_set.requirements.values():
schedule(install_req)
return order
|
[
"def",
"get_installation_order",
"(",
"self",
",",
"req_set",
")",
":",
"# type: (RequirementSet) -> List[InstallRequirement]",
"# The current implementation, which we may change at any point",
"# installs the user specified things in the order given, except when",
"# dependencies must come earlier to achieve topological order.",
"order",
"=",
"[",
"]",
"ordered_reqs",
"=",
"set",
"(",
")",
"# type: Set[InstallRequirement]",
"def",
"schedule",
"(",
"req",
")",
":",
"if",
"req",
".",
"satisfied_by",
"or",
"req",
"in",
"ordered_reqs",
":",
"return",
"if",
"req",
".",
"constraint",
":",
"return",
"ordered_reqs",
".",
"add",
"(",
"req",
")",
"for",
"dep",
"in",
"self",
".",
"_discovered_dependencies",
"[",
"req",
".",
"name",
"]",
":",
"schedule",
"(",
"dep",
")",
"order",
".",
"append",
"(",
"req",
")",
"for",
"install_req",
"in",
"req_set",
".",
"requirements",
".",
"values",
"(",
")",
":",
"schedule",
"(",
"install_req",
")",
"return",
"order"
] |
Create the installation order.
The installation order is topological - requirements are installed
before the requiring thing. We break cycles at an arbitrary point,
and make no other guarantees.
|
[
"Create",
"the",
"installation",
"order",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/resolve.py#L399-L425
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
_xml_escape
|
def _xml_escape(data):
"""Escape &, <, >, ", ', etc. in a string of data."""
# ampersand must be replaced first
from_symbols = '&><"\''
to_symbols = ('&'+s+';' for s in "amp gt lt quot apos".split())
for from_,to_ in zip(from_symbols, to_symbols):
data = data.replace(from_, to_)
return data
|
python
|
def _xml_escape(data):
"""Escape &, <, >, ", ', etc. in a string of data."""
# ampersand must be replaced first
from_symbols = '&><"\''
to_symbols = ('&'+s+';' for s in "amp gt lt quot apos".split())
for from_,to_ in zip(from_symbols, to_symbols):
data = data.replace(from_, to_)
return data
|
[
"def",
"_xml_escape",
"(",
"data",
")",
":",
"# ampersand must be replaced first",
"from_symbols",
"=",
"'&><\"\\''",
"to_symbols",
"=",
"(",
"'&'",
"+",
"s",
"+",
"';'",
"for",
"s",
"in",
"\"amp gt lt quot apos\"",
".",
"split",
"(",
")",
")",
"for",
"from_",
",",
"to_",
"in",
"zip",
"(",
"from_symbols",
",",
"to_symbols",
")",
":",
"data",
"=",
"data",
".",
"replace",
"(",
"from_",
",",
"to_",
")",
"return",
"data"
] |
Escape &, <, >, ", ', etc. in a string of data.
|
[
"Escape",
"&",
"<",
">",
"etc",
".",
"in",
"a",
"string",
"of",
"data",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L219-L227
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
line
|
def line( loc, strg ):
"""Returns the line of text containing loc within a string, counting newlines as line separators.
"""
lastCR = strg.rfind("\n", 0, loc)
nextCR = strg.find("\n", loc)
if nextCR >= 0:
return strg[lastCR+1:nextCR]
else:
return strg[lastCR+1:]
|
python
|
def line( loc, strg ):
"""Returns the line of text containing loc within a string, counting newlines as line separators.
"""
lastCR = strg.rfind("\n", 0, loc)
nextCR = strg.find("\n", loc)
if nextCR >= 0:
return strg[lastCR+1:nextCR]
else:
return strg[lastCR+1:]
|
[
"def",
"line",
"(",
"loc",
",",
"strg",
")",
":",
"lastCR",
"=",
"strg",
".",
"rfind",
"(",
"\"\\n\"",
",",
"0",
",",
"loc",
")",
"nextCR",
"=",
"strg",
".",
"find",
"(",
"\"\\n\"",
",",
"loc",
")",
"if",
"nextCR",
">=",
"0",
":",
"return",
"strg",
"[",
"lastCR",
"+",
"1",
":",
"nextCR",
"]",
"else",
":",
"return",
"strg",
"[",
"lastCR",
"+",
"1",
":",
"]"
] |
Returns the line of text containing loc within a string, counting newlines as line separators.
|
[
"Returns",
"the",
"line",
"of",
"text",
"containing",
"loc",
"within",
"a",
"string",
"counting",
"newlines",
"as",
"line",
"separators",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L1124-L1132
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
traceParseAction
|
def traceParseAction(f):
"""Decorator for debugging parse actions.
When the parse action is called, this decorator will print
``">> entering method-name(line:<current_source_line>, <parse_location>, <matched_tokens>)"``.
When the parse action completes, the decorator will print
``"<<"`` followed by the returned value, or any exception that the parse action raised.
Example::
wd = Word(alphas)
@traceParseAction
def remove_duplicate_chars(tokens):
return ''.join(sorted(set(''.join(tokens))))
wds = OneOrMore(wd).setParseAction(remove_duplicate_chars)
print(wds.parseString("slkdjs sld sldd sdlf sdljf"))
prints::
>>entering remove_duplicate_chars(line: 'slkdjs sld sldd sdlf sdljf', 0, (['slkdjs', 'sld', 'sldd', 'sdlf', 'sdljf'], {}))
<<leaving remove_duplicate_chars (ret: 'dfjkls')
['dfjkls']
"""
f = _trim_arity(f)
def z(*paArgs):
thisFunc = f.__name__
s,l,t = paArgs[-3:]
if len(paArgs)>3:
thisFunc = paArgs[0].__class__.__name__ + '.' + thisFunc
sys.stderr.write( ">>entering %s(line: '%s', %d, %r)\n" % (thisFunc,line(l,s),l,t) )
try:
ret = f(*paArgs)
except Exception as exc:
sys.stderr.write( "<<leaving %s (exception: %s)\n" % (thisFunc,exc) )
raise
sys.stderr.write( "<<leaving %s (ret: %r)\n" % (thisFunc,ret) )
return ret
try:
z.__name__ = f.__name__
except AttributeError:
pass
return z
|
python
|
def traceParseAction(f):
"""Decorator for debugging parse actions.
When the parse action is called, this decorator will print
``">> entering method-name(line:<current_source_line>, <parse_location>, <matched_tokens>)"``.
When the parse action completes, the decorator will print
``"<<"`` followed by the returned value, or any exception that the parse action raised.
Example::
wd = Word(alphas)
@traceParseAction
def remove_duplicate_chars(tokens):
return ''.join(sorted(set(''.join(tokens))))
wds = OneOrMore(wd).setParseAction(remove_duplicate_chars)
print(wds.parseString("slkdjs sld sldd sdlf sdljf"))
prints::
>>entering remove_duplicate_chars(line: 'slkdjs sld sldd sdlf sdljf', 0, (['slkdjs', 'sld', 'sldd', 'sdlf', 'sdljf'], {}))
<<leaving remove_duplicate_chars (ret: 'dfjkls')
['dfjkls']
"""
f = _trim_arity(f)
def z(*paArgs):
thisFunc = f.__name__
s,l,t = paArgs[-3:]
if len(paArgs)>3:
thisFunc = paArgs[0].__class__.__name__ + '.' + thisFunc
sys.stderr.write( ">>entering %s(line: '%s', %d, %r)\n" % (thisFunc,line(l,s),l,t) )
try:
ret = f(*paArgs)
except Exception as exc:
sys.stderr.write( "<<leaving %s (exception: %s)\n" % (thisFunc,exc) )
raise
sys.stderr.write( "<<leaving %s (ret: %r)\n" % (thisFunc,ret) )
return ret
try:
z.__name__ = f.__name__
except AttributeError:
pass
return z
|
[
"def",
"traceParseAction",
"(",
"f",
")",
":",
"f",
"=",
"_trim_arity",
"(",
"f",
")",
"def",
"z",
"(",
"*",
"paArgs",
")",
":",
"thisFunc",
"=",
"f",
".",
"__name__",
"s",
",",
"l",
",",
"t",
"=",
"paArgs",
"[",
"-",
"3",
":",
"]",
"if",
"len",
"(",
"paArgs",
")",
">",
"3",
":",
"thisFunc",
"=",
"paArgs",
"[",
"0",
"]",
".",
"__class__",
".",
"__name__",
"+",
"'.'",
"+",
"thisFunc",
"sys",
".",
"stderr",
".",
"write",
"(",
"\">>entering %s(line: '%s', %d, %r)\\n\"",
"%",
"(",
"thisFunc",
",",
"line",
"(",
"l",
",",
"s",
")",
",",
"l",
",",
"t",
")",
")",
"try",
":",
"ret",
"=",
"f",
"(",
"*",
"paArgs",
")",
"except",
"Exception",
"as",
"exc",
":",
"sys",
".",
"stderr",
".",
"write",
"(",
"\"<<leaving %s (exception: %s)\\n\"",
"%",
"(",
"thisFunc",
",",
"exc",
")",
")",
"raise",
"sys",
".",
"stderr",
".",
"write",
"(",
"\"<<leaving %s (ret: %r)\\n\"",
"%",
"(",
"thisFunc",
",",
"ret",
")",
")",
"return",
"ret",
"try",
":",
"z",
".",
"__name__",
"=",
"f",
".",
"__name__",
"except",
"AttributeError",
":",
"pass",
"return",
"z"
] |
Decorator for debugging parse actions.
When the parse action is called, this decorator will print
``">> entering method-name(line:<current_source_line>, <parse_location>, <matched_tokens>)"``.
When the parse action completes, the decorator will print
``"<<"`` followed by the returned value, or any exception that the parse action raised.
Example::
wd = Word(alphas)
@traceParseAction
def remove_duplicate_chars(tokens):
return ''.join(sorted(set(''.join(tokens))))
wds = OneOrMore(wd).setParseAction(remove_duplicate_chars)
print(wds.parseString("slkdjs sld sldd sdlf sdljf"))
prints::
>>entering remove_duplicate_chars(line: 'slkdjs sld sldd sdlf sdljf', 0, (['slkdjs', 'sld', 'sldd', 'sdlf', 'sdljf'], {}))
<<leaving remove_duplicate_chars (ret: 'dfjkls')
['dfjkls']
|
[
"Decorator",
"for",
"debugging",
"parse",
"actions",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L4846-L4889
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
delimitedList
|
def delimitedList( expr, delim=",", combine=False ):
"""Helper to define a delimited list of expressions - the delimiter
defaults to ','. By default, the list elements and delimiters can
have intervening whitespace, and comments, but this can be
overridden by passing ``combine=True`` in the constructor. If
``combine`` is set to ``True``, the matching tokens are
returned as a single token string, with the delimiters included;
otherwise, the matching tokens are returned as a list of tokens,
with the delimiters suppressed.
Example::
delimitedList(Word(alphas)).parseString("aa,bb,cc") # -> ['aa', 'bb', 'cc']
delimitedList(Word(hexnums), delim=':', combine=True).parseString("AA:BB:CC:DD:EE") # -> ['AA:BB:CC:DD:EE']
"""
dlName = _ustr(expr)+" ["+_ustr(delim)+" "+_ustr(expr)+"]..."
if combine:
return Combine( expr + ZeroOrMore( delim + expr ) ).setName(dlName)
else:
return ( expr + ZeroOrMore( Suppress( delim ) + expr ) ).setName(dlName)
|
python
|
def delimitedList( expr, delim=",", combine=False ):
"""Helper to define a delimited list of expressions - the delimiter
defaults to ','. By default, the list elements and delimiters can
have intervening whitespace, and comments, but this can be
overridden by passing ``combine=True`` in the constructor. If
``combine`` is set to ``True``, the matching tokens are
returned as a single token string, with the delimiters included;
otherwise, the matching tokens are returned as a list of tokens,
with the delimiters suppressed.
Example::
delimitedList(Word(alphas)).parseString("aa,bb,cc") # -> ['aa', 'bb', 'cc']
delimitedList(Word(hexnums), delim=':', combine=True).parseString("AA:BB:CC:DD:EE") # -> ['AA:BB:CC:DD:EE']
"""
dlName = _ustr(expr)+" ["+_ustr(delim)+" "+_ustr(expr)+"]..."
if combine:
return Combine( expr + ZeroOrMore( delim + expr ) ).setName(dlName)
else:
return ( expr + ZeroOrMore( Suppress( delim ) + expr ) ).setName(dlName)
|
[
"def",
"delimitedList",
"(",
"expr",
",",
"delim",
"=",
"\",\"",
",",
"combine",
"=",
"False",
")",
":",
"dlName",
"=",
"_ustr",
"(",
"expr",
")",
"+",
"\" [\"",
"+",
"_ustr",
"(",
"delim",
")",
"+",
"\" \"",
"+",
"_ustr",
"(",
"expr",
")",
"+",
"\"]...\"",
"if",
"combine",
":",
"return",
"Combine",
"(",
"expr",
"+",
"ZeroOrMore",
"(",
"delim",
"+",
"expr",
")",
")",
".",
"setName",
"(",
"dlName",
")",
"else",
":",
"return",
"(",
"expr",
"+",
"ZeroOrMore",
"(",
"Suppress",
"(",
"delim",
")",
"+",
"expr",
")",
")",
".",
"setName",
"(",
"dlName",
")"
] |
Helper to define a delimited list of expressions - the delimiter
defaults to ','. By default, the list elements and delimiters can
have intervening whitespace, and comments, but this can be
overridden by passing ``combine=True`` in the constructor. If
``combine`` is set to ``True``, the matching tokens are
returned as a single token string, with the delimiters included;
otherwise, the matching tokens are returned as a list of tokens,
with the delimiters suppressed.
Example::
delimitedList(Word(alphas)).parseString("aa,bb,cc") # -> ['aa', 'bb', 'cc']
delimitedList(Word(hexnums), delim=':', combine=True).parseString("AA:BB:CC:DD:EE") # -> ['AA:BB:CC:DD:EE']
|
[
"Helper",
"to",
"define",
"a",
"delimited",
"list",
"of",
"expressions",
"-",
"the",
"delimiter",
"defaults",
"to",
".",
"By",
"default",
"the",
"list",
"elements",
"and",
"delimiters",
"can",
"have",
"intervening",
"whitespace",
"and",
"comments",
"but",
"this",
"can",
"be",
"overridden",
"by",
"passing",
"combine",
"=",
"True",
"in",
"the",
"constructor",
".",
"If",
"combine",
"is",
"set",
"to",
"True",
"the",
"matching",
"tokens",
"are",
"returned",
"as",
"a",
"single",
"token",
"string",
"with",
"the",
"delimiters",
"included",
";",
"otherwise",
"the",
"matching",
"tokens",
"are",
"returned",
"as",
"a",
"list",
"of",
"tokens",
"with",
"the",
"delimiters",
"suppressed",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L4894-L4913
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
originalTextFor
|
def originalTextFor(expr, asString=True):
"""Helper to return the original, untokenized text for a given
expression. Useful to restore the parsed fields of an HTML start
tag into the raw tag text itself, or to revert separate tokens with
intervening whitespace back to the original matching input text. By
default, returns astring containing the original parsed text.
If the optional ``asString`` argument is passed as
``False``, then the return value is
a :class:`ParseResults` containing any results names that
were originally matched, and a single token containing the original
matched text from the input string. So if the expression passed to
:class:`originalTextFor` contains expressions with defined
results names, you must set ``asString`` to ``False`` if you
want to preserve those results name values.
Example::
src = "this is test <b> bold <i>text</i> </b> normal text "
for tag in ("b","i"):
opener,closer = makeHTMLTags(tag)
patt = originalTextFor(opener + SkipTo(closer) + closer)
print(patt.searchString(src)[0])
prints::
['<b> bold <i>text</i> </b>']
['<i>text</i>']
"""
locMarker = Empty().setParseAction(lambda s,loc,t: loc)
endlocMarker = locMarker.copy()
endlocMarker.callPreparse = False
matchExpr = locMarker("_original_start") + expr + endlocMarker("_original_end")
if asString:
extractText = lambda s,l,t: s[t._original_start:t._original_end]
else:
def extractText(s,l,t):
t[:] = [s[t.pop('_original_start'):t.pop('_original_end')]]
matchExpr.setParseAction(extractText)
matchExpr.ignoreExprs = expr.ignoreExprs
return matchExpr
|
python
|
def originalTextFor(expr, asString=True):
"""Helper to return the original, untokenized text for a given
expression. Useful to restore the parsed fields of an HTML start
tag into the raw tag text itself, or to revert separate tokens with
intervening whitespace back to the original matching input text. By
default, returns astring containing the original parsed text.
If the optional ``asString`` argument is passed as
``False``, then the return value is
a :class:`ParseResults` containing any results names that
were originally matched, and a single token containing the original
matched text from the input string. So if the expression passed to
:class:`originalTextFor` contains expressions with defined
results names, you must set ``asString`` to ``False`` if you
want to preserve those results name values.
Example::
src = "this is test <b> bold <i>text</i> </b> normal text "
for tag in ("b","i"):
opener,closer = makeHTMLTags(tag)
patt = originalTextFor(opener + SkipTo(closer) + closer)
print(patt.searchString(src)[0])
prints::
['<b> bold <i>text</i> </b>']
['<i>text</i>']
"""
locMarker = Empty().setParseAction(lambda s,loc,t: loc)
endlocMarker = locMarker.copy()
endlocMarker.callPreparse = False
matchExpr = locMarker("_original_start") + expr + endlocMarker("_original_end")
if asString:
extractText = lambda s,l,t: s[t._original_start:t._original_end]
else:
def extractText(s,l,t):
t[:] = [s[t.pop('_original_start'):t.pop('_original_end')]]
matchExpr.setParseAction(extractText)
matchExpr.ignoreExprs = expr.ignoreExprs
return matchExpr
|
[
"def",
"originalTextFor",
"(",
"expr",
",",
"asString",
"=",
"True",
")",
":",
"locMarker",
"=",
"Empty",
"(",
")",
".",
"setParseAction",
"(",
"lambda",
"s",
",",
"loc",
",",
"t",
":",
"loc",
")",
"endlocMarker",
"=",
"locMarker",
".",
"copy",
"(",
")",
"endlocMarker",
".",
"callPreparse",
"=",
"False",
"matchExpr",
"=",
"locMarker",
"(",
"\"_original_start\"",
")",
"+",
"expr",
"+",
"endlocMarker",
"(",
"\"_original_end\"",
")",
"if",
"asString",
":",
"extractText",
"=",
"lambda",
"s",
",",
"l",
",",
"t",
":",
"s",
"[",
"t",
".",
"_original_start",
":",
"t",
".",
"_original_end",
"]",
"else",
":",
"def",
"extractText",
"(",
"s",
",",
"l",
",",
"t",
")",
":",
"t",
"[",
":",
"]",
"=",
"[",
"s",
"[",
"t",
".",
"pop",
"(",
"'_original_start'",
")",
":",
"t",
".",
"pop",
"(",
"'_original_end'",
")",
"]",
"]",
"matchExpr",
".",
"setParseAction",
"(",
"extractText",
")",
"matchExpr",
".",
"ignoreExprs",
"=",
"expr",
".",
"ignoreExprs",
"return",
"matchExpr"
] |
Helper to return the original, untokenized text for a given
expression. Useful to restore the parsed fields of an HTML start
tag into the raw tag text itself, or to revert separate tokens with
intervening whitespace back to the original matching input text. By
default, returns astring containing the original parsed text.
If the optional ``asString`` argument is passed as
``False``, then the return value is
a :class:`ParseResults` containing any results names that
were originally matched, and a single token containing the original
matched text from the input string. So if the expression passed to
:class:`originalTextFor` contains expressions with defined
results names, you must set ``asString`` to ``False`` if you
want to preserve those results name values.
Example::
src = "this is test <b> bold <i>text</i> </b> normal text "
for tag in ("b","i"):
opener,closer = makeHTMLTags(tag)
patt = originalTextFor(opener + SkipTo(closer) + closer)
print(patt.searchString(src)[0])
prints::
['<b> bold <i>text</i> </b>']
['<i>text</i>']
|
[
"Helper",
"to",
"return",
"the",
"original",
"untokenized",
"text",
"for",
"a",
"given",
"expression",
".",
"Useful",
"to",
"restore",
"the",
"parsed",
"fields",
"of",
"an",
"HTML",
"start",
"tag",
"into",
"the",
"raw",
"tag",
"text",
"itself",
"or",
"to",
"revert",
"separate",
"tokens",
"with",
"intervening",
"whitespace",
"back",
"to",
"the",
"original",
"matching",
"input",
"text",
".",
"By",
"default",
"returns",
"astring",
"containing",
"the",
"original",
"parsed",
"text",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L5146-L5186
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
locatedExpr
|
def locatedExpr(expr):
"""Helper to decorate a returned token with its starting and ending
locations in the input string.
This helper adds the following results names:
- locn_start = location where matched expression begins
- locn_end = location where matched expression ends
- value = the actual parsed results
Be careful if the input text contains ``<TAB>`` characters, you
may want to call :class:`ParserElement.parseWithTabs`
Example::
wd = Word(alphas)
for match in locatedExpr(wd).searchString("ljsdf123lksdjjf123lkkjj1222"):
print(match)
prints::
[[0, 'ljsdf', 5]]
[[8, 'lksdjjf', 15]]
[[18, 'lkkjj', 23]]
"""
locator = Empty().setParseAction(lambda s,l,t: l)
return Group(locator("locn_start") + expr("value") + locator.copy().leaveWhitespace()("locn_end"))
|
python
|
def locatedExpr(expr):
"""Helper to decorate a returned token with its starting and ending
locations in the input string.
This helper adds the following results names:
- locn_start = location where matched expression begins
- locn_end = location where matched expression ends
- value = the actual parsed results
Be careful if the input text contains ``<TAB>`` characters, you
may want to call :class:`ParserElement.parseWithTabs`
Example::
wd = Word(alphas)
for match in locatedExpr(wd).searchString("ljsdf123lksdjjf123lkkjj1222"):
print(match)
prints::
[[0, 'ljsdf', 5]]
[[8, 'lksdjjf', 15]]
[[18, 'lkkjj', 23]]
"""
locator = Empty().setParseAction(lambda s,l,t: l)
return Group(locator("locn_start") + expr("value") + locator.copy().leaveWhitespace()("locn_end"))
|
[
"def",
"locatedExpr",
"(",
"expr",
")",
":",
"locator",
"=",
"Empty",
"(",
")",
".",
"setParseAction",
"(",
"lambda",
"s",
",",
"l",
",",
"t",
":",
"l",
")",
"return",
"Group",
"(",
"locator",
"(",
"\"locn_start\"",
")",
"+",
"expr",
"(",
"\"value\"",
")",
"+",
"locator",
".",
"copy",
"(",
")",
".",
"leaveWhitespace",
"(",
")",
"(",
"\"locn_end\"",
")",
")"
] |
Helper to decorate a returned token with its starting and ending
locations in the input string.
This helper adds the following results names:
- locn_start = location where matched expression begins
- locn_end = location where matched expression ends
- value = the actual parsed results
Be careful if the input text contains ``<TAB>`` characters, you
may want to call :class:`ParserElement.parseWithTabs`
Example::
wd = Word(alphas)
for match in locatedExpr(wd).searchString("ljsdf123lksdjjf123lkkjj1222"):
print(match)
prints::
[[0, 'ljsdf', 5]]
[[8, 'lksdjjf', 15]]
[[18, 'lkkjj', 23]]
|
[
"Helper",
"to",
"decorate",
"a",
"returned",
"token",
"with",
"its",
"starting",
"and",
"ending",
"locations",
"in",
"the",
"input",
"string",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L5194-L5220
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
srange
|
def srange(s):
r"""Helper to easily define string ranges for use in Word
construction. Borrows syntax from regexp '[]' string range
definitions::
srange("[0-9]") -> "0123456789"
srange("[a-z]") -> "abcdefghijklmnopqrstuvwxyz"
srange("[a-z$_]") -> "abcdefghijklmnopqrstuvwxyz$_"
The input string must be enclosed in []'s, and the returned string
is the expanded character set joined into a single string. The
values enclosed in the []'s may be:
- a single character
- an escaped character with a leading backslash (such as ``\-``
or ``\]``)
- an escaped hex character with a leading ``'\x'``
(``\x21``, which is a ``'!'`` character) (``\0x##``
is also supported for backwards compatibility)
- an escaped octal character with a leading ``'\0'``
(``\041``, which is a ``'!'`` character)
- a range of any of the above, separated by a dash (``'a-z'``,
etc.)
- any combination of the above (``'aeiouy'``,
``'a-zA-Z0-9_$'``, etc.)
"""
_expanded = lambda p: p if not isinstance(p,ParseResults) else ''.join(unichr(c) for c in range(ord(p[0]),ord(p[1])+1))
try:
return "".join(_expanded(part) for part in _reBracketExpr.parseString(s).body)
except Exception:
return ""
|
python
|
def srange(s):
r"""Helper to easily define string ranges for use in Word
construction. Borrows syntax from regexp '[]' string range
definitions::
srange("[0-9]") -> "0123456789"
srange("[a-z]") -> "abcdefghijklmnopqrstuvwxyz"
srange("[a-z$_]") -> "abcdefghijklmnopqrstuvwxyz$_"
The input string must be enclosed in []'s, and the returned string
is the expanded character set joined into a single string. The
values enclosed in the []'s may be:
- a single character
- an escaped character with a leading backslash (such as ``\-``
or ``\]``)
- an escaped hex character with a leading ``'\x'``
(``\x21``, which is a ``'!'`` character) (``\0x##``
is also supported for backwards compatibility)
- an escaped octal character with a leading ``'\0'``
(``\041``, which is a ``'!'`` character)
- a range of any of the above, separated by a dash (``'a-z'``,
etc.)
- any combination of the above (``'aeiouy'``,
``'a-zA-Z0-9_$'``, etc.)
"""
_expanded = lambda p: p if not isinstance(p,ParseResults) else ''.join(unichr(c) for c in range(ord(p[0]),ord(p[1])+1))
try:
return "".join(_expanded(part) for part in _reBracketExpr.parseString(s).body)
except Exception:
return ""
|
[
"def",
"srange",
"(",
"s",
")",
":",
"_expanded",
"=",
"lambda",
"p",
":",
"p",
"if",
"not",
"isinstance",
"(",
"p",
",",
"ParseResults",
")",
"else",
"''",
".",
"join",
"(",
"unichr",
"(",
"c",
")",
"for",
"c",
"in",
"range",
"(",
"ord",
"(",
"p",
"[",
"0",
"]",
")",
",",
"ord",
"(",
"p",
"[",
"1",
"]",
")",
"+",
"1",
")",
")",
"try",
":",
"return",
"\"\"",
".",
"join",
"(",
"_expanded",
"(",
"part",
")",
"for",
"part",
"in",
"_reBracketExpr",
".",
"parseString",
"(",
"s",
")",
".",
"body",
")",
"except",
"Exception",
":",
"return",
"\"\""
] |
r"""Helper to easily define string ranges for use in Word
construction. Borrows syntax from regexp '[]' string range
definitions::
srange("[0-9]") -> "0123456789"
srange("[a-z]") -> "abcdefghijklmnopqrstuvwxyz"
srange("[a-z$_]") -> "abcdefghijklmnopqrstuvwxyz$_"
The input string must be enclosed in []'s, and the returned string
is the expanded character set joined into a single string. The
values enclosed in the []'s may be:
- a single character
- an escaped character with a leading backslash (such as ``\-``
or ``\]``)
- an escaped hex character with a leading ``'\x'``
(``\x21``, which is a ``'!'`` character) (``\0x##``
is also supported for backwards compatibility)
- an escaped octal character with a leading ``'\0'``
(``\041``, which is a ``'!'`` character)
- a range of any of the above, separated by a dash (``'a-z'``,
etc.)
- any combination of the above (``'aeiouy'``,
``'a-zA-Z0-9_$'``, etc.)
|
[
"r",
"Helper",
"to",
"easily",
"define",
"string",
"ranges",
"for",
"use",
"in",
"Word",
"construction",
".",
"Borrows",
"syntax",
"from",
"regexp",
"[]",
"string",
"range",
"definitions",
"::"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L5237-L5267
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
matchOnlyAtCol
|
def matchOnlyAtCol(n):
"""Helper method for defining parse actions that require matching at
a specific column in the input text.
"""
def verifyCol(strg,locn,toks):
if col(locn,strg) != n:
raise ParseException(strg,locn,"matched token not at column %d" % n)
return verifyCol
|
python
|
def matchOnlyAtCol(n):
"""Helper method for defining parse actions that require matching at
a specific column in the input text.
"""
def verifyCol(strg,locn,toks):
if col(locn,strg) != n:
raise ParseException(strg,locn,"matched token not at column %d" % n)
return verifyCol
|
[
"def",
"matchOnlyAtCol",
"(",
"n",
")",
":",
"def",
"verifyCol",
"(",
"strg",
",",
"locn",
",",
"toks",
")",
":",
"if",
"col",
"(",
"locn",
",",
"strg",
")",
"!=",
"n",
":",
"raise",
"ParseException",
"(",
"strg",
",",
"locn",
",",
"\"matched token not at column %d\"",
"%",
"n",
")",
"return",
"verifyCol"
] |
Helper method for defining parse actions that require matching at
a specific column in the input text.
|
[
"Helper",
"method",
"for",
"defining",
"parse",
"actions",
"that",
"require",
"matching",
"at",
"a",
"specific",
"column",
"in",
"the",
"input",
"text",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L5269-L5276
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
tokenMap
|
def tokenMap(func, *args):
"""Helper to define a parse action by mapping a function to all
elements of a ParseResults list. If any additional args are passed,
they are forwarded to the given function as additional arguments
after the token, as in
``hex_integer = Word(hexnums).setParseAction(tokenMap(int, 16))``,
which will convert the parsed data to an integer using base 16.
Example (compare the last to example in :class:`ParserElement.transformString`::
hex_ints = OneOrMore(Word(hexnums)).setParseAction(tokenMap(int, 16))
hex_ints.runTests('''
00 11 22 aa FF 0a 0d 1a
''')
upperword = Word(alphas).setParseAction(tokenMap(str.upper))
OneOrMore(upperword).runTests('''
my kingdom for a horse
''')
wd = Word(alphas).setParseAction(tokenMap(str.title))
OneOrMore(wd).setParseAction(' '.join).runTests('''
now is the winter of our discontent made glorious summer by this sun of york
''')
prints::
00 11 22 aa FF 0a 0d 1a
[0, 17, 34, 170, 255, 10, 13, 26]
my kingdom for a horse
['MY', 'KINGDOM', 'FOR', 'A', 'HORSE']
now is the winter of our discontent made glorious summer by this sun of york
['Now Is The Winter Of Our Discontent Made Glorious Summer By This Sun Of York']
"""
def pa(s,l,t):
return [func(tokn, *args) for tokn in t]
try:
func_name = getattr(func, '__name__',
getattr(func, '__class__').__name__)
except Exception:
func_name = str(func)
pa.__name__ = func_name
return pa
|
python
|
def tokenMap(func, *args):
"""Helper to define a parse action by mapping a function to all
elements of a ParseResults list. If any additional args are passed,
they are forwarded to the given function as additional arguments
after the token, as in
``hex_integer = Word(hexnums).setParseAction(tokenMap(int, 16))``,
which will convert the parsed data to an integer using base 16.
Example (compare the last to example in :class:`ParserElement.transformString`::
hex_ints = OneOrMore(Word(hexnums)).setParseAction(tokenMap(int, 16))
hex_ints.runTests('''
00 11 22 aa FF 0a 0d 1a
''')
upperword = Word(alphas).setParseAction(tokenMap(str.upper))
OneOrMore(upperword).runTests('''
my kingdom for a horse
''')
wd = Word(alphas).setParseAction(tokenMap(str.title))
OneOrMore(wd).setParseAction(' '.join).runTests('''
now is the winter of our discontent made glorious summer by this sun of york
''')
prints::
00 11 22 aa FF 0a 0d 1a
[0, 17, 34, 170, 255, 10, 13, 26]
my kingdom for a horse
['MY', 'KINGDOM', 'FOR', 'A', 'HORSE']
now is the winter of our discontent made glorious summer by this sun of york
['Now Is The Winter Of Our Discontent Made Glorious Summer By This Sun Of York']
"""
def pa(s,l,t):
return [func(tokn, *args) for tokn in t]
try:
func_name = getattr(func, '__name__',
getattr(func, '__class__').__name__)
except Exception:
func_name = str(func)
pa.__name__ = func_name
return pa
|
[
"def",
"tokenMap",
"(",
"func",
",",
"*",
"args",
")",
":",
"def",
"pa",
"(",
"s",
",",
"l",
",",
"t",
")",
":",
"return",
"[",
"func",
"(",
"tokn",
",",
"*",
"args",
")",
"for",
"tokn",
"in",
"t",
"]",
"try",
":",
"func_name",
"=",
"getattr",
"(",
"func",
",",
"'__name__'",
",",
"getattr",
"(",
"func",
",",
"'__class__'",
")",
".",
"__name__",
")",
"except",
"Exception",
":",
"func_name",
"=",
"str",
"(",
"func",
")",
"pa",
".",
"__name__",
"=",
"func_name",
"return",
"pa"
] |
Helper to define a parse action by mapping a function to all
elements of a ParseResults list. If any additional args are passed,
they are forwarded to the given function as additional arguments
after the token, as in
``hex_integer = Word(hexnums).setParseAction(tokenMap(int, 16))``,
which will convert the parsed data to an integer using base 16.
Example (compare the last to example in :class:`ParserElement.transformString`::
hex_ints = OneOrMore(Word(hexnums)).setParseAction(tokenMap(int, 16))
hex_ints.runTests('''
00 11 22 aa FF 0a 0d 1a
''')
upperword = Word(alphas).setParseAction(tokenMap(str.upper))
OneOrMore(upperword).runTests('''
my kingdom for a horse
''')
wd = Word(alphas).setParseAction(tokenMap(str.title))
OneOrMore(wd).setParseAction(' '.join).runTests('''
now is the winter of our discontent made glorious summer by this sun of york
''')
prints::
00 11 22 aa FF 0a 0d 1a
[0, 17, 34, 170, 255, 10, 13, 26]
my kingdom for a horse
['MY', 'KINGDOM', 'FOR', 'A', 'HORSE']
now is the winter of our discontent made glorious summer by this sun of york
['Now Is The Winter Of Our Discontent Made Glorious Summer By This Sun Of York']
|
[
"Helper",
"to",
"define",
"a",
"parse",
"action",
"by",
"mapping",
"a",
"function",
"to",
"all",
"elements",
"of",
"a",
"ParseResults",
"list",
".",
"If",
"any",
"additional",
"args",
"are",
"passed",
"they",
"are",
"forwarded",
"to",
"the",
"given",
"function",
"as",
"additional",
"arguments",
"after",
"the",
"token",
"as",
"in",
"hex_integer",
"=",
"Word",
"(",
"hexnums",
")",
".",
"setParseAction",
"(",
"tokenMap",
"(",
"int",
"16",
"))",
"which",
"will",
"convert",
"the",
"parsed",
"data",
"to",
"an",
"integer",
"using",
"base",
"16",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L5308-L5354
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
withAttribute
|
def withAttribute(*args,**attrDict):
"""Helper to create a validating parse action to be used with start
tags created with :class:`makeXMLTags` or
:class:`makeHTMLTags`. Use ``withAttribute`` to qualify
a starting tag with a required attribute value, to avoid false
matches on common tags such as ``<TD>`` or ``<DIV>``.
Call ``withAttribute`` with a series of attribute names and
values. Specify the list of filter attributes names and values as:
- keyword arguments, as in ``(align="right")``, or
- as an explicit dict with ``**`` operator, when an attribute
name is also a Python reserved word, as in ``**{"class":"Customer", "align":"right"}``
- a list of name-value tuples, as in ``(("ns1:class", "Customer"), ("ns2:align","right"))``
For attribute names with a namespace prefix, you must use the second
form. Attribute names are matched insensitive to upper/lower case.
If just testing for ``class`` (with or without a namespace), use
:class:`withClass`.
To verify that the attribute exists, but without specifying a value,
pass ``withAttribute.ANY_VALUE`` as the value.
Example::
html = '''
<div>
Some text
<div type="grid">1 4 0 1 0</div>
<div type="graph">1,3 2,3 1,1</div>
<div>this has no type</div>
</div>
'''
div,div_end = makeHTMLTags("div")
# only match div tag having a type attribute with value "grid"
div_grid = div().setParseAction(withAttribute(type="grid"))
grid_expr = div_grid + SkipTo(div | div_end)("body")
for grid_header in grid_expr.searchString(html):
print(grid_header.body)
# construct a match with any div tag having a type attribute, regardless of the value
div_any_type = div().setParseAction(withAttribute(type=withAttribute.ANY_VALUE))
div_expr = div_any_type + SkipTo(div | div_end)("body")
for div_header in div_expr.searchString(html):
print(div_header.body)
prints::
1 4 0 1 0
1 4 0 1 0
1,3 2,3 1,1
"""
if args:
attrs = args[:]
else:
attrs = attrDict.items()
attrs = [(k,v) for k,v in attrs]
def pa(s,l,tokens):
for attrName,attrValue in attrs:
if attrName not in tokens:
raise ParseException(s,l,"no matching attribute " + attrName)
if attrValue != withAttribute.ANY_VALUE and tokens[attrName] != attrValue:
raise ParseException(s,l,"attribute '%s' has value '%s', must be '%s'" %
(attrName, tokens[attrName], attrValue))
return pa
|
python
|
def withAttribute(*args,**attrDict):
"""Helper to create a validating parse action to be used with start
tags created with :class:`makeXMLTags` or
:class:`makeHTMLTags`. Use ``withAttribute`` to qualify
a starting tag with a required attribute value, to avoid false
matches on common tags such as ``<TD>`` or ``<DIV>``.
Call ``withAttribute`` with a series of attribute names and
values. Specify the list of filter attributes names and values as:
- keyword arguments, as in ``(align="right")``, or
- as an explicit dict with ``**`` operator, when an attribute
name is also a Python reserved word, as in ``**{"class":"Customer", "align":"right"}``
- a list of name-value tuples, as in ``(("ns1:class", "Customer"), ("ns2:align","right"))``
For attribute names with a namespace prefix, you must use the second
form. Attribute names are matched insensitive to upper/lower case.
If just testing for ``class`` (with or without a namespace), use
:class:`withClass`.
To verify that the attribute exists, but without specifying a value,
pass ``withAttribute.ANY_VALUE`` as the value.
Example::
html = '''
<div>
Some text
<div type="grid">1 4 0 1 0</div>
<div type="graph">1,3 2,3 1,1</div>
<div>this has no type</div>
</div>
'''
div,div_end = makeHTMLTags("div")
# only match div tag having a type attribute with value "grid"
div_grid = div().setParseAction(withAttribute(type="grid"))
grid_expr = div_grid + SkipTo(div | div_end)("body")
for grid_header in grid_expr.searchString(html):
print(grid_header.body)
# construct a match with any div tag having a type attribute, regardless of the value
div_any_type = div().setParseAction(withAttribute(type=withAttribute.ANY_VALUE))
div_expr = div_any_type + SkipTo(div | div_end)("body")
for div_header in div_expr.searchString(html):
print(div_header.body)
prints::
1 4 0 1 0
1 4 0 1 0
1,3 2,3 1,1
"""
if args:
attrs = args[:]
else:
attrs = attrDict.items()
attrs = [(k,v) for k,v in attrs]
def pa(s,l,tokens):
for attrName,attrValue in attrs:
if attrName not in tokens:
raise ParseException(s,l,"no matching attribute " + attrName)
if attrValue != withAttribute.ANY_VALUE and tokens[attrName] != attrValue:
raise ParseException(s,l,"attribute '%s' has value '%s', must be '%s'" %
(attrName, tokens[attrName], attrValue))
return pa
|
[
"def",
"withAttribute",
"(",
"*",
"args",
",",
"*",
"*",
"attrDict",
")",
":",
"if",
"args",
":",
"attrs",
"=",
"args",
"[",
":",
"]",
"else",
":",
"attrs",
"=",
"attrDict",
".",
"items",
"(",
")",
"attrs",
"=",
"[",
"(",
"k",
",",
"v",
")",
"for",
"k",
",",
"v",
"in",
"attrs",
"]",
"def",
"pa",
"(",
"s",
",",
"l",
",",
"tokens",
")",
":",
"for",
"attrName",
",",
"attrValue",
"in",
"attrs",
":",
"if",
"attrName",
"not",
"in",
"tokens",
":",
"raise",
"ParseException",
"(",
"s",
",",
"l",
",",
"\"no matching attribute \"",
"+",
"attrName",
")",
"if",
"attrValue",
"!=",
"withAttribute",
".",
"ANY_VALUE",
"and",
"tokens",
"[",
"attrName",
"]",
"!=",
"attrValue",
":",
"raise",
"ParseException",
"(",
"s",
",",
"l",
",",
"\"attribute '%s' has value '%s', must be '%s'\"",
"%",
"(",
"attrName",
",",
"tokens",
"[",
"attrName",
"]",
",",
"attrValue",
")",
")",
"return",
"pa"
] |
Helper to create a validating parse action to be used with start
tags created with :class:`makeXMLTags` or
:class:`makeHTMLTags`. Use ``withAttribute`` to qualify
a starting tag with a required attribute value, to avoid false
matches on common tags such as ``<TD>`` or ``<DIV>``.
Call ``withAttribute`` with a series of attribute names and
values. Specify the list of filter attributes names and values as:
- keyword arguments, as in ``(align="right")``, or
- as an explicit dict with ``**`` operator, when an attribute
name is also a Python reserved word, as in ``**{"class":"Customer", "align":"right"}``
- a list of name-value tuples, as in ``(("ns1:class", "Customer"), ("ns2:align","right"))``
For attribute names with a namespace prefix, you must use the second
form. Attribute names are matched insensitive to upper/lower case.
If just testing for ``class`` (with or without a namespace), use
:class:`withClass`.
To verify that the attribute exists, but without specifying a value,
pass ``withAttribute.ANY_VALUE`` as the value.
Example::
html = '''
<div>
Some text
<div type="grid">1 4 0 1 0</div>
<div type="graph">1,3 2,3 1,1</div>
<div>this has no type</div>
</div>
'''
div,div_end = makeHTMLTags("div")
# only match div tag having a type attribute with value "grid"
div_grid = div().setParseAction(withAttribute(type="grid"))
grid_expr = div_grid + SkipTo(div | div_end)("body")
for grid_header in grid_expr.searchString(html):
print(grid_header.body)
# construct a match with any div tag having a type attribute, regardless of the value
div_any_type = div().setParseAction(withAttribute(type=withAttribute.ANY_VALUE))
div_expr = div_any_type + SkipTo(div | div_end)("body")
for div_header in div_expr.searchString(html):
print(div_header.body)
prints::
1 4 0 1 0
1 4 0 1 0
1,3 2,3 1,1
|
[
"Helper",
"to",
"create",
"a",
"validating",
"parse",
"action",
"to",
"be",
"used",
"with",
"start",
"tags",
"created",
"with",
":",
"class",
":",
"makeXMLTags",
"or",
":",
"class",
":",
"makeHTMLTags",
".",
"Use",
"withAttribute",
"to",
"qualify",
"a",
"starting",
"tag",
"with",
"a",
"required",
"attribute",
"value",
"to",
"avoid",
"false",
"matches",
"on",
"common",
"tags",
"such",
"as",
"<TD",
">",
"or",
"<DIV",
">",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L5425-L5493
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
nestedExpr
|
def nestedExpr(opener="(", closer=")", content=None, ignoreExpr=quotedString.copy()):
"""Helper method for defining nested lists enclosed in opening and
closing delimiters ("(" and ")" are the default).
Parameters:
- opener - opening character for a nested list
(default= ``"("``); can also be a pyparsing expression
- closer - closing character for a nested list
(default= ``")"``); can also be a pyparsing expression
- content - expression for items within the nested lists
(default= ``None``)
- ignoreExpr - expression for ignoring opening and closing
delimiters (default= :class:`quotedString`)
If an expression is not provided for the content argument, the
nested expression will capture all whitespace-delimited content
between delimiters as a list of separate values.
Use the ``ignoreExpr`` argument to define expressions that may
contain opening or closing characters that should not be treated as
opening or closing characters for nesting, such as quotedString or
a comment expression. Specify multiple expressions using an
:class:`Or` or :class:`MatchFirst`. The default is
:class:`quotedString`, but if no expressions are to be ignored, then
pass ``None`` for this argument.
Example::
data_type = oneOf("void int short long char float double")
decl_data_type = Combine(data_type + Optional(Word('*')))
ident = Word(alphas+'_', alphanums+'_')
number = pyparsing_common.number
arg = Group(decl_data_type + ident)
LPAR,RPAR = map(Suppress, "()")
code_body = nestedExpr('{', '}', ignoreExpr=(quotedString | cStyleComment))
c_function = (decl_data_type("type")
+ ident("name")
+ LPAR + Optional(delimitedList(arg), [])("args") + RPAR
+ code_body("body"))
c_function.ignore(cStyleComment)
source_code = '''
int is_odd(int x) {
return (x%2);
}
int dec_to_hex(char hchar) {
if (hchar >= '0' && hchar <= '9') {
return (ord(hchar)-ord('0'));
} else {
return (10+ord(hchar)-ord('A'));
}
}
'''
for func in c_function.searchString(source_code):
print("%(name)s (%(type)s) args: %(args)s" % func)
prints::
is_odd (int) args: [['int', 'x']]
dec_to_hex (int) args: [['char', 'hchar']]
"""
if opener == closer:
raise ValueError("opening and closing strings cannot be the same")
if content is None:
if isinstance(opener,basestring) and isinstance(closer,basestring):
if len(opener) == 1 and len(closer)==1:
if ignoreExpr is not None:
content = (Combine(OneOrMore(~ignoreExpr +
CharsNotIn(opener+closer+ParserElement.DEFAULT_WHITE_CHARS,exact=1))
).setParseAction(lambda t:t[0].strip()))
else:
content = (empty.copy()+CharsNotIn(opener+closer+ParserElement.DEFAULT_WHITE_CHARS
).setParseAction(lambda t:t[0].strip()))
else:
if ignoreExpr is not None:
content = (Combine(OneOrMore(~ignoreExpr +
~Literal(opener) + ~Literal(closer) +
CharsNotIn(ParserElement.DEFAULT_WHITE_CHARS,exact=1))
).setParseAction(lambda t:t[0].strip()))
else:
content = (Combine(OneOrMore(~Literal(opener) + ~Literal(closer) +
CharsNotIn(ParserElement.DEFAULT_WHITE_CHARS,exact=1))
).setParseAction(lambda t:t[0].strip()))
else:
raise ValueError("opening and closing arguments must be strings if no content expression is given")
ret = Forward()
if ignoreExpr is not None:
ret <<= Group( Suppress(opener) + ZeroOrMore( ignoreExpr | ret | content ) + Suppress(closer) )
else:
ret <<= Group( Suppress(opener) + ZeroOrMore( ret | content ) + Suppress(closer) )
ret.setName('nested %s%s expression' % (opener,closer))
return ret
|
python
|
def nestedExpr(opener="(", closer=")", content=None, ignoreExpr=quotedString.copy()):
"""Helper method for defining nested lists enclosed in opening and
closing delimiters ("(" and ")" are the default).
Parameters:
- opener - opening character for a nested list
(default= ``"("``); can also be a pyparsing expression
- closer - closing character for a nested list
(default= ``")"``); can also be a pyparsing expression
- content - expression for items within the nested lists
(default= ``None``)
- ignoreExpr - expression for ignoring opening and closing
delimiters (default= :class:`quotedString`)
If an expression is not provided for the content argument, the
nested expression will capture all whitespace-delimited content
between delimiters as a list of separate values.
Use the ``ignoreExpr`` argument to define expressions that may
contain opening or closing characters that should not be treated as
opening or closing characters for nesting, such as quotedString or
a comment expression. Specify multiple expressions using an
:class:`Or` or :class:`MatchFirst`. The default is
:class:`quotedString`, but if no expressions are to be ignored, then
pass ``None`` for this argument.
Example::
data_type = oneOf("void int short long char float double")
decl_data_type = Combine(data_type + Optional(Word('*')))
ident = Word(alphas+'_', alphanums+'_')
number = pyparsing_common.number
arg = Group(decl_data_type + ident)
LPAR,RPAR = map(Suppress, "()")
code_body = nestedExpr('{', '}', ignoreExpr=(quotedString | cStyleComment))
c_function = (decl_data_type("type")
+ ident("name")
+ LPAR + Optional(delimitedList(arg), [])("args") + RPAR
+ code_body("body"))
c_function.ignore(cStyleComment)
source_code = '''
int is_odd(int x) {
return (x%2);
}
int dec_to_hex(char hchar) {
if (hchar >= '0' && hchar <= '9') {
return (ord(hchar)-ord('0'));
} else {
return (10+ord(hchar)-ord('A'));
}
}
'''
for func in c_function.searchString(source_code):
print("%(name)s (%(type)s) args: %(args)s" % func)
prints::
is_odd (int) args: [['int', 'x']]
dec_to_hex (int) args: [['char', 'hchar']]
"""
if opener == closer:
raise ValueError("opening and closing strings cannot be the same")
if content is None:
if isinstance(opener,basestring) and isinstance(closer,basestring):
if len(opener) == 1 and len(closer)==1:
if ignoreExpr is not None:
content = (Combine(OneOrMore(~ignoreExpr +
CharsNotIn(opener+closer+ParserElement.DEFAULT_WHITE_CHARS,exact=1))
).setParseAction(lambda t:t[0].strip()))
else:
content = (empty.copy()+CharsNotIn(opener+closer+ParserElement.DEFAULT_WHITE_CHARS
).setParseAction(lambda t:t[0].strip()))
else:
if ignoreExpr is not None:
content = (Combine(OneOrMore(~ignoreExpr +
~Literal(opener) + ~Literal(closer) +
CharsNotIn(ParserElement.DEFAULT_WHITE_CHARS,exact=1))
).setParseAction(lambda t:t[0].strip()))
else:
content = (Combine(OneOrMore(~Literal(opener) + ~Literal(closer) +
CharsNotIn(ParserElement.DEFAULT_WHITE_CHARS,exact=1))
).setParseAction(lambda t:t[0].strip()))
else:
raise ValueError("opening and closing arguments must be strings if no content expression is given")
ret = Forward()
if ignoreExpr is not None:
ret <<= Group( Suppress(opener) + ZeroOrMore( ignoreExpr | ret | content ) + Suppress(closer) )
else:
ret <<= Group( Suppress(opener) + ZeroOrMore( ret | content ) + Suppress(closer) )
ret.setName('nested %s%s expression' % (opener,closer))
return ret
|
[
"def",
"nestedExpr",
"(",
"opener",
"=",
"\"(\"",
",",
"closer",
"=",
"\")\"",
",",
"content",
"=",
"None",
",",
"ignoreExpr",
"=",
"quotedString",
".",
"copy",
"(",
")",
")",
":",
"if",
"opener",
"==",
"closer",
":",
"raise",
"ValueError",
"(",
"\"opening and closing strings cannot be the same\"",
")",
"if",
"content",
"is",
"None",
":",
"if",
"isinstance",
"(",
"opener",
",",
"basestring",
")",
"and",
"isinstance",
"(",
"closer",
",",
"basestring",
")",
":",
"if",
"len",
"(",
"opener",
")",
"==",
"1",
"and",
"len",
"(",
"closer",
")",
"==",
"1",
":",
"if",
"ignoreExpr",
"is",
"not",
"None",
":",
"content",
"=",
"(",
"Combine",
"(",
"OneOrMore",
"(",
"~",
"ignoreExpr",
"+",
"CharsNotIn",
"(",
"opener",
"+",
"closer",
"+",
"ParserElement",
".",
"DEFAULT_WHITE_CHARS",
",",
"exact",
"=",
"1",
")",
")",
")",
".",
"setParseAction",
"(",
"lambda",
"t",
":",
"t",
"[",
"0",
"]",
".",
"strip",
"(",
")",
")",
")",
"else",
":",
"content",
"=",
"(",
"empty",
".",
"copy",
"(",
")",
"+",
"CharsNotIn",
"(",
"opener",
"+",
"closer",
"+",
"ParserElement",
".",
"DEFAULT_WHITE_CHARS",
")",
".",
"setParseAction",
"(",
"lambda",
"t",
":",
"t",
"[",
"0",
"]",
".",
"strip",
"(",
")",
")",
")",
"else",
":",
"if",
"ignoreExpr",
"is",
"not",
"None",
":",
"content",
"=",
"(",
"Combine",
"(",
"OneOrMore",
"(",
"~",
"ignoreExpr",
"+",
"~",
"Literal",
"(",
"opener",
")",
"+",
"~",
"Literal",
"(",
"closer",
")",
"+",
"CharsNotIn",
"(",
"ParserElement",
".",
"DEFAULT_WHITE_CHARS",
",",
"exact",
"=",
"1",
")",
")",
")",
".",
"setParseAction",
"(",
"lambda",
"t",
":",
"t",
"[",
"0",
"]",
".",
"strip",
"(",
")",
")",
")",
"else",
":",
"content",
"=",
"(",
"Combine",
"(",
"OneOrMore",
"(",
"~",
"Literal",
"(",
"opener",
")",
"+",
"~",
"Literal",
"(",
"closer",
")",
"+",
"CharsNotIn",
"(",
"ParserElement",
".",
"DEFAULT_WHITE_CHARS",
",",
"exact",
"=",
"1",
")",
")",
")",
".",
"setParseAction",
"(",
"lambda",
"t",
":",
"t",
"[",
"0",
"]",
".",
"strip",
"(",
")",
")",
")",
"else",
":",
"raise",
"ValueError",
"(",
"\"opening and closing arguments must be strings if no content expression is given\"",
")",
"ret",
"=",
"Forward",
"(",
")",
"if",
"ignoreExpr",
"is",
"not",
"None",
":",
"ret",
"<<=",
"Group",
"(",
"Suppress",
"(",
"opener",
")",
"+",
"ZeroOrMore",
"(",
"ignoreExpr",
"|",
"ret",
"|",
"content",
")",
"+",
"Suppress",
"(",
"closer",
")",
")",
"else",
":",
"ret",
"<<=",
"Group",
"(",
"Suppress",
"(",
"opener",
")",
"+",
"ZeroOrMore",
"(",
"ret",
"|",
"content",
")",
"+",
"Suppress",
"(",
"closer",
")",
")",
"ret",
".",
"setName",
"(",
"'nested %s%s expression'",
"%",
"(",
"opener",
",",
"closer",
")",
")",
"return",
"ret"
] |
Helper method for defining nested lists enclosed in opening and
closing delimiters ("(" and ")" are the default).
Parameters:
- opener - opening character for a nested list
(default= ``"("``); can also be a pyparsing expression
- closer - closing character for a nested list
(default= ``")"``); can also be a pyparsing expression
- content - expression for items within the nested lists
(default= ``None``)
- ignoreExpr - expression for ignoring opening and closing
delimiters (default= :class:`quotedString`)
If an expression is not provided for the content argument, the
nested expression will capture all whitespace-delimited content
between delimiters as a list of separate values.
Use the ``ignoreExpr`` argument to define expressions that may
contain opening or closing characters that should not be treated as
opening or closing characters for nesting, such as quotedString or
a comment expression. Specify multiple expressions using an
:class:`Or` or :class:`MatchFirst`. The default is
:class:`quotedString`, but if no expressions are to be ignored, then
pass ``None`` for this argument.
Example::
data_type = oneOf("void int short long char float double")
decl_data_type = Combine(data_type + Optional(Word('*')))
ident = Word(alphas+'_', alphanums+'_')
number = pyparsing_common.number
arg = Group(decl_data_type + ident)
LPAR,RPAR = map(Suppress, "()")
code_body = nestedExpr('{', '}', ignoreExpr=(quotedString | cStyleComment))
c_function = (decl_data_type("type")
+ ident("name")
+ LPAR + Optional(delimitedList(arg), [])("args") + RPAR
+ code_body("body"))
c_function.ignore(cStyleComment)
source_code = '''
int is_odd(int x) {
return (x%2);
}
int dec_to_hex(char hchar) {
if (hchar >= '0' && hchar <= '9') {
return (ord(hchar)-ord('0'));
} else {
return (10+ord(hchar)-ord('A'));
}
}
'''
for func in c_function.searchString(source_code):
print("%(name)s (%(type)s) args: %(args)s" % func)
prints::
is_odd (int) args: [['int', 'x']]
dec_to_hex (int) args: [['char', 'hchar']]
|
[
"Helper",
"method",
"for",
"defining",
"nested",
"lists",
"enclosed",
"in",
"opening",
"and",
"closing",
"delimiters",
"(",
"(",
"and",
")",
"are",
"the",
"default",
")",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L5677-L5772
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParseBaseException._from_exception
|
def _from_exception(cls, pe):
"""
internal factory method to simplify creating one type of ParseException
from another - avoids having __init__ signature conflicts among subclasses
"""
return cls(pe.pstr, pe.loc, pe.msg, pe.parserElement)
|
python
|
def _from_exception(cls, pe):
"""
internal factory method to simplify creating one type of ParseException
from another - avoids having __init__ signature conflicts among subclasses
"""
return cls(pe.pstr, pe.loc, pe.msg, pe.parserElement)
|
[
"def",
"_from_exception",
"(",
"cls",
",",
"pe",
")",
":",
"return",
"cls",
"(",
"pe",
".",
"pstr",
",",
"pe",
".",
"loc",
",",
"pe",
".",
"msg",
",",
"pe",
".",
"parserElement",
")"
] |
internal factory method to simplify creating one type of ParseException
from another - avoids having __init__ signature conflicts among subclasses
|
[
"internal",
"factory",
"method",
"to",
"simplify",
"creating",
"one",
"type",
"of",
"ParseException",
"from",
"another",
"-",
"avoids",
"having",
"__init__",
"signature",
"conflicts",
"among",
"subclasses"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L252-L257
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParseBaseException.markInputline
|
def markInputline( self, markerString = ">!<" ):
"""Extracts the exception line from the input string, and marks
the location of the exception with a special symbol.
"""
line_str = self.line
line_column = self.column - 1
if markerString:
line_str = "".join((line_str[:line_column],
markerString, line_str[line_column:]))
return line_str.strip()
|
python
|
def markInputline( self, markerString = ">!<" ):
"""Extracts the exception line from the input string, and marks
the location of the exception with a special symbol.
"""
line_str = self.line
line_column = self.column - 1
if markerString:
line_str = "".join((line_str[:line_column],
markerString, line_str[line_column:]))
return line_str.strip()
|
[
"def",
"markInputline",
"(",
"self",
",",
"markerString",
"=",
"\">!<\"",
")",
":",
"line_str",
"=",
"self",
".",
"line",
"line_column",
"=",
"self",
".",
"column",
"-",
"1",
"if",
"markerString",
":",
"line_str",
"=",
"\"\"",
".",
"join",
"(",
"(",
"line_str",
"[",
":",
"line_column",
"]",
",",
"markerString",
",",
"line_str",
"[",
"line_column",
":",
"]",
")",
")",
"return",
"line_str",
".",
"strip",
"(",
")"
] |
Extracts the exception line from the input string, and marks
the location of the exception with a special symbol.
|
[
"Extracts",
"the",
"exception",
"line",
"from",
"the",
"input",
"string",
"and",
"marks",
"the",
"location",
"of",
"the",
"exception",
"with",
"a",
"special",
"symbol",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L279-L288
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParseException.explain
|
def explain(exc, depth=16):
"""
Method to take an exception and translate the Python internal traceback into a list
of the pyparsing expressions that caused the exception to be raised.
Parameters:
- exc - exception raised during parsing (need not be a ParseException, in support
of Python exceptions that might be raised in a parse action)
- depth (default=16) - number of levels back in the stack trace to list expression
and function names; if None, the full stack trace names will be listed; if 0, only
the failing input line, marker, and exception string will be shown
Returns a multi-line string listing the ParserElements and/or function names in the
exception's stack trace.
Note: the diagnostic output will include string representations of the expressions
that failed to parse. These representations will be more helpful if you use `setName` to
give identifiable names to your expressions. Otherwise they will use the default string
forms, which may be cryptic to read.
explain() is only supported under Python 3.
"""
import inspect
if depth is None:
depth = sys.getrecursionlimit()
ret = []
if isinstance(exc, ParseBaseException):
ret.append(exc.line)
ret.append(' ' * (exc.col - 1) + '^')
ret.append("{0}: {1}".format(type(exc).__name__, exc))
if depth > 0:
callers = inspect.getinnerframes(exc.__traceback__, context=depth)
seen = set()
for i, ff in enumerate(callers[-depth:]):
frm = ff.frame
f_self = frm.f_locals.get('self', None)
if isinstance(f_self, ParserElement):
if frm.f_code.co_name not in ('parseImpl', '_parseNoCache'):
continue
if f_self in seen:
continue
seen.add(f_self)
self_type = type(f_self)
ret.append("{0}.{1} - {2}".format(self_type.__module__,
self_type.__name__,
f_self))
elif f_self is not None:
self_type = type(f_self)
ret.append("{0}.{1}".format(self_type.__module__,
self_type.__name__))
else:
code = frm.f_code
if code.co_name in ('wrapper', '<module>'):
continue
ret.append("{0}".format(code.co_name))
depth -= 1
if not depth:
break
return '\n'.join(ret)
|
python
|
def explain(exc, depth=16):
"""
Method to take an exception and translate the Python internal traceback into a list
of the pyparsing expressions that caused the exception to be raised.
Parameters:
- exc - exception raised during parsing (need not be a ParseException, in support
of Python exceptions that might be raised in a parse action)
- depth (default=16) - number of levels back in the stack trace to list expression
and function names; if None, the full stack trace names will be listed; if 0, only
the failing input line, marker, and exception string will be shown
Returns a multi-line string listing the ParserElements and/or function names in the
exception's stack trace.
Note: the diagnostic output will include string representations of the expressions
that failed to parse. These representations will be more helpful if you use `setName` to
give identifiable names to your expressions. Otherwise they will use the default string
forms, which may be cryptic to read.
explain() is only supported under Python 3.
"""
import inspect
if depth is None:
depth = sys.getrecursionlimit()
ret = []
if isinstance(exc, ParseBaseException):
ret.append(exc.line)
ret.append(' ' * (exc.col - 1) + '^')
ret.append("{0}: {1}".format(type(exc).__name__, exc))
if depth > 0:
callers = inspect.getinnerframes(exc.__traceback__, context=depth)
seen = set()
for i, ff in enumerate(callers[-depth:]):
frm = ff.frame
f_self = frm.f_locals.get('self', None)
if isinstance(f_self, ParserElement):
if frm.f_code.co_name not in ('parseImpl', '_parseNoCache'):
continue
if f_self in seen:
continue
seen.add(f_self)
self_type = type(f_self)
ret.append("{0}.{1} - {2}".format(self_type.__module__,
self_type.__name__,
f_self))
elif f_self is not None:
self_type = type(f_self)
ret.append("{0}.{1}".format(self_type.__module__,
self_type.__name__))
else:
code = frm.f_code
if code.co_name in ('wrapper', '<module>'):
continue
ret.append("{0}".format(code.co_name))
depth -= 1
if not depth:
break
return '\n'.join(ret)
|
[
"def",
"explain",
"(",
"exc",
",",
"depth",
"=",
"16",
")",
":",
"import",
"inspect",
"if",
"depth",
"is",
"None",
":",
"depth",
"=",
"sys",
".",
"getrecursionlimit",
"(",
")",
"ret",
"=",
"[",
"]",
"if",
"isinstance",
"(",
"exc",
",",
"ParseBaseException",
")",
":",
"ret",
".",
"append",
"(",
"exc",
".",
"line",
")",
"ret",
".",
"append",
"(",
"' '",
"*",
"(",
"exc",
".",
"col",
"-",
"1",
")",
"+",
"'^'",
")",
"ret",
".",
"append",
"(",
"\"{0}: {1}\"",
".",
"format",
"(",
"type",
"(",
"exc",
")",
".",
"__name__",
",",
"exc",
")",
")",
"if",
"depth",
">",
"0",
":",
"callers",
"=",
"inspect",
".",
"getinnerframes",
"(",
"exc",
".",
"__traceback__",
",",
"context",
"=",
"depth",
")",
"seen",
"=",
"set",
"(",
")",
"for",
"i",
",",
"ff",
"in",
"enumerate",
"(",
"callers",
"[",
"-",
"depth",
":",
"]",
")",
":",
"frm",
"=",
"ff",
".",
"frame",
"f_self",
"=",
"frm",
".",
"f_locals",
".",
"get",
"(",
"'self'",
",",
"None",
")",
"if",
"isinstance",
"(",
"f_self",
",",
"ParserElement",
")",
":",
"if",
"frm",
".",
"f_code",
".",
"co_name",
"not",
"in",
"(",
"'parseImpl'",
",",
"'_parseNoCache'",
")",
":",
"continue",
"if",
"f_self",
"in",
"seen",
":",
"continue",
"seen",
".",
"add",
"(",
"f_self",
")",
"self_type",
"=",
"type",
"(",
"f_self",
")",
"ret",
".",
"append",
"(",
"\"{0}.{1} - {2}\"",
".",
"format",
"(",
"self_type",
".",
"__module__",
",",
"self_type",
".",
"__name__",
",",
"f_self",
")",
")",
"elif",
"f_self",
"is",
"not",
"None",
":",
"self_type",
"=",
"type",
"(",
"f_self",
")",
"ret",
".",
"append",
"(",
"\"{0}.{1}\"",
".",
"format",
"(",
"self_type",
".",
"__module__",
",",
"self_type",
".",
"__name__",
")",
")",
"else",
":",
"code",
"=",
"frm",
".",
"f_code",
"if",
"code",
".",
"co_name",
"in",
"(",
"'wrapper'",
",",
"'<module>'",
")",
":",
"continue",
"ret",
".",
"append",
"(",
"\"{0}\"",
".",
"format",
"(",
"code",
".",
"co_name",
")",
")",
"depth",
"-=",
"1",
"if",
"not",
"depth",
":",
"break",
"return",
"'\\n'",
".",
"join",
"(",
"ret",
")"
] |
Method to take an exception and translate the Python internal traceback into a list
of the pyparsing expressions that caused the exception to be raised.
Parameters:
- exc - exception raised during parsing (need not be a ParseException, in support
of Python exceptions that might be raised in a parse action)
- depth (default=16) - number of levels back in the stack trace to list expression
and function names; if None, the full stack trace names will be listed; if 0, only
the failing input line, marker, and exception string will be shown
Returns a multi-line string listing the ParserElements and/or function names in the
exception's stack trace.
Note: the diagnostic output will include string representations of the expressions
that failed to parse. These representations will be more helpful if you use `setName` to
give identifiable names to your expressions. Otherwise they will use the default string
forms, which may be cryptic to read.
explain() is only supported under Python 3.
|
[
"Method",
"to",
"take",
"an",
"exception",
"and",
"translate",
"the",
"Python",
"internal",
"traceback",
"into",
"a",
"list",
"of",
"the",
"pyparsing",
"expressions",
"that",
"caused",
"the",
"exception",
"to",
"be",
"raised",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L316-L382
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParseResults.pop
|
def pop( self, *args, **kwargs):
"""
Removes and returns item at specified index (default= ``last``).
Supports both ``list`` and ``dict`` semantics for ``pop()``. If
passed no argument or an integer argument, it will use ``list``
semantics and pop tokens from the list of parsed tokens. If passed
a non-integer argument (most likely a string), it will use ``dict``
semantics and pop the corresponding value from any defined results
names. A second default return value argument is supported, just as in
``dict.pop()``.
Example::
def remove_first(tokens):
tokens.pop(0)
print(OneOrMore(Word(nums)).parseString("0 123 321")) # -> ['0', '123', '321']
print(OneOrMore(Word(nums)).addParseAction(remove_first).parseString("0 123 321")) # -> ['123', '321']
label = Word(alphas)
patt = label("LABEL") + OneOrMore(Word(nums))
print(patt.parseString("AAB 123 321").dump())
# Use pop() in a parse action to remove named result (note that corresponding value is not
# removed from list form of results)
def remove_LABEL(tokens):
tokens.pop("LABEL")
return tokens
patt.addParseAction(remove_LABEL)
print(patt.parseString("AAB 123 321").dump())
prints::
['AAB', '123', '321']
- LABEL: AAB
['AAB', '123', '321']
"""
if not args:
args = [-1]
for k,v in kwargs.items():
if k == 'default':
args = (args[0], v)
else:
raise TypeError("pop() got an unexpected keyword argument '%s'" % k)
if (isinstance(args[0], int) or
len(args) == 1 or
args[0] in self):
index = args[0]
ret = self[index]
del self[index]
return ret
else:
defaultvalue = args[1]
return defaultvalue
|
python
|
def pop( self, *args, **kwargs):
"""
Removes and returns item at specified index (default= ``last``).
Supports both ``list`` and ``dict`` semantics for ``pop()``. If
passed no argument or an integer argument, it will use ``list``
semantics and pop tokens from the list of parsed tokens. If passed
a non-integer argument (most likely a string), it will use ``dict``
semantics and pop the corresponding value from any defined results
names. A second default return value argument is supported, just as in
``dict.pop()``.
Example::
def remove_first(tokens):
tokens.pop(0)
print(OneOrMore(Word(nums)).parseString("0 123 321")) # -> ['0', '123', '321']
print(OneOrMore(Word(nums)).addParseAction(remove_first).parseString("0 123 321")) # -> ['123', '321']
label = Word(alphas)
patt = label("LABEL") + OneOrMore(Word(nums))
print(patt.parseString("AAB 123 321").dump())
# Use pop() in a parse action to remove named result (note that corresponding value is not
# removed from list form of results)
def remove_LABEL(tokens):
tokens.pop("LABEL")
return tokens
patt.addParseAction(remove_LABEL)
print(patt.parseString("AAB 123 321").dump())
prints::
['AAB', '123', '321']
- LABEL: AAB
['AAB', '123', '321']
"""
if not args:
args = [-1]
for k,v in kwargs.items():
if k == 'default':
args = (args[0], v)
else:
raise TypeError("pop() got an unexpected keyword argument '%s'" % k)
if (isinstance(args[0], int) or
len(args) == 1 or
args[0] in self):
index = args[0]
ret = self[index]
del self[index]
return ret
else:
defaultvalue = args[1]
return defaultvalue
|
[
"def",
"pop",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"not",
"args",
":",
"args",
"=",
"[",
"-",
"1",
"]",
"for",
"k",
",",
"v",
"in",
"kwargs",
".",
"items",
"(",
")",
":",
"if",
"k",
"==",
"'default'",
":",
"args",
"=",
"(",
"args",
"[",
"0",
"]",
",",
"v",
")",
"else",
":",
"raise",
"TypeError",
"(",
"\"pop() got an unexpected keyword argument '%s'\"",
"%",
"k",
")",
"if",
"(",
"isinstance",
"(",
"args",
"[",
"0",
"]",
",",
"int",
")",
"or",
"len",
"(",
"args",
")",
"==",
"1",
"or",
"args",
"[",
"0",
"]",
"in",
"self",
")",
":",
"index",
"=",
"args",
"[",
"0",
"]",
"ret",
"=",
"self",
"[",
"index",
"]",
"del",
"self",
"[",
"index",
"]",
"return",
"ret",
"else",
":",
"defaultvalue",
"=",
"args",
"[",
"1",
"]",
"return",
"defaultvalue"
] |
Removes and returns item at specified index (default= ``last``).
Supports both ``list`` and ``dict`` semantics for ``pop()``. If
passed no argument or an integer argument, it will use ``list``
semantics and pop tokens from the list of parsed tokens. If passed
a non-integer argument (most likely a string), it will use ``dict``
semantics and pop the corresponding value from any defined results
names. A second default return value argument is supported, just as in
``dict.pop()``.
Example::
def remove_first(tokens):
tokens.pop(0)
print(OneOrMore(Word(nums)).parseString("0 123 321")) # -> ['0', '123', '321']
print(OneOrMore(Word(nums)).addParseAction(remove_first).parseString("0 123 321")) # -> ['123', '321']
label = Word(alphas)
patt = label("LABEL") + OneOrMore(Word(nums))
print(patt.parseString("AAB 123 321").dump())
# Use pop() in a parse action to remove named result (note that corresponding value is not
# removed from list form of results)
def remove_LABEL(tokens):
tokens.pop("LABEL")
return tokens
patt.addParseAction(remove_LABEL)
print(patt.parseString("AAB 123 321").dump())
prints::
['AAB', '123', '321']
- LABEL: AAB
['AAB', '123', '321']
|
[
"Removes",
"and",
"returns",
"item",
"at",
"specified",
"index",
"(",
"default",
"=",
"last",
")",
".",
"Supports",
"both",
"list",
"and",
"dict",
"semantics",
"for",
"pop",
"()",
".",
"If",
"passed",
"no",
"argument",
"or",
"an",
"integer",
"argument",
"it",
"will",
"use",
"list",
"semantics",
"and",
"pop",
"tokens",
"from",
"the",
"list",
"of",
"parsed",
"tokens",
".",
"If",
"passed",
"a",
"non",
"-",
"integer",
"argument",
"(",
"most",
"likely",
"a",
"string",
")",
"it",
"will",
"use",
"dict",
"semantics",
"and",
"pop",
"the",
"corresponding",
"value",
"from",
"any",
"defined",
"results",
"names",
".",
"A",
"second",
"default",
"return",
"value",
"argument",
"is",
"supported",
"just",
"as",
"in",
"dict",
".",
"pop",
"()",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L622-L675
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParseResults.extend
|
def extend( self, itemseq ):
"""
Add sequence of elements to end of ParseResults list of elements.
Example::
patt = OneOrMore(Word(alphas))
# use a parse action to append the reverse of the matched strings, to make a palindrome
def make_palindrome(tokens):
tokens.extend(reversed([t[::-1] for t in tokens]))
return ''.join(tokens)
print(patt.addParseAction(make_palindrome).parseString("lskdj sdlkjf lksd")) # -> 'lskdjsdlkjflksddsklfjkldsjdksl'
"""
if isinstance(itemseq, ParseResults):
self += itemseq
else:
self.__toklist.extend(itemseq)
|
python
|
def extend( self, itemseq ):
"""
Add sequence of elements to end of ParseResults list of elements.
Example::
patt = OneOrMore(Word(alphas))
# use a parse action to append the reverse of the matched strings, to make a palindrome
def make_palindrome(tokens):
tokens.extend(reversed([t[::-1] for t in tokens]))
return ''.join(tokens)
print(patt.addParseAction(make_palindrome).parseString("lskdj sdlkjf lksd")) # -> 'lskdjsdlkjflksddsklfjkldsjdksl'
"""
if isinstance(itemseq, ParseResults):
self += itemseq
else:
self.__toklist.extend(itemseq)
|
[
"def",
"extend",
"(",
"self",
",",
"itemseq",
")",
":",
"if",
"isinstance",
"(",
"itemseq",
",",
"ParseResults",
")",
":",
"self",
"+=",
"itemseq",
"else",
":",
"self",
".",
"__toklist",
".",
"extend",
"(",
"itemseq",
")"
] |
Add sequence of elements to end of ParseResults list of elements.
Example::
patt = OneOrMore(Word(alphas))
# use a parse action to append the reverse of the matched strings, to make a palindrome
def make_palindrome(tokens):
tokens.extend(reversed([t[::-1] for t in tokens]))
return ''.join(tokens)
print(patt.addParseAction(make_palindrome).parseString("lskdj sdlkjf lksd")) # -> 'lskdjsdlkjflksddsklfjkldsjdksl'
|
[
"Add",
"sequence",
"of",
"elements",
"to",
"end",
"of",
"ParseResults",
"list",
"of",
"elements",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L736-L753
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParseResults.asList
|
def asList( self ):
"""
Returns the parse results as a nested list of matching tokens, all converted to strings.
Example::
patt = OneOrMore(Word(alphas))
result = patt.parseString("sldkj lsdkj sldkj")
# even though the result prints in string-like form, it is actually a pyparsing ParseResults
print(type(result), result) # -> <class 'pyparsing.ParseResults'> ['sldkj', 'lsdkj', 'sldkj']
# Use asList() to create an actual list
result_list = result.asList()
print(type(result_list), result_list) # -> <class 'list'> ['sldkj', 'lsdkj', 'sldkj']
"""
return [res.asList() if isinstance(res,ParseResults) else res for res in self.__toklist]
|
python
|
def asList( self ):
"""
Returns the parse results as a nested list of matching tokens, all converted to strings.
Example::
patt = OneOrMore(Word(alphas))
result = patt.parseString("sldkj lsdkj sldkj")
# even though the result prints in string-like form, it is actually a pyparsing ParseResults
print(type(result), result) # -> <class 'pyparsing.ParseResults'> ['sldkj', 'lsdkj', 'sldkj']
# Use asList() to create an actual list
result_list = result.asList()
print(type(result_list), result_list) # -> <class 'list'> ['sldkj', 'lsdkj', 'sldkj']
"""
return [res.asList() if isinstance(res,ParseResults) else res for res in self.__toklist]
|
[
"def",
"asList",
"(",
"self",
")",
":",
"return",
"[",
"res",
".",
"asList",
"(",
")",
"if",
"isinstance",
"(",
"res",
",",
"ParseResults",
")",
"else",
"res",
"for",
"res",
"in",
"self",
".",
"__toklist",
"]"
] |
Returns the parse results as a nested list of matching tokens, all converted to strings.
Example::
patt = OneOrMore(Word(alphas))
result = patt.parseString("sldkj lsdkj sldkj")
# even though the result prints in string-like form, it is actually a pyparsing ParseResults
print(type(result), result) # -> <class 'pyparsing.ParseResults'> ['sldkj', 'lsdkj', 'sldkj']
# Use asList() to create an actual list
result_list = result.asList()
print(type(result_list), result_list) # -> <class 'list'> ['sldkj', 'lsdkj', 'sldkj']
|
[
"Returns",
"the",
"parse",
"results",
"as",
"a",
"nested",
"list",
"of",
"matching",
"tokens",
"all",
"converted",
"to",
"strings",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L822-L837
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParseResults.asDict
|
def asDict( self ):
"""
Returns the named parse results as a nested dictionary.
Example::
integer = Word(nums)
date_str = integer("year") + '/' + integer("month") + '/' + integer("day")
result = date_str.parseString('12/31/1999')
print(type(result), repr(result)) # -> <class 'pyparsing.ParseResults'> (['12', '/', '31', '/', '1999'], {'day': [('1999', 4)], 'year': [('12', 0)], 'month': [('31', 2)]})
result_dict = result.asDict()
print(type(result_dict), repr(result_dict)) # -> <class 'dict'> {'day': '1999', 'year': '12', 'month': '31'}
# even though a ParseResults supports dict-like access, sometime you just need to have a dict
import json
print(json.dumps(result)) # -> Exception: TypeError: ... is not JSON serializable
print(json.dumps(result.asDict())) # -> {"month": "31", "day": "1999", "year": "12"}
"""
if PY_3:
item_fn = self.items
else:
item_fn = self.iteritems
def toItem(obj):
if isinstance(obj, ParseResults):
if obj.haskeys():
return obj.asDict()
else:
return [toItem(v) for v in obj]
else:
return obj
return dict((k,toItem(v)) for k,v in item_fn())
|
python
|
def asDict( self ):
"""
Returns the named parse results as a nested dictionary.
Example::
integer = Word(nums)
date_str = integer("year") + '/' + integer("month") + '/' + integer("day")
result = date_str.parseString('12/31/1999')
print(type(result), repr(result)) # -> <class 'pyparsing.ParseResults'> (['12', '/', '31', '/', '1999'], {'day': [('1999', 4)], 'year': [('12', 0)], 'month': [('31', 2)]})
result_dict = result.asDict()
print(type(result_dict), repr(result_dict)) # -> <class 'dict'> {'day': '1999', 'year': '12', 'month': '31'}
# even though a ParseResults supports dict-like access, sometime you just need to have a dict
import json
print(json.dumps(result)) # -> Exception: TypeError: ... is not JSON serializable
print(json.dumps(result.asDict())) # -> {"month": "31", "day": "1999", "year": "12"}
"""
if PY_3:
item_fn = self.items
else:
item_fn = self.iteritems
def toItem(obj):
if isinstance(obj, ParseResults):
if obj.haskeys():
return obj.asDict()
else:
return [toItem(v) for v in obj]
else:
return obj
return dict((k,toItem(v)) for k,v in item_fn())
|
[
"def",
"asDict",
"(",
"self",
")",
":",
"if",
"PY_3",
":",
"item_fn",
"=",
"self",
".",
"items",
"else",
":",
"item_fn",
"=",
"self",
".",
"iteritems",
"def",
"toItem",
"(",
"obj",
")",
":",
"if",
"isinstance",
"(",
"obj",
",",
"ParseResults",
")",
":",
"if",
"obj",
".",
"haskeys",
"(",
")",
":",
"return",
"obj",
".",
"asDict",
"(",
")",
"else",
":",
"return",
"[",
"toItem",
"(",
"v",
")",
"for",
"v",
"in",
"obj",
"]",
"else",
":",
"return",
"obj",
"return",
"dict",
"(",
"(",
"k",
",",
"toItem",
"(",
"v",
")",
")",
"for",
"k",
",",
"v",
"in",
"item_fn",
"(",
")",
")"
] |
Returns the named parse results as a nested dictionary.
Example::
integer = Word(nums)
date_str = integer("year") + '/' + integer("month") + '/' + integer("day")
result = date_str.parseString('12/31/1999')
print(type(result), repr(result)) # -> <class 'pyparsing.ParseResults'> (['12', '/', '31', '/', '1999'], {'day': [('1999', 4)], 'year': [('12', 0)], 'month': [('31', 2)]})
result_dict = result.asDict()
print(type(result_dict), repr(result_dict)) # -> <class 'dict'> {'day': '1999', 'year': '12', 'month': '31'}
# even though a ParseResults supports dict-like access, sometime you just need to have a dict
import json
print(json.dumps(result)) # -> Exception: TypeError: ... is not JSON serializable
print(json.dumps(result.asDict())) # -> {"month": "31", "day": "1999", "year": "12"}
|
[
"Returns",
"the",
"named",
"parse",
"results",
"as",
"a",
"nested",
"dictionary",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L839-L873
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParseResults.getName
|
def getName(self):
r"""
Returns the results name for this token expression. Useful when several
different expressions might match at a particular location.
Example::
integer = Word(nums)
ssn_expr = Regex(r"\d\d\d-\d\d-\d\d\d\d")
house_number_expr = Suppress('#') + Word(nums, alphanums)
user_data = (Group(house_number_expr)("house_number")
| Group(ssn_expr)("ssn")
| Group(integer)("age"))
user_info = OneOrMore(user_data)
result = user_info.parseString("22 111-22-3333 #221B")
for item in result:
print(item.getName(), ':', item[0])
prints::
age : 22
ssn : 111-22-3333
house_number : 221B
"""
if self.__name:
return self.__name
elif self.__parent:
par = self.__parent()
if par:
return par.__lookup(self)
else:
return None
elif (len(self) == 1 and
len(self.__tokdict) == 1 and
next(iter(self.__tokdict.values()))[0][1] in (0,-1)):
return next(iter(self.__tokdict.keys()))
else:
return None
|
python
|
def getName(self):
r"""
Returns the results name for this token expression. Useful when several
different expressions might match at a particular location.
Example::
integer = Word(nums)
ssn_expr = Regex(r"\d\d\d-\d\d-\d\d\d\d")
house_number_expr = Suppress('#') + Word(nums, alphanums)
user_data = (Group(house_number_expr)("house_number")
| Group(ssn_expr)("ssn")
| Group(integer)("age"))
user_info = OneOrMore(user_data)
result = user_info.parseString("22 111-22-3333 #221B")
for item in result:
print(item.getName(), ':', item[0])
prints::
age : 22
ssn : 111-22-3333
house_number : 221B
"""
if self.__name:
return self.__name
elif self.__parent:
par = self.__parent()
if par:
return par.__lookup(self)
else:
return None
elif (len(self) == 1 and
len(self.__tokdict) == 1 and
next(iter(self.__tokdict.values()))[0][1] in (0,-1)):
return next(iter(self.__tokdict.keys()))
else:
return None
|
[
"def",
"getName",
"(",
"self",
")",
":",
"if",
"self",
".",
"__name",
":",
"return",
"self",
".",
"__name",
"elif",
"self",
".",
"__parent",
":",
"par",
"=",
"self",
".",
"__parent",
"(",
")",
"if",
"par",
":",
"return",
"par",
".",
"__lookup",
"(",
"self",
")",
"else",
":",
"return",
"None",
"elif",
"(",
"len",
"(",
"self",
")",
"==",
"1",
"and",
"len",
"(",
"self",
".",
"__tokdict",
")",
"==",
"1",
"and",
"next",
"(",
"iter",
"(",
"self",
".",
"__tokdict",
".",
"values",
"(",
")",
")",
")",
"[",
"0",
"]",
"[",
"1",
"]",
"in",
"(",
"0",
",",
"-",
"1",
")",
")",
":",
"return",
"next",
"(",
"iter",
"(",
"self",
".",
"__tokdict",
".",
"keys",
"(",
")",
")",
")",
"else",
":",
"return",
"None"
] |
r"""
Returns the results name for this token expression. Useful when several
different expressions might match at a particular location.
Example::
integer = Word(nums)
ssn_expr = Regex(r"\d\d\d-\d\d-\d\d\d\d")
house_number_expr = Suppress('#') + Word(nums, alphanums)
user_data = (Group(house_number_expr)("house_number")
| Group(ssn_expr)("ssn")
| Group(integer)("age"))
user_info = OneOrMore(user_data)
result = user_info.parseString("22 111-22-3333 #221B")
for item in result:
print(item.getName(), ':', item[0])
prints::
age : 22
ssn : 111-22-3333
house_number : 221B
|
[
"r",
"Returns",
"the",
"results",
"name",
"for",
"this",
"token",
"expression",
".",
"Useful",
"when",
"several",
"different",
"expressions",
"might",
"match",
"at",
"a",
"particular",
"location",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L954-L992
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParseResults.dump
|
def dump(self, indent='', depth=0, full=True):
"""
Diagnostic method for listing out the contents of
a :class:`ParseResults`. Accepts an optional ``indent`` argument so
that this string can be embedded in a nested display of other data.
Example::
integer = Word(nums)
date_str = integer("year") + '/' + integer("month") + '/' + integer("day")
result = date_str.parseString('12/31/1999')
print(result.dump())
prints::
['12', '/', '31', '/', '1999']
- day: 1999
- month: 31
- year: 12
"""
out = []
NL = '\n'
out.append( indent+_ustr(self.asList()) )
if full:
if self.haskeys():
items = sorted((str(k), v) for k,v in self.items())
for k,v in items:
if out:
out.append(NL)
out.append( "%s%s- %s: " % (indent,(' '*depth), k) )
if isinstance(v,ParseResults):
if v:
out.append( v.dump(indent,depth+1) )
else:
out.append(_ustr(v))
else:
out.append(repr(v))
elif any(isinstance(vv,ParseResults) for vv in self):
v = self
for i,vv in enumerate(v):
if isinstance(vv,ParseResults):
out.append("\n%s%s[%d]:\n%s%s%s" % (indent,(' '*(depth)),i,indent,(' '*(depth+1)),vv.dump(indent,depth+1) ))
else:
out.append("\n%s%s[%d]:\n%s%s%s" % (indent,(' '*(depth)),i,indent,(' '*(depth+1)),_ustr(vv)))
return "".join(out)
|
python
|
def dump(self, indent='', depth=0, full=True):
"""
Diagnostic method for listing out the contents of
a :class:`ParseResults`. Accepts an optional ``indent`` argument so
that this string can be embedded in a nested display of other data.
Example::
integer = Word(nums)
date_str = integer("year") + '/' + integer("month") + '/' + integer("day")
result = date_str.parseString('12/31/1999')
print(result.dump())
prints::
['12', '/', '31', '/', '1999']
- day: 1999
- month: 31
- year: 12
"""
out = []
NL = '\n'
out.append( indent+_ustr(self.asList()) )
if full:
if self.haskeys():
items = sorted((str(k), v) for k,v in self.items())
for k,v in items:
if out:
out.append(NL)
out.append( "%s%s- %s: " % (indent,(' '*depth), k) )
if isinstance(v,ParseResults):
if v:
out.append( v.dump(indent,depth+1) )
else:
out.append(_ustr(v))
else:
out.append(repr(v))
elif any(isinstance(vv,ParseResults) for vv in self):
v = self
for i,vv in enumerate(v):
if isinstance(vv,ParseResults):
out.append("\n%s%s[%d]:\n%s%s%s" % (indent,(' '*(depth)),i,indent,(' '*(depth+1)),vv.dump(indent,depth+1) ))
else:
out.append("\n%s%s[%d]:\n%s%s%s" % (indent,(' '*(depth)),i,indent,(' '*(depth+1)),_ustr(vv)))
return "".join(out)
|
[
"def",
"dump",
"(",
"self",
",",
"indent",
"=",
"''",
",",
"depth",
"=",
"0",
",",
"full",
"=",
"True",
")",
":",
"out",
"=",
"[",
"]",
"NL",
"=",
"'\\n'",
"out",
".",
"append",
"(",
"indent",
"+",
"_ustr",
"(",
"self",
".",
"asList",
"(",
")",
")",
")",
"if",
"full",
":",
"if",
"self",
".",
"haskeys",
"(",
")",
":",
"items",
"=",
"sorted",
"(",
"(",
"str",
"(",
"k",
")",
",",
"v",
")",
"for",
"k",
",",
"v",
"in",
"self",
".",
"items",
"(",
")",
")",
"for",
"k",
",",
"v",
"in",
"items",
":",
"if",
"out",
":",
"out",
".",
"append",
"(",
"NL",
")",
"out",
".",
"append",
"(",
"\"%s%s- %s: \"",
"%",
"(",
"indent",
",",
"(",
"' '",
"*",
"depth",
")",
",",
"k",
")",
")",
"if",
"isinstance",
"(",
"v",
",",
"ParseResults",
")",
":",
"if",
"v",
":",
"out",
".",
"append",
"(",
"v",
".",
"dump",
"(",
"indent",
",",
"depth",
"+",
"1",
")",
")",
"else",
":",
"out",
".",
"append",
"(",
"_ustr",
"(",
"v",
")",
")",
"else",
":",
"out",
".",
"append",
"(",
"repr",
"(",
"v",
")",
")",
"elif",
"any",
"(",
"isinstance",
"(",
"vv",
",",
"ParseResults",
")",
"for",
"vv",
"in",
"self",
")",
":",
"v",
"=",
"self",
"for",
"i",
",",
"vv",
"in",
"enumerate",
"(",
"v",
")",
":",
"if",
"isinstance",
"(",
"vv",
",",
"ParseResults",
")",
":",
"out",
".",
"append",
"(",
"\"\\n%s%s[%d]:\\n%s%s%s\"",
"%",
"(",
"indent",
",",
"(",
"' '",
"*",
"(",
"depth",
")",
")",
",",
"i",
",",
"indent",
",",
"(",
"' '",
"*",
"(",
"depth",
"+",
"1",
")",
")",
",",
"vv",
".",
"dump",
"(",
"indent",
",",
"depth",
"+",
"1",
")",
")",
")",
"else",
":",
"out",
".",
"append",
"(",
"\"\\n%s%s[%d]:\\n%s%s%s\"",
"%",
"(",
"indent",
",",
"(",
"' '",
"*",
"(",
"depth",
")",
")",
",",
"i",
",",
"indent",
",",
"(",
"' '",
"*",
"(",
"depth",
"+",
"1",
")",
")",
",",
"_ustr",
"(",
"vv",
")",
")",
")",
"return",
"\"\"",
".",
"join",
"(",
"out",
")"
] |
Diagnostic method for listing out the contents of
a :class:`ParseResults`. Accepts an optional ``indent`` argument so
that this string can be embedded in a nested display of other data.
Example::
integer = Word(nums)
date_str = integer("year") + '/' + integer("month") + '/' + integer("day")
result = date_str.parseString('12/31/1999')
print(result.dump())
prints::
['12', '/', '31', '/', '1999']
- day: 1999
- month: 31
- year: 12
|
[
"Diagnostic",
"method",
"for",
"listing",
"out",
"the",
"contents",
"of",
"a",
":",
"class",
":",
"ParseResults",
".",
"Accepts",
"an",
"optional",
"indent",
"argument",
"so",
"that",
"this",
"string",
"can",
"be",
"embedded",
"in",
"a",
"nested",
"display",
"of",
"other",
"data",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L994-L1040
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParseResults.pprint
|
def pprint(self, *args, **kwargs):
"""
Pretty-printer for parsed results as a list, using the
`pprint <https://docs.python.org/3/library/pprint.html>`_ module.
Accepts additional positional or keyword args as defined for
`pprint.pprint <https://docs.python.org/3/library/pprint.html#pprint.pprint>`_ .
Example::
ident = Word(alphas, alphanums)
num = Word(nums)
func = Forward()
term = ident | num | Group('(' + func + ')')
func <<= ident + Group(Optional(delimitedList(term)))
result = func.parseString("fna a,b,(fnb c,d,200),100")
result.pprint(width=40)
prints::
['fna',
['a',
'b',
['(', 'fnb', ['c', 'd', '200'], ')'],
'100']]
"""
pprint.pprint(self.asList(), *args, **kwargs)
|
python
|
def pprint(self, *args, **kwargs):
"""
Pretty-printer for parsed results as a list, using the
`pprint <https://docs.python.org/3/library/pprint.html>`_ module.
Accepts additional positional or keyword args as defined for
`pprint.pprint <https://docs.python.org/3/library/pprint.html#pprint.pprint>`_ .
Example::
ident = Word(alphas, alphanums)
num = Word(nums)
func = Forward()
term = ident | num | Group('(' + func + ')')
func <<= ident + Group(Optional(delimitedList(term)))
result = func.parseString("fna a,b,(fnb c,d,200),100")
result.pprint(width=40)
prints::
['fna',
['a',
'b',
['(', 'fnb', ['c', 'd', '200'], ')'],
'100']]
"""
pprint.pprint(self.asList(), *args, **kwargs)
|
[
"def",
"pprint",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"pprint",
".",
"pprint",
"(",
"self",
".",
"asList",
"(",
")",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")"
] |
Pretty-printer for parsed results as a list, using the
`pprint <https://docs.python.org/3/library/pprint.html>`_ module.
Accepts additional positional or keyword args as defined for
`pprint.pprint <https://docs.python.org/3/library/pprint.html#pprint.pprint>`_ .
Example::
ident = Word(alphas, alphanums)
num = Word(nums)
func = Forward()
term = ident | num | Group('(' + func + ')')
func <<= ident + Group(Optional(delimitedList(term)))
result = func.parseString("fna a,b,(fnb c,d,200),100")
result.pprint(width=40)
prints::
['fna',
['a',
'b',
['(', 'fnb', ['c', 'd', '200'], ')'],
'100']]
|
[
"Pretty",
"-",
"printer",
"for",
"parsed",
"results",
"as",
"a",
"list",
"using",
"the",
"pprint",
"<https",
":",
"//",
"docs",
".",
"python",
".",
"org",
"/",
"3",
"/",
"library",
"/",
"pprint",
".",
"html",
">",
"_",
"module",
".",
"Accepts",
"additional",
"positional",
"or",
"keyword",
"args",
"as",
"defined",
"for",
"pprint",
".",
"pprint",
"<https",
":",
"//",
"docs",
".",
"python",
".",
"org",
"/",
"3",
"/",
"library",
"/",
"pprint",
".",
"html#pprint",
".",
"pprint",
">",
"_",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L1042-L1067
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParserElement.copy
|
def copy( self ):
"""
Make a copy of this :class:`ParserElement`. Useful for defining
different parse actions for the same parsing pattern, using copies of
the original parse element.
Example::
integer = Word(nums).setParseAction(lambda toks: int(toks[0]))
integerK = integer.copy().addParseAction(lambda toks: toks[0]*1024) + Suppress("K")
integerM = integer.copy().addParseAction(lambda toks: toks[0]*1024*1024) + Suppress("M")
print(OneOrMore(integerK | integerM | integer).parseString("5K 100 640K 256M"))
prints::
[5120, 100, 655360, 268435456]
Equivalent form of ``expr.copy()`` is just ``expr()``::
integerM = integer().addParseAction(lambda toks: toks[0]*1024*1024) + Suppress("M")
"""
cpy = copy.copy( self )
cpy.parseAction = self.parseAction[:]
cpy.ignoreExprs = self.ignoreExprs[:]
if self.copyDefaultWhiteChars:
cpy.whiteChars = ParserElement.DEFAULT_WHITE_CHARS
return cpy
|
python
|
def copy( self ):
"""
Make a copy of this :class:`ParserElement`. Useful for defining
different parse actions for the same parsing pattern, using copies of
the original parse element.
Example::
integer = Word(nums).setParseAction(lambda toks: int(toks[0]))
integerK = integer.copy().addParseAction(lambda toks: toks[0]*1024) + Suppress("K")
integerM = integer.copy().addParseAction(lambda toks: toks[0]*1024*1024) + Suppress("M")
print(OneOrMore(integerK | integerM | integer).parseString("5K 100 640K 256M"))
prints::
[5120, 100, 655360, 268435456]
Equivalent form of ``expr.copy()`` is just ``expr()``::
integerM = integer().addParseAction(lambda toks: toks[0]*1024*1024) + Suppress("M")
"""
cpy = copy.copy( self )
cpy.parseAction = self.parseAction[:]
cpy.ignoreExprs = self.ignoreExprs[:]
if self.copyDefaultWhiteChars:
cpy.whiteChars = ParserElement.DEFAULT_WHITE_CHARS
return cpy
|
[
"def",
"copy",
"(",
"self",
")",
":",
"cpy",
"=",
"copy",
".",
"copy",
"(",
"self",
")",
"cpy",
".",
"parseAction",
"=",
"self",
".",
"parseAction",
"[",
":",
"]",
"cpy",
".",
"ignoreExprs",
"=",
"self",
".",
"ignoreExprs",
"[",
":",
"]",
"if",
"self",
".",
"copyDefaultWhiteChars",
":",
"cpy",
".",
"whiteChars",
"=",
"ParserElement",
".",
"DEFAULT_WHITE_CHARS",
"return",
"cpy"
] |
Make a copy of this :class:`ParserElement`. Useful for defining
different parse actions for the same parsing pattern, using copies of
the original parse element.
Example::
integer = Word(nums).setParseAction(lambda toks: int(toks[0]))
integerK = integer.copy().addParseAction(lambda toks: toks[0]*1024) + Suppress("K")
integerM = integer.copy().addParseAction(lambda toks: toks[0]*1024*1024) + Suppress("M")
print(OneOrMore(integerK | integerM | integer).parseString("5K 100 640K 256M"))
prints::
[5120, 100, 655360, 268435456]
Equivalent form of ``expr.copy()`` is just ``expr()``::
integerM = integer().addParseAction(lambda toks: toks[0]*1024*1024) + Suppress("M")
|
[
"Make",
"a",
"copy",
"of",
"this",
":",
"class",
":",
"ParserElement",
".",
"Useful",
"for",
"defining",
"different",
"parse",
"actions",
"for",
"the",
"same",
"parsing",
"pattern",
"using",
"copies",
"of",
"the",
"original",
"parse",
"element",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L1300-L1327
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParserElement.setName
|
def setName( self, name ):
"""
Define name for this expression, makes debugging and exception messages clearer.
Example::
Word(nums).parseString("ABC") # -> Exception: Expected W:(0123...) (at char 0), (line:1, col:1)
Word(nums).setName("integer").parseString("ABC") # -> Exception: Expected integer (at char 0), (line:1, col:1)
"""
self.name = name
self.errmsg = "Expected " + self.name
if hasattr(self,"exception"):
self.exception.msg = self.errmsg
return self
|
python
|
def setName( self, name ):
"""
Define name for this expression, makes debugging and exception messages clearer.
Example::
Word(nums).parseString("ABC") # -> Exception: Expected W:(0123...) (at char 0), (line:1, col:1)
Word(nums).setName("integer").parseString("ABC") # -> Exception: Expected integer (at char 0), (line:1, col:1)
"""
self.name = name
self.errmsg = "Expected " + self.name
if hasattr(self,"exception"):
self.exception.msg = self.errmsg
return self
|
[
"def",
"setName",
"(",
"self",
",",
"name",
")",
":",
"self",
".",
"name",
"=",
"name",
"self",
".",
"errmsg",
"=",
"\"Expected \"",
"+",
"self",
".",
"name",
"if",
"hasattr",
"(",
"self",
",",
"\"exception\"",
")",
":",
"self",
".",
"exception",
".",
"msg",
"=",
"self",
".",
"errmsg",
"return",
"self"
] |
Define name for this expression, makes debugging and exception messages clearer.
Example::
Word(nums).parseString("ABC") # -> Exception: Expected W:(0123...) (at char 0), (line:1, col:1)
Word(nums).setName("integer").parseString("ABC") # -> Exception: Expected integer (at char 0), (line:1, col:1)
|
[
"Define",
"name",
"for",
"this",
"expression",
"makes",
"debugging",
"and",
"exception",
"messages",
"clearer",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L1329-L1342
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParserElement.setResultsName
|
def setResultsName( self, name, listAllMatches=False ):
"""
Define name for referencing matching tokens as a nested attribute
of the returned parse results.
NOTE: this returns a *copy* of the original :class:`ParserElement` object;
this is so that the client can define a basic element, such as an
integer, and reference it in multiple places with different names.
You can also set results names using the abbreviated syntax,
``expr("name")`` in place of ``expr.setResultsName("name")``
- see :class:`__call__`.
Example::
date_str = (integer.setResultsName("year") + '/'
+ integer.setResultsName("month") + '/'
+ integer.setResultsName("day"))
# equivalent form:
date_str = integer("year") + '/' + integer("month") + '/' + integer("day")
"""
newself = self.copy()
if name.endswith("*"):
name = name[:-1]
listAllMatches=True
newself.resultsName = name
newself.modalResults = not listAllMatches
return newself
|
python
|
def setResultsName( self, name, listAllMatches=False ):
"""
Define name for referencing matching tokens as a nested attribute
of the returned parse results.
NOTE: this returns a *copy* of the original :class:`ParserElement` object;
this is so that the client can define a basic element, such as an
integer, and reference it in multiple places with different names.
You can also set results names using the abbreviated syntax,
``expr("name")`` in place of ``expr.setResultsName("name")``
- see :class:`__call__`.
Example::
date_str = (integer.setResultsName("year") + '/'
+ integer.setResultsName("month") + '/'
+ integer.setResultsName("day"))
# equivalent form:
date_str = integer("year") + '/' + integer("month") + '/' + integer("day")
"""
newself = self.copy()
if name.endswith("*"):
name = name[:-1]
listAllMatches=True
newself.resultsName = name
newself.modalResults = not listAllMatches
return newself
|
[
"def",
"setResultsName",
"(",
"self",
",",
"name",
",",
"listAllMatches",
"=",
"False",
")",
":",
"newself",
"=",
"self",
".",
"copy",
"(",
")",
"if",
"name",
".",
"endswith",
"(",
"\"*\"",
")",
":",
"name",
"=",
"name",
"[",
":",
"-",
"1",
"]",
"listAllMatches",
"=",
"True",
"newself",
".",
"resultsName",
"=",
"name",
"newself",
".",
"modalResults",
"=",
"not",
"listAllMatches",
"return",
"newself"
] |
Define name for referencing matching tokens as a nested attribute
of the returned parse results.
NOTE: this returns a *copy* of the original :class:`ParserElement` object;
this is so that the client can define a basic element, such as an
integer, and reference it in multiple places with different names.
You can also set results names using the abbreviated syntax,
``expr("name")`` in place of ``expr.setResultsName("name")``
- see :class:`__call__`.
Example::
date_str = (integer.setResultsName("year") + '/'
+ integer.setResultsName("month") + '/'
+ integer.setResultsName("day"))
# equivalent form:
date_str = integer("year") + '/' + integer("month") + '/' + integer("day")
|
[
"Define",
"name",
"for",
"referencing",
"matching",
"tokens",
"as",
"a",
"nested",
"attribute",
"of",
"the",
"returned",
"parse",
"results",
".",
"NOTE",
":",
"this",
"returns",
"a",
"*",
"copy",
"*",
"of",
"the",
"original",
":",
"class",
":",
"ParserElement",
"object",
";",
"this",
"is",
"so",
"that",
"the",
"client",
"can",
"define",
"a",
"basic",
"element",
"such",
"as",
"an",
"integer",
"and",
"reference",
"it",
"in",
"multiple",
"places",
"with",
"different",
"names",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L1344-L1371
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParserElement.addCondition
|
def addCondition(self, *fns, **kwargs):
"""Add a boolean predicate function to expression's list of parse actions. See
:class:`setParseAction` for function call signatures. Unlike ``setParseAction``,
functions passed to ``addCondition`` need to return boolean success/fail of the condition.
Optional keyword arguments:
- message = define a custom message to be used in the raised exception
- fatal = if True, will raise ParseFatalException to stop parsing immediately; otherwise will raise ParseException
Example::
integer = Word(nums).setParseAction(lambda toks: int(toks[0]))
year_int = integer.copy()
year_int.addCondition(lambda toks: toks[0] >= 2000, message="Only support years 2000 and later")
date_str = year_int + '/' + integer + '/' + integer
result = date_str.parseString("1999/12/31") # -> Exception: Only support years 2000 and later (at char 0), (line:1, col:1)
"""
msg = kwargs.get("message", "failed user-defined condition")
exc_type = ParseFatalException if kwargs.get("fatal", False) else ParseException
for fn in fns:
fn = _trim_arity(fn)
def pa(s,l,t):
if not bool(fn(s,l,t)):
raise exc_type(s,l,msg)
self.parseAction.append(pa)
self.callDuringTry = self.callDuringTry or kwargs.get("callDuringTry", False)
return self
|
python
|
def addCondition(self, *fns, **kwargs):
"""Add a boolean predicate function to expression's list of parse actions. See
:class:`setParseAction` for function call signatures. Unlike ``setParseAction``,
functions passed to ``addCondition`` need to return boolean success/fail of the condition.
Optional keyword arguments:
- message = define a custom message to be used in the raised exception
- fatal = if True, will raise ParseFatalException to stop parsing immediately; otherwise will raise ParseException
Example::
integer = Word(nums).setParseAction(lambda toks: int(toks[0]))
year_int = integer.copy()
year_int.addCondition(lambda toks: toks[0] >= 2000, message="Only support years 2000 and later")
date_str = year_int + '/' + integer + '/' + integer
result = date_str.parseString("1999/12/31") # -> Exception: Only support years 2000 and later (at char 0), (line:1, col:1)
"""
msg = kwargs.get("message", "failed user-defined condition")
exc_type = ParseFatalException if kwargs.get("fatal", False) else ParseException
for fn in fns:
fn = _trim_arity(fn)
def pa(s,l,t):
if not bool(fn(s,l,t)):
raise exc_type(s,l,msg)
self.parseAction.append(pa)
self.callDuringTry = self.callDuringTry or kwargs.get("callDuringTry", False)
return self
|
[
"def",
"addCondition",
"(",
"self",
",",
"*",
"fns",
",",
"*",
"*",
"kwargs",
")",
":",
"msg",
"=",
"kwargs",
".",
"get",
"(",
"\"message\"",
",",
"\"failed user-defined condition\"",
")",
"exc_type",
"=",
"ParseFatalException",
"if",
"kwargs",
".",
"get",
"(",
"\"fatal\"",
",",
"False",
")",
"else",
"ParseException",
"for",
"fn",
"in",
"fns",
":",
"fn",
"=",
"_trim_arity",
"(",
"fn",
")",
"def",
"pa",
"(",
"s",
",",
"l",
",",
"t",
")",
":",
"if",
"not",
"bool",
"(",
"fn",
"(",
"s",
",",
"l",
",",
"t",
")",
")",
":",
"raise",
"exc_type",
"(",
"s",
",",
"l",
",",
"msg",
")",
"self",
".",
"parseAction",
".",
"append",
"(",
"pa",
")",
"self",
".",
"callDuringTry",
"=",
"self",
".",
"callDuringTry",
"or",
"kwargs",
".",
"get",
"(",
"\"callDuringTry\"",
",",
"False",
")",
"return",
"self"
] |
Add a boolean predicate function to expression's list of parse actions. See
:class:`setParseAction` for function call signatures. Unlike ``setParseAction``,
functions passed to ``addCondition`` need to return boolean success/fail of the condition.
Optional keyword arguments:
- message = define a custom message to be used in the raised exception
- fatal = if True, will raise ParseFatalException to stop parsing immediately; otherwise will raise ParseException
Example::
integer = Word(nums).setParseAction(lambda toks: int(toks[0]))
year_int = integer.copy()
year_int.addCondition(lambda toks: toks[0] >= 2000, message="Only support years 2000 and later")
date_str = year_int + '/' + integer + '/' + integer
result = date_str.parseString("1999/12/31") # -> Exception: Only support years 2000 and later (at char 0), (line:1, col:1)
|
[
"Add",
"a",
"boolean",
"predicate",
"function",
"to",
"expression",
"s",
"list",
"of",
"parse",
"actions",
".",
"See",
":",
"class",
":",
"setParseAction",
"for",
"function",
"call",
"signatures",
".",
"Unlike",
"setParseAction",
"functions",
"passed",
"to",
"addCondition",
"need",
"to",
"return",
"boolean",
"success",
"/",
"fail",
"of",
"the",
"condition",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L1442-L1469
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParserElement.enablePackrat
|
def enablePackrat(cache_size_limit=128):
"""Enables "packrat" parsing, which adds memoizing to the parsing logic.
Repeated parse attempts at the same string location (which happens
often in many complex grammars) can immediately return a cached value,
instead of re-executing parsing/validating code. Memoizing is done of
both valid results and parsing exceptions.
Parameters:
- cache_size_limit - (default= ``128``) - if an integer value is provided
will limit the size of the packrat cache; if None is passed, then
the cache size will be unbounded; if 0 is passed, the cache will
be effectively disabled.
This speedup may break existing programs that use parse actions that
have side-effects. For this reason, packrat parsing is disabled when
you first import pyparsing. To activate the packrat feature, your
program must call the class method :class:`ParserElement.enablePackrat`.
For best results, call ``enablePackrat()`` immediately after
importing pyparsing.
Example::
import pyparsing
pyparsing.ParserElement.enablePackrat()
"""
if not ParserElement._packratEnabled:
ParserElement._packratEnabled = True
if cache_size_limit is None:
ParserElement.packrat_cache = ParserElement._UnboundedCache()
else:
ParserElement.packrat_cache = ParserElement._FifoCache(cache_size_limit)
ParserElement._parse = ParserElement._parseCache
|
python
|
def enablePackrat(cache_size_limit=128):
"""Enables "packrat" parsing, which adds memoizing to the parsing logic.
Repeated parse attempts at the same string location (which happens
often in many complex grammars) can immediately return a cached value,
instead of re-executing parsing/validating code. Memoizing is done of
both valid results and parsing exceptions.
Parameters:
- cache_size_limit - (default= ``128``) - if an integer value is provided
will limit the size of the packrat cache; if None is passed, then
the cache size will be unbounded; if 0 is passed, the cache will
be effectively disabled.
This speedup may break existing programs that use parse actions that
have side-effects. For this reason, packrat parsing is disabled when
you first import pyparsing. To activate the packrat feature, your
program must call the class method :class:`ParserElement.enablePackrat`.
For best results, call ``enablePackrat()`` immediately after
importing pyparsing.
Example::
import pyparsing
pyparsing.ParserElement.enablePackrat()
"""
if not ParserElement._packratEnabled:
ParserElement._packratEnabled = True
if cache_size_limit is None:
ParserElement.packrat_cache = ParserElement._UnboundedCache()
else:
ParserElement.packrat_cache = ParserElement._FifoCache(cache_size_limit)
ParserElement._parse = ParserElement._parseCache
|
[
"def",
"enablePackrat",
"(",
"cache_size_limit",
"=",
"128",
")",
":",
"if",
"not",
"ParserElement",
".",
"_packratEnabled",
":",
"ParserElement",
".",
"_packratEnabled",
"=",
"True",
"if",
"cache_size_limit",
"is",
"None",
":",
"ParserElement",
".",
"packrat_cache",
"=",
"ParserElement",
".",
"_UnboundedCache",
"(",
")",
"else",
":",
"ParserElement",
".",
"packrat_cache",
"=",
"ParserElement",
".",
"_FifoCache",
"(",
"cache_size_limit",
")",
"ParserElement",
".",
"_parse",
"=",
"ParserElement",
".",
"_parseCache"
] |
Enables "packrat" parsing, which adds memoizing to the parsing logic.
Repeated parse attempts at the same string location (which happens
often in many complex grammars) can immediately return a cached value,
instead of re-executing parsing/validating code. Memoizing is done of
both valid results and parsing exceptions.
Parameters:
- cache_size_limit - (default= ``128``) - if an integer value is provided
will limit the size of the packrat cache; if None is passed, then
the cache size will be unbounded; if 0 is passed, the cache will
be effectively disabled.
This speedup may break existing programs that use parse actions that
have side-effects. For this reason, packrat parsing is disabled when
you first import pyparsing. To activate the packrat feature, your
program must call the class method :class:`ParserElement.enablePackrat`.
For best results, call ``enablePackrat()`` immediately after
importing pyparsing.
Example::
import pyparsing
pyparsing.ParserElement.enablePackrat()
|
[
"Enables",
"packrat",
"parsing",
"which",
"adds",
"memoizing",
"to",
"the",
"parsing",
"logic",
".",
"Repeated",
"parse",
"attempts",
"at",
"the",
"same",
"string",
"location",
"(",
"which",
"happens",
"often",
"in",
"many",
"complex",
"grammars",
")",
"can",
"immediately",
"return",
"a",
"cached",
"value",
"instead",
"of",
"re",
"-",
"executing",
"parsing",
"/",
"validating",
"code",
".",
"Memoizing",
"is",
"done",
"of",
"both",
"valid",
"results",
"and",
"parsing",
"exceptions",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L1732-L1764
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParserElement.parseString
|
def parseString( self, instring, parseAll=False ):
"""
Execute the parse expression with the given string.
This is the main interface to the client code, once the complete
expression has been built.
If you want the grammar to require that the entire input string be
successfully parsed, then set ``parseAll`` to True (equivalent to ending
the grammar with ``StringEnd()``).
Note: ``parseString`` implicitly calls ``expandtabs()`` on the input string,
in order to report proper column numbers in parse actions.
If the input string contains tabs and
the grammar uses parse actions that use the ``loc`` argument to index into the
string being parsed, you can ensure you have a consistent view of the input
string by:
- calling ``parseWithTabs`` on your grammar before calling ``parseString``
(see :class:`parseWithTabs`)
- define your parse action using the full ``(s,loc,toks)`` signature, and
reference the input string using the parse action's ``s`` argument
- explictly expand the tabs in your input string before calling
``parseString``
Example::
Word('a').parseString('aaaaabaaa') # -> ['aaaaa']
Word('a').parseString('aaaaabaaa', parseAll=True) # -> Exception: Expected end of text
"""
ParserElement.resetCache()
if not self.streamlined:
self.streamline()
#~ self.saveAsList = True
for e in self.ignoreExprs:
e.streamline()
if not self.keepTabs:
instring = instring.expandtabs()
try:
loc, tokens = self._parse( instring, 0 )
if parseAll:
loc = self.preParse( instring, loc )
se = Empty() + StringEnd()
se._parse( instring, loc )
except ParseBaseException as exc:
if ParserElement.verbose_stacktrace:
raise
else:
# catch and re-raise exception from here, clears out pyparsing internal stack trace
raise exc
else:
return tokens
|
python
|
def parseString( self, instring, parseAll=False ):
"""
Execute the parse expression with the given string.
This is the main interface to the client code, once the complete
expression has been built.
If you want the grammar to require that the entire input string be
successfully parsed, then set ``parseAll`` to True (equivalent to ending
the grammar with ``StringEnd()``).
Note: ``parseString`` implicitly calls ``expandtabs()`` on the input string,
in order to report proper column numbers in parse actions.
If the input string contains tabs and
the grammar uses parse actions that use the ``loc`` argument to index into the
string being parsed, you can ensure you have a consistent view of the input
string by:
- calling ``parseWithTabs`` on your grammar before calling ``parseString``
(see :class:`parseWithTabs`)
- define your parse action using the full ``(s,loc,toks)`` signature, and
reference the input string using the parse action's ``s`` argument
- explictly expand the tabs in your input string before calling
``parseString``
Example::
Word('a').parseString('aaaaabaaa') # -> ['aaaaa']
Word('a').parseString('aaaaabaaa', parseAll=True) # -> Exception: Expected end of text
"""
ParserElement.resetCache()
if not self.streamlined:
self.streamline()
#~ self.saveAsList = True
for e in self.ignoreExprs:
e.streamline()
if not self.keepTabs:
instring = instring.expandtabs()
try:
loc, tokens = self._parse( instring, 0 )
if parseAll:
loc = self.preParse( instring, loc )
se = Empty() + StringEnd()
se._parse( instring, loc )
except ParseBaseException as exc:
if ParserElement.verbose_stacktrace:
raise
else:
# catch and re-raise exception from here, clears out pyparsing internal stack trace
raise exc
else:
return tokens
|
[
"def",
"parseString",
"(",
"self",
",",
"instring",
",",
"parseAll",
"=",
"False",
")",
":",
"ParserElement",
".",
"resetCache",
"(",
")",
"if",
"not",
"self",
".",
"streamlined",
":",
"self",
".",
"streamline",
"(",
")",
"#~ self.saveAsList = True",
"for",
"e",
"in",
"self",
".",
"ignoreExprs",
":",
"e",
".",
"streamline",
"(",
")",
"if",
"not",
"self",
".",
"keepTabs",
":",
"instring",
"=",
"instring",
".",
"expandtabs",
"(",
")",
"try",
":",
"loc",
",",
"tokens",
"=",
"self",
".",
"_parse",
"(",
"instring",
",",
"0",
")",
"if",
"parseAll",
":",
"loc",
"=",
"self",
".",
"preParse",
"(",
"instring",
",",
"loc",
")",
"se",
"=",
"Empty",
"(",
")",
"+",
"StringEnd",
"(",
")",
"se",
".",
"_parse",
"(",
"instring",
",",
"loc",
")",
"except",
"ParseBaseException",
"as",
"exc",
":",
"if",
"ParserElement",
".",
"verbose_stacktrace",
":",
"raise",
"else",
":",
"# catch and re-raise exception from here, clears out pyparsing internal stack trace",
"raise",
"exc",
"else",
":",
"return",
"tokens"
] |
Execute the parse expression with the given string.
This is the main interface to the client code, once the complete
expression has been built.
If you want the grammar to require that the entire input string be
successfully parsed, then set ``parseAll`` to True (equivalent to ending
the grammar with ``StringEnd()``).
Note: ``parseString`` implicitly calls ``expandtabs()`` on the input string,
in order to report proper column numbers in parse actions.
If the input string contains tabs and
the grammar uses parse actions that use the ``loc`` argument to index into the
string being parsed, you can ensure you have a consistent view of the input
string by:
- calling ``parseWithTabs`` on your grammar before calling ``parseString``
(see :class:`parseWithTabs`)
- define your parse action using the full ``(s,loc,toks)`` signature, and
reference the input string using the parse action's ``s`` argument
- explictly expand the tabs in your input string before calling
``parseString``
Example::
Word('a').parseString('aaaaabaaa') # -> ['aaaaa']
Word('a').parseString('aaaaabaaa', parseAll=True) # -> Exception: Expected end of text
|
[
"Execute",
"the",
"parse",
"expression",
"with",
"the",
"given",
"string",
".",
"This",
"is",
"the",
"main",
"interface",
"to",
"the",
"client",
"code",
"once",
"the",
"complete",
"expression",
"has",
"been",
"built",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L1766-L1816
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParserElement.searchString
|
def searchString( self, instring, maxMatches=_MAX_INT ):
"""
Another extension to :class:`scanString`, simplifying the access to the tokens found
to match the given parse expression. May be called with optional
``maxMatches`` argument, to clip searching after 'n' matches are found.
Example::
# a capitalized word starts with an uppercase letter, followed by zero or more lowercase letters
cap_word = Word(alphas.upper(), alphas.lower())
print(cap_word.searchString("More than Iron, more than Lead, more than Gold I need Electricity"))
# the sum() builtin can be used to merge results into a single ParseResults object
print(sum(cap_word.searchString("More than Iron, more than Lead, more than Gold I need Electricity")))
prints::
[['More'], ['Iron'], ['Lead'], ['Gold'], ['I'], ['Electricity']]
['More', 'Iron', 'Lead', 'Gold', 'I', 'Electricity']
"""
try:
return ParseResults([ t for t,s,e in self.scanString( instring, maxMatches ) ])
except ParseBaseException as exc:
if ParserElement.verbose_stacktrace:
raise
else:
# catch and re-raise exception from here, clears out pyparsing internal stack trace
raise exc
|
python
|
def searchString( self, instring, maxMatches=_MAX_INT ):
"""
Another extension to :class:`scanString`, simplifying the access to the tokens found
to match the given parse expression. May be called with optional
``maxMatches`` argument, to clip searching after 'n' matches are found.
Example::
# a capitalized word starts with an uppercase letter, followed by zero or more lowercase letters
cap_word = Word(alphas.upper(), alphas.lower())
print(cap_word.searchString("More than Iron, more than Lead, more than Gold I need Electricity"))
# the sum() builtin can be used to merge results into a single ParseResults object
print(sum(cap_word.searchString("More than Iron, more than Lead, more than Gold I need Electricity")))
prints::
[['More'], ['Iron'], ['Lead'], ['Gold'], ['I'], ['Electricity']]
['More', 'Iron', 'Lead', 'Gold', 'I', 'Electricity']
"""
try:
return ParseResults([ t for t,s,e in self.scanString( instring, maxMatches ) ])
except ParseBaseException as exc:
if ParserElement.verbose_stacktrace:
raise
else:
# catch and re-raise exception from here, clears out pyparsing internal stack trace
raise exc
|
[
"def",
"searchString",
"(",
"self",
",",
"instring",
",",
"maxMatches",
"=",
"_MAX_INT",
")",
":",
"try",
":",
"return",
"ParseResults",
"(",
"[",
"t",
"for",
"t",
",",
"s",
",",
"e",
"in",
"self",
".",
"scanString",
"(",
"instring",
",",
"maxMatches",
")",
"]",
")",
"except",
"ParseBaseException",
"as",
"exc",
":",
"if",
"ParserElement",
".",
"verbose_stacktrace",
":",
"raise",
"else",
":",
"# catch and re-raise exception from here, clears out pyparsing internal stack trace",
"raise",
"exc"
] |
Another extension to :class:`scanString`, simplifying the access to the tokens found
to match the given parse expression. May be called with optional
``maxMatches`` argument, to clip searching after 'n' matches are found.
Example::
# a capitalized word starts with an uppercase letter, followed by zero or more lowercase letters
cap_word = Word(alphas.upper(), alphas.lower())
print(cap_word.searchString("More than Iron, more than Lead, more than Gold I need Electricity"))
# the sum() builtin can be used to merge results into a single ParseResults object
print(sum(cap_word.searchString("More than Iron, more than Lead, more than Gold I need Electricity")))
prints::
[['More'], ['Iron'], ['Lead'], ['Gold'], ['I'], ['Electricity']]
['More', 'Iron', 'Lead', 'Gold', 'I', 'Electricity']
|
[
"Another",
"extension",
"to",
":",
"class",
":",
"scanString",
"simplifying",
"the",
"access",
"to",
"the",
"tokens",
"found",
"to",
"match",
"the",
"given",
"parse",
"expression",
".",
"May",
"be",
"called",
"with",
"optional",
"maxMatches",
"argument",
"to",
"clip",
"searching",
"after",
"n",
"matches",
"are",
"found",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L1936-L1964
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParserElement.split
|
def split(self, instring, maxsplit=_MAX_INT, includeSeparators=False):
"""
Generator method to split a string using the given expression as a separator.
May be called with optional ``maxsplit`` argument, to limit the number of splits;
and the optional ``includeSeparators`` argument (default= ``False``), if the separating
matching text should be included in the split results.
Example::
punc = oneOf(list(".,;:/-!?"))
print(list(punc.split("This, this?, this sentence, is badly punctuated!")))
prints::
['This', ' this', '', ' this sentence', ' is badly punctuated', '']
"""
splits = 0
last = 0
for t,s,e in self.scanString(instring, maxMatches=maxsplit):
yield instring[last:s]
if includeSeparators:
yield t[0]
last = e
yield instring[last:]
|
python
|
def split(self, instring, maxsplit=_MAX_INT, includeSeparators=False):
"""
Generator method to split a string using the given expression as a separator.
May be called with optional ``maxsplit`` argument, to limit the number of splits;
and the optional ``includeSeparators`` argument (default= ``False``), if the separating
matching text should be included in the split results.
Example::
punc = oneOf(list(".,;:/-!?"))
print(list(punc.split("This, this?, this sentence, is badly punctuated!")))
prints::
['This', ' this', '', ' this sentence', ' is badly punctuated', '']
"""
splits = 0
last = 0
for t,s,e in self.scanString(instring, maxMatches=maxsplit):
yield instring[last:s]
if includeSeparators:
yield t[0]
last = e
yield instring[last:]
|
[
"def",
"split",
"(",
"self",
",",
"instring",
",",
"maxsplit",
"=",
"_MAX_INT",
",",
"includeSeparators",
"=",
"False",
")",
":",
"splits",
"=",
"0",
"last",
"=",
"0",
"for",
"t",
",",
"s",
",",
"e",
"in",
"self",
".",
"scanString",
"(",
"instring",
",",
"maxMatches",
"=",
"maxsplit",
")",
":",
"yield",
"instring",
"[",
"last",
":",
"s",
"]",
"if",
"includeSeparators",
":",
"yield",
"t",
"[",
"0",
"]",
"last",
"=",
"e",
"yield",
"instring",
"[",
"last",
":",
"]"
] |
Generator method to split a string using the given expression as a separator.
May be called with optional ``maxsplit`` argument, to limit the number of splits;
and the optional ``includeSeparators`` argument (default= ``False``), if the separating
matching text should be included in the split results.
Example::
punc = oneOf(list(".,;:/-!?"))
print(list(punc.split("This, this?, this sentence, is badly punctuated!")))
prints::
['This', ' this', '', ' this sentence', ' is badly punctuated', '']
|
[
"Generator",
"method",
"to",
"split",
"a",
"string",
"using",
"the",
"given",
"expression",
"as",
"a",
"separator",
".",
"May",
"be",
"called",
"with",
"optional",
"maxsplit",
"argument",
"to",
"limit",
"the",
"number",
"of",
"splits",
";",
"and",
"the",
"optional",
"includeSeparators",
"argument",
"(",
"default",
"=",
"False",
")",
"if",
"the",
"separating",
"matching",
"text",
"should",
"be",
"included",
"in",
"the",
"split",
"results",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L1966-L1989
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParserElement.setWhitespaceChars
|
def setWhitespaceChars( self, chars ):
"""
Overrides the default whitespace chars
"""
self.skipWhitespace = True
self.whiteChars = chars
self.copyDefaultWhiteChars = False
return self
|
python
|
def setWhitespaceChars( self, chars ):
"""
Overrides the default whitespace chars
"""
self.skipWhitespace = True
self.whiteChars = chars
self.copyDefaultWhiteChars = False
return self
|
[
"def",
"setWhitespaceChars",
"(",
"self",
",",
"chars",
")",
":",
"self",
".",
"skipWhitespace",
"=",
"True",
"self",
".",
"whiteChars",
"=",
"chars",
"self",
".",
"copyDefaultWhiteChars",
"=",
"False",
"return",
"self"
] |
Overrides the default whitespace chars
|
[
"Overrides",
"the",
"default",
"whitespace",
"chars"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L2235-L2242
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParserElement.ignore
|
def ignore( self, other ):
"""
Define expression to be ignored (e.g., comments) while doing pattern
matching; may be called repeatedly, to define multiple comment or other
ignorable patterns.
Example::
patt = OneOrMore(Word(alphas))
patt.parseString('ablaj /* comment */ lskjd') # -> ['ablaj']
patt.ignore(cStyleComment)
patt.parseString('ablaj /* comment */ lskjd') # -> ['ablaj', 'lskjd']
"""
if isinstance(other, basestring):
other = Suppress(other)
if isinstance( other, Suppress ):
if other not in self.ignoreExprs:
self.ignoreExprs.append(other)
else:
self.ignoreExprs.append( Suppress( other.copy() ) )
return self
|
python
|
def ignore( self, other ):
"""
Define expression to be ignored (e.g., comments) while doing pattern
matching; may be called repeatedly, to define multiple comment or other
ignorable patterns.
Example::
patt = OneOrMore(Word(alphas))
patt.parseString('ablaj /* comment */ lskjd') # -> ['ablaj']
patt.ignore(cStyleComment)
patt.parseString('ablaj /* comment */ lskjd') # -> ['ablaj', 'lskjd']
"""
if isinstance(other, basestring):
other = Suppress(other)
if isinstance( other, Suppress ):
if other not in self.ignoreExprs:
self.ignoreExprs.append(other)
else:
self.ignoreExprs.append( Suppress( other.copy() ) )
return self
|
[
"def",
"ignore",
"(",
"self",
",",
"other",
")",
":",
"if",
"isinstance",
"(",
"other",
",",
"basestring",
")",
":",
"other",
"=",
"Suppress",
"(",
"other",
")",
"if",
"isinstance",
"(",
"other",
",",
"Suppress",
")",
":",
"if",
"other",
"not",
"in",
"self",
".",
"ignoreExprs",
":",
"self",
".",
"ignoreExprs",
".",
"append",
"(",
"other",
")",
"else",
":",
"self",
".",
"ignoreExprs",
".",
"append",
"(",
"Suppress",
"(",
"other",
".",
"copy",
"(",
")",
")",
")",
"return",
"self"
] |
Define expression to be ignored (e.g., comments) while doing pattern
matching; may be called repeatedly, to define multiple comment or other
ignorable patterns.
Example::
patt = OneOrMore(Word(alphas))
patt.parseString('ablaj /* comment */ lskjd') # -> ['ablaj']
patt.ignore(cStyleComment)
patt.parseString('ablaj /* comment */ lskjd') # -> ['ablaj', 'lskjd']
|
[
"Define",
"expression",
"to",
"be",
"ignored",
"(",
"e",
".",
"g",
".",
"comments",
")",
"while",
"doing",
"pattern",
"matching",
";",
"may",
"be",
"called",
"repeatedly",
"to",
"define",
"multiple",
"comment",
"or",
"other",
"ignorable",
"patterns",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L2253-L2275
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParserElement.setDebugActions
|
def setDebugActions( self, startAction, successAction, exceptionAction ):
"""
Enable display of debugging messages while doing pattern matching.
"""
self.debugActions = (startAction or _defaultStartDebugAction,
successAction or _defaultSuccessDebugAction,
exceptionAction or _defaultExceptionDebugAction)
self.debug = True
return self
|
python
|
def setDebugActions( self, startAction, successAction, exceptionAction ):
"""
Enable display of debugging messages while doing pattern matching.
"""
self.debugActions = (startAction or _defaultStartDebugAction,
successAction or _defaultSuccessDebugAction,
exceptionAction or _defaultExceptionDebugAction)
self.debug = True
return self
|
[
"def",
"setDebugActions",
"(",
"self",
",",
"startAction",
",",
"successAction",
",",
"exceptionAction",
")",
":",
"self",
".",
"debugActions",
"=",
"(",
"startAction",
"or",
"_defaultStartDebugAction",
",",
"successAction",
"or",
"_defaultSuccessDebugAction",
",",
"exceptionAction",
"or",
"_defaultExceptionDebugAction",
")",
"self",
".",
"debug",
"=",
"True",
"return",
"self"
] |
Enable display of debugging messages while doing pattern matching.
|
[
"Enable",
"display",
"of",
"debugging",
"messages",
"while",
"doing",
"pattern",
"matching",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L2277-L2285
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParserElement.setDebug
|
def setDebug( self, flag=True ):
"""
Enable display of debugging messages while doing pattern matching.
Set ``flag`` to True to enable, False to disable.
Example::
wd = Word(alphas).setName("alphaword")
integer = Word(nums).setName("numword")
term = wd | integer
# turn on debugging for wd
wd.setDebug()
OneOrMore(term).parseString("abc 123 xyz 890")
prints::
Match alphaword at loc 0(1,1)
Matched alphaword -> ['abc']
Match alphaword at loc 3(1,4)
Exception raised:Expected alphaword (at char 4), (line:1, col:5)
Match alphaword at loc 7(1,8)
Matched alphaword -> ['xyz']
Match alphaword at loc 11(1,12)
Exception raised:Expected alphaword (at char 12), (line:1, col:13)
Match alphaword at loc 15(1,16)
Exception raised:Expected alphaword (at char 15), (line:1, col:16)
The output shown is that produced by the default debug actions - custom debug actions can be
specified using :class:`setDebugActions`. Prior to attempting
to match the ``wd`` expression, the debugging message ``"Match <exprname> at loc <n>(<line>,<col>)"``
is shown. Then if the parse succeeds, a ``"Matched"`` message is shown, or an ``"Exception raised"``
message is shown. Also note the use of :class:`setName` to assign a human-readable name to the expression,
which makes debugging and exception messages easier to understand - for instance, the default
name created for the :class:`Word` expression without calling ``setName`` is ``"W:(ABCD...)"``.
"""
if flag:
self.setDebugActions( _defaultStartDebugAction, _defaultSuccessDebugAction, _defaultExceptionDebugAction )
else:
self.debug = False
return self
|
python
|
def setDebug( self, flag=True ):
"""
Enable display of debugging messages while doing pattern matching.
Set ``flag`` to True to enable, False to disable.
Example::
wd = Word(alphas).setName("alphaword")
integer = Word(nums).setName("numword")
term = wd | integer
# turn on debugging for wd
wd.setDebug()
OneOrMore(term).parseString("abc 123 xyz 890")
prints::
Match alphaword at loc 0(1,1)
Matched alphaword -> ['abc']
Match alphaword at loc 3(1,4)
Exception raised:Expected alphaword (at char 4), (line:1, col:5)
Match alphaword at loc 7(1,8)
Matched alphaword -> ['xyz']
Match alphaword at loc 11(1,12)
Exception raised:Expected alphaword (at char 12), (line:1, col:13)
Match alphaword at loc 15(1,16)
Exception raised:Expected alphaword (at char 15), (line:1, col:16)
The output shown is that produced by the default debug actions - custom debug actions can be
specified using :class:`setDebugActions`. Prior to attempting
to match the ``wd`` expression, the debugging message ``"Match <exprname> at loc <n>(<line>,<col>)"``
is shown. Then if the parse succeeds, a ``"Matched"`` message is shown, or an ``"Exception raised"``
message is shown. Also note the use of :class:`setName` to assign a human-readable name to the expression,
which makes debugging and exception messages easier to understand - for instance, the default
name created for the :class:`Word` expression without calling ``setName`` is ``"W:(ABCD...)"``.
"""
if flag:
self.setDebugActions( _defaultStartDebugAction, _defaultSuccessDebugAction, _defaultExceptionDebugAction )
else:
self.debug = False
return self
|
[
"def",
"setDebug",
"(",
"self",
",",
"flag",
"=",
"True",
")",
":",
"if",
"flag",
":",
"self",
".",
"setDebugActions",
"(",
"_defaultStartDebugAction",
",",
"_defaultSuccessDebugAction",
",",
"_defaultExceptionDebugAction",
")",
"else",
":",
"self",
".",
"debug",
"=",
"False",
"return",
"self"
] |
Enable display of debugging messages while doing pattern matching.
Set ``flag`` to True to enable, False to disable.
Example::
wd = Word(alphas).setName("alphaword")
integer = Word(nums).setName("numword")
term = wd | integer
# turn on debugging for wd
wd.setDebug()
OneOrMore(term).parseString("abc 123 xyz 890")
prints::
Match alphaword at loc 0(1,1)
Matched alphaword -> ['abc']
Match alphaword at loc 3(1,4)
Exception raised:Expected alphaword (at char 4), (line:1, col:5)
Match alphaword at loc 7(1,8)
Matched alphaword -> ['xyz']
Match alphaword at loc 11(1,12)
Exception raised:Expected alphaword (at char 12), (line:1, col:13)
Match alphaword at loc 15(1,16)
Exception raised:Expected alphaword (at char 15), (line:1, col:16)
The output shown is that produced by the default debug actions - custom debug actions can be
specified using :class:`setDebugActions`. Prior to attempting
to match the ``wd`` expression, the debugging message ``"Match <exprname> at loc <n>(<line>,<col>)"``
is shown. Then if the parse succeeds, a ``"Matched"`` message is shown, or an ``"Exception raised"``
message is shown. Also note the use of :class:`setName` to assign a human-readable name to the expression,
which makes debugging and exception messages easier to understand - for instance, the default
name created for the :class:`Word` expression without calling ``setName`` is ``"W:(ABCD...)"``.
|
[
"Enable",
"display",
"of",
"debugging",
"messages",
"while",
"doing",
"pattern",
"matching",
".",
"Set",
"flag",
"to",
"True",
"to",
"enable",
"False",
"to",
"disable",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L2287-L2328
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParserElement.parseFile
|
def parseFile( self, file_or_filename, parseAll=False ):
"""
Execute the parse expression on the given file or filename.
If a filename is specified (instead of a file object),
the entire file is opened, read, and closed before parsing.
"""
try:
file_contents = file_or_filename.read()
except AttributeError:
with open(file_or_filename, "r") as f:
file_contents = f.read()
try:
return self.parseString(file_contents, parseAll)
except ParseBaseException as exc:
if ParserElement.verbose_stacktrace:
raise
else:
# catch and re-raise exception from here, clears out pyparsing internal stack trace
raise exc
|
python
|
def parseFile( self, file_or_filename, parseAll=False ):
"""
Execute the parse expression on the given file or filename.
If a filename is specified (instead of a file object),
the entire file is opened, read, and closed before parsing.
"""
try:
file_contents = file_or_filename.read()
except AttributeError:
with open(file_or_filename, "r") as f:
file_contents = f.read()
try:
return self.parseString(file_contents, parseAll)
except ParseBaseException as exc:
if ParserElement.verbose_stacktrace:
raise
else:
# catch and re-raise exception from here, clears out pyparsing internal stack trace
raise exc
|
[
"def",
"parseFile",
"(",
"self",
",",
"file_or_filename",
",",
"parseAll",
"=",
"False",
")",
":",
"try",
":",
"file_contents",
"=",
"file_or_filename",
".",
"read",
"(",
")",
"except",
"AttributeError",
":",
"with",
"open",
"(",
"file_or_filename",
",",
"\"r\"",
")",
"as",
"f",
":",
"file_contents",
"=",
"f",
".",
"read",
"(",
")",
"try",
":",
"return",
"self",
".",
"parseString",
"(",
"file_contents",
",",
"parseAll",
")",
"except",
"ParseBaseException",
"as",
"exc",
":",
"if",
"ParserElement",
".",
"verbose_stacktrace",
":",
"raise",
"else",
":",
"# catch and re-raise exception from here, clears out pyparsing internal stack trace",
"raise",
"exc"
] |
Execute the parse expression on the given file or filename.
If a filename is specified (instead of a file object),
the entire file is opened, read, and closed before parsing.
|
[
"Execute",
"the",
"parse",
"expression",
"on",
"the",
"given",
"file",
"or",
"filename",
".",
"If",
"a",
"filename",
"is",
"specified",
"(",
"instead",
"of",
"a",
"file",
"object",
")",
"the",
"entire",
"file",
"is",
"opened",
"read",
"and",
"closed",
"before",
"parsing",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L2350-L2368
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
Regex.sub
|
def sub(self, repl):
"""
Return Regex with an attached parse action to transform the parsed
result as if called using `re.sub(expr, repl, string) <https://docs.python.org/3/library/re.html#re.sub>`_.
Example::
make_html = Regex(r"(\w+):(.*?):").sub(r"<\1>\2</\1>")
print(make_html.transformString("h1:main title:"))
# prints "<h1>main title</h1>"
"""
if self.asGroupList:
warnings.warn("cannot use sub() with Regex(asGroupList=True)",
SyntaxWarning, stacklevel=2)
raise SyntaxError()
if self.asMatch and callable(repl):
warnings.warn("cannot use sub() with a callable with Regex(asMatch=True)",
SyntaxWarning, stacklevel=2)
raise SyntaxError()
if self.asMatch:
def pa(tokens):
return tokens[0].expand(repl)
else:
def pa(tokens):
return self.re.sub(repl, tokens[0])
return self.addParseAction(pa)
|
python
|
def sub(self, repl):
"""
Return Regex with an attached parse action to transform the parsed
result as if called using `re.sub(expr, repl, string) <https://docs.python.org/3/library/re.html#re.sub>`_.
Example::
make_html = Regex(r"(\w+):(.*?):").sub(r"<\1>\2</\1>")
print(make_html.transformString("h1:main title:"))
# prints "<h1>main title</h1>"
"""
if self.asGroupList:
warnings.warn("cannot use sub() with Regex(asGroupList=True)",
SyntaxWarning, stacklevel=2)
raise SyntaxError()
if self.asMatch and callable(repl):
warnings.warn("cannot use sub() with a callable with Regex(asMatch=True)",
SyntaxWarning, stacklevel=2)
raise SyntaxError()
if self.asMatch:
def pa(tokens):
return tokens[0].expand(repl)
else:
def pa(tokens):
return self.re.sub(repl, tokens[0])
return self.addParseAction(pa)
|
[
"def",
"sub",
"(",
"self",
",",
"repl",
")",
":",
"if",
"self",
".",
"asGroupList",
":",
"warnings",
".",
"warn",
"(",
"\"cannot use sub() with Regex(asGroupList=True)\"",
",",
"SyntaxWarning",
",",
"stacklevel",
"=",
"2",
")",
"raise",
"SyntaxError",
"(",
")",
"if",
"self",
".",
"asMatch",
"and",
"callable",
"(",
"repl",
")",
":",
"warnings",
".",
"warn",
"(",
"\"cannot use sub() with a callable with Regex(asMatch=True)\"",
",",
"SyntaxWarning",
",",
"stacklevel",
"=",
"2",
")",
"raise",
"SyntaxError",
"(",
")",
"if",
"self",
".",
"asMatch",
":",
"def",
"pa",
"(",
"tokens",
")",
":",
"return",
"tokens",
"[",
"0",
"]",
".",
"expand",
"(",
"repl",
")",
"else",
":",
"def",
"pa",
"(",
"tokens",
")",
":",
"return",
"self",
".",
"re",
".",
"sub",
"(",
"repl",
",",
"tokens",
"[",
"0",
"]",
")",
"return",
"self",
".",
"addParseAction",
"(",
"pa",
")"
] |
Return Regex with an attached parse action to transform the parsed
result as if called using `re.sub(expr, repl, string) <https://docs.python.org/3/library/re.html#re.sub>`_.
Example::
make_html = Regex(r"(\w+):(.*?):").sub(r"<\1>\2</\1>")
print(make_html.transformString("h1:main title:"))
# prints "<h1>main title</h1>"
|
[
"Return",
"Regex",
"with",
"an",
"attached",
"parse",
"action",
"to",
"transform",
"the",
"parsed",
"result",
"as",
"if",
"called",
"using",
"re",
".",
"sub",
"(",
"expr",
"repl",
"string",
")",
"<https",
":",
"//",
"docs",
".",
"python",
".",
"org",
"/",
"3",
"/",
"library",
"/",
"re",
".",
"html#re",
".",
"sub",
">",
"_",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L3067-L3094
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
ParseExpression.leaveWhitespace
|
def leaveWhitespace( self ):
"""Extends ``leaveWhitespace`` defined in base class, and also invokes ``leaveWhitespace`` on
all contained expressions."""
self.skipWhitespace = False
self.exprs = [ e.copy() for e in self.exprs ]
for e in self.exprs:
e.leaveWhitespace()
return self
|
python
|
def leaveWhitespace( self ):
"""Extends ``leaveWhitespace`` defined in base class, and also invokes ``leaveWhitespace`` on
all contained expressions."""
self.skipWhitespace = False
self.exprs = [ e.copy() for e in self.exprs ]
for e in self.exprs:
e.leaveWhitespace()
return self
|
[
"def",
"leaveWhitespace",
"(",
"self",
")",
":",
"self",
".",
"skipWhitespace",
"=",
"False",
"self",
".",
"exprs",
"=",
"[",
"e",
".",
"copy",
"(",
")",
"for",
"e",
"in",
"self",
".",
"exprs",
"]",
"for",
"e",
"in",
"self",
".",
"exprs",
":",
"e",
".",
"leaveWhitespace",
"(",
")",
"return",
"self"
] |
Extends ``leaveWhitespace`` defined in base class, and also invokes ``leaveWhitespace`` on
all contained expressions.
|
[
"Extends",
"leaveWhitespace",
"defined",
"in",
"base",
"class",
"and",
"also",
"invokes",
"leaveWhitespace",
"on",
"all",
"contained",
"expressions",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L3586-L3593
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
pyparsing_common.convertToDate
|
def convertToDate(fmt="%Y-%m-%d"):
"""
Helper to create a parse action for converting parsed date string to Python datetime.date
Params -
- fmt - format to be passed to datetime.strptime (default= ``"%Y-%m-%d"``)
Example::
date_expr = pyparsing_common.iso8601_date.copy()
date_expr.setParseAction(pyparsing_common.convertToDate())
print(date_expr.parseString("1999-12-31"))
prints::
[datetime.date(1999, 12, 31)]
"""
def cvt_fn(s,l,t):
try:
return datetime.strptime(t[0], fmt).date()
except ValueError as ve:
raise ParseException(s, l, str(ve))
return cvt_fn
|
python
|
def convertToDate(fmt="%Y-%m-%d"):
"""
Helper to create a parse action for converting parsed date string to Python datetime.date
Params -
- fmt - format to be passed to datetime.strptime (default= ``"%Y-%m-%d"``)
Example::
date_expr = pyparsing_common.iso8601_date.copy()
date_expr.setParseAction(pyparsing_common.convertToDate())
print(date_expr.parseString("1999-12-31"))
prints::
[datetime.date(1999, 12, 31)]
"""
def cvt_fn(s,l,t):
try:
return datetime.strptime(t[0], fmt).date()
except ValueError as ve:
raise ParseException(s, l, str(ve))
return cvt_fn
|
[
"def",
"convertToDate",
"(",
"fmt",
"=",
"\"%Y-%m-%d\"",
")",
":",
"def",
"cvt_fn",
"(",
"s",
",",
"l",
",",
"t",
")",
":",
"try",
":",
"return",
"datetime",
".",
"strptime",
"(",
"t",
"[",
"0",
"]",
",",
"fmt",
")",
".",
"date",
"(",
")",
"except",
"ValueError",
"as",
"ve",
":",
"raise",
"ParseException",
"(",
"s",
",",
"l",
",",
"str",
"(",
"ve",
")",
")",
"return",
"cvt_fn"
] |
Helper to create a parse action for converting parsed date string to Python datetime.date
Params -
- fmt - format to be passed to datetime.strptime (default= ``"%Y-%m-%d"``)
Example::
date_expr = pyparsing_common.iso8601_date.copy()
date_expr.setParseAction(pyparsing_common.convertToDate())
print(date_expr.parseString("1999-12-31"))
prints::
[datetime.date(1999, 12, 31)]
|
[
"Helper",
"to",
"create",
"a",
"parse",
"action",
"for",
"converting",
"parsed",
"date",
"string",
"to",
"Python",
"datetime",
".",
"date"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L6136-L6158
|
train
|
pypa/pipenv
|
pipenv/vendor/pyparsing.py
|
pyparsing_common.convertToDatetime
|
def convertToDatetime(fmt="%Y-%m-%dT%H:%M:%S.%f"):
"""Helper to create a parse action for converting parsed
datetime string to Python datetime.datetime
Params -
- fmt - format to be passed to datetime.strptime (default= ``"%Y-%m-%dT%H:%M:%S.%f"``)
Example::
dt_expr = pyparsing_common.iso8601_datetime.copy()
dt_expr.setParseAction(pyparsing_common.convertToDatetime())
print(dt_expr.parseString("1999-12-31T23:59:59.999"))
prints::
[datetime.datetime(1999, 12, 31, 23, 59, 59, 999000)]
"""
def cvt_fn(s,l,t):
try:
return datetime.strptime(t[0], fmt)
except ValueError as ve:
raise ParseException(s, l, str(ve))
return cvt_fn
|
python
|
def convertToDatetime(fmt="%Y-%m-%dT%H:%M:%S.%f"):
"""Helper to create a parse action for converting parsed
datetime string to Python datetime.datetime
Params -
- fmt - format to be passed to datetime.strptime (default= ``"%Y-%m-%dT%H:%M:%S.%f"``)
Example::
dt_expr = pyparsing_common.iso8601_datetime.copy()
dt_expr.setParseAction(pyparsing_common.convertToDatetime())
print(dt_expr.parseString("1999-12-31T23:59:59.999"))
prints::
[datetime.datetime(1999, 12, 31, 23, 59, 59, 999000)]
"""
def cvt_fn(s,l,t):
try:
return datetime.strptime(t[0], fmt)
except ValueError as ve:
raise ParseException(s, l, str(ve))
return cvt_fn
|
[
"def",
"convertToDatetime",
"(",
"fmt",
"=",
"\"%Y-%m-%dT%H:%M:%S.%f\"",
")",
":",
"def",
"cvt_fn",
"(",
"s",
",",
"l",
",",
"t",
")",
":",
"try",
":",
"return",
"datetime",
".",
"strptime",
"(",
"t",
"[",
"0",
"]",
",",
"fmt",
")",
"except",
"ValueError",
"as",
"ve",
":",
"raise",
"ParseException",
"(",
"s",
",",
"l",
",",
"str",
"(",
"ve",
")",
")",
"return",
"cvt_fn"
] |
Helper to create a parse action for converting parsed
datetime string to Python datetime.datetime
Params -
- fmt - format to be passed to datetime.strptime (default= ``"%Y-%m-%dT%H:%M:%S.%f"``)
Example::
dt_expr = pyparsing_common.iso8601_datetime.copy()
dt_expr.setParseAction(pyparsing_common.convertToDatetime())
print(dt_expr.parseString("1999-12-31T23:59:59.999"))
prints::
[datetime.datetime(1999, 12, 31, 23, 59, 59, 999000)]
|
[
"Helper",
"to",
"create",
"a",
"parse",
"action",
"for",
"converting",
"parsed",
"datetime",
"string",
"to",
"Python",
"datetime",
".",
"datetime"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pyparsing.py#L6161-L6183
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
_match_vcs_scheme
|
def _match_vcs_scheme(url):
# type: (str) -> Optional[str]
"""Look for VCS schemes in the URL.
Returns the matched VCS scheme, or None if there's no match.
"""
from pipenv.patched.notpip._internal.vcs import VcsSupport
for scheme in VcsSupport.schemes:
if url.lower().startswith(scheme) and url[len(scheme)] in '+:':
return scheme
return None
|
python
|
def _match_vcs_scheme(url):
# type: (str) -> Optional[str]
"""Look for VCS schemes in the URL.
Returns the matched VCS scheme, or None if there's no match.
"""
from pipenv.patched.notpip._internal.vcs import VcsSupport
for scheme in VcsSupport.schemes:
if url.lower().startswith(scheme) and url[len(scheme)] in '+:':
return scheme
return None
|
[
"def",
"_match_vcs_scheme",
"(",
"url",
")",
":",
"# type: (str) -> Optional[str]",
"from",
"pipenv",
".",
"patched",
".",
"notpip",
".",
"_internal",
".",
"vcs",
"import",
"VcsSupport",
"for",
"scheme",
"in",
"VcsSupport",
".",
"schemes",
":",
"if",
"url",
".",
"lower",
"(",
")",
".",
"startswith",
"(",
"scheme",
")",
"and",
"url",
"[",
"len",
"(",
"scheme",
")",
"]",
"in",
"'+:'",
":",
"return",
"scheme",
"return",
"None"
] |
Look for VCS schemes in the URL.
Returns the matched VCS scheme, or None if there's no match.
|
[
"Look",
"for",
"VCS",
"schemes",
"in",
"the",
"URL",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L77-L87
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
_is_url_like_archive
|
def _is_url_like_archive(url):
# type: (str) -> bool
"""Return whether the URL looks like an archive.
"""
filename = Link(url).filename
for bad_ext in ARCHIVE_EXTENSIONS:
if filename.endswith(bad_ext):
return True
return False
|
python
|
def _is_url_like_archive(url):
# type: (str) -> bool
"""Return whether the URL looks like an archive.
"""
filename = Link(url).filename
for bad_ext in ARCHIVE_EXTENSIONS:
if filename.endswith(bad_ext):
return True
return False
|
[
"def",
"_is_url_like_archive",
"(",
"url",
")",
":",
"# type: (str) -> bool",
"filename",
"=",
"Link",
"(",
"url",
")",
".",
"filename",
"for",
"bad_ext",
"in",
"ARCHIVE_EXTENSIONS",
":",
"if",
"filename",
".",
"endswith",
"(",
"bad_ext",
")",
":",
"return",
"True",
"return",
"False"
] |
Return whether the URL looks like an archive.
|
[
"Return",
"whether",
"the",
"URL",
"looks",
"like",
"an",
"archive",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L90-L98
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
_ensure_html_header
|
def _ensure_html_header(response):
# type: (Response) -> None
"""Check the Content-Type header to ensure the response contains HTML.
Raises `_NotHTML` if the content type is not text/html.
"""
content_type = response.headers.get("Content-Type", "")
if not content_type.lower().startswith("text/html"):
raise _NotHTML(content_type, response.request.method)
|
python
|
def _ensure_html_header(response):
# type: (Response) -> None
"""Check the Content-Type header to ensure the response contains HTML.
Raises `_NotHTML` if the content type is not text/html.
"""
content_type = response.headers.get("Content-Type", "")
if not content_type.lower().startswith("text/html"):
raise _NotHTML(content_type, response.request.method)
|
[
"def",
"_ensure_html_header",
"(",
"response",
")",
":",
"# type: (Response) -> None",
"content_type",
"=",
"response",
".",
"headers",
".",
"get",
"(",
"\"Content-Type\"",
",",
"\"\"",
")",
"if",
"not",
"content_type",
".",
"lower",
"(",
")",
".",
"startswith",
"(",
"\"text/html\"",
")",
":",
"raise",
"_NotHTML",
"(",
"content_type",
",",
"response",
".",
"request",
".",
"method",
")"
] |
Check the Content-Type header to ensure the response contains HTML.
Raises `_NotHTML` if the content type is not text/html.
|
[
"Check",
"the",
"Content",
"-",
"Type",
"header",
"to",
"ensure",
"the",
"response",
"contains",
"HTML",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L109-L117
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
_ensure_html_response
|
def _ensure_html_response(url, session):
# type: (str, PipSession) -> None
"""Send a HEAD request to the URL, and ensure the response contains HTML.
Raises `_NotHTTP` if the URL is not available for a HEAD request, or
`_NotHTML` if the content type is not text/html.
"""
scheme, netloc, path, query, fragment = urllib_parse.urlsplit(url)
if scheme not in {'http', 'https'}:
raise _NotHTTP()
resp = session.head(url, allow_redirects=True)
resp.raise_for_status()
_ensure_html_header(resp)
|
python
|
def _ensure_html_response(url, session):
# type: (str, PipSession) -> None
"""Send a HEAD request to the URL, and ensure the response contains HTML.
Raises `_NotHTTP` if the URL is not available for a HEAD request, or
`_NotHTML` if the content type is not text/html.
"""
scheme, netloc, path, query, fragment = urllib_parse.urlsplit(url)
if scheme not in {'http', 'https'}:
raise _NotHTTP()
resp = session.head(url, allow_redirects=True)
resp.raise_for_status()
_ensure_html_header(resp)
|
[
"def",
"_ensure_html_response",
"(",
"url",
",",
"session",
")",
":",
"# type: (str, PipSession) -> None",
"scheme",
",",
"netloc",
",",
"path",
",",
"query",
",",
"fragment",
"=",
"urllib_parse",
".",
"urlsplit",
"(",
"url",
")",
"if",
"scheme",
"not",
"in",
"{",
"'http'",
",",
"'https'",
"}",
":",
"raise",
"_NotHTTP",
"(",
")",
"resp",
"=",
"session",
".",
"head",
"(",
"url",
",",
"allow_redirects",
"=",
"True",
")",
"resp",
".",
"raise_for_status",
"(",
")",
"_ensure_html_header",
"(",
"resp",
")"
] |
Send a HEAD request to the URL, and ensure the response contains HTML.
Raises `_NotHTTP` if the URL is not available for a HEAD request, or
`_NotHTML` if the content type is not text/html.
|
[
"Send",
"a",
"HEAD",
"request",
"to",
"the",
"URL",
"and",
"ensure",
"the",
"response",
"contains",
"HTML",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L124-L138
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
_get_html_response
|
def _get_html_response(url, session):
# type: (str, PipSession) -> Response
"""Access an HTML page with GET, and return the response.
This consists of three parts:
1. If the URL looks suspiciously like an archive, send a HEAD first to
check the Content-Type is HTML, to avoid downloading a large file.
Raise `_NotHTTP` if the content type cannot be determined, or
`_NotHTML` if it is not HTML.
2. Actually perform the request. Raise HTTP exceptions on network failures.
3. Check the Content-Type header to make sure we got HTML, and raise
`_NotHTML` otherwise.
"""
if _is_url_like_archive(url):
_ensure_html_response(url, session=session)
logger.debug('Getting page %s', url)
resp = session.get(
url,
headers={
"Accept": "text/html",
# We don't want to blindly returned cached data for
# /simple/, because authors generally expecting that
# twine upload && pip install will function, but if
# they've done a pip install in the last ~10 minutes
# it won't. Thus by setting this to zero we will not
# blindly use any cached data, however the benefit of
# using max-age=0 instead of no-cache, is that we will
# still support conditional requests, so we will still
# minimize traffic sent in cases where the page hasn't
# changed at all, we will just always incur the round
# trip for the conditional GET now instead of only
# once per 10 minutes.
# For more information, please see pypa/pip#5670.
"Cache-Control": "max-age=0",
},
)
resp.raise_for_status()
# The check for archives above only works if the url ends with
# something that looks like an archive. However that is not a
# requirement of an url. Unless we issue a HEAD request on every
# url we cannot know ahead of time for sure if something is HTML
# or not. However we can check after we've downloaded it.
_ensure_html_header(resp)
return resp
|
python
|
def _get_html_response(url, session):
# type: (str, PipSession) -> Response
"""Access an HTML page with GET, and return the response.
This consists of three parts:
1. If the URL looks suspiciously like an archive, send a HEAD first to
check the Content-Type is HTML, to avoid downloading a large file.
Raise `_NotHTTP` if the content type cannot be determined, or
`_NotHTML` if it is not HTML.
2. Actually perform the request. Raise HTTP exceptions on network failures.
3. Check the Content-Type header to make sure we got HTML, and raise
`_NotHTML` otherwise.
"""
if _is_url_like_archive(url):
_ensure_html_response(url, session=session)
logger.debug('Getting page %s', url)
resp = session.get(
url,
headers={
"Accept": "text/html",
# We don't want to blindly returned cached data for
# /simple/, because authors generally expecting that
# twine upload && pip install will function, but if
# they've done a pip install in the last ~10 minutes
# it won't. Thus by setting this to zero we will not
# blindly use any cached data, however the benefit of
# using max-age=0 instead of no-cache, is that we will
# still support conditional requests, so we will still
# minimize traffic sent in cases where the page hasn't
# changed at all, we will just always incur the round
# trip for the conditional GET now instead of only
# once per 10 minutes.
# For more information, please see pypa/pip#5670.
"Cache-Control": "max-age=0",
},
)
resp.raise_for_status()
# The check for archives above only works if the url ends with
# something that looks like an archive. However that is not a
# requirement of an url. Unless we issue a HEAD request on every
# url we cannot know ahead of time for sure if something is HTML
# or not. However we can check after we've downloaded it.
_ensure_html_header(resp)
return resp
|
[
"def",
"_get_html_response",
"(",
"url",
",",
"session",
")",
":",
"# type: (str, PipSession) -> Response",
"if",
"_is_url_like_archive",
"(",
"url",
")",
":",
"_ensure_html_response",
"(",
"url",
",",
"session",
"=",
"session",
")",
"logger",
".",
"debug",
"(",
"'Getting page %s'",
",",
"url",
")",
"resp",
"=",
"session",
".",
"get",
"(",
"url",
",",
"headers",
"=",
"{",
"\"Accept\"",
":",
"\"text/html\"",
",",
"# We don't want to blindly returned cached data for",
"# /simple/, because authors generally expecting that",
"# twine upload && pip install will function, but if",
"# they've done a pip install in the last ~10 minutes",
"# it won't. Thus by setting this to zero we will not",
"# blindly use any cached data, however the benefit of",
"# using max-age=0 instead of no-cache, is that we will",
"# still support conditional requests, so we will still",
"# minimize traffic sent in cases where the page hasn't",
"# changed at all, we will just always incur the round",
"# trip for the conditional GET now instead of only",
"# once per 10 minutes.",
"# For more information, please see pypa/pip#5670.",
"\"Cache-Control\"",
":",
"\"max-age=0\"",
",",
"}",
",",
")",
"resp",
".",
"raise_for_status",
"(",
")",
"# The check for archives above only works if the url ends with",
"# something that looks like an archive. However that is not a",
"# requirement of an url. Unless we issue a HEAD request on every",
"# url we cannot know ahead of time for sure if something is HTML",
"# or not. However we can check after we've downloaded it.",
"_ensure_html_header",
"(",
"resp",
")",
"return",
"resp"
] |
Access an HTML page with GET, and return the response.
This consists of three parts:
1. If the URL looks suspiciously like an archive, send a HEAD first to
check the Content-Type is HTML, to avoid downloading a large file.
Raise `_NotHTTP` if the content type cannot be determined, or
`_NotHTML` if it is not HTML.
2. Actually perform the request. Raise HTTP exceptions on network failures.
3. Check the Content-Type header to make sure we got HTML, and raise
`_NotHTML` otherwise.
|
[
"Access",
"an",
"HTML",
"page",
"with",
"GET",
"and",
"return",
"the",
"response",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L141-L189
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
_find_name_version_sep
|
def _find_name_version_sep(egg_info, canonical_name):
# type: (str, str) -> int
"""Find the separator's index based on the package's canonical name.
`egg_info` must be an egg info string for the given package, and
`canonical_name` must be the package's canonical name.
This function is needed since the canonicalized name does not necessarily
have the same length as the egg info's name part. An example::
>>> egg_info = 'foo__bar-1.0'
>>> canonical_name = 'foo-bar'
>>> _find_name_version_sep(egg_info, canonical_name)
8
"""
# Project name and version must be separated by one single dash. Find all
# occurrences of dashes; if the string in front of it matches the canonical
# name, this is the one separating the name and version parts.
for i, c in enumerate(egg_info):
if c != "-":
continue
if canonicalize_name(egg_info[:i]) == canonical_name:
return i
raise ValueError("{} does not match {}".format(egg_info, canonical_name))
|
python
|
def _find_name_version_sep(egg_info, canonical_name):
# type: (str, str) -> int
"""Find the separator's index based on the package's canonical name.
`egg_info` must be an egg info string for the given package, and
`canonical_name` must be the package's canonical name.
This function is needed since the canonicalized name does not necessarily
have the same length as the egg info's name part. An example::
>>> egg_info = 'foo__bar-1.0'
>>> canonical_name = 'foo-bar'
>>> _find_name_version_sep(egg_info, canonical_name)
8
"""
# Project name and version must be separated by one single dash. Find all
# occurrences of dashes; if the string in front of it matches the canonical
# name, this is the one separating the name and version parts.
for i, c in enumerate(egg_info):
if c != "-":
continue
if canonicalize_name(egg_info[:i]) == canonical_name:
return i
raise ValueError("{} does not match {}".format(egg_info, canonical_name))
|
[
"def",
"_find_name_version_sep",
"(",
"egg_info",
",",
"canonical_name",
")",
":",
"# type: (str, str) -> int",
"# Project name and version must be separated by one single dash. Find all",
"# occurrences of dashes; if the string in front of it matches the canonical",
"# name, this is the one separating the name and version parts.",
"for",
"i",
",",
"c",
"in",
"enumerate",
"(",
"egg_info",
")",
":",
"if",
"c",
"!=",
"\"-\"",
":",
"continue",
"if",
"canonicalize_name",
"(",
"egg_info",
"[",
":",
"i",
"]",
")",
"==",
"canonical_name",
":",
"return",
"i",
"raise",
"ValueError",
"(",
"\"{} does not match {}\"",
".",
"format",
"(",
"egg_info",
",",
"canonical_name",
")",
")"
] |
Find the separator's index based on the package's canonical name.
`egg_info` must be an egg info string for the given package, and
`canonical_name` must be the package's canonical name.
This function is needed since the canonicalized name does not necessarily
have the same length as the egg info's name part. An example::
>>> egg_info = 'foo__bar-1.0'
>>> canonical_name = 'foo-bar'
>>> _find_name_version_sep(egg_info, canonical_name)
8
|
[
"Find",
"the",
"separator",
"s",
"index",
"based",
"on",
"the",
"package",
"s",
"canonical",
"name",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L896-L919
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
_egg_info_matches
|
def _egg_info_matches(egg_info, canonical_name):
# type: (str, str) -> Optional[str]
"""Pull the version part out of a string.
:param egg_info: The string to parse. E.g. foo-2.1
:param canonical_name: The canonicalized name of the package this
belongs to.
"""
try:
version_start = _find_name_version_sep(egg_info, canonical_name) + 1
except ValueError:
return None
version = egg_info[version_start:]
if not version:
return None
return version
|
python
|
def _egg_info_matches(egg_info, canonical_name):
# type: (str, str) -> Optional[str]
"""Pull the version part out of a string.
:param egg_info: The string to parse. E.g. foo-2.1
:param canonical_name: The canonicalized name of the package this
belongs to.
"""
try:
version_start = _find_name_version_sep(egg_info, canonical_name) + 1
except ValueError:
return None
version = egg_info[version_start:]
if not version:
return None
return version
|
[
"def",
"_egg_info_matches",
"(",
"egg_info",
",",
"canonical_name",
")",
":",
"# type: (str, str) -> Optional[str]",
"try",
":",
"version_start",
"=",
"_find_name_version_sep",
"(",
"egg_info",
",",
"canonical_name",
")",
"+",
"1",
"except",
"ValueError",
":",
"return",
"None",
"version",
"=",
"egg_info",
"[",
"version_start",
":",
"]",
"if",
"not",
"version",
":",
"return",
"None",
"return",
"version"
] |
Pull the version part out of a string.
:param egg_info: The string to parse. E.g. foo-2.1
:param canonical_name: The canonicalized name of the package this
belongs to.
|
[
"Pull",
"the",
"version",
"part",
"out",
"of",
"a",
"string",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L922-L937
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
_determine_base_url
|
def _determine_base_url(document, page_url):
"""Determine the HTML document's base URL.
This looks for a ``<base>`` tag in the HTML document. If present, its href
attribute denotes the base URL of anchor tags in the document. If there is
no such tag (or if it does not have a valid href attribute), the HTML
file's URL is used as the base URL.
:param document: An HTML document representation. The current
implementation expects the result of ``html5lib.parse()``.
:param page_url: The URL of the HTML document.
"""
for base in document.findall(".//base"):
href = base.get("href")
if href is not None:
return href
return page_url
|
python
|
def _determine_base_url(document, page_url):
"""Determine the HTML document's base URL.
This looks for a ``<base>`` tag in the HTML document. If present, its href
attribute denotes the base URL of anchor tags in the document. If there is
no such tag (or if it does not have a valid href attribute), the HTML
file's URL is used as the base URL.
:param document: An HTML document representation. The current
implementation expects the result of ``html5lib.parse()``.
:param page_url: The URL of the HTML document.
"""
for base in document.findall(".//base"):
href = base.get("href")
if href is not None:
return href
return page_url
|
[
"def",
"_determine_base_url",
"(",
"document",
",",
"page_url",
")",
":",
"for",
"base",
"in",
"document",
".",
"findall",
"(",
"\".//base\"",
")",
":",
"href",
"=",
"base",
".",
"get",
"(",
"\"href\"",
")",
"if",
"href",
"is",
"not",
"None",
":",
"return",
"href",
"return",
"page_url"
] |
Determine the HTML document's base URL.
This looks for a ``<base>`` tag in the HTML document. If present, its href
attribute denotes the base URL of anchor tags in the document. If there is
no such tag (or if it does not have a valid href attribute), the HTML
file's URL is used as the base URL.
:param document: An HTML document representation. The current
implementation expects the result of ``html5lib.parse()``.
:param page_url: The URL of the HTML document.
|
[
"Determine",
"the",
"HTML",
"document",
"s",
"base",
"URL",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L940-L956
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
_get_encoding_from_headers
|
def _get_encoding_from_headers(headers):
"""Determine if we have any encoding information in our headers.
"""
if headers and "Content-Type" in headers:
content_type, params = cgi.parse_header(headers["Content-Type"])
if "charset" in params:
return params['charset']
return None
|
python
|
def _get_encoding_from_headers(headers):
"""Determine if we have any encoding information in our headers.
"""
if headers and "Content-Type" in headers:
content_type, params = cgi.parse_header(headers["Content-Type"])
if "charset" in params:
return params['charset']
return None
|
[
"def",
"_get_encoding_from_headers",
"(",
"headers",
")",
":",
"if",
"headers",
"and",
"\"Content-Type\"",
"in",
"headers",
":",
"content_type",
",",
"params",
"=",
"cgi",
".",
"parse_header",
"(",
"headers",
"[",
"\"Content-Type\"",
"]",
")",
"if",
"\"charset\"",
"in",
"params",
":",
"return",
"params",
"[",
"'charset'",
"]",
"return",
"None"
] |
Determine if we have any encoding information in our headers.
|
[
"Determine",
"if",
"we",
"have",
"any",
"encoding",
"information",
"in",
"our",
"headers",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L959-L966
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
PackageFinder._candidate_sort_key
|
def _candidate_sort_key(self, candidate, ignore_compatibility=True):
# type: (InstallationCandidate, bool) -> CandidateSortingKey
"""
Function used to generate link sort key for link tuples.
The greater the return value, the more preferred it is.
If not finding wheels, then sorted by version only.
If finding wheels, then the sort order is by version, then:
1. existing installs
2. wheels ordered via Wheel.support_index_min(self.valid_tags)
3. source archives
If prefer_binary was set, then all wheels are sorted above sources.
Note: it was considered to embed this logic into the Link
comparison operators, but then different sdist links
with the same version, would have to be considered equal
"""
support_num = len(self.valid_tags)
build_tag = tuple() # type: BuildTag
binary_preference = 0
if candidate.location.is_wheel:
# can raise InvalidWheelFilename
wheel = Wheel(candidate.location.filename)
if not wheel.supported(self.valid_tags) and not ignore_compatibility:
raise UnsupportedWheel(
"%s is not a supported wheel for this platform. It "
"can't be sorted." % wheel.filename
)
if self.prefer_binary:
binary_preference = 1
tags = self.valid_tags if not ignore_compatibility else None
try:
pri = -(wheel.support_index_min(tags=tags))
except TypeError:
pri = -(support_num)
if wheel.build_tag is not None:
match = re.match(r'^(\d+)(.*)$', wheel.build_tag)
build_tag_groups = match.groups()
build_tag = (int(build_tag_groups[0]), build_tag_groups[1])
else: # sdist
pri = -(support_num)
return (binary_preference, candidate.version, build_tag, pri)
|
python
|
def _candidate_sort_key(self, candidate, ignore_compatibility=True):
# type: (InstallationCandidate, bool) -> CandidateSortingKey
"""
Function used to generate link sort key for link tuples.
The greater the return value, the more preferred it is.
If not finding wheels, then sorted by version only.
If finding wheels, then the sort order is by version, then:
1. existing installs
2. wheels ordered via Wheel.support_index_min(self.valid_tags)
3. source archives
If prefer_binary was set, then all wheels are sorted above sources.
Note: it was considered to embed this logic into the Link
comparison operators, but then different sdist links
with the same version, would have to be considered equal
"""
support_num = len(self.valid_tags)
build_tag = tuple() # type: BuildTag
binary_preference = 0
if candidate.location.is_wheel:
# can raise InvalidWheelFilename
wheel = Wheel(candidate.location.filename)
if not wheel.supported(self.valid_tags) and not ignore_compatibility:
raise UnsupportedWheel(
"%s is not a supported wheel for this platform. It "
"can't be sorted." % wheel.filename
)
if self.prefer_binary:
binary_preference = 1
tags = self.valid_tags if not ignore_compatibility else None
try:
pri = -(wheel.support_index_min(tags=tags))
except TypeError:
pri = -(support_num)
if wheel.build_tag is not None:
match = re.match(r'^(\d+)(.*)$', wheel.build_tag)
build_tag_groups = match.groups()
build_tag = (int(build_tag_groups[0]), build_tag_groups[1])
else: # sdist
pri = -(support_num)
return (binary_preference, candidate.version, build_tag, pri)
|
[
"def",
"_candidate_sort_key",
"(",
"self",
",",
"candidate",
",",
"ignore_compatibility",
"=",
"True",
")",
":",
"# type: (InstallationCandidate, bool) -> CandidateSortingKey",
"support_num",
"=",
"len",
"(",
"self",
".",
"valid_tags",
")",
"build_tag",
"=",
"tuple",
"(",
")",
"# type: BuildTag",
"binary_preference",
"=",
"0",
"if",
"candidate",
".",
"location",
".",
"is_wheel",
":",
"# can raise InvalidWheelFilename",
"wheel",
"=",
"Wheel",
"(",
"candidate",
".",
"location",
".",
"filename",
")",
"if",
"not",
"wheel",
".",
"supported",
"(",
"self",
".",
"valid_tags",
")",
"and",
"not",
"ignore_compatibility",
":",
"raise",
"UnsupportedWheel",
"(",
"\"%s is not a supported wheel for this platform. It \"",
"\"can't be sorted.\"",
"%",
"wheel",
".",
"filename",
")",
"if",
"self",
".",
"prefer_binary",
":",
"binary_preference",
"=",
"1",
"tags",
"=",
"self",
".",
"valid_tags",
"if",
"not",
"ignore_compatibility",
"else",
"None",
"try",
":",
"pri",
"=",
"-",
"(",
"wheel",
".",
"support_index_min",
"(",
"tags",
"=",
"tags",
")",
")",
"except",
"TypeError",
":",
"pri",
"=",
"-",
"(",
"support_num",
")",
"if",
"wheel",
".",
"build_tag",
"is",
"not",
"None",
":",
"match",
"=",
"re",
".",
"match",
"(",
"r'^(\\d+)(.*)$'",
",",
"wheel",
".",
"build_tag",
")",
"build_tag_groups",
"=",
"match",
".",
"groups",
"(",
")",
"build_tag",
"=",
"(",
"int",
"(",
"build_tag_groups",
"[",
"0",
"]",
")",
",",
"build_tag_groups",
"[",
"1",
"]",
")",
"else",
":",
"# sdist",
"pri",
"=",
"-",
"(",
"support_num",
")",
"return",
"(",
"binary_preference",
",",
"candidate",
".",
"version",
",",
"build_tag",
",",
"pri",
")"
] |
Function used to generate link sort key for link tuples.
The greater the return value, the more preferred it is.
If not finding wheels, then sorted by version only.
If finding wheels, then the sort order is by version, then:
1. existing installs
2. wheels ordered via Wheel.support_index_min(self.valid_tags)
3. source archives
If prefer_binary was set, then all wheels are sorted above sources.
Note: it was considered to embed this logic into the Link
comparison operators, but then different sdist links
with the same version, would have to be considered equal
|
[
"Function",
"used",
"to",
"generate",
"link",
"sort",
"key",
"for",
"link",
"tuples",
".",
"The",
"greater",
"the",
"return",
"value",
"the",
"more",
"preferred",
"it",
"is",
".",
"If",
"not",
"finding",
"wheels",
"then",
"sorted",
"by",
"version",
"only",
".",
"If",
"finding",
"wheels",
"then",
"the",
"sort",
"order",
"is",
"by",
"version",
"then",
":",
"1",
".",
"existing",
"installs",
"2",
".",
"wheels",
"ordered",
"via",
"Wheel",
".",
"support_index_min",
"(",
"self",
".",
"valid_tags",
")",
"3",
".",
"source",
"archives",
"If",
"prefer_binary",
"was",
"set",
"then",
"all",
"wheels",
"are",
"sorted",
"above",
"sources",
".",
"Note",
":",
"it",
"was",
"considered",
"to",
"embed",
"this",
"logic",
"into",
"the",
"Link",
"comparison",
"operators",
"but",
"then",
"different",
"sdist",
"links",
"with",
"the",
"same",
"version",
"would",
"have",
"to",
"be",
"considered",
"equal"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L450-L489
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
PackageFinder._get_index_urls_locations
|
def _get_index_urls_locations(self, project_name):
# type: (str) -> List[str]
"""Returns the locations found via self.index_urls
Checks the url_name on the main (first in the list) index and
use this url_name to produce all locations
"""
def mkurl_pypi_url(url):
loc = posixpath.join(
url,
urllib_parse.quote(canonicalize_name(project_name)))
# For maximum compatibility with easy_install, ensure the path
# ends in a trailing slash. Although this isn't in the spec
# (and PyPI can handle it without the slash) some other index
# implementations might break if they relied on easy_install's
# behavior.
if not loc.endswith('/'):
loc = loc + '/'
return loc
return [mkurl_pypi_url(url) for url in self.index_urls]
|
python
|
def _get_index_urls_locations(self, project_name):
# type: (str) -> List[str]
"""Returns the locations found via self.index_urls
Checks the url_name on the main (first in the list) index and
use this url_name to produce all locations
"""
def mkurl_pypi_url(url):
loc = posixpath.join(
url,
urllib_parse.quote(canonicalize_name(project_name)))
# For maximum compatibility with easy_install, ensure the path
# ends in a trailing slash. Although this isn't in the spec
# (and PyPI can handle it without the slash) some other index
# implementations might break if they relied on easy_install's
# behavior.
if not loc.endswith('/'):
loc = loc + '/'
return loc
return [mkurl_pypi_url(url) for url in self.index_urls]
|
[
"def",
"_get_index_urls_locations",
"(",
"self",
",",
"project_name",
")",
":",
"# type: (str) -> List[str]",
"def",
"mkurl_pypi_url",
"(",
"url",
")",
":",
"loc",
"=",
"posixpath",
".",
"join",
"(",
"url",
",",
"urllib_parse",
".",
"quote",
"(",
"canonicalize_name",
"(",
"project_name",
")",
")",
")",
"# For maximum compatibility with easy_install, ensure the path",
"# ends in a trailing slash. Although this isn't in the spec",
"# (and PyPI can handle it without the slash) some other index",
"# implementations might break if they relied on easy_install's",
"# behavior.",
"if",
"not",
"loc",
".",
"endswith",
"(",
"'/'",
")",
":",
"loc",
"=",
"loc",
"+",
"'/'",
"return",
"loc",
"return",
"[",
"mkurl_pypi_url",
"(",
"url",
")",
"for",
"url",
"in",
"self",
".",
"index_urls",
"]"
] |
Returns the locations found via self.index_urls
Checks the url_name on the main (first in the list) index and
use this url_name to produce all locations
|
[
"Returns",
"the",
"locations",
"found",
"via",
"self",
".",
"index_urls"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L565-L586
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
PackageFinder.find_all_candidates
|
def find_all_candidates(self, project_name):
# type: (str) -> List[Optional[InstallationCandidate]]
"""Find all available InstallationCandidate for project_name
This checks index_urls and find_links.
All versions found are returned as an InstallationCandidate list.
See _link_package_versions for details on which files are accepted
"""
index_locations = self._get_index_urls_locations(project_name)
index_file_loc, index_url_loc = self._sort_locations(index_locations)
fl_file_loc, fl_url_loc = self._sort_locations(
self.find_links, expand_dir=True,
)
file_locations = (Link(url) for url in itertools.chain(
index_file_loc, fl_file_loc,
))
# We trust every url that the user has given us whether it was given
# via --index-url or --find-links.
# We want to filter out any thing which does not have a secure origin.
url_locations = [
link for link in itertools.chain(
(Link(url) for url in index_url_loc),
(Link(url) for url in fl_url_loc),
)
if self._validate_secure_origin(logger, link)
]
logger.debug('%d location(s) to search for versions of %s:',
len(url_locations), project_name)
for location in url_locations:
logger.debug('* %s', location)
canonical_name = canonicalize_name(project_name)
formats = self.format_control.get_allowed_formats(canonical_name)
search = Search(project_name, canonical_name, formats)
find_links_versions = self._package_versions(
# We trust every directly linked archive in find_links
(Link(url, '-f') for url in self.find_links),
search
)
page_versions = []
for page in self._get_pages(url_locations, project_name):
try:
logger.debug('Analyzing links from page %s', page.url)
except AttributeError:
continue
with indent_log():
page_versions.extend(
self._package_versions(page.iter_links(), search)
)
file_versions = self._package_versions(file_locations, search)
if file_versions:
file_versions.sort(reverse=True)
logger.debug(
'Local files found: %s',
', '.join([
url_to_path(candidate.location.url)
for candidate in file_versions
])
)
# This is an intentional priority ordering
return file_versions + find_links_versions + page_versions
|
python
|
def find_all_candidates(self, project_name):
# type: (str) -> List[Optional[InstallationCandidate]]
"""Find all available InstallationCandidate for project_name
This checks index_urls and find_links.
All versions found are returned as an InstallationCandidate list.
See _link_package_versions for details on which files are accepted
"""
index_locations = self._get_index_urls_locations(project_name)
index_file_loc, index_url_loc = self._sort_locations(index_locations)
fl_file_loc, fl_url_loc = self._sort_locations(
self.find_links, expand_dir=True,
)
file_locations = (Link(url) for url in itertools.chain(
index_file_loc, fl_file_loc,
))
# We trust every url that the user has given us whether it was given
# via --index-url or --find-links.
# We want to filter out any thing which does not have a secure origin.
url_locations = [
link for link in itertools.chain(
(Link(url) for url in index_url_loc),
(Link(url) for url in fl_url_loc),
)
if self._validate_secure_origin(logger, link)
]
logger.debug('%d location(s) to search for versions of %s:',
len(url_locations), project_name)
for location in url_locations:
logger.debug('* %s', location)
canonical_name = canonicalize_name(project_name)
formats = self.format_control.get_allowed_formats(canonical_name)
search = Search(project_name, canonical_name, formats)
find_links_versions = self._package_versions(
# We trust every directly linked archive in find_links
(Link(url, '-f') for url in self.find_links),
search
)
page_versions = []
for page in self._get_pages(url_locations, project_name):
try:
logger.debug('Analyzing links from page %s', page.url)
except AttributeError:
continue
with indent_log():
page_versions.extend(
self._package_versions(page.iter_links(), search)
)
file_versions = self._package_versions(file_locations, search)
if file_versions:
file_versions.sort(reverse=True)
logger.debug(
'Local files found: %s',
', '.join([
url_to_path(candidate.location.url)
for candidate in file_versions
])
)
# This is an intentional priority ordering
return file_versions + find_links_versions + page_versions
|
[
"def",
"find_all_candidates",
"(",
"self",
",",
"project_name",
")",
":",
"# type: (str) -> List[Optional[InstallationCandidate]]",
"index_locations",
"=",
"self",
".",
"_get_index_urls_locations",
"(",
"project_name",
")",
"index_file_loc",
",",
"index_url_loc",
"=",
"self",
".",
"_sort_locations",
"(",
"index_locations",
")",
"fl_file_loc",
",",
"fl_url_loc",
"=",
"self",
".",
"_sort_locations",
"(",
"self",
".",
"find_links",
",",
"expand_dir",
"=",
"True",
",",
")",
"file_locations",
"=",
"(",
"Link",
"(",
"url",
")",
"for",
"url",
"in",
"itertools",
".",
"chain",
"(",
"index_file_loc",
",",
"fl_file_loc",
",",
")",
")",
"# We trust every url that the user has given us whether it was given",
"# via --index-url or --find-links.",
"# We want to filter out any thing which does not have a secure origin.",
"url_locations",
"=",
"[",
"link",
"for",
"link",
"in",
"itertools",
".",
"chain",
"(",
"(",
"Link",
"(",
"url",
")",
"for",
"url",
"in",
"index_url_loc",
")",
",",
"(",
"Link",
"(",
"url",
")",
"for",
"url",
"in",
"fl_url_loc",
")",
",",
")",
"if",
"self",
".",
"_validate_secure_origin",
"(",
"logger",
",",
"link",
")",
"]",
"logger",
".",
"debug",
"(",
"'%d location(s) to search for versions of %s:'",
",",
"len",
"(",
"url_locations",
")",
",",
"project_name",
")",
"for",
"location",
"in",
"url_locations",
":",
"logger",
".",
"debug",
"(",
"'* %s'",
",",
"location",
")",
"canonical_name",
"=",
"canonicalize_name",
"(",
"project_name",
")",
"formats",
"=",
"self",
".",
"format_control",
".",
"get_allowed_formats",
"(",
"canonical_name",
")",
"search",
"=",
"Search",
"(",
"project_name",
",",
"canonical_name",
",",
"formats",
")",
"find_links_versions",
"=",
"self",
".",
"_package_versions",
"(",
"# We trust every directly linked archive in find_links",
"(",
"Link",
"(",
"url",
",",
"'-f'",
")",
"for",
"url",
"in",
"self",
".",
"find_links",
")",
",",
"search",
")",
"page_versions",
"=",
"[",
"]",
"for",
"page",
"in",
"self",
".",
"_get_pages",
"(",
"url_locations",
",",
"project_name",
")",
":",
"try",
":",
"logger",
".",
"debug",
"(",
"'Analyzing links from page %s'",
",",
"page",
".",
"url",
")",
"except",
"AttributeError",
":",
"continue",
"with",
"indent_log",
"(",
")",
":",
"page_versions",
".",
"extend",
"(",
"self",
".",
"_package_versions",
"(",
"page",
".",
"iter_links",
"(",
")",
",",
"search",
")",
")",
"file_versions",
"=",
"self",
".",
"_package_versions",
"(",
"file_locations",
",",
"search",
")",
"if",
"file_versions",
":",
"file_versions",
".",
"sort",
"(",
"reverse",
"=",
"True",
")",
"logger",
".",
"debug",
"(",
"'Local files found: %s'",
",",
"', '",
".",
"join",
"(",
"[",
"url_to_path",
"(",
"candidate",
".",
"location",
".",
"url",
")",
"for",
"candidate",
"in",
"file_versions",
"]",
")",
")",
"# This is an intentional priority ordering",
"return",
"file_versions",
"+",
"find_links_versions",
"+",
"page_versions"
] |
Find all available InstallationCandidate for project_name
This checks index_urls and find_links.
All versions found are returned as an InstallationCandidate list.
See _link_package_versions for details on which files are accepted
|
[
"Find",
"all",
"available",
"InstallationCandidate",
"for",
"project_name"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L588-L656
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
PackageFinder.find_requirement
|
def find_requirement(self, req, upgrade, ignore_compatibility=False):
# type: (InstallRequirement, bool, bool) -> Optional[Link]
"""Try to find a Link matching req
Expects req, an InstallRequirement and upgrade, a boolean
Returns a Link if found,
Raises DistributionNotFound or BestVersionAlreadyInstalled otherwise
"""
all_candidates = self.find_all_candidates(req.name)
# Filter out anything which doesn't match our specifier
compatible_versions = set(
req.specifier.filter(
# We turn the version object into a str here because otherwise
# when we're debundled but setuptools isn't, Python will see
# packaging.version.Version and
# pkg_resources._vendor.packaging.version.Version as different
# types. This way we'll use a str as a common data interchange
# format. If we stop using the pkg_resources provided specifier
# and start using our own, we can drop the cast to str().
[str(c.version) for c in all_candidates],
prereleases=(
self.allow_all_prereleases
if self.allow_all_prereleases else None
),
)
)
applicable_candidates = [
# Again, converting to str to deal with debundling.
c for c in all_candidates if str(c.version) in compatible_versions
]
if applicable_candidates:
best_candidate = max(applicable_candidates,
key=self._candidate_sort_key)
else:
best_candidate = None
if req.satisfied_by is not None:
installed_version = parse_version(req.satisfied_by.version)
else:
installed_version = None
if installed_version is None and best_candidate is None:
logger.critical(
'Could not find a version that satisfies the requirement %s '
'(from versions: %s)',
req,
', '.join(
sorted(
{str(c.version) for c in all_candidates},
key=parse_version,
)
)
)
raise DistributionNotFound(
'No matching distribution found for %s' % req
)
best_installed = False
if installed_version and (
best_candidate is None or
best_candidate.version <= installed_version):
best_installed = True
if not upgrade and installed_version is not None:
if best_installed:
logger.debug(
'Existing installed version (%s) is most up-to-date and '
'satisfies requirement',
installed_version,
)
else:
logger.debug(
'Existing installed version (%s) satisfies requirement '
'(most up-to-date version is %s)',
installed_version,
best_candidate.version,
)
return None
if best_installed:
# We have an existing version, and its the best version
logger.debug(
'Installed version (%s) is most up-to-date (past versions: '
'%s)',
installed_version,
', '.join(sorted(compatible_versions, key=parse_version)) or
"none",
)
raise BestVersionAlreadyInstalled
logger.debug(
'Using version %s (newest of versions: %s)',
best_candidate.version,
', '.join(sorted(compatible_versions, key=parse_version))
)
return best_candidate.location
|
python
|
def find_requirement(self, req, upgrade, ignore_compatibility=False):
# type: (InstallRequirement, bool, bool) -> Optional[Link]
"""Try to find a Link matching req
Expects req, an InstallRequirement and upgrade, a boolean
Returns a Link if found,
Raises DistributionNotFound or BestVersionAlreadyInstalled otherwise
"""
all_candidates = self.find_all_candidates(req.name)
# Filter out anything which doesn't match our specifier
compatible_versions = set(
req.specifier.filter(
# We turn the version object into a str here because otherwise
# when we're debundled but setuptools isn't, Python will see
# packaging.version.Version and
# pkg_resources._vendor.packaging.version.Version as different
# types. This way we'll use a str as a common data interchange
# format. If we stop using the pkg_resources provided specifier
# and start using our own, we can drop the cast to str().
[str(c.version) for c in all_candidates],
prereleases=(
self.allow_all_prereleases
if self.allow_all_prereleases else None
),
)
)
applicable_candidates = [
# Again, converting to str to deal with debundling.
c for c in all_candidates if str(c.version) in compatible_versions
]
if applicable_candidates:
best_candidate = max(applicable_candidates,
key=self._candidate_sort_key)
else:
best_candidate = None
if req.satisfied_by is not None:
installed_version = parse_version(req.satisfied_by.version)
else:
installed_version = None
if installed_version is None and best_candidate is None:
logger.critical(
'Could not find a version that satisfies the requirement %s '
'(from versions: %s)',
req,
', '.join(
sorted(
{str(c.version) for c in all_candidates},
key=parse_version,
)
)
)
raise DistributionNotFound(
'No matching distribution found for %s' % req
)
best_installed = False
if installed_version and (
best_candidate is None or
best_candidate.version <= installed_version):
best_installed = True
if not upgrade and installed_version is not None:
if best_installed:
logger.debug(
'Existing installed version (%s) is most up-to-date and '
'satisfies requirement',
installed_version,
)
else:
logger.debug(
'Existing installed version (%s) satisfies requirement '
'(most up-to-date version is %s)',
installed_version,
best_candidate.version,
)
return None
if best_installed:
# We have an existing version, and its the best version
logger.debug(
'Installed version (%s) is most up-to-date (past versions: '
'%s)',
installed_version,
', '.join(sorted(compatible_versions, key=parse_version)) or
"none",
)
raise BestVersionAlreadyInstalled
logger.debug(
'Using version %s (newest of versions: %s)',
best_candidate.version,
', '.join(sorted(compatible_versions, key=parse_version))
)
return best_candidate.location
|
[
"def",
"find_requirement",
"(",
"self",
",",
"req",
",",
"upgrade",
",",
"ignore_compatibility",
"=",
"False",
")",
":",
"# type: (InstallRequirement, bool, bool) -> Optional[Link]",
"all_candidates",
"=",
"self",
".",
"find_all_candidates",
"(",
"req",
".",
"name",
")",
"# Filter out anything which doesn't match our specifier",
"compatible_versions",
"=",
"set",
"(",
"req",
".",
"specifier",
".",
"filter",
"(",
"# We turn the version object into a str here because otherwise",
"# when we're debundled but setuptools isn't, Python will see",
"# packaging.version.Version and",
"# pkg_resources._vendor.packaging.version.Version as different",
"# types. This way we'll use a str as a common data interchange",
"# format. If we stop using the pkg_resources provided specifier",
"# and start using our own, we can drop the cast to str().",
"[",
"str",
"(",
"c",
".",
"version",
")",
"for",
"c",
"in",
"all_candidates",
"]",
",",
"prereleases",
"=",
"(",
"self",
".",
"allow_all_prereleases",
"if",
"self",
".",
"allow_all_prereleases",
"else",
"None",
")",
",",
")",
")",
"applicable_candidates",
"=",
"[",
"# Again, converting to str to deal with debundling.",
"c",
"for",
"c",
"in",
"all_candidates",
"if",
"str",
"(",
"c",
".",
"version",
")",
"in",
"compatible_versions",
"]",
"if",
"applicable_candidates",
":",
"best_candidate",
"=",
"max",
"(",
"applicable_candidates",
",",
"key",
"=",
"self",
".",
"_candidate_sort_key",
")",
"else",
":",
"best_candidate",
"=",
"None",
"if",
"req",
".",
"satisfied_by",
"is",
"not",
"None",
":",
"installed_version",
"=",
"parse_version",
"(",
"req",
".",
"satisfied_by",
".",
"version",
")",
"else",
":",
"installed_version",
"=",
"None",
"if",
"installed_version",
"is",
"None",
"and",
"best_candidate",
"is",
"None",
":",
"logger",
".",
"critical",
"(",
"'Could not find a version that satisfies the requirement %s '",
"'(from versions: %s)'",
",",
"req",
",",
"', '",
".",
"join",
"(",
"sorted",
"(",
"{",
"str",
"(",
"c",
".",
"version",
")",
"for",
"c",
"in",
"all_candidates",
"}",
",",
"key",
"=",
"parse_version",
",",
")",
")",
")",
"raise",
"DistributionNotFound",
"(",
"'No matching distribution found for %s'",
"%",
"req",
")",
"best_installed",
"=",
"False",
"if",
"installed_version",
"and",
"(",
"best_candidate",
"is",
"None",
"or",
"best_candidate",
".",
"version",
"<=",
"installed_version",
")",
":",
"best_installed",
"=",
"True",
"if",
"not",
"upgrade",
"and",
"installed_version",
"is",
"not",
"None",
":",
"if",
"best_installed",
":",
"logger",
".",
"debug",
"(",
"'Existing installed version (%s) is most up-to-date and '",
"'satisfies requirement'",
",",
"installed_version",
",",
")",
"else",
":",
"logger",
".",
"debug",
"(",
"'Existing installed version (%s) satisfies requirement '",
"'(most up-to-date version is %s)'",
",",
"installed_version",
",",
"best_candidate",
".",
"version",
",",
")",
"return",
"None",
"if",
"best_installed",
":",
"# We have an existing version, and its the best version",
"logger",
".",
"debug",
"(",
"'Installed version (%s) is most up-to-date (past versions: '",
"'%s)'",
",",
"installed_version",
",",
"', '",
".",
"join",
"(",
"sorted",
"(",
"compatible_versions",
",",
"key",
"=",
"parse_version",
")",
")",
"or",
"\"none\"",
",",
")",
"raise",
"BestVersionAlreadyInstalled",
"logger",
".",
"debug",
"(",
"'Using version %s (newest of versions: %s)'",
",",
"best_candidate",
".",
"version",
",",
"', '",
".",
"join",
"(",
"sorted",
"(",
"compatible_versions",
",",
"key",
"=",
"parse_version",
")",
")",
")",
"return",
"best_candidate",
".",
"location"
] |
Try to find a Link matching req
Expects req, an InstallRequirement and upgrade, a boolean
Returns a Link if found,
Raises DistributionNotFound or BestVersionAlreadyInstalled otherwise
|
[
"Try",
"to",
"find",
"a",
"Link",
"matching",
"req"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L658-L756
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
PackageFinder._get_pages
|
def _get_pages(self, locations, project_name):
# type: (Iterable[Link], str) -> Iterable[HTMLPage]
"""
Yields (page, page_url) from the given locations, skipping
locations that have errors.
"""
seen = set() # type: Set[Link]
for location in locations:
if location in seen:
continue
seen.add(location)
page = _get_html_page(location, session=self.session)
if page is None:
continue
yield page
|
python
|
def _get_pages(self, locations, project_name):
# type: (Iterable[Link], str) -> Iterable[HTMLPage]
"""
Yields (page, page_url) from the given locations, skipping
locations that have errors.
"""
seen = set() # type: Set[Link]
for location in locations:
if location in seen:
continue
seen.add(location)
page = _get_html_page(location, session=self.session)
if page is None:
continue
yield page
|
[
"def",
"_get_pages",
"(",
"self",
",",
"locations",
",",
"project_name",
")",
":",
"# type: (Iterable[Link], str) -> Iterable[HTMLPage]",
"seen",
"=",
"set",
"(",
")",
"# type: Set[Link]",
"for",
"location",
"in",
"locations",
":",
"if",
"location",
"in",
"seen",
":",
"continue",
"seen",
".",
"add",
"(",
"location",
")",
"page",
"=",
"_get_html_page",
"(",
"location",
",",
"session",
"=",
"self",
".",
"session",
")",
"if",
"page",
"is",
"None",
":",
"continue",
"yield",
"page"
] |
Yields (page, page_url) from the given locations, skipping
locations that have errors.
|
[
"Yields",
"(",
"page",
"page_url",
")",
"from",
"the",
"given",
"locations",
"skipping",
"locations",
"that",
"have",
"errors",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L758-L774
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
PackageFinder._link_package_versions
|
def _link_package_versions(self, link, search, ignore_compatibility=True):
# type: (Link, Search, bool) -> Optional[InstallationCandidate]
"""Return an InstallationCandidate or None"""
version = None
if link.egg_fragment:
egg_info = link.egg_fragment
ext = link.ext
else:
egg_info, ext = link.splitext()
if not ext:
self._log_skipped_link(link, 'not a file')
return None
if ext not in SUPPORTED_EXTENSIONS:
self._log_skipped_link(
link, 'unsupported archive format: %s' % ext,
)
return None
if "binary" not in search.formats and ext == WHEEL_EXTENSION and not ignore_compatibility:
self._log_skipped_link(
link, 'No binaries permitted for %s' % search.supplied,
)
return None
if "macosx10" in link.path and ext == '.zip' and not ignore_compatibility:
self._log_skipped_link(link, 'macosx10 one')
return None
if ext == WHEEL_EXTENSION:
try:
wheel = Wheel(link.filename)
except InvalidWheelFilename:
self._log_skipped_link(link, 'invalid wheel filename')
return None
if canonicalize_name(wheel.name) != search.canonical:
self._log_skipped_link(
link, 'wrong project name (not %s)' % search.supplied)
return None
if not wheel.supported(self.valid_tags) and not ignore_compatibility:
self._log_skipped_link(
link, 'it is not compatible with this Python')
return None
version = wheel.version
# This should be up by the search.ok_binary check, but see issue 2700.
if "source" not in search.formats and ext != WHEEL_EXTENSION:
self._log_skipped_link(
link, 'No sources permitted for %s' % search.supplied,
)
return None
if not version:
version = _egg_info_matches(egg_info, search.canonical)
if not version:
self._log_skipped_link(
link, 'Missing project version for %s' % search.supplied)
return None
match = self._py_version_re.search(version)
if match:
version = version[:match.start()]
py_version = match.group(1)
if py_version != sys.version[:3]:
self._log_skipped_link(
link, 'Python version is incorrect')
return None
try:
support_this_python = check_requires_python(link.requires_python)
except specifiers.InvalidSpecifier:
logger.debug("Package %s has an invalid Requires-Python entry: %s",
link.filename, link.requires_python)
support_this_python = True
if not support_this_python and not ignore_compatibility:
logger.debug("The package %s is incompatible with the python "
"version in use. Acceptable python versions are: %s",
link, link.requires_python)
return None
logger.debug('Found link %s, version: %s', link, version)
return InstallationCandidate(search.supplied, version, link, link.requires_python)
|
python
|
def _link_package_versions(self, link, search, ignore_compatibility=True):
# type: (Link, Search, bool) -> Optional[InstallationCandidate]
"""Return an InstallationCandidate or None"""
version = None
if link.egg_fragment:
egg_info = link.egg_fragment
ext = link.ext
else:
egg_info, ext = link.splitext()
if not ext:
self._log_skipped_link(link, 'not a file')
return None
if ext not in SUPPORTED_EXTENSIONS:
self._log_skipped_link(
link, 'unsupported archive format: %s' % ext,
)
return None
if "binary" not in search.formats and ext == WHEEL_EXTENSION and not ignore_compatibility:
self._log_skipped_link(
link, 'No binaries permitted for %s' % search.supplied,
)
return None
if "macosx10" in link.path and ext == '.zip' and not ignore_compatibility:
self._log_skipped_link(link, 'macosx10 one')
return None
if ext == WHEEL_EXTENSION:
try:
wheel = Wheel(link.filename)
except InvalidWheelFilename:
self._log_skipped_link(link, 'invalid wheel filename')
return None
if canonicalize_name(wheel.name) != search.canonical:
self._log_skipped_link(
link, 'wrong project name (not %s)' % search.supplied)
return None
if not wheel.supported(self.valid_tags) and not ignore_compatibility:
self._log_skipped_link(
link, 'it is not compatible with this Python')
return None
version = wheel.version
# This should be up by the search.ok_binary check, but see issue 2700.
if "source" not in search.formats and ext != WHEEL_EXTENSION:
self._log_skipped_link(
link, 'No sources permitted for %s' % search.supplied,
)
return None
if not version:
version = _egg_info_matches(egg_info, search.canonical)
if not version:
self._log_skipped_link(
link, 'Missing project version for %s' % search.supplied)
return None
match = self._py_version_re.search(version)
if match:
version = version[:match.start()]
py_version = match.group(1)
if py_version != sys.version[:3]:
self._log_skipped_link(
link, 'Python version is incorrect')
return None
try:
support_this_python = check_requires_python(link.requires_python)
except specifiers.InvalidSpecifier:
logger.debug("Package %s has an invalid Requires-Python entry: %s",
link.filename, link.requires_python)
support_this_python = True
if not support_this_python and not ignore_compatibility:
logger.debug("The package %s is incompatible with the python "
"version in use. Acceptable python versions are: %s",
link, link.requires_python)
return None
logger.debug('Found link %s, version: %s', link, version)
return InstallationCandidate(search.supplied, version, link, link.requires_python)
|
[
"def",
"_link_package_versions",
"(",
"self",
",",
"link",
",",
"search",
",",
"ignore_compatibility",
"=",
"True",
")",
":",
"# type: (Link, Search, bool) -> Optional[InstallationCandidate]",
"version",
"=",
"None",
"if",
"link",
".",
"egg_fragment",
":",
"egg_info",
"=",
"link",
".",
"egg_fragment",
"ext",
"=",
"link",
".",
"ext",
"else",
":",
"egg_info",
",",
"ext",
"=",
"link",
".",
"splitext",
"(",
")",
"if",
"not",
"ext",
":",
"self",
".",
"_log_skipped_link",
"(",
"link",
",",
"'not a file'",
")",
"return",
"None",
"if",
"ext",
"not",
"in",
"SUPPORTED_EXTENSIONS",
":",
"self",
".",
"_log_skipped_link",
"(",
"link",
",",
"'unsupported archive format: %s'",
"%",
"ext",
",",
")",
"return",
"None",
"if",
"\"binary\"",
"not",
"in",
"search",
".",
"formats",
"and",
"ext",
"==",
"WHEEL_EXTENSION",
"and",
"not",
"ignore_compatibility",
":",
"self",
".",
"_log_skipped_link",
"(",
"link",
",",
"'No binaries permitted for %s'",
"%",
"search",
".",
"supplied",
",",
")",
"return",
"None",
"if",
"\"macosx10\"",
"in",
"link",
".",
"path",
"and",
"ext",
"==",
"'.zip'",
"and",
"not",
"ignore_compatibility",
":",
"self",
".",
"_log_skipped_link",
"(",
"link",
",",
"'macosx10 one'",
")",
"return",
"None",
"if",
"ext",
"==",
"WHEEL_EXTENSION",
":",
"try",
":",
"wheel",
"=",
"Wheel",
"(",
"link",
".",
"filename",
")",
"except",
"InvalidWheelFilename",
":",
"self",
".",
"_log_skipped_link",
"(",
"link",
",",
"'invalid wheel filename'",
")",
"return",
"None",
"if",
"canonicalize_name",
"(",
"wheel",
".",
"name",
")",
"!=",
"search",
".",
"canonical",
":",
"self",
".",
"_log_skipped_link",
"(",
"link",
",",
"'wrong project name (not %s)'",
"%",
"search",
".",
"supplied",
")",
"return",
"None",
"if",
"not",
"wheel",
".",
"supported",
"(",
"self",
".",
"valid_tags",
")",
"and",
"not",
"ignore_compatibility",
":",
"self",
".",
"_log_skipped_link",
"(",
"link",
",",
"'it is not compatible with this Python'",
")",
"return",
"None",
"version",
"=",
"wheel",
".",
"version",
"# This should be up by the search.ok_binary check, but see issue 2700.",
"if",
"\"source\"",
"not",
"in",
"search",
".",
"formats",
"and",
"ext",
"!=",
"WHEEL_EXTENSION",
":",
"self",
".",
"_log_skipped_link",
"(",
"link",
",",
"'No sources permitted for %s'",
"%",
"search",
".",
"supplied",
",",
")",
"return",
"None",
"if",
"not",
"version",
":",
"version",
"=",
"_egg_info_matches",
"(",
"egg_info",
",",
"search",
".",
"canonical",
")",
"if",
"not",
"version",
":",
"self",
".",
"_log_skipped_link",
"(",
"link",
",",
"'Missing project version for %s'",
"%",
"search",
".",
"supplied",
")",
"return",
"None",
"match",
"=",
"self",
".",
"_py_version_re",
".",
"search",
"(",
"version",
")",
"if",
"match",
":",
"version",
"=",
"version",
"[",
":",
"match",
".",
"start",
"(",
")",
"]",
"py_version",
"=",
"match",
".",
"group",
"(",
"1",
")",
"if",
"py_version",
"!=",
"sys",
".",
"version",
"[",
":",
"3",
"]",
":",
"self",
".",
"_log_skipped_link",
"(",
"link",
",",
"'Python version is incorrect'",
")",
"return",
"None",
"try",
":",
"support_this_python",
"=",
"check_requires_python",
"(",
"link",
".",
"requires_python",
")",
"except",
"specifiers",
".",
"InvalidSpecifier",
":",
"logger",
".",
"debug",
"(",
"\"Package %s has an invalid Requires-Python entry: %s\"",
",",
"link",
".",
"filename",
",",
"link",
".",
"requires_python",
")",
"support_this_python",
"=",
"True",
"if",
"not",
"support_this_python",
"and",
"not",
"ignore_compatibility",
":",
"logger",
".",
"debug",
"(",
"\"The package %s is incompatible with the python \"",
"\"version in use. Acceptable python versions are: %s\"",
",",
"link",
",",
"link",
".",
"requires_python",
")",
"return",
"None",
"logger",
".",
"debug",
"(",
"'Found link %s, version: %s'",
",",
"link",
",",
"version",
")",
"return",
"InstallationCandidate",
"(",
"search",
".",
"supplied",
",",
"version",
",",
"link",
",",
"link",
".",
"requires_python",
")"
] |
Return an InstallationCandidate or None
|
[
"Return",
"an",
"InstallationCandidate",
"or",
"None"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L814-L893
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/index.py
|
HTMLPage.iter_links
|
def iter_links(self):
# type: () -> Iterable[Link]
"""Yields all links in the page"""
document = html5lib.parse(
self.content,
transport_encoding=_get_encoding_from_headers(self.headers),
namespaceHTMLElements=False,
)
base_url = _determine_base_url(document, self.url)
for anchor in document.findall(".//a"):
if anchor.get("href"):
href = anchor.get("href")
url = _clean_link(urllib_parse.urljoin(base_url, href))
pyrequire = anchor.get('data-requires-python')
pyrequire = unescape(pyrequire) if pyrequire else None
yield Link(url, self.url, requires_python=pyrequire)
|
python
|
def iter_links(self):
# type: () -> Iterable[Link]
"""Yields all links in the page"""
document = html5lib.parse(
self.content,
transport_encoding=_get_encoding_from_headers(self.headers),
namespaceHTMLElements=False,
)
base_url = _determine_base_url(document, self.url)
for anchor in document.findall(".//a"):
if anchor.get("href"):
href = anchor.get("href")
url = _clean_link(urllib_parse.urljoin(base_url, href))
pyrequire = anchor.get('data-requires-python')
pyrequire = unescape(pyrequire) if pyrequire else None
yield Link(url, self.url, requires_python=pyrequire)
|
[
"def",
"iter_links",
"(",
"self",
")",
":",
"# type: () -> Iterable[Link]",
"document",
"=",
"html5lib",
".",
"parse",
"(",
"self",
".",
"content",
",",
"transport_encoding",
"=",
"_get_encoding_from_headers",
"(",
"self",
".",
"headers",
")",
",",
"namespaceHTMLElements",
"=",
"False",
",",
")",
"base_url",
"=",
"_determine_base_url",
"(",
"document",
",",
"self",
".",
"url",
")",
"for",
"anchor",
"in",
"document",
".",
"findall",
"(",
"\".//a\"",
")",
":",
"if",
"anchor",
".",
"get",
"(",
"\"href\"",
")",
":",
"href",
"=",
"anchor",
".",
"get",
"(",
"\"href\"",
")",
"url",
"=",
"_clean_link",
"(",
"urllib_parse",
".",
"urljoin",
"(",
"base_url",
",",
"href",
")",
")",
"pyrequire",
"=",
"anchor",
".",
"get",
"(",
"'data-requires-python'",
")",
"pyrequire",
"=",
"unescape",
"(",
"pyrequire",
")",
"if",
"pyrequire",
"else",
"None",
"yield",
"Link",
"(",
"url",
",",
"self",
".",
"url",
",",
"requires_python",
"=",
"pyrequire",
")"
] |
Yields all links in the page
|
[
"Yields",
"all",
"links",
"in",
"the",
"page"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/index.py#L992-L1007
|
train
|
pypa/pipenv
|
pipenv/vendor/pexpect/run.py
|
run
|
def run(command, timeout=30, withexitstatus=False, events=None,
extra_args=None, logfile=None, cwd=None, env=None, **kwargs):
'''
This function runs the given command; waits for it to finish; then
returns all output as a string. STDERR is included in output. If the full
path to the command is not given then the path is searched.
Note that lines are terminated by CR/LF (\\r\\n) combination even on
UNIX-like systems because this is the standard for pseudottys. If you set
'withexitstatus' to true, then run will return a tuple of (command_output,
exitstatus). If 'withexitstatus' is false then this returns just
command_output.
The run() function can often be used instead of creating a spawn instance.
For example, the following code uses spawn::
from pexpect import *
child = spawn('scp foo user@example.com:.')
child.expect('(?i)password')
child.sendline(mypassword)
The previous code can be replace with the following::
from pexpect import *
run('scp foo user@example.com:.', events={'(?i)password': mypassword})
**Examples**
Start the apache daemon on the local machine::
from pexpect import *
run("/usr/local/apache/bin/apachectl start")
Check in a file using SVN::
from pexpect import *
run("svn ci -m 'automatic commit' my_file.py")
Run a command and capture exit status::
from pexpect import *
(command_output, exitstatus) = run('ls -l /bin', withexitstatus=1)
The following will run SSH and execute 'ls -l' on the remote machine. The
password 'secret' will be sent if the '(?i)password' pattern is ever seen::
run("ssh username@machine.example.com 'ls -l'",
events={'(?i)password':'secret\\n'})
This will start mencoder to rip a video from DVD. This will also display
progress ticks every 5 seconds as it runs. For example::
from pexpect import *
def print_ticks(d):
print d['event_count'],
run("mencoder dvd://1 -o video.avi -oac copy -ovc copy",
events={TIMEOUT:print_ticks}, timeout=5)
The 'events' argument should be either a dictionary or a tuple list that
contains patterns and responses. Whenever one of the patterns is seen
in the command output, run() will send the associated response string.
So, run() in the above example can be also written as:
run("mencoder dvd://1 -o video.avi -oac copy -ovc copy",
events=[(TIMEOUT,print_ticks)], timeout=5)
Use a tuple list for events if the command output requires a delicate
control over what pattern should be matched, since the tuple list is passed
to pexpect() as its pattern list, with the order of patterns preserved.
Note that you should put newlines in your string if Enter is necessary.
Like the example above, the responses may also contain a callback, either
a function or method. It should accept a dictionary value as an argument.
The dictionary contains all the locals from the run() function, so you can
access the child spawn object or any other variable defined in run()
(event_count, child, and extra_args are the most useful). A callback may
return True to stop the current run process. Otherwise run() continues
until the next event. A callback may also return a string which will be
sent to the child. 'extra_args' is not used by directly run(). It provides
a way to pass data to a callback function through run() through the locals
dictionary passed to a callback.
Like :class:`spawn`, passing *encoding* will make it work with unicode
instead of bytes. You can pass *codec_errors* to control how errors in
encoding and decoding are handled.
'''
if timeout == -1:
child = spawn(command, maxread=2000, logfile=logfile, cwd=cwd, env=env,
**kwargs)
else:
child = spawn(command, timeout=timeout, maxread=2000, logfile=logfile,
cwd=cwd, env=env, **kwargs)
if isinstance(events, list):
patterns= [x for x,y in events]
responses = [y for x,y in events]
elif isinstance(events, dict):
patterns = list(events.keys())
responses = list(events.values())
else:
# This assumes EOF or TIMEOUT will eventually cause run to terminate.
patterns = None
responses = None
child_result_list = []
event_count = 0
while True:
try:
index = child.expect(patterns)
if isinstance(child.after, child.allowed_string_types):
child_result_list.append(child.before + child.after)
else:
# child.after may have been a TIMEOUT or EOF,
# which we don't want appended to the list.
child_result_list.append(child.before)
if isinstance(responses[index], child.allowed_string_types):
child.send(responses[index])
elif (isinstance(responses[index], types.FunctionType) or
isinstance(responses[index], types.MethodType)):
callback_result = responses[index](locals())
sys.stdout.flush()
if isinstance(callback_result, child.allowed_string_types):
child.send(callback_result)
elif callback_result:
break
else:
raise TypeError("parameter `event' at index {index} must be "
"a string, method, or function: {value!r}"
.format(index=index, value=responses[index]))
event_count = event_count + 1
except TIMEOUT:
child_result_list.append(child.before)
break
except EOF:
child_result_list.append(child.before)
break
child_result = child.string_type().join(child_result_list)
if withexitstatus:
child.close()
return (child_result, child.exitstatus)
else:
return child_result
|
python
|
def run(command, timeout=30, withexitstatus=False, events=None,
extra_args=None, logfile=None, cwd=None, env=None, **kwargs):
'''
This function runs the given command; waits for it to finish; then
returns all output as a string. STDERR is included in output. If the full
path to the command is not given then the path is searched.
Note that lines are terminated by CR/LF (\\r\\n) combination even on
UNIX-like systems because this is the standard for pseudottys. If you set
'withexitstatus' to true, then run will return a tuple of (command_output,
exitstatus). If 'withexitstatus' is false then this returns just
command_output.
The run() function can often be used instead of creating a spawn instance.
For example, the following code uses spawn::
from pexpect import *
child = spawn('scp foo user@example.com:.')
child.expect('(?i)password')
child.sendline(mypassword)
The previous code can be replace with the following::
from pexpect import *
run('scp foo user@example.com:.', events={'(?i)password': mypassword})
**Examples**
Start the apache daemon on the local machine::
from pexpect import *
run("/usr/local/apache/bin/apachectl start")
Check in a file using SVN::
from pexpect import *
run("svn ci -m 'automatic commit' my_file.py")
Run a command and capture exit status::
from pexpect import *
(command_output, exitstatus) = run('ls -l /bin', withexitstatus=1)
The following will run SSH and execute 'ls -l' on the remote machine. The
password 'secret' will be sent if the '(?i)password' pattern is ever seen::
run("ssh username@machine.example.com 'ls -l'",
events={'(?i)password':'secret\\n'})
This will start mencoder to rip a video from DVD. This will also display
progress ticks every 5 seconds as it runs. For example::
from pexpect import *
def print_ticks(d):
print d['event_count'],
run("mencoder dvd://1 -o video.avi -oac copy -ovc copy",
events={TIMEOUT:print_ticks}, timeout=5)
The 'events' argument should be either a dictionary or a tuple list that
contains patterns and responses. Whenever one of the patterns is seen
in the command output, run() will send the associated response string.
So, run() in the above example can be also written as:
run("mencoder dvd://1 -o video.avi -oac copy -ovc copy",
events=[(TIMEOUT,print_ticks)], timeout=5)
Use a tuple list for events if the command output requires a delicate
control over what pattern should be matched, since the tuple list is passed
to pexpect() as its pattern list, with the order of patterns preserved.
Note that you should put newlines in your string if Enter is necessary.
Like the example above, the responses may also contain a callback, either
a function or method. It should accept a dictionary value as an argument.
The dictionary contains all the locals from the run() function, so you can
access the child spawn object or any other variable defined in run()
(event_count, child, and extra_args are the most useful). A callback may
return True to stop the current run process. Otherwise run() continues
until the next event. A callback may also return a string which will be
sent to the child. 'extra_args' is not used by directly run(). It provides
a way to pass data to a callback function through run() through the locals
dictionary passed to a callback.
Like :class:`spawn`, passing *encoding* will make it work with unicode
instead of bytes. You can pass *codec_errors* to control how errors in
encoding and decoding are handled.
'''
if timeout == -1:
child = spawn(command, maxread=2000, logfile=logfile, cwd=cwd, env=env,
**kwargs)
else:
child = spawn(command, timeout=timeout, maxread=2000, logfile=logfile,
cwd=cwd, env=env, **kwargs)
if isinstance(events, list):
patterns= [x for x,y in events]
responses = [y for x,y in events]
elif isinstance(events, dict):
patterns = list(events.keys())
responses = list(events.values())
else:
# This assumes EOF or TIMEOUT will eventually cause run to terminate.
patterns = None
responses = None
child_result_list = []
event_count = 0
while True:
try:
index = child.expect(patterns)
if isinstance(child.after, child.allowed_string_types):
child_result_list.append(child.before + child.after)
else:
# child.after may have been a TIMEOUT or EOF,
# which we don't want appended to the list.
child_result_list.append(child.before)
if isinstance(responses[index], child.allowed_string_types):
child.send(responses[index])
elif (isinstance(responses[index], types.FunctionType) or
isinstance(responses[index], types.MethodType)):
callback_result = responses[index](locals())
sys.stdout.flush()
if isinstance(callback_result, child.allowed_string_types):
child.send(callback_result)
elif callback_result:
break
else:
raise TypeError("parameter `event' at index {index} must be "
"a string, method, or function: {value!r}"
.format(index=index, value=responses[index]))
event_count = event_count + 1
except TIMEOUT:
child_result_list.append(child.before)
break
except EOF:
child_result_list.append(child.before)
break
child_result = child.string_type().join(child_result_list)
if withexitstatus:
child.close()
return (child_result, child.exitstatus)
else:
return child_result
|
[
"def",
"run",
"(",
"command",
",",
"timeout",
"=",
"30",
",",
"withexitstatus",
"=",
"False",
",",
"events",
"=",
"None",
",",
"extra_args",
"=",
"None",
",",
"logfile",
"=",
"None",
",",
"cwd",
"=",
"None",
",",
"env",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"timeout",
"==",
"-",
"1",
":",
"child",
"=",
"spawn",
"(",
"command",
",",
"maxread",
"=",
"2000",
",",
"logfile",
"=",
"logfile",
",",
"cwd",
"=",
"cwd",
",",
"env",
"=",
"env",
",",
"*",
"*",
"kwargs",
")",
"else",
":",
"child",
"=",
"spawn",
"(",
"command",
",",
"timeout",
"=",
"timeout",
",",
"maxread",
"=",
"2000",
",",
"logfile",
"=",
"logfile",
",",
"cwd",
"=",
"cwd",
",",
"env",
"=",
"env",
",",
"*",
"*",
"kwargs",
")",
"if",
"isinstance",
"(",
"events",
",",
"list",
")",
":",
"patterns",
"=",
"[",
"x",
"for",
"x",
",",
"y",
"in",
"events",
"]",
"responses",
"=",
"[",
"y",
"for",
"x",
",",
"y",
"in",
"events",
"]",
"elif",
"isinstance",
"(",
"events",
",",
"dict",
")",
":",
"patterns",
"=",
"list",
"(",
"events",
".",
"keys",
"(",
")",
")",
"responses",
"=",
"list",
"(",
"events",
".",
"values",
"(",
")",
")",
"else",
":",
"# This assumes EOF or TIMEOUT will eventually cause run to terminate.",
"patterns",
"=",
"None",
"responses",
"=",
"None",
"child_result_list",
"=",
"[",
"]",
"event_count",
"=",
"0",
"while",
"True",
":",
"try",
":",
"index",
"=",
"child",
".",
"expect",
"(",
"patterns",
")",
"if",
"isinstance",
"(",
"child",
".",
"after",
",",
"child",
".",
"allowed_string_types",
")",
":",
"child_result_list",
".",
"append",
"(",
"child",
".",
"before",
"+",
"child",
".",
"after",
")",
"else",
":",
"# child.after may have been a TIMEOUT or EOF,",
"# which we don't want appended to the list.",
"child_result_list",
".",
"append",
"(",
"child",
".",
"before",
")",
"if",
"isinstance",
"(",
"responses",
"[",
"index",
"]",
",",
"child",
".",
"allowed_string_types",
")",
":",
"child",
".",
"send",
"(",
"responses",
"[",
"index",
"]",
")",
"elif",
"(",
"isinstance",
"(",
"responses",
"[",
"index",
"]",
",",
"types",
".",
"FunctionType",
")",
"or",
"isinstance",
"(",
"responses",
"[",
"index",
"]",
",",
"types",
".",
"MethodType",
")",
")",
":",
"callback_result",
"=",
"responses",
"[",
"index",
"]",
"(",
"locals",
"(",
")",
")",
"sys",
".",
"stdout",
".",
"flush",
"(",
")",
"if",
"isinstance",
"(",
"callback_result",
",",
"child",
".",
"allowed_string_types",
")",
":",
"child",
".",
"send",
"(",
"callback_result",
")",
"elif",
"callback_result",
":",
"break",
"else",
":",
"raise",
"TypeError",
"(",
"\"parameter `event' at index {index} must be \"",
"\"a string, method, or function: {value!r}\"",
".",
"format",
"(",
"index",
"=",
"index",
",",
"value",
"=",
"responses",
"[",
"index",
"]",
")",
")",
"event_count",
"=",
"event_count",
"+",
"1",
"except",
"TIMEOUT",
":",
"child_result_list",
".",
"append",
"(",
"child",
".",
"before",
")",
"break",
"except",
"EOF",
":",
"child_result_list",
".",
"append",
"(",
"child",
".",
"before",
")",
"break",
"child_result",
"=",
"child",
".",
"string_type",
"(",
")",
".",
"join",
"(",
"child_result_list",
")",
"if",
"withexitstatus",
":",
"child",
".",
"close",
"(",
")",
"return",
"(",
"child_result",
",",
"child",
".",
"exitstatus",
")",
"else",
":",
"return",
"child_result"
] |
This function runs the given command; waits for it to finish; then
returns all output as a string. STDERR is included in output. If the full
path to the command is not given then the path is searched.
Note that lines are terminated by CR/LF (\\r\\n) combination even on
UNIX-like systems because this is the standard for pseudottys. If you set
'withexitstatus' to true, then run will return a tuple of (command_output,
exitstatus). If 'withexitstatus' is false then this returns just
command_output.
The run() function can often be used instead of creating a spawn instance.
For example, the following code uses spawn::
from pexpect import *
child = spawn('scp foo user@example.com:.')
child.expect('(?i)password')
child.sendline(mypassword)
The previous code can be replace with the following::
from pexpect import *
run('scp foo user@example.com:.', events={'(?i)password': mypassword})
**Examples**
Start the apache daemon on the local machine::
from pexpect import *
run("/usr/local/apache/bin/apachectl start")
Check in a file using SVN::
from pexpect import *
run("svn ci -m 'automatic commit' my_file.py")
Run a command and capture exit status::
from pexpect import *
(command_output, exitstatus) = run('ls -l /bin', withexitstatus=1)
The following will run SSH and execute 'ls -l' on the remote machine. The
password 'secret' will be sent if the '(?i)password' pattern is ever seen::
run("ssh username@machine.example.com 'ls -l'",
events={'(?i)password':'secret\\n'})
This will start mencoder to rip a video from DVD. This will also display
progress ticks every 5 seconds as it runs. For example::
from pexpect import *
def print_ticks(d):
print d['event_count'],
run("mencoder dvd://1 -o video.avi -oac copy -ovc copy",
events={TIMEOUT:print_ticks}, timeout=5)
The 'events' argument should be either a dictionary or a tuple list that
contains patterns and responses. Whenever one of the patterns is seen
in the command output, run() will send the associated response string.
So, run() in the above example can be also written as:
run("mencoder dvd://1 -o video.avi -oac copy -ovc copy",
events=[(TIMEOUT,print_ticks)], timeout=5)
Use a tuple list for events if the command output requires a delicate
control over what pattern should be matched, since the tuple list is passed
to pexpect() as its pattern list, with the order of patterns preserved.
Note that you should put newlines in your string if Enter is necessary.
Like the example above, the responses may also contain a callback, either
a function or method. It should accept a dictionary value as an argument.
The dictionary contains all the locals from the run() function, so you can
access the child spawn object or any other variable defined in run()
(event_count, child, and extra_args are the most useful). A callback may
return True to stop the current run process. Otherwise run() continues
until the next event. A callback may also return a string which will be
sent to the child. 'extra_args' is not used by directly run(). It provides
a way to pass data to a callback function through run() through the locals
dictionary passed to a callback.
Like :class:`spawn`, passing *encoding* will make it work with unicode
instead of bytes. You can pass *codec_errors* to control how errors in
encoding and decoding are handled.
|
[
"This",
"function",
"runs",
"the",
"given",
"command",
";",
"waits",
"for",
"it",
"to",
"finish",
";",
"then",
"returns",
"all",
"output",
"as",
"a",
"string",
".",
"STDERR",
"is",
"included",
"in",
"output",
".",
"If",
"the",
"full",
"path",
"to",
"the",
"command",
"is",
"not",
"given",
"then",
"the",
"path",
"is",
"searched",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pexpect/run.py#L7-L148
|
train
|
pypa/pipenv
|
pipenv/vendor/pexpect/run.py
|
runu
|
def runu(command, timeout=30, withexitstatus=False, events=None,
extra_args=None, logfile=None, cwd=None, env=None, **kwargs):
"""Deprecated: pass encoding to run() instead.
"""
kwargs.setdefault('encoding', 'utf-8')
return run(command, timeout=timeout, withexitstatus=withexitstatus,
events=events, extra_args=extra_args, logfile=logfile, cwd=cwd,
env=env, **kwargs)
|
python
|
def runu(command, timeout=30, withexitstatus=False, events=None,
extra_args=None, logfile=None, cwd=None, env=None, **kwargs):
"""Deprecated: pass encoding to run() instead.
"""
kwargs.setdefault('encoding', 'utf-8')
return run(command, timeout=timeout, withexitstatus=withexitstatus,
events=events, extra_args=extra_args, logfile=logfile, cwd=cwd,
env=env, **kwargs)
|
[
"def",
"runu",
"(",
"command",
",",
"timeout",
"=",
"30",
",",
"withexitstatus",
"=",
"False",
",",
"events",
"=",
"None",
",",
"extra_args",
"=",
"None",
",",
"logfile",
"=",
"None",
",",
"cwd",
"=",
"None",
",",
"env",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"kwargs",
".",
"setdefault",
"(",
"'encoding'",
",",
"'utf-8'",
")",
"return",
"run",
"(",
"command",
",",
"timeout",
"=",
"timeout",
",",
"withexitstatus",
"=",
"withexitstatus",
",",
"events",
"=",
"events",
",",
"extra_args",
"=",
"extra_args",
",",
"logfile",
"=",
"logfile",
",",
"cwd",
"=",
"cwd",
",",
"env",
"=",
"env",
",",
"*",
"*",
"kwargs",
")"
] |
Deprecated: pass encoding to run() instead.
|
[
"Deprecated",
":",
"pass",
"encoding",
"to",
"run",
"()",
"instead",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pexpect/run.py#L150-L157
|
train
|
pypa/pipenv
|
pipenv/vendor/shellingham/posix/_default.py
|
get_process_mapping
|
def get_process_mapping():
"""Try to look up the process tree via the output of `ps`.
"""
output = subprocess.check_output([
'ps', '-ww', '-o', 'pid=', '-o', 'ppid=', '-o', 'args=',
])
if not isinstance(output, str):
output = output.decode(sys.stdout.encoding)
processes = {}
for line in output.split('\n'):
try:
pid, ppid, args = line.strip().split(None, 2)
except ValueError:
continue
processes[pid] = Process(
args=tuple(shlex.split(args)), pid=pid, ppid=ppid,
)
return processes
|
python
|
def get_process_mapping():
"""Try to look up the process tree via the output of `ps`.
"""
output = subprocess.check_output([
'ps', '-ww', '-o', 'pid=', '-o', 'ppid=', '-o', 'args=',
])
if not isinstance(output, str):
output = output.decode(sys.stdout.encoding)
processes = {}
for line in output.split('\n'):
try:
pid, ppid, args = line.strip().split(None, 2)
except ValueError:
continue
processes[pid] = Process(
args=tuple(shlex.split(args)), pid=pid, ppid=ppid,
)
return processes
|
[
"def",
"get_process_mapping",
"(",
")",
":",
"output",
"=",
"subprocess",
".",
"check_output",
"(",
"[",
"'ps'",
",",
"'-ww'",
",",
"'-o'",
",",
"'pid='",
",",
"'-o'",
",",
"'ppid='",
",",
"'-o'",
",",
"'args='",
",",
"]",
")",
"if",
"not",
"isinstance",
"(",
"output",
",",
"str",
")",
":",
"output",
"=",
"output",
".",
"decode",
"(",
"sys",
".",
"stdout",
".",
"encoding",
")",
"processes",
"=",
"{",
"}",
"for",
"line",
"in",
"output",
".",
"split",
"(",
"'\\n'",
")",
":",
"try",
":",
"pid",
",",
"ppid",
",",
"args",
"=",
"line",
".",
"strip",
"(",
")",
".",
"split",
"(",
"None",
",",
"2",
")",
"except",
"ValueError",
":",
"continue",
"processes",
"[",
"pid",
"]",
"=",
"Process",
"(",
"args",
"=",
"tuple",
"(",
"shlex",
".",
"split",
"(",
"args",
")",
")",
",",
"pid",
"=",
"pid",
",",
"ppid",
"=",
"ppid",
",",
")",
"return",
"processes"
] |
Try to look up the process tree via the output of `ps`.
|
[
"Try",
"to",
"look",
"up",
"the",
"process",
"tree",
"via",
"the",
"output",
"of",
"ps",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/shellingham/posix/_default.py#L10-L27
|
train
|
pypa/pipenv
|
pipenv/vendor/requests/models.py
|
RequestEncodingMixin.path_url
|
def path_url(self):
"""Build the path URL to use."""
url = []
p = urlsplit(self.url)
path = p.path
if not path:
path = '/'
url.append(path)
query = p.query
if query:
url.append('?')
url.append(query)
return ''.join(url)
|
python
|
def path_url(self):
"""Build the path URL to use."""
url = []
p = urlsplit(self.url)
path = p.path
if not path:
path = '/'
url.append(path)
query = p.query
if query:
url.append('?')
url.append(query)
return ''.join(url)
|
[
"def",
"path_url",
"(",
"self",
")",
":",
"url",
"=",
"[",
"]",
"p",
"=",
"urlsplit",
"(",
"self",
".",
"url",
")",
"path",
"=",
"p",
".",
"path",
"if",
"not",
"path",
":",
"path",
"=",
"'/'",
"url",
".",
"append",
"(",
"path",
")",
"query",
"=",
"p",
".",
"query",
"if",
"query",
":",
"url",
".",
"append",
"(",
"'?'",
")",
"url",
".",
"append",
"(",
"query",
")",
"return",
"''",
".",
"join",
"(",
"url",
")"
] |
Build the path URL to use.
|
[
"Build",
"the",
"path",
"URL",
"to",
"use",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/requests/models.py#L62-L80
|
train
|
pypa/pipenv
|
pipenv/vendor/requests/models.py
|
RequestEncodingMixin._encode_params
|
def _encode_params(data):
"""Encode parameters in a piece of data.
Will successfully encode parameters when passed as a dict or a list of
2-tuples. Order is retained if data is a list of 2-tuples but arbitrary
if parameters are supplied as a dict.
"""
if isinstance(data, (str, bytes)):
return data
elif hasattr(data, 'read'):
return data
elif hasattr(data, '__iter__'):
result = []
for k, vs in to_key_val_list(data):
if isinstance(vs, basestring) or not hasattr(vs, '__iter__'):
vs = [vs]
for v in vs:
if v is not None:
result.append(
(k.encode('utf-8') if isinstance(k, str) else k,
v.encode('utf-8') if isinstance(v, str) else v))
return urlencode(result, doseq=True)
else:
return data
|
python
|
def _encode_params(data):
"""Encode parameters in a piece of data.
Will successfully encode parameters when passed as a dict or a list of
2-tuples. Order is retained if data is a list of 2-tuples but arbitrary
if parameters are supplied as a dict.
"""
if isinstance(data, (str, bytes)):
return data
elif hasattr(data, 'read'):
return data
elif hasattr(data, '__iter__'):
result = []
for k, vs in to_key_val_list(data):
if isinstance(vs, basestring) or not hasattr(vs, '__iter__'):
vs = [vs]
for v in vs:
if v is not None:
result.append(
(k.encode('utf-8') if isinstance(k, str) else k,
v.encode('utf-8') if isinstance(v, str) else v))
return urlencode(result, doseq=True)
else:
return data
|
[
"def",
"_encode_params",
"(",
"data",
")",
":",
"if",
"isinstance",
"(",
"data",
",",
"(",
"str",
",",
"bytes",
")",
")",
":",
"return",
"data",
"elif",
"hasattr",
"(",
"data",
",",
"'read'",
")",
":",
"return",
"data",
"elif",
"hasattr",
"(",
"data",
",",
"'__iter__'",
")",
":",
"result",
"=",
"[",
"]",
"for",
"k",
",",
"vs",
"in",
"to_key_val_list",
"(",
"data",
")",
":",
"if",
"isinstance",
"(",
"vs",
",",
"basestring",
")",
"or",
"not",
"hasattr",
"(",
"vs",
",",
"'__iter__'",
")",
":",
"vs",
"=",
"[",
"vs",
"]",
"for",
"v",
"in",
"vs",
":",
"if",
"v",
"is",
"not",
"None",
":",
"result",
".",
"append",
"(",
"(",
"k",
".",
"encode",
"(",
"'utf-8'",
")",
"if",
"isinstance",
"(",
"k",
",",
"str",
")",
"else",
"k",
",",
"v",
".",
"encode",
"(",
"'utf-8'",
")",
"if",
"isinstance",
"(",
"v",
",",
"str",
")",
"else",
"v",
")",
")",
"return",
"urlencode",
"(",
"result",
",",
"doseq",
"=",
"True",
")",
"else",
":",
"return",
"data"
] |
Encode parameters in a piece of data.
Will successfully encode parameters when passed as a dict or a list of
2-tuples. Order is retained if data is a list of 2-tuples but arbitrary
if parameters are supplied as a dict.
|
[
"Encode",
"parameters",
"in",
"a",
"piece",
"of",
"data",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/requests/models.py#L83-L107
|
train
|
pypa/pipenv
|
pipenv/vendor/requests/models.py
|
RequestEncodingMixin._encode_files
|
def _encode_files(files, data):
"""Build the body for a multipart/form-data request.
Will successfully encode files when passed as a dict or a list of
tuples. Order is retained if data is a list of tuples but arbitrary
if parameters are supplied as a dict.
The tuples may be 2-tuples (filename, fileobj), 3-tuples (filename, fileobj, contentype)
or 4-tuples (filename, fileobj, contentype, custom_headers).
"""
if (not files):
raise ValueError("Files must be provided.")
elif isinstance(data, basestring):
raise ValueError("Data must not be a string.")
new_fields = []
fields = to_key_val_list(data or {})
files = to_key_val_list(files or {})
for field, val in fields:
if isinstance(val, basestring) or not hasattr(val, '__iter__'):
val = [val]
for v in val:
if v is not None:
# Don't call str() on bytestrings: in Py3 it all goes wrong.
if not isinstance(v, bytes):
v = str(v)
new_fields.append(
(field.decode('utf-8') if isinstance(field, bytes) else field,
v.encode('utf-8') if isinstance(v, str) else v))
for (k, v) in files:
# support for explicit filename
ft = None
fh = None
if isinstance(v, (tuple, list)):
if len(v) == 2:
fn, fp = v
elif len(v) == 3:
fn, fp, ft = v
else:
fn, fp, ft, fh = v
else:
fn = guess_filename(v) or k
fp = v
if isinstance(fp, (str, bytes, bytearray)):
fdata = fp
elif hasattr(fp, 'read'):
fdata = fp.read()
elif fp is None:
continue
else:
fdata = fp
rf = RequestField(name=k, data=fdata, filename=fn, headers=fh)
rf.make_multipart(content_type=ft)
new_fields.append(rf)
body, content_type = encode_multipart_formdata(new_fields)
return body, content_type
|
python
|
def _encode_files(files, data):
"""Build the body for a multipart/form-data request.
Will successfully encode files when passed as a dict or a list of
tuples. Order is retained if data is a list of tuples but arbitrary
if parameters are supplied as a dict.
The tuples may be 2-tuples (filename, fileobj), 3-tuples (filename, fileobj, contentype)
or 4-tuples (filename, fileobj, contentype, custom_headers).
"""
if (not files):
raise ValueError("Files must be provided.")
elif isinstance(data, basestring):
raise ValueError("Data must not be a string.")
new_fields = []
fields = to_key_val_list(data or {})
files = to_key_val_list(files or {})
for field, val in fields:
if isinstance(val, basestring) or not hasattr(val, '__iter__'):
val = [val]
for v in val:
if v is not None:
# Don't call str() on bytestrings: in Py3 it all goes wrong.
if not isinstance(v, bytes):
v = str(v)
new_fields.append(
(field.decode('utf-8') if isinstance(field, bytes) else field,
v.encode('utf-8') if isinstance(v, str) else v))
for (k, v) in files:
# support for explicit filename
ft = None
fh = None
if isinstance(v, (tuple, list)):
if len(v) == 2:
fn, fp = v
elif len(v) == 3:
fn, fp, ft = v
else:
fn, fp, ft, fh = v
else:
fn = guess_filename(v) or k
fp = v
if isinstance(fp, (str, bytes, bytearray)):
fdata = fp
elif hasattr(fp, 'read'):
fdata = fp.read()
elif fp is None:
continue
else:
fdata = fp
rf = RequestField(name=k, data=fdata, filename=fn, headers=fh)
rf.make_multipart(content_type=ft)
new_fields.append(rf)
body, content_type = encode_multipart_formdata(new_fields)
return body, content_type
|
[
"def",
"_encode_files",
"(",
"files",
",",
"data",
")",
":",
"if",
"(",
"not",
"files",
")",
":",
"raise",
"ValueError",
"(",
"\"Files must be provided.\"",
")",
"elif",
"isinstance",
"(",
"data",
",",
"basestring",
")",
":",
"raise",
"ValueError",
"(",
"\"Data must not be a string.\"",
")",
"new_fields",
"=",
"[",
"]",
"fields",
"=",
"to_key_val_list",
"(",
"data",
"or",
"{",
"}",
")",
"files",
"=",
"to_key_val_list",
"(",
"files",
"or",
"{",
"}",
")",
"for",
"field",
",",
"val",
"in",
"fields",
":",
"if",
"isinstance",
"(",
"val",
",",
"basestring",
")",
"or",
"not",
"hasattr",
"(",
"val",
",",
"'__iter__'",
")",
":",
"val",
"=",
"[",
"val",
"]",
"for",
"v",
"in",
"val",
":",
"if",
"v",
"is",
"not",
"None",
":",
"# Don't call str() on bytestrings: in Py3 it all goes wrong.",
"if",
"not",
"isinstance",
"(",
"v",
",",
"bytes",
")",
":",
"v",
"=",
"str",
"(",
"v",
")",
"new_fields",
".",
"append",
"(",
"(",
"field",
".",
"decode",
"(",
"'utf-8'",
")",
"if",
"isinstance",
"(",
"field",
",",
"bytes",
")",
"else",
"field",
",",
"v",
".",
"encode",
"(",
"'utf-8'",
")",
"if",
"isinstance",
"(",
"v",
",",
"str",
")",
"else",
"v",
")",
")",
"for",
"(",
"k",
",",
"v",
")",
"in",
"files",
":",
"# support for explicit filename",
"ft",
"=",
"None",
"fh",
"=",
"None",
"if",
"isinstance",
"(",
"v",
",",
"(",
"tuple",
",",
"list",
")",
")",
":",
"if",
"len",
"(",
"v",
")",
"==",
"2",
":",
"fn",
",",
"fp",
"=",
"v",
"elif",
"len",
"(",
"v",
")",
"==",
"3",
":",
"fn",
",",
"fp",
",",
"ft",
"=",
"v",
"else",
":",
"fn",
",",
"fp",
",",
"ft",
",",
"fh",
"=",
"v",
"else",
":",
"fn",
"=",
"guess_filename",
"(",
"v",
")",
"or",
"k",
"fp",
"=",
"v",
"if",
"isinstance",
"(",
"fp",
",",
"(",
"str",
",",
"bytes",
",",
"bytearray",
")",
")",
":",
"fdata",
"=",
"fp",
"elif",
"hasattr",
"(",
"fp",
",",
"'read'",
")",
":",
"fdata",
"=",
"fp",
".",
"read",
"(",
")",
"elif",
"fp",
"is",
"None",
":",
"continue",
"else",
":",
"fdata",
"=",
"fp",
"rf",
"=",
"RequestField",
"(",
"name",
"=",
"k",
",",
"data",
"=",
"fdata",
",",
"filename",
"=",
"fn",
",",
"headers",
"=",
"fh",
")",
"rf",
".",
"make_multipart",
"(",
"content_type",
"=",
"ft",
")",
"new_fields",
".",
"append",
"(",
"rf",
")",
"body",
",",
"content_type",
"=",
"encode_multipart_formdata",
"(",
"new_fields",
")",
"return",
"body",
",",
"content_type"
] |
Build the body for a multipart/form-data request.
Will successfully encode files when passed as a dict or a list of
tuples. Order is retained if data is a list of tuples but arbitrary
if parameters are supplied as a dict.
The tuples may be 2-tuples (filename, fileobj), 3-tuples (filename, fileobj, contentype)
or 4-tuples (filename, fileobj, contentype, custom_headers).
|
[
"Build",
"the",
"body",
"for",
"a",
"multipart",
"/",
"form",
"-",
"data",
"request",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/requests/models.py#L110-L171
|
train
|
pypa/pipenv
|
pipenv/vendor/requests/models.py
|
RequestHooksMixin.register_hook
|
def register_hook(self, event, hook):
"""Properly register a hook."""
if event not in self.hooks:
raise ValueError('Unsupported event specified, with event name "%s"' % (event))
if isinstance(hook, Callable):
self.hooks[event].append(hook)
elif hasattr(hook, '__iter__'):
self.hooks[event].extend(h for h in hook if isinstance(h, Callable))
|
python
|
def register_hook(self, event, hook):
"""Properly register a hook."""
if event not in self.hooks:
raise ValueError('Unsupported event specified, with event name "%s"' % (event))
if isinstance(hook, Callable):
self.hooks[event].append(hook)
elif hasattr(hook, '__iter__'):
self.hooks[event].extend(h for h in hook if isinstance(h, Callable))
|
[
"def",
"register_hook",
"(",
"self",
",",
"event",
",",
"hook",
")",
":",
"if",
"event",
"not",
"in",
"self",
".",
"hooks",
":",
"raise",
"ValueError",
"(",
"'Unsupported event specified, with event name \"%s\"'",
"%",
"(",
"event",
")",
")",
"if",
"isinstance",
"(",
"hook",
",",
"Callable",
")",
":",
"self",
".",
"hooks",
"[",
"event",
"]",
".",
"append",
"(",
"hook",
")",
"elif",
"hasattr",
"(",
"hook",
",",
"'__iter__'",
")",
":",
"self",
".",
"hooks",
"[",
"event",
"]",
".",
"extend",
"(",
"h",
"for",
"h",
"in",
"hook",
"if",
"isinstance",
"(",
"h",
",",
"Callable",
")",
")"
] |
Properly register a hook.
|
[
"Properly",
"register",
"a",
"hook",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/requests/models.py#L175-L184
|
train
|
pypa/pipenv
|
pipenv/vendor/requests/models.py
|
RequestHooksMixin.deregister_hook
|
def deregister_hook(self, event, hook):
"""Deregister a previously registered hook.
Returns True if the hook existed, False if not.
"""
try:
self.hooks[event].remove(hook)
return True
except ValueError:
return False
|
python
|
def deregister_hook(self, event, hook):
"""Deregister a previously registered hook.
Returns True if the hook existed, False if not.
"""
try:
self.hooks[event].remove(hook)
return True
except ValueError:
return False
|
[
"def",
"deregister_hook",
"(",
"self",
",",
"event",
",",
"hook",
")",
":",
"try",
":",
"self",
".",
"hooks",
"[",
"event",
"]",
".",
"remove",
"(",
"hook",
")",
"return",
"True",
"except",
"ValueError",
":",
"return",
"False"
] |
Deregister a previously registered hook.
Returns True if the hook existed, False if not.
|
[
"Deregister",
"a",
"previously",
"registered",
"hook",
".",
"Returns",
"True",
"if",
"the",
"hook",
"existed",
"False",
"if",
"not",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/requests/models.py#L186-L195
|
train
|
pypa/pipenv
|
pipenv/vendor/requests/models.py
|
PreparedRequest.prepare
|
def prepare(self,
method=None, url=None, headers=None, files=None, data=None,
params=None, auth=None, cookies=None, hooks=None, json=None):
"""Prepares the entire request with the given parameters."""
self.prepare_method(method)
self.prepare_url(url, params)
self.prepare_headers(headers)
self.prepare_cookies(cookies)
self.prepare_body(data, files, json)
self.prepare_auth(auth, url)
# Note that prepare_auth must be last to enable authentication schemes
# such as OAuth to work on a fully prepared request.
# This MUST go after prepare_auth. Authenticators could add a hook
self.prepare_hooks(hooks)
|
python
|
def prepare(self,
method=None, url=None, headers=None, files=None, data=None,
params=None, auth=None, cookies=None, hooks=None, json=None):
"""Prepares the entire request with the given parameters."""
self.prepare_method(method)
self.prepare_url(url, params)
self.prepare_headers(headers)
self.prepare_cookies(cookies)
self.prepare_body(data, files, json)
self.prepare_auth(auth, url)
# Note that prepare_auth must be last to enable authentication schemes
# such as OAuth to work on a fully prepared request.
# This MUST go after prepare_auth. Authenticators could add a hook
self.prepare_hooks(hooks)
|
[
"def",
"prepare",
"(",
"self",
",",
"method",
"=",
"None",
",",
"url",
"=",
"None",
",",
"headers",
"=",
"None",
",",
"files",
"=",
"None",
",",
"data",
"=",
"None",
",",
"params",
"=",
"None",
",",
"auth",
"=",
"None",
",",
"cookies",
"=",
"None",
",",
"hooks",
"=",
"None",
",",
"json",
"=",
"None",
")",
":",
"self",
".",
"prepare_method",
"(",
"method",
")",
"self",
".",
"prepare_url",
"(",
"url",
",",
"params",
")",
"self",
".",
"prepare_headers",
"(",
"headers",
")",
"self",
".",
"prepare_cookies",
"(",
"cookies",
")",
"self",
".",
"prepare_body",
"(",
"data",
",",
"files",
",",
"json",
")",
"self",
".",
"prepare_auth",
"(",
"auth",
",",
"url",
")",
"# Note that prepare_auth must be last to enable authentication schemes",
"# such as OAuth to work on a fully prepared request.",
"# This MUST go after prepare_auth. Authenticators could add a hook",
"self",
".",
"prepare_hooks",
"(",
"hooks",
")"
] |
Prepares the entire request with the given parameters.
|
[
"Prepares",
"the",
"entire",
"request",
"with",
"the",
"given",
"parameters",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/requests/models.py#L307-L323
|
train
|
pypa/pipenv
|
pipenv/vendor/requests/models.py
|
PreparedRequest.prepare_body
|
def prepare_body(self, data, files, json=None):
"""Prepares the given HTTP body data."""
# Check if file, fo, generator, iterator.
# If not, run through normal process.
# Nottin' on you.
body = None
content_type = None
if not data and json is not None:
# urllib3 requires a bytes-like body. Python 2's json.dumps
# provides this natively, but Python 3 gives a Unicode string.
content_type = 'application/json'
body = complexjson.dumps(json)
if not isinstance(body, bytes):
body = body.encode('utf-8')
is_stream = all([
hasattr(data, '__iter__'),
not isinstance(data, (basestring, list, tuple, Mapping))
])
try:
length = super_len(data)
except (TypeError, AttributeError, UnsupportedOperation):
length = None
if is_stream:
body = data
if getattr(body, 'tell', None) is not None:
# Record the current file position before reading.
# This will allow us to rewind a file in the event
# of a redirect.
try:
self._body_position = body.tell()
except (IOError, OSError):
# This differentiates from None, allowing us to catch
# a failed `tell()` later when trying to rewind the body
self._body_position = object()
if files:
raise NotImplementedError('Streamed bodies and files are mutually exclusive.')
if length:
self.headers['Content-Length'] = builtin_str(length)
else:
self.headers['Transfer-Encoding'] = 'chunked'
else:
# Multi-part file uploads.
if files:
(body, content_type) = self._encode_files(files, data)
else:
if data:
body = self._encode_params(data)
if isinstance(data, basestring) or hasattr(data, 'read'):
content_type = None
else:
content_type = 'application/x-www-form-urlencoded'
self.prepare_content_length(body)
# Add content-type if it wasn't explicitly provided.
if content_type and ('content-type' not in self.headers):
self.headers['Content-Type'] = content_type
self.body = body
|
python
|
def prepare_body(self, data, files, json=None):
"""Prepares the given HTTP body data."""
# Check if file, fo, generator, iterator.
# If not, run through normal process.
# Nottin' on you.
body = None
content_type = None
if not data and json is not None:
# urllib3 requires a bytes-like body. Python 2's json.dumps
# provides this natively, but Python 3 gives a Unicode string.
content_type = 'application/json'
body = complexjson.dumps(json)
if not isinstance(body, bytes):
body = body.encode('utf-8')
is_stream = all([
hasattr(data, '__iter__'),
not isinstance(data, (basestring, list, tuple, Mapping))
])
try:
length = super_len(data)
except (TypeError, AttributeError, UnsupportedOperation):
length = None
if is_stream:
body = data
if getattr(body, 'tell', None) is not None:
# Record the current file position before reading.
# This will allow us to rewind a file in the event
# of a redirect.
try:
self._body_position = body.tell()
except (IOError, OSError):
# This differentiates from None, allowing us to catch
# a failed `tell()` later when trying to rewind the body
self._body_position = object()
if files:
raise NotImplementedError('Streamed bodies and files are mutually exclusive.')
if length:
self.headers['Content-Length'] = builtin_str(length)
else:
self.headers['Transfer-Encoding'] = 'chunked'
else:
# Multi-part file uploads.
if files:
(body, content_type) = self._encode_files(files, data)
else:
if data:
body = self._encode_params(data)
if isinstance(data, basestring) or hasattr(data, 'read'):
content_type = None
else:
content_type = 'application/x-www-form-urlencoded'
self.prepare_content_length(body)
# Add content-type if it wasn't explicitly provided.
if content_type and ('content-type' not in self.headers):
self.headers['Content-Type'] = content_type
self.body = body
|
[
"def",
"prepare_body",
"(",
"self",
",",
"data",
",",
"files",
",",
"json",
"=",
"None",
")",
":",
"# Check if file, fo, generator, iterator.",
"# If not, run through normal process.",
"# Nottin' on you.",
"body",
"=",
"None",
"content_type",
"=",
"None",
"if",
"not",
"data",
"and",
"json",
"is",
"not",
"None",
":",
"# urllib3 requires a bytes-like body. Python 2's json.dumps",
"# provides this natively, but Python 3 gives a Unicode string.",
"content_type",
"=",
"'application/json'",
"body",
"=",
"complexjson",
".",
"dumps",
"(",
"json",
")",
"if",
"not",
"isinstance",
"(",
"body",
",",
"bytes",
")",
":",
"body",
"=",
"body",
".",
"encode",
"(",
"'utf-8'",
")",
"is_stream",
"=",
"all",
"(",
"[",
"hasattr",
"(",
"data",
",",
"'__iter__'",
")",
",",
"not",
"isinstance",
"(",
"data",
",",
"(",
"basestring",
",",
"list",
",",
"tuple",
",",
"Mapping",
")",
")",
"]",
")",
"try",
":",
"length",
"=",
"super_len",
"(",
"data",
")",
"except",
"(",
"TypeError",
",",
"AttributeError",
",",
"UnsupportedOperation",
")",
":",
"length",
"=",
"None",
"if",
"is_stream",
":",
"body",
"=",
"data",
"if",
"getattr",
"(",
"body",
",",
"'tell'",
",",
"None",
")",
"is",
"not",
"None",
":",
"# Record the current file position before reading.",
"# This will allow us to rewind a file in the event",
"# of a redirect.",
"try",
":",
"self",
".",
"_body_position",
"=",
"body",
".",
"tell",
"(",
")",
"except",
"(",
"IOError",
",",
"OSError",
")",
":",
"# This differentiates from None, allowing us to catch",
"# a failed `tell()` later when trying to rewind the body",
"self",
".",
"_body_position",
"=",
"object",
"(",
")",
"if",
"files",
":",
"raise",
"NotImplementedError",
"(",
"'Streamed bodies and files are mutually exclusive.'",
")",
"if",
"length",
":",
"self",
".",
"headers",
"[",
"'Content-Length'",
"]",
"=",
"builtin_str",
"(",
"length",
")",
"else",
":",
"self",
".",
"headers",
"[",
"'Transfer-Encoding'",
"]",
"=",
"'chunked'",
"else",
":",
"# Multi-part file uploads.",
"if",
"files",
":",
"(",
"body",
",",
"content_type",
")",
"=",
"self",
".",
"_encode_files",
"(",
"files",
",",
"data",
")",
"else",
":",
"if",
"data",
":",
"body",
"=",
"self",
".",
"_encode_params",
"(",
"data",
")",
"if",
"isinstance",
"(",
"data",
",",
"basestring",
")",
"or",
"hasattr",
"(",
"data",
",",
"'read'",
")",
":",
"content_type",
"=",
"None",
"else",
":",
"content_type",
"=",
"'application/x-www-form-urlencoded'",
"self",
".",
"prepare_content_length",
"(",
"body",
")",
"# Add content-type if it wasn't explicitly provided.",
"if",
"content_type",
"and",
"(",
"'content-type'",
"not",
"in",
"self",
".",
"headers",
")",
":",
"self",
".",
"headers",
"[",
"'Content-Type'",
"]",
"=",
"content_type",
"self",
".",
"body",
"=",
"body"
] |
Prepares the given HTTP body data.
|
[
"Prepares",
"the",
"given",
"HTTP",
"body",
"data",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/requests/models.py#L452-L519
|
train
|
pypa/pipenv
|
pipenv/vendor/requests/models.py
|
PreparedRequest.prepare_content_length
|
def prepare_content_length(self, body):
"""Prepare Content-Length header based on request method and body"""
if body is not None:
length = super_len(body)
if length:
# If length exists, set it. Otherwise, we fallback
# to Transfer-Encoding: chunked.
self.headers['Content-Length'] = builtin_str(length)
elif self.method not in ('GET', 'HEAD') and self.headers.get('Content-Length') is None:
# Set Content-Length to 0 for methods that can have a body
# but don't provide one. (i.e. not GET or HEAD)
self.headers['Content-Length'] = '0'
|
python
|
def prepare_content_length(self, body):
"""Prepare Content-Length header based on request method and body"""
if body is not None:
length = super_len(body)
if length:
# If length exists, set it. Otherwise, we fallback
# to Transfer-Encoding: chunked.
self.headers['Content-Length'] = builtin_str(length)
elif self.method not in ('GET', 'HEAD') and self.headers.get('Content-Length') is None:
# Set Content-Length to 0 for methods that can have a body
# but don't provide one. (i.e. not GET or HEAD)
self.headers['Content-Length'] = '0'
|
[
"def",
"prepare_content_length",
"(",
"self",
",",
"body",
")",
":",
"if",
"body",
"is",
"not",
"None",
":",
"length",
"=",
"super_len",
"(",
"body",
")",
"if",
"length",
":",
"# If length exists, set it. Otherwise, we fallback",
"# to Transfer-Encoding: chunked.",
"self",
".",
"headers",
"[",
"'Content-Length'",
"]",
"=",
"builtin_str",
"(",
"length",
")",
"elif",
"self",
".",
"method",
"not",
"in",
"(",
"'GET'",
",",
"'HEAD'",
")",
"and",
"self",
".",
"headers",
".",
"get",
"(",
"'Content-Length'",
")",
"is",
"None",
":",
"# Set Content-Length to 0 for methods that can have a body",
"# but don't provide one. (i.e. not GET or HEAD)",
"self",
".",
"headers",
"[",
"'Content-Length'",
"]",
"=",
"'0'"
] |
Prepare Content-Length header based on request method and body
|
[
"Prepare",
"Content",
"-",
"Length",
"header",
"based",
"on",
"request",
"method",
"and",
"body"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/requests/models.py#L521-L532
|
train
|
pypa/pipenv
|
pipenv/vendor/requests/models.py
|
PreparedRequest.prepare_auth
|
def prepare_auth(self, auth, url=''):
"""Prepares the given HTTP auth data."""
# If no Auth is explicitly provided, extract it from the URL first.
if auth is None:
url_auth = get_auth_from_url(self.url)
auth = url_auth if any(url_auth) else None
if auth:
if isinstance(auth, tuple) and len(auth) == 2:
# special-case basic HTTP auth
auth = HTTPBasicAuth(*auth)
# Allow auth to make its changes.
r = auth(self)
# Update self to reflect the auth changes.
self.__dict__.update(r.__dict__)
# Recompute Content-Length
self.prepare_content_length(self.body)
|
python
|
def prepare_auth(self, auth, url=''):
"""Prepares the given HTTP auth data."""
# If no Auth is explicitly provided, extract it from the URL first.
if auth is None:
url_auth = get_auth_from_url(self.url)
auth = url_auth if any(url_auth) else None
if auth:
if isinstance(auth, tuple) and len(auth) == 2:
# special-case basic HTTP auth
auth = HTTPBasicAuth(*auth)
# Allow auth to make its changes.
r = auth(self)
# Update self to reflect the auth changes.
self.__dict__.update(r.__dict__)
# Recompute Content-Length
self.prepare_content_length(self.body)
|
[
"def",
"prepare_auth",
"(",
"self",
",",
"auth",
",",
"url",
"=",
"''",
")",
":",
"# If no Auth is explicitly provided, extract it from the URL first.",
"if",
"auth",
"is",
"None",
":",
"url_auth",
"=",
"get_auth_from_url",
"(",
"self",
".",
"url",
")",
"auth",
"=",
"url_auth",
"if",
"any",
"(",
"url_auth",
")",
"else",
"None",
"if",
"auth",
":",
"if",
"isinstance",
"(",
"auth",
",",
"tuple",
")",
"and",
"len",
"(",
"auth",
")",
"==",
"2",
":",
"# special-case basic HTTP auth",
"auth",
"=",
"HTTPBasicAuth",
"(",
"*",
"auth",
")",
"# Allow auth to make its changes.",
"r",
"=",
"auth",
"(",
"self",
")",
"# Update self to reflect the auth changes.",
"self",
".",
"__dict__",
".",
"update",
"(",
"r",
".",
"__dict__",
")",
"# Recompute Content-Length",
"self",
".",
"prepare_content_length",
"(",
"self",
".",
"body",
")"
] |
Prepares the given HTTP auth data.
|
[
"Prepares",
"the",
"given",
"HTTP",
"auth",
"data",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/requests/models.py#L534-L554
|
train
|
pypa/pipenv
|
pipenv/vendor/requests/models.py
|
PreparedRequest.prepare_cookies
|
def prepare_cookies(self, cookies):
"""Prepares the given HTTP cookie data.
This function eventually generates a ``Cookie`` header from the
given cookies using cookielib. Due to cookielib's design, the header
will not be regenerated if it already exists, meaning this function
can only be called once for the life of the
:class:`PreparedRequest <PreparedRequest>` object. Any subsequent calls
to ``prepare_cookies`` will have no actual effect, unless the "Cookie"
header is removed beforehand.
"""
if isinstance(cookies, cookielib.CookieJar):
self._cookies = cookies
else:
self._cookies = cookiejar_from_dict(cookies)
cookie_header = get_cookie_header(self._cookies, self)
if cookie_header is not None:
self.headers['Cookie'] = cookie_header
|
python
|
def prepare_cookies(self, cookies):
"""Prepares the given HTTP cookie data.
This function eventually generates a ``Cookie`` header from the
given cookies using cookielib. Due to cookielib's design, the header
will not be regenerated if it already exists, meaning this function
can only be called once for the life of the
:class:`PreparedRequest <PreparedRequest>` object. Any subsequent calls
to ``prepare_cookies`` will have no actual effect, unless the "Cookie"
header is removed beforehand.
"""
if isinstance(cookies, cookielib.CookieJar):
self._cookies = cookies
else:
self._cookies = cookiejar_from_dict(cookies)
cookie_header = get_cookie_header(self._cookies, self)
if cookie_header is not None:
self.headers['Cookie'] = cookie_header
|
[
"def",
"prepare_cookies",
"(",
"self",
",",
"cookies",
")",
":",
"if",
"isinstance",
"(",
"cookies",
",",
"cookielib",
".",
"CookieJar",
")",
":",
"self",
".",
"_cookies",
"=",
"cookies",
"else",
":",
"self",
".",
"_cookies",
"=",
"cookiejar_from_dict",
"(",
"cookies",
")",
"cookie_header",
"=",
"get_cookie_header",
"(",
"self",
".",
"_cookies",
",",
"self",
")",
"if",
"cookie_header",
"is",
"not",
"None",
":",
"self",
".",
"headers",
"[",
"'Cookie'",
"]",
"=",
"cookie_header"
] |
Prepares the given HTTP cookie data.
This function eventually generates a ``Cookie`` header from the
given cookies using cookielib. Due to cookielib's design, the header
will not be regenerated if it already exists, meaning this function
can only be called once for the life of the
:class:`PreparedRequest <PreparedRequest>` object. Any subsequent calls
to ``prepare_cookies`` will have no actual effect, unless the "Cookie"
header is removed beforehand.
|
[
"Prepares",
"the",
"given",
"HTTP",
"cookie",
"data",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/requests/models.py#L556-L574
|
train
|
pypa/pipenv
|
pipenv/vendor/requests/models.py
|
PreparedRequest.prepare_hooks
|
def prepare_hooks(self, hooks):
"""Prepares the given hooks."""
# hooks can be passed as None to the prepare method and to this
# method. To prevent iterating over None, simply use an empty list
# if hooks is False-y
hooks = hooks or []
for event in hooks:
self.register_hook(event, hooks[event])
|
python
|
def prepare_hooks(self, hooks):
"""Prepares the given hooks."""
# hooks can be passed as None to the prepare method and to this
# method. To prevent iterating over None, simply use an empty list
# if hooks is False-y
hooks = hooks or []
for event in hooks:
self.register_hook(event, hooks[event])
|
[
"def",
"prepare_hooks",
"(",
"self",
",",
"hooks",
")",
":",
"# hooks can be passed as None to the prepare method and to this",
"# method. To prevent iterating over None, simply use an empty list",
"# if hooks is False-y",
"hooks",
"=",
"hooks",
"or",
"[",
"]",
"for",
"event",
"in",
"hooks",
":",
"self",
".",
"register_hook",
"(",
"event",
",",
"hooks",
"[",
"event",
"]",
")"
] |
Prepares the given hooks.
|
[
"Prepares",
"the",
"given",
"hooks",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/requests/models.py#L576-L583
|
train
|
pypa/pipenv
|
pipenv/vendor/requests/models.py
|
Response.is_permanent_redirect
|
def is_permanent_redirect(self):
"""True if this Response one of the permanent versions of redirect."""
return ('location' in self.headers and self.status_code in (codes.moved_permanently, codes.permanent_redirect))
|
python
|
def is_permanent_redirect(self):
"""True if this Response one of the permanent versions of redirect."""
return ('location' in self.headers and self.status_code in (codes.moved_permanently, codes.permanent_redirect))
|
[
"def",
"is_permanent_redirect",
"(",
"self",
")",
":",
"return",
"(",
"'location'",
"in",
"self",
".",
"headers",
"and",
"self",
".",
"status_code",
"in",
"(",
"codes",
".",
"moved_permanently",
",",
"codes",
".",
"permanent_redirect",
")",
")"
] |
True if this Response one of the permanent versions of redirect.
|
[
"True",
"if",
"this",
"Response",
"one",
"of",
"the",
"permanent",
"versions",
"of",
"redirect",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/requests/models.py#L715-L717
|
train
|
pypa/pipenv
|
pipenv/vendor/requests/models.py
|
Response.text
|
def text(self):
"""Content of the response, in unicode.
If Response.encoding is None, encoding will be guessed using
``chardet``.
The encoding of the response content is determined based solely on HTTP
headers, following RFC 2616 to the letter. If you can take advantage of
non-HTTP knowledge to make a better guess at the encoding, you should
set ``r.encoding`` appropriately before accessing this property.
"""
# Try charset from content-type
content = None
encoding = self.encoding
if not self.content:
return str('')
# Fallback to auto-detected encoding.
if self.encoding is None:
encoding = self.apparent_encoding
# Decode unicode from given encoding.
try:
content = str(self.content, encoding, errors='replace')
except (LookupError, TypeError):
# A LookupError is raised if the encoding was not found which could
# indicate a misspelling or similar mistake.
#
# A TypeError can be raised if encoding is None
#
# So we try blindly encoding.
content = str(self.content, errors='replace')
return content
|
python
|
def text(self):
"""Content of the response, in unicode.
If Response.encoding is None, encoding will be guessed using
``chardet``.
The encoding of the response content is determined based solely on HTTP
headers, following RFC 2616 to the letter. If you can take advantage of
non-HTTP knowledge to make a better guess at the encoding, you should
set ``r.encoding`` appropriately before accessing this property.
"""
# Try charset from content-type
content = None
encoding = self.encoding
if not self.content:
return str('')
# Fallback to auto-detected encoding.
if self.encoding is None:
encoding = self.apparent_encoding
# Decode unicode from given encoding.
try:
content = str(self.content, encoding, errors='replace')
except (LookupError, TypeError):
# A LookupError is raised if the encoding was not found which could
# indicate a misspelling or similar mistake.
#
# A TypeError can be raised if encoding is None
#
# So we try blindly encoding.
content = str(self.content, errors='replace')
return content
|
[
"def",
"text",
"(",
"self",
")",
":",
"# Try charset from content-type",
"content",
"=",
"None",
"encoding",
"=",
"self",
".",
"encoding",
"if",
"not",
"self",
".",
"content",
":",
"return",
"str",
"(",
"''",
")",
"# Fallback to auto-detected encoding.",
"if",
"self",
".",
"encoding",
"is",
"None",
":",
"encoding",
"=",
"self",
".",
"apparent_encoding",
"# Decode unicode from given encoding.",
"try",
":",
"content",
"=",
"str",
"(",
"self",
".",
"content",
",",
"encoding",
",",
"errors",
"=",
"'replace'",
")",
"except",
"(",
"LookupError",
",",
"TypeError",
")",
":",
"# A LookupError is raised if the encoding was not found which could",
"# indicate a misspelling or similar mistake.",
"#",
"# A TypeError can be raised if encoding is None",
"#",
"# So we try blindly encoding.",
"content",
"=",
"str",
"(",
"self",
".",
"content",
",",
"errors",
"=",
"'replace'",
")",
"return",
"content"
] |
Content of the response, in unicode.
If Response.encoding is None, encoding will be guessed using
``chardet``.
The encoding of the response content is determined based solely on HTTP
headers, following RFC 2616 to the letter. If you can take advantage of
non-HTTP knowledge to make a better guess at the encoding, you should
set ``r.encoding`` appropriately before accessing this property.
|
[
"Content",
"of",
"the",
"response",
"in",
"unicode",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/requests/models.py#L836-L871
|
train
|
pypa/pipenv
|
pipenv/vendor/requests/models.py
|
Response.raise_for_status
|
def raise_for_status(self):
"""Raises stored :class:`HTTPError`, if one occurred."""
http_error_msg = ''
if isinstance(self.reason, bytes):
# We attempt to decode utf-8 first because some servers
# choose to localize their reason strings. If the string
# isn't utf-8, we fall back to iso-8859-1 for all other
# encodings. (See PR #3538)
try:
reason = self.reason.decode('utf-8')
except UnicodeDecodeError:
reason = self.reason.decode('iso-8859-1')
else:
reason = self.reason
if 400 <= self.status_code < 500:
http_error_msg = u'%s Client Error: %s for url: %s' % (self.status_code, reason, self.url)
elif 500 <= self.status_code < 600:
http_error_msg = u'%s Server Error: %s for url: %s' % (self.status_code, reason, self.url)
if http_error_msg:
raise HTTPError(http_error_msg, response=self)
|
python
|
def raise_for_status(self):
"""Raises stored :class:`HTTPError`, if one occurred."""
http_error_msg = ''
if isinstance(self.reason, bytes):
# We attempt to decode utf-8 first because some servers
# choose to localize their reason strings. If the string
# isn't utf-8, we fall back to iso-8859-1 for all other
# encodings. (See PR #3538)
try:
reason = self.reason.decode('utf-8')
except UnicodeDecodeError:
reason = self.reason.decode('iso-8859-1')
else:
reason = self.reason
if 400 <= self.status_code < 500:
http_error_msg = u'%s Client Error: %s for url: %s' % (self.status_code, reason, self.url)
elif 500 <= self.status_code < 600:
http_error_msg = u'%s Server Error: %s for url: %s' % (self.status_code, reason, self.url)
if http_error_msg:
raise HTTPError(http_error_msg, response=self)
|
[
"def",
"raise_for_status",
"(",
"self",
")",
":",
"http_error_msg",
"=",
"''",
"if",
"isinstance",
"(",
"self",
".",
"reason",
",",
"bytes",
")",
":",
"# We attempt to decode utf-8 first because some servers",
"# choose to localize their reason strings. If the string",
"# isn't utf-8, we fall back to iso-8859-1 for all other",
"# encodings. (See PR #3538)",
"try",
":",
"reason",
"=",
"self",
".",
"reason",
".",
"decode",
"(",
"'utf-8'",
")",
"except",
"UnicodeDecodeError",
":",
"reason",
"=",
"self",
".",
"reason",
".",
"decode",
"(",
"'iso-8859-1'",
")",
"else",
":",
"reason",
"=",
"self",
".",
"reason",
"if",
"400",
"<=",
"self",
".",
"status_code",
"<",
"500",
":",
"http_error_msg",
"=",
"u'%s Client Error: %s for url: %s'",
"%",
"(",
"self",
".",
"status_code",
",",
"reason",
",",
"self",
".",
"url",
")",
"elif",
"500",
"<=",
"self",
".",
"status_code",
"<",
"600",
":",
"http_error_msg",
"=",
"u'%s Server Error: %s for url: %s'",
"%",
"(",
"self",
".",
"status_code",
",",
"reason",
",",
"self",
".",
"url",
")",
"if",
"http_error_msg",
":",
"raise",
"HTTPError",
"(",
"http_error_msg",
",",
"response",
"=",
"self",
")"
] |
Raises stored :class:`HTTPError`, if one occurred.
|
[
"Raises",
"stored",
":",
"class",
":",
"HTTPError",
"if",
"one",
"occurred",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/requests/models.py#L917-L940
|
train
|
pypa/pipenv
|
pipenv/vendor/requests/models.py
|
Response.close
|
def close(self):
"""Releases the connection back to the pool. Once this method has been
called the underlying ``raw`` object must not be accessed again.
*Note: Should not normally need to be called explicitly.*
"""
if not self._content_consumed:
self.raw.close()
release_conn = getattr(self.raw, 'release_conn', None)
if release_conn is not None:
release_conn()
|
python
|
def close(self):
"""Releases the connection back to the pool. Once this method has been
called the underlying ``raw`` object must not be accessed again.
*Note: Should not normally need to be called explicitly.*
"""
if not self._content_consumed:
self.raw.close()
release_conn = getattr(self.raw, 'release_conn', None)
if release_conn is not None:
release_conn()
|
[
"def",
"close",
"(",
"self",
")",
":",
"if",
"not",
"self",
".",
"_content_consumed",
":",
"self",
".",
"raw",
".",
"close",
"(",
")",
"release_conn",
"=",
"getattr",
"(",
"self",
".",
"raw",
",",
"'release_conn'",
",",
"None",
")",
"if",
"release_conn",
"is",
"not",
"None",
":",
"release_conn",
"(",
")"
] |
Releases the connection back to the pool. Once this method has been
called the underlying ``raw`` object must not be accessed again.
*Note: Should not normally need to be called explicitly.*
|
[
"Releases",
"the",
"connection",
"back",
"to",
"the",
"pool",
".",
"Once",
"this",
"method",
"has",
"been",
"called",
"the",
"underlying",
"raw",
"object",
"must",
"not",
"be",
"accessed",
"again",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/requests/models.py#L942-L953
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_internal/commands/install.py
|
create_env_error_message
|
def create_env_error_message(error, show_traceback, using_user_site):
"""Format an error message for an EnvironmentError
It may occur anytime during the execution of the install command.
"""
parts = []
# Mention the error if we are not going to show a traceback
parts.append("Could not install packages due to an EnvironmentError")
if not show_traceback:
parts.append(": ")
parts.append(str(error))
else:
parts.append(".")
# Spilt the error indication from a helper message (if any)
parts[-1] += "\n"
# Suggest useful actions to the user:
# (1) using user site-packages or (2) verifying the permissions
if error.errno == errno.EACCES:
user_option_part = "Consider using the `--user` option"
permissions_part = "Check the permissions"
if not using_user_site:
parts.extend([
user_option_part, " or ",
permissions_part.lower(),
])
else:
parts.append(permissions_part)
parts.append(".\n")
return "".join(parts).strip() + "\n"
|
python
|
def create_env_error_message(error, show_traceback, using_user_site):
"""Format an error message for an EnvironmentError
It may occur anytime during the execution of the install command.
"""
parts = []
# Mention the error if we are not going to show a traceback
parts.append("Could not install packages due to an EnvironmentError")
if not show_traceback:
parts.append(": ")
parts.append(str(error))
else:
parts.append(".")
# Spilt the error indication from a helper message (if any)
parts[-1] += "\n"
# Suggest useful actions to the user:
# (1) using user site-packages or (2) verifying the permissions
if error.errno == errno.EACCES:
user_option_part = "Consider using the `--user` option"
permissions_part = "Check the permissions"
if not using_user_site:
parts.extend([
user_option_part, " or ",
permissions_part.lower(),
])
else:
parts.append(permissions_part)
parts.append(".\n")
return "".join(parts).strip() + "\n"
|
[
"def",
"create_env_error_message",
"(",
"error",
",",
"show_traceback",
",",
"using_user_site",
")",
":",
"parts",
"=",
"[",
"]",
"# Mention the error if we are not going to show a traceback",
"parts",
".",
"append",
"(",
"\"Could not install packages due to an EnvironmentError\"",
")",
"if",
"not",
"show_traceback",
":",
"parts",
".",
"append",
"(",
"\": \"",
")",
"parts",
".",
"append",
"(",
"str",
"(",
"error",
")",
")",
"else",
":",
"parts",
".",
"append",
"(",
"\".\"",
")",
"# Spilt the error indication from a helper message (if any)",
"parts",
"[",
"-",
"1",
"]",
"+=",
"\"\\n\"",
"# Suggest useful actions to the user:",
"# (1) using user site-packages or (2) verifying the permissions",
"if",
"error",
".",
"errno",
"==",
"errno",
".",
"EACCES",
":",
"user_option_part",
"=",
"\"Consider using the `--user` option\"",
"permissions_part",
"=",
"\"Check the permissions\"",
"if",
"not",
"using_user_site",
":",
"parts",
".",
"extend",
"(",
"[",
"user_option_part",
",",
"\" or \"",
",",
"permissions_part",
".",
"lower",
"(",
")",
",",
"]",
")",
"else",
":",
"parts",
".",
"append",
"(",
"permissions_part",
")",
"parts",
".",
"append",
"(",
"\".\\n\"",
")",
"return",
"\"\"",
".",
"join",
"(",
"parts",
")",
".",
"strip",
"(",
")",
"+",
"\"\\n\""
] |
Format an error message for an EnvironmentError
It may occur anytime during the execution of the install command.
|
[
"Format",
"an",
"error",
"message",
"for",
"an",
"EnvironmentError"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_internal/commands/install.py#L533-L566
|
train
|
pypa/pipenv
|
pipenv/vendor/urllib3/connectionpool.py
|
_ipv6_host
|
def _ipv6_host(host, scheme):
"""
Process IPv6 address literals
"""
# httplib doesn't like it when we include brackets in IPv6 addresses
# Specifically, if we include brackets but also pass the port then
# httplib crazily doubles up the square brackets on the Host header.
# Instead, we need to make sure we never pass ``None`` as the port.
# However, for backward compatibility reasons we can't actually
# *assert* that. See http://bugs.python.org/issue28539
#
# Also if an IPv6 address literal has a zone identifier, the
# percent sign might be URIencoded, convert it back into ASCII
if host.startswith('[') and host.endswith(']'):
host = host.replace('%25', '%').strip('[]')
if scheme in NORMALIZABLE_SCHEMES:
host = host.lower()
return host
|
python
|
def _ipv6_host(host, scheme):
"""
Process IPv6 address literals
"""
# httplib doesn't like it when we include brackets in IPv6 addresses
# Specifically, if we include brackets but also pass the port then
# httplib crazily doubles up the square brackets on the Host header.
# Instead, we need to make sure we never pass ``None`` as the port.
# However, for backward compatibility reasons we can't actually
# *assert* that. See http://bugs.python.org/issue28539
#
# Also if an IPv6 address literal has a zone identifier, the
# percent sign might be URIencoded, convert it back into ASCII
if host.startswith('[') and host.endswith(']'):
host = host.replace('%25', '%').strip('[]')
if scheme in NORMALIZABLE_SCHEMES:
host = host.lower()
return host
|
[
"def",
"_ipv6_host",
"(",
"host",
",",
"scheme",
")",
":",
"# httplib doesn't like it when we include brackets in IPv6 addresses",
"# Specifically, if we include brackets but also pass the port then",
"# httplib crazily doubles up the square brackets on the Host header.",
"# Instead, we need to make sure we never pass ``None`` as the port.",
"# However, for backward compatibility reasons we can't actually",
"# *assert* that. See http://bugs.python.org/issue28539",
"#",
"# Also if an IPv6 address literal has a zone identifier, the",
"# percent sign might be URIencoded, convert it back into ASCII",
"if",
"host",
".",
"startswith",
"(",
"'['",
")",
"and",
"host",
".",
"endswith",
"(",
"']'",
")",
":",
"host",
"=",
"host",
".",
"replace",
"(",
"'%25'",
",",
"'%'",
")",
".",
"strip",
"(",
"'[]'",
")",
"if",
"scheme",
"in",
"NORMALIZABLE_SCHEMES",
":",
"host",
"=",
"host",
".",
"lower",
"(",
")",
"return",
"host"
] |
Process IPv6 address literals
|
[
"Process",
"IPv6",
"address",
"literals"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/urllib3/connectionpool.py#L878-L896
|
train
|
pypa/pipenv
|
pipenv/vendor/urllib3/connectionpool.py
|
HTTPConnectionPool._get_conn
|
def _get_conn(self, timeout=None):
"""
Get a connection. Will return a pooled connection if one is available.
If no connections are available and :prop:`.block` is ``False``, then a
fresh connection is returned.
:param timeout:
Seconds to wait before giving up and raising
:class:`urllib3.exceptions.EmptyPoolError` if the pool is empty and
:prop:`.block` is ``True``.
"""
conn = None
try:
conn = self.pool.get(block=self.block, timeout=timeout)
except AttributeError: # self.pool is None
raise ClosedPoolError(self, "Pool is closed.")
except queue.Empty:
if self.block:
raise EmptyPoolError(self,
"Pool reached maximum size and no more "
"connections are allowed.")
pass # Oh well, we'll create a new connection then
# If this is a persistent connection, check if it got disconnected
if conn and is_connection_dropped(conn):
log.debug("Resetting dropped connection: %s", self.host)
conn.close()
if getattr(conn, 'auto_open', 1) == 0:
# This is a proxied connection that has been mutated by
# httplib._tunnel() and cannot be reused (since it would
# attempt to bypass the proxy)
conn = None
return conn or self._new_conn()
|
python
|
def _get_conn(self, timeout=None):
"""
Get a connection. Will return a pooled connection if one is available.
If no connections are available and :prop:`.block` is ``False``, then a
fresh connection is returned.
:param timeout:
Seconds to wait before giving up and raising
:class:`urllib3.exceptions.EmptyPoolError` if the pool is empty and
:prop:`.block` is ``True``.
"""
conn = None
try:
conn = self.pool.get(block=self.block, timeout=timeout)
except AttributeError: # self.pool is None
raise ClosedPoolError(self, "Pool is closed.")
except queue.Empty:
if self.block:
raise EmptyPoolError(self,
"Pool reached maximum size and no more "
"connections are allowed.")
pass # Oh well, we'll create a new connection then
# If this is a persistent connection, check if it got disconnected
if conn and is_connection_dropped(conn):
log.debug("Resetting dropped connection: %s", self.host)
conn.close()
if getattr(conn, 'auto_open', 1) == 0:
# This is a proxied connection that has been mutated by
# httplib._tunnel() and cannot be reused (since it would
# attempt to bypass the proxy)
conn = None
return conn or self._new_conn()
|
[
"def",
"_get_conn",
"(",
"self",
",",
"timeout",
"=",
"None",
")",
":",
"conn",
"=",
"None",
"try",
":",
"conn",
"=",
"self",
".",
"pool",
".",
"get",
"(",
"block",
"=",
"self",
".",
"block",
",",
"timeout",
"=",
"timeout",
")",
"except",
"AttributeError",
":",
"# self.pool is None",
"raise",
"ClosedPoolError",
"(",
"self",
",",
"\"Pool is closed.\"",
")",
"except",
"queue",
".",
"Empty",
":",
"if",
"self",
".",
"block",
":",
"raise",
"EmptyPoolError",
"(",
"self",
",",
"\"Pool reached maximum size and no more \"",
"\"connections are allowed.\"",
")",
"pass",
"# Oh well, we'll create a new connection then",
"# If this is a persistent connection, check if it got disconnected",
"if",
"conn",
"and",
"is_connection_dropped",
"(",
"conn",
")",
":",
"log",
".",
"debug",
"(",
"\"Resetting dropped connection: %s\"",
",",
"self",
".",
"host",
")",
"conn",
".",
"close",
"(",
")",
"if",
"getattr",
"(",
"conn",
",",
"'auto_open'",
",",
"1",
")",
"==",
"0",
":",
"# This is a proxied connection that has been mutated by",
"# httplib._tunnel() and cannot be reused (since it would",
"# attempt to bypass the proxy)",
"conn",
"=",
"None",
"return",
"conn",
"or",
"self",
".",
"_new_conn",
"(",
")"
] |
Get a connection. Will return a pooled connection if one is available.
If no connections are available and :prop:`.block` is ``False``, then a
fresh connection is returned.
:param timeout:
Seconds to wait before giving up and raising
:class:`urllib3.exceptions.EmptyPoolError` if the pool is empty and
:prop:`.block` is ``True``.
|
[
"Get",
"a",
"connection",
".",
"Will",
"return",
"a",
"pooled",
"connection",
"if",
"one",
"is",
"available",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/urllib3/connectionpool.py#L212-L248
|
train
|
pypa/pipenv
|
pipenv/vendor/urllib3/connectionpool.py
|
HTTPConnectionPool._get_timeout
|
def _get_timeout(self, timeout):
""" Helper that always returns a :class:`urllib3.util.Timeout` """
if timeout is _Default:
return self.timeout.clone()
if isinstance(timeout, Timeout):
return timeout.clone()
else:
# User passed us an int/float. This is for backwards compatibility,
# can be removed later
return Timeout.from_float(timeout)
|
python
|
def _get_timeout(self, timeout):
""" Helper that always returns a :class:`urllib3.util.Timeout` """
if timeout is _Default:
return self.timeout.clone()
if isinstance(timeout, Timeout):
return timeout.clone()
else:
# User passed us an int/float. This is for backwards compatibility,
# can be removed later
return Timeout.from_float(timeout)
|
[
"def",
"_get_timeout",
"(",
"self",
",",
"timeout",
")",
":",
"if",
"timeout",
"is",
"_Default",
":",
"return",
"self",
".",
"timeout",
".",
"clone",
"(",
")",
"if",
"isinstance",
"(",
"timeout",
",",
"Timeout",
")",
":",
"return",
"timeout",
".",
"clone",
"(",
")",
"else",
":",
"# User passed us an int/float. This is for backwards compatibility,",
"# can be removed later",
"return",
"Timeout",
".",
"from_float",
"(",
"timeout",
")"
] |
Helper that always returns a :class:`urllib3.util.Timeout`
|
[
"Helper",
"that",
"always",
"returns",
"a",
":",
"class",
":",
"urllib3",
".",
"util",
".",
"Timeout"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/urllib3/connectionpool.py#L290-L300
|
train
|
pypa/pipenv
|
pipenv/vendor/urllib3/connectionpool.py
|
HTTPConnectionPool._raise_timeout
|
def _raise_timeout(self, err, url, timeout_value):
"""Is the error actually a timeout? Will raise a ReadTimeout or pass"""
if isinstance(err, SocketTimeout):
raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
# See the above comment about EAGAIN in Python 3. In Python 2 we have
# to specifically catch it and throw the timeout error
if hasattr(err, 'errno') and err.errno in _blocking_errnos:
raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
# Catch possible read timeouts thrown as SSL errors. If not the
# case, rethrow the original. We need to do this because of:
# http://bugs.python.org/issue10272
if 'timed out' in str(err) or 'did not complete (read)' in str(err): # Python < 2.7.4
raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
|
python
|
def _raise_timeout(self, err, url, timeout_value):
"""Is the error actually a timeout? Will raise a ReadTimeout or pass"""
if isinstance(err, SocketTimeout):
raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
# See the above comment about EAGAIN in Python 3. In Python 2 we have
# to specifically catch it and throw the timeout error
if hasattr(err, 'errno') and err.errno in _blocking_errnos:
raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
# Catch possible read timeouts thrown as SSL errors. If not the
# case, rethrow the original. We need to do this because of:
# http://bugs.python.org/issue10272
if 'timed out' in str(err) or 'did not complete (read)' in str(err): # Python < 2.7.4
raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
|
[
"def",
"_raise_timeout",
"(",
"self",
",",
"err",
",",
"url",
",",
"timeout_value",
")",
":",
"if",
"isinstance",
"(",
"err",
",",
"SocketTimeout",
")",
":",
"raise",
"ReadTimeoutError",
"(",
"self",
",",
"url",
",",
"\"Read timed out. (read timeout=%s)\"",
"%",
"timeout_value",
")",
"# See the above comment about EAGAIN in Python 3. In Python 2 we have",
"# to specifically catch it and throw the timeout error",
"if",
"hasattr",
"(",
"err",
",",
"'errno'",
")",
"and",
"err",
".",
"errno",
"in",
"_blocking_errnos",
":",
"raise",
"ReadTimeoutError",
"(",
"self",
",",
"url",
",",
"\"Read timed out. (read timeout=%s)\"",
"%",
"timeout_value",
")",
"# Catch possible read timeouts thrown as SSL errors. If not the",
"# case, rethrow the original. We need to do this because of:",
"# http://bugs.python.org/issue10272",
"if",
"'timed out'",
"in",
"str",
"(",
"err",
")",
"or",
"'did not complete (read)'",
"in",
"str",
"(",
"err",
")",
":",
"# Python < 2.7.4",
"raise",
"ReadTimeoutError",
"(",
"self",
",",
"url",
",",
"\"Read timed out. (read timeout=%s)\"",
"%",
"timeout_value",
")"
] |
Is the error actually a timeout? Will raise a ReadTimeout or pass
|
[
"Is",
"the",
"error",
"actually",
"a",
"timeout?",
"Will",
"raise",
"a",
"ReadTimeout",
"or",
"pass"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/urllib3/connectionpool.py#L302-L317
|
train
|
pypa/pipenv
|
pipenv/vendor/urllib3/connectionpool.py
|
HTTPConnectionPool.urlopen
|
def urlopen(self, method, url, body=None, headers=None, retries=None,
redirect=True, assert_same_host=True, timeout=_Default,
pool_timeout=None, release_conn=None, chunked=False,
body_pos=None, **response_kw):
"""
Get a connection from the pool and perform an HTTP request. This is the
lowest level call for making a request, so you'll need to specify all
the raw details.
.. note::
More commonly, it's appropriate to use a convenience method provided
by :class:`.RequestMethods`, such as :meth:`request`.
.. note::
`release_conn` will only behave as expected if
`preload_content=False` because we want to make
`preload_content=False` the default behaviour someday soon without
breaking backwards compatibility.
:param method:
HTTP request method (such as GET, POST, PUT, etc.)
:param body:
Data to send in the request body (useful for creating
POST requests, see HTTPConnectionPool.post_url for
more convenience).
:param headers:
Dictionary of custom headers to send, such as User-Agent,
If-None-Match, etc. If None, pool headers are used. If provided,
these headers completely replace any pool-specific headers.
:param retries:
Configure the number of retries to allow before raising a
:class:`~urllib3.exceptions.MaxRetryError` exception.
Pass ``None`` to retry until you receive a response. Pass a
:class:`~urllib3.util.retry.Retry` object for fine-grained control
over different types of retries.
Pass an integer number to retry connection errors that many times,
but no other types of errors. Pass zero to never retry.
If ``False``, then retries are disabled and any exception is raised
immediately. Also, instead of raising a MaxRetryError on redirects,
the redirect response will be returned.
:type retries: :class:`~urllib3.util.retry.Retry`, False, or an int.
:param redirect:
If True, automatically handle redirects (status codes 301, 302,
303, 307, 308). Each redirect counts as a retry. Disabling retries
will disable redirect, too.
:param assert_same_host:
If ``True``, will make sure that the host of the pool requests is
consistent else will raise HostChangedError. When False, you can
use the pool on an HTTP proxy and request foreign hosts.
:param timeout:
If specified, overrides the default timeout for this one
request. It may be a float (in seconds) or an instance of
:class:`urllib3.util.Timeout`.
:param pool_timeout:
If set and the pool is set to block=True, then this method will
block for ``pool_timeout`` seconds and raise EmptyPoolError if no
connection is available within the time period.
:param release_conn:
If False, then the urlopen call will not release the connection
back into the pool once a response is received (but will release if
you read the entire contents of the response such as when
`preload_content=True`). This is useful if you're not preloading
the response's content immediately. You will need to call
``r.release_conn()`` on the response ``r`` to return the connection
back into the pool. If None, it takes the value of
``response_kw.get('preload_content', True)``.
:param chunked:
If True, urllib3 will send the body using chunked transfer
encoding. Otherwise, urllib3 will send the body using the standard
content-length form. Defaults to False.
:param int body_pos:
Position to seek to in file-like body in the event of a retry or
redirect. Typically this won't need to be set because urllib3 will
auto-populate the value when needed.
:param \\**response_kw:
Additional parameters are passed to
:meth:`urllib3.response.HTTPResponse.from_httplib`
"""
if headers is None:
headers = self.headers
if not isinstance(retries, Retry):
retries = Retry.from_int(retries, redirect=redirect, default=self.retries)
if release_conn is None:
release_conn = response_kw.get('preload_content', True)
# Check host
if assert_same_host and not self.is_same_host(url):
raise HostChangedError(self, url, retries)
conn = None
# Track whether `conn` needs to be released before
# returning/raising/recursing. Update this variable if necessary, and
# leave `release_conn` constant throughout the function. That way, if
# the function recurses, the original value of `release_conn` will be
# passed down into the recursive call, and its value will be respected.
#
# See issue #651 [1] for details.
#
# [1] <https://github.com/shazow/urllib3/issues/651>
release_this_conn = release_conn
# Merge the proxy headers. Only do this in HTTP. We have to copy the
# headers dict so we can safely change it without those changes being
# reflected in anyone else's copy.
if self.scheme == 'http':
headers = headers.copy()
headers.update(self.proxy_headers)
# Must keep the exception bound to a separate variable or else Python 3
# complains about UnboundLocalError.
err = None
# Keep track of whether we cleanly exited the except block. This
# ensures we do proper cleanup in finally.
clean_exit = False
# Rewind body position, if needed. Record current position
# for future rewinds in the event of a redirect/retry.
body_pos = set_file_position(body, body_pos)
try:
# Request a connection from the queue.
timeout_obj = self._get_timeout(timeout)
conn = self._get_conn(timeout=pool_timeout)
conn.timeout = timeout_obj.connect_timeout
is_new_proxy_conn = self.proxy is not None and not getattr(conn, 'sock', None)
if is_new_proxy_conn:
self._prepare_proxy(conn)
# Make the request on the httplib connection object.
httplib_response = self._make_request(conn, method, url,
timeout=timeout_obj,
body=body, headers=headers,
chunked=chunked)
# If we're going to release the connection in ``finally:``, then
# the response doesn't need to know about the connection. Otherwise
# it will also try to release it and we'll have a double-release
# mess.
response_conn = conn if not release_conn else None
# Pass method to Response for length checking
response_kw['request_method'] = method
# Import httplib's response into our own wrapper object
response = self.ResponseCls.from_httplib(httplib_response,
pool=self,
connection=response_conn,
retries=retries,
**response_kw)
# Everything went great!
clean_exit = True
except queue.Empty:
# Timed out by queue.
raise EmptyPoolError(self, "No pool connections are available.")
except (TimeoutError, HTTPException, SocketError, ProtocolError,
BaseSSLError, SSLError, CertificateError) as e:
# Discard the connection for these exceptions. It will be
# replaced during the next _get_conn() call.
clean_exit = False
if isinstance(e, (BaseSSLError, CertificateError)):
e = SSLError(e)
elif isinstance(e, (SocketError, NewConnectionError)) and self.proxy:
e = ProxyError('Cannot connect to proxy.', e)
elif isinstance(e, (SocketError, HTTPException)):
e = ProtocolError('Connection aborted.', e)
retries = retries.increment(method, url, error=e, _pool=self,
_stacktrace=sys.exc_info()[2])
retries.sleep()
# Keep track of the error for the retry warning.
err = e
finally:
if not clean_exit:
# We hit some kind of exception, handled or otherwise. We need
# to throw the connection away unless explicitly told not to.
# Close the connection, set the variable to None, and make sure
# we put the None back in the pool to avoid leaking it.
conn = conn and conn.close()
release_this_conn = True
if release_this_conn:
# Put the connection back to be reused. If the connection is
# expired then it will be None, which will get replaced with a
# fresh connection during _get_conn.
self._put_conn(conn)
if not conn:
# Try again
log.warning("Retrying (%r) after connection "
"broken by '%r': %s", retries, err, url)
return self.urlopen(method, url, body, headers, retries,
redirect, assert_same_host,
timeout=timeout, pool_timeout=pool_timeout,
release_conn=release_conn, body_pos=body_pos,
**response_kw)
def drain_and_release_conn(response):
try:
# discard any remaining response body, the connection will be
# released back to the pool once the entire response is read
response.read()
except (TimeoutError, HTTPException, SocketError, ProtocolError,
BaseSSLError, SSLError) as e:
pass
# Handle redirect?
redirect_location = redirect and response.get_redirect_location()
if redirect_location:
if response.status == 303:
method = 'GET'
try:
retries = retries.increment(method, url, response=response, _pool=self)
except MaxRetryError:
if retries.raise_on_redirect:
# Drain and release the connection for this response, since
# we're not returning it to be released manually.
drain_and_release_conn(response)
raise
return response
# drain and return the connection to the pool before recursing
drain_and_release_conn(response)
retries.sleep_for_retry(response)
log.debug("Redirecting %s -> %s", url, redirect_location)
return self.urlopen(
method, redirect_location, body, headers,
retries=retries, redirect=redirect,
assert_same_host=assert_same_host,
timeout=timeout, pool_timeout=pool_timeout,
release_conn=release_conn, body_pos=body_pos,
**response_kw)
# Check if we should retry the HTTP response.
has_retry_after = bool(response.getheader('Retry-After'))
if retries.is_retry(method, response.status, has_retry_after):
try:
retries = retries.increment(method, url, response=response, _pool=self)
except MaxRetryError:
if retries.raise_on_status:
# Drain and release the connection for this response, since
# we're not returning it to be released manually.
drain_and_release_conn(response)
raise
return response
# drain and return the connection to the pool before recursing
drain_and_release_conn(response)
retries.sleep(response)
log.debug("Retry: %s", url)
return self.urlopen(
method, url, body, headers,
retries=retries, redirect=redirect,
assert_same_host=assert_same_host,
timeout=timeout, pool_timeout=pool_timeout,
release_conn=release_conn,
body_pos=body_pos, **response_kw)
return response
|
python
|
def urlopen(self, method, url, body=None, headers=None, retries=None,
redirect=True, assert_same_host=True, timeout=_Default,
pool_timeout=None, release_conn=None, chunked=False,
body_pos=None, **response_kw):
"""
Get a connection from the pool and perform an HTTP request. This is the
lowest level call for making a request, so you'll need to specify all
the raw details.
.. note::
More commonly, it's appropriate to use a convenience method provided
by :class:`.RequestMethods`, such as :meth:`request`.
.. note::
`release_conn` will only behave as expected if
`preload_content=False` because we want to make
`preload_content=False` the default behaviour someday soon without
breaking backwards compatibility.
:param method:
HTTP request method (such as GET, POST, PUT, etc.)
:param body:
Data to send in the request body (useful for creating
POST requests, see HTTPConnectionPool.post_url for
more convenience).
:param headers:
Dictionary of custom headers to send, such as User-Agent,
If-None-Match, etc. If None, pool headers are used. If provided,
these headers completely replace any pool-specific headers.
:param retries:
Configure the number of retries to allow before raising a
:class:`~urllib3.exceptions.MaxRetryError` exception.
Pass ``None`` to retry until you receive a response. Pass a
:class:`~urllib3.util.retry.Retry` object for fine-grained control
over different types of retries.
Pass an integer number to retry connection errors that many times,
but no other types of errors. Pass zero to never retry.
If ``False``, then retries are disabled and any exception is raised
immediately. Also, instead of raising a MaxRetryError on redirects,
the redirect response will be returned.
:type retries: :class:`~urllib3.util.retry.Retry`, False, or an int.
:param redirect:
If True, automatically handle redirects (status codes 301, 302,
303, 307, 308). Each redirect counts as a retry. Disabling retries
will disable redirect, too.
:param assert_same_host:
If ``True``, will make sure that the host of the pool requests is
consistent else will raise HostChangedError. When False, you can
use the pool on an HTTP proxy and request foreign hosts.
:param timeout:
If specified, overrides the default timeout for this one
request. It may be a float (in seconds) or an instance of
:class:`urllib3.util.Timeout`.
:param pool_timeout:
If set and the pool is set to block=True, then this method will
block for ``pool_timeout`` seconds and raise EmptyPoolError if no
connection is available within the time period.
:param release_conn:
If False, then the urlopen call will not release the connection
back into the pool once a response is received (but will release if
you read the entire contents of the response such as when
`preload_content=True`). This is useful if you're not preloading
the response's content immediately. You will need to call
``r.release_conn()`` on the response ``r`` to return the connection
back into the pool. If None, it takes the value of
``response_kw.get('preload_content', True)``.
:param chunked:
If True, urllib3 will send the body using chunked transfer
encoding. Otherwise, urllib3 will send the body using the standard
content-length form. Defaults to False.
:param int body_pos:
Position to seek to in file-like body in the event of a retry or
redirect. Typically this won't need to be set because urllib3 will
auto-populate the value when needed.
:param \\**response_kw:
Additional parameters are passed to
:meth:`urllib3.response.HTTPResponse.from_httplib`
"""
if headers is None:
headers = self.headers
if not isinstance(retries, Retry):
retries = Retry.from_int(retries, redirect=redirect, default=self.retries)
if release_conn is None:
release_conn = response_kw.get('preload_content', True)
# Check host
if assert_same_host and not self.is_same_host(url):
raise HostChangedError(self, url, retries)
conn = None
# Track whether `conn` needs to be released before
# returning/raising/recursing. Update this variable if necessary, and
# leave `release_conn` constant throughout the function. That way, if
# the function recurses, the original value of `release_conn` will be
# passed down into the recursive call, and its value will be respected.
#
# See issue #651 [1] for details.
#
# [1] <https://github.com/shazow/urllib3/issues/651>
release_this_conn = release_conn
# Merge the proxy headers. Only do this in HTTP. We have to copy the
# headers dict so we can safely change it without those changes being
# reflected in anyone else's copy.
if self.scheme == 'http':
headers = headers.copy()
headers.update(self.proxy_headers)
# Must keep the exception bound to a separate variable or else Python 3
# complains about UnboundLocalError.
err = None
# Keep track of whether we cleanly exited the except block. This
# ensures we do proper cleanup in finally.
clean_exit = False
# Rewind body position, if needed. Record current position
# for future rewinds in the event of a redirect/retry.
body_pos = set_file_position(body, body_pos)
try:
# Request a connection from the queue.
timeout_obj = self._get_timeout(timeout)
conn = self._get_conn(timeout=pool_timeout)
conn.timeout = timeout_obj.connect_timeout
is_new_proxy_conn = self.proxy is not None and not getattr(conn, 'sock', None)
if is_new_proxy_conn:
self._prepare_proxy(conn)
# Make the request on the httplib connection object.
httplib_response = self._make_request(conn, method, url,
timeout=timeout_obj,
body=body, headers=headers,
chunked=chunked)
# If we're going to release the connection in ``finally:``, then
# the response doesn't need to know about the connection. Otherwise
# it will also try to release it and we'll have a double-release
# mess.
response_conn = conn if not release_conn else None
# Pass method to Response for length checking
response_kw['request_method'] = method
# Import httplib's response into our own wrapper object
response = self.ResponseCls.from_httplib(httplib_response,
pool=self,
connection=response_conn,
retries=retries,
**response_kw)
# Everything went great!
clean_exit = True
except queue.Empty:
# Timed out by queue.
raise EmptyPoolError(self, "No pool connections are available.")
except (TimeoutError, HTTPException, SocketError, ProtocolError,
BaseSSLError, SSLError, CertificateError) as e:
# Discard the connection for these exceptions. It will be
# replaced during the next _get_conn() call.
clean_exit = False
if isinstance(e, (BaseSSLError, CertificateError)):
e = SSLError(e)
elif isinstance(e, (SocketError, NewConnectionError)) and self.proxy:
e = ProxyError('Cannot connect to proxy.', e)
elif isinstance(e, (SocketError, HTTPException)):
e = ProtocolError('Connection aborted.', e)
retries = retries.increment(method, url, error=e, _pool=self,
_stacktrace=sys.exc_info()[2])
retries.sleep()
# Keep track of the error for the retry warning.
err = e
finally:
if not clean_exit:
# We hit some kind of exception, handled or otherwise. We need
# to throw the connection away unless explicitly told not to.
# Close the connection, set the variable to None, and make sure
# we put the None back in the pool to avoid leaking it.
conn = conn and conn.close()
release_this_conn = True
if release_this_conn:
# Put the connection back to be reused. If the connection is
# expired then it will be None, which will get replaced with a
# fresh connection during _get_conn.
self._put_conn(conn)
if not conn:
# Try again
log.warning("Retrying (%r) after connection "
"broken by '%r': %s", retries, err, url)
return self.urlopen(method, url, body, headers, retries,
redirect, assert_same_host,
timeout=timeout, pool_timeout=pool_timeout,
release_conn=release_conn, body_pos=body_pos,
**response_kw)
def drain_and_release_conn(response):
try:
# discard any remaining response body, the connection will be
# released back to the pool once the entire response is read
response.read()
except (TimeoutError, HTTPException, SocketError, ProtocolError,
BaseSSLError, SSLError) as e:
pass
# Handle redirect?
redirect_location = redirect and response.get_redirect_location()
if redirect_location:
if response.status == 303:
method = 'GET'
try:
retries = retries.increment(method, url, response=response, _pool=self)
except MaxRetryError:
if retries.raise_on_redirect:
# Drain and release the connection for this response, since
# we're not returning it to be released manually.
drain_and_release_conn(response)
raise
return response
# drain and return the connection to the pool before recursing
drain_and_release_conn(response)
retries.sleep_for_retry(response)
log.debug("Redirecting %s -> %s", url, redirect_location)
return self.urlopen(
method, redirect_location, body, headers,
retries=retries, redirect=redirect,
assert_same_host=assert_same_host,
timeout=timeout, pool_timeout=pool_timeout,
release_conn=release_conn, body_pos=body_pos,
**response_kw)
# Check if we should retry the HTTP response.
has_retry_after = bool(response.getheader('Retry-After'))
if retries.is_retry(method, response.status, has_retry_after):
try:
retries = retries.increment(method, url, response=response, _pool=self)
except MaxRetryError:
if retries.raise_on_status:
# Drain and release the connection for this response, since
# we're not returning it to be released manually.
drain_and_release_conn(response)
raise
return response
# drain and return the connection to the pool before recursing
drain_and_release_conn(response)
retries.sleep(response)
log.debug("Retry: %s", url)
return self.urlopen(
method, url, body, headers,
retries=retries, redirect=redirect,
assert_same_host=assert_same_host,
timeout=timeout, pool_timeout=pool_timeout,
release_conn=release_conn,
body_pos=body_pos, **response_kw)
return response
|
[
"def",
"urlopen",
"(",
"self",
",",
"method",
",",
"url",
",",
"body",
"=",
"None",
",",
"headers",
"=",
"None",
",",
"retries",
"=",
"None",
",",
"redirect",
"=",
"True",
",",
"assert_same_host",
"=",
"True",
",",
"timeout",
"=",
"_Default",
",",
"pool_timeout",
"=",
"None",
",",
"release_conn",
"=",
"None",
",",
"chunked",
"=",
"False",
",",
"body_pos",
"=",
"None",
",",
"*",
"*",
"response_kw",
")",
":",
"if",
"headers",
"is",
"None",
":",
"headers",
"=",
"self",
".",
"headers",
"if",
"not",
"isinstance",
"(",
"retries",
",",
"Retry",
")",
":",
"retries",
"=",
"Retry",
".",
"from_int",
"(",
"retries",
",",
"redirect",
"=",
"redirect",
",",
"default",
"=",
"self",
".",
"retries",
")",
"if",
"release_conn",
"is",
"None",
":",
"release_conn",
"=",
"response_kw",
".",
"get",
"(",
"'preload_content'",
",",
"True",
")",
"# Check host",
"if",
"assert_same_host",
"and",
"not",
"self",
".",
"is_same_host",
"(",
"url",
")",
":",
"raise",
"HostChangedError",
"(",
"self",
",",
"url",
",",
"retries",
")",
"conn",
"=",
"None",
"# Track whether `conn` needs to be released before",
"# returning/raising/recursing. Update this variable if necessary, and",
"# leave `release_conn` constant throughout the function. That way, if",
"# the function recurses, the original value of `release_conn` will be",
"# passed down into the recursive call, and its value will be respected.",
"#",
"# See issue #651 [1] for details.",
"#",
"# [1] <https://github.com/shazow/urllib3/issues/651>",
"release_this_conn",
"=",
"release_conn",
"# Merge the proxy headers. Only do this in HTTP. We have to copy the",
"# headers dict so we can safely change it without those changes being",
"# reflected in anyone else's copy.",
"if",
"self",
".",
"scheme",
"==",
"'http'",
":",
"headers",
"=",
"headers",
".",
"copy",
"(",
")",
"headers",
".",
"update",
"(",
"self",
".",
"proxy_headers",
")",
"# Must keep the exception bound to a separate variable or else Python 3",
"# complains about UnboundLocalError.",
"err",
"=",
"None",
"# Keep track of whether we cleanly exited the except block. This",
"# ensures we do proper cleanup in finally.",
"clean_exit",
"=",
"False",
"# Rewind body position, if needed. Record current position",
"# for future rewinds in the event of a redirect/retry.",
"body_pos",
"=",
"set_file_position",
"(",
"body",
",",
"body_pos",
")",
"try",
":",
"# Request a connection from the queue.",
"timeout_obj",
"=",
"self",
".",
"_get_timeout",
"(",
"timeout",
")",
"conn",
"=",
"self",
".",
"_get_conn",
"(",
"timeout",
"=",
"pool_timeout",
")",
"conn",
".",
"timeout",
"=",
"timeout_obj",
".",
"connect_timeout",
"is_new_proxy_conn",
"=",
"self",
".",
"proxy",
"is",
"not",
"None",
"and",
"not",
"getattr",
"(",
"conn",
",",
"'sock'",
",",
"None",
")",
"if",
"is_new_proxy_conn",
":",
"self",
".",
"_prepare_proxy",
"(",
"conn",
")",
"# Make the request on the httplib connection object.",
"httplib_response",
"=",
"self",
".",
"_make_request",
"(",
"conn",
",",
"method",
",",
"url",
",",
"timeout",
"=",
"timeout_obj",
",",
"body",
"=",
"body",
",",
"headers",
"=",
"headers",
",",
"chunked",
"=",
"chunked",
")",
"# If we're going to release the connection in ``finally:``, then",
"# the response doesn't need to know about the connection. Otherwise",
"# it will also try to release it and we'll have a double-release",
"# mess.",
"response_conn",
"=",
"conn",
"if",
"not",
"release_conn",
"else",
"None",
"# Pass method to Response for length checking",
"response_kw",
"[",
"'request_method'",
"]",
"=",
"method",
"# Import httplib's response into our own wrapper object",
"response",
"=",
"self",
".",
"ResponseCls",
".",
"from_httplib",
"(",
"httplib_response",
",",
"pool",
"=",
"self",
",",
"connection",
"=",
"response_conn",
",",
"retries",
"=",
"retries",
",",
"*",
"*",
"response_kw",
")",
"# Everything went great!",
"clean_exit",
"=",
"True",
"except",
"queue",
".",
"Empty",
":",
"# Timed out by queue.",
"raise",
"EmptyPoolError",
"(",
"self",
",",
"\"No pool connections are available.\"",
")",
"except",
"(",
"TimeoutError",
",",
"HTTPException",
",",
"SocketError",
",",
"ProtocolError",
",",
"BaseSSLError",
",",
"SSLError",
",",
"CertificateError",
")",
"as",
"e",
":",
"# Discard the connection for these exceptions. It will be",
"# replaced during the next _get_conn() call.",
"clean_exit",
"=",
"False",
"if",
"isinstance",
"(",
"e",
",",
"(",
"BaseSSLError",
",",
"CertificateError",
")",
")",
":",
"e",
"=",
"SSLError",
"(",
"e",
")",
"elif",
"isinstance",
"(",
"e",
",",
"(",
"SocketError",
",",
"NewConnectionError",
")",
")",
"and",
"self",
".",
"proxy",
":",
"e",
"=",
"ProxyError",
"(",
"'Cannot connect to proxy.'",
",",
"e",
")",
"elif",
"isinstance",
"(",
"e",
",",
"(",
"SocketError",
",",
"HTTPException",
")",
")",
":",
"e",
"=",
"ProtocolError",
"(",
"'Connection aborted.'",
",",
"e",
")",
"retries",
"=",
"retries",
".",
"increment",
"(",
"method",
",",
"url",
",",
"error",
"=",
"e",
",",
"_pool",
"=",
"self",
",",
"_stacktrace",
"=",
"sys",
".",
"exc_info",
"(",
")",
"[",
"2",
"]",
")",
"retries",
".",
"sleep",
"(",
")",
"# Keep track of the error for the retry warning.",
"err",
"=",
"e",
"finally",
":",
"if",
"not",
"clean_exit",
":",
"# We hit some kind of exception, handled or otherwise. We need",
"# to throw the connection away unless explicitly told not to.",
"# Close the connection, set the variable to None, and make sure",
"# we put the None back in the pool to avoid leaking it.",
"conn",
"=",
"conn",
"and",
"conn",
".",
"close",
"(",
")",
"release_this_conn",
"=",
"True",
"if",
"release_this_conn",
":",
"# Put the connection back to be reused. If the connection is",
"# expired then it will be None, which will get replaced with a",
"# fresh connection during _get_conn.",
"self",
".",
"_put_conn",
"(",
"conn",
")",
"if",
"not",
"conn",
":",
"# Try again",
"log",
".",
"warning",
"(",
"\"Retrying (%r) after connection \"",
"\"broken by '%r': %s\"",
",",
"retries",
",",
"err",
",",
"url",
")",
"return",
"self",
".",
"urlopen",
"(",
"method",
",",
"url",
",",
"body",
",",
"headers",
",",
"retries",
",",
"redirect",
",",
"assert_same_host",
",",
"timeout",
"=",
"timeout",
",",
"pool_timeout",
"=",
"pool_timeout",
",",
"release_conn",
"=",
"release_conn",
",",
"body_pos",
"=",
"body_pos",
",",
"*",
"*",
"response_kw",
")",
"def",
"drain_and_release_conn",
"(",
"response",
")",
":",
"try",
":",
"# discard any remaining response body, the connection will be",
"# released back to the pool once the entire response is read",
"response",
".",
"read",
"(",
")",
"except",
"(",
"TimeoutError",
",",
"HTTPException",
",",
"SocketError",
",",
"ProtocolError",
",",
"BaseSSLError",
",",
"SSLError",
")",
"as",
"e",
":",
"pass",
"# Handle redirect?",
"redirect_location",
"=",
"redirect",
"and",
"response",
".",
"get_redirect_location",
"(",
")",
"if",
"redirect_location",
":",
"if",
"response",
".",
"status",
"==",
"303",
":",
"method",
"=",
"'GET'",
"try",
":",
"retries",
"=",
"retries",
".",
"increment",
"(",
"method",
",",
"url",
",",
"response",
"=",
"response",
",",
"_pool",
"=",
"self",
")",
"except",
"MaxRetryError",
":",
"if",
"retries",
".",
"raise_on_redirect",
":",
"# Drain and release the connection for this response, since",
"# we're not returning it to be released manually.",
"drain_and_release_conn",
"(",
"response",
")",
"raise",
"return",
"response",
"# drain and return the connection to the pool before recursing",
"drain_and_release_conn",
"(",
"response",
")",
"retries",
".",
"sleep_for_retry",
"(",
"response",
")",
"log",
".",
"debug",
"(",
"\"Redirecting %s -> %s\"",
",",
"url",
",",
"redirect_location",
")",
"return",
"self",
".",
"urlopen",
"(",
"method",
",",
"redirect_location",
",",
"body",
",",
"headers",
",",
"retries",
"=",
"retries",
",",
"redirect",
"=",
"redirect",
",",
"assert_same_host",
"=",
"assert_same_host",
",",
"timeout",
"=",
"timeout",
",",
"pool_timeout",
"=",
"pool_timeout",
",",
"release_conn",
"=",
"release_conn",
",",
"body_pos",
"=",
"body_pos",
",",
"*",
"*",
"response_kw",
")",
"# Check if we should retry the HTTP response.",
"has_retry_after",
"=",
"bool",
"(",
"response",
".",
"getheader",
"(",
"'Retry-After'",
")",
")",
"if",
"retries",
".",
"is_retry",
"(",
"method",
",",
"response",
".",
"status",
",",
"has_retry_after",
")",
":",
"try",
":",
"retries",
"=",
"retries",
".",
"increment",
"(",
"method",
",",
"url",
",",
"response",
"=",
"response",
",",
"_pool",
"=",
"self",
")",
"except",
"MaxRetryError",
":",
"if",
"retries",
".",
"raise_on_status",
":",
"# Drain and release the connection for this response, since",
"# we're not returning it to be released manually.",
"drain_and_release_conn",
"(",
"response",
")",
"raise",
"return",
"response",
"# drain and return the connection to the pool before recursing",
"drain_and_release_conn",
"(",
"response",
")",
"retries",
".",
"sleep",
"(",
"response",
")",
"log",
".",
"debug",
"(",
"\"Retry: %s\"",
",",
"url",
")",
"return",
"self",
".",
"urlopen",
"(",
"method",
",",
"url",
",",
"body",
",",
"headers",
",",
"retries",
"=",
"retries",
",",
"redirect",
"=",
"redirect",
",",
"assert_same_host",
"=",
"assert_same_host",
",",
"timeout",
"=",
"timeout",
",",
"pool_timeout",
"=",
"pool_timeout",
",",
"release_conn",
"=",
"release_conn",
",",
"body_pos",
"=",
"body_pos",
",",
"*",
"*",
"response_kw",
")",
"return",
"response"
] |
Get a connection from the pool and perform an HTTP request. This is the
lowest level call for making a request, so you'll need to specify all
the raw details.
.. note::
More commonly, it's appropriate to use a convenience method provided
by :class:`.RequestMethods`, such as :meth:`request`.
.. note::
`release_conn` will only behave as expected if
`preload_content=False` because we want to make
`preload_content=False` the default behaviour someday soon without
breaking backwards compatibility.
:param method:
HTTP request method (such as GET, POST, PUT, etc.)
:param body:
Data to send in the request body (useful for creating
POST requests, see HTTPConnectionPool.post_url for
more convenience).
:param headers:
Dictionary of custom headers to send, such as User-Agent,
If-None-Match, etc. If None, pool headers are used. If provided,
these headers completely replace any pool-specific headers.
:param retries:
Configure the number of retries to allow before raising a
:class:`~urllib3.exceptions.MaxRetryError` exception.
Pass ``None`` to retry until you receive a response. Pass a
:class:`~urllib3.util.retry.Retry` object for fine-grained control
over different types of retries.
Pass an integer number to retry connection errors that many times,
but no other types of errors. Pass zero to never retry.
If ``False``, then retries are disabled and any exception is raised
immediately. Also, instead of raising a MaxRetryError on redirects,
the redirect response will be returned.
:type retries: :class:`~urllib3.util.retry.Retry`, False, or an int.
:param redirect:
If True, automatically handle redirects (status codes 301, 302,
303, 307, 308). Each redirect counts as a retry. Disabling retries
will disable redirect, too.
:param assert_same_host:
If ``True``, will make sure that the host of the pool requests is
consistent else will raise HostChangedError. When False, you can
use the pool on an HTTP proxy and request foreign hosts.
:param timeout:
If specified, overrides the default timeout for this one
request. It may be a float (in seconds) or an instance of
:class:`urllib3.util.Timeout`.
:param pool_timeout:
If set and the pool is set to block=True, then this method will
block for ``pool_timeout`` seconds and raise EmptyPoolError if no
connection is available within the time period.
:param release_conn:
If False, then the urlopen call will not release the connection
back into the pool once a response is received (but will release if
you read the entire contents of the response such as when
`preload_content=True`). This is useful if you're not preloading
the response's content immediately. You will need to call
``r.release_conn()`` on the response ``r`` to return the connection
back into the pool. If None, it takes the value of
``response_kw.get('preload_content', True)``.
:param chunked:
If True, urllib3 will send the body using chunked transfer
encoding. Otherwise, urllib3 will send the body using the standard
content-length form. Defaults to False.
:param int body_pos:
Position to seek to in file-like body in the event of a retry or
redirect. Typically this won't need to be set because urllib3 will
auto-populate the value when needed.
:param \\**response_kw:
Additional parameters are passed to
:meth:`urllib3.response.HTTPResponse.from_httplib`
|
[
"Get",
"a",
"connection",
"from",
"the",
"pool",
"and",
"perform",
"an",
"HTTP",
"request",
".",
"This",
"is",
"the",
"lowest",
"level",
"call",
"for",
"making",
"a",
"request",
"so",
"you",
"ll",
"need",
"to",
"specify",
"all",
"the",
"raw",
"details",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/urllib3/connectionpool.py#L446-L733
|
train
|
pypa/pipenv
|
pipenv/vendor/urllib3/connectionpool.py
|
HTTPSConnectionPool._prepare_conn
|
def _prepare_conn(self, conn):
"""
Prepare the ``connection`` for :meth:`urllib3.util.ssl_wrap_socket`
and establish the tunnel if proxy is used.
"""
if isinstance(conn, VerifiedHTTPSConnection):
conn.set_cert(key_file=self.key_file,
cert_file=self.cert_file,
cert_reqs=self.cert_reqs,
ca_certs=self.ca_certs,
ca_cert_dir=self.ca_cert_dir,
assert_hostname=self.assert_hostname,
assert_fingerprint=self.assert_fingerprint)
conn.ssl_version = self.ssl_version
return conn
|
python
|
def _prepare_conn(self, conn):
"""
Prepare the ``connection`` for :meth:`urllib3.util.ssl_wrap_socket`
and establish the tunnel if proxy is used.
"""
if isinstance(conn, VerifiedHTTPSConnection):
conn.set_cert(key_file=self.key_file,
cert_file=self.cert_file,
cert_reqs=self.cert_reqs,
ca_certs=self.ca_certs,
ca_cert_dir=self.ca_cert_dir,
assert_hostname=self.assert_hostname,
assert_fingerprint=self.assert_fingerprint)
conn.ssl_version = self.ssl_version
return conn
|
[
"def",
"_prepare_conn",
"(",
"self",
",",
"conn",
")",
":",
"if",
"isinstance",
"(",
"conn",
",",
"VerifiedHTTPSConnection",
")",
":",
"conn",
".",
"set_cert",
"(",
"key_file",
"=",
"self",
".",
"key_file",
",",
"cert_file",
"=",
"self",
".",
"cert_file",
",",
"cert_reqs",
"=",
"self",
".",
"cert_reqs",
",",
"ca_certs",
"=",
"self",
".",
"ca_certs",
",",
"ca_cert_dir",
"=",
"self",
".",
"ca_cert_dir",
",",
"assert_hostname",
"=",
"self",
".",
"assert_hostname",
",",
"assert_fingerprint",
"=",
"self",
".",
"assert_fingerprint",
")",
"conn",
".",
"ssl_version",
"=",
"self",
".",
"ssl_version",
"return",
"conn"
] |
Prepare the ``connection`` for :meth:`urllib3.util.ssl_wrap_socket`
and establish the tunnel if proxy is used.
|
[
"Prepare",
"the",
"connection",
"for",
":",
"meth",
":",
"urllib3",
".",
"util",
".",
"ssl_wrap_socket",
"and",
"establish",
"the",
"tunnel",
"if",
"proxy",
"is",
"used",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/urllib3/connectionpool.py#L782-L797
|
train
|
pypa/pipenv
|
pipenv/vendor/urllib3/connectionpool.py
|
HTTPSConnectionPool._prepare_proxy
|
def _prepare_proxy(self, conn):
"""
Establish tunnel connection early, because otherwise httplib
would improperly set Host: header to proxy's IP:port.
"""
conn.set_tunnel(self._proxy_host, self.port, self.proxy_headers)
conn.connect()
|
python
|
def _prepare_proxy(self, conn):
"""
Establish tunnel connection early, because otherwise httplib
would improperly set Host: header to proxy's IP:port.
"""
conn.set_tunnel(self._proxy_host, self.port, self.proxy_headers)
conn.connect()
|
[
"def",
"_prepare_proxy",
"(",
"self",
",",
"conn",
")",
":",
"conn",
".",
"set_tunnel",
"(",
"self",
".",
"_proxy_host",
",",
"self",
".",
"port",
",",
"self",
".",
"proxy_headers",
")",
"conn",
".",
"connect",
"(",
")"
] |
Establish tunnel connection early, because otherwise httplib
would improperly set Host: header to proxy's IP:port.
|
[
"Establish",
"tunnel",
"connection",
"early",
"because",
"otherwise",
"httplib",
"would",
"improperly",
"set",
"Host",
":",
"header",
"to",
"proxy",
"s",
"IP",
":",
"port",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/urllib3/connectionpool.py#L799-L805
|
train
|
pypa/pipenv
|
pipenv/vendor/pythonfinder/pythonfinder.py
|
Finder.reload_system_path
|
def reload_system_path(self):
# type: () -> None
"""
Rebuilds the base system path and all of the contained finders within it.
This will re-apply any changes to the environment or any version changes on the system.
"""
if self._system_path is not None:
self._system_path.clear_caches()
self._system_path = None
six.moves.reload_module(pyfinder_path)
self._system_path = self.create_system_path()
|
python
|
def reload_system_path(self):
# type: () -> None
"""
Rebuilds the base system path and all of the contained finders within it.
This will re-apply any changes to the environment or any version changes on the system.
"""
if self._system_path is not None:
self._system_path.clear_caches()
self._system_path = None
six.moves.reload_module(pyfinder_path)
self._system_path = self.create_system_path()
|
[
"def",
"reload_system_path",
"(",
"self",
")",
":",
"# type: () -> None",
"if",
"self",
".",
"_system_path",
"is",
"not",
"None",
":",
"self",
".",
"_system_path",
".",
"clear_caches",
"(",
")",
"self",
".",
"_system_path",
"=",
"None",
"six",
".",
"moves",
".",
"reload_module",
"(",
"pyfinder_path",
")",
"self",
".",
"_system_path",
"=",
"self",
".",
"create_system_path",
"(",
")"
] |
Rebuilds the base system path and all of the contained finders within it.
This will re-apply any changes to the environment or any version changes on the system.
|
[
"Rebuilds",
"the",
"base",
"system",
"path",
"and",
"all",
"of",
"the",
"contained",
"finders",
"within",
"it",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pythonfinder/pythonfinder.py#L78-L90
|
train
|
pypa/pipenv
|
pipenv/vendor/pythonfinder/pythonfinder.py
|
Finder.find_python_version
|
def find_python_version(
self, major=None, minor=None, patch=None, pre=None, dev=None, arch=None, name=None
):
# type: (Optional[Union[str, int]], Optional[int], Optional[int], Optional[bool], Optional[bool], Optional[str], Optional[str]) -> PathEntry
"""
Find the python version which corresponds most closely to the version requested.
:param Union[str, int] major: The major version to look for, or the full version, or the name of the target version.
:param Optional[int] minor: The minor version. If provided, disables string-based lookups from the major version field.
:param Optional[int] patch: The patch version.
:param Optional[bool] pre: If provided, specifies whether to search pre-releases.
:param Optional[bool] dev: If provided, whether to search dev-releases.
:param Optional[str] arch: If provided, which architecture to search.
:param Optional[str] name: *Name* of the target python, e.g. ``anaconda3-5.3.0``
:return: A new *PathEntry* pointer at a matching python version, if one can be located.
:rtype: :class:`pythonfinder.models.path.PathEntry`
"""
from .models import PythonVersion
minor = int(minor) if minor is not None else minor
patch = int(patch) if patch is not None else patch
version_dict = {
"minor": minor,
"patch": patch,
} # type: Dict[str, Union[str, int, Any]]
if (
isinstance(major, six.string_types)
and pre is None
and minor is None
and dev is None
and patch is None
):
if arch is None and "-" in major and major[0].isdigit():
orig_string = "{0!s}".format(major)
major, _, arch = major.rpartition("-")
if arch.startswith("x"):
arch = arch.lstrip("x")
if arch.lower().endswith("bit"):
arch = arch.lower().replace("bit", "")
if not (arch.isdigit() and (int(arch) & int(arch) - 1) == 0):
major = orig_string
arch = None
else:
arch = "{0}bit".format(arch)
try:
version_dict = PythonVersion.parse(major)
except (ValueError, InvalidPythonVersion):
if name is None:
name = "{0!s}".format(major)
major = None
version_dict = {}
elif major[0].isalpha():
name = "%s" % major
major = None
else:
if "." in major and all(part.isdigit() for part in major.split(".")[:2]):
match = version_re.match(major)
version_dict = match.groupdict()
version_dict["is_prerelease"] = bool(
version_dict.get("prerel", False)
)
version_dict["is_devrelease"] = bool(version_dict.get("dev", False))
else:
version_dict = {
"major": major,
"minor": minor,
"patch": patch,
"pre": pre,
"dev": dev,
"arch": arch,
}
if version_dict.get("minor") is not None:
minor = int(version_dict["minor"])
if version_dict.get("patch") is not None:
patch = int(version_dict["patch"])
if version_dict.get("major") is not None:
major = int(version_dict["major"])
_pre = version_dict.get("is_prerelease", pre)
pre = bool(_pre) if _pre is not None else pre
_dev = version_dict.get("is_devrelease", dev)
dev = bool(_dev) if _dev is not None else dev
arch = (
version_dict.get("architecture", None) if arch is None else arch
) # type: ignore
if os.name == "nt" and self.windows_finder is not None:
match = self.windows_finder.find_python_version(
major=major,
minor=minor,
patch=patch,
pre=pre,
dev=dev,
arch=arch,
name=name,
)
if match:
return match
return self.system_path.find_python_version(
major=major, minor=minor, patch=patch, pre=pre, dev=dev, arch=arch, name=name
)
|
python
|
def find_python_version(
self, major=None, minor=None, patch=None, pre=None, dev=None, arch=None, name=None
):
# type: (Optional[Union[str, int]], Optional[int], Optional[int], Optional[bool], Optional[bool], Optional[str], Optional[str]) -> PathEntry
"""
Find the python version which corresponds most closely to the version requested.
:param Union[str, int] major: The major version to look for, or the full version, or the name of the target version.
:param Optional[int] minor: The minor version. If provided, disables string-based lookups from the major version field.
:param Optional[int] patch: The patch version.
:param Optional[bool] pre: If provided, specifies whether to search pre-releases.
:param Optional[bool] dev: If provided, whether to search dev-releases.
:param Optional[str] arch: If provided, which architecture to search.
:param Optional[str] name: *Name* of the target python, e.g. ``anaconda3-5.3.0``
:return: A new *PathEntry* pointer at a matching python version, if one can be located.
:rtype: :class:`pythonfinder.models.path.PathEntry`
"""
from .models import PythonVersion
minor = int(minor) if minor is not None else minor
patch = int(patch) if patch is not None else patch
version_dict = {
"minor": minor,
"patch": patch,
} # type: Dict[str, Union[str, int, Any]]
if (
isinstance(major, six.string_types)
and pre is None
and minor is None
and dev is None
and patch is None
):
if arch is None and "-" in major and major[0].isdigit():
orig_string = "{0!s}".format(major)
major, _, arch = major.rpartition("-")
if arch.startswith("x"):
arch = arch.lstrip("x")
if arch.lower().endswith("bit"):
arch = arch.lower().replace("bit", "")
if not (arch.isdigit() and (int(arch) & int(arch) - 1) == 0):
major = orig_string
arch = None
else:
arch = "{0}bit".format(arch)
try:
version_dict = PythonVersion.parse(major)
except (ValueError, InvalidPythonVersion):
if name is None:
name = "{0!s}".format(major)
major = None
version_dict = {}
elif major[0].isalpha():
name = "%s" % major
major = None
else:
if "." in major and all(part.isdigit() for part in major.split(".")[:2]):
match = version_re.match(major)
version_dict = match.groupdict()
version_dict["is_prerelease"] = bool(
version_dict.get("prerel", False)
)
version_dict["is_devrelease"] = bool(version_dict.get("dev", False))
else:
version_dict = {
"major": major,
"minor": minor,
"patch": patch,
"pre": pre,
"dev": dev,
"arch": arch,
}
if version_dict.get("minor") is not None:
minor = int(version_dict["minor"])
if version_dict.get("patch") is not None:
patch = int(version_dict["patch"])
if version_dict.get("major") is not None:
major = int(version_dict["major"])
_pre = version_dict.get("is_prerelease", pre)
pre = bool(_pre) if _pre is not None else pre
_dev = version_dict.get("is_devrelease", dev)
dev = bool(_dev) if _dev is not None else dev
arch = (
version_dict.get("architecture", None) if arch is None else arch
) # type: ignore
if os.name == "nt" and self.windows_finder is not None:
match = self.windows_finder.find_python_version(
major=major,
minor=minor,
patch=patch,
pre=pre,
dev=dev,
arch=arch,
name=name,
)
if match:
return match
return self.system_path.find_python_version(
major=major, minor=minor, patch=patch, pre=pre, dev=dev, arch=arch, name=name
)
|
[
"def",
"find_python_version",
"(",
"self",
",",
"major",
"=",
"None",
",",
"minor",
"=",
"None",
",",
"patch",
"=",
"None",
",",
"pre",
"=",
"None",
",",
"dev",
"=",
"None",
",",
"arch",
"=",
"None",
",",
"name",
"=",
"None",
")",
":",
"# type: (Optional[Union[str, int]], Optional[int], Optional[int], Optional[bool], Optional[bool], Optional[str], Optional[str]) -> PathEntry",
"from",
".",
"models",
"import",
"PythonVersion",
"minor",
"=",
"int",
"(",
"minor",
")",
"if",
"minor",
"is",
"not",
"None",
"else",
"minor",
"patch",
"=",
"int",
"(",
"patch",
")",
"if",
"patch",
"is",
"not",
"None",
"else",
"patch",
"version_dict",
"=",
"{",
"\"minor\"",
":",
"minor",
",",
"\"patch\"",
":",
"patch",
",",
"}",
"# type: Dict[str, Union[str, int, Any]]",
"if",
"(",
"isinstance",
"(",
"major",
",",
"six",
".",
"string_types",
")",
"and",
"pre",
"is",
"None",
"and",
"minor",
"is",
"None",
"and",
"dev",
"is",
"None",
"and",
"patch",
"is",
"None",
")",
":",
"if",
"arch",
"is",
"None",
"and",
"\"-\"",
"in",
"major",
"and",
"major",
"[",
"0",
"]",
".",
"isdigit",
"(",
")",
":",
"orig_string",
"=",
"\"{0!s}\"",
".",
"format",
"(",
"major",
")",
"major",
",",
"_",
",",
"arch",
"=",
"major",
".",
"rpartition",
"(",
"\"-\"",
")",
"if",
"arch",
".",
"startswith",
"(",
"\"x\"",
")",
":",
"arch",
"=",
"arch",
".",
"lstrip",
"(",
"\"x\"",
")",
"if",
"arch",
".",
"lower",
"(",
")",
".",
"endswith",
"(",
"\"bit\"",
")",
":",
"arch",
"=",
"arch",
".",
"lower",
"(",
")",
".",
"replace",
"(",
"\"bit\"",
",",
"\"\"",
")",
"if",
"not",
"(",
"arch",
".",
"isdigit",
"(",
")",
"and",
"(",
"int",
"(",
"arch",
")",
"&",
"int",
"(",
"arch",
")",
"-",
"1",
")",
"==",
"0",
")",
":",
"major",
"=",
"orig_string",
"arch",
"=",
"None",
"else",
":",
"arch",
"=",
"\"{0}bit\"",
".",
"format",
"(",
"arch",
")",
"try",
":",
"version_dict",
"=",
"PythonVersion",
".",
"parse",
"(",
"major",
")",
"except",
"(",
"ValueError",
",",
"InvalidPythonVersion",
")",
":",
"if",
"name",
"is",
"None",
":",
"name",
"=",
"\"{0!s}\"",
".",
"format",
"(",
"major",
")",
"major",
"=",
"None",
"version_dict",
"=",
"{",
"}",
"elif",
"major",
"[",
"0",
"]",
".",
"isalpha",
"(",
")",
":",
"name",
"=",
"\"%s\"",
"%",
"major",
"major",
"=",
"None",
"else",
":",
"if",
"\".\"",
"in",
"major",
"and",
"all",
"(",
"part",
".",
"isdigit",
"(",
")",
"for",
"part",
"in",
"major",
".",
"split",
"(",
"\".\"",
")",
"[",
":",
"2",
"]",
")",
":",
"match",
"=",
"version_re",
".",
"match",
"(",
"major",
")",
"version_dict",
"=",
"match",
".",
"groupdict",
"(",
")",
"version_dict",
"[",
"\"is_prerelease\"",
"]",
"=",
"bool",
"(",
"version_dict",
".",
"get",
"(",
"\"prerel\"",
",",
"False",
")",
")",
"version_dict",
"[",
"\"is_devrelease\"",
"]",
"=",
"bool",
"(",
"version_dict",
".",
"get",
"(",
"\"dev\"",
",",
"False",
")",
")",
"else",
":",
"version_dict",
"=",
"{",
"\"major\"",
":",
"major",
",",
"\"minor\"",
":",
"minor",
",",
"\"patch\"",
":",
"patch",
",",
"\"pre\"",
":",
"pre",
",",
"\"dev\"",
":",
"dev",
",",
"\"arch\"",
":",
"arch",
",",
"}",
"if",
"version_dict",
".",
"get",
"(",
"\"minor\"",
")",
"is",
"not",
"None",
":",
"minor",
"=",
"int",
"(",
"version_dict",
"[",
"\"minor\"",
"]",
")",
"if",
"version_dict",
".",
"get",
"(",
"\"patch\"",
")",
"is",
"not",
"None",
":",
"patch",
"=",
"int",
"(",
"version_dict",
"[",
"\"patch\"",
"]",
")",
"if",
"version_dict",
".",
"get",
"(",
"\"major\"",
")",
"is",
"not",
"None",
":",
"major",
"=",
"int",
"(",
"version_dict",
"[",
"\"major\"",
"]",
")",
"_pre",
"=",
"version_dict",
".",
"get",
"(",
"\"is_prerelease\"",
",",
"pre",
")",
"pre",
"=",
"bool",
"(",
"_pre",
")",
"if",
"_pre",
"is",
"not",
"None",
"else",
"pre",
"_dev",
"=",
"version_dict",
".",
"get",
"(",
"\"is_devrelease\"",
",",
"dev",
")",
"dev",
"=",
"bool",
"(",
"_dev",
")",
"if",
"_dev",
"is",
"not",
"None",
"else",
"dev",
"arch",
"=",
"(",
"version_dict",
".",
"get",
"(",
"\"architecture\"",
",",
"None",
")",
"if",
"arch",
"is",
"None",
"else",
"arch",
")",
"# type: ignore",
"if",
"os",
".",
"name",
"==",
"\"nt\"",
"and",
"self",
".",
"windows_finder",
"is",
"not",
"None",
":",
"match",
"=",
"self",
".",
"windows_finder",
".",
"find_python_version",
"(",
"major",
"=",
"major",
",",
"minor",
"=",
"minor",
",",
"patch",
"=",
"patch",
",",
"pre",
"=",
"pre",
",",
"dev",
"=",
"dev",
",",
"arch",
"=",
"arch",
",",
"name",
"=",
"name",
",",
")",
"if",
"match",
":",
"return",
"match",
"return",
"self",
".",
"system_path",
".",
"find_python_version",
"(",
"major",
"=",
"major",
",",
"minor",
"=",
"minor",
",",
"patch",
"=",
"patch",
",",
"pre",
"=",
"pre",
",",
"dev",
"=",
"dev",
",",
"arch",
"=",
"arch",
",",
"name",
"=",
"name",
")"
] |
Find the python version which corresponds most closely to the version requested.
:param Union[str, int] major: The major version to look for, or the full version, or the name of the target version.
:param Optional[int] minor: The minor version. If provided, disables string-based lookups from the major version field.
:param Optional[int] patch: The patch version.
:param Optional[bool] pre: If provided, specifies whether to search pre-releases.
:param Optional[bool] dev: If provided, whether to search dev-releases.
:param Optional[str] arch: If provided, which architecture to search.
:param Optional[str] name: *Name* of the target python, e.g. ``anaconda3-5.3.0``
:return: A new *PathEntry* pointer at a matching python version, if one can be located.
:rtype: :class:`pythonfinder.models.path.PathEntry`
|
[
"Find",
"the",
"python",
"version",
"which",
"corresponds",
"most",
"closely",
"to",
"the",
"version",
"requested",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pythonfinder/pythonfinder.py#L122-L223
|
train
|
pypa/pipenv
|
pipenv/vendor/shellingham/posix.py
|
get_shell
|
def get_shell(pid=None, max_depth=6):
"""Get the shell that the supplied pid or os.getpid() is running in.
"""
pid = str(pid or os.getpid())
mapping = _get_process_mapping()
login_shell = os.environ.get('SHELL', '')
for _ in range(max_depth):
try:
proc = mapping[pid]
except KeyError:
break
name = os.path.basename(proc.args[0]).lower()
if name in SHELL_NAMES:
return (name, proc.args[0])
elif proc.args[0].startswith('-'):
# This is the login shell. Use the SHELL environ if possible
# because it provides better information.
if login_shell:
name = login_shell.lower()
else:
name = proc.args[0][1:].lower()
return (os.path.basename(name), name)
pid = proc.ppid # Go up one level.
return None
|
python
|
def get_shell(pid=None, max_depth=6):
"""Get the shell that the supplied pid or os.getpid() is running in.
"""
pid = str(pid or os.getpid())
mapping = _get_process_mapping()
login_shell = os.environ.get('SHELL', '')
for _ in range(max_depth):
try:
proc = mapping[pid]
except KeyError:
break
name = os.path.basename(proc.args[0]).lower()
if name in SHELL_NAMES:
return (name, proc.args[0])
elif proc.args[0].startswith('-'):
# This is the login shell. Use the SHELL environ if possible
# because it provides better information.
if login_shell:
name = login_shell.lower()
else:
name = proc.args[0][1:].lower()
return (os.path.basename(name), name)
pid = proc.ppid # Go up one level.
return None
|
[
"def",
"get_shell",
"(",
"pid",
"=",
"None",
",",
"max_depth",
"=",
"6",
")",
":",
"pid",
"=",
"str",
"(",
"pid",
"or",
"os",
".",
"getpid",
"(",
")",
")",
"mapping",
"=",
"_get_process_mapping",
"(",
")",
"login_shell",
"=",
"os",
".",
"environ",
".",
"get",
"(",
"'SHELL'",
",",
"''",
")",
"for",
"_",
"in",
"range",
"(",
"max_depth",
")",
":",
"try",
":",
"proc",
"=",
"mapping",
"[",
"pid",
"]",
"except",
"KeyError",
":",
"break",
"name",
"=",
"os",
".",
"path",
".",
"basename",
"(",
"proc",
".",
"args",
"[",
"0",
"]",
")",
".",
"lower",
"(",
")",
"if",
"name",
"in",
"SHELL_NAMES",
":",
"return",
"(",
"name",
",",
"proc",
".",
"args",
"[",
"0",
"]",
")",
"elif",
"proc",
".",
"args",
"[",
"0",
"]",
".",
"startswith",
"(",
"'-'",
")",
":",
"# This is the login shell. Use the SHELL environ if possible",
"# because it provides better information.",
"if",
"login_shell",
":",
"name",
"=",
"login_shell",
".",
"lower",
"(",
")",
"else",
":",
"name",
"=",
"proc",
".",
"args",
"[",
"0",
"]",
"[",
"1",
":",
"]",
".",
"lower",
"(",
")",
"return",
"(",
"os",
".",
"path",
".",
"basename",
"(",
"name",
")",
",",
"name",
")",
"pid",
"=",
"proc",
".",
"ppid",
"# Go up one level.",
"return",
"None"
] |
Get the shell that the supplied pid or os.getpid() is running in.
|
[
"Get",
"the",
"shell",
"that",
"the",
"supplied",
"pid",
"or",
"os",
".",
"getpid",
"()",
"is",
"running",
"in",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/shellingham/posix.py#L33-L56
|
train
|
pypa/pipenv
|
pipenv/vendor/backports/enum/__init__.py
|
_make_class_unpicklable
|
def _make_class_unpicklable(cls):
"""Make the given class un-picklable."""
def _break_on_call_reduce(self, protocol=None):
raise TypeError('%r cannot be pickled' % self)
cls.__reduce_ex__ = _break_on_call_reduce
cls.__module__ = '<unknown>'
|
python
|
def _make_class_unpicklable(cls):
"""Make the given class un-picklable."""
def _break_on_call_reduce(self, protocol=None):
raise TypeError('%r cannot be pickled' % self)
cls.__reduce_ex__ = _break_on_call_reduce
cls.__module__ = '<unknown>'
|
[
"def",
"_make_class_unpicklable",
"(",
"cls",
")",
":",
"def",
"_break_on_call_reduce",
"(",
"self",
",",
"protocol",
"=",
"None",
")",
":",
"raise",
"TypeError",
"(",
"'%r cannot be pickled'",
"%",
"self",
")",
"cls",
".",
"__reduce_ex__",
"=",
"_break_on_call_reduce",
"cls",
".",
"__module__",
"=",
"'<unknown>'"
] |
Make the given class un-picklable.
|
[
"Make",
"the",
"given",
"class",
"un",
"-",
"picklable",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/backports/enum/__init__.py#L86-L91
|
train
|
pypa/pipenv
|
pipenv/vendor/backports/enum/__init__.py
|
_convert
|
def _convert(cls, name, module, filter, source=None):
"""
Create a new Enum subclass that replaces a collection of global constants
"""
# convert all constants from source (or module) that pass filter() to
# a new Enum called name, and export the enum and its members back to
# module;
# also, replace the __reduce_ex__ method so unpickling works in
# previous Python versions
module_globals = vars(_sys.modules[module])
if source:
source = vars(source)
else:
source = module_globals
members = dict((name, value) for name, value in source.items() if filter(name))
cls = cls(name, members, module=module)
cls.__reduce_ex__ = _reduce_ex_by_name
module_globals.update(cls.__members__)
module_globals[name] = cls
return cls
|
python
|
def _convert(cls, name, module, filter, source=None):
"""
Create a new Enum subclass that replaces a collection of global constants
"""
# convert all constants from source (or module) that pass filter() to
# a new Enum called name, and export the enum and its members back to
# module;
# also, replace the __reduce_ex__ method so unpickling works in
# previous Python versions
module_globals = vars(_sys.modules[module])
if source:
source = vars(source)
else:
source = module_globals
members = dict((name, value) for name, value in source.items() if filter(name))
cls = cls(name, members, module=module)
cls.__reduce_ex__ = _reduce_ex_by_name
module_globals.update(cls.__members__)
module_globals[name] = cls
return cls
|
[
"def",
"_convert",
"(",
"cls",
",",
"name",
",",
"module",
",",
"filter",
",",
"source",
"=",
"None",
")",
":",
"# convert all constants from source (or module) that pass filter() to",
"# a new Enum called name, and export the enum and its members back to",
"# module;",
"# also, replace the __reduce_ex__ method so unpickling works in",
"# previous Python versions",
"module_globals",
"=",
"vars",
"(",
"_sys",
".",
"modules",
"[",
"module",
"]",
")",
"if",
"source",
":",
"source",
"=",
"vars",
"(",
"source",
")",
"else",
":",
"source",
"=",
"module_globals",
"members",
"=",
"dict",
"(",
"(",
"name",
",",
"value",
")",
"for",
"name",
",",
"value",
"in",
"source",
".",
"items",
"(",
")",
"if",
"filter",
"(",
"name",
")",
")",
"cls",
"=",
"cls",
"(",
"name",
",",
"members",
",",
"module",
"=",
"module",
")",
"cls",
".",
"__reduce_ex__",
"=",
"_reduce_ex_by_name",
"module_globals",
".",
"update",
"(",
"cls",
".",
"__members__",
")",
"module_globals",
"[",
"name",
"]",
"=",
"cls",
"return",
"cls"
] |
Create a new Enum subclass that replaces a collection of global constants
|
[
"Create",
"a",
"new",
"Enum",
"subclass",
"that",
"replaces",
"a",
"collection",
"of",
"global",
"constants"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/backports/enum/__init__.py#L789-L808
|
train
|
pypa/pipenv
|
pipenv/vendor/backports/enum/__init__.py
|
unique
|
def unique(enumeration):
"""Class decorator that ensures only unique members exist in an enumeration."""
duplicates = []
for name, member in enumeration.__members__.items():
if name != member.name:
duplicates.append((name, member.name))
if duplicates:
duplicate_names = ', '.join(
["%s -> %s" % (alias, name) for (alias, name) in duplicates]
)
raise ValueError('duplicate names found in %r: %s' %
(enumeration, duplicate_names)
)
return enumeration
|
python
|
def unique(enumeration):
"""Class decorator that ensures only unique members exist in an enumeration."""
duplicates = []
for name, member in enumeration.__members__.items():
if name != member.name:
duplicates.append((name, member.name))
if duplicates:
duplicate_names = ', '.join(
["%s -> %s" % (alias, name) for (alias, name) in duplicates]
)
raise ValueError('duplicate names found in %r: %s' %
(enumeration, duplicate_names)
)
return enumeration
|
[
"def",
"unique",
"(",
"enumeration",
")",
":",
"duplicates",
"=",
"[",
"]",
"for",
"name",
",",
"member",
"in",
"enumeration",
".",
"__members__",
".",
"items",
"(",
")",
":",
"if",
"name",
"!=",
"member",
".",
"name",
":",
"duplicates",
".",
"append",
"(",
"(",
"name",
",",
"member",
".",
"name",
")",
")",
"if",
"duplicates",
":",
"duplicate_names",
"=",
"', '",
".",
"join",
"(",
"[",
"\"%s -> %s\"",
"%",
"(",
"alias",
",",
"name",
")",
"for",
"(",
"alias",
",",
"name",
")",
"in",
"duplicates",
"]",
")",
"raise",
"ValueError",
"(",
"'duplicate names found in %r: %s'",
"%",
"(",
"enumeration",
",",
"duplicate_names",
")",
")",
"return",
"enumeration"
] |
Class decorator that ensures only unique members exist in an enumeration.
|
[
"Class",
"decorator",
"that",
"ensures",
"only",
"unique",
"members",
"exist",
"in",
"an",
"enumeration",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/backports/enum/__init__.py#L824-L837
|
train
|
pypa/pipenv
|
pipenv/vendor/backports/enum/__init__.py
|
EnumMeta._create_
|
def _create_(cls, class_name, names=None, module=None, type=None, start=1):
"""Convenience method to create a new Enum class.
`names` can be:
* A string containing member names, separated either with spaces or
commas. Values are auto-numbered from 1.
* An iterable of member names. Values are auto-numbered from 1.
* An iterable of (member name, value) pairs.
* A mapping of member name -> value.
"""
if pyver < 3.0:
# if class_name is unicode, attempt a conversion to ASCII
if isinstance(class_name, unicode):
try:
class_name = class_name.encode('ascii')
except UnicodeEncodeError:
raise TypeError('%r is not representable in ASCII' % class_name)
metacls = cls.__class__
if type is None:
bases = (cls, )
else:
bases = (type, cls)
classdict = metacls.__prepare__(class_name, bases)
_order_ = []
# special processing needed for names?
if isinstance(names, basestring):
names = names.replace(',', ' ').split()
if isinstance(names, (tuple, list)) and isinstance(names[0], basestring):
names = [(e, i+start) for (i, e) in enumerate(names)]
# Here, names is either an iterable of (name, value) or a mapping.
item = None # in case names is empty
for item in names:
if isinstance(item, basestring):
member_name, member_value = item, names[item]
else:
member_name, member_value = item
classdict[member_name] = member_value
_order_.append(member_name)
# only set _order_ in classdict if name/value was not from a mapping
if not isinstance(item, basestring):
classdict['_order_'] = ' '.join(_order_)
enum_class = metacls.__new__(metacls, class_name, bases, classdict)
# TODO: replace the frame hack if a blessed way to know the calling
# module is ever developed
if module is None:
try:
module = _sys._getframe(2).f_globals['__name__']
except (AttributeError, ValueError):
pass
if module is None:
_make_class_unpicklable(enum_class)
else:
enum_class.__module__ = module
return enum_class
|
python
|
def _create_(cls, class_name, names=None, module=None, type=None, start=1):
"""Convenience method to create a new Enum class.
`names` can be:
* A string containing member names, separated either with spaces or
commas. Values are auto-numbered from 1.
* An iterable of member names. Values are auto-numbered from 1.
* An iterable of (member name, value) pairs.
* A mapping of member name -> value.
"""
if pyver < 3.0:
# if class_name is unicode, attempt a conversion to ASCII
if isinstance(class_name, unicode):
try:
class_name = class_name.encode('ascii')
except UnicodeEncodeError:
raise TypeError('%r is not representable in ASCII' % class_name)
metacls = cls.__class__
if type is None:
bases = (cls, )
else:
bases = (type, cls)
classdict = metacls.__prepare__(class_name, bases)
_order_ = []
# special processing needed for names?
if isinstance(names, basestring):
names = names.replace(',', ' ').split()
if isinstance(names, (tuple, list)) and isinstance(names[0], basestring):
names = [(e, i+start) for (i, e) in enumerate(names)]
# Here, names is either an iterable of (name, value) or a mapping.
item = None # in case names is empty
for item in names:
if isinstance(item, basestring):
member_name, member_value = item, names[item]
else:
member_name, member_value = item
classdict[member_name] = member_value
_order_.append(member_name)
# only set _order_ in classdict if name/value was not from a mapping
if not isinstance(item, basestring):
classdict['_order_'] = ' '.join(_order_)
enum_class = metacls.__new__(metacls, class_name, bases, classdict)
# TODO: replace the frame hack if a blessed way to know the calling
# module is ever developed
if module is None:
try:
module = _sys._getframe(2).f_globals['__name__']
except (AttributeError, ValueError):
pass
if module is None:
_make_class_unpicklable(enum_class)
else:
enum_class.__module__ = module
return enum_class
|
[
"def",
"_create_",
"(",
"cls",
",",
"class_name",
",",
"names",
"=",
"None",
",",
"module",
"=",
"None",
",",
"type",
"=",
"None",
",",
"start",
"=",
"1",
")",
":",
"if",
"pyver",
"<",
"3.0",
":",
"# if class_name is unicode, attempt a conversion to ASCII",
"if",
"isinstance",
"(",
"class_name",
",",
"unicode",
")",
":",
"try",
":",
"class_name",
"=",
"class_name",
".",
"encode",
"(",
"'ascii'",
")",
"except",
"UnicodeEncodeError",
":",
"raise",
"TypeError",
"(",
"'%r is not representable in ASCII'",
"%",
"class_name",
")",
"metacls",
"=",
"cls",
".",
"__class__",
"if",
"type",
"is",
"None",
":",
"bases",
"=",
"(",
"cls",
",",
")",
"else",
":",
"bases",
"=",
"(",
"type",
",",
"cls",
")",
"classdict",
"=",
"metacls",
".",
"__prepare__",
"(",
"class_name",
",",
"bases",
")",
"_order_",
"=",
"[",
"]",
"# special processing needed for names?",
"if",
"isinstance",
"(",
"names",
",",
"basestring",
")",
":",
"names",
"=",
"names",
".",
"replace",
"(",
"','",
",",
"' '",
")",
".",
"split",
"(",
")",
"if",
"isinstance",
"(",
"names",
",",
"(",
"tuple",
",",
"list",
")",
")",
"and",
"isinstance",
"(",
"names",
"[",
"0",
"]",
",",
"basestring",
")",
":",
"names",
"=",
"[",
"(",
"e",
",",
"i",
"+",
"start",
")",
"for",
"(",
"i",
",",
"e",
")",
"in",
"enumerate",
"(",
"names",
")",
"]",
"# Here, names is either an iterable of (name, value) or a mapping.",
"item",
"=",
"None",
"# in case names is empty",
"for",
"item",
"in",
"names",
":",
"if",
"isinstance",
"(",
"item",
",",
"basestring",
")",
":",
"member_name",
",",
"member_value",
"=",
"item",
",",
"names",
"[",
"item",
"]",
"else",
":",
"member_name",
",",
"member_value",
"=",
"item",
"classdict",
"[",
"member_name",
"]",
"=",
"member_value",
"_order_",
".",
"append",
"(",
"member_name",
")",
"# only set _order_ in classdict if name/value was not from a mapping",
"if",
"not",
"isinstance",
"(",
"item",
",",
"basestring",
")",
":",
"classdict",
"[",
"'_order_'",
"]",
"=",
"' '",
".",
"join",
"(",
"_order_",
")",
"enum_class",
"=",
"metacls",
".",
"__new__",
"(",
"metacls",
",",
"class_name",
",",
"bases",
",",
"classdict",
")",
"# TODO: replace the frame hack if a blessed way to know the calling",
"# module is ever developed",
"if",
"module",
"is",
"None",
":",
"try",
":",
"module",
"=",
"_sys",
".",
"_getframe",
"(",
"2",
")",
".",
"f_globals",
"[",
"'__name__'",
"]",
"except",
"(",
"AttributeError",
",",
"ValueError",
")",
":",
"pass",
"if",
"module",
"is",
"None",
":",
"_make_class_unpicklable",
"(",
"enum_class",
")",
"else",
":",
"enum_class",
".",
"__module__",
"=",
"module",
"return",
"enum_class"
] |
Convenience method to create a new Enum class.
`names` can be:
* A string containing member names, separated either with spaces or
commas. Values are auto-numbered from 1.
* An iterable of member names. Values are auto-numbered from 1.
* An iterable of (member name, value) pairs.
* A mapping of member name -> value.
|
[
"Convenience",
"method",
"to",
"create",
"a",
"new",
"Enum",
"class",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/backports/enum/__init__.py#L422-L481
|
train
|
pypa/pipenv
|
pipenv/vendor/backports/enum/__init__.py
|
EnumMeta._get_mixins_
|
def _get_mixins_(bases):
"""Returns the type for creating enum members, and the first inherited
enum class.
bases: the tuple of bases that was given to __new__
"""
if not bases or Enum is None:
return object, Enum
# double check that we are not subclassing a class with existing
# enumeration members; while we're at it, see if any other data
# type has been mixed in so we can use the correct __new__
member_type = first_enum = None
for base in bases:
if (base is not Enum and
issubclass(base, Enum) and
base._member_names_):
raise TypeError("Cannot extend enumerations")
# base is now the last base in bases
if not issubclass(base, Enum):
raise TypeError("new enumerations must be created as "
"`ClassName([mixin_type,] enum_type)`")
# get correct mix-in type (either mix-in type of Enum subclass, or
# first base if last base is Enum)
if not issubclass(bases[0], Enum):
member_type = bases[0] # first data type
first_enum = bases[-1] # enum type
else:
for base in bases[0].__mro__:
# most common: (IntEnum, int, Enum, object)
# possible: (<Enum 'AutoIntEnum'>, <Enum 'IntEnum'>,
# <class 'int'>, <Enum 'Enum'>,
# <class 'object'>)
if issubclass(base, Enum):
if first_enum is None:
first_enum = base
else:
if member_type is None:
member_type = base
return member_type, first_enum
|
python
|
def _get_mixins_(bases):
"""Returns the type for creating enum members, and the first inherited
enum class.
bases: the tuple of bases that was given to __new__
"""
if not bases or Enum is None:
return object, Enum
# double check that we are not subclassing a class with existing
# enumeration members; while we're at it, see if any other data
# type has been mixed in so we can use the correct __new__
member_type = first_enum = None
for base in bases:
if (base is not Enum and
issubclass(base, Enum) and
base._member_names_):
raise TypeError("Cannot extend enumerations")
# base is now the last base in bases
if not issubclass(base, Enum):
raise TypeError("new enumerations must be created as "
"`ClassName([mixin_type,] enum_type)`")
# get correct mix-in type (either mix-in type of Enum subclass, or
# first base if last base is Enum)
if not issubclass(bases[0], Enum):
member_type = bases[0] # first data type
first_enum = bases[-1] # enum type
else:
for base in bases[0].__mro__:
# most common: (IntEnum, int, Enum, object)
# possible: (<Enum 'AutoIntEnum'>, <Enum 'IntEnum'>,
# <class 'int'>, <Enum 'Enum'>,
# <class 'object'>)
if issubclass(base, Enum):
if first_enum is None:
first_enum = base
else:
if member_type is None:
member_type = base
return member_type, first_enum
|
[
"def",
"_get_mixins_",
"(",
"bases",
")",
":",
"if",
"not",
"bases",
"or",
"Enum",
"is",
"None",
":",
"return",
"object",
",",
"Enum",
"# double check that we are not subclassing a class with existing",
"# enumeration members; while we're at it, see if any other data",
"# type has been mixed in so we can use the correct __new__",
"member_type",
"=",
"first_enum",
"=",
"None",
"for",
"base",
"in",
"bases",
":",
"if",
"(",
"base",
"is",
"not",
"Enum",
"and",
"issubclass",
"(",
"base",
",",
"Enum",
")",
"and",
"base",
".",
"_member_names_",
")",
":",
"raise",
"TypeError",
"(",
"\"Cannot extend enumerations\"",
")",
"# base is now the last base in bases",
"if",
"not",
"issubclass",
"(",
"base",
",",
"Enum",
")",
":",
"raise",
"TypeError",
"(",
"\"new enumerations must be created as \"",
"\"`ClassName([mixin_type,] enum_type)`\"",
")",
"# get correct mix-in type (either mix-in type of Enum subclass, or",
"# first base if last base is Enum)",
"if",
"not",
"issubclass",
"(",
"bases",
"[",
"0",
"]",
",",
"Enum",
")",
":",
"member_type",
"=",
"bases",
"[",
"0",
"]",
"# first data type",
"first_enum",
"=",
"bases",
"[",
"-",
"1",
"]",
"# enum type",
"else",
":",
"for",
"base",
"in",
"bases",
"[",
"0",
"]",
".",
"__mro__",
":",
"# most common: (IntEnum, int, Enum, object)",
"# possible: (<Enum 'AutoIntEnum'>, <Enum 'IntEnum'>,",
"# <class 'int'>, <Enum 'Enum'>,",
"# <class 'object'>)",
"if",
"issubclass",
"(",
"base",
",",
"Enum",
")",
":",
"if",
"first_enum",
"is",
"None",
":",
"first_enum",
"=",
"base",
"else",
":",
"if",
"member_type",
"is",
"None",
":",
"member_type",
"=",
"base",
"return",
"member_type",
",",
"first_enum"
] |
Returns the type for creating enum members, and the first inherited
enum class.
bases: the tuple of bases that was given to __new__
|
[
"Returns",
"the",
"type",
"for",
"creating",
"enum",
"members",
"and",
"the",
"first",
"inherited",
"enum",
"class",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/backports/enum/__init__.py#L484-L527
|
train
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.