Aleš Erjavec avatar Aleš Erjavec committed 3abfd8a

Added support for reading distribution meta info from .dist-info directory.

Comments (0)

Files changed (1)

Orange/OrangeCanvas/help/manager.py

 import string
 import itertools
 import logging
+import email
+
+from distutils.version import StrictVersion
 
 from operator import itemgetter
 
     return  "\n".join(trim_leading_lines(trim_trailing_lines(lines)))
 
 
-def parse_pkg_info(contents):
-    lines = contents.expandtabs().splitlines()
-    parsed = {}
-    current_block = None
-    for line in lines:
-        if line.startswith(" "):
-            parsed[current_block].append(line)
-        elif line.strip():
-            current_block, block_contents = line.split(": ", 1)
-            if current_block == "Classifier":
-                if current_block not in parsed:
-                    parsed[current_block] = [trim(block_contents)]
-                else:
-                    parsed[current_block].append(trim(block_contents))
-            else:
-                parsed[current_block] = [block_contents]
+# Fields allowing multiple use (from PEP-0345)
+MULTIPLE_KEYS = ["Platform", "Supported-Platform", "Classifier",
+                 "Requires-Dist", "Provides-Dist", "Obsoletes-Dist",
+                 "Project-URL"]
 
-    for key, val in parsed.items():
-        if key != "Classifier":
-            parsed[key] = trim("\n".join(val))
 
-    return parsed
+def parse_meta(contents):
+    message = email.message_from_string(contents)
+    meta = {}
+    for key in set(message.keys()):
+        if key in MULTIPLE_KEYS:
+            meta[key] = message.get_all(key)
+        else:
+            meta[key] = message.get(key)
 
+    version = StrictVersion(meta["Metadata-Version"])
 
-def get_pkg_info_entry(dist, name):
+    if version >= StrictVersion("1.3") and "Description" not in meta:
+        desc = message.get_payload()
+        if desc:
+            meta["Description"] = desc
+    return meta
+
+
+def get_meta_entry(dist, name):
     """
     Get the contents of the named entry from the distributions PKG-INFO file
     """
-    pkg_info = parse_pkg_info(dist.get_metadata("PKG-INFO"))
-    return pkg_info[name]
+    meta = get_dist_meta(dist)
+    return meta.get(name)
 
 
 def get_dist_url(dist):
     """
     Return the 'url' of the distribution (as passed to setup function)
     """
-    return get_pkg_info_entry(dist, "Home-page")
+    return get_meta_entry(dist, "Home-page")
+
+
+def get_dist_meta(dist):
+    if dist.has_metadata("PKG-INFO"):
+        # egg-info
+        contents = dist.get_metadata("PKG-INFO")
+    elif dist.has_metadata("METADATA"):
+        contents = dist.get_metadata("METADATA")
+    return parse_meta(contents)
 
 
 def create_intersphinx_provider(entry_point):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.