Skip to content

Commit

Permalink
remove xmlrpclib completely, re + urlopen it is
Browse files Browse the repository at this point in the history
  • Loading branch information
wuub committed Aug 5, 2013
1 parent fbb61cf commit 354fa0a
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
import sublime_plugin

try:
from xmlrpclib import ServerProxy
import ConfigParser as configparser
from urllib2 import Request, urlopen
unicode_type = unicode
PY2 = True
except ImportError:
from xmlrpc.client import ServerProxy
import configparser
from urllib.request import Request, urlopen
unicode_type = str
Expand All @@ -29,7 +27,6 @@
SETTINGS_PIP_INDEX = None

def plugin_loaded():
"""Monkeypatch ServerProxy for testing on plugin load"""
global CACHE, SETTINGS, SETTINGS_PIP_INDEX
CACHE = SimpleCache()

Expand All @@ -42,10 +39,6 @@ def update_global():
SETTINGS_PIP_INDEX = SETTINGS.get("pip_index", None)
SETTINGS.add_on_change("pip_index", update_global)

if SETTINGS.get("debug", False):
global ServerProxy
ServerProxy = FakePackagesIndex


def get_pip_index():
"""return url of pypi xmlrpc endpoint"""
Expand Down Expand Up @@ -91,22 +84,6 @@ def clear(self):
self._dict.clear()


class FakePackagesIndex(object):
def __init__(self, url):
self._url = url

def list_packages(self):
time.sleep(3.0)
return ["Flask", "Flask-SqlAlchemy", "Flask-WTF"]

def package_releases(self, package_name, show_hidden=False):
time.sleep(1.0)
if show_hidden:
return ["1.1.1", "1.1.2"]
else:
return ["1.1.1"]


def status_message(msg):
"""Workaround for osx run_on_main_thread problem"""
sublime.set_timeout(functools.partial(sublime.status_message, msg), 0)
Expand All @@ -115,7 +92,13 @@ def status_message(msg):
def _fetch_packages():
"""Does the actual package list fetch, returns a list of unicode names"""
status_message("requirements.txt: listing packages...")
packages = ServerProxy(get_pip_index()).list_packages()
query = b'''<?xml version='1.0'?>\n<methodCall>\n<methodName>list_packages</methodName>\n<params></params>\n</methodCall>\n'''
req = Request(get_pip_index(), data=query, headers={"Content-Type": "text/xml"})
result = urlopen(req).read().decode("utf-8")
if "<fault>" in result:
packages = []
else:
packages = re.findall("<string>(.+?)</string>", result)
status_message("requirements.txt: got {count}".format(count=len(packages)))
if not isinstance(packages[0], unicode_type):
packages = [pkg.decode("utf-8") for pkg in packages]
Expand Down

0 comments on commit 354fa0a

Please sign in to comment.