Ejemplo n.º 1
0
 def test_lazyimport(self):
     """
     Test that the viz module is lazily imported.
     """
     
     self.assertEqual(type(gvas.viz.sns), type(lazyModule('seaborn')))
     self.assertEqual(type(gvas.viz.plt), type(lazyModule('matplotlib.pyplot')))
     self.assertEqual(type(gvas.viz.np), type(lazyModule('numpy')))
     self.assertEqual(type(gvas.viz.pd), type(lazyModule('pandas')))
Ejemplo n.º 2
0
 def __init__(self, app):
     self.variables = dict(
         app=app,
         chandler=chandler,
         peak=peak,
         wx=imports.lazyModule('wx'),
     )
def dispatchTasks(wkbk, start_row):

    min_row = 0
    if start_row is None:
        min_row = 2
    else:
        min_row = int(start_row) - 2

    shtTasks = wkbk.worksheet("Tasks")
    namesTasks = shtTasks.col_values(1)
    stateTasks = shtTasks.col_values(4)

    # Having the name of each handler, dispatch to them
    print 'Start work at row #{} in the "Tasks" sheet.'.format(min_row + 2)
    complete = 0
    for row, task in enumerate(namesTasks):
        # print 'Row is #{}/{} '.format(row,min_row)
        # print 'Task #{} has {} to do.'.format(row+1,stateTasks[row])
        if row > min_row:
            if task not in ("Model Class", "", None)\
                    and int(stateTasks[row]) > 0:
                print '\n\nTask#{} uses the module "{}".'.format(row + 1, task)
                print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

                getattr(
                    lazyModule('models.' + task), task)().process(wkbk, row)
                complete += 1
    print 'Found {} tasks to process.'.format(complete)
    return
Ejemplo n.º 4
0
 def __init__(self, app):
     self.variables = dict(
         app=app,
         chandler=chandler,
         peak=peak,
         wx=imports.lazyModule('wx'),
     )
Ejemplo n.º 5
0
 def __init__(self, conf=None):
     if conf is None:
         conf = lazyModule('hammock.conf')
     server = 'http://{0}:{1}/'.format(
         conf.settings['couch']['host'],
         conf.settings['couch']['port'])
     super(Server,self).__init__(server)
     self.resource.credentials = ( conf.settings['couch']['username'],
                                   base64.b64decode(conf.settings['couch']['password']) )
Ejemplo n.º 6
0
def toBases(bases, name=''):

    if isinstance(bases, (str, ModuleType)):
        bases = bases,

    for b in bases:

        if isinstance(b, str):
            b = lazyModule(name, b)

        yield b
Ejemplo n.º 7
0
"""API functions and classes for the peak.naming package"""

from interfaces import *
from syntax import *
from names import *
from contexts import *
import spi

from peak.util.imports import lazyModule
URL = lazyModule(__name__, '../URL')
del lazyModule


def InitialContext(parent, componentName=None, **options):
    """Get an initial naming context, based on 'parent' and keyword options

    'parent' is the component which will be used as the naming context's
    parent component, to obtain any required configuration data.  The
    'componentName' argument and 'options' keyword arguments are used
    to set up the context's name and attributes, just as with a normal
    PEAK component constructor.

    This function implements the 'binding.IComponentFactory' interface, and
    thus can be used as a factory for a 'binding.Make()' attribute.  That
    is, you can do this::

        myInitCtx = binding.Make(naming.InitialContext)

    in a class to create a 'myInitCtx' attribute.  This can be useful if
    you will be using naming functions a lot and would like to hang onto
    an initial context.
Ejemplo n.º 8
0
""" goulash.bin._goulash
"""
import os
from argparse import ArgumentParser

from peak.util.imports import lazyModule

from goulash import version

fileserver = lazyModule('goulash.fileserver')
inspect = lazyModule('goulash._inspect')
projects = lazyModule('goulash.projects')
boiler = lazyModule('goulash.bin.boiler')
from goulash.docs import docs_handler
from jinja2 import Template


def get_parser():
    """ build the default parser """
    parser = ArgumentParser()
    # parser.set_conflict_handler("resolve")
    parser.add_argument(
        "-v", '--version', default=False, dest='version',
        action='store_true',
        help=("show version information"))
    subparsers = parser.add_subparsers(help='commands')
    help_parser = subparsers.add_parser('help', help='show help info')
    help_parser.set_defaults(subcommand='help')

    bparser = subparsers.add_parser('boiler', help='boilerplate generation')
    bparser.add_argument(
 def testIt(self, task):
     getattr(lazyModule('models.' + task), task)().test("AAA")
     return
Ejemplo n.º 10
0
from threads import *
from net import *
from callbacks import callsback, DefaultCallback, CallLater
from fileutil import *

def soupify(*a, **k): #this method exists so that BeautifulSoup isn't imported until needed.
    import BeautifulSoup
    return BeautifulSoup.BeautifulSoup(*a, **k)

import urllib2_file #side effects
import proxy_settings #side effects

#from ie import IEEvents, IEBrowser, JavaScript, GetIE

observe = lazyModule('util.observe')

### SOME MONKEYPATCHING

import rfc822

__old_init = rfc822.Message.__init__
__old_str  = rfc822.Message.__str__

def __init__(self, fp, *a, **k):
    if isinstance(fp, basestring):
        from cStringIO import StringIO
        return __old_init(self, StringIO(fp), *a, **k)
    else:
        return __old_init(self, fp, *a, **k)
Ejemplo n.º 11
0
#    Distributed under the terms of the GNU Lesser General Public License
#    http://www.gnu.org/copyleft/lesser.html

__all__ = ['write_dot', 'read_dot', 'graphviz_layout', 'pydot_layout',
           'to_pydot', 'from_pydot']

import sys
from networkx.utils import _get_fh
import networkx

try:
    from peak.util.imports import lazyModule
except:
    from networkx.util.imports import lazyModule

pydot=lazyModule('pydot')


def write_dot(G,path):
    """Write NetworkX graph G to Graphviz dot format on path.

    Path can be a string or a file handle.
    """
    fh=_get_fh(path,'w')
    P=to_pydot(G)
    fh.write(P.to_string())
    fh.flush() # might be a user filehandle so leave open (but flush)
    return

def read_dot(path):
    """Return a NetworkX Graph or DiGraph from a dot file on path.
Ejemplo n.º 12
0
def doImport(parser, data):
    attrs = SOX.validatedAttributes(parser, data, ('module', ), ('as', ))
    module = str(attrs['module'])
    as_ = attrs.get('as', module.split('.')[-1])
    getGlobals(data['previous'])[as_] = lazyModule(module)
Ejemplo n.º 13
0
import cPickle 
import codecs
import locale
import string
import sys
import time

from networkx.utils import is_string_like, _get_fh
import networkx

try:
    from peak.util.imports import lazyModule
except:
    from networkx.util.imports import lazyModule

yaml=lazyModule('yaml')

def write_yaml(G, path, default_flow_style=False, **kwds):
    """Write graph G in YAML text format to path. 

    See http://www.yaml.org

    """
    fh=_get_fh(path,mode='w')        
    yaml.dump(G,fh,default_flow_style=default_flow_style,**kwds)
    

def read_yaml(path):
    """Read graph from YAML format from path.

    See http://www.yaml.org
Ejemplo n.º 14
0
import wx
import sys
import actionIDs

from peak.util.imports import lazyModule

common = lazyModule('common')
from eventStack import AppEventHandlerMixin
config = lazyModule('config')
from weakref import WeakValueDictionary


def stub():
    pass


def tracecall(fn):
    """
    This decorator allows us to register that an event handler has fired
    during automated tests.
    """
    def trace(*args):
        if getattr(wx.GetApp(), "testing", False):
            wx.GetApp().event_fired = fn
            print "function %r fired" % fn
            return stub
        else:
            return fn(*args)

    return trace
Ejemplo n.º 15
0
def declareModule(name, relativePath=None, bases=(), patches=()):
    """Package Inheritance Shortcut and Third-party Patches

    This function lets you "pre-declare" that a module should have
    some set of bases or "patch modules" applied to it.  This lets
    you work around the single-inheritance limitation of package
    inheritance, and it also lets you apply "patch modules" to
    third-party code that doesn't call 'setupModule()'.

    To use this, you must call it *before* the module has been
    imported, even lazily.  You can call it again as many times
    as you like, as long as the base and patch lists remain the
    same for a given module.  Also note that the module must *exist*;
    that is, there must be some Python-findable source or bytecode
    for the specified module.  It can be "inherited" via package
    inheritance from its containing package's base package; you just
    can't make up a phony module name and have it work.  (This limitation
    might get lifted later, if it turns out to be useful.)

    'bases' are placed in the target module's '__bases__', *after*
    any bases declared in the package.  'patches' are applied as
    though they were the first modules to call 'patchModule()'.
    So the overall "MRO" for the resulting module looks like this:

        1. patches by modules calling 'patchModule()'

        2. modules specified by 'declareModule(patches=)'

        3. the module itself

        4. any '__bases__' declared by the module

        5. base modules supplied by 'declareModule(bases=)'

    Note that both 'bases' and 'patches' may be modules,
    relative paths to modules (relative to the declared module,
    *not* the 'name' parameter), or tuples of modules or paths.

    Using 'declareModule()' makes it easier to do multiple inheritance
    with packages.  For example, suppose you have packages 'square'
    and 'circle', and want to make a package 'curvyBox' that inherits
    from both.

    Further suppose that the 'square' package contains a 'square.rect'
    module, and a 'square.fill' module, and 'circle' contains a
    'circle.curve' module, and a 'circle.fill' module.  Because of
    the way the Python package '__path__' attribute works, package
    inheritance won't combine the '.fill' modules; instead, it will
    pick the first '.fill' module found.

    To solve this, we could create a 'curvyBox.fill' module that inherited
    from both 'square.fill' and 'circle.fill'.  But if there are many such
    modules or subpackages, and they will be empty but for '__bases__', we
    can use 'declareModule()' to avoid having to create individual
    subdirectories and '.py' files.

    This can be as simple as creating 'curvyBox.py' (or
    'curvybox/__init__.py'), and writing this code::

        from peak.api import *

        __bases__ = '../square', '../circle'

        config.declareModule(__name__, 'fill',
            bases = ('../../circle/fill',)  # relative to 'curvyBox.fill'
        )

        config.setupModule()

    This will add 'circle.fill' to the '__bases__' of 'curvyBox.fill' (which
    will be the inherited 'square.fill' module.

    Another usage of 'declareModule()' is to patch a third-party module::

        import my_additions
        config.declareModule('third.party.module', patches=(my_additions,))
    """

    if relativePath:
        name = joinPath(name, relativePath)

    if name in declarations:

        if declarations[name] == (bases, patches):
            return lazyModule(name)  # already declared it this way

        raise SpecificationError(
            ("%s has already been declared differently" % name), patches,
            declarations[name])

    declarations[name] = bases, patches

    def load(module):

        if hasattr(module, '__codeList__'):  # first load might leave code
            del module.__codeList__

        plist = patchMap.setdefault(name, [])
        for patch in toBases(patches, name):
            plist.extend(getCodeListForModule(patch))

        module.__bases__ = moduleBases(module, name) + tuple(
            toBases(bases, name))
        buildModule(module)

    # ensure that we are run before any other 'whenImported' hooks
    getModuleHooks(name).insert(0, load)

    return lazyModule(name)
    def getRecord(self, model, id, external=False):
        if self.modelIrModelData == None:
            self.setIrModelDataModel()

        return getattr(lazyModule('models.' + model), model)().getRecord(id, external)
Ejemplo n.º 17
0
import sys
if not sys.platform.startswith('win'):
    # Importing from gui.native.win on other platforms should never happen.
    assert 0

from peak.util.imports import lazyModule
dockconstants = lazyModule('gui.native.win.dockconstants')
winhelpers = lazyModule('gui.native.win.winhelpers')
process = lazyModule('gui.native.win.process')
toplevel = lazyModule('gui.native.win.toplevel')
winconstants = lazyModule('gui.native.win.winconstants')
winpaths = lazyModule('gui.native.win.winpaths')
winutil = lazyModule('gui.native.win.winutil')
appbar = lazyModule('gui.native.win.appbar')

Ejemplo n.º 18
0
           'write_dot', 'read_dot', 
           'graphviz_layout',
           'pygraphviz_layout']

import os
import sys
import networkx
from networkx.utils import _get_fh,is_string_like


try:
    from peak.util.imports import lazyModule
except:
    from networkx.util.imports import lazyModule

pygraphviz=lazyModule('pygraphviz')


def from_agraph(A,create_using=None):
    """Return a NetworkX Graph or DiGraph from a PyGraphviz graph.

    Parameters
    ----------
    A : PyGraphviz AGraph
      A graph created with PyGraphviz
      
    create_using : NetworkX graph class instance      
      The output is created using the given graph class instance

    Examples
    --------
Ejemplo n.º 19
0
'''
This file will contain all platform-specific extensions or method replacements to wx API,
such as overriding wx.LaunchDefaultBrowser on Windows or adding wx.Window.Cut method.
'''

from peak.util.imports import lazyModule, whenImported
wx = lazyModule('wx')
os = lazyModule('os')
wintypes = lazyModule('ctypes.wintypes')

from threading import Thread
from logging import getLogger; log = getLogger('winextensions')
import traceback

def browse(url):
    'Opens "url" in the default web browser.'

    def go():
        try:
            os.startfile(url)
        except WindowsError:
            if hasattr(traceback, 'print_exc_once'):
                traceback.print_exc_once()
            else:
                traceback.print_exc()
            log.error('could not open browser for url: %r', url)
            _fire_browser_error_popup()

    # reenable once we have advanced prefs
    t = Thread(target=go)
    t.setDaemon(True)
Ejemplo n.º 20
0
'''
A Jabber connection to the Digsby server.
'''
from __future__ import with_statement
from peak.util.imports import lazyModule
import datetime
from digsby.loadbalance import DigsbyLoadBalanceAPI, DigsbyLoadBalanceManager
callbacks = lazyModule('util.callbacks')
mapping = lazyModule('util.primitives.mapping')
funcs = lazyModule('util.primitives.funcs')
error_handling = lazyModule('util.primitives.error_handling')
hook_util = lazyModule('util.hook_util')
util_threads = lazyModule('util.threads')
import common
import sys, traceback
from common import pref, netcall
from hashlib import sha256
import jabber
from jabber import JID
from pyxmpp.presence import Presence
from pyxmpp.roster import RosterItem
from operator import itemgetter
import digsby
from .digsbybuddy import DigsbyBuddy
import random
import string
import blobs

import simplejson

from logging import getLogger
Ejemplo n.º 21
0
""" goulash.docs
"""
import os
import shutil

import addict
from fabric import api
from fabric.colors import red
from peak.util.imports import lazyModule

fileserver = lazyModule('goulash.fileserver')
inspect = lazyModule('goulash._inspect')
boiler = lazyModule('goulash.boiler')

from goulash._os import copy_tree
from goulash.decorators import require_bin
from goulash.version import __version__ as version


def _get_ctx(args):
    args.docroot = os.path.abspath(args.docroot)
    DOCS_ROOT = args.docroot
    SRC_ROOT = os.path.dirname(args.docroot)
    DOCS_URL = 'http://localhost:8000'
    DOCS_API_ROOT = os.path.join(DOCS_ROOT, 'api')
    DOCS_SITE_DIR = os.path.join(DOCS_ROOT, 'site')
    PROJECT_NAME = inspect._main_package(
        SRC_ROOT, default=os.path.dirname(os.path.abspath(SRC_ROOT)))
    ctx = locals().copy()
    ctx.pop('args')
    return ctx
Ejemplo n.º 22
0
"""

##########################################################################
## Imports
##########################################################################

from operator import itemgetter
from collections import defaultdict, Counter

from cloudscope.utils.statistics import mean, median
from cloudscope.utils.strings import snake_case
from cloudscope.exceptions import BadValue
from peak.util.imports import lazyModule

# Perform lazy loading of vizualiation libraries
pd  = lazyModule('pandas')

##########################################################################
## Time Series Handlers
##########################################################################

class TimeSeriesAggregator(object):
    """
    This complex class can handle the results time series data by key. A
    single instance is a callable that takes a key and an iterable of values,
    it then routes the value to the appropriate handler based on the key and
    returns a dictionary of the aggregates computed by key.

    If no handler is associated with the key, then by default it just counts
    the number of items in the time series (len). No exceptions are raised.
    """
Ejemplo n.º 23
0
Utilities for visualizing the topology with communications results.
"""

##########################################################################
## Imports
##########################################################################

from .analysis import aggregator

from operator import add
from collections import defaultdict
from peak.util.imports import lazyModule
from cloudscope.exceptions import BadValue

# Perform lazy loading of vizualiation libraries
gt = lazyModule('graph_tool.all')
np = lazyModule('numpy')
nx = lazyModule('networkx')


##########################################################################
## Helper Functions
##########################################################################

def merge(dicts):
    """
    Creates a single dictionary from a list of dictionaries via update.
    """
    obj = {}
    for d in dicts:
        obj.update(d)
Ejemplo n.º 24
0
from interfaces import *
from transactions import *
from data_managers import *
from connections import *
from caches import *
from lazy_loader import *

from peak.util.imports import lazyModule

xmi = lazyModule('peak.storage.xmi')

del lazyModule


def DMFor(*classes):
    from peak.api import config
    return config.ProviderOf(IDataManager, *classes)
Ejemplo n.º 25
0
""" smashlib.util.ipy

    import shortcuts for ipython.  this also might help to keep
    smashlib in sync with changing ipython target versions?
"""
import re
import keyword
from IPython.utils.coloransi import TermColors
from goulash.cache import MWT
from peak.util.imports import lazyModule
logging = lazyModule('smashlib._logging')

r_cmd = re.compile('^[\w-]+$')


def green(txt):
    return TermColors.Green + txt + TermColors.Normal


def uninstall_prefilter(HandlerOrCheckerClass):
    """ uninstalls argument from running IPython instance,
        where the argument may be either a class describing either
        a prefilter Handler or a Checker.
    """
    # are singletons involved here?  not sure we can use
    # manager.unregister_handler() etc, since it does an
    # instance check instead of a class check.
    ip = get_ipython()
    # uninstall if handler
    handlers = ip.prefilter_manager.handlers
    for handler_name, handler in handlers.items():
Ejemplo n.º 26
0
"""Integration/acceptance tests for SEF.SimpleModel, etc."""

from unittest import TestCase, makeSuite, TestSuite
from peak.api import *
from peak.model.queries import *
from peak.util.imports import lazyModule
from peak.tests import testRoot

UML13 = lazyModule('peak.metamodels.UML13')


class UML_DM(storage.xmi.DM):

    metamodel = UML13

    UML13 = UML13

    Data_Types = binding.Obtain('UML13/Foundation/Data_Types')
    Multiplicity = binding.Obtain('Data_Types/Multiplicity')
    MultiplicityRange = binding.Obtain('Data_Types/MultiplicityRange')

    Package = binding.Obtain('UML13/Model_Management/Package')
    Class = binding.Obtain('UML13/Foundation/Core/Class')


class UMLTest(TestCase):
    def setUp(self):
        self.m = m = UML_DM(testRoot())
        self.pkg = m.Package()

    def checkNameSet(self):
Ejemplo n.º 27
0
from fileutil import *


def soupify(
    *a, **k
):  #this method exists so that BeautifulSoup isn't imported until needed.
    import BeautifulSoup
    return BeautifulSoup.BeautifulSoup(*a, **k)


import urllib2_file  #side effects
import proxy_settings  #side effects

#from ie import IEEvents, IEBrowser, JavaScript, GetIE

observe = lazyModule('util.observe')

### SOME MONKEYPATCHING

import rfc822

__old_init = rfc822.Message.__init__
__old_str = rfc822.Message.__str__


def __init__(self, fp, *a, **k):
    if isinstance(fp, basestring):
        from cStringIO import StringIO
        return __old_init(self, StringIO(fp), *a, **k)
    else:
        return __old_init(self, fp, *a, **k)
Ejemplo n.º 28
0
from peak.util.imports import lazyModule
from util.primitives import odict
import struct
util         = lazyModule('util')
auxencodings = lazyModule('util.auxencodings')
import sys
import array
import logging
import string

from collections import defaultdict

import oscar

log = logging.getLogger('oscar.util')

flagged = lambda f, v: f&v==f

len_to_fmt = {0: '',  1: 'B',
              2: 'H', 4: 'I',
              8: 'Q', 16:'16s',}

def lowerstrip(s):
    return s.lower().replace(' ','')

def tflv(type_, flags=1, data=''):
    return struct.pack('!HBB', type_, flags, len(data)) + data

def tflv_list(*tflvs): return ''.join(tflv(*t) for t in tflvs)

def tlv(type_, length=0, value=0):
Ejemplo n.º 29
0
import cPickle
import codecs
import locale
import string
import sys
import time

from networkx.utils import is_string_like, _get_fh
import networkx

try:
    from peak.util.imports import lazyModule
except:
    from networkx.util.imports import lazyModule

yaml = lazyModule('yaml')


def write_yaml(G, path, default_flow_style=False, **kwds):
    """Write graph G in YAML text format to path. 

    See http://www.yaml.org

    """
    fh = _get_fh(path, mode='w')
    yaml.dump(G, fh, default_flow_style=default_flow_style, **kwds)


def read_yaml(path):
    """Read graph from YAML format from path.
Ejemplo n.º 30
0
__all__ = ['circular_layout',
           'random_layout',
           'shell_layout',
           'spring_layout',
           'spectral_layout',
           'fruchterman_reingold_layout']

import networkx

try:
    from peak.util.imports import lazyModule
except:
    from networkx.util.imports import lazyModule

np=lazyModule('numpy')
#linalg=lazyModule('scipy.sparse.linalg')
#spdiags=lazyModule('scipy.sparse.spdiags')
#eigen_symmetric=lazyModule('scipy.sparse.linalg.eigen_symmetric')

def random_layout(G,dim=2):
    n=len(G)
    pos=np.asarray(np.random.random((n,dim)),dtype=np.float32)
    return dict(zip(G,pos))


def circular_layout(G, dim=2, scale=1):
    # dim=2 only
    """Position nodes on a circle.

    Parameters
Ejemplo n.º 31
0
# ID: tree.py [] [email protected] $

"""
Utility module that implements a Tree data structure.
"""

##########################################################################
## Imports
##########################################################################

from __future__ import division

from peak.util.imports import lazyModule

# Lazy load visualizations
gt  = lazyModule('graph_tool.all')


##########################################################################
## Trees
##########################################################################

class Tree(object):
    """
    A Tree object is essentially a label and a list of children that can
    themselves be Tree objects. Trees implement bidirectionality with pointers
    to both their parent and the children. Any Tree without a parent is
    considered to be a root node.

    Trees can also maintain any arbitrary data behond their label.
    """
Ejemplo n.º 32
0
import threading
import util.urllib2_file
import simplejson
from pprint import pformat
from pstats import Stats

from util import program_dir, traceguard, threaded, RepeatTimer
from common import profile
from prefs.prefsdata import inflate
from traceback import print_exc, format_exc
from operator import attrgetter

from hashlib import sha256

from peak.util.imports import lazyModule
gui_native = lazyModule('gui.native')

LOGFILE_MAX_BYTES = 9 * 2**20

rep_map = {'no':0,
           'yes':1,
           'unknown':3}

ZIPFILE_KEY = ('f', 'datafile.zip')

from functools import partial

default_priority = 100 # lower = first

DESCRIPTION_LIMIT = 10000
Ejemplo n.º 33
0
__all__ = [
    'from_whatever', 'from_dict_of_dicts', 'to_dict_of_dicts',
    'from_dict_of_lists', 'to_dict_of_lists', 'from_edgelist', 'to_edgelist',
    'from_numpy_matrix', 'to_numpy_matrix', 'from_scipy_sparse_matrix',
    'to_scipy_sparse_matrix'
]

import networkx

try:
    from peak.util.imports import lazyModule
except:
    from networkx.util.imports import lazyModule

numpy = lazyModule('numpy')
scipy = lazyModule('scipy')
sparse = lazyModule('scipy.sparse')


def _prep_create_using(create_using):
    """
    Return a graph object ready to be populated.

    If create_using is None return the default (just networkx.Graph())
    If create_using.clear() works, assume it returns a graph object.
    Otherwise raise an exception because create_using is not a networkx graph.

    """
    if create_using is None:
        G = networkx.Graph()
Ejemplo n.º 34
0
""" kinbaku._ast
"""
import compiler
from StringIO import StringIO

import sourcecodegen
from peak.util.imports import lazyModule

from kinbaku._sourcecodegen import generate_code
from kinbaku._types import BadDotPath
core = lazyModule('kinbaku.core')

node_has_lineno  = lambda node: hasattr(node,'lineno')
node_is_function = lambda node: isinstance(node,compiler.ast.Function)
node_is_class    = lambda node: isinstance(node,compiler.ast.Class)
node_is_module   = lambda node: isinstance(node,compiler.ast.Module)

from pythoscope.store import code_of # ?

def src2stringio(src_code):
    """ """
    fhandle = StringIO()
    fhandle.write(src_code)
    fhandle.seek(0)
    return fhandle

def walk(node, parent=None, lineage=[],
         test=None, results={}, callback=None):
    """ walker for ast rooted at <node>

          callback will be invoked for node,parent,lineage
Ejemplo n.º 35
0
from peak.core import __all__

__all__ = __all__[:] + [
    'commands',
    'events',
    'logs',  #'net', 'query',
    'running',
    'security',
    'storage',
    'web',
]

# Convenience features
from peak.util.imports import lazyModule

commands = lazyModule('peak.running.commands')
events = lazyModule('peak.events.api')
logs = lazyModule('peak.running.logs')
#net         = lazyModule('peak.net.api')
#query       = lazyModule('peak.query.api')
running = lazyModule('peak.running.api')
security = lazyModule('peak.security.api')
storage = lazyModule('peak.storage.api')
web = lazyModule('peak.web.api')

# This is here so that 'peak help iif' will return something useful.
# It is not actually exported from here!  'config.ini_files' imports it
# directly, so it'll be available from .ini files.


def iif(cond, Then, Else):
Ejemplo n.º 36
0
__all__ = ['from_whatever', 
           'from_dict_of_dicts', 'to_dict_of_dicts',
           'from_dict_of_lists', 'to_dict_of_lists',
           'from_edgelist', 'to_edgelist',
           'from_numpy_matrix', 'to_numpy_matrix',
           'from_scipy_sparse_matrix', 'to_scipy_sparse_matrix']

import networkx

try:
    from peak.util.imports import lazyModule
except:
    from networkx.util.imports import lazyModule

numpy=lazyModule('numpy')
scipy=lazyModule('scipy')
sparse=lazyModule('scipy.sparse')

def _prep_create_using(create_using):
    """
    Return a graph object ready to be populated.

    If create_using is None return the default (just networkx.Graph())
    If create_using.clear() works, assume it returns a graph object.
    Otherwise raise an exception because create_using is not a networkx graph.

    """
    if create_using is None:
        G=networkx.Graph()
    else:
Ejemplo n.º 37
0
from peak.util.imports import lazyModule
from pyxmpp.utils import from_utf8
from pyxmpp.objects import StanzaPayloadObject
from pyxmpp.xmlextra import get_node_ns_uri
from digsby.widgets import DIGSBY_WIDGETS_NS
from hashlib import sha256
from util import callsback
from digsby.web import digsby_webget
from logging import getLogger
log = getLogger('digsby.widget')
skin = lazyModule('gui.skin')

from util.xml_tag import tag
from urllib2 import urlopen
from urllib import urlencode

from common import profile
from util.net import UrlQuery
import wx


def iswidget(buddy):
    'Returns True if the given buddy is from a webpage widget.'

    return hasattr(buddy, 'jid') and buddy.jid.domain == 'guest.digsby.org'


def get_local_swf():
    return skin.resourcedir() / 'widget' / 'digsby_widget.swf'

Ejemplo n.º 38
0
"""

##########################################################################
## Imports
##########################################################################

from operator import itemgetter
from collections import defaultdict, Counter

from cloudscope.utils.statistics import mean, median
from cloudscope.utils.strings import snake_case
from cloudscope.exceptions import BadValue
from peak.util.imports import lazyModule

# Perform lazy loading of vizualiation libraries
pd = lazyModule('pandas')

##########################################################################
## Time Series Handlers
##########################################################################


class TimeSeriesAggregator(object):
    """
    This complex class can handle the results time series data by key. A
    single instance is a callable that takes a key and an iterable of values,
    it then routes the value to the appropriate handler based on the key and
    returns a dictionary of the aggregates computed by key.

    If no handler is associated with the key, then by default it just counts
    the number of items in the time series (len). No exceptions are raised.
Ejemplo n.º 39
0
""" ixle.heuristics.nlp

    natural language processing heuristics
"""
from collections import OrderedDict

import nltk

from ixle.python import ope
from .base import H, Heuristic
from ixle.schema import Item
from peak.util.imports import lazyModule

heuristics = lazyModule('ixle.heuristics')



class freq_dist(Heuristic):
    """ this heuristic returns a dictionary of {token:frequency}
        for this item whenever

          1) the item exists (or is mounted), AND
          2) the is_text(item) heuristic is true

        NOTE:
          this will require nltk and perhaps some of the nltk
          data, depending on how the distro ships things.
          if you seem to be missing tokenizers, try running
          this from console:

            >>> import nltk;
Ejemplo n.º 40
0
#
# ID: viz.py [] [email protected] $

"""
Helper functions for creating output vizualiations from simulations.
"""

##########################################################################
## Imports
##########################################################################

from gvas.config import settings
from peak.util.imports import lazyModule

# Perform lazy loading of vizualiation libraries
sns = lazyModule('seaborn')
plt = lazyModule('matplotlib.pyplot')
np  = lazyModule('numpy')
pd  = lazyModule('pandas')

##########################################################################
## Helper Functions
##########################################################################

def configure(**kwargs):
    """
    Sets various configurations for Seaborn from the settings or arguments.
    """

    # Get configurations to do modifications on them.
    style   = kwargs.pop('style', settings.vizualization.style)
Ejemplo n.º 41
0
'''
A Jabber connection to the Digsby server.
'''
from __future__ import with_statement
from peak.util.imports import lazyModule
import datetime
from digsby.loadbalance import DigsbyLoadBalanceAPI, DigsbyLoadBalanceManager
callbacks      = lazyModule('util.callbacks')
mapping        = lazyModule('util.primitives.mapping')
funcs          = lazyModule('util.primitives.funcs')
error_handling = lazyModule('util.primitives.error_handling')
hook_util      = lazyModule('util.hook_util')
util_threads   = lazyModule('util.threads')
import common
import sys, traceback
from common import pref, netcall
from hashlib import sha256
import jabber
from jabber import JID
from pyxmpp.presence import Presence
from pyxmpp.roster import RosterItem
from operator import itemgetter
import digsby
from .digsbybuddy import DigsbyBuddy
import random
import string
import blobs

import simplejson

from logging import getLogger
Ejemplo n.º 42
0
Helper functions for creating output vizualiations from simulations.
"""

##########################################################################
## Imports
##########################################################################

from operator import itemgetter
from collections import defaultdict
from cloudscope.config import settings
from peak.util.imports import lazyModule
from cloudscope.colors import ColorMap
from networkx.readwrite import json_graph

# Perform lazy loading of vizualiation libraries
nx  = lazyModule('networkx')
gt  = lazyModule('graph_tool.all')
sns = lazyModule('seaborn')
plt = lazyModule('matplotlib.pyplot')
np  = lazyModule('numpy')
pd  = lazyModule('pandas')

##########################################################################
## Helper Functions
##########################################################################

def configure(**kwargs):
    """
    Sets various configurations for Seaborn from the settings or arguments.
    """
Ejemplo n.º 43
0
"""Runtime environment tools for logging, locking, process control, etc.

Please see the individual modules for useful classes, etc."""

from interfaces import *
from peak.util.imports import lazyModule

commands = lazyModule('peak.running.commands')

del lazyModule
Ejemplo n.º 44
0
Objects for computing Statistics and probabilities (reused from Ben's old code)
"""

##########################################################################
## Imports
##########################################################################

import math
import json

from itertools import islice
from collections import Counter
from peak.util.imports import lazyModule

# Lazy loading of optional dependencies
pylab = lazyModule('pylab')

##########################################################################
## Statistical computation functions
##########################################################################

def mean(data):
    """
    TODO: if Numpy becomes a dependency, change this to a Numpy computation.
    """
    # Don't do anything without data
    if not data: return None

    data = list(map(float, data))
    if data:
        return sum(data) / len(data)
Ejemplo n.º 45
0
# ID: tree.py [] [email protected] $

"""
Utility module that implements a Tree data structure.
"""

##########################################################################
## Imports
##########################################################################

from __future__ import division

from peak.util.imports import lazyModule

# Lazy load visualizations
gt = lazyModule("graph_tool.all")


##########################################################################
## Trees
##########################################################################


class Tree(object):
    """
    A Tree object is essentially a label and a list of children that can
    themselves be Tree objects. Trees implement bidirectionality with pointers
    to both their parent and the children. Any Tree without a parent is
    considered to be a root node.

    Trees can also maintain any arbitrary data behond their label.
Ejemplo n.º 46
0
'''
This file will contain all platform-specific extensions or method replacements to wx API,
such as overriding wx.LaunchDefaultBrowser on Windows or adding wx.Window.Cut method.
'''

from peak.util.imports import lazyModule, whenImported
wx = lazyModule('wx')
os = lazyModule('os')
wintypes = lazyModule('ctypes.wintypes')

from threading import Thread
from logging import getLogger
log = getLogger('winextensions')
import traceback


def browse(url):
    'Opens "url" in the default web browser.'

    def go():
        try:
            os.startfile(url)
        except WindowsError:
            if hasattr(traceback, 'print_exc_once'):
                traceback.print_exc_once()
            else:
                traceback.print_exc()
            log.error('could not open browser for url: %r', url)
            _fire_browser_error_popup()

    # reenable once we have advanced prefs
Ejemplo n.º 47
0
'''
Registers auxillary encodings in the codecs module.

>>> 'x\x9cK\xc9L/N\xaa\x04\x00\x08\x9d\x02\x83'.decode('zip')
'digsby'
'''
from peak.util.imports import lazyModule

sys            = lazyModule('sys')
warnings       = lazyModule('warnings')
locale         = lazyModule('locale')
collections    = lazyModule('collections')
urllib         = lazyModule('urllib')
urllib2        = lazyModule('urllib2')
codecs         = lazyModule('codecs')
StringIO       = lazyModule('StringIO')
zipfile        = lazyModule('zipfile')
gzip           = lazyModule('gzip')
htmlentitydefs = lazyModule('htmlentitydefs')
base64         = lazyModule('base64')
#pylzma         = lazyModule('pylzma')

HAVE_LZMA = False #until proven otherwise

ENCODE_LZMA = False

__simplechars_enc = {
                 ord('<') : 'lt',
                 ord('>') : 'gt',
                 #ord("'") : 'apos',
                 ord('"') : 'quot',
Ejemplo n.º 48
0
from peak.util.imports import lazyModule
from pyxmpp.utils import from_utf8
from pyxmpp.objects import StanzaPayloadObject
from pyxmpp.xmlextra import get_node_ns_uri
from digsby.widgets import DIGSBY_WIDGETS_NS
from hashlib import sha256
from util import callsback
from digsby.web import digsby_webget
from logging import getLogger; log = getLogger('digsby.widget')
skin = lazyModule('gui.skin')

from util.xml_tag import tag
from urllib2 import urlopen
from urllib import urlencode


from common import profile
from util.net import UrlQuery
import wx



def iswidget(buddy):
    'Returns True if the given buddy is from a webpage widget.'

    return hasattr(buddy, 'jid') and buddy.jid.domain == 'guest.digsby.org'

def get_local_swf():
    return skin.resourcedir() / 'widget' / 'digsby_widget.swf'

Ejemplo n.º 49
0
    'protocols',
    'dispatch',
    'NOT_GIVEN',
    'NOT_FOUND',
    'Items',  # Primitives
    'PropertyName',
    'binding',
    'config',
    'model',
    'naming',  # Core
    'exceptions',
]

from peak.util.imports import lazyModule, whenImported

binding = lazyModule('peak.binding.api')
config = lazyModule('peak.config.api')
model = lazyModule('peak.model.api')
naming = lazyModule('peak.naming.api')
exceptions = lazyModule('peak.exceptions')

from peak.util.symbols import NOT_GIVEN, NOT_FOUND


def Items(mapping=None, **kwargs):
    """Convert 'mapping' and/or 'kwargs' into a list of '(key,val)' items

        Key/value item lists are often easier or more efficient to manipulate
        than mapping objects, so PEAK API's will often use such lists as
        a preferred parameter format.  Sometimes, however, the syntactic sugar
        of keyword items, possibly in combination with an existing mapping