downloader.py :  » IDE » PIDA » pida-0.6beta3 » pida » services » plugins » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » IDE » PIDA 
PIDA » pida 0.6beta3 » pida » services » plugins » downloader.py
# license gpl2 or later

import re
import urllib2
from xml.etree import ElementTree
from  import metadata
from urllib import basejoin
from collections import defaultdict
import logging
log = logging.getLogger('pida.services.plugins.downloader')
#XXX: ugly hack to avout dealing with html trees
link_re = re.compile(r'''href=['"](.*?)["'].*?>(.*?)</''', re.M)
plugin_name_re = re.compile("""
    .* # prefix crud
    pida.plugins.(?P<name>\w+)-(?P<version>[\w.]+)
    .(?P<ext>zip|meta|egg|tar.bz|tar.gz)
    (?P<crud>.*)
""", re.VERBOSE)


def find_latest_metadata(url):
    for name, version, data in find_latest(url):
        if 'meta' not in data:
            log.error('%s-%s doesnt supply metadata', name, version)
            continue
        if 'tar.gz' not in data:
            log.error('%s-%s doesnt supply a packed plugin', name, version)
            continue
        plugin = name.split('.')[-1]
        fd = urllib2.urlopen(data['meta'])
        meta = metadata.from_string(fd.read(), None, plugin)
        meta.url = data['tar.gz']
        yield meta

def find_latest(url):
    for name, versions in find_plugin_versions(url):
        #XXX: better version key
        if not versions:
            continue
        latest = max(versions,key=lambda x:x.split('.'))
        yield name, latest, versions[latest]

def find_plugin_versions(url):
    for href, name in find_plugins(url):
        yield name, find_versions(basejoin(url, href))

def find_versions(url):
    versions = {}
    for href, name in find_urls(url):
        match = plugin_name_re.match(href)
        if match:
            version = match.group('version')
            ext = match.group('ext')
            if version not in versions:
                versions[version] = {}
            #XXX: might break for absolute url's
            versions[version][ext] = basejoin(url, href)
    return versions


def find_plugins(url):
    items = find_urls(url)
    return [
        (href, name)
        for href, name in items
        if 'pida' in href.lower()
     ]

def find_urls(url,):
    try:
        fd = urllib2.urlopen(url)
    except urllib2.URLError, e:
        log.warning(_("Can't contact plugin website"))
        return ()
    data = fd.read()
    return link_re.findall(data)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.