Changeset 11688:3abfd8a227d0 in orange


Ignore:
Timestamp:
09/06/13 19:46:31 (7 months ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
Children:
11689:b3d7eae5e2c4, 11709:d5173ab3f7b1
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Orange/OrangeCanvas/help/manager.py

    r11320 r11688  
    77import itertools 
    88import logging 
     9import email 
     10 
     11from distutils.version import StrictVersion 
    912 
    1013from operator import itemgetter 
     
    179182 
    180183 
    181 def parse_pkg_info(contents): 
    182     lines = contents.expandtabs().splitlines() 
    183     parsed = {} 
    184     current_block = None 
    185     for line in lines: 
    186         if line.startswith(" "): 
    187             parsed[current_block].append(line) 
    188         elif line.strip(): 
    189             current_block, block_contents = line.split(": ", 1) 
    190             if current_block == "Classifier": 
    191                 if current_block not in parsed: 
    192                     parsed[current_block] = [trim(block_contents)] 
    193                 else: 
    194                     parsed[current_block].append(trim(block_contents)) 
    195             else: 
    196                 parsed[current_block] = [block_contents] 
    197  
    198     for key, val in parsed.items(): 
    199         if key != "Classifier": 
    200             parsed[key] = trim("\n".join(val)) 
    201  
    202     return parsed 
    203  
    204  
    205 def get_pkg_info_entry(dist, name): 
     184# Fields allowing multiple use (from PEP-0345) 
     185MULTIPLE_KEYS = ["Platform", "Supported-Platform", "Classifier", 
     186                 "Requires-Dist", "Provides-Dist", "Obsoletes-Dist", 
     187                 "Project-URL"] 
     188 
     189 
     190def parse_meta(contents): 
     191    message = email.message_from_string(contents) 
     192    meta = {} 
     193    for key in set(message.keys()): 
     194        if key in MULTIPLE_KEYS: 
     195            meta[key] = message.get_all(key) 
     196        else: 
     197            meta[key] = message.get(key) 
     198 
     199    version = StrictVersion(meta["Metadata-Version"]) 
     200 
     201    if version >= StrictVersion("1.3") and "Description" not in meta: 
     202        desc = message.get_payload() 
     203        if desc: 
     204            meta["Description"] = desc 
     205    return meta 
     206 
     207 
     208def get_meta_entry(dist, name): 
    206209    """ 
    207210    Get the contents of the named entry from the distributions PKG-INFO file 
    208211    """ 
    209     pkg_info = parse_pkg_info(dist.get_metadata("PKG-INFO")) 
    210     return pkg_info[name] 
     212    meta = get_dist_meta(dist) 
     213    return meta.get(name) 
    211214 
    212215 
     
    215218    Return the 'url' of the distribution (as passed to setup function) 
    216219    """ 
    217     return get_pkg_info_entry(dist, "Home-page") 
     220    return get_meta_entry(dist, "Home-page") 
     221 
     222 
     223def get_dist_meta(dist): 
     224    if dist.has_metadata("PKG-INFO"): 
     225        # egg-info 
     226        contents = dist.get_metadata("PKG-INFO") 
     227    elif dist.has_metadata("METADATA"): 
     228        contents = dist.get_metadata("METADATA") 
     229    return parse_meta(contents) 
    218230 
    219231 
Note: See TracChangeset for help on using the changeset viewer.