1# -*- coding: utf-8 -*-
2# Copyright (C) 2012, Almar Klein
3#
4# Visvis is distributed under the terms of the (new) BSD License.
5# The full license can be found in 'license.txt'.
6
7""" Package visvis.processing
8
9Each module in this directory contains a function with the same
10name as the module it's in. These functions are automatically loaded
11in Visvis, thus making it easy to expand Visvis' functionality.
12
13In the future, I may make it possible to supply a cython implementation
14of a function (along side a pure Python one implementation), so that
15after running a compile script, the function will run much faster.
16As for now, I'm ok with processing whole arrays at once using numpy.
17(I'm not sure, for example, whether calculateNormals can be made faster
18in Cython.)
19
20There are three things to take into account when making a new function:
21- The function to add should be the same name as the module in which
22  it is defined. Other helper functions or classes can be defined, but
23  these are not inserted in the Visvis namespace.
24- The function's docstring should be very descriptive and starting with
25  the function's signature. The docstring is also used to produce the
26  on-line documentation.
27
28"""
29
30import os
31import sys
32import zipfile
33
34from visvis.core.misc import splitPathInZip
35
36
37def _insertFunctions():
38
39    # see which files we have
40    if hasattr(sys, '_MEIPASS'):  # PyInstaller
41        zipfilename, path = sys.executable, ""
42    else:
43        path = __file__
44        path = os.path.dirname( os.path.abspath(path) )
45        zipfilename, path = splitPathInZip(path)
46
47    if zipfilename:
48        # get list of files from zipfile
49        z = zipfile.ZipFile(zipfilename)
50        files = [os.path.split(i)[1] for i in z.namelist()
51                    if i.startswith('visvis') and i.count('processing')]
52    else:
53        # get list of files from file system
54        files = os.listdir(path)
55
56    # extract all functions
57    names = []
58    for file in files:
59        # not this file
60        if file.startswith('_'):
61            continue
62        # only python files
63        if file.endswith('.pyc'):
64            if file[:-1] in files:
65                continue # only try import once
66        elif not file.endswith('.py'):
67            continue
68        # build name
69        funname = os.path.splitext(file)[0]
70        # import module
71        mod = __import__("visvis.processing."+funname, fromlist=[funname])
72        if not hasattr(mod,funname):
73            print("module %s does not have a function with the same name!" % funname)
74        else:
75            # insert into THIS namespace
76            g = globals()
77            g[funname] = mod.__dict__[funname]
78            names.append(funname)
79
80    return names
81
82
83# do it and clean up
84_functionNames = _insertFunctions()
85