[docs]classPackageRequirement:""" Package requirement object parse given requirement item to get relevant informations. A package object information is fullfilled in two cycles, firstly the requirement parser that is done in PackageRequirement itself then with the data collection and computation from the Analyzer. When parsing has failed, all attributes depending from collection and computation will be None. Arguments: source (string): A requirement line to parse. Keyword Arguments: environment (dict): Optionnal dictionnary of environment variables to use with possible specifier marker resolution. Attributes: source (string): Given requirement source. The source string is expected to be stripped from leading whitespaces else it could causes unexpected parsing false positive. environment (dict): Given environment dictionnary status (string): Computed requirement status from parsing process. Status can be: * ``parsed``: requirement has been properly parsed as supported syntax; * ``analyzed``: requirement has been parsed and has been processed by the Analyzer; * ``unsupported-argument``: unsupported Pip argument, aborted parsing; * ``unsupported-localpath``: unsupported local path to package, aborted parsing; * ``unsupported-url``: unsupported package url, aborted parsing; * ``invalid``: invalid requirement syntax (as from PEP425 and PEP440), aborted computation from parsed source; * ``marker-reject``: Requirement did have marker that does not match required environment variables when given, aborted computation from parsed source; Commonly to get all valid requirements that have been properly analyzed, you will just seek for items with status ``analyzed``. ``parsed`` status should never occurs when analyzer has been involved. marker (packaging.markers.Marker): Marker object parsed from source. name (string): Package name parsed from source. parsed (packaging.requirements.Requirement): Requirement object parsed from source. pypi_url (string): Possible package URL collected from API after the parsed source. repository_url (string): Possible repository URL collected from API after the parsed source. highest_published (datetime.datetime): Date of the highest release version available on Pypi. highest_version (packaging.version.Version): Collected highest release version available on Pypi. lateness (list): List of tuples of version string and datetime for each released version that are higher than the resolved version. This means all available package versions that could upgraded to. resolved_version (string): Resolved release version from specifiers in parsed source. Specifiers are used against collected release version available on Pypi. This value will be null if no specifier can be found. Internally a null value is assumed the requirement can use the latest version and so there is no lateness to compute. resolved_published (datetime.datetime): Date of the resolved release. specifier (packaging.requirements.SpecifierSet): Possible version specifiers parsed from source. url (string): Possible mirror URL parsed from source. extras (set): Possible parsed set of extras environ names from source. parsing_error (object): The exception object raise from ``packaging.Requirement`` when there was a parsing error. """STATUS_LABELS={"parsed":"Parsed requirement syntax","analyzed":"Analyzed package informations","unsupported-argument":"Unsupported Pip argument","unsupported-localpath":"Local package is not supported","unsupported-url":"Direct package URL is not supported","invalid":"Invalid syntax","marker-reject":"Rejected by marker evaluation against given environment","unknown":"Unexpected failure",}VALID_STATUSES=("parsed","analyzed")PUBLISHED_ATTRIBUTES=["extras","highest_published","highest_version","lateness","marker","name","parsed","pypi_url","repository_url","source","specifier","status","url","resolved_version","resolved_published","parsing_error",]def__init__(self,source,environment=None):self.source=sourceself.environment=environment# All details set to null on defaultself.extras=Noneself.highest_published=Noneself.highest_version=Noneself.lateness=Noneself.marker=Noneself.name=Noneself.parsed=Noneself.pypi_url=Noneself.repository_url=Noneself.specifier=Noneself.status=Noneself.url=Noneself.resolved_version=Noneself.resolved_published=Noneself.parsing_error=None# Check if source syntax is supportedifself.source.startswith("-"):self.status="unsupported-argument"elifself.source.startswith((".","/")):self.status="unsupported-localpath"elifself.source.startswith(("http://","https://")):self.status="unsupported-url"else:try:self.parsed=Requirement(self.source)exceptInvalidRequirementase:self.status="invalid"self.parsing_error=eelse:# Initialize basic details from parsed sourceself.name=self.parsed.nameself.url=self.parsed.urlself.extras=self.parsed.extrasself.specifier=self.parsed.specifierself.marker=self.parsed.marker# If environment and marker are not empty, evaluate marker against# given environment variables to possibly flag source as rejected# from markerif(self.environmentandself.markerandself.marker.evaluate(self.environment)isFalse):self.status="marker-reject"else:self.status="parsed"def__str__(self):return"[{status}] {source}".format(status=self.status,source=self.source)def__repr__(self):return"<PackageRequirement:{status}> {source}".format(status=self.status,source=self.source)@propertydefis_valid(self):""" Express if package has been properly parsed and result to a proper package and not unsupported or invalid parsed rule. """returnself.statusinself.VALID_STATUSES
[docs]defdata(self):""" Return public attributes into a dictionnary. Returns: dict: The public attributes to publish. """return{k:getattr(self,k)forkinself.PUBLISHED_ATTRIBUTES}