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

"""
JSON 2 HTML Converter
=====================

(c) Varun Malhotra 2013
Source Code: https://github.com/softvar/json2html

Contributors:
-------------
1. Michel Müller (@muellermichel), https://github.com/softvar/json2html/pull/2
2. Daniel Lekic (@lekic), https://github.com/softvar/json2html/pull/17

LICENSE: MIT
--------
"""

import sys

if sys.version_info[:2] < (2, 7):
    from ordereddict import OrderedDict
    import simplejson as json_parser
else:
    from collections import OrderedDict
    import json as json_parser

if sys.version_info[:2] < (3, 0):
    text = unicode
    text_types = [unicode, str]
else:
    text = str
    text_types = [str]

class Json2Html:
    def convert(self, json="", table_attributes='border="1"', clubbing=True, encode=False):
        """
            Convert JSON to HTML Table format
        """
        # table attributes such as class, id, data-attr-*, etc.
        # eg: table_attributes = 'class = "table table-bordered sortable"'
        self.table_init_markup = "
" % table_attributes
        self.clubbing = clubbing
        json_input = None
        if not json:
            json_input = {}
        elif type(json) in text_types:
            json_input = json_parser.loads(json, object_pairs_hook=OrderedDict)
        else:
            json_input = json
        converted = self.convert_json_node(json_input)
        if encode:
            return converted.encode('ascii', 'xmlcharrefreplace')
        return converted

    def column_headers_from_list_of_dicts(self, json_input):
        """
            This method is required to implement clubbing.
            It tries to come up with column headers for your input
        """
        if not json_input \
        or not hasattr(json_input, '__getitem__') \
        or not hasattr(json_input[0], 'keys'):
            return None
        column_headers = json_input[0].keys()
        for entry in json_input:
            if not hasattr(entry, 'keys') \
            or not hasattr(entry, '__iter__') \
            or len(entry.keys()) != len(column_headers):
                return None
            for header in column_headers:
                if header not in entry:
                    return None
        return column_headers

    def convert_json_node(self, json_input):
        """
            Dispatch JSON input according to the outermost type and process it
            to generate the super awesome HTML format.
            We try to adhere to duck typing such that users can just pass all kinds
            of funky objects to json2html that *behave* like dicts and lists and other
            basic JSON types.
        """
        if type(json_input) in text_types:
            return text(json_input)
        if hasattr(json_input, 'items'):
            return self.convert_object(json_input)
        if hasattr(json_input, '__iter__') and hasattr(json_input, '__getitem__'):
            return self.convert_list(json_input)
        return text(json_input)

    def convert_list(self, list_input):
        """
            Iterate over the JSON list and process it
            to generate either an HTML table or a HTML list, depending on what's inside.
            If suppose some key has array of objects and all the keys are same,
            instead of creating a new row for each such entry,
            club such values, thus it makes more sense and more readable table.

            @example:
                jsonObject = {
                    "sampleData": [
                        {"a":1, "b":2, "c":3},
                        {"a":5, "b":6, "c":7}
                    ]
                }
                OUTPUT:
                _____________________________
                |               |   |   |   |
                |               | a | c | b |
                |   sampleData  |---|---|---|
                |               | 1 | 3 | 2 |
                |               | 5 | 7 | 6 |
                -----------------------------

            @contributed by: @muellermichel
        """
        if not list_input:
            return ""
        converted_output = ""
        column_headers = None
        if self.clubbing:
            column_headers = self.column_headers_from_list_of_dicts(list_input)
        if column_headers is not None:
            converted_output += self.table_init_markup
            converted_output += '
' + '	'.join(column_headers) + '
'
            for list_entry in list_input:
                converted_output += '
'
                converted_output += '	'.join([self.convert_json_node(list_entry[column_header]) for column_header in
                                                     column_headers])
                converted_output += '
'
            converted_output += '
'
            return converted_output

        #so you don't want or need clubbing eh? This makes @muellermichel very sad... ;(
        #alright, let's fall back to a basic list here...
        converted_output = '

'
        converted_output += '

'.join([self.convert_json_node(child) for child in list_input])
        converted_output += '

'
        return converted_output

    def convert_object(self, json_input):
        """
            Iterate over the JSON object and process it
            to generate the super awesome HTML Table format
        """
        if not json_input:
            return "" #avoid empty tables
        converted_output = self.table_init_markup + "
"
        converted_output += "

".join([
            "%s	%s	" %(
                self.convert_json_node(k),
                self.convert_json_node(v)
            )
            for k, v in json_input.items()
        ])
        converted_output += '

'
        return converted_output

json2html = Json2Html()
<commit_msg>escaping of text nodes to prevent XSS<commit_after># -*- coding: utf-8 -*-

"""
JSON 2 HTML Converter
=====================

(c) Varun Malhotra 2013
Source Code: https://github.com/softvar/json2html

Contributors:
-------------
1. Michel Müller (@muellermichel), https://github.com/softvar/json2html/pull/2
2. Daniel Lekic (@lekic), https://github.com/softvar/json2html/pull/17

LICENSE: MIT
--------
"""

import sys, cgi

if sys.version_info[:2] < (2, 7):
    from ordereddict import OrderedDict
    import simplejson as json_parser
else:
    from collections import OrderedDict
    import json as json_parser

if sys.version_info[:2] < (3, 0):
    text = unicode
    text_types = [unicode, str]
else:
    text = str
    text_types = [str]

class Json2Html:
    def convert(self, json="", table_attributes='border="1"', clubbing=True, encode=False, escape=True):
        """
            Convert JSON to HTML Table format
        """
        # table attributes such as class, id, data-attr-*, etc.
        # eg: table_attributes = 'class = "table table-bordered sortable"'
        self.table_init_markup = "
" % table_attributes
        self.clubbing = clubbing
        self.escape = escape
        json_input = None
        if not json:
            json_input = {}
        elif type(json) in text_types:
            json_input = json_parser.loads(json, object_pairs_hook=OrderedDict)
        else:
            json_input = json
        converted = self.convert_json_node(json_input)
        if encode:
            return converted.encode('ascii', 'xmlcharrefreplace')
        return converted

    def column_headers_from_list_of_dicts(self, json_input):
        """
            This method is required to implement clubbing.
            It tries to come up with column headers for your input
        """
        if not json_input \
        or not hasattr(json_input, '__getitem__') \
        or not hasattr(json_input[0], 'keys'):
            return None
        column_headers = json_input[0].keys()
        for entry in json_input:
            if not hasattr(entry, 'keys') \
            or not hasattr(entry, '__iter__') \
            or len(entry.keys()) != len(column_headers):
                return None
            for header in column_headers:
                if header not in entry:
                    return None
        return column_headers

    def convert_json_node(self, json_input):
        """
            Dispatch JSON input according to the outermost type and process it
            to generate the super awesome HTML format.
            We try to adhere to duck typing such that users can just pass all kinds
            of funky objects to json2html that *behave* like dicts and lists and other
            basic JSON types.
        """
        if type(json_input) in text_types:
            if self.escape:
                return cgi.escape(text(json_input))
            else:
                return text(json_input)
        if hasattr(json_input, 'items'):
            return self.convert_object(json_input)
        if hasattr(json_input, '__iter__') and hasattr(json_input, '__getitem__'):
            return self.convert_list(json_input)
        return text(json_input)

    def convert_list(self, list_input):
        """
            Iterate over the JSON list and process it
            to generate either an HTML table or a HTML list, depending on what's inside.
            If suppose some key has array of objects and all the keys are same,
            instead of creating a new row for each such entry,
            club such values, thus it makes more sense and more readable table.

            @example:
                jsonObject = {
                    "sampleData": [
                        {"a":1, "b":2, "c":3},
                        {"a":5, "b":6, "c":7}
                    ]
                }
                OUTPUT:
                _____________________________
                |               |   |   |   |
                |               | a | c | b |
                |   sampleData  |---|---|---|
                |               | 1 | 3 | 2 |
                |               | 5 | 7 | 6 |
                -----------------------------

            @contributed by: @muellermichel
        """
        if not list_input:
            return ""
        converted_output = ""
        column_headers = None
        if self.clubbing:
            column_headers = self.column_headers_from_list_of_dicts(list_input)
        if column_headers is not None:
            converted_output += self.table_init_markup
            converted_output += '
' + '	'.join(column_headers) + '
'
            for list_entry in list_input:
                converted_output += '
'
                converted_output += '	'.join([self.convert_json_node(list_entry[column_header]) for column_header in
                                                     column_headers])
                converted_output += '
'
            converted_output += '
'
            return converted_output

        #so you don't want or need clubbing eh? This makes @muellermichel very sad... ;(
        #alright, let's fall back to a basic list here...
        converted_output = '

'
        converted_output += '

'.join([self.convert_json_node(child) for child in list_input])
        converted_output += '

'
        return converted_output

    def convert_object(self, json_input):
        """
            Iterate over the JSON object and process it
            to generate the super awesome HTML Table format
        """
        if not json_input:
            return "" #avoid empty tables
        converted_output = self.table_init_markup + "
"
        converted_output += "

".join([
            "%s	%s	" %(
                self.convert_json_node(k),
                self.convert_json_node(v)
            )
            for k, v in json_input.items()
        ])
        converted_output += '

'
        return converted_output

json2html = Json2Html()
<|endoftext|>


<commit_before>#!/usr/bin/env python
"""
Run multiple simulations varying a single parameter.
"""

import foampy
from foampy.dictionaries import replace_single_line_value
import numpy as np
from subprocess import call
import os
import pandas as pd
from modules import processing as pr

def zero_tsr_fluc():
    """Set TSR fluctuation amplitude to zero."""
    replace_single_line_value("fvOptions", "tsrAmplitude", 0.0)

def set_tsr(val):
    """Set mean tip speed ratio."""
    print("Setting TSR to", tsr)
    replace_single_line_value("fvOptions", "tipSpeedRatio", val)

def log_perf(param="tsr", append=True):
    """Log performance to file."""
    if not os.path.isdir("processed"):
        os.mkdir("processed")
    fpath = "processed/{}_sweep.csv".format(param)
    if append and os.path.isfile(fpath)
        df = pd.read_csv(fpath)
    else:
        df = pd.DataFrame(columns=["tsr", "cp", "cd"])
    df = df.append(pr.calc_perf(t1=3.0), ignore_index=True)
    df.to_csv("fpath", index=False)

def tsr_sweep(start=0.4, stop=3.4, step=0.5, append=False):
    """Run over multiple TSRs. `stop` will be included."""
    if not append and os.path.isfile("processed/tsr_sweep.csv"):
        os.remove("processed/tsr_sweep.csv")
    tsrs = np.arange(start, stop + 0.5*step, step)
    zero_tsr_fluc()
    cp = []
    cd = []
    for tsr in tsrs:
        set_tsr(tsr)
        if tsr == tsrs[0]:
            call("./Allclean")
            call("./Allrun")
        else:
            call("pimpleFoam | tee log.pimpleFoam", shell=True)
        os.rename("log.pimpleFoam", "log.pimpleFoam." + str(tsr))
        log_perf(append=True)
    # Checkout original fvOptions
    call["git", "checkout", "system/fvOptions"]

if __name__ == "__main__":
    tsr_sweep(0.4, 3.4, 0.5, append=False)
<commit_msg>Make paramsweep.py executable<commit_after>#!/usr/bin/env python
"""
Run multiple simulations varying a single parameter.
"""

import foampy
from foampy.dictionaries import replace_single_line_value
import numpy as np
from subprocess import call
import os
import pandas as pd
from modules import processing as pr

def zero_tsr_fluc():
    """Set TSR fluctuation amplitude to zero."""
    replace_single_line_value("fvOptions", "tsrAmplitude", 0.0)

def set_tsr(val):
    """Set mean tip speed ratio."""
    print("Setting TSR to", tsr)
    replace_single_line_value("fvOptions", "tipSpeedRatio", val)

def log_perf(param="tsr", append=True):
    """Log performance to file."""
    if not os.path.isdir("processed"):
        os.mkdir("processed")
    fpath = "processed/{}_sweep.csv".format(param)
    if append and os.path.isfile(fpath)
        df = pd.read_csv(fpath)
    else:
        df = pd.DataFrame(columns=["tsr", "cp", "cd"])
    df = df.append(pr.calc_perf(t1=3.0), ignore_index=True)
    df.to_csv("fpath", index=False)

def tsr_sweep(start=0.4, stop=3.4, step=0.5, append=False):
    """Run over multiple TSRs. `stop` will be included."""
    if not append and os.path.isfile("processed/tsr_sweep.csv"):
        os.remove("processed/tsr_sweep.csv")
    tsrs = np.arange(start, stop + 0.5*step, step)
    zero_tsr_fluc()
    cp = []
    cd = []
    for tsr in tsrs:
        set_tsr(tsr)
        if tsr == tsrs[0]:
            call("./Allclean")
            call("./Allrun")
        else:
            call("pimpleFoam | tee log.pimpleFoam", shell=True)
        os.rename("log.pimpleFoam", "log.pimpleFoam." + str(tsr))
        log_perf(append=True)
    # Checkout original fvOptions
    call["git", "checkout", "system/fvOptions"]

if __name__ == "__main__":
    tsr_sweep(0.4, 3.4, 0.5, append=False)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2015, imageio contributors
# imageio is distributed under the terms of the (new) BSD License.

""" Storage of image data in tiff format.
"""

from __future__ import absolute_import, print_function, division

from .. import formats
from ..core import Format

import numpy as np

tifffile = None

def load_tifffile():
    """ Defer loading of the tifffile module.
    """
    global tifffile
    try:
        import tifffile as _tifffile
    except ImportError:
        from . import _tifffile
    tifffile = _tifffile

TIFF_FORMATS = ('.tif', '.tiff', '.stk', '.lsm')

class TiffFormat(Format):

    """ Provides support for a wide range of Tiff images.

    Parameters for reading
    ----------------------
    None

    Parameters for saving
    ---------------------
    bigtiff : bool
        If True, the BigTIFF format is used.
    byteorder : {'<', '>'}
        The endianness of the data in the file.
        By default this is the system's native byte order.
    software : str
        Name of the software used to create the image.
        Saved with the first page only.
    photometric : {'minisblack', 'miniswhite', 'rgb'}
            The color space of the image data.
            By default this setting is inferred from the data shape.
    planarconfig : {'contig', 'planar'}
        Specifies if samples are stored contiguous or in separate planes.
        By default this setting is inferred from the data shape.
        'contig': last dimension contains samples.
        'planar': third last dimension contains samples.
    resolution : (float, float) or ((int, int), (int, int))
        X and Y resolution in dots per inch as float or rational numbers.
    description : str
        The subject of the image. Saved with the first page only.
    compress : int
        Values from 0 to 9 controlling the level of zlib compression.
        If 0, data are written uncompressed (default).
    volume : bool
        If True, volume data are stored in one tile (if applicable) using
        the SGI image_depth and tile_depth tags.
        Image width and depth must be multiple of 16.
        Few software can read this format, e.g. MeVisLab.
    writeshape : bool
        If True, write the data shape to the image_description tag
        if necessary and no other description is given.
    extratags: sequence of tuples
        Additional tags as [(code, dtype, count, value, writeonce)].

        code : int
            The TIFF tag Id.
        dtype : str
            Data type of items in 'value' in Python struct format.
            One of B, s, H, I, 2I, b, h, i, f, d, Q, or q.
        count : int
            Number of data values. Not used for string values.
        value : sequence
            'Count' values compatible with 'dtype'.
        writeonce : bool
            If True, the tag is written to the first page only.
    """

    def _can_read(self, request):
        if request.filename.lower().endswith(TIFF_FORMATS):
            return True  # We support any kind of image data
        else:
            return False

    def _can_write(self, request):
        if request.filename.lower().endswith(TIFF_FORMATS):
            return True  # We support any kind of image data
        else:
            return False

    # -- reader

    class Reader(Format.Reader):

        def _open(self):
            if not tifffile:
                load_tifffile()
            self._tf = tifffile.TiffFile(self.request.filename)

        def _close(self):
            self._tf.close()

        def _get_length(self):
            print(len(self._tf.series), len(self._tf.pages))
            return len(self._tf)

        def _get_data(self, index):
            # Get data
            if index < 0 or index >= len(self._tf):
                raise IndexError(
                    'Index out of range while reading from tiff file')
            im = self._tf[index].asarray()
            # Return array and empty meta data
            return im, {}

        def _get_meta_data(self, index):
            raise RuntimeError('The tiff format does not support meta data.')

    # -- writer

    class Writer(Format.Writer):

        def _open(self, bigtiff=None, byteorder=None, software=None,
                  **kwargs):
            self._parameters = kwargs
            if not tifffile:
                load_tifffile()
            self._tf = tifffile.TiffWriter(self.request.filename,
                                           bigtiff, byteorder, software)

        def _close(self):
            self._tf.close()

        def _append_data(self, im, meta):
            # ignore metadata
            self._tf.save(np.asanyarray(im), **self._parameters)

        def set_meta_data(self, meta):
            raise RuntimeError('The tiff format does not support meta data.')

# Register
format = TiffFormat('tiff', "TIFF format", 'tif tiff stk lsm', 'iIvV')
formats.add_format(format)
<commit_msg>Remove debug print<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2015, imageio contributors
# imageio is distributed under the terms of the (new) BSD License.

""" Storage of image data in tiff format.
"""

from __future__ import absolute_import, print_function, division

from .. import formats
from ..core import Format

import numpy as np

tifffile = None

def load_tifffile():
    """ Defer loading of the tifffile module.
    """
    global tifffile
    try:
        import tifffile as _tifffile
    except ImportError:
        from . import _tifffile
    tifffile = _tifffile

TIFF_FORMATS = ('.tif', '.tiff', '.stk', '.lsm')

class TiffFormat(Format):

    """ Provides support for a wide range of Tiff images.

    Parameters for reading
    ----------------------
    None

    Parameters for saving
    ---------------------
    bigtiff : bool
        If True, the BigTIFF format is used.
    byteorder : {'<', '>'}
        The endianness of the data in the file.
        By default this is the system's native byte order.
    software : str
        Name of the software used to create the image.
        Saved with the first page only.
    photometric : {'minisblack', 'miniswhite', 'rgb'}
            The color space of the image data.
            By default this setting is inferred from the data shape.
    planarconfig : {'contig', 'planar'}
        Specifies if samples are stored contiguous or in separate planes.
        By default this setting is inferred from the data shape.
        'contig': last dimension contains samples.
        'planar': third last dimension contains samples.
    resolution : (float, float) or ((int, int), (int, int))
        X and Y resolution in dots per inch as float or rational numbers.
    description : str
        The subject of the image. Saved with the first page only.
    compress : int
        Values from 0 to 9 controlling the level of zlib compression.
        If 0, data are written uncompressed (default).
    volume : bool
        If True, volume data are stored in one tile (if applicable) using
        the SGI image_depth and tile_depth tags.
        Image width and depth must be multiple of 16.
        Few software can read this format, e.g. MeVisLab.
    writeshape : bool
        If True, write the data shape to the image_description tag
        if necessary and no other description is given.
    extratags: sequence of tuples
        Additional tags as [(code, dtype, count, value, writeonce)].

        code : int
            The TIFF tag Id.
        dtype : str
            Data type of items in 'value' in Python struct format.
            One of B, s, H, I, 2I, b, h, i, f, d, Q, or q.
        count : int
            Number of data values. Not used for string values.
        value : sequence
            'Count' values compatible with 'dtype'.
        writeonce : bool
            If True, the tag is written to the first page only.
    """

    def _can_read(self, request):
        if request.filename.lower().endswith(TIFF_FORMATS):
            return True  # We support any kind of image data
        else:
            return False

    def _can_write(self, request):
        if request.filename.lower().endswith(TIFF_FORMATS):
            return True  # We support any kind of image data
        else:
            return False

    # -- reader

    class Reader(Format.Reader):

        def _open(self):
            if not tifffile:
                load_tifffile()
            self._tf = tifffile.TiffFile(self.request.filename)

        def _close(self):
            self._tf.close()

        def _get_length(self):
            return len(self._tf)

        def _get_data(self, index):
            # Get data
            if index < 0 or index >= len(self._tf):
                raise IndexError(
                    'Index out of range while reading from tiff file')
            im = self._tf[index].asarray()
            # Return array and empty meta data
            return im, {}

        def _get_meta_data(self, index):
            raise RuntimeError('The tiff format does not support meta data.')

    # -- writer

    class Writer(Format.Writer):

        def _open(self, bigtiff=None, byteorder=None, software=None,
                  **kwargs):
            self._parameters = kwargs
            if not tifffile:
                load_tifffile()
            self._tf = tifffile.TiffWriter(self.request.filename,
                                           bigtiff, byteorder, software)

        def _close(self):
            self._tf.close()

        def _append_data(self, im, meta):
            # ignore metadata
            self._tf.save(np.asanyarray(im), **self._parameters)

        def set_meta_data(self, meta):
            raise RuntimeError('The tiff format does not support meta data.')

# Register
format = TiffFormat('tiff', "TIFF format", 'tif tiff stk lsm', 'iIvV')
formats.add_format(format)
<|endoftext|>


<commit_before>import bson

import urlparse

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from pymongo.connection import Connection

from tastypie.bundle import Bundle
from tastypie.exceptions import NotFound
from tastypie.resources import Resource

class MongoDB(object):

    def __init__(self, db_uri):

        uri_parts = urlparse.urlparse(db_uri)

        if uri_parts.path:
            database_name = uri_parts.path[1:]
        else:
            try:
                database_name = settings.MONGODB_NAME
            except AttributeError:
                raise ImproperlyConfigured('You did not supply the database name in MONGODB_URI neither in MONGODB_NAME')

        self.connection = Connection(db_uri)
        self.database = self.connection[database_name]

    def get_collection(self, collection):
        return self.database[collection]

def get_db():
    try:
        db_uri = settings.MONGODB_URI
    except AttributeError:
        raise ImproperlyConfigured('Missing required MONGODB_URI setting')

    return MongoDB(db_uri)

def get_user(request, collection):
    return collection.find_one({'user': request.user.id}, safe=True)

def get_or_create_user(request, collection, key, initial):
    user_id = request.user.id

    user = collection.find_one({'user': user_id}, safe=True)
    if user is None:
        user = {'user': user_id, key: initial}
        user['_id'] = collection.insert(user)

    return user

class MongoObj(object):
    """This class is required for Tastypie"""

    def __init__(self, initial=None):
        self.__dict__['_data'] = {}

        if hasattr(initial, 'items'):
            self.__dict__['_data'] = initial

    def __getattr__(self, name):
        return self._data.get(name, None)

    def __setattr__(self, name, value):
        self.__dict__['_data'][name] = value

    def to_dict(self):
        return self._data

class MongoResource(Resource):

    collection = None  # subclasses should implement this

    def __init__(self, *args, **kwargs):
        super(MongoResource, self).__init__(*args, **kwargs)

        self._collection = get_db().get_collection(self._meta.collection)

    def get_resource_uri(self, bundle_or_obj):
        kwargs = {'resource_name': self._meta.resource_name}
        if isinstance(bundle_or_obj, Bundle):
            kwargs['pk'] = str(bundle_or_obj.obj.uuid)
        else:
            kwargs['pk'] = str(bundle_or_obj.uuid)

        if self._meta.api_name is not None:
            kwargs['api_name'] = self._meta.api_name

        return self._build_reverse_url('api_dispatch_detail', kwargs=kwargs)

    def get_object_list(self, request):
        results = []
        for result in self._collection.find():
            obj = MongoObj(initial=result)
            obj.uuid = str(result['_id'])
            results.append(obj)

        return results

    def obj_get_list(self, request=None, **kwargs):
        # no filtering by now
        return self.get_object_list(request)

    def obj_get(self, request=None, **kwargs):
        user = self._get_or_create_user(request, **kwargs)
        oid = kwargs['pk']

        result = user[self._meta.datakey].get(oid, None)
        if result is None:
            raise NotFound('Invalid resource lookup data provided')

        obj = MongoObj(initial=result)
        obj.uuid = oid
        return obj

    def obj_create(self, bundle, request=None, **kwargs):
        bundle = self.full_hydrate(bundle)
        bundle.obj = MongoObj(bundle.data)
        _id = self._collection.insert(bundle.data, safe=True)
        bundle.obj.uuid = str(_id)
        return bundle

    def _get_or_create_user(self, request, **kwargs):
        return get_or_create_user(request, self._collection,
                                  self._meta.datakey,
                                  self._initial(request, **kwargs))

    def _initial(self, request, **kwargs):
        return {}
<commit_msg>Use a more robust database wrapper<commit_after>import urlparse

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from pymongo.connection import Connection

from tastypie.bundle import Bundle
from tastypie.exceptions import NotFound
from tastypie.resources import Resource

DEFAULT_MONGODB_HOST = 'localhost'
DEFAULT_MONGODB_PORT = 27017
DEFAULT_MONGODB_NAME = 'moocng'
DEFAULT_MONGODB_URI = 'mongodb://%s:%d/%s' % (DEFAULT_MONGODB_HOST,
                                              DEFAULT_MONGODB_PORT,
                                              DEFAULT_MONGODB_NAME)

class MongoDB(object):

    def __init__(self, db_uri=DEFAULT_MONGODB_URI,
                 connection_factory=Connection):
        self.db_uri = urlparse.urlparse(db_uri)
        self.connection = connection_factory(
            host=self.db_uri.hostname or DEFAULT_MONGODB_HOST,
            port=self.db_uri.port or DEFAULT_MONGODB_PORT)

        if self.db_uri.path:
            self.database_name = self.db_uri.path[1:]
        else:
            self.database_name = DEFAULT_MONGODB_NAME

        self.database = self.get_database()

    def get_connection(self):
        return self.connection

    def get_database(self):
        database = self.connection[self.database_name]
        if self.db_uri.username and self.db_uri.password:
            database.authenticate(self.db_uri.username, self.db_uri.password)

        return database

    def get_collection(self, collection):
        return self.database[collection]

def get_db():
    try:
        db_uri = settings.MONGODB_URI
    except AttributeError:
        raise ImproperlyConfigured('Missing required MONGODB_URI setting')

    return MongoDB(db_uri)

def get_user(request, collection):
    return collection.find_one({'user': request.user.id}, safe=True)

def get_or_create_user(request, collection, key, initial):
    user_id = request.user.id

    user = collection.find_one({'user': user_id}, safe=True)
    if user is None:
        user = {'user': user_id, key: initial}
        user['_id'] = collection.insert(user)

    return user

class MongoObj(object):
    """This class is required for Tastypie"""

    def __init__(self, initial=None):
        self.__dict__['_data'] = {}

        if hasattr(initial, 'items'):
            self.__dict__['_data'] = initial

    def __getattr__(self, name):
        return self._data.get(name, None)

    def __setattr__(self, name, value):
        self.__dict__['_data'][name] = value

    def to_dict(self):
        return self._data

class MongoResource(Resource):

    collection = None  # subclasses should implement this

    def __init__(self, *args, **kwargs):
        super(MongoResource, self).__init__(*args, **kwargs)

        self._collection = get_db().get_collection(self._meta.collection)

    def get_resource_uri(self, bundle_or_obj):
        kwargs = {'resource_name': self._meta.resource_name}
        if isinstance(bundle_or_obj, Bundle):
            kwargs['pk'] = str(bundle_or_obj.obj.uuid)
        else:
            kwargs['pk'] = str(bundle_or_obj.uuid)

        if self._meta.api_name is not None:
            kwargs['api_name'] = self._meta.api_name

        return self._build_reverse_url('api_dispatch_detail', kwargs=kwargs)

    def get_object_list(self, request):
        results = []
        for result in self._collection.find():
            obj = MongoObj(initial=result)
            obj.uuid = str(result['_id'])
            results.append(obj)

        return results

    def obj_get_list(self, request=None, **kwargs):
        # no filtering by now
        return self.get_object_list(request)

    def obj_get(self, request=None, **kwargs):
        user = self._get_or_create_user(request, **kwargs)
        oid = kwargs['pk']

        result = user[self._meta.datakey].get(oid, None)
        if result is None:
            raise NotFound('Invalid resource lookup data provided')

        obj = MongoObj(initial=result)
        obj.uuid = oid
        return obj

    def obj_create(self, bundle, request=None, **kwargs):
        bundle = self.full_hydrate(bundle)
        bundle.obj = MongoObj(bundle.data)
        _id = self._collection.insert(bundle.data, safe=True)
        bundle.obj.uuid = str(_id)
        return bundle

    def _get_or_create_user(self, request, **kwargs):
        return get_or_create_user(request, self._collection,
                                  self._meta.datakey,
                                  self._initial(request, **kwargs))

    def _initial(self, request, **kwargs):
        return {}
<|endoftext|>


<commit_before>#
# Author    : Manuel Bernal Llinares
# Project   : trackhub-creator
# Timestamp : 02-08-2017 16:40
# ---
# © 2017 Manuel Bernal Llinares <mbdebian@gmail.com>
# All rights reserved.
#

"""
This module contains models for dealing with PoGo stuff
"""

import os
import abc
import threading
# Application imports
import config_manager as main_app_config_manager
from parallel.models import ParallelRunner, CommandLineRunnerFactory
from parallel.exceptions import CommandLineRunnerException
from . import config_manager as module_config_manager
from .exceptions import PogoRunnerException

class PogoRunResult:
    def __init__(self,
                 ncbi_taxonomy_id=None,
                 pogo_source_file_path=None,
                 protein_sequence_file_path=None,
                 gtf_file_path=None):
        """
        Just the constructor, I had this implemented as syntactic sugar, but I was wrong
        :param ncbi_taxonomy_id: ncbi taxonomy id for this PoGo run results
        :param pogo_source_file_path: path of the source file used to run PoGo
        :param protein_sequence_file_path: FASTA file
        :param gtf_file_path: GTF file
        """
        # Logging
        self.__logger = main_app_config_manager.get_app_config_manager().get_logger_for(
            "{}.{}".format(__name__, type(self).__name__))
        # Map<pogo_result_file_extension, pogo_result_file_path>
        self.__pogo_result_file_paths = {}
        self.__pogo_source_file_path = None
        # I knew this was going to give me trouble
        self.set_ncbi_taxonomy_id(ncbi_taxonomy_id)
        self.set_protein_sequence_file_path(protein_sequence_file_path)
        self.set_gtf_file_path(gtf_file_path)
        self.set_pogo_source_file_path(pogo_source_file_path)

    def __generate_pogo_result_file_paths(self, pogo_source_file_path):
        self.__pogo_result_file_paths = {}
        pogo_results_folder_path = os.path.dirname(pogo_source_file_path)
        pogo_source_file_name = os.path.basename(pogo_source_file_path)
        for root, dirs, files in os.walk(pogo_results_folder_path):
            for pogo_result_file_path in files:
                pogo_result_file_name = os.path.basename(pogo_result_file_path)
                if pogo_result_file_name.startswith(pogo_source_file_name) and (
                            pogo_result_file_name != pogo_source_file_name):
                    # It is file generated by PoGo
                    pogo_result_file_extension = pogo_result_file_name.split(pogo_source_file_name)[1]
                    # self.__pogo_result_file_paths[pogo_result_file_extension] = pogo_result_file_path
                    # I need the full path for the pogo result file path
                    self.__pogo_result_file_paths[pogo_result_file_extension] = os.path.join(pogo_results_folder_path,
                                                                                pogo_result_file_path)
                    self._get_logger().info(
                        "PoGo run result '{}' ---> '{}', at full path '{}'".format(pogo_result_file_extension,
                                                                                pogo_result_file_path,
                                                                                self.__pogo_result_file_paths[
                                                                                pogo_result_file_extension]))

    def __get_pogo_result_file_path(self, file_extension):
        if file_extension in self.__pogo_result_file_paths:
            return self.__pogo_result_file_paths[file_extension]
        return None

    def _get_logger(self):
        return self.__logger

    def set_ncbi_taxonomy_id(self, taxonomy_id):
        self.__ncbi_taxonomy_id = taxonomy_id

    def set_pogo_source_file_path(self, pogo_source_file_path):
        if not self.__pogo_source_file_path or (self.__pogo_source_file_path != pogo_source_file_path):
            self.__pogo_source_file_path = pogo_source_file_path
            # Regenerate pogo result file paths
            self.__generate_pogo_result_file_paths(pogo_source_file_path)

    def set_protein_sequence_file_path(self, protein_sequence_file_path):
        self.__protein_sequence_file_path = protein_sequence_file_path

    def set_gtf_file_path(self, gtf_file_path):
        self.__gtf_file_path = gtf_file_path

    def get_ncbi_taxonomy_id(self):
        return self.__ncbi_taxonomy_id

    def get_pogo_result_main_bed_file_path(self):
        return self.__get_pogo_result_file_path(
            module_config_manager.get_configuration_service().get_pogo_result_file_extension_for_main_bed_file())

    def get_pogo_result_main_ptm_bed_file_path(self):
        return self.__get_pogo_result_file_path(
            module_config_manager.get_configuration_service().get_pogo_result_file_extension_for_main_ptm_bed_file()
        )

# PoGo Runners
class PogoRunnerFactory:
    @staticmethod
    def get_pogo_runner(ncbi_taxonomy_id, pogo_input_file, protein_sequence_file_path, gtf_file_path):
        # TODO - In the future, more PoGo runners will be implemented
        return PogoRunnerLocalThread(ncbi_taxonomy_id, pogo_input_file, protein_sequence_file_path, gtf_file_path)

class PogoRunner(ParallelRunner):
    def __init__(self,
                 ncbi_taxonomy_id=None,
                 pogo_input_file=None,
                 protein_sequence_file_path=None,
                 gtf_file_path=None):
        super().__init__()
        self.ncbi_taxonomy_id = ncbi_taxonomy_id
        self.pogo_input_file = pogo_input_file
        self.protein_sequence_file_path = protein_sequence_file_path
        self.gtf_file_path = gtf_file_path
        self._stdout = b' '
        self._stderr = b' '
        self.__success = True
        self.__pogo_run_result = None

    def _set_failed_to_run_pogo(self):
        self.__success = False

    def _set_success_to_run_pogo(self):
        self.__success = self.__success and True

    def _validate_environment_for_running_pogo(self):
        validation_ok = True
        if not self.ncbi_taxonomy_id:
            self._logger.warning("NO NCBI TAXONOMY ID has been specified for running PoGo with input file '{}'"
                                 .format(self.pogo_input_file))
        if not os.path.isfile(self.pogo_input_file):
            self._logger.error("PoGo input file '{}' IS NOT VALID".format(self.pogo_input_file))
            validation_ok = False
        if not os.path.isfile(self.protein_sequence_file_path):
            self._logger.error("Protein sequence file '{}' IS NOT VALID".format(self.protein_sequence_file_path))
            validation_ok = False
        if not os.path.isfile(self.gtf_file_path):
            self._logger.error("GTF file '{}' IS NOT VALID".format(self.gtf_file_path))
            validation_ok = False
        if not os.path.isfile(module_config_manager.get_configuration_service().get_pogo_binary_file_path()):
            self._logger.error("PoGo binary file '{}' IS NOT VALID"
                               .format(module_config_manager.get_configuration_service().get_pogo_binary_file_path()))
            validation_ok = False
        return validation_ok

    def get_stdout(self):
        # Never give it back until the runner is done with whatever it is doing
        if not self._done:
            raise PogoRunnerException("PoGo Runner is NOT DONE doing its job, thus 'stdout' is NOT AVAILABLE")
        return self._stdout

    def get_stderr(self):
        # Never give it back until the runner is done with whatever it is doing
        if not self._done:
            raise PogoRunnerException("PoGo Runner is NOT DONE doing its job, thus 'stderr' is NOT AVAILABLE")
        return self._stderr

    def get_pogo_run_command(self):
        pogo_parameter_species = ''
        if self.ncbi_taxonomy_id:
            pogo_parameter_species = " -species {} ".format(self.ncbi_taxonomy_id)
        pogo_command = "time {}{} -fasta {} -gtf {} -in {}" \
            .format(module_config_manager.get_configuration_service().get_pogo_binary_file_path(),
                    pogo_parameter_species,
                    self.protein_sequence_file_path,
                    self.gtf_file_path,
                    self.pogo_input_file)
        self._logger.debug("Command for running PoGo - '{}'".format(pogo_command))
        return pogo_command

    @abc.abstractmethod
    def _get_command_line_runner(self):
        """
        Although a factory is used to get a command line runner, its initialization differs depending on the final
        PogoRunner, i.e. we delegate to subclasses the provisioning of an initialized CommandLineRunner, how we use
        it, is the same for all subclasses.
        It will prepare the command line runner completely, including the command it has to run.
        :return: a CommandLineRunner for running PoGo
        """
        ...

    def _run(self):
        if not self._validate_environment_for_running_pogo():
            self._logger.error("Can't run PoGo for file '{}'".format(self.pogo_input_file))
            self._set_failed_to_run_pogo()
        else:
            command_line_runner = self._get_command_line_runner()
            try:
                command_line_runner.start()
                self._logger.debug("PoGo --- STARTED --- command '{}'".format(command_line_runner.command))
                command_line_runner.wait()
            except CommandLineRunnerException as e:
                self._logger.error("ERROR RUNNING PoGo - {}, STDOUT ---> '{}', STDERR ---> '{}'"
                                   .format(e.value,
                                           command_line_runner.get_stdout().decode('utf8'),
                                           command_line_runner.get_stderr().decode('utf8')))
                self._set_failed_to_run_pogo()
            finally:
                self._stdout = command_line_runner.get_stdout()
                self._stderr = command_line_runner.get_stderr()
            if not command_line_runner.command_success:
                self._set_failed_to_run_pogo()
                self._logger.error("PoGo FAILED on input file '{}', command '{}'"
                                   .format(self.pogo_input_file, command_line_runner.command))
            else:
                # Successful run of PoGo
                self._set_success_to_run_pogo()
                self._logger.info("PoGo SUCCESS on input file '{}', command '{}'"
                                  .format(self.pogo_input_file, command_line_runner.command))

    def get_pogo_run_result(self):
        if not self.is_done():
            raise PogoRunnerException("PoGo runner IS NOT FINISHED YET, thus, results can't be retrieved")
        if not self.__pogo_run_result:
            self.__pogo_run_result = PogoRunResult(self.ncbi_taxonomy_id,
                                                   self.pogo_input_file,
                                                   self.protein_sequence_file_path,
                                                   self.gtf_file_path)
        return self.__pogo_run_result

    def is_success(self):
        return self.__success

class PogoRunnerLocalThread(PogoRunner):
    def __init__(self,
                 ncbi_taxonomy_id=None,
                 pogo_input_file=None,
                 protein_sequence_file_path=None,
                 gtf_file_path=None):
        super().__init__(ncbi_taxonomy_id, pogo_input_file, protein_sequence_file_path, gtf_file_path)
        self._logger = main_app_config_manager \
            .get_app_config_manager() \
            .get_logger_for("{}.{}-{}".format(__name__, type(self).__name__, threading.current_thread().getName()))

    def _get_command_line_runner(self):
        command_line_runner = CommandLineRunnerFactory.get_multithread_command_line_runner()
        command_line_runner.command = self.get_pogo_run_command()
        command_line_runner.timeout = module_config_manager.get_configuration_service().get_pogo_run_timeout()
        return command_line_runner

class PogoRunnerHpc(PogoRunner):
    pass

if __name__ == '__main__':
    print("ERROR: This script is part of a pipeline collection and it is not meant to be run in stand alone mode")
<commit_msg>TODO tag on extension for pogo mismatch option, pogo run results abstraction<commit_after>#
# Author    : Manuel Bernal Llinares
# Project   : trackhub-creator
# Timestamp : 02-08-2017 16:40
# ---
# © 2017 Manuel Bernal Llinares <mbdebian@gmail.com>
# All rights reserved.
#

"""
This module contains models for dealing with PoGo stuff
"""

import os
import abc
import threading
# Application imports
import config_manager as main_app_config_manager
from parallel.models import ParallelRunner, CommandLineRunnerFactory
from parallel.exceptions import CommandLineRunnerException
from . import config_manager as module_config_manager
from .exceptions import PogoRunnerException

class PogoRunResult:
    # TODO - Needs to be extended for abstracting from results files from '-mm' parameter use
    def __init__(self,
                 ncbi_taxonomy_id=None,
                 pogo_source_file_path=None,
                 protein_sequence_file_path=None,
                 gtf_file_path=None):
        """
        Just the constructor, I had this implemented as syntactic sugar, but I was wrong
        :param ncbi_taxonomy_id: ncbi taxonomy id for this PoGo run results
        :param pogo_source_file_path: path of the source file used to run PoGo
        :param protein_sequence_file_path: FASTA file
        :param gtf_file_path: GTF file
        """
        # Logging
        self.__logger = main_app_config_manager.get_app_config_manager().get_logger_for(
            "{}.{}".format(__name__, type(self).__name__))
        # Map<pogo_result_file_extension, pogo_result_file_path>
        self.__pogo_result_file_paths = {}
        self.__pogo_source_file_path = None
        # I knew this was going to give me trouble
        self.set_ncbi_taxonomy_id(ncbi_taxonomy_id)
        self.set_protein_sequence_file_path(protein_sequence_file_path)
        self.set_gtf_file_path(gtf_file_path)
        self.set_pogo_source_file_path(pogo_source_file_path)

    def __generate_pogo_result_file_paths(self, pogo_source_file_path):
        self.__pogo_result_file_paths = {}
        pogo_results_folder_path = os.path.dirname(pogo_source_file_path)
        pogo_source_file_name = os.path.basename(pogo_source_file_path)
        for root, dirs, files in os.walk(pogo_results_folder_path):
            for pogo_result_file_path in files:
                pogo_result_file_name = os.path.basename(pogo_result_file_path)
                if pogo_result_file_name.startswith(pogo_source_file_name) and (
                            pogo_result_file_name != pogo_source_file_name):
                    # It is file generated by PoGo
                    pogo_result_file_extension = pogo_result_file_name.split(pogo_source_file_name)[1]
                    # self.__pogo_result_file_paths[pogo_result_file_extension] = pogo_result_file_path
                    # I need the full path for the pogo result file path
                    self.__pogo_result_file_paths[pogo_result_file_extension] = os.path.join(pogo_results_folder_path,
                                                                                pogo_result_file_path)
                    self._get_logger().info(
                        "PoGo run result '{}' ---> '{}', at full path '{}'".format(pogo_result_file_extension,
                                                                                pogo_result_file_path,
                                                                                self.__pogo_result_file_paths[
                                                                                pogo_result_file_extension]))

    def __get_pogo_result_file_path(self, file_extension):
        if file_extension in self.__pogo_result_file_paths:
            return self.__pogo_result_file_paths[file_extension]
        return None

    def _get_logger(self):
        return self.__logger

    def set_ncbi_taxonomy_id(self, taxonomy_id):
        self.__ncbi_taxonomy_id = taxonomy_id

    def set_pogo_source_file_path(self, pogo_source_file_path):
        if not self.__pogo_source_file_path or (self.__pogo_source_file_path != pogo_source_file_path):
            self.__pogo_source_file_path = pogo_source_file_path
            # Regenerate pogo result file paths
            self.__generate_pogo_result_file_paths(pogo_source_file_path)

    def set_protein_sequence_file_path(self, protein_sequence_file_path):
        self.__protein_sequence_file_path = protein_sequence_file_path

    def set_gtf_file_path(self, gtf_file_path):
        self.__gtf_file_path = gtf_file_path

    def get_ncbi_taxonomy_id(self):
        return self.__ncbi_taxonomy_id

    def get_pogo_result_main_bed_file_path(self):
        return self.__get_pogo_result_file_path(
            module_config_manager.get_configuration_service().get_pogo_result_file_extension_for_main_bed_file())

    def get_pogo_result_main_ptm_bed_file_path(self):
        return self.__get_pogo_result_file_path(
            module_config_manager.get_configuration_service().get_pogo_result_file_extension_for_main_ptm_bed_file()
        )

# PoGo Runners
class PogoRunnerFactory:
    @staticmethod
    def get_pogo_runner(ncbi_taxonomy_id, pogo_input_file, protein_sequence_file_path, gtf_file_path):
        # TODO - In the future, more PoGo runners will be implemented
        return PogoRunnerLocalThread(ncbi_taxonomy_id, pogo_input_file, protein_sequence_file_path, gtf_file_path)

class PogoRunner(ParallelRunner):
    def __init__(self,
                 ncbi_taxonomy_id=None,
                 pogo_input_file=None,
                 protein_sequence_file_path=None,
                 gtf_file_path=None):
        super().__init__()
        self.ncbi_taxonomy_id = ncbi_taxonomy_id
        self.pogo_input_file = pogo_input_file
        self.protein_sequence_file_path = protein_sequence_file_path
        self.gtf_file_path = gtf_file_path
        self._stdout = b' '
        self._stderr = b' '
        self.__success = True
        self.__pogo_run_result = None

    def _set_failed_to_run_pogo(self):
        self.__success = False

    def _set_success_to_run_pogo(self):
        self.__success = self.__success and True

    def _validate_environment_for_running_pogo(self):
        validation_ok = True
        if not self.ncbi_taxonomy_id:
            self._logger.warning("NO NCBI TAXONOMY ID has been specified for running PoGo with input file '{}'"
                                 .format(self.pogo_input_file))
        if not os.path.isfile(self.pogo_input_file):
            self._logger.error("PoGo input file '{}' IS NOT VALID".format(self.pogo_input_file))
            validation_ok = False
        if not os.path.isfile(self.protein_sequence_file_path):
            self._logger.error("Protein sequence file '{}' IS NOT VALID".format(self.protein_sequence_file_path))
            validation_ok = False
        if not os.path.isfile(self.gtf_file_path):
            self._logger.error("GTF file '{}' IS NOT VALID".format(self.gtf_file_path))
            validation_ok = False
        if not os.path.isfile(module_config_manager.get_configuration_service().get_pogo_binary_file_path()):
            self._logger.error("PoGo binary file '{}' IS NOT VALID"
                               .format(module_config_manager.get_configuration_service().get_pogo_binary_file_path()))
            validation_ok = False
        return validation_ok

    def get_stdout(self):
        # Never give it back until the runner is done with whatever it is doing
        if not self._done:
            raise PogoRunnerException("PoGo Runner is NOT DONE doing its job, thus 'stdout' is NOT AVAILABLE")
        return self._stdout

    def get_stderr(self):
        # Never give it back until the runner is done with whatever it is doing
        if not self._done:
            raise PogoRunnerException("PoGo Runner is NOT DONE doing its job, thus 'stderr' is NOT AVAILABLE")
        return self._stderr

    def get_pogo_run_command(self):
        pogo_parameter_species = ''
        if self.ncbi_taxonomy_id:
            pogo_parameter_species = " -species {} ".format(self.ncbi_taxonomy_id)
        pogo_command = "time {}{} -fasta {} -gtf {} -in {}" \
            .format(module_config_manager.get_configuration_service().get_pogo_binary_file_path(),
                    pogo_parameter_species,
                    self.protein_sequence_file_path,
                    self.gtf_file_path,
                    self.pogo_input_file)
        self._logger.debug("Command for running PoGo - '{}'".format(pogo_command))
        return pogo_command

    @abc.abstractmethod
    def _get_command_line_runner(self):
        """
        Although a factory is used to get a command line runner, its initialization differs depending on the final
        PogoRunner, i.e. we delegate to subclasses the provisioning of an initialized CommandLineRunner, how we use
        it, is the same for all subclasses.
        It will prepare the command line runner completely, including the command it has to run.
        :return: a CommandLineRunner for running PoGo
        """
        ...

    def _run(self):
        if not self._validate_environment_for_running_pogo():
            self._logger.error("Can't run PoGo for file '{}'".format(self.pogo_input_file))
            self._set_failed_to_run_pogo()
        else:
            command_line_runner = self._get_command_line_runner()
            try:
                command_line_runner.start()
                self._logger.debug("PoGo --- STARTED --- command '{}'".format(command_line_runner.command))
                command_line_runner.wait()
            except CommandLineRunnerException as e:
                self._logger.error("ERROR RUNNING PoGo - {}, STDOUT ---> '{}', STDERR ---> '{}'"
                                   .format(e.value,
                                           command_line_runner.get_stdout().decode('utf8'),
                                           command_line_runner.get_stderr().decode('utf8')))
                self._set_failed_to_run_pogo()
            finally:
                self._stdout = command_line_runner.get_stdout()
                self._stderr = command_line_runner.get_stderr()
            if not command_line_runner.command_success:
                self._set_failed_to_run_pogo()
                self._logger.error("PoGo FAILED on input file '{}', command '{}'"
                                   .format(self.pogo_input_file, command_line_runner.command))
            else:
                # Successful run of PoGo
                self._set_success_to_run_pogo()
                self._logger.info("PoGo SUCCESS on input file '{}', command '{}'"
                                  .format(self.pogo_input_file, command_line_runner.command))

    def get_pogo_run_result(self):
        if not self.is_done():
            raise PogoRunnerException("PoGo runner IS NOT FINISHED YET, thus, results can't be retrieved")
        if not self.__pogo_run_result:
            self.__pogo_run_result = PogoRunResult(self.ncbi_taxonomy_id,
                                                   self.pogo_input_file,
                                                   self.protein_sequence_file_path,
                                                   self.gtf_file_path)
        return self.__pogo_run_result

    def is_success(self):
        return self.__success

class PogoRunnerLocalThread(PogoRunner):
    def __init__(self,
                 ncbi_taxonomy_id=None,
                 pogo_input_file=None,
                 protein_sequence_file_path=None,
                 gtf_file_path=None):
        super().__init__(ncbi_taxonomy_id, pogo_input_file, protein_sequence_file_path, gtf_file_path)
        self._logger = main_app_config_manager \
            .get_app_config_manager() \
            .get_logger_for("{}.{}-{}".format(__name__, type(self).__name__, threading.current_thread().getName()))

    def _get_command_line_runner(self):
        command_line_runner = CommandLineRunnerFactory.get_multithread_command_line_runner()
        command_line_runner.command = self.get_pogo_run_command()
        command_line_runner.timeout = module_config_manager.get_configuration_service().get_pogo_run_timeout()
        return command_line_runner

class PogoRunnerHpc(PogoRunner):
    pass

if __name__ == '__main__':
    print("ERROR: This script is part of a pipeline collection and it is not meant to be run in stand alone mode")
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2008 John Paulett (john -at- paulett.org)
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
import types
import jsonpickle.util as util
import jsonpickle.tags as tags
import jsonpickle.handlers as handlers

class Pickler(object):
    """Converts a Python object to a JSON representation.

    Setting unpicklable to False removes the ability to regenerate
    the objects into object types beyond what the standard
    simplejson library supports.

    Setting max_depth to a negative number means there is no
    limit to the depth jsonpickle should recurse into an
    object.  Setting it to zero or higher places a hard limit
    on how deep jsonpickle recurses into objects, dictionaries, etc.

    >>> p = Pickler()
    >>> p.flatten('hello world')
    'hello world'
    """

    def __init__(self, unpicklable=True, max_depth=None):
        self.unpicklable = unpicklable
        ## The current recursion depth
        self._depth = -1
        ## The maximal recursion depth
        self._max_depth = max_depth
        ## Maps id(obj) to reference names
        self._objs = {}
        ## The namestack grows whenever we recurse into a child object
        self._namestack = []

    def _reset(self):
        self._objs = {}
        self._namestack = []

    def _push(self):
        """Steps down one level in the namespace.
        """
        self._depth += 1

    def _pop(self, value):
        """Step up one level in the namespace and return the value.
        If we're at the root, reset the pickler's state.
        """
        self._depth -= 1
        if self._depth == -1:
            self._reset()
        return value

    def _mkref(self, obj):
        objid = id(obj)
        if objid not in self._objs:
            self._objs[objid] = '/' + '/'.join(self._namestack)
            return True
        return False

    def _getref(self, obj):
        return {tags.REF: self._objs.get(id(obj))}

    def flatten(self, obj):
        """Takes an object and returns a JSON-safe representation of it.

        Simply returns any of the basic builtin datatypes

        >>> p = Pickler()
        >>> p.flatten('hello world')
        'hello world'
        >>> p.flatten(u'hello world')
        u'hello world'
        >>> p.flatten(49)
        49
        >>> p.flatten(350.0)
        350.0
        >>> p.flatten(True)
        True
        >>> p.flatten(False)
        False
        >>> r = p.flatten(None)
        >>> r is None
        True
        >>> p.flatten(False)
        False
        >>> p.flatten([1, 2, 3, 4])
        [1, 2, 3, 4]
        >>> p.flatten((1,2,))[tags.TUPLE]
        [1, 2]
        >>> p.flatten({'key': 'value'})
        {'key': 'value'}
        """

        self._push()

        if self._depth == self._max_depth:
            return self._pop(repr(obj))

        if util.is_primitive(obj):
            return self._pop(obj)

        if util.is_list(obj):
            return self._pop([ self.flatten(v) for v in obj ])

        # We handle tuples and sets by encoding them in a "(tuple|set)dict"
        if util.is_tuple(obj):
            if self.unpicklable is True:
                return self._pop({tags.TUPLE: [ self.flatten(v) for v in obj ]})
            else:
                return self._pop([ self.flatten(v) for v in obj ])

        if util.is_set(obj):
            if self.unpicklable is True:
                return self._pop({tags.SET: [ self.flatten(v) for v in obj ]})
            else:
                return self._pop([ self.flatten(v) for v in obj ])

        if util.is_dictionary(obj):
            return self._pop(self._flatten_dict_obj(obj, obj.__class__()))

        if util.is_type(obj):
            return self._pop(_mktyperef(obj))

        if util.is_object(obj):
            if self._mkref(obj):
                # We've never seen this object so return its
                # json representation.
                return self._pop(self._flatten_obj_instance(obj))
            else:
                # We've seen this object before so place an object
                # reference tag in the data. This avoids infinite recursion
                # when processing cyclical objects.
                return self._pop(self._getref(obj))

            return self._pop(data)
        # else, what else? (methods, functions, old style classes...)

    def _flatten_obj_instance(self, obj):
        """Recursively flatten an instance and return a json-friendly dict
        """
        data = {}
        has_class = hasattr(obj, '__class__')
        has_dict = hasattr(obj, '__dict__')
        has_slots = not has_dict and hasattr(obj, '__slots__')
        has_getstate = has_dict and hasattr(obj, '__getstate__')
        has_getstate_support = has_getstate and hasattr(obj, '__setstate__')
        HandlerClass = handlers.registry.get(type(obj))

        if (has_class and not util.is_repr(obj) and
                not util.is_module(obj)):
            module, name = _getclassdetail(obj)
            if self.unpicklable is True:
                data[tags.OBJECT] = '%s.%s' % (module, name)
            # Check for a custom handler
            if HandlerClass:
                handler = HandlerClass(self)
                return handler.flatten(obj, data)

        if util.is_module(obj):
            if self.unpicklable is True:
                data[tags.REPR] = '%s/%s' % (obj.__name__,
                                             obj.__name__)
            else:
                data = unicode(obj)
            return data

        if util.is_repr(obj):
            if self.unpicklable is True:
                data[tags.REPR] = '%s/%s' % (obj.__class__.__module__,
                                             repr(obj))
            else:
                data = unicode(obj)
            return data

        if util.is_dictionary_subclass(obj):
            return self._flatten_dict_obj(obj, data)

        if util.is_noncomplex(obj):
            return [self.flatten(v) for v in obj]

        if has_dict:
            # Support objects that subclasses list and set
            if util.is_collection_subclass(obj):
                return self._flatten_collection_obj(obj, data)

            # Support objects with __getstate__(); this ensures that
            # both __setstate__() and __getstate__() are implemented
            if has_getstate_support:
                data[tags.STATE] = self.flatten(obj.__getstate__())
                return data

            # hack for zope persistent objects; this unghostifies the object
            getattr(obj, '_', None)
            return self._flatten_dict_obj(obj.__dict__, data)

        if has_slots:
            return self._flatten_newstyle_with_slots(obj, data)

    def _flatten_dict_obj(self, obj, data):
        """Recursively call flatten() and return json-friendly dict
        """
        for k, v in sorted(obj.iteritems(), key=lambda item:item[0]):
            self._flatten_key_value_pair(k, v, data)
        return data

    def _flatten_newstyle_with_slots(self, obj, data):
        """Return a json-friendly dict for new-style objects with __slots__.
        """
        for k in obj.__slots__:
            self._flatten_key_value_pair(k, getattr(obj, k), data)
        return data

    def _flatten_key_value_pair(self, k, v, data):
        """Flatten a key/value pair into the passed-in dictionary."""
        if not util.is_picklable(k, v):
            return data
        if type(k) not in types.StringTypes:
            try:
                k = repr(k)
            except:
                k = unicode(k)
        self._namestack.append(k)
        data[k] = self.flatten(v)
        self._namestack.pop()
        return data

    def _flatten_collection_obj(self, obj, data):
        """Return a json-friendly dict for a collection subclass."""
        self._flatten_dict_obj(obj.__dict__, data)
        data[tags.SEQ] = [ self.flatten(v) for v in obj ]
        return data

def _mktyperef(obj):
    """Return a typeref dictionary.  Used for references.

    >>> from jsonpickle import tags
    >>> _mktyperef(AssertionError)[tags.TYPE].rsplit('.', 1)[0]
    'exceptions'

    >>> _mktyperef(AssertionError)[tags.TYPE].rsplit('.', 1)[-1]
    'AssertionError'
    """
    return {tags.TYPE: '%s.%s' % (obj.__module__, obj.__name__)}

def _getclassdetail(obj):
    """Helper class to return the class of an object.

    >>> class Example(object): pass
    >>> _getclassdetail(Example())
    ('jsonpickle.pickler', 'Example')
    >>> _getclassdetail(25)
    ('__builtin__', 'int')
    >>> _getclassdetail(None)
    ('__builtin__', 'NoneType')
    >>> _getclassdetail(False)
    ('__builtin__', 'bool')
    """
    cls = obj.__class__
    module = getattr(cls, '__module__')
    name = getattr(cls, '__name__')
    return module, name
<commit_msg>Don't use py/ref when unpicklable=False.<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2008 John Paulett (john -at- paulett.org)
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
import types
import jsonpickle.util as util
import jsonpickle.tags as tags
import jsonpickle.handlers as handlers

class Pickler(object):
    """Converts a Python object to a JSON representation.

    Setting unpicklable to False removes the ability to regenerate
    the objects into object types beyond what the standard
    simplejson library supports.

    Setting max_depth to a negative number means there is no
    limit to the depth jsonpickle should recurse into an
    object.  Setting it to zero or higher places a hard limit
    on how deep jsonpickle recurses into objects, dictionaries, etc.

    >>> p = Pickler()
    >>> p.flatten('hello world')
    'hello world'
    """

    def __init__(self, unpicklable=True, max_depth=None):
        self.unpicklable = unpicklable
        ## The current recursion depth
        self._depth = -1
        ## The maximal recursion depth
        self._max_depth = max_depth
        ## Maps id(obj) to reference names
        self._objs = {}
        ## The namestack grows whenever we recurse into a child object
        self._namestack = []

    def _reset(self):
        self._objs = {}
        self._namestack = []

    def _push(self):
        """Steps down one level in the namespace.
        """
        self._depth += 1

    def _pop(self, value):
        """Step up one level in the namespace and return the value.
        If we're at the root, reset the pickler's state.
        """
        self._depth -= 1
        if self._depth == -1:
            self._reset()
        return value

    def _mkref(self, obj):
        # Do not use references if not unpicklable.
        if self.unpicklable is False:
            return True
        objid = id(obj)
        if objid not in self._objs:
            self._objs[objid] = '/' + '/'.join(self._namestack)
            return True
        return False

    def _getref(self, obj):
        return {tags.REF: self._objs.get(id(obj))}

    def flatten(self, obj):
        """Takes an object and returns a JSON-safe representation of it.

        Simply returns any of the basic builtin datatypes

        >>> p = Pickler()
        >>> p.flatten('hello world')
        'hello world'
        >>> p.flatten(u'hello world')
        u'hello world'
        >>> p.flatten(49)
        49
        >>> p.flatten(350.0)
        350.0
        >>> p.flatten(True)
        True
        >>> p.flatten(False)
        False
        >>> r = p.flatten(None)
        >>> r is None
        True
        >>> p.flatten(False)
        False
        >>> p.flatten([1, 2, 3, 4])
        [1, 2, 3, 4]
        >>> p.flatten((1,2,))[tags.TUPLE]
        [1, 2]
        >>> p.flatten({'key': 'value'})
        {'key': 'value'}
        """

        self._push()

        if self._depth == self._max_depth:
            return self._pop(repr(obj))

        if util.is_primitive(obj):
            return self._pop(obj)

        if util.is_list(obj):
            return self._pop([ self.flatten(v) for v in obj ])

        # We handle tuples and sets by encoding them in a "(tuple|set)dict"
        if util.is_tuple(obj):
            if self.unpicklable is True:
                return self._pop({tags.TUPLE: [ self.flatten(v) for v in obj ]})
            else:
                return self._pop([ self.flatten(v) for v in obj ])

        if util.is_set(obj):
            if self.unpicklable is True:
                return self._pop({tags.SET: [ self.flatten(v) for v in obj ]})
            else:
                return self._pop([ self.flatten(v) for v in obj ])

        if util.is_dictionary(obj):
            return self._pop(self._flatten_dict_obj(obj, obj.__class__()))

        if util.is_type(obj):
            return self._pop(_mktyperef(obj))

        if util.is_object(obj):
            if self._mkref(obj):
                # We've never seen this object so return its
                # json representation.
                return self._pop(self._flatten_obj_instance(obj))
            else:
                # We've seen this object before so place an object
                # reference tag in the data. This avoids infinite recursion
                # when processing cyclical objects.
                return self._pop(self._getref(obj))

            return self._pop(data)
        # else, what else? (methods, functions, old style classes...)

    def _flatten_obj_instance(self, obj):
        """Recursively flatten an instance and return a json-friendly dict
        """
        data = {}
        has_class = hasattr(obj, '__class__')
        has_dict = hasattr(obj, '__dict__')
        has_slots = not has_dict and hasattr(obj, '__slots__')
        has_getstate = has_dict and hasattr(obj, '__getstate__')
        has_getstate_support = has_getstate and hasattr(obj, '__setstate__')
        HandlerClass = handlers.registry.get(type(obj))

        if (has_class and not util.is_repr(obj) and
                not util.is_module(obj)):
            module, name = _getclassdetail(obj)
            if self.unpicklable is True:
                data[tags.OBJECT] = '%s.%s' % (module, name)
            # Check for a custom handler
            if HandlerClass:
                handler = HandlerClass(self)
                return handler.flatten(obj, data)

        if util.is_module(obj):
            if self.unpicklable is True:
                data[tags.REPR] = '%s/%s' % (obj.__name__,
                                             obj.__name__)
            else:
                data = unicode(obj)
            return data

        if util.is_repr(obj):
            if self.unpicklable is True:
                data[tags.REPR] = '%s/%s' % (obj.__class__.__module__,
                                             repr(obj))
            else:
                data = unicode(obj)
            return data

        if util.is_dictionary_subclass(obj):
            return self._flatten_dict_obj(obj, data)

        if util.is_noncomplex(obj):
            return [self.flatten(v) for v in obj]

        if has_dict:
            # Support objects that subclasses list and set
            if util.is_collection_subclass(obj):
                return self._flatten_collection_obj(obj, data)

            # Support objects with __getstate__(); this ensures that
            # both __setstate__() and __getstate__() are implemented
            if has_getstate_support:
                data[tags.STATE] = self.flatten(obj.__getstate__())
                return data

            # hack for zope persistent objects; this unghostifies the object
            getattr(obj, '_', None)
            return self._flatten_dict_obj(obj.__dict__, data)

        if has_slots:
            return self._flatten_newstyle_with_slots(obj, data)

    def _flatten_dict_obj(self, obj, data):
        """Recursively call flatten() and return json-friendly dict
        """
        for k, v in sorted(obj.iteritems(), key=lambda item:item[0]):
            self._flatten_key_value_pair(k, v, data)
        return data

    def _flatten_newstyle_with_slots(self, obj, data):
        """Return a json-friendly dict for new-style objects with __slots__.
        """
        for k in obj.__slots__:
            self._flatten_key_value_pair(k, getattr(obj, k), data)
        return data

    def _flatten_key_value_pair(self, k, v, data):
        """Flatten a key/value pair into the passed-in dictionary."""
        if not util.is_picklable(k, v):
            return data
        if type(k) not in types.StringTypes:
            try:
                k = repr(k)
            except:
                k = unicode(k)
        self._namestack.append(k)
        data[k] = self.flatten(v)
        self._namestack.pop()
        return data

    def _flatten_collection_obj(self, obj, data):
        """Return a json-friendly dict for a collection subclass."""
        self._flatten_dict_obj(obj.__dict__, data)
        data[tags.SEQ] = [ self.flatten(v) for v in obj ]
        return data

def _mktyperef(obj):
    """Return a typeref dictionary.  Used for references.

    >>> from jsonpickle import tags
    >>> _mktyperef(AssertionError)[tags.TYPE].rsplit('.', 1)[0]
    'exceptions'

    >>> _mktyperef(AssertionError)[tags.TYPE].rsplit('.', 1)[-1]
    'AssertionError'
    """
    return {tags.TYPE: '%s.%s' % (obj.__module__, obj.__name__)}

def _getclassdetail(obj):
    """Helper class to return the class of an object.

    >>> class Example(object): pass
    >>> _getclassdetail(Example())
    ('jsonpickle.pickler', 'Example')
    >>> _getclassdetail(25)
    ('__builtin__', 'int')
    >>> _getclassdetail(None)
    ('__builtin__', 'NoneType')
    >>> _getclassdetail(False)
    ('__builtin__', 'bool')
    """
    cls = obj.__class__
    module = getattr(cls, '__module__')
    name = getattr(cls, '__name__')
    return module, name
<|endoftext|>