Commits

Vinay Sajip  committed 8e32f87

Scheme changes.

  • Participants
  • Parent commits 90c9f5b

Comments (0)

Files changed (2)

File distlib/locators.py

     # Leave out binaries from downloadables, for now.
     downloadable_extensions = source_extensions
 
-    def __init__(self):
+    def __init__(self, scheme='default'):
         self._cache = {}
+        self.scheme = scheme
+
+    def _get_scheme(self):
+        return self._scheme
+
+    def _set_scheme(self, value):
+        self._scheme = value
+
+    scheme = property(_get_scheme, _set_scheme)
 
     def _get_project(self, name):
         """
             dist = result[version]
             md = dist.metadata
         else:
-            md = Metadata()
+            md = Metadata(scheme=self.scheme)
             md['Name'] = name
             md['Version'] = version
             dist = Distribution(md)
         dist.locator = self
         result[version] = dist
 
-    def locate(self, requirement, scheme='default'):
+    def locate(self, requirement):
         """
         Find the most recent distribution which matches the given
         matcher, using the provided scheme.
         """
         result = None
-        if isinstance(scheme, string_types):
-            scheme = get_scheme(scheme)
+        scheme = get_scheme(self.scheme)
         matcher = scheme.matcher(requirement)
         versions = self.get_project(matcher.name)
         if versions:
 
 
 class PyPIRPCLocator(Locator):
-    def __init__(self, url):
-        super(PyPIRPCLocator, self).__init__()
+    def __init__(self, url, **kwargs):
+        super(PyPIRPCLocator, self).__init__(**kwargs)
         self.base_url = url
         self.client = xmlrpclib.ServerProxy(url)
 
         for v in versions:
             urls = self.client.release_urls(name, v)
             data = self.client.release_data(name, v)
-            metadata = Metadata()
+            metadata = Metadata(scheme=self.scheme)
             metadata.update(data)
             dist = Distribution(metadata)
             if urls:
         return result
 
 class PyPIJSONLocator(Locator):
-    def __init__(self, url):
-        super(PyPIJSONLocator, self).__init__()
+    def __init__(self, url, **kwargs):
+        super(PyPIJSONLocator, self).__init__(**kwargs)
         self.base_url = ensure_slash(url)
 
     def get_distribution_names(self):
             resp = urlopen(url)
             data = resp.read().decode() # for now
             d = json.loads(data)
-            md = Metadata()
+            md = Metadata(scheme=self.scheme)
             md.update(d['info'])
             dist = Distribution(md)
             urls = d['urls']
         'none': lambda b: b,
     }
 
-    def __init__(self, url, timeout=None, num_workers=10):
-        super(SimpleScrapingLocator, self).__init__()
+    def __init__(self, url, timeout=None, num_workers=10, **kwargs):
+        super(SimpleScrapingLocator, self).__init__(**kwargs)
         self.base_url = ensure_slash(url)
         self.timeout = timeout
         self._page_cache = {}
         return result
 
 class DirectoryLocator(Locator):
-    def __init__(self, path):
-        super(DirectoryLocator, self).__init__()
+    def __init__(self, path, **kwargs):
+        super(DirectoryLocator, self).__init__(**kwargs)
         path = os.path.abspath(path)
         if not os.path.isdir(path):
             raise ValueError('Not a directory: %r' % path)
             for info in data.get('files', []):
                 if info['ptype'] != 'sdist' or info['pyversion'] != 'source':
                     continue
-                md = Metadata()
+                md = Metadata(scheme=self.scheme)
                 md['Name'] = data['name']
                 md['Version'] = version = info['version']
                 md['Download-URL'] = info['url']
     Chain and/or merge a list of locators.
     """
     def __init__(self, *locators, **kwargs):
-        super(AggregatingLocator, self).__init__()
+        self.merge = kwargs.pop('merge', False)
         self.locators = locators
-        self.merge = kwargs.get('merge', False)
+        super(AggregatingLocator, self).__init__(**kwargs)
+
+    def _set_scheme(self, value):
+        self._scheme = value
+        for locator in self.locators:
+            locator.scheme = value
+
+    scheme = property(Locator.scheme.fget, _set_scheme)
 
     def _get_project(self, name):
         result = {}
                     SimpleScrapingLocator('http://pypi.python.org/simple/',
                                           timeout=3.0))
 
-def locate(requirement, scheme='default', locator=None):
-    """
-    Locate a downloadable distribution, given a requirement (project name and
-    version constraints, if any).
-    """
-    #logger.debug('locate %r starting', requirement)
-    result = None
-    if locator is None:
-        locator = default_locator
-    return locator.locate(requirement, scheme)
+locate = default_locator.locate

File distlib/metadata.py

     # also document the mapping API and UNKNOWN default key
 
     def __init__(self, path=None, platform_dependent=False,
-                 execution_context=None, fileobj=None, mapping=None):
+                 execution_context=None, fileobj=None, mapping=None,
+                 scheme='default'):
         self._fields = {}
         self.requires_files = []
         self.docutils_support = _HAS_DOCUTILS
         self.platform_dependent = platform_dependent
         self.execution_context = execution_context
-        self.scheme = get_scheme('default')
+        self.scheme = scheme
         self.dependencies = {}
         if [path, fileobj, mapping].count(None) < 2:
             raise TypeError('path, fileobj and mapping are exclusive')
         if logger.isEnabledFor(logging.WARNING):
             project_name = self['Name']
 
+            scheme = get_scheme(self.scheme)
             if name in _PREDICATE_FIELDS and value is not None:
                 for v in value:
                     # check that the values are valid
-                    if not self.scheme.is_valid_matcher(v.split(';')[0]):
+                    if not scheme.is_valid_matcher(v.split(';')[0]):
                         logger.warning(
                             '%r: %r is not valid (field %r)',
                             project_name, v, name)
             # FIXME this rejects UNKNOWN, is that right?
             elif name in _VERSIONS_FIELDS and value is not None:
-                if not self.scheme.is_valid_constraint_list(value):
+                if not scheme.is_valid_constraint_list(value):
                     logger.warning('%r: %r is not a valid version (field %r)',
                                    project_name, value, name)
             elif name in _VERSION_FIELDS and value is not None:
-                if not self.scheme.is_valid_version(value):
+                if not scheme.is_valid_version(value):
                     logger.warning('%r: %r is not a valid version (field %r)',
                                    project_name, value, name)
 
         if self['Metadata-Version'] != '1.2':
             return missing, warnings
 
+        scheme = get_scheme(self.scheme)
+
         def are_valid_constraints(value):
             for v in value:
-                if not self.scheme.is_valid_matcher(v.split(';')[0]):
+                if not scheme.is_valid_matcher(v.split(';')[0]):
                     return False
             return True
 
         for fields, controller in ((_PREDICATE_FIELDS, are_valid_constraints),
                                    (_VERSIONS_FIELDS,
-                                    self.scheme.is_valid_constraint_list),
+                                    scheme.is_valid_constraint_list),
                                    (_VERSION_FIELDS,
-                                    self.scheme.is_valid_version)):
+                                    scheme.is_valid_version)):
             for field in fields:
                 value = self.get(field, None)
                 if value is not None and not controller(value):