[Python-checkins] python/nondist/sandbox/setuptools pkg_resources.py, 1.9, 1.10

pje@users.sourceforge.net pje at users.sourceforge.net
Sat May 21 23:43:00 CEST 2005


Update of /cvsroot/python/python/nondist/sandbox/setuptools
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32219

Modified Files:
	pkg_resources.py 
Log Message:
Add basic "Requirement" class that can tell whether a distribution or
version meets its version requirements.


Index: pkg_resources.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setuptools/pkg_resources.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- pkg_resources.py	3 Apr 2005 18:52:21 -0000	1.9
+++ pkg_resources.py	21 May 2005 21:42:57 -0000	1.10
@@ -19,7 +19,7 @@
     'resource_stream', 'resource_filename', 'set_extraction_path',
     'cleanup_resources', 'parse_requirements', 'parse_version',
     'compatible_platforms', 'get_platform',
-    'Distribution', # 'glob_resources'
+    'Distribution', 'Requirement', # 'glob_resources'
 ]
 
 import sys, os, zipimport, time, re
@@ -740,7 +740,7 @@
     """Yield ``Requirement`` objects for each specification in `strs`
 
     `strs` must be an instance of ``basestring``, or a (possibly-nested)
-    sequence thereof.
+    iterable thereof.
     """
     # create a steppable iterator, so we can handle \-continuations
     lines = iter(yield_lines(strs))
@@ -772,10 +772,61 @@
             elif not LINE_END(line,p):
                 raise ValueError("Expected ',' or EOL in",line,"at",line[p:])
 
-        yield distname.replace('_','-'), specs
+        yield Requirement(distname.replace('_','-'), specs)
+
+
+
+
+class Requirement:
+
+    def __init__(self, distname, specs=()):
+        self.distname = distname
+        self.key = distname.lower()
+        index = [(parse_version(v),state_machine[op],op,v) for op,v in specs]
+        index.sort()
+        self.specs = [(op,ver) for parsed,trans,op,ver in index]
+        self.index = index
 
+    def __str__(self):
+        return self.distname + ','.join([''.join(s) for s in self.specs])
 
+    def __repr__(self):
+        return "Requirement(%r, %r)" % (self.distname, self.specs)
 
+    def __eq__(self,other):
+        return isinstance(other,Requirement) \
+            and self.key==other.key and self.specs==other.specs
+
+    def __contains__(self,item):
+        if isinstance(item,Distribution):
+            if item.key <> self.key:
+                return False
+            item = item.parsed_version
+        elif isinstance(item,basestring):
+            item = parse_version(item)
+        last = True
+        for parsed,trans,op,ver in self.index:
+            action = trans[cmp(item,parsed)]
+            if action=='F':
+                return False
+            elif action=='T':
+                return True
+            elif action=='+':
+                last = True
+            elif action=='-':
+                last = False
+        return last
+
+
+state_machine = {
+    #       =><
+    '<' :  '--T',
+    '<=':  'T-T',
+    '>' :  'F+F',
+    '>=':  'T+F',
+    '==':  'T..',
+    '!=':  'F..',
+}
 
 def _get_mro(cls):
     """Get an mro for a type or classic class"""
@@ -808,13 +859,3 @@
 _initialize(globals())
 
 
-
-
-
-
-
-
-
-
-
-



More information about the Python-checkins mailing list