<commit_before># cerbero - a multi-platform build system for Open Source software
# Copyright (C) 2012 Andoni Morales Alastruey <ylatuya@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import os
import tempfile
import shutil
from pathlib import Path

from cerbero.bootstrap import BootstrapperBase
from cerbero.bootstrap.bootstrapper import register_bootstrapper
from cerbero.config import Architecture, Distro, Platform
from cerbero.errors import ConfigurationError
from cerbero.utils import shell, _, fix_winpath, to_unixpath, git
from cerbero.utils import messages as m

# Toolchain
GCC_VERSION = '4.7.3'
MINGW_DOWNLOAD_TPL = 'https://gstreamer.freedesktop.org/data/cerbero/toolchain/windows/mingw-%s-gcc-%s-%s-%s.tar.xz'
MINGW_CHECKSUMS = {
    'mingw-w32-gcc-4.7.3-linux-x86.tar.xz': '16a3ad2584f0dc25ec122029143b186c99f362d1be1a77a338431262491004ae',
    'mingw-w64-gcc-4.7.3-linux-x86_64.tar.xz': 'e673536cc89a778043789484f691d7e35458a5d72638dc4b0123f92ecf868592',
    'mingw-w32-gcc-4.7.3-windows-x86.tar.xz': 'da783488ab3a2b28471c13ece97c643f8e8ec774308fb2a01152b23618f13a33',
    'mingw-w32-gcc-4.7.3-windows-x86_64.tar.xz': '820fa7490b3d738b9cf8c360cdd9a7aeb0592510a8ea50486e721b5b92722b08',
}

# MinGW Perl
PERL_VERSION = '5.24.0'
MINGW_PERL_TPL = 'https://sourceforge.net/projects/perl-mingw/files/{0}/perl-{0}-mingw32.zip'
MINGW_PERL_CHECKSUM = '9d4db40959727d43b4ff4b9884f5aebda20292347978036683684c2608c1397b'

# Khronos wglext.h for Windows GL
OPENGL_COMMIT = 'a4b0c7d5d10a8fca5866c6d08a608010843a4b36'
KHRONOS_WGL_TPL = 'https://raw.githubusercontent.com/KhronosGroup/OpenGL-Registry/{}/api/GL/wglext.h'
WGL_CHECKSUM = '8961c809d180e3590fca32053341fe3a83394edcb936f7699f0045feadb16115'

# Extra binary dependencies
GNOME_FTP = 'https://download.gnome.org/binaries/win32/'
WINDOWS_BIN_DEPS = [
    ('intltool/0.40/intltool_0.40.4-1_win32.zip',
     '7180a780cee26c5544c06a73513c735b7c8c107db970b40eb7486ea6c936cb33')]

# MSYS packages needed
MINGWGET_DEPS = ['msys-wget', 'msys-flex', 'msys-bison', 'msys-perl']

class WindowsBootstrapper(BootstrapperBase):
    '''
    Bootstrapper for windows builds.
    Installs the mingw-w64 compiler toolchain and headers for Directx
    '''

    def __init__(self, config, offline):
        super().__init__(config, offline)
        self.prefix = self.config.toolchain_prefix
        self.perl_prefix = self.config.mingw_perl_prefix
        self.platform = self.config.target_platform
        self.arch = self.config.target_arch
        if self.arch == Architecture.X86:
            self.version = 'w32'
        else:
            self.version = 'w64'
        self.platform = self.config.platform
        # Register all network resources this bootstrapper needs. They will all
        # be downloaded into self.config.local_sources
        #
        # MinGW toolchain
        url = MINGW_DOWNLOAD_TPL % (self.version, GCC_VERSION, self.platform, self.arch)
        self.fetch_urls.append((url, MINGW_CHECKSUMS[os.path.basename(url)]))
        self.extract_steps.append((url, True, self.prefix))
        # wglext.h
        url = KHRONOS_WGL_TPL.format(OPENGL_COMMIT)
        self.fetch_urls.append((url, WGL_CHECKSUM))
        inst_path = os.path.join(self.prefix, self.config.host, 'include/GL')
        self.extract_steps.append((url, False, inst_path))
        if self.platform == Platform.WINDOWS:
            # MinGW Perl needed by openssl
            url = MINGW_PERL_TPL.format(PERL_VERSION)
            self.fetch_urls.append((url, MINGW_PERL_CHECKSUM))
            self.extract_steps.append((url, True, self.perl_prefix))
            # Newer versions of binary deps such as intltool. Must be extracted
            # after the MinGW toolchain from above is extracted so that it
            # replaces the older files.
            for dep, checksum in WINDOWS_BIN_DEPS:
                url = GNOME_FTP + dep
                self.fetch_urls.append((url, checksum))
                self.extract_steps.append((url, True, self.prefix))

    def start(self):
        if not git.check_line_endings(self.config.platform):
            raise ConfigurationError("git is configured to use automatic line "
                    "endings conversion. You can fix it running:\n"
                    "$git config core.autocrlf false")
        self.check_dirs()
        self.fix_mingw()
        self.fix_non_prefixed_strings()
        if self.platform == Platform.WINDOWS:
            self.fix_openssl_mingw_perl()
            self.fix_bin_deps()
            # FIXME: This uses the network
            self.install_mingwget_deps()
            self.fix_mingw_unused()

    def check_dirs(self):
        if not os.path.exists(self.perl_prefix):
            os.makedirs(self.perl_prefix)
        if not os.path.exists(self.prefix):
            os.makedirs(self.prefix)
        etc_path = os.path.join(self.config.prefix, 'etc')
        if not os.path.exists(etc_path):
            os.makedirs(etc_path)

    def fix_mingw(self):
        self.fix_lib_paths()
        if self.arch == Architecture.X86:
            try:
                shutil.rmtree('/mingw/lib')
            except Exception:
                pass

    def fix_openssl_mingw_perl(self):
        '''
        This perl is only used by openssl; we can't use it everywhere else
        because it can't find msys tools, and so perl scripts like autom4te
        fail to run, f.ex., m4. Lucky for us, openssl doesn't use those.
        '''
        # Move perl installation from perl-5.xx.y to perl
        perldir = os.path.join(self.perl_prefix, 'perl-' + PERL_VERSION)
        for d in os.listdir(perldir):
            dest = os.path.join(self.perl_prefix, d)
            if os.path.exists(dest):
                shutil.rmtree(dest)
            shutil.move(os.path.join(perldir, d), self.perl_prefix)
        os.rmdir(perldir)

    def install_mingwget_deps(self):
        for dep in MINGWGET_DEPS:
            shell.call('mingw-get install %s' % dep)

    def fix_bin_deps(self):
        # replace /opt/perl/bin/perl in intltool
        files = shell.ls_files(['bin/intltool*'], self.prefix)
        for f in files:
            shell.replace(os.path.join(self.prefix, f),
                          {'/opt/perl/bin/perl': '/bin/perl'})

    def fix_lib_paths(self):
        orig_sysroot = self._find_mingw_sys_root()
        if self.config.platform != Platform.WINDOWS:
            new_sysroot = os.path.join(self.prefix, 'mingw', 'lib')
        else:
            new_sysroot = os.path.join(self.prefix, 'lib')
        lib_path = new_sysroot

        # Replace the old sysroot in all .la files
        for path in [f for f in os.listdir(lib_path) if f.endswith('la')]:
            path = os.path.abspath(os.path.join(lib_path, path))
            shell.replace(path, {orig_sysroot: new_sysroot})

    def _find_mingw_sys_root(self):
        if self.config.platform != Platform.WINDOWS:
            f = os.path.join(self.prefix, 'mingw', 'lib', 'libstdc++.la')
        else:
            f = os.path.join(self.prefix, 'lib', 'libstdc++.la')
        with open(f, 'r') as f:
            # get the "libdir=/path" line
            libdir = [x for x in f.readlines() if x.startswith('libdir=')][0]
            # get the path
            libdir = libdir.split('=')[1]
            # strip the surrounding quotes
            print("Replacing old libdir : ", libdir)
            return libdir.strip()[1:-1]

    def fix_mingw_unused(self):
        msys_mingw_bindir = Path(shutil.which('mingw-get')).parent
        # Fixes checks in configure, where cpp -v is called
        # to get some include dirs (which doesn't looks like a good idea).
        # If we only have the host-prefixed cpp, this problem is gone.
        #
        # MSYS's link.exe overrides MSVC's link.exe in new shells, so rename it
        for tool in ('cpp', 'link'):
            if (msys_mingw_bindir / (tool + '.exe')).is_file():
                os.replace(msys_mingw_bindir / (tool + '.exe'),
                           msys_mingw_bindir / (tool + '.exe.bck'))

    def fix_non_prefixed_strings(self):
        # libtool m4 macros uses non-prefixed 'strings' command. We need to
        # create a copy here
        if self.config.platform == Platform.WINDOWS:
            ext = '.exe'
        else:
            ext = ''
        if self.config.target_arch == Architecture.X86:
            host = 'i686-w64-mingw32'
        else:
            host = 'x86_64-w64-mingw32'
        bindir = os.path.join(self.config.toolchain_prefix, 'bin')
        p_strings = os.path.join(bindir, '%s-strings%s' % (host, ext))
        strings = os.path.join(bindir, 'strings%s' % ext)
        if os.path.exists(strings):
            os.remove(strings)
        shutil.copy(p_strings, strings)

def register_all():
    register_bootstrapper(Distro.WINDOWS, WindowsBootstrapper)
<commit_msg>cerbero/windows: Don't install msys-wget<commit_after># cerbero - a multi-platform build system for Open Source software
# Copyright (C) 2012 Andoni Morales Alastruey <ylatuya@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import os
import tempfile
import shutil
from pathlib import Path

from cerbero.bootstrap import BootstrapperBase
from cerbero.bootstrap.bootstrapper import register_bootstrapper
from cerbero.config import Architecture, Distro, Platform
from cerbero.errors import ConfigurationError
from cerbero.utils import shell, _, fix_winpath, to_unixpath, git
from cerbero.utils import messages as m

# Toolchain
GCC_VERSION = '4.7.3'
MINGW_DOWNLOAD_TPL = 'https://gstreamer.freedesktop.org/data/cerbero/toolchain/windows/mingw-%s-gcc-%s-%s-%s.tar.xz'
MINGW_CHECKSUMS = {
    'mingw-w32-gcc-4.7.3-linux-x86.tar.xz': '16a3ad2584f0dc25ec122029143b186c99f362d1be1a77a338431262491004ae',
    'mingw-w64-gcc-4.7.3-linux-x86_64.tar.xz': 'e673536cc89a778043789484f691d7e35458a5d72638dc4b0123f92ecf868592',
    'mingw-w32-gcc-4.7.3-windows-x86.tar.xz': 'da783488ab3a2b28471c13ece97c643f8e8ec774308fb2a01152b23618f13a33',
    'mingw-w32-gcc-4.7.3-windows-x86_64.tar.xz': '820fa7490b3d738b9cf8c360cdd9a7aeb0592510a8ea50486e721b5b92722b08',
}

# MinGW Perl
PERL_VERSION = '5.24.0'
MINGW_PERL_TPL = 'https://sourceforge.net/projects/perl-mingw/files/{0}/perl-{0}-mingw32.zip'
MINGW_PERL_CHECKSUM = '9d4db40959727d43b4ff4b9884f5aebda20292347978036683684c2608c1397b'

# Khronos wglext.h for Windows GL
OPENGL_COMMIT = 'a4b0c7d5d10a8fca5866c6d08a608010843a4b36'
KHRONOS_WGL_TPL = 'https://raw.githubusercontent.com/KhronosGroup/OpenGL-Registry/{}/api/GL/wglext.h'
WGL_CHECKSUM = '8961c809d180e3590fca32053341fe3a83394edcb936f7699f0045feadb16115'

# Extra binary dependencies
GNOME_FTP = 'https://download.gnome.org/binaries/win32/'
WINDOWS_BIN_DEPS = [
    ('intltool/0.40/intltool_0.40.4-1_win32.zip',
     '7180a780cee26c5544c06a73513c735b7c8c107db970b40eb7486ea6c936cb33')]

# MSYS packages needed
MINGWGET_DEPS = ['msys-flex', 'msys-bison', 'msys-perl']

class WindowsBootstrapper(BootstrapperBase):
    '''
    Bootstrapper for windows builds.
    Installs the mingw-w64 compiler toolchain and headers for Directx
    '''

    def __init__(self, config, offline):
        super().__init__(config, offline)
        self.prefix = self.config.toolchain_prefix
        self.perl_prefix = self.config.mingw_perl_prefix
        self.platform = self.config.target_platform
        self.arch = self.config.target_arch
        if self.arch == Architecture.X86:
            self.version = 'w32'
        else:
            self.version = 'w64'
        self.platform = self.config.platform
        # Register all network resources this bootstrapper needs. They will all
        # be downloaded into self.config.local_sources
        #
        # MinGW toolchain
        url = MINGW_DOWNLOAD_TPL % (self.version, GCC_VERSION, self.platform, self.arch)
        self.fetch_urls.append((url, MINGW_CHECKSUMS[os.path.basename(url)]))
        self.extract_steps.append((url, True, self.prefix))
        # wglext.h
        url = KHRONOS_WGL_TPL.format(OPENGL_COMMIT)
        self.fetch_urls.append((url, WGL_CHECKSUM))
        inst_path = os.path.join(self.prefix, self.config.host, 'include/GL')
        self.extract_steps.append((url, False, inst_path))
        if self.platform == Platform.WINDOWS:
            # MinGW Perl needed by openssl
            url = MINGW_PERL_TPL.format(PERL_VERSION)
            self.fetch_urls.append((url, MINGW_PERL_CHECKSUM))
            self.extract_steps.append((url, True, self.perl_prefix))
            # Newer versions of binary deps such as intltool. Must be extracted
            # after the MinGW toolchain from above is extracted so that it
            # replaces the older files.
            for dep, checksum in WINDOWS_BIN_DEPS:
                url = GNOME_FTP + dep
                self.fetch_urls.append((url, checksum))
                self.extract_steps.append((url, True, self.prefix))

    def start(self):
        if not git.check_line_endings(self.config.platform):
            raise ConfigurationError("git is configured to use automatic line "
                    "endings conversion. You can fix it running:\n"
                    "$git config core.autocrlf false")
        self.check_dirs()
        self.fix_mingw()
        self.fix_non_prefixed_strings()
        if self.platform == Platform.WINDOWS:
            self.fix_openssl_mingw_perl()
            self.fix_bin_deps()
            # FIXME: This uses the network
            self.install_mingwget_deps()
            self.fix_mingw_unused()

    def check_dirs(self):
        if not os.path.exists(self.perl_prefix):
            os.makedirs(self.perl_prefix)
        if not os.path.exists(self.prefix):
            os.makedirs(self.prefix)
        etc_path = os.path.join(self.config.prefix, 'etc')
        if not os.path.exists(etc_path):
            os.makedirs(etc_path)

    def fix_mingw(self):
        self.fix_lib_paths()
        if self.arch == Architecture.X86:
            try:
                shutil.rmtree('/mingw/lib')
            except Exception:
                pass

    def fix_openssl_mingw_perl(self):
        '''
        This perl is only used by openssl; we can't use it everywhere else
        because it can't find msys tools, and so perl scripts like autom4te
        fail to run, f.ex., m4. Lucky for us, openssl doesn't use those.
        '''
        # Move perl installation from perl-5.xx.y to perl
        perldir = os.path.join(self.perl_prefix, 'perl-' + PERL_VERSION)
        for d in os.listdir(perldir):
            dest = os.path.join(self.perl_prefix, d)
            if os.path.exists(dest):
                shutil.rmtree(dest)
            shutil.move(os.path.join(perldir, d), self.perl_prefix)
        os.rmdir(perldir)

    def install_mingwget_deps(self):
        for dep in MINGWGET_DEPS:
            shell.call('mingw-get install %s' % dep)

    def fix_bin_deps(self):
        # replace /opt/perl/bin/perl in intltool
        files = shell.ls_files(['bin/intltool*'], self.prefix)
        for f in files:
            shell.replace(os.path.join(self.prefix, f),
                          {'/opt/perl/bin/perl': '/bin/perl'})

    def fix_lib_paths(self):
        orig_sysroot = self._find_mingw_sys_root()
        if self.config.platform != Platform.WINDOWS:
            new_sysroot = os.path.join(self.prefix, 'mingw', 'lib')
        else:
            new_sysroot = os.path.join(self.prefix, 'lib')
        lib_path = new_sysroot

        # Replace the old sysroot in all .la files
        for path in [f for f in os.listdir(lib_path) if f.endswith('la')]:
            path = os.path.abspath(os.path.join(lib_path, path))
            shell.replace(path, {orig_sysroot: new_sysroot})

    def _find_mingw_sys_root(self):
        if self.config.platform != Platform.WINDOWS:
            f = os.path.join(self.prefix, 'mingw', 'lib', 'libstdc++.la')
        else:
            f = os.path.join(self.prefix, 'lib', 'libstdc++.la')
        with open(f, 'r') as f:
            # get the "libdir=/path" line
            libdir = [x for x in f.readlines() if x.startswith('libdir=')][0]
            # get the path
            libdir = libdir.split('=')[1]
            # strip the surrounding quotes
            print("Replacing old libdir : ", libdir)
            return libdir.strip()[1:-1]

    def fix_mingw_unused(self):
        msys_mingw_bindir = Path(shutil.which('mingw-get')).parent
        # Fixes checks in configure, where cpp -v is called
        # to get some include dirs (which doesn't looks like a good idea).
        # If we only have the host-prefixed cpp, this problem is gone.
        #
        # MSYS's link.exe overrides MSVC's link.exe in new shells, so rename it
        for tool in ('cpp', 'link'):
            if (msys_mingw_bindir / (tool + '.exe')).is_file():
                os.replace(msys_mingw_bindir / (tool + '.exe'),
                           msys_mingw_bindir / (tool + '.exe.bck'))

    def fix_non_prefixed_strings(self):
        # libtool m4 macros uses non-prefixed 'strings' command. We need to
        # create a copy here
        if self.config.platform == Platform.WINDOWS:
            ext = '.exe'
        else:
            ext = ''
        if self.config.target_arch == Architecture.X86:
            host = 'i686-w64-mingw32'
        else:
            host = 'x86_64-w64-mingw32'
        bindir = os.path.join(self.config.toolchain_prefix, 'bin')
        p_strings = os.path.join(bindir, '%s-strings%s' % (host, ext))
        strings = os.path.join(bindir, 'strings%s' % ext)
        if os.path.exists(strings):
            os.remove(strings)
        shutil.copy(p_strings, strings)

def register_all():
    register_bootstrapper(Distro.WINDOWS, WindowsBootstrapper)
<|endoftext|>


<commit_before>from numpy import array
from traits.api import Instance, Callable, on_trait_change
from chaco.api import DataRange2D, ImageData

# Adapted (ie. copied and modified) from function_data_source.

# Given the time frequently required for image manipulation,
# it would be awesome if there was a mechanism for returning
# partial results as they become available.

class FunctionImageData(ImageData):
    """ A class that provides data for a 2-D image based upon the range
    supplied.  This class can be used as the data source for an image plot
    or contour plot.

    Computation should be fairly swift for acceptable interactive performance.
    """

    # The function to call with the low and high values of the range
    # in the x and y dimensions.  It should return either a 2-D array
    # of numerical values, or an array of RGB or RGBA values (shape should
    # be (n, m), (n, m, 3) or (n, m, 4)).
    func = Callable

    # the 2D data_range required for the data shown
    data_range = Instance(DataRange2D)

    def __init__(self, **kw):
        super(FunctionImageData, self).__init__(**kw)
        # Explicitly construct the initial data set for ImageData
        self.recalculate()

    @on_trait_change('data_range.updated')
    def recalculate(self):
        if self.func is not None and self.data_range is not None:
            newarray = self.func(self.data_range.x_range.low, self.data_range.x_range.high,
                self.data_range.y_range.low, self.data_range.y_range.high)
            ImageData.set_data(self, newarray)
        else:
            self._data = array([], dtype=float)

    def set_data(self, *args, **kw):
        raise RuntimeError("Cannot set numerical data on a FunctionDataSource")

    def set_mask(self, mask):
        # This would be REALLY FREAKING SLICK, but it's currently unimplemented
        raise NotImplementedError

    def remove_mask(self):
        raise NotImplementedError
<commit_msg>Fix error message.<commit_after>from numpy import array
from traits.api import Instance, Callable, on_trait_change
from chaco.api import DataRange2D, ImageData

# Adapted (ie. copied and modified) from function_data_source.

# Given the time frequently required for image manipulation,
# it would be awesome if there was a mechanism for returning
# partial results as they become available.

class FunctionImageData(ImageData):
    """ A class that provides data for a 2-D image based upon the range
    supplied.  This class can be used as the data source for an image plot
    or contour plot.

    Computation should be fairly swift for acceptable interactive performance.
    """

    # The function to call with the low and high values of the range
    # in the x and y dimensions.  It should return either a 2-D array
    # of numerical values, or an array of RGB or RGBA values (shape should
    # be (n, m), (n, m, 3) or (n, m, 4)).
    func = Callable

    # the 2D data_range required for the data shown
    data_range = Instance(DataRange2D)

    def __init__(self, **kw):
        super(FunctionImageData, self).__init__(**kw)
        # Explicitly construct the initial data set for ImageData
        self.recalculate()

    @on_trait_change('data_range.updated')
    def recalculate(self):
        if self.func is not None and self.data_range is not None:
            newarray = self.func(
                self.data_range.x_range.low,
                self.data_range.x_range.high,
                self.data_range.y_range.low,
                self.data_range.y_range.high
            )
            ImageData.set_data(self, newarray)
        else:
            self._data = array([], dtype=float)

    def set_data(self, *args, **kw):
        raise RuntimeError("Cannot set numerical data on a FunctionImageData")

    def set_mask(self, mask):
        # This would be REALLY FREAKING SLICK, but it's currently unimplemented
        raise NotImplementedError

    def remove_mask(self):
        raise NotImplementedError
<|endoftext|>


<commit_before>import sys

#sys.path.append('/home/02608/grantdel/pythonlib/lib/python2.7/site-packages')
import psycopg2
import datetime
import io

class Document:

    userID = ""
    userLat = ""
    userLong = ""

    fileFrom = ""

    Feature_Freq = {}
    total_words = 0
    #Feature_Prob = {}

    def __init__(self, ID, latit, longit, file_from):
        self.userID = ID
        self.userLat = latit
        self.userLong = longit
        #self.Feature_Freq = F_Freq
        self.fileFrom = file_from

def Load(tf, tbl_name, conn_info, traintype):

    #Connecting to Database
    conn = psycopg2.connect(conn_info)
    print "DB Connection Success"

    trainFile = tf

    #openTrain = codecs.open(trainFile, 'r', encoding='utf-8')
    filename = trainFile[trainFile.rfind('/')+1:]
    print filename
    begin_time = datetime.datetime.now()
    docList = []
    x = 0
    y = 0

    read_time_begin = datetime.datetime.now()
    z = 0

    with io.open(trainFile, 'r', encoding='utf-8') as f:
        for person in f:
            x += 1
            #print "####NEW Doucment####"
            #print userID, latit, longit
            try:
                row = person.strip().split('\t')
                if traintype == "twitter":
                    #print row[0]
                    userID = row[0]
                    latit = row[1].split(',')[0]
                    longit = row[1].split(',')[1]
                elif traintype == "wiki":
                    userID = row[0]
                    page_name = row[1]
                    latit = row[2].split(',')[0]
                    longit = row[2].split(',')[1]
                newDocument = Document(userID, latit, longit, filename)
                docList.append(newDocument)
            except:
                print "@@@@@error reading user@@@@@@"
                print row
                print z
                #break
            z += 1
            if z % 5000 == 0:
                print z
                print datetime.datetime.now()

    print "------Done reading in the data-------"
    read_time_end = datetime.datetime.now()
    print read_time_end - read_time_begin

    cur = conn.cursor()

    cur.execute("CREATE TABLE IF NOT EXISTS %s (uid varchar(20) primary key, latit float, longit float, geog Geography(Point, 4326), filefrom varchar(50));" % (tbl_name, ))

    cur.execute("DELETE FROM %s" % tbl_name)

    print "Inserting points to dtbl"

    for p in docList:
        SQL1 = "INSERT INTO %s VALUES (%s, %s, %s, ST_GeographyFromText('SRID=4326;POINT(%s %s)'), %s);" % (tbl_name, '%s', '%s', '%s', '%s', '%s', '%s')
        data = (p.userID, float(p.userLat), float(p.userLong), float(p.userLong), float(p.userLat), p.fileFrom)
        cur.execute(SQL1, data)

    cur.execute("CREATE INDEX %s_gist ON %s USING gist(geog);" % (tbl_name, tbl_name))

    conn.commit()
    conn.close()

    print "Number of documents loaded: ", len(docList)

    print "Done Loading ", tbl_name
<commit_msg>LoadDBV1 - Added a small condition to avoid loading rows when they are malformed. The triggering of the try condition when row[9] doesn't exist achieves this effect.<commit_after>import sys

#sys.path.append('/home/02608/grantdel/pythonlib/lib/python2.7/site-packages')
import psycopg2
import datetime
import io

class Document:

    userID = ""
    userLat = ""
    userLong = ""

    fileFrom = ""

    Feature_Freq = {}
    total_words = 0
    #Feature_Prob = {}

    def __init__(self, ID, latit, longit, file_from):
        self.userID = ID
        self.userLat = latit
        self.userLong = longit
        #self.Feature_Freq = F_Freq
        self.fileFrom = file_from

def Load(tf, tbl_name, conn_info, traintype):

    #Connecting to Database
    conn = psycopg2.connect(conn_info)
    print "DB Connection Success"

    trainFile = tf

    #openTrain = codecs.open(trainFile, 'r', encoding='utf-8')
    filename = trainFile[trainFile.rfind('/')+1:]
    print filename
    begin_time = datetime.datetime.now()
    docList = []
    x = 0
    y = 0

    read_time_begin = datetime.datetime.now()
    z = 0

    with io.open(trainFile, 'r', encoding='utf-8') as f:
        for person in f:
            x += 1
            #print "####NEW Doucment####"
            #print userID, latit, longit
            try:
                row = person.strip().split('\t')
                if traintype == "twitter":
                    #print row[0]
                    userID = row[0]
                    latit = row[1].split(',')[0]
                    longit = row[1].split(',')[1]
                elif traintype == "wiki":
                    userID = row[0]
                    page_name = row[1]
                    latit = row[2].split(',')[0]
                    longit = row[2].split(',')[1]
                    F_Freq = row[9]
                newDocument = Document(userID, latit, longit, filename)
                docList.append(newDocument)
            except:
                print "@@@@@error reading user@@@@@@"
                print row
                print z
                #break
            z += 1
            if z % 5000 == 0:
                print z
                print datetime.datetime.now()

    print "------Done reading in the data-------"
    read_time_end = datetime.datetime.now()
    print read_time_end - read_time_begin

    cur = conn.cursor()

    cur.execute("CREATE TABLE IF NOT EXISTS %s (uid varchar(20) primary key, latit float, longit float, geog Geography(Point, 4326), filefrom varchar(50));" % (tbl_name, ))

    cur.execute("DELETE FROM %s" % tbl_name)

    print "Inserting points to dtbl"

    for p in docList:
        SQL1 = "INSERT INTO %s VALUES (%s, %s, %s, ST_GeographyFromText('SRID=4326;POINT(%s %s)'), %s);" % (tbl_name, '%s', '%s', '%s', '%s', '%s', '%s')
        data = (p.userID, float(p.userLat), float(p.userLong), float(p.userLong), float(p.userLat), p.fileFrom)
        cur.execute(SQL1, data)

    cur.execute("CREATE INDEX %s_gist ON %s USING gist(geog);" % (tbl_name, tbl_name))

    conn.commit()
    conn.close()

    print "Number of documents loaded: ", len(docList)

    print "Done Loading ", tbl_name
<|endoftext|>


<commit_before>"""

A program designed to randomize a pollyanna gift exchange where
- Parents cannot give to their children.
- Children cannot give to their parents.
- Siblings cannot give to each other.
- Spouses/Couples cannot give to each other.

"""

import random

class Person:

    def __init__(self, name, restricted_list=None, giving_to=None):
        self.name = name
        self.restricted_list = restricted_list
        self.giving_to = giving_to

def select(person, hat_list):
    '''person should be a Person object. hat_list should be a list of strings of participants.'''
    okay_list = []
    for each in hat:
        okay_list.append(each)
    for name in person.restricted_list:
        if name in okay_list:
            okay_list.remove(name)
        else:
            pass
    return random.choice(okay_list)

joe = Person('Joe', ['Joe', 'Jeff', 'Dave', 'Adam'])
dave = Person('Dave', ['Dave', 'Jeff', 'Adam', 'Joe'])
adam = Person('Adam', ['Adam', 'Jeff', 'Dave', 'Joe'])
justin = Person('Justin', ['Justin', 'Angela', 'Stefan'])
stefan = Person('Stefan', ['Stefan', 'Angela', 'Amanda', 'Justin'])
amanda = Person('Amanda', ['Amanda', 'Stefan'])
francesca = Person('Francesca', ['Francesca', 'Renee', 'George'])
jeff = Person('Jeff', ['Jeff', 'Dave', 'Joe', 'Adam', 'Renee', 'Angela'])
angela = Person('Angela', ['Angela', 'Stefan', 'Justin', 'Renee', 'Jeff'])
renee = Person('Renee', ['Renee', 'George', 'Francesca', 'Angela', 'Jeff'])
george = Person('George', ['George', 'Renee', 'Francesca'])
nanna = Person('Nanna', ['Jeff', 'Renee', 'Angela'])

participants = [joe, dave, adam, justin, stefan, amanda, francesca, jeff,
angela, renee, george, nanna]

# This should make it run until it completes successfully.
completed = False
while not completed:
    hat = []
    for participant in participants:
        hat.append(participant.name)
    try:
        for participant in participants:
            participant.giving_to = select(participant, hat)
            hat.remove(participant.giving_to)
        completed = True
    except IndexError:
        completed = False

if completed:
    print('Success. You are awesome.')
    with open('full_results.txt', 'w') as f:
        for participant in participants:
            f.write(participant.name + ' --> ' + participant.giving_to + '\n')

    for participant in participants:
        filename = '%s.txt' % participant.name
        with open(filename, 'w') as file:
            file.write(participant.giving_to)
<commit_msg>Added the Subfolder for Individual Results<commit_after>"""

A program designed to randomize a pollyanna gift exchange where
- Parents cannot give to their children.
- Children cannot give to their parents.
- Siblings cannot give to each other.
- Spouses/Couples cannot give to each other.

"""

import os
import random

class Person:

    def __init__(self, name, restricted_list=None, giving_to=None):
        self.name = name
        self.restricted_list = restricted_list
        self.giving_to = giving_to

def select(person, hat_list):
    '''person should be a Person object. hat_list should be a list of strings of participants.'''
    okay_list = []
    for each in hat:
        okay_list.append(each)
    for name in person.restricted_list:
        if name in okay_list:
            okay_list.remove(name)
        else:
            pass
    return random.choice(okay_list)

joe = Person('Joe', ['Joe', 'Jeff', 'Dave', 'Adam'])
dave = Person('Dave', ['Dave', 'Jeff', 'Adam', 'Joe'])
adam = Person('Adam', ['Adam', 'Jeff', 'Dave', 'Joe'])
justin = Person('Justin', ['Justin', 'Angela', 'Stefan'])
stefan = Person('Stefan', ['Stefan', 'Angela', 'Amanda', 'Justin'])
amanda = Person('Amanda', ['Amanda', 'Stefan'])
francesca = Person('Francesca', ['Francesca', 'Renee', 'George'])
jeff = Person('Jeff', ['Jeff', 'Dave', 'Joe', 'Adam', 'Renee', 'Angela'])
angela = Person('Angela', ['Angela', 'Stefan', 'Justin', 'Renee', 'Jeff'])
renee = Person('Renee', ['Renee', 'George', 'Francesca', 'Angela', 'Jeff'])
george = Person('George', ['George', 'Renee', 'Francesca'])
nanna = Person('Nanna', ['Jeff', 'Renee', 'Angela'])

participants = [joe, dave, adam, justin, stefan, amanda, francesca, jeff,
angela, renee, george, nanna]

# This should make it run until it completes successfully.
completed = False
while not completed:
    hat = []
    for participant in participants:
        hat.append(participant.name)
    try:
        for participant in participants:
            participant.giving_to = select(participant, hat)
            hat.remove(participant.giving_to)
        completed = True
    except IndexError:
        completed = False

if completed:
    print('Success. You are awesome.')
    with open('full_results.txt', 'w') as f:
        for participant in participants:
            f.write(participant.name + ' --> ' + participant.giving_to + '\n')
    os.chdir(os.getcwd() + '\Individual_Results')
    for participant in participants:
        filename = '%s.txt' % participant.name
        with open(filename, 'w') as file:
            file.write(participant.giving_to)
<|endoftext|>


<commit_before>###############################################################################
#   actionAngle: a Python module to calculate  actions, angles, and frequencies
#
#      class: actionAngleIsochroneInverse
#
#             Calculate (x,v) coordinates for the Isochrone potential from
#             given actions-angle coordinates
#
###############################################################################
import numpy
from scipy import optimize
from galpy.potential import IsochronePotential
from galpy.actionAngle_src.actionAngle import actionAngle
_APY_LOADED= True
try:
    from astropy import units
except ImportError:
    _APY_LOADED= False
class actionAngleIsochroneInverse(actionAngle):
    """Inverse action-angle formalism for the isochrone potential, on the Jphi, Jtheta system of Binney & Tremaine (2008); following McGill & Binney (1990) for transformations"""
    def __init__(self,*args,**kwargs):
        """
        NAME:
           __init__
        PURPOSE:
           initialize an actionAngleIsochroneInverse object
        INPUT:
           Either:

              b= scale parameter of the isochrone parameter (can be Quantity)

              ip= instance of a IsochronePotential

           ro= distance from vantage point to GC (kpc; can be Quantity)

           vo= circular velocity at ro (km/s; can be Quantity)

        OUTPUT:

           instance

        HISTORY:

           2017-11-14 - Started - Bovy (UofT)

        """
        actionAngle.__init__(self,
                             ro=kwargs.get('ro',None),vo=kwargs.get('vo',None))
        if not 'b' in kwargs and not 'ip' in kwargs: #pragma: no cover
            raise IOError("Must specify b= for actionAngleIsochrone")
        if 'ip' in kwargs:
            ip= kwargs['ip']
            if not isinstance(ip,IsochronePotential): #pragma: no cover
                raise IOError("'Provided ip= does not appear to be an instance of an IsochronePotential")
            # Check the units
            self._pot= ip
            self._check_consistent_units()
            self.b= ip.b
            self.amp= ip._amp
        else:
            self.b= kwargs['b']
            if _APY_LOADED and isinstance(self.b,units.Quantity):
                self.b= self.b.to(units.kpc).value/self._ro
            rb= numpy.sqrt(self.b**2.+1.)
            self.amp= (self.b+rb)**2.*rb
        # In case we ever decide to implement this in C...
        self._c= False
        ext_loaded= False
        if ext_loaded and (('c' in kwargs and kwargs['c'])
                           or not 'c' in kwargs): #pragma: no cover
            self._c= True
        else:
            self._c= False
        if not self._c:
            self._ip= IsochronePotential(amp=self.amp,b=self.b)
        #Define _pot, because some functions that use actionAngle instances need this
        self._pot= IsochronePotential(amp=self.amp,b=self.b)
        # Check the units
        self._check_consistent_units()
        return None

    def __call__(self,jr,jphi,jz,angler,anglephi,anglez,**kwargs):
        """
        NAME:

           __call__

        PURPOSE:

           evaluate the phase-space coordinates (x,v) for a number of angles on a single torus

        INPUT:

           jr - radial action (scalar)

           jphi - azimuthal action (scalar)

           jz - vertical action (scalar)

           angler - radial angle (array [N])

           anglephi - azimuthal angle (array [N])

           anglez - vertical angle (array [N])

           tol= (object-wide value) goal for |dJ|/|J| along the torus

        OUTPUT:

           [R,vR,vT,z,vz,phi]

        HISTORY:

           2017-11-14 - Written - Bovy (UofT)

        """
        return self.xvFreqs(jr,jphi,jz,angler,anglephi,anglez,**kwargs)[0]

    def xvFreqs(self,jr,jphi,jz,angler,anglephi,anglez,**kwargs):
        """
        NAME:

           xvFreqs

        PURPOSE:

           evaluate the phase-space coordinates (x,v) for a number of angles on a single torus as well as the frequencies

        INPUT:

           jr - radial action (scalar)

           jphi - azimuthal action (scalar)

           jz - vertical action (scalar)

           angler - radial angle (array [N])

           anglephi - azimuthal angle (array [N])

           anglez - vertical angle (array [N])

        OUTPUT:

           ([R,vR,vT,z,vz,phi],OmegaR,Omegaphi,Omegaz)

        HISTORY:

           2017-11-15 - Written - Bovy (UofT)

        """
        L= jz+numpy.fabs(jphi) # total angular momentum
        L2= L**2.
        sqrtfourbkL2= numpy.sqrt(L2+4.*self.b*self.amp)
        H= -2.*self.amp**2./(2.*jr+L+sqrtfourbkL2)**2.
        # Calculate the frequencies
        omegar= (-2.*H)**1.5/self.amp
        omegaz= (1.+L/sqrtfourbkL2)/2.*omegar
        # Start on getting the coordinates
        a= -self.amp/2./H-self.b
        ab= a+self.b
        e= numpy.sqrt(1.+L2/(2.*H*a**2.))
        # Solve Kepler's-ish equation
        angler= numpy.atleast_1d(angler) % (2.*numpy.pi)
        anglephi= numpy.atleast_1d(anglephi) % (2.*numpy.pi)
        anglez= numpy.atleast_1d(anglez) % (2.*numpy.pi)
        psi= numpy.empty(len(angler))
        for ii,ar in enumerate(angler):
            try:
                psi[ii]= optimize.newton(lambda x: x-a*e/ab*numpy.sin(x)-ar,
                                         0.,
                                         lambda x: 1-a*e/ab*numpy.cos(x))
            except RuntimeError: # Newton-Raphson did not converge
                psi[ii]= optimize.brentq(lambda x: x-a*e/ab*numpy.sin(x)-ar,
                                         0.,2.*numpy.pi)
        cospsi= numpy.cos(psi)
        r= a*numpy.sqrt((1.-e*cospsi)*(1.-e*cospsi+2.*self.b/a))
        vr= numpy.sqrt(self.amp/ab)*a*e*numpy.sin(psi)/r
        tanpsi2= numpy.tan(psi/2.)
        tan11= numpy.arctan(numpy.sqrt((1.+e)/(1.-e))*tanpsi2)
        tan12= numpy.arctan(\
            numpy.sqrt((a*(1.+e)+2.*self.b)/(a*(1.-e)+2.*self.b))*tanpsi2)
        tan11[tan11 < 0.]+= numpy.pi
        tan12[tan12 < 0.]+= numpy.pi
        Lambdapsi= tan11+L/sqrtfourbkL2*tan12
        chi= anglez-omegaz/omegar*angler+Lambdapsi
        lowerl= numpy.sqrt(1.-jphi**2./L2)
        sintheta= numpy.sin(chi)*lowerl
        costheta= numpy.sqrt(1.-sintheta**2.)
        vtheta= L*lowerl*numpy.cos(chi)/costheta/r
        R= r*costheta
        z= r*sintheta
        vR= vr*costheta-vtheta*sintheta
        vz= vr*sintheta+vtheta*costheta
        sinu= sintheta/costheta*jphi/L/lowerl
        u= numpy.arcsin(sinu)
        u[vtheta < 0.]= numpy.pi-u[vtheta < 0.]
        phi= anglephi-numpy.sign(jphi)*anglez+u
        phi= phi % (2.*numpy.pi)
        phi[phi < 0.]+= 2.*numpy.pi
        return (numpy.array([R,vR,jphi/R,z,vz,phi]).T,
                omegar,numpy.sign(jphi)*omegaz,omegaz)

    def Freqs(self,jr,jphi,jz,**kwargs):
        """
        NAME:

           Freqs

        PURPOSE:

           return the frequencies corresponding to a torus

        INPUT:

           jr - radial action (scalar)

           jphi - azimuthal action (scalar)

           jz - vertical action (scalar)

        OUTPUT:

           (OmegaR,Omegaphi,Omegaz)

        HISTORY:

           2017-11-15 - Written - Bovy (UofT)

        """
        L= jz+numpy.fabs(jphi) # total angular momentum
        sqrtfourbkL2= numpy.sqrt(L**2.+4.*self.b*self.amp)
        H= -2.*self.amp**2./(2.*jr+L+sqrtfourbkL2)**2.
        # Calculate the frequencies
        omegar= (-2.*H)**1.5/self.amp
        omegaz= (1.+L/sqrtfourbkL2)/2.*omegar
        return (omegar,numpy.sign(jphi)*omegaz,omegaz)

<commit_msg>Use general actionAngleInverse class for actionAngleIsochroneInverse; change output of actionAngleIsochroneInverse routines to R,vR,... to allow units to be added<commit_after>###############################################################################
#   actionAngle: a Python module to calculate  actions, angles, and frequencies
#
#      class: actionAngleIsochroneInverse
#
#             Calculate (x,v) coordinates for the Isochrone potential from
#             given actions-angle coordinates
#
###############################################################################
import numpy
from scipy import optimize
from galpy.potential import IsochronePotential
from galpy.actionAngle_src.actionAngleInverse import actionAngleInverse
_APY_LOADED= True
try:
    from astropy import units
except ImportError:
    _APY_LOADED= False
class actionAngleIsochroneInverse(actionAngleInverse):
    """Inverse action-angle formalism for the isochrone potential, on the Jphi, Jtheta system of Binney & Tremaine (2008); following McGill & Binney (1990) for transformations"""
    def __init__(self,*args,**kwargs):
        """
        NAME:

           __init__

        PURPOSE:

           initialize an actionAngleIsochroneInverse object

        INPUT:

           Either:

              b= scale parameter of the isochrone parameter (can be Quantity)

              ip= instance of a IsochronePotential

           ro= distance from vantage point to GC (kpc; can be Quantity)

           vo= circular velocity at ro (km/s; can be Quantity)

        OUTPUT:

           instance

        HISTORY:

           2017-11-14 - Started - Bovy (UofT)

        """
        actionAngleInverse.__init__(self,*args,**kwargs)
        if not 'b' in kwargs and not 'ip' in kwargs: #pragma: no cover
            raise IOError("Must specify b= for actionAngleIsochrone")
        if 'ip' in kwargs:
            ip= kwargs['ip']
            if not isinstance(ip,IsochronePotential): #pragma: no cover
                raise IOError("'Provided ip= does not appear to be an instance of an IsochronePotential")
            # Check the units
            self._pot= ip
            self._check_consistent_units()
            self.b= ip.b
            self.amp= ip._amp
        else:
            self.b= kwargs['b']
            if _APY_LOADED and isinstance(self.b,units.Quantity):
                self.b= self.b.to(units.kpc).value/self._ro
            rb= numpy.sqrt(self.b**2.+1.)
            self.amp= (self.b+rb)**2.*rb
        # In case we ever decide to implement this in C...
        self._c= False
        ext_loaded= False
        if ext_loaded and (('c' in kwargs and kwargs['c'])
                           or not 'c' in kwargs): #pragma: no cover
            self._c= True
        else:
            self._c= False
        if not self._c:
            self._ip= IsochronePotential(amp=self.amp,b=self.b)
        #Define _pot, because some functions that use actionAngle instances need this
        self._pot= IsochronePotential(amp=self.amp,b=self.b)
        # Check the units
        self._check_consistent_units()
        return None

    def _evaluate(self,jr,jphi,jz,angler,anglephi,anglez,**kwargs):
        """
        NAME:

           __call__

        PURPOSE:

           evaluate the phase-space coordinates (x,v) for a number of angles on a single torus

        INPUT:

           jr - radial action (scalar)

           jphi - azimuthal action (scalar)

           jz - vertical action (scalar)

           angler - radial angle (array [N])

           anglephi - azimuthal angle (array [N])

           anglez - vertical angle (array [N])

           tol= (object-wide value) goal for |dJ|/|J| along the torus

        OUTPUT:

           [R,vR,vT,z,vz,phi]

        HISTORY:

           2017-11-14 - Written - Bovy (UofT)

        """
        return self._xvFreqs(jr,jphi,jz,angler,anglephi,anglez,**kwargs)[:6]

    def _xvFreqs(self,jr,jphi,jz,angler,anglephi,anglez,**kwargs):
        """
        NAME:

           xvFreqs

        PURPOSE:

           evaluate the phase-space coordinates (x,v) for a number of angles on a single torus as well as the frequencies

        INPUT:

           jr - radial action (scalar)

           jphi - azimuthal action (scalar)

           jz - vertical action (scalar)

           angler - radial angle (array [N])

           anglephi - azimuthal angle (array [N])

           anglez - vertical angle (array [N])

        OUTPUT:

           ([R,vR,vT,z,vz,phi],OmegaR,Omegaphi,Omegaz)

        HISTORY:

           2017-11-15 - Written - Bovy (UofT)

        """
        L= jz+numpy.fabs(jphi) # total angular momentum
        L2= L**2.
        sqrtfourbkL2= numpy.sqrt(L2+4.*self.b*self.amp)
        H= -2.*self.amp**2./(2.*jr+L+sqrtfourbkL2)**2.
        # Calculate the frequencies
        omegar= (-2.*H)**1.5/self.amp
        omegaz= (1.+L/sqrtfourbkL2)/2.*omegar
        # Start on getting the coordinates
        a= -self.amp/2./H-self.b
        ab= a+self.b
        e= numpy.sqrt(1.+L2/(2.*H*a**2.))
        # Solve Kepler's-ish equation
        angler= numpy.atleast_1d(angler) % (2.*numpy.pi)
        anglephi= numpy.atleast_1d(anglephi) % (2.*numpy.pi)
        anglez= numpy.atleast_1d(anglez) % (2.*numpy.pi)
        psi= numpy.empty(len(angler))
        for ii,ar in enumerate(angler):
            try:
                psi[ii]= optimize.newton(lambda x: x-a*e/ab*numpy.sin(x)-ar,
                                         0.,
                                         lambda x: 1-a*e/ab*numpy.cos(x))
            except RuntimeError: # Newton-Raphson did not converge
                psi[ii]= optimize.brentq(lambda x: x-a*e/ab*numpy.sin(x)-ar,
                                         0.,2.*numpy.pi)
        cospsi= numpy.cos(psi)
        r= a*numpy.sqrt((1.-e*cospsi)*(1.-e*cospsi+2.*self.b/a))
        vr= numpy.sqrt(self.amp/ab)*a*e*numpy.sin(psi)/r
        tanpsi2= numpy.tan(psi/2.)
        tan11= numpy.arctan(numpy.sqrt((1.+e)/(1.-e))*tanpsi2)
        tan12= numpy.arctan(\
            numpy.sqrt((a*(1.+e)+2.*self.b)/(a*(1.-e)+2.*self.b))*tanpsi2)
        tan11[tan11 < 0.]+= numpy.pi
        tan12[tan12 < 0.]+= numpy.pi
        Lambdapsi= tan11+L/sqrtfourbkL2*tan12
        chi= anglez-omegaz/omegar*angler+Lambdapsi
        lowerl= numpy.sqrt(1.-jphi**2./L2)
        sintheta= numpy.sin(chi)*lowerl
        costheta= numpy.sqrt(1.-sintheta**2.)
        vtheta= L*lowerl*numpy.cos(chi)/costheta/r
        R= r*costheta
        z= r*sintheta
        vR= vr*costheta-vtheta*sintheta
        vz= vr*sintheta+vtheta*costheta
        sinu= sintheta/costheta*jphi/L/lowerl
        u= numpy.arcsin(sinu)
        u[vtheta < 0.]= numpy.pi-u[vtheta < 0.]
        phi= anglephi-numpy.sign(jphi)*anglez+u
        phi= phi % (2.*numpy.pi)
        phi[phi < 0.]+= 2.*numpy.pi
        return (R,vR,jphi/R,z,vz,phi,
                omegar,numpy.sign(jphi)*omegaz,omegaz)

    def _Freqs(self,jr,jphi,jz,**kwargs):
        """
        NAME:

           Freqs

        PURPOSE:

           return the frequencies corresponding to a torus

        INPUT:

           jr - radial action (scalar)

           jphi - azimuthal action (scalar)

           jz - vertical action (scalar)

        OUTPUT:

           (OmegaR,Omegaphi,Omegaz)

        HISTORY:

           2017-11-15 - Written - Bovy (UofT)

        """
        L= jz+numpy.fabs(jphi) # total angular momentum
        sqrtfourbkL2= numpy.sqrt(L**2.+4.*self.b*self.amp)
        H= -2.*self.amp**2./(2.*jr+L+sqrtfourbkL2)**2.
        # Calculate the frequencies
        omegar= (-2.*H)**1.5/self.amp
        omegaz= (1.+L/sqrtfourbkL2)/2.*omegar
        return (omegar,numpy.sign(jphi)*omegaz,omegaz)

<|endoftext|>


<commit_before>import eventlet
import oslo_messaging
from oslo_config import cfg
from oslo_log import log as logging
from gbpservice.nfp.configurator.agents import loadbalancer_v1
from neutron.common import rpc as n_rpc
from neutron import context
eventlet.monkey_patch()

LOG = logging.getLogger(__name__)

CONFIGURATOR_TOPIC = "configurator"
HOST = "Devstack-8"

"""
Test file to send LBaaS standard rpcs to configurator module using
generic apis
"""

class LBResources(object):

    def __init__(self):
        self.driver_name = 'loadbalancer'

        self.vip = {
                    "status": "ACTIVE",
                    "protocol": "TCP",
                    "description": {"floating_ip": "192.168.100.149",
                                    "provider_interface_mac":
                                        "aa:bb:cc:dd:ee:ff"},
                    "address": "42.0.0.14",
                    "protocol_port": 22,
                    "port_id": "cfd9fcc0-c27b-478b-985e-8dd73f2c16e8",
                    "id": "7a755739-1bbb-4211-9130-b6c82d9169a5",
                    "status_description": None,
                    "name": "lb-vip",
                    "admin_state_up": True,
                    "subnet_id": "b31cdafe-bdf3-4c19-b768-34d623d77d6c",
                    "tenant_id": "f6b09b7a590642d8ac6de73df0ab0686",
                    "connection_limit": -1,
                    "pool_id": "6350c0fd-07f8-46ff-b797-62acd23760de",
                    "session_persistence": None
                    }

        self.pool = {"status": "ACTIVE",
                     "lb_method": "ROUND_ROBIN",
                     "protocol": "TCP",
                     "description": "",
                     "health_monitors": [],
                     "members":
                     [
                        "4910851f-4af7-4592-ad04-08b508c6fa21",
                        "76d2a5fc-b39f-4419-9f33-3b21cf16fe47"
                     ],
                     "status_description": None,
                     "id": "6350c0fd-07f8-46ff-b797-62acd23760de",
                     "vip_id": "7a755739-1bbb-4211-9130-b6c82d9169a5",
                     "name": "lb-pool",
                     "admin_state_up": True,
                     "subnet_id": "b31cdafe-bdf3-4c19-b768-34d623d77d6c",
                     "tenant_id": "f6b09b7a590642d8ac6de73df0ab0686",
                     "health_monitors_status": [],
                     "provider": "haproxy"
                     }

        self.old_pool = {"status": "ACTIVE",
                         "lb_method": "ROUND_ROBIN",
                         "protocol": "TCP",
                         "description": "",
                         "health_monitors": [],
                         "members":
                         [
                            "4910851f-4af7-4592-ad04-08b508c6fa21",
                            "76d2a5fc-b39f-4419-9f33-3b21cf16fe47"
                         ],
                         "status_description": None,
                         "id": "6350c0fd-07f8-46ff-b797-62acd23760de",
                         "vip_id": "7a755739-1bbb-4211-9130-b6c82d9169a5",
                         "name": "lb-pool",
                         "admin_state_up": True,
                         "subnet_id": "b31cdafe-bdf3-4c19-b768-34d623d77d6c",
                         "tenant_id": "f6b09b7a590642d8ac6de73df0ab0686",
                         "health_monitors_status": [],
                         "provider": "haproxy"
                         }

        self.member = {
                    "admin_state_up": True,
                    "status": "ACTIVE",
                    "status_description": None,
                    "weight": 1,
                    "address": "42.0.0.11",
                    "tenant_id": "f6b09b7a590642d8ac6de73df0ab0686",
                    "protocol_port": 80,
                    "id": "4910851f-4af7-4592-ad04-08b508c6fa21",
                    "pool_id": "6350c0fd-07f8-46ff-b797-62acd23760de"
                    }

        self.health_monitor = {
                    "admin_state_up": True,
                    "tenant_id": "f6b09b7a590642d8ac6de73df0ab0686",
                    "delay": 10,
                    "max_retries": 3,
                    "timeout": 10,
                    "pools": [],
                    "type": "PING",
                    "id": "c30d8a88-c719-4b93-aa64-c58efb397d86"
                     }

class LBContext(object):
    def __init__(self):
        self.ports = [
            {"status": "ACTIVE",
             "name": "",
             "allowed_address_pairs": [],
             "admin_state_up": True,
             "network_id": "92f423a7-f44e-4726-b453-c8a1369a3ad0",
             "tenant_id": "5e67167662f94fd5987e12a68ea6c1d8",
             "extra_dhcp_opts": [],
             "binding:vnic_type": "normal",
             "device_owner": "network:dhcp",
             "mac_address": "fa:16:3e:01:19:11",
             "fixed_ips": [
                 {
                     "subnet_id": "2670bdcd-1bcf-4b97-858d-ab0d621983cc",
                     "ip_address": "11.0.0.3"
                     },
                 {
                     "subnet_id": "94aee832-935b-4e23-8f90-b6a81b0195b1",
                     "ip_address": "192.168.0.2"
                        }
                ],
             "id": "cfd9fcc0-c27b-478b-985e-8dd73f2c16e8",
             "security_groups": [],
             "device_id": ("dhcpf986c817-fd54-5bae-a8e4-e473b69100d2-"
                           "92f423a7-f44e-4726-b453-c8a1369a3ad0")
             },
            {"status": "ACTIVE",
             "name": ("aff8163b-f964-4ad7-a222-0e0a6e5593fe-"
                      "ea9ff596-51bc-4381-8aff-ee9f0ef7e319"),
             "allowed_address_pairs": [],
             "admin_state_up": True,
             "network_id": "0ced2567-47a0-4b67-be52-0e9695e8b0e6",
             "tenant_id": "5e67167662f94fd5987e12a68ea6c1d8",
             "extra_dhcp_opts": [],
             "binding:vnic_type": "normal",
             "device_owner": "network:router_interface",
             "mac_address": "fa:16:3e:1b:f2:44",
             "fixed_ips": [
                 {
                     "subnet_id": "ea9ff596-51bc-4381-8aff-ee9f0ef7e319",
                     "ip_address": "11.0.3.2"
                 }
               ],
             "id": "31df0d68-e9ea-4713-a629-29e6d87c2727",
             "security_groups": ["fb44b3f5-a319-4176-9e3b-361c5faafb66"],
             "device_id": "aff8163b-f964-4ad7-a222-0e0a6e5593fe"
             },
            {
             "status": "ACTIVE",
             "name": (
                "aff8163b-f964-4ad7-a222-0e0a6e5593fe-8eacf5cf"
                "-1e92-4e7b-90c4-cc68ef8c4e88"),
             "allowed_address_pairs": [],
             "admin_state_up": True,
             "network_id": "2e9652e8-bd95-472a-96b5-6a7939ae0f8d",
             "tenant_id": "5e67167662f94fd5987e12a68ea6c1d8",
             "extra_dhcp_opts": [],
             "binding:vnic_type": "normal",
             "device_owner": "network:router_interface",
             "mac_address": "fa:16:3e:49:44:b3",
             "fixed_ips": [
                 {
                     "subnet_id": "8eacf5cf-1e92-4e7b-90c4-cc68ef8c4e88",
                     "ip_address": "11.0.4.2"
                     }
                 ],
             "id": "214eaa12-36c9-45b1-8fee-350ce2ff2dae",
             "security_groups": ["fb44b3f5-a319-4176-9e3b-361c5faafb66"],
             "device_id": "aff8163b-f964-4ad7-a222-0e0a6e5593fe"
             }
            ]

        self.subnets = [
                {
                    "name": "apic_owned_ew-consumer",
                    "enable_dhcp": True,
                    "network_id": "0ced2567-47a0-4b67-be52-0e9695e8b0e6",
                    "tenant_id": "5e67167662f94fd5987e12a68ea6c1d8",
                    "dns_nameservers": [],
                    "gateway_ip": "11.0.3.1",
                    "ipv6_ra_mode": None,
                    "allocation_pools": [
                        {
                            "start": "11.0.3.2",
                            "end": "11.0.3.254"
                            }
                        ],
                    "host_routes": [],
                    "ip_version": 4,
                    "ipv6_address_mode": None,
                    "cidr": "11.0.3.0/24",
                    "id": "ea9ff596-51bc-4381-8aff-ee9f0ef7e319"
                    },
                {
                    "name": "apic_owned_ew-provider",
                    "enable_dhcp": True,
                    "network_id": "2e9652e8-bd95-472a-96b5-6a7939ae0f8d",
                    "tenant_id": "5e67167662f94fd5987e12a68ea6c1d8",
                    "dns_nameservers": [],
                    "gateway_ip": "11.0.4.1",
                    "ipv6_ra_mode": None,
                    "allocation_pools": [
                        {
                            "start": "11.0.4.2",
                            "end": "11.0.4.254"
                            }
                        ],
                    "host_routes": [],
                    "ip_version": 4,
                    "ipv6_address_mode": None,
                    "cidr": "11.0.4.0/24",
                    "id": "94aee832-935b-4e23-8f90-b6a81b0195b1"
                    }
                ]
        self.pools = [
                {"status": "ACTIVE",
                    "lb_method": "ROUND_ROBIN",
                    "protocol": "TCP",
                    "description": "",
                    "health_monitors": [],
                    "members":
                    [
                        "4910851f-4af7-4592-ad04-08b508c6fa21",
                        "76d2a5fc-b39f-4419-9f33-3b21cf16fe47"
                        ],
                    "status_description": None,
                    "id": "6350c0fd-07f8-46ff-b797-62acd23760de",
                    "vip_id": "7a755739-1bbb-4211-9130-b6c82d9169a5",
                    "name": "lb-pool",
                    "admin_state_up": True,
                    "subnet_id": "b31cdafe-bdf3-4c19-b768-34d623d77d6c",
                    "tenant_id": "f6b09b7a590642d8ac6de73df0ab0686",
                    "health_monitors_status": [],
                    "provider": "haproxy"},
                ]

        self.vips = [
                {
                    "status": "ACTIVE",
                    "protocol": "TCP",
                    "description": {"floating_ip": "192.168.100.149",
                                    "provider_interface_mac":
                                        "aa:bb:cc:dd:ee:ff"},
                    "address": "42.0.0.14",
                    "protocol_port": 22,
                    "port_id": "cfd9fcc0-c27b-478b-985e-8dd73f2c16e8",
                    "id": "7a755739-1bbb-4211-9130-b6c82d9169a5",
                    "status_description": None,
                    "name": "lb-vip",
                    "admin_state_up": True,
                    "subnet_id": "b31cdafe-bdf3-4c19-b768-34d623d77d6c",
                    "tenant_id": "f6b09b7a590642d8ac6de73df0ab0686",
                    "connection_limit": -1,
                    "pool_id": "6350c0fd-07f8-46ff-b797-62acd23760de",
                    "session_persistence": None
                    }
                ]

        self.health_monitors = [
                {
                    "admin_state_up": True,
                    "tenant_id": "f6b09b7a590642d8ac6de73df0ab0686",
                    "delay": 10,
                    "max_retries": 3,
                    "timeout": 10,
                    "pools": [],
                    "type": "PING",
                    "id": "c30d8a88-c719-4b93-aa64-c58efb397d86"
                     }
                ]
        self.members = [
                {
                    "admin_state_up": True,
                    "status": "ACTIVE",
                    "status_description": None,
                    "weight": 1,
                    "address": "42.0.0.11",
                    "tenant_id": "f6b09b7a590642d8ac6de73df0ab0686",
                    "protocol_port": 80,
                    "id": "4910851f-4af7-4592-ad04-08b508c6fa21",
                    "pool_id": "6350c0fd-07f8-46ff-b797-62acd23760de"},
                {
                    "admin_state_up": True,
                    "status": "ACTIVE",
                    "status_description": None,
                    "weight": 1,
                    "address": "42.0.0.13",
                    "tenant_id": "f6b09b7a590642d8ac6de73df0ab0686",
                    "protocol_port": 22,
                    "id": "76d2a5fc-b39f-4419-9f33-3b21cf16fe47",
                    "pool_id": "6350c0fd-07f8-46ff-b797-62acd23760de"
                    }
                ]

    def get_service_info(self):
        service_info = {}
        service_info['pools'] = self.pools
        service_info['members'] = self.members
        service_info['vips'] = self.vips
        service_info['health_monitors'] = self.health_monitors
        service_info['ports'] = self.ports
        service_info['subnets'] = self.subnets
        return service_info

class LBRpcSenderV1(object):
    """ Client side of service manager """

    API_VERSION = '1.0'

    def __init__(self, host=HOST, topic=CONFIGURATOR_TOPIC):
        self.host = host
        super(LBRpcSenderV1, self).__init__()
        target = oslo_messaging.Target(
                                       topic=topic,
                                       version=self.API_VERSION)
        n_rpc.init(cfg.CONF)
        self.client = n_rpc.get_client(target)
        self.lb_resources = LBResources()
        self.context = context.get_admin_context_without_session()
        self.lb_context = LBContext()

    def _prepare_request_data(self, resource, kwargs):
        request_data = {
             'info':   {'version': 'v1',
                        'service_type': 'loadbalancer'},
             'config': [{'resource': resource,
                         'kwargs': kwargs}]
            }
        return request_data

    def test_create_pool(self):
        print 'called test_create_pool'
        pool_info = {'context': {'service_info':
                                 self.lb_context.get_service_info()},
                     'pool': self.lb_resources.pool,
                     'driver_name': 'loadbalancer'
                     }

        pool_data = self._prepare_request_data('pool', pool_info)
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context, 'create_network_function_config',
                          request_data=pool_data)

    def test_update_pool(self):
        print 'called test_update_pool'
        cctxt = self.client.prepare(server=self.host)
        pool_info = {'context': {'service_info':
                                 self.lb_context.get_service_info()},
                     'old_pool': self.lb_resources.pool,
                     'pool': self.lb_resources.pool,
                     }

        pool_data = self._prepare_request_data('pool', pool_info)
        return cctxt.cast(self.context, 'update_network_function_config',
                          request_data=pool_data)

    def test_delete_pool(self):
        print 'called test_delete_pool'
        pool_info = {'context': {'service_info':
                                 self.lb_context.get_service_info()},
                     'pool': self.lb_resources.pool
                     }

        pool_data = self._prepare_request_data('pool', pool_info)
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context, 'delete_network_function_config',
                          request_data=pool_data)

    def test_create_vip(self):
        print 'called test_create_vip'
        vip_info = {'context': {'service_info':
                                self.lb_context.get_service_info()},
                    'vip': self.lb_resources.vip
                    }

        vip_data = self._prepare_request_data('vip', vip_info)
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context, 'create_network_function_config',
                          request_data=vip_data)

    def test_update_vip(self):
        print 'called test_update_vip'
        vip_info = {'context': {'service_info':
                                self.lb_context.get_service_info()},
                    'old_vip': self.lb_resources.vip,
                    'vip': self.lb_resources.vip
                    }

        vip_data = self._prepare_request_data('vip', vip_info)
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context, 'update_network_function_config',
                          request_data=vip_data)

    def test_delete_vip(self):
        print 'called test_delete_vip'
        vip_info = {'context': {'service_info':
                                self.lb_context.get_service_info()},
                    'vip': self.lb_resources.vip
                    }

        vip_data = self._prepare_request_data('vip', vip_info)
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context, 'delete_network_function_config',
                          request_data=vip_data)

    def test_create_member(self):
        print 'called test_create_member'
        member_info = {'context': {'service_info':
                                   self.lb_context.get_service_info()},
                       'member': self.lb_resources.member
                       }

        member_data = self._prepare_request_data('member', member_info)
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context, 'create_network_function_config',
                          request_data=member_data)

    def test_update_member(self):
        print 'called test_update_member'
        member_info = {'context': {'service_info':
                                   self.lb_context.get_service_info()},
                       'old_member': self.lb_resources.member,
                       'member': self.lb_resources.member
                       }

        member_data = self._prepare_request_data('member', member_info)
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context, 'update_network_function_config',
                          request_data=member_data)

    def test_delete_member(self):
        print 'called test_delete_member'
        member_info = {'context': {'service_info':
                                   self.lb_context.get_service_info()},
                       'member': self.lb_resources.member
                       }

        member_data = self._prepare_request_data('member', member_info)
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context, 'delete_network_function_config',
                          request_data=member_data)

    def test_create_pool_health_monitor(self):
        print 'called test_create_health_monitor'
        hm_info = {'context': {'service_info':
                               self.lb_context.get_service_info()},
                   'health_monitor': self.lb_resources.health_monitor,
                   'pool_id': self.lb_resources.pool['id']
                   }

        hm_data = self._prepare_request_data('pool_health_monitor',
                                             hm_info)
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context, 'create_network_function_config',
                          request_data=hm_data)

    def test_update_pool_health_monitor(self):
        print 'called test_update_health_monitor'
        hm_info = {'context': {'service_info':
                               self.lb_context.get_service_info()},
                   'old_health_monitor': self.lb_resources.health_monitor,
                   'health_monitor': self.lb_resources.health_monitor,
                   'pool_id': self.lb_resources.pool['id']
                   }

        hm_data = self._prepare_request_data('pool_health_monitor',
                                             hm_info)
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context, 'update_network_function_config',
                          request_data=hm_data)

    def test_delete_pool_health_monitor(self):
        print 'called test_delete_health_monitor'
        hm_info = {'context': {'service_info':
                               self.lb_context.get_service_info()},
                   'health_monitor': self.lb_resources.health_monitor,
                   'pool_id': self.lb_resources.pool['id']
                   }

        hm_data = self._prepare_request_data('pool_health_monitor',
                                             hm_info)
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context, 'delete_network_function_config',
                          request_data=hm_data)

    def test_configure_interfaces(self):
        print 'called test_configure_interfaces'
        data = {'context': {},
                'service_type': 'loadbalancer',
                'request_info': {},
                }
        request_data = {
                     'info':   {'version': 'v1'},
                     'config': [{'resource': 'interfaces',
                                'kwargs': data}]
                       }
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context,
                          'create_network_function_device_config',
                          request_data=request_data)

    def test_clear_interfaces(self):
        print 'called test_clear_interfaces'
        data = {'context': {},
                'service_type': 'loadbalancer',
                'request_info': {},
                }
        request_data = {
                     'info':   {'version': 'v1'},
                     'config': [{'resource': 'interfaces',
                                'kwargs': data}]
                       }
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context,
                          'delete_network_function_device_config',
                          request_data=request_data)

    def test_configure_source_routes(self):
        print 'called test_configure_source_routes'
        data = {'context': {},
                'service_type': 'loadbalancer',
                'request_info': {},
                }
        request_data = {
                     'info':   {'version': 'v1'},
                     'config': [{'resource': 'routes',
                                'kwargs': data}]
                       }
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context,
                          'create_network_function_device_config',
                          request_data=request_data)

    def test_clear_source_routes(self):
        print 'called test_clear_source_routes'
        data = {'context': {},
                'service_type': 'loadbalancer',
                'request_info': {},
                }
        request_data = {
                     'info':   {'version': 'v1'},
                     'config': [{'resource': 'routes',
                                'kwargs': data}]
                       }
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context,
                          'delete_network_function_device_config',
                          request_data=request_data)

    def test_configure_healthmonitor(self, periodicity):
        print 'called test_configure_healthmonitor'
        data = {'context': {},
                'service_type': 'loadbalancer',
                'vmid': '6350c0fd-07f8-46ff-b797-62acd2371234',
                'mgmt_ip': '127.0.0.1',
                'periodicity': periodicity,
                'request_info': {}
                }

        request_data = {
                     'info':   {'version': 'v1'},
                     'config': [{'resource': 'healthmonitor',
                                'kwargs': data}]
                       }
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context,
                          'create_network_function_device_config',
                          request_data=request_data)

    def test_clear_healthmonitor(self):
        print 'called test_clear_healthmonitor'
        data = {'context': {},
                'service_type': 'loadbalancer',
                'vmid': '6350c0fd-07f8-46ff-b797-62acd2371234',
                'request_info': {}
                }
        request_data = {
                     'info':   {'version': 'v1'},
                     'config': [{'resource': 'healthmonitor',
                                'kwargs': data}]
                       }
        cctxt = self.client.prepare(server=self.host)
        return cctxt.call(self.context,
                          'delete_network_function_device_config',
                          request_data=request_data)

# LBaaS rpc test cases
client = LBRpcSenderV1(HOST, CONFIGURATOR_TOPIC)
client.test_create_pool()
client.test_create_vip()
client.test_create_member()
client.test_create_pool_health_monitor()

# client.test_update_pool()
# client.test_update_vip()
# client.test_update_member()
# client.test_update_pool_health_monitor()
#
# client.test_delete_pool_health_monitor()
# client.test_delete_member()
# client.test_delete_vip()
# client.test_delete_pool()

# Generic Config Test cases
# client.test_configure_interfaces()
# client.test_clear_interfaces()
# client.test_configure_source_routes()
# client.test_clear_source_routes()

# client.test_configure_healthmonitor('initial')
# client.test_configure_healthmonitor('forever')
# client.test_clear_healthmonitor()
<commit_msg>Removed the informal unit test file of LB from configurator.<commit_after><|endoftext|>


<commit_before>import sys
import os
import logging
import subprocess
from shutil import copy, rmtree
from .helpers import get_rhino_ironpython_path

logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)

if __name__ == '__main__':
    location = None
    if len(sys.argv) > 1:
        location = sys.argv[1]
        try:
            location = int(location)
        except ValueError:
            pass

    rhino_ironpython_path = get_rhino_ironpython_path(location=location)

    # Install the package to Rhino IronPython Users lib
    package_name = 'gh-python-remote'
    pip_cmd = [sys.executable, '-m', 'pip', 'install', '--upgrade',
               '--target=' + rhino_ironpython_path,
               '--no-binary=:all:', '--no-compile', '--ignore-requires-python',
               package_name, ]
    print('\n\nThis will install ghpythonremote in Rhino IronPython with the command:')
    print(' '.join(pip_cmd))
    subprocess.check_call(pip_cmd)

    # Get the Grasshopper libraries base dir
    gh_userobjects_path = os.path.join(os.getenv('APPDATA', ''), 'Grasshopper', 'UserObjects')
    if os.path.isdir(gh_userobjects_path):
        dest_dir_path = os.path.join(gh_userobjects_path, package_name)
    else:
        logging.warning('Could not find Grasshopper "UserObjects" special folder; '
                        + 'example files will be installed to default temp dir.')
        dest_dir_path = os.path.join(os.getenv('TEMP', 'C:\\'), package_name)
    # Get the filepath in the installed package
    source_dir_path = os.path.dirname(os.path.realpath(__file__))
    # Send the files there
    if os.path.isdir(dest_dir_path):
        rmtree(dest_dir_path)
    os.mkdir(dest_dir_path)
    copy_pairs = [
        ('examples', [
            'examples/curves.3dm',
            'examples/GH_python_remote.ghx',
            'examples/IronPython_to_CPython.py'
            'examples/GH_to_python.py',
            'examples/python_to_GH.py',
        ]),
        ('', [
            'ghcluster/GHPythonRemote.ghuser',
        ])
    ]
    for copy_pair in copy_pairs:
        dest = os.path.join(dest_dir_path, copy_pair[0])
        if not os.path.isdir(dest):
            os.mkdir(dest)
        for file_rel_path in copy_pair[1]:
            source = os.path.join(source_dir_path, *file_rel_path.split('/'))
            copy(source, dest)
    logging.info('Copied example files to {!s}'.format(dest_dir_path))<commit_msg>Typo in files to copy<commit_after>import sys
import os
import logging
import subprocess
from shutil import copy, rmtree
from .helpers import get_rhino_ironpython_path

logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)

if __name__ == '__main__':
    location = None
    if len(sys.argv) > 1:
        location = sys.argv[1]
        try:
            location = int(location)
        except ValueError:
            pass

    rhino_ironpython_path = get_rhino_ironpython_path(location=location)

    # Install the package to Rhino IronPython Users lib
    package_name = 'gh-python-remote'
    pip_cmd = [sys.executable, '-m', 'pip', 'install', '--upgrade',
               '--target=' + rhino_ironpython_path,
               '--no-binary=:all:', '--no-compile', '--ignore-requires-python',
               package_name, ]
    print('\n\nThis will install ghpythonremote in Rhino IronPython with the command:')
    print(' '.join(pip_cmd))
    subprocess.check_call(pip_cmd)

    # Get the Grasshopper libraries base dir
    gh_userobjects_path = os.path.join(os.getenv('APPDATA', ''), 'Grasshopper', 'UserObjects')
    if os.path.isdir(gh_userobjects_path):
        dest_dir_path = os.path.join(gh_userobjects_path, package_name)
    else:
        logging.warning('Could not find Grasshopper "UserObjects" special folder; '
                        + 'example files will be installed to default temp dir.')
        dest_dir_path = os.path.join(os.getenv('TEMP', 'C:\\'), package_name)
    # Get the filepath in the installed package
    source_dir_path = os.path.dirname(os.path.realpath(__file__))
    # Send the files there
    if os.path.isdir(dest_dir_path):
        rmtree(dest_dir_path)
    os.mkdir(dest_dir_path)
    copy_pairs = [
        ('examples', [
            'examples/curves.3dm',
            'examples/GH_python_remote.ghx',
            'examples/IronPython_to_CPython.py',
            'examples/GH_to_python.py',
            'examples/python_to_GH.py',
        ]),
        ('', [
            'ghcluster/GHPythonRemote.ghuser',
        ])
    ]
    for copy_pair in copy_pairs:
        dest = os.path.join(dest_dir_path, copy_pair[0])
        if not os.path.isdir(dest):
            os.mkdir(dest)
        for file_rel_path in copy_pair[1]:
            source = os.path.join(source_dir_path, *file_rel_path.split('/'))
            copy(source, dest)
    logging.info('Copied example files to {!s}'.format(dest_dir_path))<|endoftext|>


<commit_before>import numpy as np

from ...extern.vispy import scene

from ..viewer_options import VispyOptionsWidget
from ..vispy_widget import VispyWidget

def test_vispy_widget():

    w = VispyWidget()
    d = VispyOptionsWidget(vispy_widget=w)

    w.show()
    d.show()

    # Try adding marker visuals to the scene
    positions = np.random.random((1000, 3))
    scat_visual = scene.visuals.Markers()
    scat_visual.set_data(positions, symbol='disc', edge_color=None, face_color='red')
    w.add_data_visual(scat_visual)

    d.set_limits(-1., 1., -1., 1., -1., 1.)

    np.testing.assert_equal(scat_visual.transform.scale, [1., 1., 1., 1.])
    np.testing.assert_equal(scat_visual.transform.translate, [0., 0., 0., 0.])

    d.x_min = 0
    d.x_max = +1

    np.testing.assert_equal(scat_visual.transform.scale, [2., 1., 1., 1.])
    np.testing.assert_equal(scat_visual.transform.translate, [-1., 0., 0., 0.])

    d.y_min = 0.
    d.y_max = +2

    np.testing.assert_equal(scat_visual.transform.scale, [2., 1., 1., 1.])
    np.testing.assert_equal(scat_visual.transform.translate, [-1., -1., 0., 0.])

    d.z_min = -8
    d.z_max = +0

    np.testing.assert_equal(scat_visual.transform.scale, [2., 1., 0.25, 1.])
    np.testing.assert_equal(scat_visual.transform.translate, [-1, -1., 1., 0.])

    d.x_stretch = 10

    np.testing.assert_equal(scat_visual.transform.scale, [20., 1., 0.25, 1.])
    np.testing.assert_equal(scat_visual.transform.translate, [-10, -1., 1., 0.])

    d.y_stretch = 5

    np.testing.assert_equal(scat_visual.transform.scale, [20., 5., 0.25, 1.])
    np.testing.assert_equal(scat_visual.transform.translate, [-10, -5., 1., 0.])

    d.z_stretch = 4

    np.testing.assert_equal(scat_visual.transform.scale, [20., 5., 1, 1.])
    np.testing.assert_equal(scat_visual.transform.translate, [-10, -5., 4., 0.])
<commit_msg>Fixed test<commit_after>import numpy as np

from ...extern.vispy import scene

from ..viewer_options import VispyOptionsWidget
from ..vispy_widget import VispyWidgetHelper

def test_vispy_widget():

    w = VispyWidgetHelper()
    d = VispyOptionsWidget(vispy_widget=w)

    w.canvas.native.show()
    d.show()

    # Try adding marker visuals to the scene
    positions = np.random.random((1000, 3))
    scat_visual = scene.visuals.Markers()
    scat_visual.set_data(positions, symbol='disc', edge_color=None, face_color='red')
    w.add_data_visual(scat_visual)

    d.set_limits(-1., 1., -1., 1., -1., 1.)

    np.testing.assert_equal(scat_visual.transform.scale, [1., 1., 1., 1.])
    np.testing.assert_equal(scat_visual.transform.translate, [0., 0., 0., 0.])

    d.x_min = 0
    d.x_max = +1

    np.testing.assert_equal(scat_visual.transform.scale, [2., 1., 1., 1.])
    np.testing.assert_equal(scat_visual.transform.translate, [-1., 0., 0., 0.])

    d.y_min = 0.
    d.y_max = +2

    np.testing.assert_equal(scat_visual.transform.scale, [2., 1., 1., 1.])
    np.testing.assert_equal(scat_visual.transform.translate, [-1., -1., 0., 0.])

    d.z_min = -8
    d.z_max = +0

    np.testing.assert_equal(scat_visual.transform.scale, [2., 1., 0.25, 1.])
    np.testing.assert_equal(scat_visual.transform.translate, [-1, -1., 1., 0.])

    d.x_stretch = 10

    np.testing.assert_equal(scat_visual.transform.scale, [20., 1., 0.25, 1.])
    np.testing.assert_equal(scat_visual.transform.translate, [-10, -1., 1., 0.])

    d.y_stretch = 5

    np.testing.assert_equal(scat_visual.transform.scale, [20., 5., 0.25, 1.])
    np.testing.assert_equal(scat_visual.transform.translate, [-10, -5., 1., 0.])

    d.z_stretch = 4

    np.testing.assert_equal(scat_visual.transform.scale, [20., 5., 1, 1.])
    np.testing.assert_equal(scat_visual.transform.translate, [-10, -5., 4., 0.])
<|endoftext|>


<commit_before>''' -- imports from installed packages -- '''
from django.template import Library

''' -- imports from application folders/files -- '''
from gnowsys_ndf.settings import GAPPS
from gnowsys_ndf.ndf.models import *
from gnowsys_ndf.ndf.views.methods import check_existing_group
import re
from django.contrib.auth.models import User
from django.shortcuts import render_to_response, render
from gnowsys_ndf.ndf.views.methods import get_drawers
from django.template import RequestContext,loader
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.http import Http404
register = Library()
db = get_database()

@register.simple_tag
def get_all_users_to_invite():
  try:
    inv_users={}
    users=User.objects.all()
    for each in users:
      inv_users[each.username.__str__()+"<"+each.email.__str__()+">"]=each.id
    return inv_users
  except Exception as e:
    print str(e)

@register.inclusion_tag('ndf/twist_replies.html')
def get_reply(thread,parent,ind,token,user):
  return {'thread':thread,'reply': parent,'user':user,'indent':ind+10,'csrf_token':token,'eachrep':parent}

@register.assignment_tag
def get_all_replies(parent):
   gs_collection = db[Node.collection_name]
   ex_reply=""
   if parent:
     ex_reply=gs_collection.GSystem.find({'$and':[{'_type':'GSystem'},{'prior_node':ObjectId(parent._id)}]})
     ex_reply.sort('created_at',-1)
   return ex_reply

@register.inclusion_tag('ndf/drawer_widget.html')
def edit_drawer_widget(field, group_name, node, checked=None):

  drawers = None
  drawer1 = None
  drawer2 = None

  if node :
    if field == "collection":
      if checked == "Quiz":
        checked = "QuizItem"
      else:
        checked = None

      drawers = get_drawers(group_name, node._id, node.collection_set, checked)

    elif field == "prior_node":
      checked = None
      drawers = get_drawers(group_name, node._id, node.prior_node, checked)

    drawer1 = drawers['1']
    drawer2 = drawers['2']

  else:
    if field == "collection" and checked == "Quiz":
      checked = "QuizItem"
    else:
      # To make the collection work as Heterogenous one, by default
      checked = None

    drawer1 = get_drawers(group_name, None, [], checked)

  return {'template': 'ndf/drawer_widget.html', 'widget_for': field, 'drawer1': drawer1, 'drawer2': drawer2, 'group_name': group_name}

@register.inclusion_tag('ndf/gapps_menubar.html')
def get_gapps_menubar(group_name, selectedGapp):
  """Get Gapps menu-bar
  """

  collection = db[Node.collection_name]
  gst_cur = collection.Node.find({'_type': 'GSystemType', 'name': {'$in': GAPPS}})

  gapps = {}
  i = 0;
  for app in gst_cur:
    i = i+1;
    gapps[i] = {'id': app._id, 'name': app.name.lower()}

  selectedGapp = selectedGapp.split("/")[2]

  return {'template': 'ndf/gapps_menubar.html', 'gapps': gapps, 'selectedGapp':selectedGapp, 'group_name': group_name}

@register.assignment_tag
def get_forum_twists(forum):
  gs_collection = db[Node.collection_name]
  ret_replies=[]
  exstng_reply=gs_collection.GSystem.find({'$and':[{'_type':'GSystem'},{'prior_node':ObjectId(forum._id)}]})
  exstng_reply.sort('created_at')
  for each in exstng_reply:
    ret_replies.append(each)
  return ret_replies

lp=[]
def get_rec_objs(ob_id):
  lp.append(ob_id)
  exstng_reply=gs_collection.GSystem.find({'$and':[{'_type':'GSystem'},{'prior_node':ObjectId(ob_id)}]})
  for each in exstng_reply:
    get_rec_objs(each)
  return lp

@register.assignment_tag
def get_twist_replies(twist):
  gs_collection = db[Node.collection_name]
  ret_replies={}
  entries=[]
  exstng_reply=gs_collection.GSystem.find({'$and':[{'_type':'GSystem'},{'prior_node':ObjectId(twist._id)}]})
  for each in exstng_reply:
    lst=get_rec_objs(each)

  return ret_replies

@register.assignment_tag
def check_user_join(request,groupname):
  if not request.user:
    return "null"
  user=request.user
  usern=User.objects.filter(username=user)
  if usern:
    usern=User.objects.get(username=user)
    user_id=usern.id
  else:
    return "null"
  col_Group = db[Group.collection_name]
  colg = col_Group.Group.one({'$and':[{'_type':'Group'},{'name':groupname}]})
  if colg:
    if colg.created_by == user_id:
      return "author"
    if colg.author_set:
      if user_id in colg.author_set:
        return "joined"
      else:
        return "not"
    else:
      return "not"
  else:
    return "nullobj"

@register.assignment_tag
def check_group(groupname):
  fl = check_existing_group(groupname)
  return fl

@register.assignment_tag
def get_group_name(groupurl):
  sp=groupurl.split("/",2)
  if len(sp)<=1:
    return "home"
  if sp[1]:
    chsp = check_existing_group(sp[1])
    if chsp:
      return sp[1]
    else:
      return "home"
  else:
      return "home"

@register.assignment_tag
def get_existing_groups():
  group = []
  col_Group = db[Group.collection_name]
  colg = col_Group.Group.find({'_type': u'Group'})
  colg.sort('name')
  gr = list(colg)
  for items in gr:
    if items.name:
      group.append(items)
  return group

@register.assignment_tag
def get_existing_groups_excluded(grname):
  group = []
  col_Group = db[Group.collection_name]
  colg = col_Group.Group.find({'_type':u'Group','group_type':"PUBLIC"})
  colg.sort('name')
  gr=list(colg)
  for items in gr:
    if items.name != grname:
      group.append(items)
  if not group:
    return "None"
  return group

@register.assignment_tag
def get_group_policy(group_name,user):
  try:
    policy = ""
    col_Group = db[Group.collection_name]
    colg = col_Group.Group.one({'$and':[{'_type':'Group'},{'name':group_name}]})
    if colg:
      policy = str(colg.subscription_policy)
  except:
    pass
  return policy

@register.assignment_tag
def get_user_group(user):

  group = []
  col_Group = db[Group.collection_name]

  colg = col_Group.Group.find({ '_type': u'Group',
                                'name': {'$nin': ['home']},
                                '$or':[{'created_by':user.id}, {'group_type':'PUBLIC'},{'author_set':user.id}]
                              })

  for items in colg:

      group.append(items)
  if not group:
    return "None"
  return group

@register.assignment_tag
def get_group_type(grname,user):
  col_Group = db[Group.collection_name]
  grname1 = re.sub('/', '', grname)
  #get all the strings after and before the /
  split_result = re.split(r'[/=]', grname)

  # check wheather Group exist in the database
  colg=col_Group.Group.one({'_type': 'Group','name':split_result[1]})
  #check if Group exist in the database
  if colg is not None:
	# Check is user is logged in
	if  user.id:
		# condition for group accesseble to logged user
	  	if colg.group_type=="PUBLIC" or colg.created_by==user.id or colg.author_set==user.id:
			return "allowed"
		else:
			raise Http404
	else:
		#condition for groups,accesseble to not logged users
		if colg.group_type=="PUBLIC":

			return "allowed"
                else:
			print "redirection"
			raise Http404
  else:

	return "pass"

'''this template function is used to get the user object from template'''
@register.assignment_tag
def get_user_object(user_id):
  user_obj=""
  try:
    user_obj=User.objects.get(id=user_id)
  except Exception as e:
    print "User Not found in User Table",e
  return user_obj

'''this template function is used to get the user object from template'''
@register.assignment_tag
def get_grid_fs_object(f):
  '''get the gridfs object by object id'''
  grid_fs_obj = ""
  try:
    file_collection = db[File.collection_name]
    file_obj = file_collection.File.one({'_id':ObjectId(f['_id'])})
    grid_fs_obj =  file_obj.fs.files.get(file_obj.fs_file_ids[0])
  except Exception as e:
    print "Object does not exist", e
  return grid_fs_obj

<commit_msg>Url filtering & Group policies modified regarding join/edit group<commit_after>''' -- imports from installed packages -- '''
from django.template import Library

''' -- imports from application folders/files -- '''
from gnowsys_ndf.settings import GAPPS
from gnowsys_ndf.ndf.models import *
from gnowsys_ndf.ndf.views.methods import check_existing_group
import re
from django.contrib.auth.models import User
from django.shortcuts import render_to_response, render
from gnowsys_ndf.ndf.views.methods import get_drawers
from django.template import RequestContext,loader
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.http import Http404
register = Library()
db = get_database()

@register.simple_tag
def get_all_users_to_invite():
  try:
    inv_users={}
    users=User.objects.all()
    for each in users:
      inv_users[each.username.__str__()+"<"+each.email.__str__()+">"]=each.id
    return inv_users
  except Exception as e:
    print str(e)

@register.inclusion_tag('ndf/twist_replies.html')
def get_reply(thread,parent,ind,token,user):
  return {'thread':thread,'reply': parent,'user':user,'indent':ind+10,'csrf_token':token,'eachrep':parent}

@register.assignment_tag
def get_all_replies(parent):
   gs_collection = db[Node.collection_name]
   ex_reply=""
   if parent:
     ex_reply=gs_collection.GSystem.find({'$and':[{'_type':'GSystem'},{'prior_node':ObjectId(parent._id)}]})
     ex_reply.sort('created_at',-1)
   return ex_reply

@register.inclusion_tag('ndf/drawer_widget.html')
def edit_drawer_widget(field, group_name, node, checked=None):

  drawers = None
  drawer1 = None
  drawer2 = None

  if node :
    if field == "collection":
      if checked == "Quiz":
        checked = "QuizItem"
      else:
        checked = None

      drawers = get_drawers(group_name, node._id, node.collection_set, checked)

    elif field == "prior_node":
      checked = None
      drawers = get_drawers(group_name, node._id, node.prior_node, checked)

    drawer1 = drawers['1']
    drawer2 = drawers['2']

  else:
    if field == "collection" and checked == "Quiz":
      checked = "QuizItem"
    else:
      # To make the collection work as Heterogenous one, by default
      checked = None

    drawer1 = get_drawers(group_name, None, [], checked)

  return {'template': 'ndf/drawer_widget.html', 'widget_for': field, 'drawer1': drawer1, 'drawer2': drawer2, 'group_name': group_name}

@register.inclusion_tag('ndf/gapps_menubar.html')
def get_gapps_menubar(group_name, selectedGapp):
  """Get Gapps menu-bar
  """

  collection = db[Node.collection_name]
  gst_cur = collection.Node.find({'_type': 'GSystemType', 'name': {'$in': GAPPS}})

  gapps = {}
  i = 0;
  for app in gst_cur:
    i = i+1;
    gapps[i] = {'id': app._id, 'name': app.name.lower()}

  selectedGapp = selectedGapp.split("/")[2]

  return {'template': 'ndf/gapps_menubar.html', 'gapps': gapps, 'selectedGapp':selectedGapp, 'group_name': group_name}

@register.assignment_tag
def get_forum_twists(forum):
  gs_collection = db[Node.collection_name]
  ret_replies=[]
  exstng_reply=gs_collection.GSystem.find({'$and':[{'_type':'GSystem'},{'prior_node':ObjectId(forum._id)}]})
  exstng_reply.sort('created_at')
  for each in exstng_reply:
    ret_replies.append(each)
  return ret_replies

lp=[]
def get_rec_objs(ob_id):
  lp.append(ob_id)
  exstng_reply=gs_collection.GSystem.find({'$and':[{'_type':'GSystem'},{'prior_node':ObjectId(ob_id)}]})
  for each in exstng_reply:
    get_rec_objs(each)
  return lp

@register.assignment_tag
def get_twist_replies(twist):
  gs_collection = db[Node.collection_name]
  ret_replies={}
  entries=[]
  exstng_reply=gs_collection.GSystem.find({'$and':[{'_type':'GSystem'},{'prior_node':ObjectId(twist._id)}]})
  for each in exstng_reply:
    lst=get_rec_objs(each)

  return ret_replies

@register.assignment_tag
def check_user_join(request,groupname):
  if not request.user:
    return "null"
  user=request.user
  usern=User.objects.filter(username=user)
  if usern:
    usern=User.objects.get(username=user)
    user_id=usern.id
  else:
    return "null"
  col_Group = db[Group.collection_name]
  colg = col_Group.Group.one({'$and':[{'_type':'Group'},{'name':groupname}]})
  if colg:
    if colg.created_by == user_id:
      return "author"
    if colg.author_set:
      if user_id in colg.author_set:
        return "joined"
      else:
        return "not"
    else:
      return "not"
  else:
    return "nullobj"

@register.assignment_tag
def check_group(groupname):
  fl = check_existing_group(groupname)
  return fl

@register.assignment_tag
def get_group_name(groupurl):
  sp=groupurl.split("/",2)
  if len(sp)<=1:
    return "home"
  if sp[1]:
    chsp = check_existing_group(sp[1])
    if chsp:
      return sp[1]
    else:
      return "home"
  else:
      return "home"

@register.assignment_tag
def get_existing_groups():
  group = []
  col_Group = db[Group.collection_name]
  colg = col_Group.Group.find({'_type': u'Group'})
  colg.sort('name')
  gr = list(colg)
  for items in gr:
    if items.name:
      group.append(items)
  return group

@register.assignment_tag
def get_existing_groups_excluded(grname):
  group = []
  col_Group = db[Group.collection_name]
  colg = col_Group.Group.find({'_type':u'Group','group_type':"PUBLIC"})
  colg.sort('name')
  gr=list(colg)
  for items in gr:
    if items.name != grname:
      group.append(items)
  if not group:
    return "None"
  return group

@register.assignment_tag
def get_group_policy(group_name,user):
  try:
    policy = ""
    col_Group = db[Group.collection_name]
    colg = col_Group.Group.one({'$and':[{'_type':'Group'},{'name':group_name}]})
    if colg:
      policy = str(colg.subscription_policy)
  except:
    pass
  return policy

@register.assignment_tag
def get_user_group(user):

  group = []
  col_Group = db[Group.collection_name]

  colg = col_Group.Group.find({ '_type': u'Group',
                                'name': {'$nin': ['home']},
                                '$or':[{'created_by':user.id}, {'group_type':'PUBLIC'},{'author_set':user.id}]
                              })

  for items in colg:

      group.append(items)
  if not group:
    return "None"
  return group

@register.assignment_tag
def get_group_type(grname,user):
  col_Group = db[Group.collection_name]

  #get all the strings after and before the /
  split_result = re.split(r'[/=]', grname)

  # check wheather Group exist in the database
  colg=col_Group.Group.one({'_type': 'Group','name':split_result[1]})
  #Query for implemting the same senario with ObjectId instead of Group name
  #  colg=col_Group.Group.one({'_type': 'Group','_id':split_result[1]})

  #check if Group exist in the database
  if colg is not None:
	# Check is user is logged in
	if  user.id:
		# condition for group accesseble to logged user
	  	if colg.group_type=="PUBLIC" or colg.created_by==user.id or user.id in colg.author_set:
			return "allowed"
		else:
			raise Http404
	else:
		#condition for groups,accesseble to not logged users
		if colg.group_type=="PUBLIC":

			return "allowed"
                else:
			print "redirection"
			raise Http404
  else:

	return "pass"

'''this template function is used to get the user object from template'''
@register.assignment_tag
def get_user_object(user_id):
  user_obj=""
  try:
    user_obj=User.objects.get(id=user_id)
  except Exception as e:
    print "User Not found in User Table",e
  return user_obj

'''this template function is used to get the user object from template'''
@register.assignment_tag
def get_grid_fs_object(f):
  '''get the gridfs object by object id'''
  grid_fs_obj = ""
  try:
    file_collection = db[File.collection_name]
    file_obj = file_collection.File.one({'_id':ObjectId(f['_id'])})
    grid_fs_obj =  file_obj.fs.files.get(file_obj.fs_file_ids[0])
  except Exception as e:
    print "Object does not exist", e
  return grid_fs_obj

<|endoftext|>