<s> import collections <EOL> import re <EOL> import six <EOL> from . compat import OrderedDict <EOL> from . _package_utils import parse_package_full_name <EOL> from . requirement import Requirement <EOL> from . requirement_parser import RawRequirementParser <EOL> from . utils import Callable <EOL> from . version import SemanticVersion , Version <EOL> from . bundled . traitlets import HasTraits , Instance , List , Long , Unicode <EOL> R = Requirement . from_string <EOL> _DEPENDENCY_TYPES = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> _SECTION_RE = re . compile ( """<STR_LIT>""" % { "<STR_LIT>" : "<STR_LIT:|>" . join ( _DEPENDENCY_TYPES ) } , re . VERBOSE ) <EOL> def _parse_name_version_part ( name_version , version_factory ) : <EOL> name , version_string = parse_package_full_name ( name_version ) <EOL> version = version_factory ( version_string ) <EOL> return name , version <EOL> def _parse_requirements_string ( parser , s , version_factory ) : <EOL> m = _SECTION_RE . search ( s ) <EOL> if m is None : <EOL> raise ValueError ( "<STR_LIT>" % s ) <EOL> else : <EOL> requirements_type , requirements_string = m . groups ( ) <EOL> requirements = OrderedDict ( ) <EOL> for requirement_string in requirements_string . split ( "<STR_LIT:U+002C>" ) : <EOL> for distribution_name , specs in parser . parse ( requirement_string ) . items ( ) : <EOL> if not distribution_name in requirements : <EOL> requirements [ distribution_name ] = [ ] <EOL> requirements [ distribution_name ] . extend ( specs ) <EOL> return requirements_type , [ Requirement ( name , reqs , version_factory ) <EOL> for name , reqs in six . iteritems ( requirements ) ] <EOL> def parse_package_string ( package_string , version_factory ) : <EOL> parser = RawRequirementParser ( ) <EOL> parts = package_string . split ( "<STR_LIT:;>" ) <EOL> name , version = _parse_name_version_part ( parts [ <NUM_LIT:0> ] , version_factory ) <EOL> requirements_lists = collections . defaultdict ( list ) <EOL> for part in parts [ <NUM_LIT:1> : ] : <EOL> requirement_type , requirements = _parse_requirements_string ( parser , part , version_factory ) <EOL> requirements_lists [ requirement_type ] . extend ( requirements ) <EOL> provides = requirements_lists [ "<STR_LIT>" ] <EOL> depends = requirements_lists [ "<STR_LIT>" ] <EOL> conflicts = requirements_lists [ "<STR_LIT>" ] <EOL> replaces = requirements_lists [ "<STR_LIT>" ] <EOL> suggests = requirements_lists [ "<STR_LIT>" ] <EOL> return name , version , provides , depends , conflicts , replaces , suggests <EOL> class PackageInfo ( HasTraits ) : <EOL> """<STR_LIT>""" <EOL> name = Unicode ( ) <EOL> version = Instance ( Version ) <EOL> version_factory = Callable ( ) <EOL> dependencies = List ( Instance ( Requirement ) ) <EOL> provides = List ( Instance ( Requirement ) ) <EOL> conflicts = List ( Instance ( Requirement ) ) <EOL> replaces = List ( Instance ( Requirement ) ) <EOL> suggests = List ( Instance ( Requirement ) ) <EOL> id = Long ( - <NUM_LIT:1> ) <EOL> _repository = Instance ( "<STR_LIT>" ) <EOL> @ classmethod <EOL> def from_string ( cls , package_string , version_factory = SemanticVersion . from_string ) : <EOL> """<STR_LIT>""" <EOL> name , version , provides , dependencies , conflicts , replaces , suggests = parse_package_string ( package_string , version_factory ) <EOL> return cls ( name = name , version = version , version_factory = version_factory , <EOL> provides = list ( provides ) , <EOL> dependencies = list ( dependencies ) , conflicts = list ( conflicts ) , <EOL> replaces = list ( replaces ) , suggests = list ( suggests ) ) <EOL> def __init__ ( self , name , version , <EOL> version_factory = SemanticVersion . from_string , dependencies = None , <EOL> provides = None , conflicts = None , replaces = None , suggests = None , ** kw ) : <EOL> if dependencies is None : <EOL> dependencies = [ ] <EOL> if provides is None : <EOL> provides = [ ] <EOL> if conflicts is None : <EOL> conflicts = [ ] <EOL> if replaces is None : <EOL> replaces = [ ] <EOL> if suggests is None : <EOL> suggests = [ ] <EOL> super ( PackageInfo , self ) . __init__ ( name = name , version = version , <EOL> version_factory = version_factory , <EOL> dependencies = dependencies , <EOL> provides = provides , <EOL> conflicts = conflicts , <EOL> replaces = replaces , <EOL> suggests = suggests , <EOL> ** kw ) <EOL> @ property <EOL> def unique_name ( self ) : <EOL> return self . name + "<STR_LIT:->" + str ( self . version ) <EOL> @ property <EOL> def package_string ( self ) : <EOL> strings = [ "<STR_LIT>" % ( self . name , self . version ) ] <EOL> if self . dependencies : <EOL> strings . append ( "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( str ( s ) for s in self . dependencies ) ) <EOL> if self . provides : <EOL> strings . append ( "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( str ( s ) for s in self . provides ) ) <EOL> if self . conflicts : <EOL> strings . append ( "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( str ( s ) for s in self . conflicts ) ) <EOL> if self . replaces : <EOL> strings . append ( "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( str ( s ) for s in self . replaces ) ) <EOL> if self . suggests : <EOL> strings . append ( "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( str ( s ) for s in self . suggests ) ) <EOL> return "<STR_LIT>" . join ( strings ) <EOL> @ property <EOL> def repository ( self ) : <EOL> return self . _repository <EOL> @ repository . setter <EOL> def repository ( self , repository ) : <EOL> if self . _repository is not None : <EOL> raise ValueError ( "<STR_LIT>" % format ( self . _repository ) ) <EOL> self . _repository = repository <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . package_string <EOL> def __str__ ( self ) : <EOL> return self . unique_name <EOL> def __eq__ ( self , other ) : <EOL> return self . name == other . name and self . version == other . version and self . provides == other . provides and self . dependencies == other . dependencies and self . id == other . id <EOL> def __hash__ ( self ) : <EOL> return hash ( "<STR_LIT>" % ( str ( self ) , self . id ) ) </s>
