<commit_before>from __future__ import with_statement

import locale
locale.setlocale(locale.LC_ALL, '')

import pkg_resources
__version__ = pkg_resources.get_distribution('jarn.viewdoc').version

import sys
import os
import getopt
import webbrowser
import ConfigParser
import functools

from os.path import abspath, expanduser, dirname, basename
from os.path import split, join, isdir, isfile
from subprocess import Popen, PIPE
from docutils.core import publish_string

if sys.version_info[0] >= 3:
    import builtins

VERSION = "jarn.viewdoc %s" % __version__
USAGE = "Try 'viewdoc --help' for more information"

HELP = """\
Usage: viewdoc [options] [rst-file|egg-dir]

Documentation viewer

Options:
  -s style, --style=style, or --style
                      Select the custom style added to the HTML output.
                      Used to override the configuration file setting of
                      the same name.

  -l, --list-styles   List available styles and exit.
  -h, --help          Print this help message and exit.
  -v, --version       Print the version string and exit.

  rst-file            reST file to view.
  egg-dir             Package whose long description to view. Defaults to
                      the current working directory.
"""

PLAIN = """\
<style type="text/css">
body { margin-left: 10em; margin-right: 10em; }
</style>
"""

PYPI = """\
<link rel="stylesheet" href="http://www.python.org/styles/styles.css" type="text/css" />
<style type="text/css">
body { margin-left: 10em; margin-right: 10em; font-size: 95%; }
a:link { text-decoration: none; color: #0000aa; }
a:visited { text-decoration: none; color: #551a8b; }
a.reference { border-bottom: 1px dashed #cccccc; }
</style>
"""

SMALL = """\
<link rel="stylesheet" href="http://www.python.org/styles/styles.css" type="text/css" />
<style type="text/css">
body { margin-left: 10em; margin-right: 10em; font-size: 90%; }
a:link { text-decoration: none; color: #0000aa; }
a:visited { text-decoration: none; color: #551a8b; }
a.reference { border-bottom: 1px dashed #cccccc; }
</style>
"""

DEFAULT_CONFIG = """\
[viewdoc]
style = pypi

[styles]
plain =
    <style type="text/css">
    body { margin-left: 10em; margin-right: 10em; }
    </style>
pypi =
    <link rel="stylesheet" href="http://www.python.org/styles/styles.css" type="text/css" />
    <style type="text/css">
    body { margin-left: 10em; margin-right: 10em; font-size: 95%; }
    a:link { text-decoration: none; color: #0000aa; }
    a:visited { text-decoration: none; color: #551a8b; }
    a.reference { border-bottom: 1px dashed #cccccc; }
    </style>
small =
    <link rel="stylesheet" href="http://www.python.org/styles/styles.css" type="text/css" />
    <style type="text/css">
    body { margin-left: 10em; margin-right: 10em; font-size: 90%; }
    a:link { text-decoration: none; color: #0000aa; }
    a:visited { text-decoration: none; color: #551a8b; }
    a.reference { border-bottom: 1px dashed #cccccc; }
    </style>
"""

def msg_exit(msg, rc=0):
    """Print msg to stdout and exit with rc.
    """
    print msg
    sys.exit(rc)

def err_exit(msg, rc=1):
    """Print msg to stderr and exit with rc.
    """
    print >>sys.stderr, msg
    sys.exit(rc)

def warn(msg):
    """Print warning msg to stderr.
    """
    print >>sys.stderr, 'WARNING:', msg

class changedir(object):
    """Change directory."""

    def __init__(self, dir):
        self.old = os.getcwd()
        self.dir = dir or self.old

    def __enter__(self):
        os.chdir(self.dir)

    def __exit__(self, *ignored):
        os.chdir(self.old)

class Python(object):

    def __init__(self):
        self.python = sys.executable
        self.version_info = sys.version_info

    def __str__(self):
        return self.python

    def is_valid_python(self):
        return (self.version_info[:2] >= (2, 5))

    def check_valid_python(self):
        if not self.is_valid_python():
            err_exit('Python >= 2.5 required')

class Process(object):

    def __init__(self, env=None):
        self.env = env

    def popen(self, cmd):
        """Execute an external command and return (rc, output).
        """
        process = Popen(cmd, shell=True, stdout=PIPE, env=self.env)
        stdoutdata, stderrdata = process.communicate()
        return process.returncode, stdoutdata

class Setuptools(object):

    def __init__(self):
        self.process = Process(env=self.get_env())
        self.python = Python()

    def get_env(self):
        # Make sure setuptools is found if mkrelease has
        # been installed with zc.buildout
        path = []
        for name in ('setuptools',):
            try:
                dist = pkg_resources.get_distribution(name)
            except pkg_resources.DistributionNotFound:
                continue
            path.append(dist.location)
        env = os.environ.copy()
        env['PYTHONPATH'] = ':'.join(path)
        return env

    def is_valid_package(self):
        return isfile('setup.py')

    def check_valid_package(self):
        if not self.is_valid_package():
            err_exit('No setup.py found in %s' % os.getcwd())

    def get_long_description(self):
        rc, long_description = self.process.popen(
            '"%s" setup.py --long-description' % self.python)
        if rc != 0:
            err_exit('Bad setup.py')
        if sys.version_info[0] >= 3:
            try:
                return long_description.decode('utf-8')
            except UnicodeDecodeError, e:
                err_exit('Error decoding long description: %s' % (e,))
        return long_description

class Docutils(object):

    def read_file(self, infile):
        """Read a reST file into a string.
        """
        if sys.version_info[0] >= 3:
            open = functools.partial(builtins.open, encoding='utf-8')
        try:
            with open(infile, 'rt') as file:
                return file.read()
        except UnicodeDecodeError, e:
            err_exit('Error decoding %s: %s' % (infile, e))
        except (IOError, OSError), e:
            err_exit('Error reading %s: %s' % (infile, e.strerror or e))

    def write_file(self, html, outfile):
        """Write an HTML string to a file.
        """
        if sys.version_info[0] >= 3:
            open = functools.partial(builtins.open, encoding='utf-8')
        try:
            with open(outfile, 'wt') as file:
                file.write(html)
        except (IOError, OSError), e:
            err_exit('Error writing %s: %s' % (outfile, e.strerror or e))

    def convert_string(self, rest):
        """Convert a reST string to an HTML string.
        """
        try:
            html = publish_string(rest, writer_name='html')
        except SystemExit, e:
            err_exit('HTML conversion failed with error: %s' % e.code)
        else:
            if sys.version_info[0] >= 3:
                return html.decode('utf-8')
            return html

    def strip_xml_header(self, html):
        """Strip any <?xml version="1.0" encoding="utf-8" ?> header.
        """
        if html.startswith('<?xml '):
            return html.split('\n', 1)[1]
        return html

    def apply_styles(self, html, styles):
        """Insert style information into the HTML string.
        """
        index = html.find('</head>')
        if index >= 0:
            return ''.join((html[:index], styles, html[index:]))
        return html

    def publish_string(self, rest, outfile, styles=''):
        """Render a reST string as HTML.
        """
        html = self.convert_string(rest)
        html = self.strip_xml_header(html)
        html = self.apply_styles(html, styles)
        self.write_file(html, outfile)
        return outfile

    def publish_file(self, infile, outfile, styles=''):
        """Render a reST file as HTML.
        """
        rest = self.read_file(infile)
        return self.publish_string(rest, outfile, styles)

class errors2warnings(object):
    """Turn ConfigParser.Errors into warnings."""

    def __enter__(self):
        pass

    def __exit__(self, type, value, tb):
        if isinstance(value, ConfigParser.Error):
            warn(str(value))
            return True

class Defaults(object):

    def __init__(self):
        """Read the config file.
        """
        filename = expanduser('~/.viewdoc')
        if not isfile(filename):
            self.write_default_config(filename)

        parser = ConfigParser.ConfigParser()
        with errors2warnings():
            parser.read(filename)

        def get(section, key, default=None):
            if parser.has_option(section, key):
                with errors2warnings():
                    return parser.get(section, key, raw=True)
            return default

        def getitems(section, default=None):
            if parser.has_section(section):
                with errors2warnings():
                    return parser.items(section, raw=True)
            return default

        self.known_styles = {}
        for key, value in getitems('styles', []):
            self.known_styles[key] = value.strip()+'\n'
        self.known_styles.setdefault('pypi', PYPI)

        self.default_style = get('viewdoc', 'style', 'pypi').strip()
        if self.default_style not in self.known_styles:
            self.default_style = 'pypi'

        self.styles = self.known_styles.get(self.default_style, '')

    def write_default_config(self, filename):
        """Write the default config file.
        """
        if sys.version_info[0] >= 3:
            open = functools.partial(builtins.open, encoding='utf-8')
        try:
            with open(filename, 'wt') as file:
                file.write(DEFAULT_CONFIG)
        except (IOError, OSError), e:
            print >>sys.stderr, 'Error writing %s: %s' % (filename, e.strerror or e)

class DocumentationViewer(object):

    def __init__(self, args):
        """Set defaults.
        """
        self.defaults = Defaults()
        self.python = Python()
        self.setuptools = Setuptools()
        self.docutils = Docutils()
        self.styles = self.defaults.styles
        self.args = args

    def parse_options(self, args):
        """Parse command line options.
        """
        style_names = tuple(self.defaults.known_styles)
        style_opts = tuple('--'+x for x in style_names)

        try:
            options, args = getopt.gnu_getopt(args, 'hls:v',
                ('help', 'style=', 'version', 'list-styles') + style_names)
        except getopt.GetoptError, e:
            err_exit('viewdoc: %s\n%s' % (e.msg, USAGE))

        for name, value in options:
            if name in ('-s', '--style'):
                self.styles = self.defaults.known_styles.get(value, '')
            elif name in style_opts:
                self.styles = self.defaults.known_styles.get(name[2:], '')
            elif name in ('-l', '--list-styles'):
                self.list_styles()
            elif name in ('-h', '--help'):
                msg_exit(HELP)
            elif name in ('-v', '--version'):
                msg_exit(VERSION)

        if len(args) > 1:
            err_exit('viewdoc: too many arguments\n%s' % USAGE)
        return args

    def list_styles(self):
        """Print available styles and exit.
        """
        for style in sorted(self.defaults.known_styles):
            if style == self.defaults.default_style:
                print style, '(default)'
            else:
                print style
        sys.exit(0)

    def render_file(self, filename):
        """Convert a reST file to HTML.
        """
        dirname, basename = split(filename)
        with changedir(dirname):
            infile = abspath(basename)
            outfile = abspath('.%s.html' % basename)
            self.docutils.publish_file(infile, outfile, self.styles)
            return outfile

    def render_long_description(self, dirname):
        """Convert a package's long description to HTML.
        """
        with changedir(dirname):
            self.setuptools.check_valid_package()
            long_description = self.setuptools.get_long_description()
            outfile = abspath('.long-description.html')
            self.docutils.publish_string(long_description, outfile, self.styles)
            return outfile

    def run(self):
        """Render and display Python package documentation.
        """
        self.python.check_valid_python()

        args = self.parse_options(self.args)
        if args:
            arg = args[0]
        else:
            arg = os.curdir
        if arg:
            arg = expanduser(arg)

        if isfile(arg):
            outfile = self.render_file(arg)
        elif isdir(arg):
            outfile = self.render_long_description(arg)
        else:
            err_exit('No such file or directory: %s' % arg)

        webbrowser.open('file://%s' % outfile)

def main(args=None):
    if args is None:
        args = sys.argv[1:]
    try:
        DocumentationViewer(args).run()
    except SystemExit, e:
        return e.code
    return 0

if __name__ == '__main__':
    sys.exit(main())

<commit_msg>That didn't work for Python 2.<commit_after>from __future__ import with_statement

import locale
locale.setlocale(locale.LC_ALL, '')

import pkg_resources
__version__ = pkg_resources.get_distribution('jarn.viewdoc').version

import sys
import os
import getopt
import webbrowser
import ConfigParser

from os.path import abspath, expanduser, dirname, basename
from os.path import split, join, isdir, isfile
from subprocess import Popen, PIPE
from contextlib import closing
from docutils.core import publish_string

VERSION = "jarn.viewdoc %s" % __version__
USAGE = "Try 'viewdoc --help' for more information"

HELP = """\
Usage: viewdoc [options] [rst-file|egg-dir]

Documentation viewer

Options:
  -s style, --style=style, or --style
                      Select the custom style added to the HTML output.
                      Used to override the configuration file setting of
                      the same name.

  -l, --list-styles   List available styles and exit.
  -h, --help          Print this help message and exit.
  -v, --version       Print the version string and exit.

  rst-file            reST file to view.
  egg-dir             Package whose long description to view. Defaults to
                      the current working directory.
"""

PLAIN = """\
<style type="text/css">
body { margin-left: 10em; margin-right: 10em; }
</style>
"""

PYPI = """\
<link rel="stylesheet" href="http://www.python.org/styles/styles.css" type="text/css" />
<style type="text/css">
body { margin-left: 10em; margin-right: 10em; font-size: 95%; }
a:link { text-decoration: none; color: #0000aa; }
a:visited { text-decoration: none; color: #551a8b; }
a.reference { border-bottom: 1px dashed #cccccc; }
</style>
"""

SMALL = """\
<link rel="stylesheet" href="http://www.python.org/styles/styles.css" type="text/css" />
<style type="text/css">
body { margin-left: 10em; margin-right: 10em; font-size: 90%; }
a:link { text-decoration: none; color: #0000aa; }
a:visited { text-decoration: none; color: #551a8b; }
a.reference { border-bottom: 1px dashed #cccccc; }
</style>
"""

DEFAULT_CONFIG = """\
[viewdoc]
style = pypi

[styles]
plain =
    <style type="text/css">
    body { margin-left: 10em; margin-right: 10em; }
    </style>
pypi =
    <link rel="stylesheet" href="http://www.python.org/styles/styles.css" type="text/css" />
    <style type="text/css">
    body { margin-left: 10em; margin-right: 10em; font-size: 95%; }
    a:link { text-decoration: none; color: #0000aa; }
    a:visited { text-decoration: none; color: #551a8b; }
    a.reference { border-bottom: 1px dashed #cccccc; }
    </style>
small =
    <link rel="stylesheet" href="http://www.python.org/styles/styles.css" type="text/css" />
    <style type="text/css">
    body { margin-left: 10em; margin-right: 10em; font-size: 90%; }
    a:link { text-decoration: none; color: #0000aa; }
    a:visited { text-decoration: none; color: #551a8b; }
    a.reference { border-bottom: 1px dashed #cccccc; }
    </style>
"""

def msg_exit(msg, rc=0):
    """Print msg to stdout and exit with rc.
    """
    print msg
    sys.exit(rc)

def err_exit(msg, rc=1):
    """Print msg to stderr and exit with rc.
    """
    print >>sys.stderr, msg
    sys.exit(rc)

def warn(msg):
    """Print warning msg to stderr.
    """
    print >>sys.stderr, 'WARNING:', msg

class changedir(object):
    """Change directory."""

    def __init__(self, dir):
        self.old = os.getcwd()
        self.dir = dir or self.old

    def __enter__(self):
        os.chdir(self.dir)

    def __exit__(self, *ignored):
        os.chdir(self.old)

class Python(object):

    def __init__(self):
        self.python = sys.executable
        self.version_info = sys.version_info

    def __str__(self):
        return self.python

    def is_valid_python(self):
        return (self.version_info[:2] >= (2, 5))

    def check_valid_python(self):
        if not self.is_valid_python():
            err_exit('Python >= 2.5 required')

class Process(object):

    def __init__(self, env=None):
        self.env = env

    def popen(self, cmd):
        """Execute an external command and return (rc, output).
        """
        process = Popen(cmd, shell=True, stdout=PIPE, env=self.env)
        stdoutdata, stderrdata = process.communicate()
        return process.returncode, stdoutdata

class Setuptools(object):

    def __init__(self):
        self.process = Process(env=self.get_env())
        self.python = Python()

    def get_env(self):
        # Make sure setuptools is found if mkrelease has
        # been installed with zc.buildout
        path = []
        for name in ('setuptools',):
            try:
                dist = pkg_resources.get_distribution(name)
            except pkg_resources.DistributionNotFound:
                continue
            path.append(dist.location)
        env = os.environ.copy()
        env['PYTHONPATH'] = ':'.join(path)
        return env

    def is_valid_package(self):
        return isfile('setup.py')

    def check_valid_package(self):
        if not self.is_valid_package():
            err_exit('No setup.py found in %s' % os.getcwd())

    def get_long_description(self):
        rc, long_description = self.process.popen(
            '"%s" setup.py --long-description' % self.python)
        if rc != 0:
            err_exit('Bad setup.py')
        if sys.version_info[0] >= 3:
            try:
                return long_description.decode('utf-8')
            except UnicodeDecodeError, e:
                err_exit('Error decoding long description: %s' % (e,))
        return long_description

class Docutils(object):

    def read_file(self, infile):
        """Read a reST file into a string.
        """
        try:
            if sys.version_info[0] >= 3:
                file = open(infile, 'rt', encoding='utf-8')
            else:
                file = open(infile, 'rt')
            with closing(file):
                return file.read()
        except UnicodeDecodeError, e:
            err_exit('Error decoding %s: %s' % (infile, e))
        except (IOError, OSError), e:
            err_exit('Error reading %s: %s' % (infile, e.strerror or e))

    def write_file(self, html, outfile):
        """Write an HTML string to a file.
        """
        try:
            if sys.version_info[0] >= 3:
                file = open(outfile, 'wt', encoding='utf-8')
            else:
                file = open(outfile, 'wt')
            with closing(file):
                file.write(html)
        except (IOError, OSError), e:
            err_exit('Error writing %s: %s' % (outfile, e.strerror or e))

    def convert_string(self, rest):
        """Convert a reST string to an HTML string.
        """
        try:
            html = publish_string(rest, writer_name='html')
        except SystemExit, e:
            err_exit('HTML conversion failed with error: %s' % e.code)
        else:
            if sys.version_info[0] >= 3:
                return html.decode('utf-8')
            return html

    def strip_xml_header(self, html):
        """Strip any <?xml version="1.0" encoding="utf-8" ?> header.
        """
        if html.startswith('<?xml '):
            return html.split('\n', 1)[1]
        return html

    def apply_styles(self, html, styles):
        """Insert style information into the HTML string.
        """
        index = html.find('</head>')
        if index >= 0:
            return ''.join((html[:index], styles, html[index:]))
        return html

    def publish_string(self, rest, outfile, styles=''):
        """Render a reST string as HTML.
        """
        html = self.convert_string(rest)
        html = self.strip_xml_header(html)
        html = self.apply_styles(html, styles)
        self.write_file(html, outfile)
        return outfile

    def publish_file(self, infile, outfile, styles=''):
        """Render a reST file as HTML.
        """
        rest = self.read_file(infile)
        return self.publish_string(rest, outfile, styles)

class errors2warnings(object):
    """Turn ConfigParser.Errors into warnings."""

    def __enter__(self):
        pass

    def __exit__(self, type, value, tb):
        if isinstance(value, ConfigParser.Error):
            warn(str(value))
            return True

class Defaults(object):

    def __init__(self):
        """Read the config file.
        """
        filename = expanduser('~/.viewdoc')
        if not isfile(filename):
            self.write_default_config(filename)

        parser = ConfigParser.ConfigParser()
        with errors2warnings():
            parser.read(filename)

        def get(section, key, default=None):
            if parser.has_option(section, key):
                with errors2warnings():
                    return parser.get(section, key, raw=True)
            return default

        def getitems(section, default=None):
            if parser.has_section(section):
                with errors2warnings():
                    return parser.items(section, raw=True)
            return default

        self.known_styles = {}
        for key, value in getitems('styles', []):
            self.known_styles[key] = value.strip()+'\n'
        self.known_styles.setdefault('pypi', PYPI)

        self.default_style = get('viewdoc', 'style', 'pypi').strip()
        if self.default_style not in self.known_styles:
            self.default_style = 'pypi'

        self.styles = self.known_styles.get(self.default_style, '')

    def write_default_config(self, filename):
        """Write the default config file.
        """
        try:
            with open(filename, 'wt') as file:
                file.write(DEFAULT_CONFIG)
        except (IOError, OSError), e:
            print >>sys.stderr, 'Error writing %s: %s' % (filename, e.strerror or e)

class DocumentationViewer(object):

    def __init__(self, args):
        """Set defaults.
        """
        self.defaults = Defaults()
        self.python = Python()
        self.setuptools = Setuptools()
        self.docutils = Docutils()
        self.styles = self.defaults.styles
        self.args = args

    def parse_options(self, args):
        """Parse command line options.
        """
        style_names = tuple(self.defaults.known_styles)
        style_opts = tuple('--'+x for x in style_names)

        try:
            options, args = getopt.gnu_getopt(args, 'hls:v',
                ('help', 'style=', 'version', 'list-styles') + style_names)
        except getopt.GetoptError, e:
            err_exit('viewdoc: %s\n%s' % (e.msg, USAGE))

        for name, value in options:
            if name in ('-s', '--style'):
                self.styles = self.defaults.known_styles.get(value, '')
            elif name in style_opts:
                self.styles = self.defaults.known_styles.get(name[2:], '')
            elif name in ('-l', '--list-styles'):
                self.list_styles()
            elif name in ('-h', '--help'):
                msg_exit(HELP)
            elif name in ('-v', '--version'):
                msg_exit(VERSION)

        if len(args) > 1:
            err_exit('viewdoc: too many arguments\n%s' % USAGE)
        return args

    def list_styles(self):
        """Print available styles and exit.
        """
        for style in sorted(self.defaults.known_styles):
            if style == self.defaults.default_style:
                print style, '(default)'
            else:
                print style
        sys.exit(0)

    def render_file(self, filename):
        """Convert a reST file to HTML.
        """
        dirname, basename = split(filename)
        with changedir(dirname):
            infile = abspath(basename)
            outfile = abspath('.%s.html' % basename)
            self.docutils.publish_file(infile, outfile, self.styles)
            return outfile

    def render_long_description(self, dirname):
        """Convert a package's long description to HTML.
        """
        with changedir(dirname):
            self.setuptools.check_valid_package()
            long_description = self.setuptools.get_long_description()
            outfile = abspath('.long-description.html')
            self.docutils.publish_string(long_description, outfile, self.styles)
            return outfile

    def run(self):
        """Render and display Python package documentation.
        """
        self.python.check_valid_python()

        args = self.parse_options(self.args)
        if args:
            arg = args[0]
        else:
            arg = os.curdir
        if arg:
            arg = expanduser(arg)

        if isfile(arg):
            outfile = self.render_file(arg)
        elif isdir(arg):
            outfile = self.render_long_description(arg)
        else:
            err_exit('No such file or directory: %s' % arg)

        webbrowser.open('file://%s' % outfile)

def main(args=None):
    if args is None:
        args = sys.argv[1:]
    try:
        DocumentationViewer(args).run()
    except SystemExit, e:
        return e.code
    return 0

if __name__ == '__main__':
    sys.exit(main())

<|endoftext|>


<commit_before># Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This script is used to synthesize generated parts of this library."""

import synthtool as s
import synthtool.gcp as gcp
import synthtool.languages.java as java

gapic = gcp.GAPICBazel()

service = 'scheduler'
versions = ['v1beta1', 'v1']
config_pattern = '/google/cloud/scheduler/artman_cloudscheduler_{version}.yaml'

for version in versions:
  java.bazel_library(
    service=service,
    version=version,
    gapic=gapic,
  )

java.common_templates()<commit_msg>chore: enable context aware commits (#95)<commit_after># Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This script is used to synthesize generated parts of this library."""

import synthtool as s
import synthtool.gcp as gcp
import synthtool.languages.java as java

AUTOSYNTH_MULTIPLE_COMMITS = True

gapic = gcp.GAPICBazel()

service = 'scheduler'
versions = ['v1beta1', 'v1']
config_pattern = '/google/cloud/scheduler/artman_cloudscheduler_{version}.yaml'

for version in versions:
  java.bazel_library(
    service=service,
    version=version,
    gapic=gapic,
  )

java.common_templates()<|endoftext|>


<commit_before>import os
import sys

path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(path)
print sys.path
print os.path.abspath(__file__)
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_pjaxr.test_settings'

import django
from django.conf import settings
from django.test.utils import get_runner

def usage():
    return """
    Usage: python runtests.py [UnitTestClass].[method]

    You can pass the Class name of the `UnitTestClass` you want to test.

    Append a method name if you only want to test a specific method of that class.
    """

def main():
    TestRunner = get_runner(settings)

    test_runner = TestRunner()

    failures = test_runner.run_tests(['django_pjaxr'])

    sys.exit(failures)

if __name__ == '__main__':
    main()<commit_msg>try adjusting syspath for travis<commit_after>import os
import sys

path = os.path.abspath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', '..'))
sys.path.append(path)
print sys.path
print os.path.abspath(__file__)
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_pjaxr.test_settings'

import django
from django.conf import settings
from django.test.utils import get_runner

def usage():
    return """
    Usage: python runtests.py [UnitTestClass].[method]

    You can pass the Class name of the `UnitTestClass` you want to test.

    Append a method name if you only want to test a specific method of that class.
    """

def main():
    TestRunner = get_runner(settings)

    test_runner = TestRunner()

    failures = test_runner.run_tests(['django_pjaxr'])

    sys.exit(failures)

if __name__ == '__main__':
    main()<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

import logging
import requests
from datetime import date
from flask import Blueprint, jsonify, request, session

from app import app
from model import format_aid, get_article, vote_article
from model import get_begin_day, \
    get_entries, get_before_day, get_after_day

logger = logging.getLogger(__name__)

api_page = Blueprint('api_page',
                     __name__,
                     template_folder='template')

@api_page.route('/feed/atom', methods=['POST'])
def gen_atom_feed():
    try:
        url = '{}/atom'.format(app.config['FEED_SUBMIT_URL'])
        r = requests.post(url, request.form)
    except:
        logger.error('genspider[atom] error')
        return jsonify(err=1, msg='exception')
    rj = r.json()
    return jsonify(err=rj['err'], msg=rj['msg'])

@api_page.route('/feed/blog', methods=['POST'])
def gen_blog_feed():
    try:
        url = '{}/blog'.format(app.config['FEED_SUBMIT_URL'])
        r = requests.post(url, request.form)
    except:
        logger.error('genspider[blog] error')
        return jsonify(err=1, msg='exception')
    rj = r.json()
    return jsonify(err=rj['err'], msg=rj['msg'])

@api_page.route('/vote', methods=['POST'])
def vote():
    if 'uid' not in session:
        return jsonify(err=1, msg='no uid')
    if 'aid' not in request.form:
        return jsonify(err=2, msg='no aid')
    aid = request.form['aid']
    try:
        aid = format_aid(aid)
    except:
        return jsonify(err=3, msg='invalid aid')
    a = get_article(aid)
    if a is None:
        return jsonify(err=4, msg='no article')
    vote_article(a)
    return jsonify(err=0, aid=str(aid))

def get_day(day):
    y, m, d = [int(i) for i in day.split('-')]
    return date(y, m, d)

@api_page.route('/day', methods=['GET'])
def day_entries():
    try:
        day_entry = get_day(request.args.get('day', None))
    except:
        return jsonify(err=1, msg='invalid day')

    day_begin = get_begin_day()

    if day_begin is None or day_entry is None:
        return jsonify(err=1, msg='no articles')

    if day_entry < day_begin or day_entry > date.today():
        return jsonify(err=1, msg='no articles')

    entries = get_entries(day_entry)
    if len(entries) == 0:
        entries = None

    day_before = get_before_day(day_entry)
    if day_before is not None:
        day_before = day_before.strftime('%Y-%m-%d')
    day_after = get_after_day(day_entry)
    if day_after is not None:
        day_after = day_after.strftime('%Y-%m-%d')

    return jsonify(err=0,
                   day_before=day_before,
                   day_after=day_after,
                   data=entries)
<commit_msg>categories<commit_after># -*- coding: utf-8 -*-

import logging
import requests
from datetime import date
from flask import Blueprint, jsonify, request, session

from app import app
from model import format_aid, get_article, vote_article
from model import get_begin_day, get_entries, get_before_day, \
    get_after_day, get_categories

logger = logging.getLogger(__name__)

api_page = Blueprint('api_page',
                     __name__,
                     template_folder='template')

@api_page.route('/feed/atom', methods=['POST'])
def gen_atom_feed():
    try:
        url = '{}/atom'.format(app.config['FEED_SUBMIT_URL'])
        r = requests.post(url, request.form)
    except:
        logger.error('genspider[atom] error')
        return jsonify(err=1, msg='exception')
    rj = r.json()
    return jsonify(err=rj['err'], msg=rj['msg'])

@api_page.route('/feed/blog', methods=['POST'])
def gen_blog_feed():
    try:
        url = '{}/blog'.format(app.config['FEED_SUBMIT_URL'])
        r = requests.post(url, request.form)
    except:
        logger.error('genspider[blog] error')
        return jsonify(err=1, msg='exception')
    rj = r.json()
    return jsonify(err=rj['err'], msg=rj['msg'])

@api_page.route('/vote', methods=['POST'])
def vote():
    if 'uid' not in session:
        return jsonify(err=1, msg='no uid')
    if 'aid' not in request.form:
        return jsonify(err=2, msg='no aid')
    aid = request.form['aid']
    try:
        aid = format_aid(aid)
    except:
        return jsonify(err=3, msg='invalid aid')
    a = get_article(aid)
    if a is None:
        return jsonify(err=4, msg='no article')
    vote_article(a)
    return jsonify(err=0, aid=str(aid))

def get_day(day):
    y, m, d = [int(i) for i in day.split('-')]
    return date(y, m, d)

@api_page.route('/day', methods=['GET'])
def day_entries():
    try:
        day_entry = get_day(request.args.get('day', None))
    except:
        return jsonify(err=1, msg='invalid day')

    day_begin = get_begin_day()

    if day_begin is None or day_entry is None:
        return jsonify(err=1, msg='no articles')

    if day_entry < day_begin or day_entry > date.today():
        return jsonify(err=1, msg='no articles')

    entries = get_entries(day_entry)
    if len(entries) == 0:
        entries = None

    day_before = get_before_day(day_entry)
    if day_before is not None:
        day_before = day_before.strftime('%Y-%m-%d')
    day_after = get_after_day(day_entry)
    if day_after is not None:
        day_after = day_after.strftime('%Y-%m-%d')

    return jsonify(err=0,
                   day_before=day_before,
                   day_after=day_after,
                   data=entries)

@api_page.route('/categories', methods=['GET'])
def categories():
    categories = get_categories()
    return jsonify(err=0,
                   data=categories)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
    component
    ~~~~~~~~~

    A module that implements various helper functions to assist in using
component(1) with a python workflow.

    :copyright: (c) 2013 by Daniel Chatfield
    :license: MIT, see LICENSE for details.
"""

def require(component_name, package_name=None):
    pass

def export(object):
    pass<commit_msg>Make pep8 compliant<commit_after># -*- coding: utf-8 -*-
"""
    component
    ~~~~~~~~~

    A module that implements various helper functions to assist in using
component(1) with a python workflow.

    :copyright: (c) 2013 by Daniel Chatfield
    :license: MIT, see LICENSE for details.
"""

def require(component_name, package_name=None):
    pass

def export(object):
    pass
<|endoftext|>


<commit_before>from rest_framework import permissions, viewsets
from rest_framework.response import Response
from django.http import HttpResponse
from models import RetailPartnerInfo
from permissions import IsAuthorOfRP
from serializers import RetailPartnerSerializer
import json
from utils import map_retail_partner_images
from pymongo import MongoClient
import mandrill
from config import SENDER_EMAIL, MANDRILL_API_KEY

class RetailPartnerViewSet(viewsets.ModelViewSet):
    queryset = RetailPartnerInfo.objects.all()
    serializer_class = RetailPartnerSerializer

    def get_permissions(self):
        if self.request.method in permissions.SAFE_METHODS:
            return (permissions.AllowAny(),)
        return (permissions.IsAuthenticated(), IsAuthorOfRP(),)

    def perform_create(self, serializer):
        instance = serializer.save(author=self.request.user)
        if self.request.data.get('files'):
            map_retail_partner_images(self.request.data['files'], instance.id)
        return super(RetailPartnerViewSet, self).perform_create(serializer)

class AccountRetailPartnersViewSet(viewsets.ViewSet):
    queryset = RetailPartnerInfo.objects.select_related('author').all()
    serializer_class = RetailPartnerSerializer
    def list(self, request, account_username=None):
        queryset = self.queryset.filter(author__username=account_username)
        serializer = self.serializer_class(queryset, many=True)

        return Response(serializer.data)

def get_rp_profile(request):
    rp_id = request.GET['id']
    retail_partner = RetailPartnerInfo.objects.get(id=rp_id)
    serialized = RetailPartnerSerializer(retail_partner)
    client = MongoClient()
    db = client['retail_partners']
    coll = db['rp_images']
    response_dict = json.loads(json.dumps(serialized.data))
    rp_doc = coll.find_one({'rp_id': int(rp_id)})
    try:
        existing_images = rp_doc['images']
        response_dict['images'] = existing_images
    except Exception as e:
        print str(e)
    return HttpResponse(json.dumps(response_dict))

def get_city_profile(request):
    city_name = request.GET['city']
    retail_partner = RetailPartnerInfo.objects.filter(city=city_name)
    serializer_class = RetailPartnerSerializer
    serializer = serializer_class(retail_partner, many=True)
    return HttpResponse(json.dumps(serializer.data))

def update_rp_data(request):
    rp_data = json.loads(request.body)
    print rp_data
    try:
        rp_data.pop('author')
        map_retail_partner_images(rp_data['files'], rp_data['id'])
    except Exception as e:
        print str(e)
    try:
        rp_data.pop('images')
    except Exception as e:
        print str(e)
    try:
        rp_data.pop('files')
    except Exception as e:
        print str(e)
    rp_obj = RetailPartnerInfo.objects.filter(id=rp_data['id']).update(**rp_data)
    return HttpResponse('Updated Rp Data')

def deactivate_retail_partner(request):
    data = json.loads(request.body)
    rp_list = data['deactivate_list']
    for rp_id in rp_list:
        retail_partner = RetailPartnerInfo.objects.get(id=rp_id)
        retail_partner.status = 5
        retail_partner.save()
    return HttpResponse('Deactivation Request Successful!')

def deactivate_retail_partner_admin(request):
    rp_id = request.GET['id']
    retail_partner = RetailPartnerInfo.objects.get(id=rp_id)
    retail_partner.status = 3
    retail_partner.save()
    return HttpResponse('Deactivation Successful!')

def reactivate_retail_partner(request):
    rp_id = request.GET['id']
    retail_partner = RetailPartnerInfo.objects.get(id=rp_id)
    retail_partner.status = 6
    retail_partner.save()
    return HttpResponse('Successfully Reactivated!')

def reactivate_retail_partner_admin(request):
    rp_id = request.GET['id']
    retail_partner = RetailPartnerInfo.objects.get(id=rp_id)
    retail_partner.status = 2
    retail_partner.save()
    return HttpResponse('Successfully Reactivated!')

def validate_rp_data(request):
    rp_id = request.GET['id']
    retail_partner = RetailPartnerInfo.objects.get(id=rp_id)
    retail_partner.status = 2
    retail_partner.save()
    return HttpResponse('Successfully Validated!')

def send_for_review(request):
    rp_id = request.GET['id']
    retail_partner = RetailPartnerInfo.objects.get(id=rp_id)
    retail_partner.status = 4
    retail_partner.save()
    return HttpResponse('Successfully Sent for Review!')

def app_retail_partners(request):
    retail_partners = RetailPartnerInfo.objects.filter(status=2)
    response = []
    for rp in retail_partners:
        rp_obj = {
            "Contact Name": rp.contact_name,
            "Business Name": rp.business_name,
            "Address": rp.address_line_1,
            "City": rp.city,
            "State": rp.state,
            "Country": rp.country,
            "Pin Code": rp.pin,
            "Contact": rp.contact_number,
            "Location Latitude": str(rp.location_latitude),
            "Location Longitude": str(rp.location_longitude),
            "Location String": rp.location_string,
            "Email": rp.email,
            "Website": "",
            "S no": str(rp.id),
            "location_type": rp.retail_location_type,
            "rp_category" : rp.retail_partner_category
        }
        response.append(rp_obj)
    return HttpResponse(json.dumps(response))

def fetch_data_for_validation(request):
    retail_partners = RetailPartnerInfo.objects.all()
    serializer_class = RetailPartnerSerializer
    serializer = serializer_class(retail_partners, many=True)
    return HttpResponse(json.dumps(serializer.data))

def send_mail(request):
    try:
        data = json.loads(request.body)
        email_list = data['email']
        for email in email_list:
            subject = data['subject']
            message = data['message']
            mandrill_client = mandrill.Mandrill(MANDRILL_API_KEY)
            message = {'from_email': SENDER_EMAIL,'from_name': 'Trestor','subject': subject,'text': message,'to': [{'email': email,'type': 'to'}]}
            result = mandrill_client.messages.send(message=message, async=False, ip_pool='Main Pool')
            print result
        return HttpResponse('Email Sent Successfully!')
    except Exception as e:
        return HttpResponse(str(e))<commit_msg>Mail setting changes<commit_after>from rest_framework import permissions, viewsets
from rest_framework.response import Response
from django.http import HttpResponse
from models import RetailPartnerInfo
from permissions import IsAuthorOfRP
from serializers import RetailPartnerSerializer
import json
from utils import map_retail_partner_images
from pymongo import MongoClient
import mandrill
from config import SENDER_EMAIL, MANDRILL_API_KEY

class RetailPartnerViewSet(viewsets.ModelViewSet):
    queryset = RetailPartnerInfo.objects.all()
    serializer_class = RetailPartnerSerializer

    def get_permissions(self):
        if self.request.method in permissions.SAFE_METHODS:
            return (permissions.AllowAny(),)
        return (permissions.IsAuthenticated(), IsAuthorOfRP(),)

    def perform_create(self, serializer):
        instance = serializer.save(author=self.request.user)
        if self.request.data.get('files'):
            map_retail_partner_images(self.request.data['files'], instance.id)
        return super(RetailPartnerViewSet, self).perform_create(serializer)

class AccountRetailPartnersViewSet(viewsets.ViewSet):
    queryset = RetailPartnerInfo.objects.select_related('author').all()
    serializer_class = RetailPartnerSerializer
    def list(self, request, account_username=None):
        queryset = self.queryset.filter(author__username=account_username)
        serializer = self.serializer_class(queryset, many=True)

        return Response(serializer.data)

def get_rp_profile(request):
    rp_id = request.GET['id']
    retail_partner = RetailPartnerInfo.objects.get(id=rp_id)
    serialized = RetailPartnerSerializer(retail_partner)
    client = MongoClient()
    db = client['retail_partners']
    coll = db['rp_images']
    response_dict = json.loads(json.dumps(serialized.data))
    rp_doc = coll.find_one({'rp_id': int(rp_id)})
    try:
        existing_images = rp_doc['images']
        response_dict['images'] = existing_images
    except Exception as e:
        print str(e)
    return HttpResponse(json.dumps(response_dict))

def get_city_profile(request):
    city_name = request.GET['city']
    retail_partner = RetailPartnerInfo.objects.filter(city=city_name)
    serializer_class = RetailPartnerSerializer
    serializer = serializer_class(retail_partner, many=True)
    return HttpResponse(json.dumps(serializer.data))

def update_rp_data(request):
    rp_data = json.loads(request.body)
    print rp_data
    try:
        rp_data.pop('author')
        map_retail_partner_images(rp_data['files'], rp_data['id'])
    except Exception as e:
        print str(e)
    try:
        rp_data.pop('images')
    except Exception as e:
        print str(e)
    try:
        rp_data.pop('files')
    except Exception as e:
        print str(e)
    rp_obj = RetailPartnerInfo.objects.filter(id=rp_data['id']).update(**rp_data)
    return HttpResponse('Updated Rp Data')

def deactivate_retail_partner(request):
    data = json.loads(request.body)
    rp_list = data['deactivate_list']
    for rp_id in rp_list:
        retail_partner = RetailPartnerInfo.objects.get(id=rp_id)
        retail_partner.status = 5
        retail_partner.save()
    return HttpResponse('Deactivation Request Successful!')

def deactivate_retail_partner_admin(request):
    rp_id = request.GET['id']
    retail_partner = RetailPartnerInfo.objects.get(id=rp_id)
    retail_partner.status = 3
    retail_partner.save()
    return HttpResponse('Deactivation Successful!')

def reactivate_retail_partner(request):
    rp_id = request.GET['id']
    retail_partner = RetailPartnerInfo.objects.get(id=rp_id)
    retail_partner.status = 6
    retail_partner.save()
    return HttpResponse('Successfully Reactivated!')

def reactivate_retail_partner_admin(request):
    rp_id = request.GET['id']
    retail_partner = RetailPartnerInfo.objects.get(id=rp_id)
    retail_partner.status = 2
    retail_partner.save()
    return HttpResponse('Successfully Reactivated!')

def validate_rp_data(request):
    rp_id = request.GET['id']
    retail_partner = RetailPartnerInfo.objects.get(id=rp_id)
    retail_partner.status = 2
    retail_partner.save()
    return HttpResponse('Successfully Validated!')

def send_for_review(request):
    rp_id = request.GET['id']
    retail_partner = RetailPartnerInfo.objects.get(id=rp_id)
    retail_partner.status = 4
    retail_partner.save()
    return HttpResponse('Successfully Sent for Review!')

def app_retail_partners(request):
    retail_partners = RetailPartnerInfo.objects.filter(status=2)
    response = []
    for rp in retail_partners:
        rp_obj = {
            "Contact Name": rp.contact_name,
            "Business Name": rp.business_name,
            "Address": rp.address_line_1,
            "City": rp.city,
            "State": rp.state,
            "Country": rp.country,
            "Pin Code": rp.pin,
            "Contact": rp.contact_number,
            "Location Latitude": str(rp.location_latitude),
            "Location Longitude": str(rp.location_longitude),
            "Location String": rp.location_string,
            "Email": rp.email,
            "Website": "",
            "S no": str(rp.id),
            "location_type": rp.retail_location_type,
            "rp_category" : rp.retail_partner_category
        }
        response.append(rp_obj)
    return HttpResponse(json.dumps(response))

def fetch_data_for_validation(request):
    retail_partners = RetailPartnerInfo.objects.all()
    serializer_class = RetailPartnerSerializer
    serializer = serializer_class(retail_partners, many=True)
    return HttpResponse(json.dumps(serializer.data))

def send_mail(request):
    try:
        data = json.loads(request.body)
        email_list = data['email']
        for email in email_list:
            subject = data['subject']
            message = data['message']
            mandrill_client = mandrill.Mandrill(MANDRILL_API_KEY)
            message = {'from_email': SENDER_EMAIL,'from_name': 'CodeBull','subject': subject,'text': message,'to': [{'email': email,'type': 'to'}]}
            result = mandrill_client.messages.send(message=message, async=False, ip_pool='Main Pool')
            print result
        return HttpResponse('Email Sent Successfully!')
    except Exception as e:
        return HttpResponse(str(e))<|endoftext|>


<commit_before>class CpuStoppedCall(Exception):
    pass

instruction_map = {}
instruction_names = {}

DEBUG = False

def instruction(alt=None):
    def decorator(func):
        number = 110 + len(instruction_map)
        instruction_map[number] = func
        instruction_names[alt or func.__name__] = number
        return func
    return decorator

def exception_wrapper(func):
    def decorator(*args):
        try:
            if DEBUG:
                print("OPcode: {.__name__}".format(func))
            return func(*args)
        except TypeError as e:
            raise Exception("Instruction received invalid amount of arguments",
                            "expected {}, recieved {}".format(func.__code__.co_argcount, len(args)), e)
        except ValueError as e:  # we assume a value error is caused by attempting to treat an absolute value as a mutable object
            raise Exception(
                "Possible attempt to use absolute value (#) as mutable type", e)
    decorator.__name__ = func.__name__
    decorator.__doc__ = func.__doc__
    return decorator

class InstructionSet:
    """Container for cpu instructions

    Arguments
    ---------

    cpu: <cpu object> CPU object to use to execute commands
        Not needed if only being used to compile a program

    Functions
    ---------

    run_encoded( command, *args ): Execute a decoded instruction

    encode_name( command_name ): Return numeric ID of a instruction, returns None if non existant instruction
    """

    def __init__(self, cpu=None):  # no cpu needed for compiler
        self.encoded_commands = instruction_map.copy()
        self.instruction_names = instruction_names.copy()
        self.cpu = cpu

    def run_encoded(self, command, *args):
        """Run an encoded instruction

        Arguments
        ---------

        command: <int> Decoded command to execute

        *args: <str> Operands to run instruction with"""
        command = self.encoded_commands.get(command)
        if command:
            command(self, *args)
        else:
            raise CpuStoppedCall(
                "Invalid command, ID was: {}. Arguments were: {}".format(command, args))

    def encode_name(self, command_name):
        return self.instruction_names.get(command_name)

    @instruction()
    @exception_wrapper
    def add(self, value):
        self.cpu.registers["acc"] += self.cpu.interpret_read_address(value)

    @instruction()
    @exception_wrapper
    def sub(self, value):
        self.cpu.registers["acc"] -= self.cpu.interpret_read_address(value)

    @instruction()
    @exception_wrapper
    def mul(self, value):
        self.cpu.registers["acc"] *= self.cpu.interpret_read_address(value)

    @instruction()
    @exception_wrapper
    def div(self, value):
        self.cpu.registers["acc"] /= self.cpu.interpret_read_address(value)

    @instruction()
    @exception_wrapper
    def set(self, value):
        self.cpu.registers["acc"] = self.cpu.interpret_read_address(value)

    @instruction()
    @exception_wrapper
    def mov(self, from_loc, to_loc):
        if to_loc.startswith("@"):
            self.cpu.registers[to_loc.lstrip(
                "@")] = self.cpu.interpret_read_address(from_loc)
        else:
            self.cpu.memory[self.cpu.interpret_write_address(
                to_loc)] = self.cpu.interpret_read_address(from_loc)

    @instruction()
    @exception_wrapper
    def cmp(self, a, b=0):
        av = self.cpu.interpret_read_address(a)
        # print("comp interpreted as {}".format(av))
        bv = self.cpu.interpret_read_address(b) if b else 0

        functions = [
            (lambda a, b: a < b),
            (lambda a, b: a > b),
            (lambda a, b: a <= b),
            (lambda a, b: a >= b),
            (lambda a, b: a == b)
        ]
        self.cpu.registers["cmp"] = "".join(
            [str(1 if i(av, bv) else 0) for i in functions])

    # jumps to memory address provided, no interpreting
    def _internal_jump(self, location):
        self.cpu.registers["cur"] = location

    @instruction()
    @exception_wrapper
    def jump(self, jump):
        self._internal_jump(self.cpu.interpret_read_address(jump))

    @instruction()
    @exception_wrapper
    def _test_cmp(self, index):
        return int(self.cpu.registers["cmp"][index])

    @instruction()
    @exception_wrapper
    def lje(self, jump):  # less than
        if self._test_cmp(0):
            self.jump(jump)

    @instruction()
    @exception_wrapper
    def mje(self, jump):  # more than
        if self._test_cmp(1):
            self.jump(jump)

    @instruction()
    @exception_wrapper
    def leje(self, jump):  # less than equal
        if self._test_cmp(2):
            self.jump(jump)

    @instruction()
    @exception_wrapper
    def meje(self, jump):  # more than equal
        if self._test_cmp(3):
            self.jump(jump)

    @instruction()
    @exception_wrapper
    def eqje(self, jump):  # equal
        if self._test_cmp(4):
            self.jump(jump)

    @instruction()
    @exception_wrapper
    def prntint(self, memloc):
        print(self.cpu.interpret_read_address(memloc), end="")

    @instruction()
    @exception_wrapper
    def prntstr(self, memloc):
        print(chr(self.cpu.interpret_read_address(memloc)), end='')

    @instruction()
    @exception_wrapper
    def prntnl(self):
        print("\n")

    @instruction(alt="input")
    @exception_wrapper
    def inp(self, memloc):
        if memloc.startswith("@"):
            self.cpu.registers[memloc.strip("@").lower()] = int(
                input("Enter number: "))
        else:
            self.cpu.memory[self.cpu.interpret_write_address(
                memloc)] = int(input("Enter number: "))

      # like anything wrong could happen here
    @instruction()
    @exception_wrapper
    def halt(self):
        raise CpuStoppedCall("CPU halt triggered")

    @instruction()
    @exception_wrapper
    def movloc(self, from_loc, to_loc):
        # move from location stored in location to location
        if to_loc.startswith("@"):
            self.cpu.registers[to_loc.lstrip("@")] = self.cpu.memory[
                self.cpu.interpret_read_address(from_loc)]
        else:
            self.cpu.memory[self.cpu.interpret_write_address(to_loc)] = self.cpu.memory[
                self.cpu.interpret_read_address(from_loc)]

    @instruction()
    @exception_wrapper
    def popstk(self, memloc=None):
        if self.cpu.registers["stk"] > self.cpu.memory.size:
            return 0  # assume everything above maximum address is 0
        if not memloc is None:
            if memloc.startswith("@"):
                self.cpu.registers[memloc.lstrip("@")] = self.cpu.memory[
                    self.cpu.registers["stk"]]
            else:
                self.cpu.memory[self.cpu.interpret_write_address(memloc)] = self.cpu.memory[
                    self.cpu.registers["stk"]]
        self.cpu.registers["stk"] += 1  # stack descends upwardas

    @instruction()
    @exception_wrapper
    def pushstk(self, value):
        # decrement first since last push will leave us one below
        self.cpu.registers["stk"] -= 1
        self.cpu.memory[self.cpu.registers["stk"]
                        ] = self.cpu.interpret_read_address(value)

    @instruction()
    @exception_wrapper
    def call(self, function_location, *args):
        collected_args = [self.cpu.interpret_read_address(i) for i in args]
        # since stack position will change once we push return location
        self._push_stk_py(self.cpu.registers["cur"])
        # push return address to stack
        for i in collected_args:
            self._push_stk_py(i)  # push vars to stack
        self.jump(function_location)

    def _push_stk_py(self, value):
        self.cpu.registers["stk"] -= 1
        self.cpu.memory[self.cpu.registers["stk"]] = value

    def _pop_stk_py(self):
        if self.cpu.registers["stk"] > self.cpu.memory.size:
            return 0
        pre = self.cpu.memory[self.cpu.registers["stk"]]
        self.cpu.registers["stk"] += 1
        return pre

    @instruction()
    @exception_wrapper
    def ret(self, retval=None):
        ret_loc = self._pop_stk_py()
        if retval is not None:
            self._push_stk_py(self.cpu.interpret_read_address(retval))
        self._internal_jump(ret_loc)

    @instruction()
    @exception_wrapper
    def nop(self):
        pass
<commit_msg>removed instruction decorator for a internal func added not equal comparison<commit_after>class CpuStoppedCall(Exception):
    pass

instruction_map = {}
instruction_names = {}

DEBUG = False

def instruction(alt=None):
    def decorator(func):
        number = 110 + len(instruction_map)
        instruction_map[number] = func
        instruction_names[alt or func.__name__] = number
        return func
    return decorator

def exception_wrapper(func):
    def decorator(*args):
        try:
            if DEBUG:
                print("OPcode: {.__name__}".format(func))
            return func(*args)
        except TypeError as e:
            raise Exception("Instruction received invalid amount of arguments",
                            "expected {}, recieved {}".format(func.__code__.co_argcount, len(args)), e)
        except ValueError as e:  # we assume a value error is caused by attempting to treat an absolute value as a mutable object
            raise Exception(
                "Possible attempt to use absolute value (#) as mutable type", e)
    decorator.__name__ = func.__name__
    decorator.__doc__ = func.__doc__
    return decorator

class InstructionSet:
    """Container for cpu instructions

    Arguments
    ---------

    cpu: <cpu object> CPU object to use to execute commands
        Not needed if only being used to compile a program

    Functions
    ---------

    run_encoded( command, *args ): Execute a decoded instruction

    encode_name( command_name ): Return numeric ID of a instruction, returns None if non existant instruction
    """

    def __init__(self, cpu=None):  # no cpu needed for compiler
        self.encoded_commands = instruction_map.copy()
        self.instruction_names = instruction_names.copy()
        self.cpu = cpu

    def run_encoded(self, command, *args):
        """Run an encoded instruction

        Arguments
        ---------

        command: <int> Decoded command to execute

        *args: <str> Operands to run instruction with"""
        command = self.encoded_commands.get(command)
        if command:
            command(self, *args)
        else:
            raise CpuStoppedCall(
                "Invalid command, ID was: {}. Arguments were: {}".format(command, args))

    def encode_name(self, command_name):
        return self.instruction_names.get(command_name)

    @instruction()
    @exception_wrapper
    def add(self, value):
        self.cpu.registers["acc"] += self.cpu.interpret_read_address(value)

    @instruction()
    @exception_wrapper
    def sub(self, value):
        self.cpu.registers["acc"] -= self.cpu.interpret_read_address(value)

    @instruction()
    @exception_wrapper
    def mul(self, value):
        self.cpu.registers["acc"] *= self.cpu.interpret_read_address(value)

    @instruction()
    @exception_wrapper
    def div(self, value):
        self.cpu.registers["acc"] /= self.cpu.interpret_read_address(value)

    @instruction()
    @exception_wrapper
    def set(self, value):
        self.cpu.registers["acc"] = self.cpu.interpret_read_address(value)

    @instruction()
    @exception_wrapper
    def mov(self, from_loc, to_loc):
        if to_loc.startswith("@"):
            self.cpu.registers[to_loc.lstrip(
                "@")] = self.cpu.interpret_read_address(from_loc)
        else:
            self.cpu.memory[self.cpu.interpret_write_address(
                to_loc)] = self.cpu.interpret_read_address(from_loc)

    @instruction()
    @exception_wrapper
    def cmp(self, a, b=0):
        av = self.cpu.interpret_read_address(a)
        # print("comp interpreted as {}".format(av))
        bv = self.cpu.interpret_read_address(b) if b else 0

        functions = [
            (lambda a, b: a < b),
            (lambda a, b: a > b),
            (lambda a, b: a <= b),
            (lambda a, b: a >= b),
            (lambda a, b: a == b)
        ]
        self.cpu.registers["cmp"] = "".join(
            [str(1 if i(av, bv) else 0) for i in functions])

    # jumps to memory address provided, no interpreting
    def _internal_jump(self, location):
        self.cpu.registers["cur"] = location

    @instruction()
    @exception_wrapper
    def jump(self, jump):
        self._internal_jump(self.cpu.interpret_read_address(jump))

    def _test_cmp(self, index):
        return int(self.cpu.registers["cmp"][index])

    @instruction()
    @exception_wrapper
    def lje(self, jump):  # less than
        if self._test_cmp(0):
            self.jump(jump)

    @instruction()
    @exception_wrapper
    def mje(self, jump):  # more than
        if self._test_cmp(1):
            self.jump(jump)

    @instruction()
    @exception_wrapper
    def leje(self, jump):  # less than equal
        if self._test_cmp(2):
            self.jump(jump)

    @instruction()
    @exception_wrapper
    def meje(self, jump):  # more than equal
        if self._test_cmp(3):
            self.jump(jump)

    @instruction()
    @exception_wrapper
    def eqje(self, jump):  # equal
        if self._test_cmp(4):
            self.jump(jump)

    @instruction()
    @exception_wrapper
    def nqje(self, jump):  # not equal
        if self._test_cmp(5):
            self.jump(jump)

    @instruction()
    @exception_wrapper
    def prntint(self, memloc):
        print(self.cpu.interpret_read_address(memloc), end="")

    @instruction()
    @exception_wrapper
    def prntstr(self, memloc):
        print(chr(self.cpu.interpret_read_address(memloc)), end='')

    @instruction()
    @exception_wrapper
    def prntnl(self):
        print("\n")

    @instruction(alt="input")
    @exception_wrapper
    def inp(self, memloc):
        if memloc.startswith("@"):
            self.cpu.registers[memloc.strip("@").lower()] = int(
                input("Enter number: "))
        else:
            self.cpu.memory[self.cpu.interpret_write_address(
                memloc)] = int(input("Enter number: "))

      # like anything wrong could happen here
    @instruction()
    @exception_wrapper
    def halt(self):
        raise CpuStoppedCall("CPU halt triggered")

    @instruction()
    @exception_wrapper
    def movloc(self, from_loc, to_loc):
        # move from location stored in location to location
        if to_loc.startswith("@"):
            self.cpu.registers[to_loc.lstrip("@")] = self.cpu.memory[
                self.cpu.interpret_read_address(from_loc)]
        else:
            self.cpu.memory[self.cpu.interpret_write_address(to_loc)] = self.cpu.memory[
                self.cpu.interpret_read_address(from_loc)]

    @instruction()
    @exception_wrapper
    def popstk(self, memloc=None):
        if self.cpu.registers["stk"] > self.cpu.memory.size:
            return 0  # assume everything above maximum address is 0
        if not memloc is None:
            if memloc.startswith("@"):
                self.cpu.registers[memloc.lstrip("@")] = self.cpu.memory[
                    self.cpu.registers["stk"]]
            else:
                self.cpu.memory[self.cpu.interpret_write_address(memloc)] = self.cpu.memory[
                    self.cpu.registers["stk"]]
        self.cpu.registers["stk"] += 1  # stack descends upwardas

    @instruction()
    @exception_wrapper
    def pushstk(self, value):
        # decrement first since last push will leave us one below
        self.cpu.registers["stk"] -= 1
        self.cpu.memory[self.cpu.registers["stk"]
                        ] = self.cpu.interpret_read_address(value)

    @instruction()
    @exception_wrapper
    def call(self, function_location, *args):
        collected_args = [self.cpu.interpret_read_address(i) for i in args]
        # since stack position will change once we push return location
        self._push_stk_py(self.cpu.registers["cur"])
        # push return address to stack
        for i in collected_args:
            self._push_stk_py(i)  # push vars to stack
        self.jump(function_location)

    def _push_stk_py(self, value):
        self.cpu.registers["stk"] -= 1
        self.cpu.memory[self.cpu.registers["stk"]] = value

    def _pop_stk_py(self):
        if self.cpu.registers["stk"] > self.cpu.memory.size:
            return 0
        pre = self.cpu.memory[self.cpu.registers["stk"]]
        self.cpu.registers["stk"] += 1
        return pre

    @instruction()
    @exception_wrapper
    def ret(self, retval=None):
        ret_loc = self._pop_stk_py()
        if retval is not None:
            self._push_stk_py(self.cpu.interpret_read_address(retval))
        self._internal_jump(ret_loc)

    @instruction()
    @exception_wrapper
    def nop(self):
        pass
<|endoftext|>


<commit_before>#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import numpy as np
import time
import sys
import msh

import multiprocessing
def fun(f, q_in, q_out):
    while True:
        i, x = q_in.get()
        if i is None:
            break
        q_out.put((i, f(x)))
def parmap(f, X, nprocs=multiprocessing.cpu_count()):
    q_in = multiprocessing.Queue(1)
    q_out = multiprocessing.Queue()
    proc = [multiprocessing.Process(target=fun, args=(f, q_in, q_out))
            for _ in range(nprocs)]
    for p in proc:
        p.daemon = True
        p.start()
    sent = [q_in.put((i, x)) for i, x in enumerate(X)]
    [q_in.put((None, None)) for _ in range(nprocs)]
    res = [q_out.get() for _ in range(len(sent))]
    [p.join() for p in proc]
    return [x for i, x in sorted(res)]

def isPointInTetra(pt,pts):
    t = time.time()
    A=np.ones((4,4))
    A[:,:3]=pts
    mats = np.zeros((5,4,4))
    for i in range(5):
        if i == 0:
            mats[0] = np.copy(A)
        else:
            mats[i] = np.copy(A)
            mats[i,i-1,:3]=pt
    octree.time1+=time.time()-t
    t = time.time()
    dets = np.linalg.det(mats)
    octree.time2+=time.time()-t
    t = time.time()
    if np.sum(np.sign(dets))==5 or np.sum(np.sign(dets))==-5:
        octree.time3+=time.time()-t
        return True
    else:
        octree.time3+=time.time()-t
        return False

    """

    A=np.ones((4,4))
    A[:,:3]=pts
    detA=np.linalg.det(A)

    t = time.time()
    for i in range(4):
        tmp =
        tmp[i,:3] = pt
        det = np.linalg.det(tmp)
        octree.time2+=time.time()-t
        if np.sign(det)!=np.sign(detA):
            return False
    return True
    """
def interpolate(pt, pts, vectors):
    dist = np.linalg.norm(pts-pt,axis=1)
    s = np.sum(1./dist)
    vec = [ np.sum([1./d*v for d,v in zip(dist,vectors[:,j])])/s for j in range(3) ]
    return np.array(vec)
def cartesian(arrays, out=None):
    """
    Generate a cartesian product of input arrays.
    """
    arrays = [np.asarray(x) for x in arrays]
    dtype = arrays[0].dtype

    n = np.prod([x.size for x in arrays])
    if out is None:
        out = np.zeros([n, len(arrays)], dtype=dtype)

    m = n / arrays[0].size
    out[:,0] = np.repeat(arrays[0], m)
    if arrays[1:]:
        cartesian(arrays[1:], out=out[0:m,1:])
        for j in xrange(1, arrays[0].size):
            out[j*m:(j+1)*m,1:] = out[0:m,1:]
    return out

class Octree:
    def __init__(self, mesh, depth):
        self.time1 = 0
        self.time2 = 0
        self.time3 = 0
        self.depth = depth
        self.mesh = mesh
        self.getOctreeIndices()
        self.distributeTetras()
        self.makeTetraSet()
    def getOctreeIndices(self):
        tmpPts = np.copy(self.mesh.verts[:,:3])
        tmpPts-=[self.mesh.xmin, self.mesh.ymin, self.mesh.zmin]
        tmpPts/=self.mesh.dims
        tmpPts*=2**self.depth
        tmpPts[tmpPts==2**self.depth]=2**self.depth-1
        self.indices = tmpPts.astype(np.int16, copy=False)
    def distributeTetras(self):
        for indTetra, tetra in enumerate(self.mesh.tets):
            ptInds = self.indices[tetra[:4]]
            mi, ma = np.min(ptInds,axis=0), np.max(ptInds,axis=0)
            diff = ma-mi
            nb = np.sum(diff)
            if nb>1:
                if nb==2:
                    ptInds=mi+cartesian([range(diff[0]+1),range(diff[1]+1),range(diff[2]+1)])
                else:
                    ptInds=mi+cartesian([[1,0],[1,0],[1,0]])
        self.tetras = [[[[] for k in range(2**self.depth)] for j in range(2**self.depth)] for j in range(2**self.depth)]
        for indTetra, tetra in enumerate(self.mesh.tets):
            i=-1
            for ptIndex in tetra[:4]:
                j = self.indices[ptIndex]
                self.tetras[j[0]][j[1]][j[2]].append(indTetra)
    def makeTetraSet(self):
        for i in range(2**self.depth):
            for j in range(2**self.depth):
                for k in range(2**self.depth):
                    self.tetras[i][j][k] = set(self.tetras[i][j][k])
    def getPtIndices(self,pt):
        ind = np.copy(pt)
        ind-=[self.mesh.xmin, self.mesh.ymin, self.mesh.zmin]
        ind/=self.mesh.dims
        ind*=2**self.depth
        ind-=1e-8
        ind = ind.astype(np.int16, copy=False)
        ind[ind==2**self.depth]=2**self.depth-1
        return ind
    def computeVelocity(self,pt):
        velocity = None
        ptIndex = self.getPtIndices(pt)
        for i in self.tetras[ptIndex[0]][ptIndex[1]][ptIndex[2]]:
            ptInds = self.mesh.tets[i,:4]
            pts = self.mesh.verts[ptInds][:,:3]
            if isPointInTetra(pt, pts):
                velocity = interpolate(pt,pts,self.mesh.vectors[ptInds])
                break
        return velocity
    def computeTrajectory(self, pt, step, maxIt):
        pos = []
        pos.append(pt)
        velocity = self.computeVelocity(pt)
        if velocity is None:
            print "First velocity is none at point",pt
            sys.exit()
        for it in range(maxIt):
            pt = pt + step*velocity
            pos.append(pt)
            velocity = self.computeVelocity(pt)
            if velocity is None:
                print "Velocity is none at point",pt
                break
            if it==maxIt-1:
                print "Reached end of streamline at point",pt
        return pos
    def saveSubdivision(self, inds, outfile):
        mesh2 = msh.Mesh(self.mesh.path)
        mesh2.readSol()
        for t in self.tetras[inds[0]][inds[1]][inds[2]]:
            mesh2.tets[t,-1]=10
        for ind in range(10):
            mesh2.removeRef(ind)
        mesh2.tris = np.array([])
        mesh2.discardUnused()
        div = self.mesh.dims/8
        mins = [self.mesh.xmin + inds[0]*div[0], self.mesh.ymin + inds[1]*div[1], self.mesh.zmin + inds[2]*div[2]]
        mesh3=msh.Mesh(cube=[mins[0], mins[0]+div[0], mins[1], mins[1]+div[1], mins[2], mins[2]+div[2]])
        mesh2.fondre(mesh3)
        mesh2.write(outfile)
        mesh2.writeSol(outfile[:-5]+".sol")

if __name__ == "__main__":
    print "1 - Opening the .mesh file and the corresponding .sol file"
    t = time.time()
    mesh = msh.Mesh("demo/snailbox3d1.mesh")
    mesh.readSol()
    print "Mesh reading in", time.time() - t,"s."

    print "2 - Creating the octree structure"
    t = time.time()
    octree = Octree(mesh, 3)
    print "Octree structure created in", time.time() - t,"s."

    print "3 - Computing the streamlines"
    t = time.time()
    #Parameters
    nPoints = 10
    maxIt   = 1000
    step    = 0.01
    #First points
    initialXZ = np.random.normal(loc=0.5,scale=0.15,size=(nPoints,2))
    initialPts = np.insert(initialXZ, 1, 0.02, axis=1)-0.5
    initialPts = initialPts*octree.mesh.dims + octree.mesh.center
    #Actual computing
    positions = parmap(lambda x: octree.computeTrajectory(x, step, maxIt), initialPts)
    print "Streamlines computed in", time.time()-t
    print "Length of each streamline:", [len(p) for p in positions]
<commit_msg>Minor modifications<commit_after>#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import numpy as np
import time
import sys
import msh
import multiprocessing

#Parallel functions
def fun(f, q_in, q_out):
    while True:
        i, x = q_in.get()
        if i is None:
            break
        q_out.put((i, f(x)))
def parmap(f, X, nprocs=multiprocessing.cpu_count()):
    q_in = multiprocessing.Queue(1)
    q_out = multiprocessing.Queue()
    proc = [multiprocessing.Process(target=fun, args=(f, q_in, q_out))
            for _ in range(nprocs)]
    for p in proc:
        p.daemon = True
        p.start()
    sent = [q_in.put((i, x)) for i, x in enumerate(X)]
    [q_in.put((None, None)) for _ in range(nprocs)]
    res = [q_out.get() for _ in range(len(sent))]
    [p.join() for p in proc]
    return [x for i, x in sorted(res)]

#Maths
def isPointInTetra(pt,pts):
    A=np.ones((4,4))
    A[:,:3]=pts
    mats = np.zeros((5,4,4))
    for i in range(5):
        if i == 0:
            mats[0] = np.copy(A)
        else:
            mats[i] = np.copy(A)
            mats[i,i-1,:3]=pt
    dets = np.linalg.det(mats)
    if np.sum(np.sign(dets))==5 or np.sum(np.sign(dets))==-5:
        return True
    else:
        return False
def interpolate(pt, pts, vectors):
    dist = np.linalg.norm(pts-pt,axis=1)
    s = np.sum(1./dist)
    vec = [ np.sum([1./d*v for d,v in zip(dist,vectors[:,j])])/s for j in range(3) ]
    return np.array(vec)
def cartesian(arrays, out=None):
    """
    Generate a cartesian product of input arrays.
    """
    arrays = [np.asarray(x) for x in arrays]
    dtype = arrays[0].dtype

    n = np.prod([x.size for x in arrays])
    if out is None:
        out = np.zeros([n, len(arrays)], dtype=dtype)

    m = n / arrays[0].size
    out[:,0] = np.repeat(arrays[0], m)
    if arrays[1:]:
        cartesian(arrays[1:], out=out[0:m,1:])
        for j in xrange(1, arrays[0].size):
            out[j*m:(j+1)*m,1:] = out[0:m,1:]
    return out

#Octree class and methods
class Octree:
    def __init__(self, mesh, depth):
        self.time1 = 0
        self.time2 = 0
        self.time3 = 0
        self.depth = depth
        self.mesh = mesh
        self.getOctreeIndices()
        self.distributeTetras()
        self.makeTetraSet()
    def getOctreeIndices(self):
        tmpPts = np.copy(self.mesh.verts[:,:3])
        tmpPts-=[self.mesh.xmin, self.mesh.ymin, self.mesh.zmin]
        tmpPts/=self.mesh.dims
        tmpPts*=2**self.depth
        tmpPts[tmpPts==2**self.depth]=2**self.depth-1
        self.indices = tmpPts.astype(np.int16, copy=False)
    def distributeTetras(self):
        for indTetra, tetra in enumerate(self.mesh.tets):
            ptInds = self.indices[tetra[:4]]
            mi, ma = np.min(ptInds,axis=0), np.max(ptInds,axis=0)
            diff = ma-mi
            nb = np.sum(diff)
            if nb>1:
                if nb==2:
                    ptInds=mi+cartesian([range(diff[0]+1),range(diff[1]+1),range(diff[2]+1)])
                else:
                    ptInds=mi+cartesian([[1,0],[1,0],[1,0]])
        self.tetras = [[[[] for k in range(2**self.depth)] for j in range(2**self.depth)] for j in range(2**self.depth)]
        for indTetra, tetra in enumerate(self.mesh.tets):
            i=-1
            for ptIndex in tetra[:4]:
                j = self.indices[ptIndex]
                self.tetras[j[0]][j[1]][j[2]].append(indTetra)
    def makeTetraSet(self):
        for i in range(2**self.depth):
            for j in range(2**self.depth):
                for k in range(2**self.depth):
                    self.tetras[i][j][k] = set(self.tetras[i][j][k])
    def getPtIndices(self,pt):
        ind = np.copy(pt)
        ind-=[self.mesh.xmin, self.mesh.ymin, self.mesh.zmin]
        ind/=self.mesh.dims
        ind*=2**self.depth
        ind-=1e-8
        ind = ind.astype(np.int16, copy=False)
        ind[ind==2**self.depth]=2**self.depth-1
        return ind
    def computeVelocity(self,pt):
        velocity = None
        ptIndex = self.getPtIndices(pt)
        for i in self.tetras[ptIndex[0]][ptIndex[1]][ptIndex[2]]:
            ptInds = self.mesh.tets[i,:4]
            pts = self.mesh.verts[ptInds][:,:3]
            if isPointInTetra(pt, pts):
                velocity = interpolate(pt,pts,self.mesh.vectors[ptInds])
                break
        return velocity
    def computeTrajectory(self, pt, step, maxIt):
        pos = []
        pos.append(pt)
        velocity = self.computeVelocity(pt)
        if velocity is None:
            print "First velocity is none at point",pt
            sys.exit()
        for it in range(maxIt):
            pt = pt + step*velocity
            pos.append(pt)
            velocity = self.computeVelocity(pt)
            if velocity is None:
                print "Velocity is none at point",pt
                break
            if it==maxIt-1:
                print "Reached end of streamline at point",pt
        return pos
    def saveSubdivision(self, inds, outfile):
        mesh2 = msh.Mesh(self.mesh.path)
        mesh2.readSol()
        for t in self.tetras[inds[0]][inds[1]][inds[2]]:
            mesh2.tets[t,-1]=10
        for ind in range(10):
            mesh2.removeRef(ind)
        mesh2.tris = np.array([])
        mesh2.discardUnused()
        div = self.mesh.dims/8
        mins = [self.mesh.xmin + inds[0]*div[0], self.mesh.ymin + inds[1]*div[1], self.mesh.zmin + inds[2]*div[2]]
        mesh3=msh.Mesh(cube=[mins[0], mins[0]+div[0], mins[1], mins[1]+div[1], mins[2], mins[2]+div[2]])
        mesh2.fondre(mesh3)
        mesh2.write(outfile)
        mesh2.writeSol(outfile[:-5]+".sol")

if __name__ == "__main__":
    print "1 - Opening the .mesh file and the corresponding .sol file"
    t = time.time()
    mesh = msh.Mesh("demo/snailbox3d1.mesh")
    mesh.readSol()
    print "Mesh reading in", time.time() - t,"s."

    print "2 - Creating the octree structure"
    t = time.time()
    octree = Octree(mesh, 3)
    print "Octree structure created in", time.time() - t,"s."

    print "3 - Computing the streamlines"
    t = time.time()
    #Parameters
    nPoints = 10
    maxIt   = 1000
    step    = 0.01
    #First points
    initialXZ = np.random.normal(loc=0.5,scale=0.15,size=(nPoints,2))
    initialPts = np.insert(initialXZ, 1, 0.02, axis=1)-0.5
    initialPts = initialPts*octree.mesh.dims + octree.mesh.center
    #Actual computing
    positions = parmap(lambda x: octree.computeTrajectory(x, step, maxIt), initialPts)
    print "Streamlines computed in", time.time()-t
    print "Length of each streamline:", [len(p) for p in positions]
<|endoftext|>


<commit_before>'''Basic Samsung serial driver'''

TCP_PORT = 1515

param_ipAddress = Parameter({"value":"192.168.100.1","title":"IP address","order":0, "schema":{"type":"string"}})
param_id = Parameter({"value":"1","title":"ID","order":0, "schema":{"type":"integer"}})

def main(arg = None):
  print 'Nodel script started.'

  tcp.setDest('%s:%s' % (param_ipAddress, TCP_PORT))

def connected():
  print 'connected'

def received(data):
  print 'received ' + data.encode('hex')

def sent(data):
  print 'sent ' + data.encode('hex')

def disconnected():
  print 'disconnected'

def timeout():
  console.warn("TCP timeout")

tcp = TCP(dest=None, connected=connected, received=received, sent=sent, disconnected=disconnected, timeout=timeout, sendDelimiters=None, receiveDelimiters=None, binaryStartStopFlags=None)

def local_action_TurnOn(arg = None):
  """{"title":"Turns on","desc":"Turns this node on.","group":"Power","caution":"Ensure hardware is in a state to be turned on.","order":2}"""
  msg = '\x11%s\x01\x01' % chr(int(param_id))

  checksum = sum([ord(c) for c in msg]) & 0xff

  tcp.request('\xaa%s%s' % (msg, chr(checksum)), lambda resp: console.log('Got response [%s] to power on request.' % resp.encode('hex')))

def local_action_TurnOff(arg = None):
  """{"title":"Turns off","desc":"Turns this node on.","group":"Power","caution":"Ensure hardware is in a state to be turned on.","order":2}"""
  msg = '\x11%s\x01\x00' % chr(int(param_id))

  checksum = sum([ord(c) for c in msg]) & 0xff

  tcp.request('\xaa%s%s' % (msg, chr(checksum)), lambda resp: console.log('Got response [%s] to power off request.' % resp.encode('hex')))
<commit_msg>getting more sophisticated.<commit_after>'''Basic Samsung serial driver - manual http://www.samsung.com/us/pdf/UX_DX.pdf'''

TCP_PORT = 1515

param_ipAddress = Parameter({"value":"192.168.100.1","title":"IP address","order":0, "schema":{"type":"string"}})
param_id = Parameter({"value":"1","title":"ID","order":0, "schema":{"type":"integer"}})

local_event_DebugShowLogging = LocalEvent({'group': 'Debug', 'schema': {'type': 'boolean'}})

# general device status
local_event_Status = LocalEvent({'order': -100, 'schema': {'type': 'string', 'enum': ['Missing', 'OK']}})

local_event_Power = LocalEvent({'order': 0, 'schema': {'type': 'string', 'enum': ['On', 'Off']}})
local_event_Volume = LocalEvent({'schema': {'type': 'integer'}})
local_event_Mute = LocalEvent({'schema': {'type': 'string', 'enum': ['Mute', 'Unmute']}})
# this table is incomplete
INPUT_CODE_TABLE = [ ('1F', 'PC'),
                     ('1E', 'BNC'),
                     ('18', 'DVI'),
                     ('0C', 'AV'),
                     ('04', 'S-Video'),
                     ('08', 'Component'),
                     ('20', 'MagicNet'),
                     ('1f', 'DVI_VIDEO'),
                     ('30', 'RF(TV)'),
                     ('40', 'DTV'),
                     ('21', 'HDMI') ]
local_event_InputCode = LocalEvent({'schema': {'type': 'string'}})

def handle_displayStatusTimer():
  lookup_local_action('GetDisplayStatus').call()

timer_deviceStatus = Timer(handle_displayStatusTimer, 30)

def main(arg = None):
  print 'Nodel script started.'

  tcp.setDest('%s:%s' % (param_ipAddress, TCP_PORT))

local_event_TCPStatus = LocalEvent({'schema': {'type': 'string', 'enum': ['Connected', 'Disconnected', 'Timeout']}})

def connected():
  local_event_Status.emit('OK')
  local_event_TCPStatus.emitIfDifferent('Connected')

  # wait a second and poll
  timer_deviceStatus.setDelay(1.0)

def received(data):
  if local_event_DebugShowLogging.getArg():
    print 'RECV: [%s]' % data.encode('hex')

def sent(data):
  if local_event_DebugShowLogging.getArg():
    print 'SENT: [%s]' % data.encode('hex')

def disconnected():
  local_event_TCPStatus.emitIfDifferent('Disconnected')
  local_event_Status.emit('Missing')

def timeout():
  local_event_TCPStatus.emitIfDifferent('Timeout')
  local_event_Status.emit('Missing')

tcp = TCP(connected=connected, received=received, sent=sent, disconnected=disconnected,
          timeout=timeout,
          sendDelimiters=None, receiveDelimiters=None, binaryStartStopFlags=None)

def setPower(arg):
  state = True if arg != 'Off' else False
  msg = '\x11%s\x01%s' % (chr(int(param_id)), '\x01' if state else '\x00')
  checksum = sum([ord(c) for c in msg]) & 0xff
  tcp.request('\xaa%s%s' % (msg, chr(checksum)), checkHeader)

def local_action_TurnOn(arg=None):
  """{"title":"On","desc":"Turns this node on.","group":"Power","caution":"Ensure hardware is in a state to be turned on.","order":1}"""
  setPower('On')

def local_action_TurnOff(arg=None):
  """{"title":"Off","desc":"Turns this node on.","group":"Power","caution":"Ensure hardware is in a state to be turned on.","order":2}"""
  # example response after off: aa ff 00 03 41 11 00 54
  setPower('Off')

def local_action_Power(arg=None):
  """{"title": "Set", "group": "Power", "order": 3,
      "desc":"Turns this node on or off.",
      "caution": "Ensure hardware is in a state to be turned on.",
      "schema": { "type": "string", "enum": ["On", "Off"] }}"""
  setPower(arg)

def local_action_GetDisplayStatus(arg=None):
  # example response: aaff00094100010000141000006e

  # aa   ff   00   09      41 ('A')    00
  # HDR  CMD  ID   length  ACK         R->Cmd
  # +0   1    2    3       4           5

  # 1:01   2:00    3:00     4:14     5:10     6:00    7:00          6e
  # PRW    VOL     MUTE     INPUT    ASPECT   NTimeNF FTimeNF       CSUM
  # +6     +7      +8       +9       +10      +11     +12           +13
  msg = '\x00%s\x00' % chr(int(param_id))
  checksum = sum([ord(c) for c in msg]) & 0xff

  def handleResp(arg):
    checkHeader(arg)

    local_event_Power.emit('On' if ord(arg[6]) == 1 else 'Off')
    local_event_Volume.emit(ord(arg[7]))
    local_event_Mute.emit('Mute' if ord(arg[8]) == 1 else 'Unmute')
    local_event_InputCode.emit(arg[9].encode('hex'))

  tcp.request('\xaa%s%s' % (msg, chr(checksum)), handleResp)

def local_action_ClearMenu(arg=None):
  """{"desc": "Clears the OSD menu"}"""
  msg = '\x34%s\x01\x00' % chr(int(param_id))
  checksum = sum([ord(c) for c in msg]) & 0xff
  tcp.request('\xaa%s%s' % (msg, chr(checksum)), checkHeader)

def getIRRemoteControl(arg):
  # eg. 'aa ff 00 03 41 36 01 7a

  msg = '\x36%s\x00' % chr(int(param_id))
  checksum = sum([ord(c) for c in msg]) & 0xff
  tcp.request('\xaa%s%s' % (msg, chr(checksum)), lambda resp: checkHeader(resp, lambda: irRemoteControlEvent.emit('Enabled' if resp[6] == '\x01' else 'Disabled')))

Action('Get IR Remote Control', getIRRemoteControl, {'title': 'Get', 'group': 'IR Remote Control'})

def setIRRemoteControl(arg):
  if arg != 'Disabled':
    state = True
  else:
    state = False

  msg = '\x36%s\x01%s' % (chr(int(param_id)), '\x01' if state else '\x00')
  checksum = sum([ord(c) for c in msg]) & 0xff
  tcp.request('\xaa%s%s' % (msg, chr(checksum)), lambda resp: checkHeader(resp, lambda: irRemoteControlEvent.emit(arg)))

irRemoteControlEvent = Event('IR Remote Control', {'group': 'IR Remote Control', 'schema': {'type': 'string', 'enum': ['Enabled', 'Disabled']}})
Action('IR Remote Control', setIRRemoteControl, {'title': 'Set', 'group': 'IR Remote Control', 'caution': 'Are you sure you want to enable/disable IR remote control?', 'schema': {'type': 'string', 'enum': ['Enabled', 'Disabled']}})

def checkHeader(arg, onSuccess=None):
  if arg[0] != '\xaa' or arg[1] != '\xff':
    raise Exception('Bad message structure')

  if arg[4] != 'A':
    raise Exception('Bad acknowledgement')

  if onSuccess:
    onSuccess()

<|endoftext|>


<commit_before>'''
Module to store conda build settings.
'''
from __future__ import absolute_import, division, print_function

import os
import sys
from os.path import abspath, expanduser, join

import conda.config as cc

# Don't "save" an attribute of this module for later, like build_prefix =
# conda_build.config.config.build_prefix, as that won't reflect any mutated
# changes.

class Config(object):
    __file__ = __path__ = __file__
    __package__ = __package__
    __doc__ = __doc__

    CONDA_PERL = os.getenv('CONDA_PERL', '5.18.2')
    CONDA_PY = int(os.getenv('CONDA_PY', cc.default_python.replace('.',
        '')).replace('.', ''))
    CONDA_NPY = int(os.getenv('CONDA_NPY', '19').replace('.', ''))

    PY3K = int(bool(CONDA_PY >= 30))

    def get_conda_py(self):
        return self.CONDA_PY

    _bld_root_env = os.getenv('CONDA_BLD_PATH')
    _bld_root_rc = cc.rc.get('conda-build', {}).get('root-dir')
    if _bld_root_env:
        croot = abspath(expanduser(_bld_root_env))
    elif _bld_root_rc:
        croot = abspath(expanduser(_bld_root_rc))
    elif cc.root_writable:
        croot = join(cc.root_dir, 'conda-bld')
    else:
        croot = abspath(expanduser('~/conda-bld'))

    short_build_prefix = join(cc.envs_dirs[0], '_build')
    long_build_prefix = (short_build_prefix + 8 * '_placehold')[:80]
    # XXX: Make this None to be more rigorous about requiring the build_prefix
    # to be known before it is used.
    use_long_build_prefix = False
    test_prefix = join(cc.envs_dirs[0], '_test')

    def _get_python(self, prefix):
        if sys.platform == 'win32':
            res = join(prefix, 'python.exe')
        else:
            res = join(prefix, 'bin/python')
        return res

    def _get_perl(self, prefix):
        if sys.platform == 'win32':
            res = join(prefix, 'perl.exe')
        else:
            res = join(prefix, 'bin/perl')
        return res

    @property
    def build_prefix(self):
        if self.use_long_build_prefix is None:
            raise Exception("I don't know which build prefix to use yet")
        if self.use_long_build_prefix:
            return self.long_build_prefix
        return self.short_build_prefix

    @property
    def build_python(self):
        return self._get_python(self.build_prefix)

    @property
    def test_python(self):
        return self._get_python(self.test_prefix)

    @property
    def build_perl(self):
        return self._get_perl(self.build_prefix)

    @property
    def test_perl(self):
        return self._get_perl(self.test_prefix)

    @property
    def info_dir(self):
        return join(self.build_prefix, 'info')

    @property
    def broken_dir(self):
        return join(self.croot, "broken")

    bldpkgs_dir = join(croot, cc.subdir)

config = Config()

croot = config.croot

def show():
    print('CONDA_PY:', config.CONDA_PY)
    print('CONDA_NPY:', config.CONDA_NPY)
    print('subdir:', cc.subdir)
    print('croot:', croot)
    print('build packages directory:', config.bldpkgs_dir)

if __name__ == '__main__':
    show()
<commit_msg>Don't use the long build prefix if the short prefix is already long<commit_after>'''
Module to store conda build settings.
'''
from __future__ import absolute_import, division, print_function

import os
import sys
from os.path import abspath, expanduser, join

import conda.config as cc

# Don't "save" an attribute of this module for later, like build_prefix =
# conda_build.config.config.build_prefix, as that won't reflect any mutated
# changes.

class Config(object):
    __file__ = __path__ = __file__
    __package__ = __package__
    __doc__ = __doc__

    CONDA_PERL = os.getenv('CONDA_PERL', '5.18.2')
    CONDA_PY = int(os.getenv('CONDA_PY', cc.default_python.replace('.',
        '')).replace('.', ''))
    CONDA_NPY = int(os.getenv('CONDA_NPY', '19').replace('.', ''))

    PY3K = int(bool(CONDA_PY >= 30))

    def get_conda_py(self):
        return self.CONDA_PY

    _bld_root_env = os.getenv('CONDA_BLD_PATH')
    _bld_root_rc = cc.rc.get('conda-build', {}).get('root-dir')
    if _bld_root_env:
        croot = abspath(expanduser(_bld_root_env))
    elif _bld_root_rc:
        croot = abspath(expanduser(_bld_root_rc))
    elif cc.root_writable:
        croot = join(cc.root_dir, 'conda-bld')
    else:
        croot = abspath(expanduser('~/conda-bld'))

    short_build_prefix = join(cc.envs_dirs[0], '_build')
    long_build_prefix = max(short_build_prefix, (short_build_prefix + 8 * '_placehold')[:80])
    # XXX: Make this None to be more rigorous about requiring the build_prefix
    # to be known before it is used.
    use_long_build_prefix = False
    test_prefix = join(cc.envs_dirs[0], '_test')

    def _get_python(self, prefix):
        if sys.platform == 'win32':
            res = join(prefix, 'python.exe')
        else:
            res = join(prefix, 'bin/python')
        return res

    def _get_perl(self, prefix):
        if sys.platform == 'win32':
            res = join(prefix, 'perl.exe')
        else:
            res = join(prefix, 'bin/perl')
        return res

    @property
    def build_prefix(self):
        if self.use_long_build_prefix is None:
            raise Exception("I don't know which build prefix to use yet")
        if self.use_long_build_prefix:
            return self.long_build_prefix
        return self.short_build_prefix

    @property
    def build_python(self):
        return self._get_python(self.build_prefix)

    @property
    def test_python(self):
        return self._get_python(self.test_prefix)

    @property
    def build_perl(self):
        return self._get_perl(self.build_prefix)

    @property
    def test_perl(self):
        return self._get_perl(self.test_prefix)

    @property
    def info_dir(self):
        return join(self.build_prefix, 'info')

    @property
    def broken_dir(self):
        return join(self.croot, "broken")

    bldpkgs_dir = join(croot, cc.subdir)

config = Config()

croot = config.croot

def show():
    print('CONDA_PY:', config.CONDA_PY)
    print('CONDA_NPY:', config.CONDA_NPY)
    print('subdir:', cc.subdir)
    print('croot:', croot)
    print('build packages directory:', config.bldpkgs_dir)

if __name__ == '__main__':
    show()
<|endoftext|>


<commit_before># Copyright 2016 the original author or authors.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

__author__ = "Angelo Torelli"
__copyright__ = "The authors"
__license__ = "Apache 2"
__email__ = "m.gumbel@hs-mannheim.de"
__status__ = "Production"

import random
from Steppable.ModuroSteppable import ModuroSteppable

class ColonySteppable(ModuroSteppable):
    def __init__(self, simulator, model, _frequency=1):
        ModuroSteppable.__init__(self, simulator, model, _frequency)
        self.scalarCLField = self.createScalarFieldCellLevelPy("ColonyField")

    def moduroStep(self, mcs):
        if (self.model.execConfig.colonyTagInMCS - 2) <= mcs <= (self.model.execConfig.colonyTagInMCS + 2):
            cellNr = self.cellList.__len__()
            stemNr = 0
            for cell in self.cellList:
                if cell.type == self.STEM:
                    stemNr += 1
            distance = cellNr/stemNr
            for cell in self.cellList:
                if cell.type == self.STEM:
                    cellDict['colony'] = cellNr - stemNr * distance
                    stemNr -=1
                cellDict = self.getDictionaryAttribute(cell)
                cellDict['colony'] = cell.id
        elif self.model.execConfig.colonyTagInMCS + 10 < mcs:
            self.model.execConfig.colonyTag = True
            self.scalarCLField.clear()
            for cell in self.cellList:
                cellDict = self.getDictionaryAttribute(cell)
                self.scalarCLField[cell] = cellDict['colony']

<commit_msg>Tiny error detected but not solved<commit_after># Copyright 2016 the original author or authors.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

__author__ = "Angelo Torelli"
__copyright__ = "The authors"
__license__ = "Apache 2"
__email__ = "m.gumbel@hs-mannheim.de"
__status__ = "Production"

import random
from Steppable.ModuroSteppable import ModuroSteppable

class ColonySteppable(ModuroSteppable):
    def __init__(self, simulator, model, _frequency=1):
        ModuroSteppable.__init__(self, simulator, model, _frequency)
        self.scalarCLField = self.createScalarFieldCellLevelPy("ColonyField")

    def moduroStep(self, mcs):
        #TODO: when interupt is a multiple of colonytag interval than it does not go into the if statement therefor +1
        if self.model.execConfig.colonyTagInMCS + 1 == mcs:
            cellNr = self.cellList.__len__()
            stemNr = 0
            for cell in self.cellList:
                if cell.type == self.STEM:
                    stemNr += 1
            distance = cellNr/stemNr
            for cell in self.cellList:
                if cell.type == self.STEM:
                    cellDict['colony'] = cellNr - stemNr * distance
                    stemNr -=1
                cellDict = self.getDictionaryAttribute(cell)
                cellDict['colony'] = cell.id
        elif self.model.execConfig.colonyTagInMCS < mcs:
            self.model.execConfig.colonyTag = True
            self.scalarCLField.clear()
            for cell in self.cellList:
                cellDict = self.getDictionaryAttribute(cell)
                self.scalarCLField[cell] = cellDict['colony']

<|endoftext|>