[Python-checkins] python/nondist/sandbox/setuptools pkg_resources.py, 1.25, 1.26

pje@users.sourceforge.net pje at users.sourceforge.net
Sun Jun 5 20:59:39 CEST 2005


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

Modified Files:
	pkg_resources.py 
Log Message:
Add "safe_name" and "safe_version" functions to allow sanitizing of
distribution names and versions in arbitrary packages that might be built
using EasyInstall.


Index: pkg_resources.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setuptools/pkg_resources.py,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -d -r1.25 -r1.26
--- pkg_resources.py	5 Jun 2005 01:20:09 -0000	1.25
+++ pkg_resources.py	5 Jun 2005 18:59:36 -0000	1.26
@@ -22,11 +22,23 @@
     'InvalidOption', 'Distribution', 'Requirement', 'yield_lines',
     'get_importer', 'find_distributions', 'find_on_path', 'register_finder',
     'split_sections', 'declare_namespace', 'register_namespace_handler',
+    'safe_name', 'safe_version'
 ]
 
 import sys, os, zipimport, time, re, imp
 from sets import ImmutableSet
 
+
+
+
+
+
+
+
+
+
+
+
 class ResolutionError(Exception):
     """Abstract base for dependency resolution errors"""
 
@@ -57,6 +69,17 @@
     loader = getattr(module, '__loader__', None)
     return _find_adapter(_provider_factories, loader)(module)
 
+
+
+
+
+
+
+
+
+
+
+
 def get_platform():
     """Return this platform's string for platform-specific distributions
 
@@ -80,6 +103,24 @@
     return False
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
 class IMetadataProvider:
 
     def has_metadata(name):
@@ -424,22 +465,22 @@
         dist.install_on(sys.path)
 
 
+def safe_name(name):
+    """Convert an arbitrary string to a standard distribution name
 
+    Any runs of non-alphanumeric characters are replaced with a single '-'.
+    """
+    return re.sub('[^A-Za-z0-9]+', '-', name)
 
 
+def safe_version(version):
+    """Convert an arbitrary string to a standard version string
 
-
-
-
-
-
-
-
-
-
-
-
-
+    Spaces become dots, and all other non-alphanumeric characters become
+    dashes, with runs of multiple dashes condensed to a single dash.
+    """
+    version = version.replace(' ','.')
+    return re.sub('[^A-Za-z0-9.]+', '-', version)
 
 
 



More information about the Python-checkins mailing list