<commit_before>#!/usr/bin/env python3
# Common tools:
#	pip/pip3 : install packages
#	pylint/pychecker : verify syntax
#	unittest package for xUnit.
""" This module helps setup a fresh install the way I like it. """

# Imports
import apt
import argparse
import os
import shutil
import subprocess

# Packages to install follow, broken down into categories.

PROGRAMS = """ \
    aptitude synaptic \
    kubuntu-restricted-extras kubuntu-restricted-addons \
    brother-cups-wrapper-laser brother-cups-wrapper-laser1 \
    brother-lpr-drivers-laser brother-lpr-drivers-laser1 \
    k3b kid3 krita kolourpaint4 kchmviewer yakuake plasma-widget-quickaccess \
    chromium-browser firefox \
    abiword baobab dvdrip easytag chm2pdf dia \
    gimp gimp-plugin-registry \
    fuse gparted quicksynergy \
    redshift geoclue geoclue-hostip numlockx \
    samba samba-doc samba-tools wireshark \
    ubuntuone-client ubuntuone-control-panel-qt \
    vlc ffmpeg-doc ffmpeg mplayer mencoder \
    p7zip-full rar zip unzip gzip \
    virtualbox-qt wine \
    ttf-xfree86-nonfree"""

# Series of packages follow, separated to be easily modified.
KEYRINGS = """ \
    debian-keyring debian-archive-keyring gnome-keyring \
    debian-ports-archive-keyring python-gnomekeyring python-keyring \
    ubuntu-keyring"""

PROGRAMMING = """ \
    build-essential debianutils ubuntu-dev-tools \
    automake automake1.9-doc ant ant-doc checkinstall checkbox \
    dkms docbook make-doc lynx kdiff3 kdiff3-doc patch rpm2cpio rpm \
    codeblocks kdevelop qt-sdk \
    colormake colordiff colorgcc \
    vim vim-doc vim-gtk vim-rails vim-syntax-go vim-syntax-gtk vim-doc \
    flex flex-doc bison bison-doc graphviz exuberant-ctags \
    clisp clisp-doc clisp-dev clisp-module-gdbm \
    erlang erlang-eunit \
    gcc gcc-doc gcc-4.7-source libcunit1 gdb gdb-doc cgdb xxgdb \
    libboost-all-dev libglm-dev libglew-dev libglfw-dev \
    libncursesw5-dev \
    openmpi-bin openmpi-checkpoint openmpi-common \
    gfortran \
    ghc ghc-doc ghc-haddock ghc-prof haskell-debian-utils \
    haskell-devscripts haskell-doc cabal-install \
    junit junit-doc maven openjdk-7-doc openjdk-7-jdk openjdk-7-dbg \
    openjdk-7-source openjdk-7-demo icedtea-7-plugin \
    lua5.2 lua5.2-doc luadoc \
    perl perl-doc perl-modules libpadwalker-perl \
    php5 php5-mysql phpunit php5-dev \
    python python-doc python3-doc jython jython-doc \
    ruby1.9.1-full shunit2 \
    bzr bzr-builddeb bzr-doc python-bzrlib bzrtools git git-gui git-doc \
    mercurial subversion cvs"""

CABAL = "buildwrapper scion-browser hoogle terminfo happy hlint"

PY_PACKS = "argcomplete"

class NotSudo(Exception):
    """ Throw this if we aren't sudo but need to be. """
    pass

# Functions

def install_packages():
    """ Installs required packages on this system. """
    if os.getuid() != 0:
        raise NotSudo('This stage must be run as root.')

    packages = '{} {} {}'.format(PROGRAMS, PROGRAMMING, KEYRINGS)
    packages = packages.split()

    cache = apt.Cache()
    print("One moment while we update cache.")
    cache.update()
    cache.open(None)
    print("Update done.")

    for pack in packages:
        package = cache[pack]
        if not package.is_installed:
            package.mark_install()

    cache.commit()
    cache.close()

def get_code(command, target):
    """ Wrapper function to clone repos.
    Protects against overwriting if target exists.
    command: The command that would run in bash.
    target: Where to clone to.
    """
    cmd = command.split()
    cmd.append(target)
    if not os.path.exists(target):
        subprocess.call(cmd)

def setup_config():
    """ Setup the dev environment, stuff goes in the user's home folder. """
    script_dir = os.path.realpath(__file__)
    script_dir = script_dir[0:script_dir.rindex(os.path.sep)]
    src = script_dir + '/vim_and_bash_config'
    dst = os.path.expanduser('~')

    # Link to config files, copy .vim folder.
    for fil in ['.vimrc', '.bash_aliases']:
        sfile = src + '/' + fil
        dfile = dst + '/' + fil
        if not os.path.exists(dfile):
            os.symlink(sfile, dfile)

    # Copy vim folder if not there.
    ddir = dst + '/.vim'
    if not os.path.exists(ddir):
        shutil.copytree(src.format('.vim'), ddir)

    # Init NeoBundle.
    ddir = dst + '/.vim/bundle'
    if not os.path.exists(ddir):
        print('Creating bunlde dir ' + ddir)
        os.mkdir(ddir)
    get_code('git clone https://github.com/Shougo/neobundle.vim',
             dst + '/.vim/bundle/neobundle.vim')

    # Setup git/hg prompt.
    get_code('hg clone http://bitbucket.org/sjl/hg-prompt/',
             dst + '/.hg-prompt')
    get_code('git clone git@github.com:magicmonty/bash-git-prompt.git',
             dst + '/.bash-git-prompt')

    # Setup powerline fonts if not done.
    ddir = dst + '/.fonts'
    if not os.path.exists(ddir):
        os.mkdir(ddir)
        get_code('git clone https://github.com/Lokaltog/powerline-fonts',
                 ddir + '/powerline-fonts')
        subprocess.call(['fc-cache', '-vf', ddir])

def install_cabal():
    """ Installs locally some haskell packages for Eclipse Haskell plugin. """
    cmd = ['cabal', 'update']
    subprocess.call(cmd)

    cmd = 'cabal install {}'.format(CABAL)
    cmd = cmd.split()
    subprocess.call(cmd)

def py_packages():
    """ Installs python packages using pip. """
    if os.geteuid() != 0:
        raise NotSudo('This stage must be run as root.')

    # Use python package manager.
    cmd = 'pip install ' + PY_PACKS
    subprocess.call(cmd.split())

    # Install python completion to system bash_completion.d.
    cmd = 'activate-global-python-argcomplete'
    subprocess.call(cmd.split())

def take_choice(choice):
    """ Simple wrapper function to select right action. """
    choice = int(choice)
    if choice == 1:
        install_packages()
    elif choice == 2:
        setup_config()
    elif choice == 3:
        install_cabal()
    elif choice == 4:
        py_packages()

if __name__ == '__main__':
    DESC = """ This program sets up a vanilla Ubuntu install.
    Pass a number to determine step.
    1 -> Install most packages.
    2 -> Setup vim, bash_aliases and development environment.
    3 -> Setup cabal for haskell development.
    4 -> Setup python packages through pip.
    """
    PARSER = argparse.ArgumentParser(description=DESC)
    PARSER.add_argument('choice', action='store', help='the stage')

    ARGS = PARSER.parse_args()  # Default parses argv[1:]

    try:
        take_choice(ARGS.choice)
    except IOError as exc:
        print('Failed to install: {}'.format(exc))
    except NotSudo as exc:
        print("Rerun this part of script with sudo.")
<commit_msg>Added ccache to programs.<commit_after>#!/usr/bin/env python3
# Common tools:
#	pip/pip3 : install packages
#	pylint/pychecker : verify syntax
#	unittest package for xUnit.
""" This module helps setup a fresh install the way I like it. """

# Imports
import apt
import argparse
import os
import shutil
import subprocess

# Packages to install follow, broken down into categories.

PROGRAMS = """ \
    aptitude synaptic \
    kubuntu-restricted-extras kubuntu-restricted-addons \
    brother-cups-wrapper-laser brother-cups-wrapper-laser1 \
    brother-lpr-drivers-laser brother-lpr-drivers-laser1 \
    k3b kid3 krita kolourpaint4 kchmviewer yakuake plasma-widget-quickaccess \
    chromium-browser firefox \
    abiword baobab dvdrip easytag chm2pdf dia \
    gimp gimp-plugin-registry \
    fuse gparted quicksynergy \
    redshift geoclue geoclue-hostip numlockx \
    samba samba-doc samba-tools wireshark \
    ubuntuone-client ubuntuone-control-panel-qt \
    vlc ffmpeg-doc ffmpeg mplayer mencoder \
    p7zip-full rar zip unzip gzip \
    virtualbox-qt wine \
    ttf-xfree86-nonfree"""

# Series of packages follow, separated to be easily modified.
KEYRINGS = """ \
    debian-keyring debian-archive-keyring gnome-keyring \
    debian-ports-archive-keyring python-gnomekeyring python-keyring \
    ubuntu-keyring"""

PROGRAMMING = """ \
    build-essential debianutils ubuntu-dev-tools \
    automake automake1.9-doc ant ant-doc checkinstall checkbox \
    dkms docbook make-doc lynx kdiff3 kdiff3-doc patch rpm2cpio rpm \
    codeblocks kdevelop qt-sdk \
    colormake colordiff colorgcc \
    vim vim-doc vim-gtk vim-rails vim-syntax-go vim-syntax-gtk vim-doc \
    flex flex-doc bison bison-doc graphviz exuberant-ctags \
    clisp clisp-doc clisp-dev clisp-module-gdbm \
    erlang erlang-eunit \
    gcc gcc-doc gcc-4.7-source libcunit1 gdb gdb-doc cgdb xxgdb ccache \
    libboost-all-dev libglm-dev libglew-dev libglfw-dev \
    libncursesw5-dev \
    openmpi-bin openmpi-checkpoint openmpi-common \
    gfortran \
    ghc ghc-doc ghc-haddock ghc-prof haskell-debian-utils \
    haskell-devscripts haskell-doc cabal-install \
    junit junit-doc maven openjdk-7-doc openjdk-7-jdk openjdk-7-dbg \
    openjdk-7-source openjdk-7-demo icedtea-7-plugin \
    lua5.2 lua5.2-doc luadoc \
    perl perl-doc perl-modules libpadwalker-perl \
    php5 php5-mysql phpunit php5-dev \
    python python-doc python3-doc jython jython-doc \
    ruby1.9.1-full shunit2 \
    bzr bzr-builddeb bzr-doc python-bzrlib bzrtools git git-gui git-doc \
    mercurial subversion cvs"""

CABAL = "buildwrapper scion-browser hoogle terminfo happy hlint"

PY_PACKS = "argcomplete"

class NotSudo(Exception):
    """ Throw this if we aren't sudo but need to be. """
    pass

# Functions

def install_packages():
    """ Installs required packages on this system. """
    if os.getuid() != 0:
        raise NotSudo('This stage must be run as root.')

    packages = '{} {} {}'.format(PROGRAMS, PROGRAMMING, KEYRINGS)
    packages = packages.split()

    cache = apt.Cache()
    print("One moment while we update cache.")
    cache.update()
    cache.open(None)
    print("Update done.")

    for pack in packages:
        package = cache[pack]
        if not package.is_installed:
            package.mark_install()

    cache.commit()
    cache.close()

def get_code(command, target):
    """ Wrapper function to clone repos.
    Protects against overwriting if target exists.
    command: The command that would run in bash.
    target: Where to clone to.
    """
    cmd = command.split()
    cmd.append(target)
    if not os.path.exists(target):
        subprocess.call(cmd)

def setup_config():
    """ Setup the dev environment, stuff goes in the user's home folder. """
    script_dir = os.path.realpath(__file__)
    script_dir = script_dir[0:script_dir.rindex(os.path.sep)]
    src = script_dir + '/vim_and_bash_config'
    dst = os.path.expanduser('~')

    # Link to config files, copy .vim folder.
    for fil in ['.vimrc', '.bash_aliases']:
        sfile = src + '/' + fil
        dfile = dst + '/' + fil
        if not os.path.exists(dfile):
            os.symlink(sfile, dfile)

    # Copy vim folder if not there.
    ddir = dst + '/.vim'
    if not os.path.exists(ddir):
        shutil.copytree(src.format('.vim'), ddir)

    # Init NeoBundle.
    ddir = dst + '/.vim/bundle'
    if not os.path.exists(ddir):
        print('Creating bunlde dir ' + ddir)
        os.mkdir(ddir)
    get_code('git clone https://github.com/Shougo/neobundle.vim',
             dst + '/.vim/bundle/neobundle.vim')

    # Setup git/hg prompt.
    get_code('hg clone http://bitbucket.org/sjl/hg-prompt/',
             dst + '/.hg-prompt')
    get_code('git clone git@github.com:magicmonty/bash-git-prompt.git',
             dst + '/.bash-git-prompt')

    # Setup powerline fonts if not done.
    ddir = dst + '/.fonts'
    if not os.path.exists(ddir):
        os.mkdir(ddir)
        get_code('git clone https://github.com/Lokaltog/powerline-fonts',
                 ddir + '/powerline-fonts')
        subprocess.call(['fc-cache', '-vf', ddir])

def install_cabal():
    """ Installs locally some haskell packages for Eclipse Haskell plugin. """
    cmd = ['cabal', 'update']
    subprocess.call(cmd)

    cmd = 'cabal install {}'.format(CABAL)
    cmd = cmd.split()
    subprocess.call(cmd)

def py_packages():
    """ Installs python packages using pip. """
    if os.geteuid() != 0:
        raise NotSudo('This stage must be run as root.')

    # Use python package manager.
    cmd = 'pip install ' + PY_PACKS
    subprocess.call(cmd.split())

    # Install python completion to system bash_completion.d.
    cmd = 'activate-global-python-argcomplete'
    subprocess.call(cmd.split())

def take_choice(choice):
    """ Simple wrapper function to select right action. """
    choice = int(choice)
    if choice == 1:
        install_packages()
    elif choice == 2:
        setup_config()
    elif choice == 3:
        install_cabal()
    elif choice == 4:
        py_packages()

if __name__ == '__main__':
    DESC = """ This program sets up a vanilla Ubuntu install.
    Pass a number to determine step.
    1 -> Install most packages.
    2 -> Setup vim, bash_aliases and development environment.
    3 -> Setup cabal for haskell development.
    4 -> Setup python packages through pip.
    """
    PARSER = argparse.ArgumentParser(description=DESC)
    PARSER.add_argument('choice', action='store', help='the stage')

    ARGS = PARSER.parse_args()  # Default parses argv[1:]

    try:
        take_choice(ARGS.choice)
    except IOError as exc:
        print('Failed to install: {}'.format(exc))
    except NotSudo as exc:
        print("Rerun this part of script with sudo.")
<|endoftext|>


<commit_before>from __future__ import print_function
import opentracing as ot
from instana import internal_tracer
from instana.log import logger
import opentracing.ext.tags as ext
import wrapt
import os

DJ_INSTANA_MIDDLEWARE = 'instana.django.InstanaMiddleware'

try:
    from django.utils.deprecation import MiddlewareMixin
except ImportError:
    MiddlewareMixin = object

class InstanaMiddleware(MiddlewareMixin):
    """ Django Middleware to provide request tracing for Instana """
    def __init__(self, get_response=None):
        self.get_response = get_response
        self

    def process_request(self, request):
        env = request.environ
        if 'HTTP_X_INSTANA_T' in env and 'HTTP_X_INSTANA_S' in env:
            ctx = internal_tracer.extract(ot.Format.HTTP_HEADERS, env)
            span = internal_tracer.start_span("django", child_of=ctx)
        else:
            span = internal_tracer.start_span("django")

        span.set_tag(ext.HTTP_URL, env['PATH_INFO'])
        span.set_tag("http.params", env['QUERY_STRING'])
        span.set_tag(ext.HTTP_METHOD, request.method)
        span.set_tag("http.host", env['HTTP_HOST'])
        self.span = span

    def process_response(self, request, response):
        if self.span:
            if 500 <= response.status_code <= 511:
                self.span.set_tag("error", True)
                ec = self.span.tags.get('ec', 0)
                if ec is 0:
                    self.span.set_tag("ec", ec+1)

            self.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
            internal_tracer.inject(self.span.context, ot.Format.HTTP_HEADERS, response)
            self.span.finish()
            self.span = None
        return response

    def process_exception(self, request, exception):
        if self.span:
            self.span.log_kv({'message': exception})
            self.span.set_tag("error", True)
            ec = self.span.tags.get('ec', 0)
            self.span.set_tag("ec", ec+1)

def load_middleware_wrapper(wrapped, instance, args, kwargs):
    try:
        from django.conf import settings

        if DJ_INSTANA_MIDDLEWARE in settings.MIDDLEWARE:
            return

        if type(settings.MIDDLEWARE) is tuple:
            settings.MIDDLEWARE = (DJ_INSTANA_MIDDLEWARE,) + settings.MIDDLEWARE
        elif type(settings.MIDDLEWARE) is list:
            settings.MIDDLEWARE = [DJ_INSTANA_MIDDLEWARE] + settings.MIDDLEWARE
        else:
            logger.warn("Instana: Couldn't add InstanaMiddleware to Django")

        return wrapped(*args, **kwargs)

    except Exception as e:
            logger.warn("Instana: Couldn't add InstanaMiddleware to Django: ", e)

def hook(module):
    """ Hook method to install the Instana middleware into Django >= 1.10 """
    if "INSTANA_DEV" in os.environ:
        print("==============================================================")
        print("Instana: Running django hook")
        print("==============================================================")
    wrapt.wrap_function_wrapper(module, 'BaseHandler.load_middleware', load_middleware_wrapper)

def hook19(module):
    try:
        """ Hook method to install the Instana middleware into Django <= 1.9 """
        if "INSTANA_DEV" in os.environ:
            print("==============================================================")
            print("Instana: Running django19 hook")
            print("==============================================================")

        if DJ_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE_CLASSES:
            return

        if type(module.settings.MIDDLEWARE_CLASSES) is tuple:
            module.settings.MIDDLEWARE_CLASSES = (DJ_INSTANA_MIDDLEWARE,) + module.settings.MIDDLEWARE_CLASSES
        elif type(module.settings.MIDDLEWARE_CLASSES) is list:
            module.settings.MIDDLEWARE_CLASSES = [DJ_INSTANA_MIDDLEWARE] + module.settings.MIDDLEWARE_CLASSES
        else:
            logger.warn("Instana: Couldn't add InstanaMiddleware to Django")

    except Exception as e:
            logger.warn("Instana: Couldn't add InstanaMiddleware to Django: ", e)
<commit_msg>Support old-style MIDDLEWARE_CLASSES<commit_after>from __future__ import print_function
import opentracing as ot
from instana import internal_tracer
from instana.log import logger
import opentracing.ext.tags as ext
import wrapt
import os

DJ_INSTANA_MIDDLEWARE = 'instana.django.InstanaMiddleware'

try:
    from django.utils.deprecation import MiddlewareMixin
except ImportError:
    MiddlewareMixin = object

class InstanaMiddleware(MiddlewareMixin):
    """ Django Middleware to provide request tracing for Instana """
    def __init__(self, get_response=None):
        self.get_response = get_response
        self

    def process_request(self, request):
        env = request.environ
        if 'HTTP_X_INSTANA_T' in env and 'HTTP_X_INSTANA_S' in env:
            ctx = internal_tracer.extract(ot.Format.HTTP_HEADERS, env)
            span = internal_tracer.start_span("django", child_of=ctx)
        else:
            span = internal_tracer.start_span("django")

        span.set_tag(ext.HTTP_URL, env['PATH_INFO'])
        span.set_tag("http.params", env['QUERY_STRING'])
        span.set_tag(ext.HTTP_METHOD, request.method)
        span.set_tag("http.host", env['HTTP_HOST'])
        self.span = span

    def process_response(self, request, response):
        if self.span:
            if 500 <= response.status_code <= 511:
                self.span.set_tag("error", True)
                ec = self.span.tags.get('ec', 0)
                if ec is 0:
                    self.span.set_tag("ec", ec+1)

            self.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
            internal_tracer.inject(self.span.context, ot.Format.HTTP_HEADERS, response)
            self.span.finish()
            self.span = None
        return response

    def process_exception(self, request, exception):
        if self.span:
            self.span.log_kv({'message': exception})
            self.span.set_tag("error", True)
            ec = self.span.tags.get('ec', 0)
            self.span.set_tag("ec", ec+1)

def load_middleware_wrapper(wrapped, instance, args, kwargs):
    try:
        from django.conf import settings

        # Django >=1.10 to <2.0 support old-style MIDDLEWARE_CLASSES so we
        # do as well here
        if hasattr(settings, 'MIDDLEWARE_CLASSES'):
            if DJ_INSTANA_MIDDLEWARE in settings.MIDDLEWARE_CLASSES:
                return wrapped(*args, **kwargs)

            if type(settings.MIDDLEWARE_CLASSES) is tuple:
                settings.MIDDLEWARE_CLASSES = (DJ_INSTANA_MIDDLEWARE,) + settings.MIDDLEWARE_CLASSES
            elif type(settings.MIDDLEWARE_CLASSES) is list:
                settings.MIDDLEWARE_CLASSES = [DJ_INSTANA_MIDDLEWARE] + settings.MIDDLEWARE_CLASSES
            else:
                logger.warn("Instana: Couldn't add InstanaMiddleware to Django")
        elif hasattr(settings, 'MIDDLEWARE'):
            if DJ_INSTANA_MIDDLEWARE in settings.MIDDLEWARE:
                return wrapped(*args, **kwargs)

            if type(settings.MIDDLEWARE) is tuple:
                settings.MIDDLEWARE = (DJ_INSTANA_MIDDLEWARE,) + settings.MIDDLEWARE
            elif type(settings.MIDDLEWARE) is list:
                settings.MIDDLEWARE = [DJ_INSTANA_MIDDLEWARE] + settings.MIDDLEWARE
            else:
                logger.warn("Instana: Couldn't add InstanaMiddleware to Django")
        else:
            logger.warn("Instana: Couldn't find middleware settings")

        return wrapped(*args, **kwargs)
    except Exception as e:
            logger.warn("Instana: Couldn't add InstanaMiddleware to Django: ", e)

def hook(module):
    """ Hook method to install the Instana middleware into Django >= 1.10 """
    if "INSTANA_DEV" in os.environ:
        print("==============================================================")
        print("Instana: Running django hook")
        print("==============================================================")
    wrapt.wrap_function_wrapper(module, 'BaseHandler.load_middleware', load_middleware_wrapper)

def hook19(module):
    try:
        """ Hook method to install the Instana middleware into Django <= 1.9 """
        if "INSTANA_DEV" in os.environ:
            print("==============================================================")
            print("Instana: Running django19 hook")
            print("==============================================================")

        if DJ_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE_CLASSES:
            return

        if type(module.settings.MIDDLEWARE_CLASSES) is tuple:
            module.settings.MIDDLEWARE_CLASSES = (DJ_INSTANA_MIDDLEWARE,) + module.settings.MIDDLEWARE_CLASSES
        elif type(module.settings.MIDDLEWARE_CLASSES) is list:
            module.settings.MIDDLEWARE_CLASSES = [DJ_INSTANA_MIDDLEWARE] + module.settings.MIDDLEWARE_CLASSES
        else:
            logger.warn("Instana: Couldn't add InstanaMiddleware to Django")

    except Exception as e:
            logger.warn("Instana: Couldn't add InstanaMiddleware to Django: ", e)
<|endoftext|>


<commit_before>from collections import OrderedDict
from itertools import chain
import json
import re
import pprint
import logging

from django.conf import settings

from pyelasticsearch import ElasticSearch
from pyelasticsearch.exceptions import InvalidJsonResponseError
from reversion.models import VERSION_ADD, VERSION_CHANGE, VERSION_DELETE

from editorsnotes.main.models import Project, User

from .types import DocumentTypeAdapter
from .utils import clean_query_string

__all__ = ['ElasticSearchIndex', 'ENIndex' ]

logger = logging.getLogger('editorsnotes.search')

class OrderedResponseElasticSearch(ElasticSearch):
    def _decode_response(self, response):
        try:
            json_response = json.loads(response.content,
                                       object_pairs_hook=OrderedDict)
        except ValueError:
            raise InvalidJsonResponseError(response)
        return json_response

class ElasticSearchIndex(object):
    def __init__(self):
        if not hasattr(self, 'get_name'):
            raise NotImplementedError('Must implement get_name method')
        self.name = self.get_name()
        self.is_open = False

    def _open(self):
        pass

    def open(self):
        if self.is_open:
            return self.es
        logger.info('opening ElasticSearch')
        self.es = OrderedResponseElasticSearch(settings.ELASTICSEARCH_URLS)
        self.is_open = True
        if not self.exists():
            logger.info('Creating ElasticSearch index')
            self.create()
            self.created = True
        else:
            self.created = False
        self._open()
        return self.es

    def get_settings(self):
        return {}

    def put_mapping(self):
        "Override to put mappings for inherited indices."
        pass

    def exists(self):
        #server_url, _ = self.open().servers.get()
        #resp = self.es.session.head(server_url + '/' + self.name)
        #return resp.status_code == 200
        try:
            self.open().send_request('HEAD', [self.name])
        except InvalidJsonResponseError as exc:
            if exc.response.status_code == 200:
                return True
        return False

    def create(self):
        print 'Creating index '+self.name
        created = self.open().create_index(self.name, self.get_settings())
        self.put_mapping()
        return created

    def delete(self):
        self.open()
        return self.open().delete_index(self.name)

class ENIndex(ElasticSearchIndex):
    def __init__(self, onOpen=None):
        self.document_types = {}
        self.onOpen = onOpen
        super(ENIndex, self).__init__()

    def _open(self):
        if self.onOpen:
            apply(self.onOpen, [self])

    def put_mapping(self):
        self.open()
        self.put_type_mappings()

    def get_name(self):
        return settings.ELASTICSEARCH_PREFIX + '-items'

    def get_settings(self):
        return {
            'settings': {
                'index': {
                    'analysis': {
                        'analyzer': {
                            'analyzer_shingle': {
                                'tokenizer': 'standard',
                                'filter': ['standard', 'lowercase', 'filter_shingle']
                            }
                        },
                        'filter': {
                            'filter_shingle': {
                                'type': 'shingle',
                                'max_shingle_size': 5,
                                'min_shingle_size': 2,
                                'output_unigrams': True
                            }
                        }
                    }
                }
            }
        }

    def put_type_mappings(self):
        existing_types = self.es.get_mapping(index=self.name)\
                .get(self.name, {})\
                .get('mappings', {})\
                .keys()

        unmapped_types = (d for d,dt in self.document_types.items()
                          if dt.type_label not in existing_types)

        # TODO: Warn/log when a field's type mapping changes
        for document_type in unmapped_types:
            logger.info('put type mapping for %s', document_type.__name__)
            self.document_types[document_type].put_mapping()

    def register(self, model, adapter=None, highlight_fields=None,
                 display_field=None):
        if adapter is None:
            doc_type = DocumentTypeAdapter(self, self.name, model,
                                           highlight_fields, display_field)
        else:
            doc_type = adapter(self, self.name, model)
        self.document_types[model] = doc_type

        self.put_type_mappings()

    def data_for_object(self, obj):
        self.open()
        doc_type = self.document_types.get(obj.__class__, None)
        if doc_type is None:
            return None

        # will raise an exception if it's not there! should change
        return self.es.get(index=self.name, doc_type=doc_type.type_label,
                           id=obj.id)

    def search_model(self, model, query, **kwargs):
        self.open()
        if doc_type is None:
            return None
        doc_type = self.document_types.get(model)
        return self.es.search(query, index=self.name,
                              doc_type=doc_type.type_label, **kwargs)

    def search(self, query, highlight=False, **kwargs):
        self.open()
        if isinstance(query, basestring):
            prepared_query = {
                'query': {
                    'query_string': { 'query': clean_query_string(query) }
                }
            }

        else:
            prepared_query = query

        if 'project' in kwargs:
            project = kwargs['project']
            del kwargs['project']
            prepared_query['filter'] = {
                "term": { 'serialized.project.name': project }
            }

        if highlight:
            prepared_query['highlight'] = {
                'fields': {},
                'pre_tags': [''],
                'post_tags': ['']
            }
            highlight_fields = chain(
                *[doc_type.highlight_fields
                for doc_type in self.document_types.values()])
            for field_name in highlight_fields:
                prepared_query['highlight']['fields'][field_name] = {}

        pprint.pprint(prepared_query)
        return self.open().search(prepared_query, index=self.name, **kwargs)

VERSION_ACTIONS = {
    VERSION_ADD: 'added',
    VERSION_CHANGE: 'changed',
    VERSION_DELETE: 'deleted'
}

class ActivityIndex(ElasticSearchIndex):
    def get_name(self):
        return settings.ELASTICSEARCH_PREFIX + '-activitylog'
    def get_activity_for(self, entity, size=25, **kwargs):
        query = {
            'query': {},
            'sort': {'data.time': { 'order': 'desc', 'ignore_unmapped': True }}
        }
        if isinstance(entity, User):
            query['query']['match'] = { 'data.user': entity.username }
        elif isinstance(entity, Project):
            query['query']['match'] = { 'data.project': entity.slug }
        else:
            raise ValueError('Must pass either project or user')
        query.update(kwargs)
        search = self.open().search(query, index=self.name, size=size)
        return [ hit['_source']['data'] for hit in search['hits']['hits'] ]

    def data_from_reversion_version(self, version):
        url = version.object and version.object.get_absolute_url()
        return {
            'data': OrderedDict((
                ('user', version.revision.user.username,),
                ('project', version.revision.project_metadata.project.slug,),
                ('time', version.revision.date_created,),
                ('type', version.content_type.model,),
                ('url', url),
                ('title', version.object_repr,),
                ('action', VERSION_ACTIONS[version.type],),
            )),
            'object_id': version.object_id_int,
            'version_id': version.id
        }

    def handle_edit(self, instance, version):
        self.open().index(self.name, 'activity',
                      self.data_from_reversion_version(version))
<commit_msg>fixed the UnboundLocalError<commit_after>from collections import OrderedDict
from itertools import chain
import json
import re
import pprint
import logging

from django.conf import settings

from pyelasticsearch import ElasticSearch
from pyelasticsearch.exceptions import InvalidJsonResponseError
from reversion.models import VERSION_ADD, VERSION_CHANGE, VERSION_DELETE

from editorsnotes.main.models import Project, User

from .types import DocumentTypeAdapter
from .utils import clean_query_string

__all__ = ['ElasticSearchIndex', 'ENIndex' ]

logger = logging.getLogger('editorsnotes.search')

class OrderedResponseElasticSearch(ElasticSearch):
    def _decode_response(self, response):
        try:
            json_response = json.loads(response.content,
                                       object_pairs_hook=OrderedDict)
        except ValueError:
            raise InvalidJsonResponseError(response)
        return json_response

class ElasticSearchIndex(object):
    def __init__(self):
        if not hasattr(self, 'get_name'):
            raise NotImplementedError('Must implement get_name method')
        self.name = self.get_name()
        self.is_open = False

    def _open(self):
        pass

    def open(self):
        if self.is_open:
            return self.es
        logger.info('opening ElasticSearch')
        self.es = OrderedResponseElasticSearch(settings.ELASTICSEARCH_URLS)
        self.is_open = True
        if not self.exists():
            logger.info('Creating ElasticSearch index')
            self.create()
            self.created = True
        else:
            self.created = False
        self._open()
        return self.es

    def get_settings(self):
        return {}

    def put_mapping(self):
        "Override to put mappings for inherited indices."
        pass

    def exists(self):
        #server_url, _ = self.open().servers.get()
        #resp = self.es.session.head(server_url + '/' + self.name)
        #return resp.status_code == 200
        try:
            self.open().send_request('HEAD', [self.name])
        except InvalidJsonResponseError as exc:
            if exc.response.status_code == 200:
                return True
        return False

    def create(self):
        print 'Creating index '+self.name
        created = self.open().create_index(self.name, self.get_settings())
        self.put_mapping()
        return created

    def delete(self):
        self.open()
        return self.open().delete_index(self.name)

class ENIndex(ElasticSearchIndex):
    def __init__(self, onOpen=None):
        self.document_types = {}
        self.onOpen = onOpen
        super(ENIndex, self).__init__()

    def _open(self):
        if self.onOpen:
            apply(self.onOpen, [self])

    def put_mapping(self):
        self.open()
        self.put_type_mappings()

    def get_name(self):
        return settings.ELASTICSEARCH_PREFIX + '-items'

    def get_settings(self):
        return {
            'settings': {
                'index': {
                    'analysis': {
                        'analyzer': {
                            'analyzer_shingle': {
                                'tokenizer': 'standard',
                                'filter': ['standard', 'lowercase', 'filter_shingle']
                            }
                        },
                        'filter': {
                            'filter_shingle': {
                                'type': 'shingle',
                                'max_shingle_size': 5,
                                'min_shingle_size': 2,
                                'output_unigrams': True
                            }
                        }
                    }
                }
            }
        }

    def put_type_mappings(self):
        existing_types = self.es.get_mapping(index=self.name)\
                .get(self.name, {})\
                .get('mappings', {})\
                .keys()

        unmapped_types = (d for d,dt in self.document_types.items()
                          if dt.type_label not in existing_types)

        # TODO: Warn/log when a field's type mapping changes
        for document_type in unmapped_types:
            logger.info('put type mapping for %s', document_type.__name__)
            self.document_types[document_type].put_mapping()

    def register(self, model, adapter=None, highlight_fields=None,
                 display_field=None):
        if adapter is None:
            doc_type = DocumentTypeAdapter(self, self.name, model,
                                           highlight_fields, display_field)
        else:
            doc_type = adapter(self, self.name, model)
        self.document_types[model] = doc_type

        self.put_type_mappings()

    def data_for_object(self, obj):
        self.open()
        doc_type = self.document_types.get(obj.__class__, None)
        if doc_type is None:
            return None

        # will raise an exception if it's not there! should change
        return self.es.get(index=self.name, doc_type=doc_type.type_label,
                           id=obj.id)

    def search_model(self, model, query, **kwargs):
        self.open()

        doc_type = self.document_types.get(model)
        if doc_type is None:
            return None
        return self.es.search(query, index=self.name,
                              doc_type=doc_type.type_label, **kwargs)

    def search(self, query, highlight=False, **kwargs):
        self.open()
        if isinstance(query, basestring):
            prepared_query = {
                'query': {
                    'query_string': { 'query': clean_query_string(query) }
                }
            }

        else:
            prepared_query = query

        if 'project' in kwargs:
            project = kwargs['project']
            del kwargs['project']
            prepared_query['filter'] = {
                "term": { 'serialized.project.name': project }
            }

        if highlight:
            prepared_query['highlight'] = {
                'fields': {},
                'pre_tags': [''],
                'post_tags': ['']
            }
            highlight_fields = chain(
                *[doc_type.highlight_fields
                for doc_type in self.document_types.values()])
            for field_name in highlight_fields:
                prepared_query['highlight']['fields'][field_name] = {}

        pprint.pprint(prepared_query)
        return self.open().search(prepared_query, index=self.name, **kwargs)

VERSION_ACTIONS = {
    VERSION_ADD: 'added',
    VERSION_CHANGE: 'changed',
    VERSION_DELETE: 'deleted'
}

class ActivityIndex(ElasticSearchIndex):
    def get_name(self):
        return settings.ELASTICSEARCH_PREFIX + '-activitylog'
    def get_activity_for(self, entity, size=25, **kwargs):
        query = {
            'query': {},
            'sort': {'data.time': { 'order': 'desc', 'ignore_unmapped': True }}
        }
        if isinstance(entity, User):
            query['query']['match'] = { 'data.user': entity.username }
        elif isinstance(entity, Project):
            query['query']['match'] = { 'data.project': entity.slug }
        else:
            raise ValueError('Must pass either project or user')
        query.update(kwargs)
        search = self.open().search(query, index=self.name, size=size)
        return [ hit['_source']['data'] for hit in search['hits']['hits'] ]

    def data_from_reversion_version(self, version):
        url = version.object and version.object.get_absolute_url()
        return {
            'data': OrderedDict((
                ('user', version.revision.user.username,),
                ('project', version.revision.project_metadata.project.slug,),
                ('time', version.revision.date_created,),
                ('type', version.content_type.model,),
                ('url', url),
                ('title', version.object_repr,),
                ('action', VERSION_ACTIONS[version.type],),
            )),
            'object_id': version.object_id_int,
            'version_id': version.id
        }

    def handle_edit(self, instance, version):
        self.open().index(self.name, 'activity',
                      self.data_from_reversion_version(version))
<|endoftext|>


<commit_before># Copyright 2014 - Rackspace Hosting
#
#    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.

"""Solum Conductor default handler."""

from solum.deployer import api
from solum import objects
from solum.objects import image
from solum.openstack.common.gettextutils import _
from solum.openstack.common import log as logging

LOG = logging.getLogger(__name__)

IMAGE_STATES = image.States

class Handler(object):
    def __init__(self):
        super(Handler, self).__init__()
        objects.load()

    def echo(self, ctxt, message):
        LOG.debug(_("%s") % message)

    def build_job_update(self, ctxt, build_id, status, reason,
                         created_image_id, assembly_id):
        image = objects.registry.Image.get_by_id(ctxt, build_id)
        image.status = status
        image.reason = reason
        image.created_image_id = created_image_id
        image.save(ctxt)

        # create the component if needed.
        if assembly_id is not None:
            assem = objects.registry.Assembly.get_by_id(ctxt,
                                                        assembly_id)
            if not any([comp for comp in assem.components
                        if 'Image Build' in comp.description]):
                objects.registry.Component.assign_and_create(ctxt, assem,
                                                             'Image Build',
                                                             'Image Build job',
                                                             created_image_id)
            if image.status == IMAGE_STATES.COMPLETE:
                api.API(context=ctxt).deploy(assembly_id=assem.id,
                                             image_id=image.created_image_id)
<commit_msg>Update image 'state' and 'description' fields<commit_after># Copyright 2014 - Rackspace Hosting
#
#    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.

"""Solum Conductor default handler."""

from solum.deployer import api
from solum import objects
from solum.objects import image
from solum.openstack.common.gettextutils import _
from solum.openstack.common import log as logging

LOG = logging.getLogger(__name__)

IMAGE_STATES = image.States

class Handler(object):
    def __init__(self):
        super(Handler, self).__init__()
        objects.load()

    def echo(self, ctxt, message):
        LOG.debug(_("%s") % message)

    def build_job_update(self, ctxt, build_id, state, description,
                         created_image_id, assembly_id):
        image = objects.registry.Image.get_by_id(ctxt, build_id)
        image.state = state
        image.description = description
        image.created_image_id = created_image_id
        image.save(ctxt)

        # create the component if needed.
        if assembly_id is not None:
            assem = objects.registry.Assembly.get_by_id(ctxt,
                                                        assembly_id)
            if not any([comp for comp in assem.components
                        if 'Image Build' in comp.description]):
                objects.registry.Component.assign_and_create(ctxt, assem,
                                                             'Image Build',
                                                             'Image Build job',
                                                             created_image_id)
            if image.status == IMAGE_STATES.COMPLETE:
                api.API(context=ctxt).deploy(assembly_id=assem.id,
                                             image_id=image.created_image_id)
<|endoftext|>


<commit_before>from uuid import uuid4

from eho.server.storage.storage import db

class BaseModel(object):
    def __getitem__(self, item):
        return getattr(self, item)

    def __setitem__(self, key, value):
        setattr(self, key, value)

class NodeTemplate(db.Model, BaseModel):
    __tablename__ = 'NodeTemplate'

    id = db.Column(db.String(36), primary_key=True)
    name = db.Column(db.String(80), unique=True, nullable=False)
    node_type_id = db.Column(db.String(36), db.ForeignKey('NodeType.id'),
                             nullable=False)
    tenant_id = db.Column(db.String(36), nullable=False)  # is it needed?
    flavor_id = db.Column(db.String(36), nullable=False)

    node_template_configs = db.relationship('NodeTemplateConfig',
                                            backref='node_template')
    cluster_node_counts = db.relationship('ClusterNodeCount',
                                          backref='node_template')

    def __init__(self, name, node_type_id, tenant_id, flavor_id):
        self.id = uuid4().hex
        self.name = name
        self.node_type_id = node_type_id
        self.tenant_id = tenant_id
        self.flavor_id = flavor_id

    def __repr__(self):
        return '<NodeTemplate %s / %s>' % (self.name, self.node_type_id)

class Cluster(db.Model, BaseModel):
    __tablename__ = 'Cluster'

    id = db.Column(db.String(36), primary_key=True)
    name = db.Column(db.String(80), unique=True, nullable=False)
    base_image_id = db.Column(db.String(36), nullable=False)
    status = db.Column(db.String(80))
    tenant_id = db.Column(db.String(36), nullable=False)

    nodes = db.relationship('Node', backref='cluster')
    service_urls = db.relationship('ServiceUrl', backref='cluster')
    node_counts = db.relationship('ClusterNodeCount', backref='cluster')

    # node_templates: [(node_template_id, count), ...]

    def __init__(self, name, base_image_id, tenant_id, status=None):
        self.id = uuid4().hex
        self.name = name
        self.base_image_id = base_image_id
        self.status = status
        self.tenant_id = tenant_id

    def __repr__(self):
        return '<Cluster %s / %s>' % (self.name, self.status)

node_type_node_process = db.Table('NodeType_NodeProcess', db.metadata,
                                  db.Column('node_type_id', db.String(36),
                                            db.ForeignKey('NodeType.id')),
                                  db.Column('node_process_id', db.String(36),
                                            db.ForeignKey('NodeProcess.id')))

class NodeType(db.Model, BaseModel):
    __tablename__ = 'NodeType'

    id = db.Column(db.String(36), primary_key=True)
    name = db.Column(db.String(80), unique=True, nullable=False)
    processes = db.relationship('NodeProcess',
                                secondary=node_type_node_process,
                                backref='node_types')
    node_templates = db.relationship('NodeTemplate', backref='node_type')

    def __init__(self, name):
        self.id = uuid4().hex
        self.name = name

    def __repr__(self):
        return '<NodeType %s>' % self.name

class NodeProcess(db.Model, BaseModel):
    __tablename__ = 'NodeProcess'

    id = db.Column(db.String(36), primary_key=True)
    name = db.Column(db.String(80), unique=True, nullable=False)
    node_process_properties = db.relationship('NodeProcessProperty',
                                              backref='node_process')

    def __init__(self, name):
        self.id = uuid4().hex
        self.name = name

    def __repr__(self):
        return '<NodeProcess %s>' % self.name

class NodeProcessProperty(db.Model, BaseModel):
    __tablename__ = 'NodeProcessProperty'
    __table_args__ = (
        db.UniqueConstraint('node_process_id', 'name'),
    )

    id = db.Column(db.String(36), primary_key=True)
    node_process_id = db.Column(db.String(36), db.ForeignKey('NodeProcess.id'))
    name = db.Column(db.String(80), nullable=False)
    required = db.Column(db.Boolean, nullable=False)
    default = db.Column(db.String(36))
    node_template_configs = db.relationship('NodeTemplateConfig',
                                            backref='node_process_property')

    def __init__(self, node_process_id, name, required=True, default=None):
        self.id = uuid4().hex
        self.node_process_id = node_process_id
        self.name = name
        self.required = required
        self.default = default

    def __repr__(self):
        return '<NodeProcessProperty %s>' % self.name

class NodeTemplateConfig(db.Model, BaseModel):
    __tablename__ = 'NodeTemplateConfig'
    __table_args__ = (
        db.UniqueConstraint('node_template_id', 'node_process_property_id'),
    )

    id = db.Column(db.String(36), primary_key=True)
    node_template_id = db.Column(
        db.String(36),
        db.ForeignKey('NodeTemplate.id'))
    node_process_property_id = db.Column(
        db.String(36),
        db.ForeignKey('NodeProcessProperty.id'))
    value = db.Column(db.String(36))

    def __init__(self, node_template_id, node_process_property_id, value):
        self.id = uuid4().hex
        self.node_template_id = node_template_id
        self.node_process_property_id = node_process_property_id
        self.value = value

    def __repr__(self):
        return '<NodeTemplateConfig %s.%s / %s>' \
               % (self.node_template_id, self.node_process_property_id,
                  self.value)

class ClusterNodeCount(db.Model, BaseModel):
    __tablename__ = 'ClusterNodeCount'
    __table_args__ = (
        db.UniqueConstraint('cluster_id', 'node_template_id'),
    )

    id = db.Column(db.String(36), primary_key=True)
    cluster_id = db.Column(db.String(36), db.ForeignKey('Cluster.id'))
    node_template_id = db.Column(db.String(36),
                                 db.ForeignKey('NodeTemplate.id'))
    count = db.Column(db.Integer, nullable=False)

    def __init__(self, cluster_id, node_template_id, count):
        self.id = uuid4().hex
        self.cluster_id = cluster_id
        self.node_template_id = node_template_id
        self.count = count

    def __repr__(self):
        return '<ClusterNodeCount %s / %s>' \
               % (self.node_template_id, self.count)

class Node(db.Model, BaseModel):
    __tablename__ = 'Node'

    # do we need own id?
    vm_id = db.Column(db.String(36), primary_key=True)
    cluster_id = db.Column(db.String(36), db.ForeignKey('Cluster.id'))
    node_template_id = db.Column(db.String(36),
                                 db.ForeignKey('NodeTemplate.id'))

    def __init__(self, vm_id, cluster_id, node_template_id):
        self.vm_id = vm_id
        self.cluster_id = cluster_id
        self.node_template_id = node_template_id

    def __repr__(self):
        return '<Node based on %s>' % self.node_template.name

class ServiceUrl(db.Model, BaseModel):
    __tablename__ = 'ServiceUrl'

    id = db.Column(db.String(36), primary_key=True)
    cluster_id = db.Column(db.String(36), db.ForeignKey('Cluster.id'))
    name = db.Column(db.String(80))
    url = db.Column(db.String(80), nullable=False)

    def __init__(self, cluster_id, name, url):
        self.id = uuid4().hex
        self.cluster_id = cluster_id
        self.name = name
        self.url = url

    def __repr__(self):
        return '<ServiceUrl %s / %s>' % (self.name, self.url)
<commit_msg>cascade options has been added<commit_after>from uuid import uuid4

from eho.server.storage.storage import db

class BaseModel(object):
    def __getitem__(self, item):
        return getattr(self, item)

    def __setitem__(self, key, value):
        setattr(self, key, value)

class NodeTemplate(db.Model, BaseModel):
    __tablename__ = 'NodeTemplate'

    id = db.Column(db.String(36), primary_key=True)
    name = db.Column(db.String(80), unique=True, nullable=False)
    node_type_id = db.Column(db.String(36), db.ForeignKey('NodeType.id'),
                             nullable=False)
    tenant_id = db.Column(db.String(36), nullable=False)  # is it needed?
    flavor_id = db.Column(db.String(36), nullable=False)

    node_template_configs = db.relationship('NodeTemplateConfig',
                                            cascade="all,delete",
                                            backref='node_template')
    cluster_node_counts = db.relationship('ClusterNodeCount',
                                          cascade="all,delete",
                                          backref='node_template')
    nodes = db.relationship('Node', cascade="all,delete",
                            backref='node_template')

    def __init__(self, name, node_type_id, tenant_id, flavor_id):
        self.id = uuid4().hex
        self.name = name
        self.node_type_id = node_type_id
        self.tenant_id = tenant_id
        self.flavor_id = flavor_id

    def __repr__(self):
        return '<NodeTemplate %s / %s>' % (self.name, self.node_type_id)

class Cluster(db.Model, BaseModel):
    __tablename__ = 'Cluster'

    id = db.Column(db.String(36), primary_key=True)
    name = db.Column(db.String(80), unique=True, nullable=False)
    base_image_id = db.Column(db.String(36), nullable=False)
    status = db.Column(db.String(80))
    tenant_id = db.Column(db.String(36), nullable=False)

    nodes = db.relationship('Node', cascade="all,delete", backref='cluster')
    service_urls = db.relationship('ServiceUrl', cascade="all,delete",
                                   backref='cluster')
    node_counts = db.relationship('ClusterNodeCount', cascade="all,delete",
                                  backref='cluster')

    # node_templates: [(node_template_id, count), ...]

    def __init__(self, name, base_image_id, tenant_id, status=None):
        self.id = uuid4().hex
        self.name = name
        self.base_image_id = base_image_id
        self.status = status
        self.tenant_id = tenant_id

    def __repr__(self):
        return '<Cluster %s / %s>' % (self.name, self.status)

node_type_node_process = db.Table('NodeType_NodeProcess', db.metadata,
                                  db.Column('node_type_id', db.String(36),
                                            db.ForeignKey('NodeType.id')),
                                  db.Column('node_process_id', db.String(36),
                                            db.ForeignKey('NodeProcess.id')))

class NodeType(db.Model, BaseModel):
    __tablename__ = 'NodeType'

    id = db.Column(db.String(36), primary_key=True)
    name = db.Column(db.String(80), unique=True, nullable=False)
    processes = db.relationship('NodeProcess',
                                cascade="all,delete",
                                secondary=node_type_node_process,
                                backref='node_types')
    node_templates = db.relationship('NodeTemplate', cascade="all,delete",
                                     backref='node_type')

    def __init__(self, name):
        self.id = uuid4().hex
        self.name = name

    def __repr__(self):
        return '<NodeType %s>' % self.name

class NodeProcess(db.Model, BaseModel):
    __tablename__ = 'NodeProcess'

    id = db.Column(db.String(36), primary_key=True)
    name = db.Column(db.String(80), unique=True, nullable=False)
    node_process_properties = db.relationship('NodeProcessProperty',
                                              cascade="all,delete",
                                              backref='node_process')

    def __init__(self, name):
        self.id = uuid4().hex
        self.name = name

    def __repr__(self):
        return '<NodeProcess %s>' % self.name

class NodeProcessProperty(db.Model, BaseModel):
    __tablename__ = 'NodeProcessProperty'
    __table_args__ = (
        db.UniqueConstraint('node_process_id', 'name'),
    )

    id = db.Column(db.String(36), primary_key=True)
    node_process_id = db.Column(db.String(36), db.ForeignKey('NodeProcess.id'))
    name = db.Column(db.String(80), nullable=False)
    required = db.Column(db.Boolean, nullable=False)
    default = db.Column(db.String(36))
    node_template_configs = db.relationship('NodeTemplateConfig',
                                            cascade="all,delete",
                                            backref='node_process_property')

    def __init__(self, node_process_id, name, required=True, default=None):
        self.id = uuid4().hex
        self.node_process_id = node_process_id
        self.name = name
        self.required = required
        self.default = default

    def __repr__(self):
        return '<NodeProcessProperty %s>' % self.name

class NodeTemplateConfig(db.Model, BaseModel):
    __tablename__ = 'NodeTemplateConfig'
    __table_args__ = (
        db.UniqueConstraint('node_template_id', 'node_process_property_id'),
    )

    id = db.Column(db.String(36), primary_key=True)
    node_template_id = db.Column(
        db.String(36),
        db.ForeignKey('NodeTemplate.id'))
    node_process_property_id = db.Column(
        db.String(36),
        db.ForeignKey('NodeProcessProperty.id'))
    value = db.Column(db.String(36))

    def __init__(self, node_template_id, node_process_property_id, value):
        self.id = uuid4().hex
        self.node_template_id = node_template_id
        self.node_process_property_id = node_process_property_id
        self.value = value

    def __repr__(self):
        return '<NodeTemplateConfig %s.%s / %s>' \
               % (self.node_template_id, self.node_process_property_id,
                  self.value)

class ClusterNodeCount(db.Model, BaseModel):
    __tablename__ = 'ClusterNodeCount'
    __table_args__ = (
        db.UniqueConstraint('cluster_id', 'node_template_id'),
    )

    id = db.Column(db.String(36), primary_key=True)
    cluster_id = db.Column(db.String(36), db.ForeignKey('Cluster.id'))
    node_template_id = db.Column(db.String(36),
                                 db.ForeignKey('NodeTemplate.id'))
    count = db.Column(db.Integer, nullable=False)

    def __init__(self, cluster_id, node_template_id, count):
        self.id = uuid4().hex
        self.cluster_id = cluster_id
        self.node_template_id = node_template_id
        self.count = count

    def __repr__(self):
        return '<ClusterNodeCount %s / %s>' \
               % (self.node_template_id, self.count)

class Node(db.Model, BaseModel):
    __tablename__ = 'Node'

    # do we need own id?
    vm_id = db.Column(db.String(36), primary_key=True)
    cluster_id = db.Column(db.String(36), db.ForeignKey('Cluster.id'))
    node_template_id = db.Column(db.String(36),
                                 db.ForeignKey('NodeTemplate.id'))

    def __init__(self, vm_id, cluster_id, node_template_id):
        self.vm_id = vm_id
        self.cluster_id = cluster_id
        self.node_template_id = node_template_id

    def __repr__(self):
        return '<Node based on %s>' % self.node_template.name

class ServiceUrl(db.Model, BaseModel):
    __tablename__ = 'ServiceUrl'

    id = db.Column(db.String(36), primary_key=True)
    cluster_id = db.Column(db.String(36), db.ForeignKey('Cluster.id'))
    name = db.Column(db.String(80))
    url = db.Column(db.String(80), nullable=False)

    def __init__(self, cluster_id, name, url):
        self.id = uuid4().hex
        self.cluster_id = cluster_id
        self.name = name
        self.url = url

    def __repr__(self):
        return '<ServiceUrl %s / %s>' % (self.name, self.url)
<|endoftext|>


<commit_before># Classify all HTML files by their copyright notices
# - None (no "Copyright" or "©" etc)
# - CC-BY-SA 4.0
# - All Rights Reserved
# - Other/Unknown
# As much as possible, find the exact beginning and end of the copyright
# notice, thus allowing them to be replaced as needed. But for now, just
# classify the files.
# Fixes to be done:
# * If copyright is "All Rights Reserved", simply remove that text. It is
#   now copyright="None".
# * If copyright is "None", add a <footer> at the end of document.body.main
#   or document.body, as per _layouts/default.html in the Markdown files.
#   It is now copyright="CC-BY-SA 4.0".
import os
import re
import sys
import collections
from bs4 import BeautifulSoup, Comment, Tag

# root = "/home/rosuav/gsarchive/live"
root = "/home/rosuav/gsarchive/clone" # Faster and safer, not touching the original files

copyright = re.compile(r"""
	(Copyright|©).*
	(
		Gilber[e]?t\s*(and|&)\s*Sulliv[ae]n\s*Arch[i]?ve
		| Paul\s*Howarth
		| Colin\s*Johnson
	)
	.*(All\s*R[io]ghts\s*Reserved)?
""", re.IGNORECASE | re.VERBOSE | re.DOTALL)

def classify(fn):
	info = { }
	with open(fn, "rb") as f: blob = f.read()
	soup = BeautifulSoup(blob, "html.parser")
	if soup.find(text=lambda text: isinstance(text, Comment) and "autogenerated" in text.lower()):
		info["generated"] = 1
	# Okay. So, what are we looking for, exactly?
	# 1) Easy mode: an anchor rel="license".
	for tag in soup.findAll(rel="license"):
		if tag["href"] == "https://creativecommons.org/licenses/by-sa/4.0/":
			return info | {"copyright": "CC-BY-SA 4.0"}
		if tag["href"] == "http://creativecommons.org/licenses/by-sa/4.0/":
			return info | {"copyright": "CC-BY-SA 4.0"} # Maybe fix protocol? Or not bother.
		if tag["href"] == "https://creativecommons.org/publicdomain/mark/1.0/":
			return info | {"copyright": "Public Domain"} # Fix to BY-SA 4.0 in case of future edits?
		info.setdefault("links", []).append(tag["href"])
	if "links" in info:
		return info | {"copyright": "Unknown"}
	text = []
	for cr in soup.findAll(text=True):
		if m := copyright.search(cr.text):
			return info | {"copyright": "All Rights Reserved"}
		# Look for any copyright marker, including a miswritten HTML entity
		if "copyright" in cr.text or "©" in cr.text or "&copy" in cr.text:
			# Maybe there's a full copyright notice in the parent's text,
			# but it's split up by HTML tags.
			if cr.parent("a", href="mailto:dstone4@cox.net"):
				# The WhoWasWho pages have a different copyright marker.
				# They probably should all get synchronized too, but maybe
				# not to the same value. Would be nice to use the same
				# format and CSS everywhere at least.
				return info | {"copyright": "David Stone"}
			if m := copyright.search(cr.parent.text):
				# Fixing this is going to be harder. But it's still an ARR
				# copyright notice.
				return info | {"copyright": "All Rights Reserved", "fix": "if possible"}
			text.append(cr.text)
	if text: return info | {"copyright": "Unknown", "text": text}
	return info | {"copyright": "None"}

for fn in sys.argv[1:]:
	if os.path.exists(fn):
		print(classify(fn))
		sys.exit(0)

stats = collections.Counter()
known_types = ["All Rights Reserved", "None", "CC-BY-SA 4.0", "Public Domain", "David Stone", "Unknown"]
with open("copywrong.log", "w") as log:
	for root, dirs, files in os.walk(root):
		for file in files:
			fn = os.path.join(root, file)
			info = classify(fn)
			stats[info["copyright"]] += 1
			if not stats.total() % 1000: print(stats.total(), stats)
			if info["copyright"] == "Unknown":
				print(fn, info, file=log)
			elif info["copyright"] not in known_types:
				print(fn, info)
				known_types.append(info["copyright"])
print(stats.total(), stats)

""" For manual testing:
def get(fn):
    global soup, cr
    soup = BeautifulSoup(open(fn).read(), "html.parser")
    cr = soup.find(text=lambda t: "Copyright" in t.text)
"""
<commit_msg>copywrong: Plan next steps<commit_after># Classify all HTML files by their copyright notices
# - None (no "Copyright" or "©" etc)
# - CC-BY-SA 4.0
# - All Rights Reserved
# - Other/Unknown
# As much as possible, find the exact beginning and end of the copyright
# notice, thus allowing them to be replaced as needed. But for now, just
# classify the files.
# Fixes to be done:
# * If copyright is "All Rights Reserved", simply remove that text. It is
#   now copyright="None".
# * If copyright is "Unknown", the page is talking ABOUT copyright, or is
#   referring to the copyright status of ancillary material, so treat it as
#   equivalent to None.
# * If copyright is "None", add a <footer> at the end of document.body.main
#   or document.body, as per _layouts/default.html in the Markdown files.
#   It is now copyright="CC-BY-SA 4.0".
# * Add a standard CSS file to every page modified.
import os
import re
import sys
import collections
from bs4 import BeautifulSoup, Comment, Tag

# root = "/home/rosuav/gsarchive/live"
root = "/home/rosuav/gsarchive/clone" # Faster and safer, not touching the original files

copyright = re.compile(r"""
	(Copyright|©).*
	(
		Gilber[e]?t\s*(and|&)\s*Sulliv[ae]n\s*Arch[i]?ve
		| Paul\s*Howarth
		| Colin\s*Johnson
	)
	.*(All\s*R[io]ghts\s*Reserved)?
""", re.IGNORECASE | re.VERBOSE | re.DOTALL)

def classify(fn):
	info = { }
	with open(fn, "rb") as f: blob = f.read()
	soup = BeautifulSoup(blob, "html.parser")
	if soup.find(text=lambda text: isinstance(text, Comment) and "autogenerated" in text.lower()):
		info["generated"] = 1
	# Okay. So, what are we looking for, exactly?
	# 1) Easy mode: an anchor rel="license".
	for tag in soup.findAll(rel="license"):
		if tag["href"] == "https://creativecommons.org/licenses/by-sa/4.0/":
			return info | {"copyright": "CC-BY-SA 4.0"}
		if tag["href"] == "http://creativecommons.org/licenses/by-sa/4.0/":
			return info | {"copyright": "CC-BY-SA 4.0"} # Maybe fix protocol? Or not bother.
		if tag["href"] == "https://creativecommons.org/publicdomain/mark/1.0/":
			return info | {"copyright": "Public Domain"} # Fix to BY-SA 4.0 in case of future edits?
		info.setdefault("links", []).append(tag["href"])
	if "links" in info:
		return info | {"copyright": "Unknown"}
	text = []
	for cr in soup.findAll(text=True):
		if m := copyright.search(cr.text):
			return info | {"copyright": "All Rights Reserved"}
		# Look for any copyright marker, including a miswritten HTML entity
		if "copyright" in cr.text or "©" in cr.text or "&copy" in cr.text:
			# Maybe there's a full copyright notice in the parent's text,
			# but it's split up by HTML tags.
			if cr.parent("a", href="mailto:dstone4@cox.net"):
				# The WhoWasWho pages have a different copyright marker.
				# They probably should all get synchronized too, but maybe
				# not to the same value. Would be nice to use the same
				# format and CSS everywhere at least.
				return info | {"copyright": "David Stone"}
			if m := copyright.search(cr.parent.text):
				# Fixing this is going to be harder. But it's still an ARR
				# copyright notice.
				return info | {"copyright": "All Rights Reserved", "fix": "if possible"}
			text.append(cr.text)
	if text: return info | {"copyright": "Unknown", "text": text}
	return info | {"copyright": "None"}

for fn in sys.argv[1:]:
	if os.path.exists(fn):
		print(classify(fn))
		sys.exit(0)

stats = collections.Counter()
known_types = ["All Rights Reserved", "None", "CC-BY-SA 4.0", "Public Domain", "David Stone", "Unknown"]
with open("copywrong.log", "w") as log:
	for root, dirs, files in os.walk(root):
		for file in files:
			fn = os.path.join(root, file)
			info = classify(fn)
			stats[info["copyright"]] += 1
			if not stats.total() % 1000: print(stats.total(), stats)
			if info["copyright"] == "Unknown":
				print(fn, info, file=log)
			elif info["copyright"] not in known_types:
				print(fn, info)
				known_types.append(info["copyright"])
print(stats.total(), stats)

""" For manual testing:
def get(fn):
    global soup, cr
    soup = BeautifulSoup(open(fn).read(), "html.parser")
    cr = soup.find(text=lambda t: "Copyright" in t.text)
"""
<|endoftext|>


<commit_before># Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

# Note:
# You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
# The number of elements initialized in nums1 and nums2 are m and n respectively.

class Solution:
    # @param {integer[]} nums1
    # @param {integer} m
    # @param {integer[]} nums2
    # @param {integer} n
    # @return {void} Do not return anything, modify nums1 in-place instead.
    def merge(self, nums1, m, nums2, n):
        if n == 0:
            # return nums1 # for test
            return

        if m == 0:
            nums1[:] = nums2[:]
            # return nums1 # for test
            return

        i = j = k = 0
        while i < (m + k) and j < n:
            if nums1[i] > nums2[j]:
                nums1.insert(i, nums2[j])
                j += 1
                k += 1
            i += 1

        while j < n:
            nums1[i] = nums2[j]
            i, j = i+1, j+1

        nums1[:] = nums1[:(m+n)]
        # return nums1 # for test
<commit_msg>Add another 2 diff solutions of #088<commit_after># Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

# Note:
# You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
# The number of elements initialized in nums1 and nums2 are m and n respectively.

class Solution:
    # @param {integer[]} nums1
    # @param {integer} m
    # @param {integer[]} nums2
    # @param {integer} n
    # @return {void} Do not return anything, modify nums1 in-place instead.
    def merge(self, nums1, m, nums2, n):
        if n == 0:
            # return nums1 # for test
            return

        if m == 0:
            nums1[:] = nums2[:]
            # return nums1 # for test
            return

        i = j = k = 0
        while i < (m + k) and j < n:
            if nums1[i] > nums2[j]:
                nums1.insert(i, nums2[j])
                j += 1
                k += 1
            i += 1

        while j < n:
            nums1[i] = nums2[j]
            i, j = i+1, j+1

        nums1[:] = nums1[:(m+n)]
        # return nums1 # for test

        # O(nlogn) with built-in function extra space
        # x = A[0:m]
        # y = B[0:n]
        # x.extend(y)
        # x.sort()
        # A[0:m+n] = x

        # O(nlogn) with built-in function
        # A[m:] = B[:n]
        # A.sort()<|endoftext|>


<commit_before>
import sys

ADMINS = (
        ("Mike", "michael.bryant@kcl.ac.uk"),
)

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

if "test" in sys.argv:
    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.sqlite3",
            "NAME": "test.db",
        }
    }

<commit_msg>Use terminal for dev email output<commit_after>
import sys

ADMINS = (
        ("Mike", "michael.bryant@kcl.ac.uk"),
)

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

if "test" in sys.argv:
    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.sqlite3",
            "NAME": "test.db",
        }
    }

<|endoftext|>


<commit_before>from django.utils.translation import ugettext_lazy as _
from django.forms import models as modelforms
from django.forms.fields import DateTimeField, ChoiceField, IntegerField, HiddenInput
from django.core import signals as core_signals

from ella import newman
from ella.newman import options, fields, widgets, config
from ella.exports import models

class ExportPositionInlineAdmin(newman.NewmanTabularInline):
    model = models.ExportPosition
    extra = 1

class ExportAdmin(newman.NewmanModelAdmin):
    prepopulated_fields = {'slug': ('title',)}
    suggest_fields = {
        'category': ('__unicode__', 'title', 'slug',),
    }

class ExportMetaAdmin(newman.NewmanModelAdmin):
    inlines = (ExportPositionInlineAdmin,)
    #raw_id_fields = ('photo',)
    suggest_fields = {
        'publishable': ('__unicode__', 'title', 'slug',),
        'photo': ('__unicode__', 'title', 'slug',)
    }

class HiddenIntegerField(IntegerField):
    widget = HiddenInput

class MetaInlineForm(modelforms.ModelForm):
    position_id = HiddenIntegerField(required=False)
    position_from =  DateTimeField(label=_('Valid From'), widget=widgets.DateTimeWidget)
    position_to =  DateTimeField(label=_('Valid To'), widget=widgets.DateTimeWidget)
    export =  modelforms.ModelChoiceField(models.Export.objects.all(), label=_('Export'))
    _export_stack = dict()

    def __init__(self, *args, **kwargs):
        super(MetaInlineForm, self).__init__(*args, **kwargs)
        core_signals.request_finished.connect(receiver=MetaInlineForm.reset_export_enumerator)
        existing_object = False
        new_object = False
        id_initial = None
        from_initial = to_initial = ''
        export_initial = None
        export_qs = models.Export.objects.all()
        if 'instance' in kwargs and 'data' not in kwargs:
            existing_object = True
            instance = kwargs['instance']
            if instance:
                id_initial, from_initial, to_initial, export_initial = self.get_initial_data(instance)
        elif 'data' not in kwargs:
            new_object = True

        self.assign_init_field(
            'position_id',
            HiddenIntegerField(initial=id_initial, label=u'', required=False)
        )
        self.assign_init_field(
            'position_from',
            DateTimeField(initial=from_initial, label=_('Valid From'), widget=widgets.DateTimeWidget)
        )
        self.assign_init_field(
            'position_to',
            DateTimeField(initial=to_initial, label=_('Valid To'), widget=widgets.DateTimeWidget)
        )
        self.assign_init_field(
            'export',
            modelforms.ModelChoiceField(export_qs, initial=export_initial, label=_('Export'), show_hidden_initial=True)
        )

    def assign_init_field(self, field_name, value):
        self.fields[field_name] = self.base_fields[field_name] = value

    def get_initial_data(self, instance):
        " @return tuple (visible_from, visible_to, export_initial) "
        positions = models.ExportPosition.objects.filter(object=instance)
        if not positions:
            return ('', '', None)
        pcount = positions.count()
        if pcount > 1 and not MetaInlineForm._export_stack.get(instance, False):
            MetaInlineForm._export_stack[instance] = list()
            map( lambda p: MetaInlineForm._export_stack[instance].insert(0, p), positions )
        if pcount == 1:
            pos = positions[0]
        elif pcount > 1:
            pos = MetaInlineForm._export_stack[instance].pop()
        out = (pos.pk, pos.visible_from, pos.visible_to, pos.export.pk)
        return out

    @staticmethod
    def reset_export_enumerator(*args, **kwargs):
        " Clears _export_stack at the end of HTTP request. "
        core_signals.request_finished.disconnect(receiver=MetaInlineForm.reset_export_enumerator)
        MetaInlineForm._export_stack.clear()

    def get_date_field_key(self, field):
        return '%s-%s' % (self.prefix, field)

    def clean(self):
        """
        if not self.is_valid() or not self.cleaned_data or not self.instance:
            return self.cleaned_data
        """
        self.cleaned_data['position_id'] = self.data[self.get_date_field_key('position_id')]
        self.cleaned_data['position_from'] = self.data[self.get_date_field_key('position_from')]
        self.cleaned_data['position_to'] = self.data[self.get_date_field_key('position_to')]
        self.cleaned_data['export'] = self.data[self.get_date_field_key('export')]
        return self.cleaned_data

    def save(self, commit=True):
        out = super(MetaInlineForm, self).save(commit=commit)

        def save_them():
            export = models.Export.objects.get(pk=int(self.cleaned_data['export']))
            positions = models.ExportPosition.objects.filter(
                object=self.instance,
                export=export
            )
            if not self.cleaned_data['position_id']:
                position = models.ExportPosition(
                    object=self.instance,
                )
            else:
                pos_id = int(self.cleaned_data['position_id'])
                position = models.ExportPosition.objects.get(pk=pos_id)
            position.export = export
            position.visible_from = self.cleaned_data['position_from']
            position.visible_to = self.cleaned_data['position_to']
            position.save()

        if commit:
            save_them()
        else:
            save_m2m = getattr(self, 'save_m2m', None)
            def save_all():
                if save_m2m:
                    save_m2m()
                save_them()
            self.save_m2m = save_all
        return out

class ExportMetaInline(newman.NewmanTabularInline):
    form = MetaInlineForm
    model = models.ExportMeta
    suggest_fields = {'photo': ('__unicode__', 'title', 'slug',)}
    extra = 1

newman.site.register(models.Export, ExportAdmin)
newman.site.register(models.ExportPosition)
newman.site.register(models.ExportMeta, ExportMetaAdmin)

# Register ExportMetaInline in standard PublishableAdmin
newman.site.append_inline(config.TAGGED_MODELS, ExportMetaInline)
<commit_msg>Prepared timeline url for exports<commit_after>from django.conf.urls.defaults import patterns, url
from django.utils.translation import ugettext_lazy as _
from django.forms import models as modelforms
from django.forms.fields import DateTimeField, IntegerField, HiddenInput
from django.core import signals as core_signals

from ella import newman
from ella.newman import widgets, config
from ella.exports import models

class ExportPositionInlineAdmin(newman.NewmanTabularInline):
    model = models.ExportPosition
    extra = 1

class ExportAdmin(newman.NewmanModelAdmin):
    prepopulated_fields = {'slug': ('title',)}
    suggest_fields = {
        'category': ('__unicode__', 'title', 'slug',),
    }

    def get_urls(self):

        info = self.model._meta.app_label, self.model._meta.module_name

        urlpatterns = patterns('',
            url(r'^timeline/$',
                self.timeline_changelist_view,
                name='%s_%s_suggest' % info),
        )
        urlpatterns += super(ExportAdmin, self).get_urls()
        return urlpatterns

    def timeline_changelist_view(self, request, extra_context=None):
        # TODO: continue here
        pass

class ExportMetaAdmin(newman.NewmanModelAdmin):
    inlines = (ExportPositionInlineAdmin,)
    #raw_id_fields = ('photo',)
    suggest_fields = {
        'publishable': ('__unicode__', 'title', 'slug',),
        'photo': ('__unicode__', 'title', 'slug',)
    }

class HiddenIntegerField(IntegerField):
    widget = HiddenInput

class MetaInlineForm(modelforms.ModelForm):
    position_id = HiddenIntegerField(required=False)
    position_from =  DateTimeField(label=_('Valid From'), widget=widgets.DateTimeWidget)
    position_to =  DateTimeField(label=_('Valid To'), widget=widgets.DateTimeWidget)
    export =  modelforms.ModelChoiceField(models.Export.objects.all(), label=_('Export'))
    _export_stack = dict()

    def __init__(self, *args, **kwargs):
        super(MetaInlineForm, self).__init__(*args, **kwargs)
        core_signals.request_finished.connect(receiver=MetaInlineForm.reset_export_enumerator)
        existing_object = False
        new_object = False
        id_initial = None
        from_initial = to_initial = ''
        export_initial = None
        export_qs = models.Export.objects.all()
        if 'instance' in kwargs and 'data' not in kwargs:
            existing_object = True
            instance = kwargs['instance']
            if instance:
                id_initial, from_initial, to_initial, export_initial = self.get_initial_data(instance)
        elif 'data' not in kwargs:
            new_object = True

        self.assign_init_field(
            'position_id',
            HiddenIntegerField(initial=id_initial, label=u'', required=False)
        )
        self.assign_init_field(
            'position_from',
            DateTimeField(initial=from_initial, label=_('Valid From'), widget=widgets.DateTimeWidget)
        )
        self.assign_init_field(
            'position_to',
            DateTimeField(initial=to_initial, label=_('Valid To'), widget=widgets.DateTimeWidget)
        )
        self.assign_init_field(
            'export',
            modelforms.ModelChoiceField(export_qs, initial=export_initial, label=_('Export'), show_hidden_initial=True)
        )

    def assign_init_field(self, field_name, value):
        self.fields[field_name] = self.base_fields[field_name] = value

    def get_initial_data(self, instance):
        " @return tuple (visible_from, visible_to, export_initial) "
        positions = models.ExportPosition.objects.filter(object=instance)
        if not positions:
            return ('', '', None)
        pcount = positions.count()
        if pcount > 1 and not MetaInlineForm._export_stack.get(instance, False):
            MetaInlineForm._export_stack[instance] = list()
            map( lambda p: MetaInlineForm._export_stack[instance].insert(0, p), positions )
        if pcount == 1:
            pos = positions[0]
        elif pcount > 1:
            pos = MetaInlineForm._export_stack[instance].pop()
        out = (pos.pk, pos.visible_from, pos.visible_to, pos.export.pk)
        return out

    @staticmethod
    def reset_export_enumerator(*args, **kwargs):
        " Clears _export_stack at the end of HTTP request. "
        core_signals.request_finished.disconnect(receiver=MetaInlineForm.reset_export_enumerator)
        MetaInlineForm._export_stack.clear()

    def get_date_field_key(self, field):
        return '%s-%s' % (self.prefix, field)

    def clean(self):
        """
        if not self.is_valid() or not self.cleaned_data or not self.instance:
            return self.cleaned_data
        """
        self.cleaned_data['position_id'] = self.data[self.get_date_field_key('position_id')]
        self.cleaned_data['position_from'] = self.data[self.get_date_field_key('position_from')]
        self.cleaned_data['position_to'] = self.data[self.get_date_field_key('position_to')]
        self.cleaned_data['export'] = self.data[self.get_date_field_key('export')]
        return self.cleaned_data

    def save(self, commit=True):
        out = super(MetaInlineForm, self).save(commit=commit)

        def save_them():
            export = models.Export.objects.get(pk=int(self.cleaned_data['export']))
            positions = models.ExportPosition.objects.filter(
                object=self.instance,
                export=export
            )
            if not self.cleaned_data['position_id']:
                position = models.ExportPosition(
                    object=self.instance,
                )
            else:
                pos_id = int(self.cleaned_data['position_id'])
                position = models.ExportPosition.objects.get(pk=pos_id)
            position.export = export
            position.visible_from = self.cleaned_data['position_from']
            position.visible_to = self.cleaned_data['position_to']
            position.save()

        if commit:
            save_them()
        else:
            save_m2m = getattr(self, 'save_m2m', None)
            def save_all():
                if save_m2m:
                    save_m2m()
                save_them()
            self.save_m2m = save_all
        return out

class ExportMetaInline(newman.NewmanTabularInline):
    form = MetaInlineForm
    model = models.ExportMeta
    suggest_fields = {'photo': ('__unicode__', 'title', 'slug',)}
    extra = 1

newman.site.register(models.Export, ExportAdmin)
newman.site.register(models.ExportPosition)
newman.site.register(models.ExportMeta, ExportMetaAdmin)

# Register ExportMetaInline in standard PublishableAdmin
newman.site.append_inline(config.TAGGED_MODELS, ExportMetaInline)
<|endoftext|>


<commit_before>from events import *
from networkmessage import NetworkMessage

class NetworkController():
	def __init__(self, evManager):
		self.evManager = evManager
		self.evManager.register_listener(self)

	def on_recv_message(self, msg):
		msg.strip_message()
		self.prase_packet(msg)
		return 0

	def prase_packet(self, recv):
		#source, command, body
		self.evManager.post(ConsoleEvent(recv.buffer))
		#print "recv.buffer"
		parameters = recv.buffer.split(" ", 3)
		if parameters[0].find(":") != -1:
			parameters[0] = parameters[0].split(":")[1]  # Get rid of the : at the start of the message
		if parameters[0] == "PING":
			self.evManager.post(PingEvent())
		elif parameters[0] == "ERROR":
			return
		elif parameters[1] == "PRIVMSG":  # Source, channel, message
			self.evManager.post(PrivmsgEvent(parameters[0], parameters[2], parameters[3]))

	def disconnect(self):
		#print "ProtocolGame.disconnect"
		if self.connection is not False:
			self.connection.close_connection()
			self.connection = False

	#----------------------------------------------------------------------
	def notify(self, event):
		pass
		<commit_msg>Added error checking.<commit_after>from events import *
from networkmessage import NetworkMessage

class NetworkController():
	def __init__(self, evManager):
		self.evManager = evManager
		self.evManager.register_listener(self)

	def on_recv_message(self, msg):
		msg.strip_message()
		self.prase_packet(msg)

	def prase_packet(self, recv):
		#source, command, body
		self.evManager.post(ConsoleEvent("%s" % recv.buffer))
		#print "recv.buffer"
		parameters = recv.buffer.split(" ", 3)
		if parameters[0].find(":") != -1:
			parameters[0] = parameters[0].split(":")[1]  # Get rid of the : at the start of the message
		if parameters[0] == "PING":
			self.evManager.post(PingEvent())
			return
		elif parameters[0] == "ERROR":
			return
		if len(parameters) < 2:
			try:
				error = "Error: [NetworkController::prase_packet] parameters[0] = %s packet length = %d" % (parameters[0], recv.len)
			except TypeError:
				error = "Error: [NetworkController::prase_packet] TypeError when formating parameters string"
			self.evManager.post(ConsoleEvent(error))
		elif parameters[1] == "PRIVMSG":  # Source, channel, message
			self.evManager.post(PrivmsgEvent(parameters[0], parameters[2], parameters[3]))

	def disconnect(self):
		#print "ProtocolGame.disconnect"
		if self.connection is not False:
			self.connection.close_connection()
			self.connection = False

	#----------------------------------------------------------------------
	def notify(self, event):
		pass
		<|endoftext|>