Source

pypydoc / pypy / module / micronumpy / tool / numready / main.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
This should be run under PyPy.
"""

import os
import platform
import subprocess
import tempfile
import webbrowser
from collections import OrderedDict

import jinja2

from .kinds import KINDS


class SearchableSet(object):
    def __init__(self, items=()):
        self._items = {}
        for item in items:
            self.add(item)

    def __iter__(self):
        return iter(self._items)

    def __contains__(self, other):
        return other in self._items

    def __getitem__(self, idx):
        return self._items[idx]

    def add(self, item):
        self._items[item] = item

    def __len__(self):
        return len(self._items)

class Item(object):
    def __init__(self, name, kind, subitems=None):
        self.name = name
        self.kind = kind
        self.subitems = subitems

    def __hash__(self):
        return hash(self.name)

    def __eq__(self, other):
        if isinstance(other, str):
            return self.name == other
        return self.name == other.name


class ItemStatus(object):
    def __init__(self, name, pypy_exists):
        self.name = name
        self.cls = 'exists' if pypy_exists else ''
        self.symbol = u"✔" if pypy_exists else u'✖'

    def __lt__(self, other):
        return self.name < other.name

def find_numpy_items(python, modname="numpy", attr=None):
    args = [
        python, os.path.join(os.path.dirname(__file__), "search.py"), modname
    ]
    if attr is not None:
        args.append(attr)
    lines = subprocess.check_output(args).splitlines()
    items = SearchableSet()
    for line in lines:
        kind, name = line.split(" : ", 1)
        subitems = None
        if kind == KINDS["TYPE"]:
            if name in ['ndarray', 'dtype']:
                subitems = find_numpy_items(python, modname, name)
        items.add(Item(name, kind, subitems))
    return items

def split(lst):
    SPLIT = 5
    lgt = len(lst) // SPLIT + 1
    l = [[] for i in range(lgt)]
    for i in range(lgt):
        for k in range(SPLIT):
            if k * lgt + i < len(lst):
                l[i].append(lst[k * lgt + i])
    return l

def main(argv):
    cpy_items = find_numpy_items("/usr/bin/python")
    pypy_items = find_numpy_items(argv[1], "numpypy")
    all_items = []

    msg = '%d/%d names, %d/%d ndarray attributes, %d/%d dtype attributes' % (
        len(pypy_items), len(cpy_items), len(pypy_items['ndarray'].subitems),
        len(cpy_items['ndarray'].subitems), len(pypy_items['dtype'].subitems),
        len(cpy_items['dtype'].subitems))
    for item in cpy_items:
        pypy_exists = item in pypy_items
        if item.subitems:
            for sub in item.subitems:
                all_items.append(
                    ItemStatus(item.name + "." + sub.name, pypy_exists=pypy_exists and pypy_items[item].subitems and sub in pypy_items[item].subitems)
                )
        all_items.append(ItemStatus(item.name, pypy_exists=item in pypy_items))
    env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(os.path.dirname(__file__))
    )
    html = env.get_template("page.html").render(all_items=split(sorted(all_items)), msg=msg)
    if len(argv) > 2:
        with open(argv[2], 'w') as f:
            f.write(html.encode("utf-8"))
    else:
        with tempfile.NamedTemporaryFile(delete=False) as f:
            f.write(html.encode("utf-8"))
        print "Saved in: %s" % f.name