<commit_before>__author__ = "Mr.doob, Kikko"
__url__ = ['http://mrdoob.com', 'http://github.com/kikko']
__version__ = "1"
__bpydoc__ = """\
This script exports the selected object for the three.js engine.
"""

import bpy

def rvec3d(v):
    return round(v[0], 6), round(v[1], 6), round(v[2], 6)

def rvec2d(v):
    return round(v[0], 6), round(v[1], 6)

def write(filename, scene, ob, \
        EXPORT_APPLY_MODIFIERS=True,\
        EXPORT_NORMALS=True,\
        EXPORT_UV=True,\
        EXPORT_COLORS=True):

    if not filename.lower().endswith('.js'):
        filename += '.js'

    classname = filename.split('/')[-1].replace('.js','')

    if not ob:
        raise Exception("Error, Select the object to export")
        return

    file = open(filename, 'w')

    if EXPORT_APPLY_MODIFIERS:
        mesh = ob.create_mesh(scene, True, 'PREVIEW')
    else:
        mesh = ob.data

    if not mesh:
        raise ("Error, could not get mesh data from selected object")
        return

    faceUV = len(mesh.uv_textures) > 0
    vertexUV = len(mesh.sticky) > 0
    vertexColors = len(mesh.vertex_colors) > 0

    if (not faceUV) and (not vertexUV):
        EXPORT_UV = False
    if not vertexColors:
        EXPORT_COLORS = False

    if not EXPORT_UV:
        faceUV = vertexUV = False
    if not EXPORT_COLORS:
        vertexColors = False

    if faceUV:
        active_uv_layer = mesh.active_uv_texture
        if not active_uv_layer:
            EXPORT_UV = False
            faceUV = None
        else:
            active_uv_layer = active_uv_layer.data

    if vertexColors:
        active_col_layer = mesh.active_vertex_color
        if not active_col_layer:
            EXPORT_COLORS = False
            vertexColors = None
        else:
            active_col_layer = active_col_layer.data

    # incase
    color = uvcoord = uvcoord_key = normal = normal_key = None

    file.write('var %s = function () {\n\n' % classname)

    file.write('\tvar scope = this;\n\n')

    file.write('\tTHREE.Geometry.call(this);\n\n')

    for v in mesh.verts:
        file.write('\tv( %.6f, %.6f, %.6f );\n' % (v.co.x, v.co.z, -v.co.y)) # co

    file.write('\n')

    if EXPORT_NORMALS:
        for f in mesh.faces:
            if len(f.verts) == 3:
                file.write('\tf3n( %d, %d, %d, %.6f, %.6f, %.6f );\n' % (f.verts[0], f.verts[1], f.verts[2], f.normal[0], f.normal[1], f.normal[2]))
            else:
                file.write('\tf4( %d, %d, %d, %d, %.6f, %.6f, %.6f );\n' % (f.verts[0], f.verts[1], f.verts[2], f.verts[3], f.normal[0], f.normal[1], f.normal[2]))

    else:
        for f in mesh.faces:
            if len(f.verts) == 3:
                file.write('\tf3( %d, %d, %d );\n' % (f.verts[0], f.verts[1], f.verts[2]))
            else:
                file.write('\tf4( %d, %d, %d, %d );\n' % (f.verts[0], f.verts[1], f.verts[2], f.verts[3]))

    face_index_pairs = [ (face, index) for index, face in enumerate(mesh.faces)]

    if EXPORT_UV:
        file.write('\n')
        for f, f_index in face_index_pairs:
            tface = mesh.uv_textures[0].data[f_index]
            if len(f.verts) == 3:
                file.write('\tuv( %.6f, %.6f, %.6f, %.6f, %.6f, %.6f );\n' % (tface.uv1[0], 1.0-tface.uv1[1], tface.uv2[0], 1.0-tface.uv2[1], tface.uv3[0], 1.0-tface.uv3[1]))
            else:
                file.write('\tuv( %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f );\n' % (tface.uv1[0], 1.0-tface.uv1[1], tface.uv2[0], 1.0-tface.uv2[1], tface.uv3[0], 1.0-tface.uv3[1], tface.uv4[0], 1.0-tface.uv4[1]))

    file.write('\n')

    file.write('\tfunction v( x, y, z ) {\n\n')
    file.write('\t\tscope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );\n\n')
    file.write('\t}\n\n')

    file.write('\tfunction f3( a, b, c, nx, ny, nz ) {\n\n')
    file.write('\t\tscope.faces.push( new THREE.Face3( a, b, c, nx && ny && nz ? new THREE.Vector3( nx, ny, nz ) : null ) );\n\n')
    file.write('\t}\n\n')

    file.write('\tfunction f4( a, b, c, d, nx, ny, nz ) {\n\n')
    file.write('\t\tscope.faces.push( new THREE.Face4( a, b, c, d, nx && ny && nz ? new THREE.Vector3( nx, ny, nz ) : null ) );\n\n')
    file.write('\t}\n\n')

    file.write('\tfunction uv( u1, v1, u2, v2, u3, v3, u4, v4 ) {\n\n')
    file.write('\t\tvar uv = [];\n')
    file.write('\t\tuv.push( new THREE.UV( u1, v1 ) );\n')
    file.write('\t\tuv.push( new THREE.UV( u2, v2 ) );\n')
    file.write('\t\tuv.push( new THREE.UV( u3, v3 ) );\n')
    file.write('\t\tif ( u4 && v4 ) uv.push( new THREE.UV( u4, v4 ) );\n')
    file.write('\t\tscope.uvs.push( uv );\n')
    file.write('\t}\n\n')

    file.write('}\n\n')

    file.write('%s.prototype = new THREE.Geometry();\n' % classname)
    file.write('%s.prototype.constructor = %s;' % (classname, classname))

    file.close()

    print("writing", filename, "done")

    if EXPORT_APPLY_MODIFIERS:
        bpy.data.meshes.remove(mesh)

from bpy.props import *

class ExportTHREEJS(bpy.types.Operator):
    '''TODO'''
    bl_idname = "export.three_js"
    bl_label = "Export three.js"

    # List of operator properties, the attributes will be assigned
    # to the class instance from the operator settings before calling.

    filepath = StringProperty(name="File Path", description="File path used for exporting the PLY file", maxlen=1024, default="")
    check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'})
    use_modifiers = BoolProperty(name="Apply Modifiers", description="Apply Modifiers to the exported mesh", default=True)
    use_normals = BoolProperty(name="Normals", description="Export Normals for smooth and hard shaded faces", default=True)
    use_uvs = BoolProperty(name="UVs", description="Exort the active UV layer", default=True)
    use_colors = BoolProperty(name="Vertex Colors", description="Exort the active vertex color layer", default=True)

    def poll(self, context):
        return context.active_object != None

    def execute(self, context):
        print("Selected: " + context.active_object.name)

        if not self.properties.filepath:
            raise Exception("filename not set")

        write(self.properties.filepath, context.scene, context.active_object,\
            EXPORT_APPLY_MODIFIERS=self.properties.use_modifiers,
            EXPORT_NORMALS=self.properties.use_normals,
            EXPORT_UV=self.properties.use_uvs,
            EXPORT_COLORS=self.properties.use_colors,
        )

        return {'FINISHED'}

    def invoke(self, context, event):
        wm = context.manager
        wm.add_fileselect(self)
        return {'RUNNING_MODAL'}

    def draw(self, context):
        layout = self.layout
        props = self.properties

        row = layout.row()
        row.prop(props, "use_modifiers")
        row.prop(props, "use_normals")
        row = layout.row()
        row.prop(props, "use_uvs")
        row.prop(props, "use_colors")

def menu_func(self, context):
    default_path = bpy.data.filepath.replace(".blend", ".js")
    self.layout.operator(ExportTHREEJS.bl_idname, text="three.js (.js)").filepath = default_path

def register():
    bpy.types.register(ExportTHREEJS)
    bpy.types.INFO_MT_file_export.append(menu_func)

def unregister():
    bpy.types.unregister(ExportTHREEJS)
    bpy.types.INFO_MT_file_export.remove(menu_func)

if __name__ == "__main__":
    register()

<commit_msg>- Missed a line change on the Blender 2.5b exporter<commit_after>__author__ = "Mr.doob, Kikko"
__url__ = ['http://mrdoob.com', 'http://github.com/kikko']
__version__ = "1"
__bpydoc__ = """\
This script exports the selected object for the three.js engine.
"""

import bpy

def rvec3d(v):
    return round(v[0], 6), round(v[1], 6), round(v[2], 6)

def rvec2d(v):
    return round(v[0], 6), round(v[1], 6)

def write(filename, scene, ob, \
        EXPORT_APPLY_MODIFIERS=True,\
        EXPORT_NORMALS=True,\
        EXPORT_UV=True,\
        EXPORT_COLORS=True):

    if not filename.lower().endswith('.js'):
        filename += '.js'

    classname = filename.split('/')[-1].replace('.js','')

    if not ob:
        raise Exception("Error, Select the object to export")
        return

    file = open(filename, 'w')

    if EXPORT_APPLY_MODIFIERS:
        mesh = ob.create_mesh(scene, True, 'PREVIEW')
    else:
        mesh = ob.data

    if not mesh:
        raise ("Error, could not get mesh data from selected object")
        return

    faceUV = len(mesh.uv_textures) > 0
    vertexUV = len(mesh.sticky) > 0
    vertexColors = len(mesh.vertex_colors) > 0

    if (not faceUV) and (not vertexUV):
        EXPORT_UV = False
    if not vertexColors:
        EXPORT_COLORS = False

    if not EXPORT_UV:
        faceUV = vertexUV = False
    if not EXPORT_COLORS:
        vertexColors = False

    if faceUV:
        active_uv_layer = mesh.active_uv_texture
        if not active_uv_layer:
            EXPORT_UV = False
            faceUV = None
        else:
            active_uv_layer = active_uv_layer.data

    if vertexColors:
        active_col_layer = mesh.active_vertex_color
        if not active_col_layer:
            EXPORT_COLORS = False
            vertexColors = None
        else:
            active_col_layer = active_col_layer.data

    # incase
    color = uvcoord = uvcoord_key = normal = normal_key = None

    file.write('var %s = function () {\n\n' % classname)

    file.write('\tvar scope = this;\n\n')

    file.write('\tTHREE.Geometry.call(this);\n\n')

    for v in mesh.verts:
        file.write('\tv( %.6f, %.6f, %.6f );\n' % (v.co.x, v.co.z, -v.co.y)) # co

    file.write('\n')

    if EXPORT_NORMALS:
        for f in mesh.faces:
            if len(f.verts) == 3:
                file.write('\tf3( %d, %d, %d, %.6f, %.6f, %.6f );\n' % (f.verts[0], f.verts[1], f.verts[2], f.normal[0], f.normal[1], f.normal[2]))
            else:
                file.write('\tf4( %d, %d, %d, %d, %.6f, %.6f, %.6f );\n' % (f.verts[0], f.verts[1], f.verts[2], f.verts[3], f.normal[0], f.normal[1], f.normal[2]))

    else:
        for f in mesh.faces:
            if len(f.verts) == 3:
                file.write('\tf3( %d, %d, %d );\n' % (f.verts[0], f.verts[1], f.verts[2]))
            else:
                file.write('\tf4( %d, %d, %d, %d );\n' % (f.verts[0], f.verts[1], f.verts[2], f.verts[3]))

    face_index_pairs = [ (face, index) for index, face in enumerate(mesh.faces)]

    if EXPORT_UV:
        file.write('\n')
        for f, f_index in face_index_pairs:
            tface = mesh.uv_textures[0].data[f_index]
            if len(f.verts) == 3:
                file.write('\tuv( %.6f, %.6f, %.6f, %.6f, %.6f, %.6f );\n' % (tface.uv1[0], 1.0-tface.uv1[1], tface.uv2[0], 1.0-tface.uv2[1], tface.uv3[0], 1.0-tface.uv3[1]))
            else:
                file.write('\tuv( %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f );\n' % (tface.uv1[0], 1.0-tface.uv1[1], tface.uv2[0], 1.0-tface.uv2[1], tface.uv3[0], 1.0-tface.uv3[1], tface.uv4[0], 1.0-tface.uv4[1]))

    file.write('\n')

    file.write('\tfunction v( x, y, z ) {\n\n')
    file.write('\t\tscope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );\n\n')
    file.write('\t}\n\n')

    file.write('\tfunction f3( a, b, c, nx, ny, nz ) {\n\n')
    file.write('\t\tscope.faces.push( new THREE.Face3( a, b, c, nx && ny && nz ? new THREE.Vector3( nx, ny, nz ) : null ) );\n\n')
    file.write('\t}\n\n')

    file.write('\tfunction f4( a, b, c, d, nx, ny, nz ) {\n\n')
    file.write('\t\tscope.faces.push( new THREE.Face4( a, b, c, d, nx && ny && nz ? new THREE.Vector3( nx, ny, nz ) : null ) );\n\n')
    file.write('\t}\n\n')

    file.write('\tfunction uv( u1, v1, u2, v2, u3, v3, u4, v4 ) {\n\n')
    file.write('\t\tvar uv = [];\n')
    file.write('\t\tuv.push( new THREE.UV( u1, v1 ) );\n')
    file.write('\t\tuv.push( new THREE.UV( u2, v2 ) );\n')
    file.write('\t\tuv.push( new THREE.UV( u3, v3 ) );\n')
    file.write('\t\tif ( u4 && v4 ) uv.push( new THREE.UV( u4, v4 ) );\n')
    file.write('\t\tscope.uvs.push( uv );\n')
    file.write('\t}\n\n')

    file.write('}\n\n')

    file.write('%s.prototype = new THREE.Geometry();\n' % classname)
    file.write('%s.prototype.constructor = %s;' % (classname, classname))

    file.close()

    print("writing", filename, "done")

    if EXPORT_APPLY_MODIFIERS:
        bpy.data.meshes.remove(mesh)

from bpy.props import *

class ExportTHREEJS(bpy.types.Operator):
    '''TODO'''
    bl_idname = "export.three_js"
    bl_label = "Export three.js"

    # List of operator properties, the attributes will be assigned
    # to the class instance from the operator settings before calling.

    filepath = StringProperty(name="File Path", description="File path used for exporting the PLY file", maxlen=1024, default="")
    check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'})
    use_modifiers = BoolProperty(name="Apply Modifiers", description="Apply Modifiers to the exported mesh", default=True)
    use_normals = BoolProperty(name="Normals", description="Export Normals for smooth and hard shaded faces", default=True)
    use_uvs = BoolProperty(name="UVs", description="Exort the active UV layer", default=True)
    use_colors = BoolProperty(name="Vertex Colors", description="Exort the active vertex color layer", default=True)

    def poll(self, context):
        return context.active_object != None

    def execute(self, context):
        print("Selected: " + context.active_object.name)

        if not self.properties.filepath:
            raise Exception("filename not set")

        write(self.properties.filepath, context.scene, context.active_object,\
            EXPORT_APPLY_MODIFIERS=self.properties.use_modifiers,
            EXPORT_NORMALS=self.properties.use_normals,
            EXPORT_UV=self.properties.use_uvs,
            EXPORT_COLORS=self.properties.use_colors,
        )

        return {'FINISHED'}

    def invoke(self, context, event):
        wm = context.manager
        wm.add_fileselect(self)
        return {'RUNNING_MODAL'}

    def draw(self, context):
        layout = self.layout
        props = self.properties

        row = layout.row()
        row.prop(props, "use_modifiers")
        row.prop(props, "use_normals")
        row = layout.row()
        row.prop(props, "use_uvs")
        row.prop(props, "use_colors")

def menu_func(self, context):
    default_path = bpy.data.filepath.replace(".blend", ".js")
    self.layout.operator(ExportTHREEJS.bl_idname, text="three.js (.js)").filepath = default_path

def register():
    bpy.types.register(ExportTHREEJS)
    bpy.types.INFO_MT_file_export.append(menu_func)

def unregister():
    bpy.types.unregister(ExportTHREEJS)
    bpy.types.INFO_MT_file_export.remove(menu_func)

if __name__ == "__main__":
    register()

<|endoftext|>


<commit_before># ex: set expandtab softtabstop=4 shiftwidth=4: -*- cpy-indent-level: 4; indent-tabs-mode: nil -*-
#
# Copyright (C) 2009  Contributor
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the EU DataGrid Software License.  You should
# have received a copy of the license with this program, and the
# license is published at
# http://eu-datagrid.web.cern.ch/eu-datagrid/license.html.
#
# THE FOLLOWING DISCLAIMER APPLIES TO ALL SOFTWARE CODE AND OTHER
# MATERIALS CONTRIBUTED IN CONNECTION WITH THIS PROGRAM.
#
# THIS SOFTWARE IS LICENSED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE AND ANY WARRANTY OF NON-INFRINGEMENT, ARE
# DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THIS
# SOFTWARE MAY BE REDISTRIBUTED TO OTHERS ONLY BY EFFECTIVELY USING
# THIS OR ANOTHER EQUIVALENT DISCLAIMER AS WELL AS ANY OTHER LICENSE
# TERMS THAT MAY APPLY.

from aquilon.server.broker import BrokerCommand
from aquilon.aqdb.model import Vendor
from aquilon.exceptions_ import ArgumentError
import re

class CommandAddVendor(BrokerCommand):

    required_parameters = [ "vendor" ]

    def render(self, session, vendor, **arguments):
        valid = re.compile('^[a-zA-Z0-9_.-]+$')
        if (not valid.match(vendor)):
            raise ArgumentError("vendor name '%s' is not valid" % vendor)

        existing = session.query(Vendor).filter_by(name=vendor).first()
        if existing:
            raise ArgumentError("vendor '%s' already exists" % vendor)

        dbv = Vendor(name=vendor)
        session.save(dbv)
        return
<commit_msg>replace deprecated session.save in place of sess.add()<commit_after># ex: set expandtab softtabstop=4 shiftwidth=4: -*- cpy-indent-level: 4; indent-tabs-mode: nil -*-
#
# Copyright (C) 2009  Contributor
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the EU DataGrid Software License.  You should
# have received a copy of the license with this program, and the
# license is published at
# http://eu-datagrid.web.cern.ch/eu-datagrid/license.html.
#
# THE FOLLOWING DISCLAIMER APPLIES TO ALL SOFTWARE CODE AND OTHER
# MATERIALS CONTRIBUTED IN CONNECTION WITH THIS PROGRAM.
#
# THIS SOFTWARE IS LICENSED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE AND ANY WARRANTY OF NON-INFRINGEMENT, ARE
# DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THIS
# SOFTWARE MAY BE REDISTRIBUTED TO OTHERS ONLY BY EFFECTIVELY USING
# THIS OR ANOTHER EQUIVALENT DISCLAIMER AS WELL AS ANY OTHER LICENSE
# TERMS THAT MAY APPLY.

from aquilon.server.broker import BrokerCommand
from aquilon.aqdb.model import Vendor
from aquilon.exceptions_ import ArgumentError
import re

class CommandAddVendor(BrokerCommand):

    required_parameters = [ "vendor" ]

    def render(self, session, vendor, **arguments):
        valid = re.compile('^[a-zA-Z0-9_.-]+$')
        if (not valid.match(vendor)):
            raise ArgumentError("vendor name '%s' is not valid" % vendor)

        existing = session.query(Vendor).filter_by(name=vendor).first()
        if existing:
            raise ArgumentError("vendor '%s' already exists" % vendor)

        dbv = Vendor(name=vendor)
        session.add(dbv)
        return
<|endoftext|>


<commit_before>"""
Scrapy logging facility

See documentation in docs/topics/logging.rst
"""
import sys
import logging

from twisted.python import log

from scrapy.conf import settings
from scrapy.utils.python import unicode_to_str

# Logging levels
DEBUG = logging.DEBUG
INFO = logging.INFO
WARNING = logging.WARNING
ERROR = logging.ERROR
CRITICAL = logging.CRITICAL
SILENT = CRITICAL + 1

level_names = {
    logging.DEBUG: "DEBUG",
    logging.INFO: "INFO",
    logging.WARNING: "WARNING",
    logging.ERROR: "ERROR",
    logging.CRITICAL: "CRITICAL",
    SILENT: "SILENT",
}

started = False

class ScrapyFileLogObserver(log.FileLogObserver):

    def __init__(self, f, level=INFO, encoding='utf-8'):
        self.level = level
        self.encoding = encoding
        log.FileLogObserver.__init__(self, f)

    def emit(self, eventDict):
        ev = eventDict.copy()
        if ev['isError']:
            ev.setdefault('logLevel', ERROR)
        # ignore non-error messages from outside scrapy
        if ev.get('system') != 'scrapy' and not ev['isError']:
            return
        level = ev.get('logLevel')
        if level < self.level:
            return
        spider = ev.get('spider')
        if spider:
            ev['system'] = spider.name
        message = ev.get('message')
        lvlname = level_names.get(level, 'NOLEVEL')
        if message:
            message = [unicode_to_str(x, self.encoding) for x in message]
            message[0] = "%s: %s" % (lvlname, message[0])
        ev['message'] = message
        why = ev.get('why')
        if why:
            why = "%s: %s" % (lvlname, unicode_to_str(why, self.encoding))
        ev['why'] = why
        log.FileLogObserver.emit(self, ev)

def _get_log_level(level_name_or_id=None):
    if level_name_or_id is None:
        lvlname = settings['LOG_LEVEL']
        return globals()[lvlname]
    elif isinstance(level_name_or_id, int):
        return level_name_or_id
    elif isinstance(level_name_or_id, basestring):
        return globals()[level_name_or_id]
    else:
        raise ValueError("Unknown log level: %r" % level_name_or_id)

def start(logfile=None, loglevel=None, logstdout=None):
    global started
    if started or not settings.getbool('LOG_ENABLED'):
        return
    started = True

    if log.defaultObserver: # check twisted log not already started
        loglevel = _get_log_level(loglevel)
        logfile = logfile or settings['LOG_FILE']
        file = open(logfile, 'a') if logfile else sys.stderr
        if logstdout is None:
            logstdout = settings.getbool('LOG_STDOUT')
        sflo = ScrapyFileLogObserver(file, loglevel, settings['LOG_ENCODING'])
        log.startLoggingWithObserver(sflo.emit, setStdout=logstdout)
        msg("Started project: %s" % settings['BOT_NAME'])

def msg(message, level=INFO, **kw):
    if 'component' in kw:
        import warnings
        warnings.warn("Argument `component` of scrapy.log.msg() is deprecated", \
            DeprecationWarning, stacklevel=2)
    kw.setdefault('system', 'scrapy')
    kw['logLevel'] = level
    log.msg(message, **kw)

def err(_stuff=None, _why=None, **kw):
    kw.setdefault('system', 'scrapy')
    kw['logLevel'] = kw.pop('level', ERROR)
    log.err(_stuff, _why, **kw)
<commit_msg>moved scrapy log observer logic into a separate function<commit_after>"""
Scrapy logging facility

See documentation in docs/topics/logging.rst
"""
import sys
import logging

from twisted.python import log

from scrapy.conf import settings
from scrapy.utils.python import unicode_to_str

# Logging levels
DEBUG = logging.DEBUG
INFO = logging.INFO
WARNING = logging.WARNING
ERROR = logging.ERROR
CRITICAL = logging.CRITICAL
SILENT = CRITICAL + 1

level_names = {
    logging.DEBUG: "DEBUG",
    logging.INFO: "INFO",
    logging.WARNING: "WARNING",
    logging.ERROR: "ERROR",
    logging.CRITICAL: "CRITICAL",
    SILENT: "SILENT",
}

started = False

class ScrapyFileLogObserver(log.FileLogObserver):

    def __init__(self, f, level=INFO, encoding='utf-8'):
        self.level = level
        self.encoding = encoding
        log.FileLogObserver.__init__(self, f)

    def emit(self, eventDict):
        ev = _adapt_eventdict(eventDict, self.level, self.encoding)
        if ev is not None:
            log.FileLogObserver.emit(self, ev)

def _adapt_eventdict(eventDict, log_level=INFO, encoding='utf-8'):
    """Adapt Twisted log eventDict making it suitable for logging with a Scrapy
    log observer. It may return None to indicate that the event should be
    ignored by a Scrapy log observer.

    `log_level` is the minimum level being logged, and `encoding` is the log
    encoding.
    """
    ev = eventDict.copy()
    if ev['isError']:
        ev.setdefault('logLevel', ERROR)
    # ignore non-error messages from outside scrapy
    if ev.get('system') != 'scrapy' and not ev['isError']:
        return
    level = ev.get('logLevel')
    if level < log_level:
        return
    spider = ev.get('spider')
    if spider:
        ev['system'] = spider.name
    message = ev.get('message')
    lvlname = level_names.get(level, 'NOLEVEL')
    if message:
        message = [unicode_to_str(x, encoding) for x in message]
        message[0] = "%s: %s" % (lvlname, message[0])
    ev['message'] = message
    why = ev.get('why')
    if why:
        why = "%s: %s" % (lvlname, unicode_to_str(why, encoding))
    ev['why'] = why
    return ev

def _get_log_level(level_name_or_id=None):
    if level_name_or_id is None:
        lvlname = settings['LOG_LEVEL']
        return globals()[lvlname]
    elif isinstance(level_name_or_id, int):
        return level_name_or_id
    elif isinstance(level_name_or_id, basestring):
        return globals()[level_name_or_id]
    else:
        raise ValueError("Unknown log level: %r" % level_name_or_id)

def start(logfile=None, loglevel=None, logstdout=None):
    global started
    if started or not settings.getbool('LOG_ENABLED'):
        return
    started = True

    if log.defaultObserver: # check twisted log not already started
        loglevel = _get_log_level(loglevel)
        logfile = logfile or settings['LOG_FILE']
        file = open(logfile, 'a') if logfile else sys.stderr
        if logstdout is None:
            logstdout = settings.getbool('LOG_STDOUT')
        sflo = ScrapyFileLogObserver(file, loglevel, settings['LOG_ENCODING'])
        log.startLoggingWithObserver(sflo.emit, setStdout=logstdout)
        msg("Started project: %s" % settings['BOT_NAME'])

def msg(message, level=INFO, **kw):
    if 'component' in kw:
        import warnings
        warnings.warn("Argument `component` of scrapy.log.msg() is deprecated", \
            DeprecationWarning, stacklevel=2)
    kw.setdefault('system', 'scrapy')
    kw['logLevel'] = level
    log.msg(message, **kw)

def err(_stuff=None, _why=None, **kw):
    kw.setdefault('system', 'scrapy')
    kw['logLevel'] = kw.pop('level', ERROR)
    log.err(_stuff, _why, **kw)
<|endoftext|>


<commit_before>#
# Jasy - Web Tooling Framework
# Copyright 2013 Sebastian Werner
#

import itertools

def assembleDot(node, result=None):
    """
    Joins a dot node (cascaded supported, too) into a single string like "foo.bar.Baz"
    """

    if result == None:
        result = []

    for child in node:
        if child.type == "identifier":
            result.append(child.value)
        elif child.type == "dot":
            assembleDot(child, result)
        else:
            return None

    return ".".join(result)

def combineSelector(node):
    """
    Figures out the fully qualified selector of the given Node
    """

    selector = []

    while node:
        if node.type == "selector":
            selector.append(node.name)
        elif node.type == "mixin":
            selector.append(node.selector)

        node = getattr(node, "parent", None)

    return [" ".join(item) for item in itertools.product(*reversed(selector))]

<commit_msg>Re-build selector join part of combineSelector to correctly deal with '&' ampersand placeholders in selectors<commit_after>#
# Jasy - Web Tooling Framework
# Copyright 2013 Sebastian Werner
#

import itertools

def assembleDot(node, result=None):
    """
    Joins a dot node (cascaded supported, too) into a single string like "foo.bar.Baz"
    """

    if result == None:
        result = []

    for child in node:
        if child.type == "identifier":
            result.append(child.value)
        elif child.type == "dot":
            assembleDot(child, result)
        else:
            return None

    return ".".join(result)

def combineSelector(node):
    """
    Figures out the fully qualified selector of the given Node
    """

    selector = []

    current = node
    while current:
        if current.type == "selector":
            selector.append(current.name)
        elif current.type == "mixin":
            selector.append(current.selector)

        current = getattr(current, "parent", None)

    result = []
    for item in itertools.product(*reversed(selector)):
        combined = ""
        for part in item:
            if combined:
                if "&" in part:
                    combined = part.replace("&", combined)
                else:
                    combined = "%s %s" % (combined, part)
            else:
                if "&" in part:
                    raise Exception("Can't merge selector %s - parent missing - at line %s!" % (part, node.line))
                else:
                    combined = part

        result.append(combined)

    return result

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from flask import flash, redirect, render_template, request, g, url_for, jsonify, make_response
from sqlalchemy.exc import IntegrityError
from baseframe import _
from baseframe import forms
from baseframe.forms import render_form
from coaster.views import load_models
from .. import app, lastuser
from ..models import (db, Profile, ProposalSpace, Attendee, ProposalSpaceRedirect, Participant, Event, ContactExchange)
from ..forms import ParticipantForm
from funnel.util import split_name, format_twitter_handle, make_qrcode

def participant_badge_data(participants, space):
    badges = []
    for participant in participants:
        first_name, last_name = split_name(participant.fullname)
        badges.append({
            'first_name': first_name,
            'last_name': last_name,
            'twitter': format_twitter_handle(participant.twitter),
            'company': participant.company,
            'qrcode_content': make_qrcode(u"{puk}{key}".format(puk=participant.puk, key=participant.key))
        })
    return badges

def participant_data(participant, space_id, full=False):
    if full:
        return {
            '_id': participant.id,
            'puk': participant.puk,
            'fullname': participant.fullname,
            'job_title': participant.job_title,
            'company': participant.company,
            'email': participant.email,
            'twitter': participant.twitter,
            'phone': participant.phone,
            'space_id': space_id
        }
    else:
        return {
            '_id': participant.id,
            'puk': participant.puk,
            'fullname': participant.fullname,
            'job_title': participant.job_title,
            'company': participant.company,
            'space_id': space_id
        }

def participant_checkin_data(participant, space, event):
    return {
        'pid': participant.id,
        'fullname': participant.fullname,
        'company': participant.company,
        'email': participant.email,
        'badge_printed': participant.badge_printed,
        'checked_in': participant.checked_in,
        'ticket_type_titles': participant.ticket_type_titles
    }

@app.route('/<space>/participants/json', subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    permission='view')
def participants_json(profile, space):
    return jsonify(participants=[participant_data(participant, space.id) for participant in space.participants])

@app.route('/<space>/participants/new', methods=['GET', 'POST'], subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    permission='new-participant')
def new_participant(profile, space):
    form = ParticipantForm()
    form.events.query = space.events
    if form.validate_on_submit():
        participant = Participant(proposal_space=space)
        form.populate_obj(participant)
        try:
            db.session.add(participant)
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            flash(_(u"This participant already exists."), 'info')
        return redirect(space.url_for('admin'), code=303)
    return render_form(form=form, title=_(u"New Participant"), submit=_(u"Add Participant"))

@app.route('/<space>/participant/<participant_id>/edit', methods=['GET', 'POST'], subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    (Participant, {'id': 'participant_id'}, 'participant'),
    permission='edit-participant')
def participant_edit(profile, space, participant):
    form = ParticipantForm(obj=participant, model=Participant)
    form.events.query = space.events
    if form.validate_on_submit():
        form.populate_obj(participant)
        db.session.commit()
        flash(_(u"Your changes have been saved"), 'info')
        return redirect(space.url_for('admin'), code=303)
    return render_form(form=form, title=_(u"Edit Participant"), submit=_(u"Save changes"))

@app.route('/<space>/participant', subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    permission='view')
def participant(profile, space):
    participant = Participant.query.filter_by(puk=request.args.get('puk')).first()
    if not participant:
        return jsonify(message=u"Not found", code=404)
    elif participant.key == request.args.get('key'):
        try:
            contact_exchange = ContactExchange(user_id=g.user.id, participant_id=participant.id, proposal_space_id=space.id)
            db.session.add(contact_exchange)
            db.session.commit()
        except IntegrityError:
            app.logger.warning(u"Contact Exchange already present")
            db.session.rollback()
        return jsonify(participant=participant_data(participant, space.id, full=True))
    else:
        return jsonify(message=u"Unauthorized", code=401)

@app.route('/<space>/participant/<participant_id>/badge', subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    (Participant, {'id': 'participant_id'}, 'participant'),
    permission='view-participant')
def participant_badge(profile, space, participant):
    badge_type = request.args.getlist('type')
    badge_template = 'https://images.hasgeek.com/embed/file/02f9b190577a44f48b8816af1dc4928c'
    if badge_type:
        if 'blank' in badge_type:
            return render_template('blank_badge.html', badge_template=badge_template, badges=participant_badge_data([participant], space))
        if 'nofrill' in badge_type:
            badge_template = 'https://images.hasgeek.com/embed/file/02f9b190577a44f48b8816af1dc4928c'
        if 'premium' in badge_type:
            badge_template = 'https://images.hasgeek.com/embed/file/02f9b190577a44f48b8816af1dc4928c'
    return render_template('badge.html', badge_template=badge_template, badges=participant_badge_data([participant], space))

@app.route('/<space>/event/<name>/checkin', methods=['POST'], subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    (Event, {'name': 'name', 'proposal_space': 'space'}, 'event'),
    permission='checkin-event')
def event_checkin(profile, space, event):
    form = forms.Form()
    if form.validate_on_submit():
        checked_in = True if request.form.get('checkin') == 't' else False
        participant_ids = request.form.getlist('pid')
        for participant_id in participant_ids:
            participant = Participant.query.filter_by(id=participant_id).first()
            attendee = Attendee.get(event, participant)
            attendee.checked_in = checked_in
        db.session.commit()
        if request.is_xhr:
            return jsonify(status=True, participant_ids=participant_ids, checked_in=checked_in)
    return redirect(url_for('event', profile=space.profile.name, space=space.name, name=event.name), code=303)

@app.route('/<space>/event/<name>/checkin/api', methods=['POST'], subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    (Event, {'name': 'name', 'proposal_space': 'space'}, 'event'),
    permission='checkin-event')
def checkin_puk(profile, space, event):
    checked_in = True if request.form.get('checkin') == 't' else False
    participant_puks = request.form.getlist('puk')
    attendees = []
    for participant_puk in participant_puks:
        participant = Participant.query.filter_by(puk=participant_puk).first()
        attendee = Attendee.get(event, participant)
        if not attendee:
            return make_response(jsonify(error='not_found', error_description="Attendee not found"), 404)
        attendee.checked_in = checked_in
        attendees.append({
            'puk': participant_puk,
            'fullname': participant.fullname,
            'checked_in': attendee.checked_in
            })
    db.session.commit()
    return jsonify(result=attendees)

@app.route('/<space>/event/<name>/participants/json', subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    (Event, {'name': 'name', 'proposal_space': 'space'}, 'event'),
    permission='checkin-event')
def event_participants_json(profile, space, event):
    checkin_count = 0
    participants = []
    for participant in Participant.checkin_list(event):
        participants.append(participant_checkin_data(participant, space, event))
        if participant.checked_in:
            checkin_count += 1

    return jsonify(participants=participants, total_participants=len(participants), total_checkedin=checkin_count)

@app.route('/<space>/event/<name>/badges', subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    (Event, {'name': 'name', 'proposal_space': 'space'}, 'event'),
    permission='view-event')
def event_badges(profile, space, event):
    badge_printed = True if request.args.get('badge_printed') == 't' else False
    participants = Participant.query.join(Attendee).filter(Attendee.event_id == event.id).filter(Participant.badge_printed == badge_printed).all()
    badge_type = request.args.getlist('type')
    badge_template = 'https://images.hasgeek.com/embed/file/02f9b190577a44f48b8816af1dc4928c'
    if badge_type:
        if 'blank' in badge_type:
            return render_template('blank_badge.html', badge_template=badge_template, badges=participant_badge_data(participants, space))
        if 'nofrill' in badge_type:
            badge_template = 'https://images.hasgeek.com/embed/file/02f9b190577a44f48b8816af1dc4928c'
        if 'premium' in badge_type:
            badge_template = 'https://images.hasgeek.com/embed/file/02f9b190577a44f48b8816af1dc4928c'
    return render_template('badge.html', badge_template=badge_template, badges=participant_badge_data(participants, space))
<commit_msg>Add rootconf2017 badge template.<commit_after># -*- coding: utf-8 -*-
from flask import flash, redirect, render_template, request, g, url_for, jsonify, make_response
from sqlalchemy.exc import IntegrityError
from baseframe import _
from baseframe import forms
from baseframe.forms import render_form
from coaster.views import load_models
from .. import app, lastuser
from ..models import (db, Profile, ProposalSpace, Attendee, ProposalSpaceRedirect, Participant, Event, ContactExchange)
from ..forms import ParticipantForm
from funnel.util import split_name, format_twitter_handle, make_qrcode

def participant_badge_data(participants, space):
    badges = []
    for participant in participants:
        first_name, last_name = split_name(participant.fullname)
        badges.append({
            'first_name': first_name,
            'last_name': last_name,
            'twitter': format_twitter_handle(participant.twitter),
            'company': participant.company,
            'qrcode_content': make_qrcode(u"{puk}{key}".format(puk=participant.puk, key=participant.key))
        })
    return badges

def participant_data(participant, space_id, full=False):
    if full:
        return {
            '_id': participant.id,
            'puk': participant.puk,
            'fullname': participant.fullname,
            'job_title': participant.job_title,
            'company': participant.company,
            'email': participant.email,
            'twitter': participant.twitter,
            'phone': participant.phone,
            'space_id': space_id
        }
    else:
        return {
            '_id': participant.id,
            'puk': participant.puk,
            'fullname': participant.fullname,
            'job_title': participant.job_title,
            'company': participant.company,
            'space_id': space_id
        }

def participant_checkin_data(participant, space, event):
    return {
        'pid': participant.id,
        'fullname': participant.fullname,
        'company': participant.company,
        'email': participant.email,
        'badge_printed': participant.badge_printed,
        'checked_in': participant.checked_in,
        'ticket_type_titles': participant.ticket_type_titles
    }

@app.route('/<space>/participants/json', subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    permission='view')
def participants_json(profile, space):
    return jsonify(participants=[participant_data(participant, space.id) for participant in space.participants])

@app.route('/<space>/participants/new', methods=['GET', 'POST'], subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    permission='new-participant')
def new_participant(profile, space):
    form = ParticipantForm()
    form.events.query = space.events
    if form.validate_on_submit():
        participant = Participant(proposal_space=space)
        form.populate_obj(participant)
        try:
            db.session.add(participant)
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            flash(_(u"This participant already exists."), 'info')
        return redirect(space.url_for('admin'), code=303)
    return render_form(form=form, title=_(u"New Participant"), submit=_(u"Add Participant"))

@app.route('/<space>/participant/<participant_id>/edit', methods=['GET', 'POST'], subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    (Participant, {'id': 'participant_id'}, 'participant'),
    permission='edit-participant')
def participant_edit(profile, space, participant):
    form = ParticipantForm(obj=participant, model=Participant)
    form.events.query = space.events
    if form.validate_on_submit():
        form.populate_obj(participant)
        db.session.commit()
        flash(_(u"Your changes have been saved"), 'info')
        return redirect(space.url_for('admin'), code=303)
    return render_form(form=form, title=_(u"Edit Participant"), submit=_(u"Save changes"))

@app.route('/<space>/participant', subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    permission='view')
def participant(profile, space):
    participant = Participant.query.filter_by(puk=request.args.get('puk')).first()
    if not participant:
        return jsonify(message=u"Not found", code=404)
    elif participant.key == request.args.get('key'):
        try:
            contact_exchange = ContactExchange(user_id=g.user.id, participant_id=participant.id, proposal_space_id=space.id)
            db.session.add(contact_exchange)
            db.session.commit()
        except IntegrityError:
            app.logger.warning(u"Contact Exchange already present")
            db.session.rollback()
        return jsonify(participant=participant_data(participant, space.id, full=True))
    else:
        return jsonify(message=u"Unauthorized", code=401)

@app.route('/<space>/participant/<participant_id>/badge', subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    (Participant, {'id': 'participant_id'}, 'participant'),
    permission='view-participant')
def participant_badge(profile, space, participant):
    badge_type = request.args.getlist('type')
    badge_template = 'https://images.hasgeek.com/embed/file/255da4692aec4ce7b17e258d0c94336a'
    if badge_type:
        if 'blank' in badge_type:
            badge_template = 'https://images.hasgeek.com/embed/file/4b8c0b4abe8142978cc11b02494f540f'
            return render_template('blank_badge.html', badge_template=badge_template, badges=participant_badge_data([participant], space))
        if 'nofrill' in badge_type:
            badge_template = 'https://images.hasgeek.com/embed/file/02f9b190577a44f48b8816af1dc4928c'
        if 'premium' in badge_type:
            badge_template = 'https://images.hasgeek.com/embed/file/02f9b190577a44f48b8816af1dc4928c'
    return render_template('badge.html', badge_template=badge_template, badges=participant_badge_data([participant], space))

@app.route('/<space>/event/<name>/checkin', methods=['POST'], subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    (Event, {'name': 'name', 'proposal_space': 'space'}, 'event'),
    permission='checkin-event')
def event_checkin(profile, space, event):
    form = forms.Form()
    if form.validate_on_submit():
        checked_in = True if request.form.get('checkin') == 't' else False
        participant_ids = request.form.getlist('pid')
        for participant_id in participant_ids:
            participant = Participant.query.filter_by(id=participant_id).first()
            attendee = Attendee.get(event, participant)
            attendee.checked_in = checked_in
        db.session.commit()
        if request.is_xhr:
            return jsonify(status=True, participant_ids=participant_ids, checked_in=checked_in)
    return redirect(url_for('event', profile=space.profile.name, space=space.name, name=event.name), code=303)

@app.route('/<space>/event/<name>/checkin/api', methods=['POST'], subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    (Event, {'name': 'name', 'proposal_space': 'space'}, 'event'),
    permission='checkin-event')
def checkin_puk(profile, space, event):
    checked_in = True if request.form.get('checkin') == 't' else False
    participant_puks = request.form.getlist('puk')
    attendees = []
    for participant_puk in participant_puks:
        participant = Participant.query.filter_by(puk=participant_puk).first()
        attendee = Attendee.get(event, participant)
        if not attendee:
            return make_response(jsonify(error='not_found', error_description="Attendee not found"), 404)
        attendee.checked_in = checked_in
        attendees.append({
            'puk': participant_puk,
            'fullname': participant.fullname,
            'checked_in': attendee.checked_in
            })
    db.session.commit()
    return jsonify(result=attendees)

@app.route('/<space>/event/<name>/participants/json', subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    (Event, {'name': 'name', 'proposal_space': 'space'}, 'event'),
    permission='checkin-event')
def event_participants_json(profile, space, event):
    checkin_count = 0
    participants = []
    for participant in Participant.checkin_list(event):
        participants.append(participant_checkin_data(participant, space, event))
        if participant.checked_in:
            checkin_count += 1

    return jsonify(participants=participants, total_participants=len(participants), total_checkedin=checkin_count)

@app.route('/<space>/event/<name>/badges', subdomain='<profile>')
@lastuser.requires_login
@load_models(
    (Profile, {'name': 'profile'}, 'g.profile'),
    ((ProposalSpace, ProposalSpaceRedirect), {'name': 'space', 'profile': 'profile'}, 'space'),
    (Event, {'name': 'name', 'proposal_space': 'space'}, 'event'),
    permission='view-event')
def event_badges(profile, space, event):
    badge_printed = True if request.args.get('badge_printed') == 't' else False
    participants = Participant.query.join(Attendee).filter(Attendee.event_id == event.id).filter(Participant.badge_printed == badge_printed).all()
    badge_type = request.args.getlist('type')
    badge_template = 'https://images.hasgeek.com/embed/file/255da4692aec4ce7b17e258d0c94336a'
    if badge_type:
        if 'blank' in badge_type:
            badge_template = 'https://images.hasgeek.com/embed/file/4b8c0b4abe8142978cc11b02494f540f'
            return render_template('blank_badge.html', badge_template=badge_template, badges=participant_badge_data(participants, space))
        if 'nofrill' in badge_type:
            badge_template = 'https://images.hasgeek.com/embed/file/02f9b190577a44f48b8816af1dc4928c'
        if 'premium' in badge_type:
            badge_template = 'https://images.hasgeek.com/embed/file/02f9b190577a44f48b8816af1dc4928c'
    return render_template('badge.html', badge_template=badge_template, badges=participant_badge_data(participants, space))
<|endoftext|>


<commit_before>#!/usr/bin/env python
#-- Content-Encoding: UTF-8 --
"""
PSEM2M Compiler: Configuration utilities

:author: Thomas Calmant
"""

import ConfigParser

# ------------------------------------------------------------------------------

class ExtSafeConfigParser(ConfigParser.SafeConfigParser):
    """
    SafeConfigParser with utility methods
    """
    def get_default(self, section, option, default=None):
        """
        Retrieves the found configuration value, or the default one

        :param section: Configuration section
        :param option: Configuration option
        :param default: Default value
        :return: The option value or *default*
        """
        try:
            return self.get(section, option)

        except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
            # Not found
            return default

    def get_list(self, section, option, default=[]):
        """
        Retrieves a list from the configuration

        :param section: Configuration section
        :param option: Configuration option
        :param default: Default value
        :return: The parsed list or *default*
        """
        values = self.get_default(section, option)
        if not values:
            # Option not found
            return default

        # Split values
        values = values.split(',')

        # Return the list with stripped entries
        return [value.strip() for value in values if value.strip()]
<commit_msg>Ajout de méthodes utilitaires à compiler.config<commit_after>#!/usr/bin/env python
#-- Content-Encoding: UTF-8 --
"""
PSEM2M Compiler: Configuration utilities

:author: Thomas Calmant
"""

import ConfigParser
import os

# ------------------------------------------------------------------------------

def expand_path(path):
    """
    Expands the given path

    :param path: A path
    :return: The expanded path
    """
    return os.path.abspath(os.path.expanduser(os.path.expandvars(path)))

# ------------------------------------------------------------------------------

class ExtSafeConfigParser(ConfigParser.SafeConfigParser):
    """
    SafeConfigParser with utility methods
    """
    def get_default(self, section, option, default=None):
        """
        Retrieves the found configuration value, or the default one

        :param section: Configuration section
        :param option: Configuration option
        :param default: Default value
        :return: The option value or *default*
        """
        try:
            return self.get(section, option)

        except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
            # Not found
            return default

    def get_list(self, section, option, default=[]):
        """
        Retrieves a list from the configuration

        :param section: Configuration section
        :param option: Configuration option
        :param default: Default value
        :return: The parsed list or *default*
        """
        values = self.get_default(section, option)
        if not values:
            # Option not found
            return default

        # Split values
        values = values.split(',')

        # Return the list with stripped entries
        return [value.strip() for value in values if value.strip()]

    def get_path(self, section, option, default=None):
        """
        Retrieves a path from the configuration.

        Same as ``get_default()``, but expanding the path

        :param section: Configuration section
        :param option: Configuration option
        :param default: Default value
        :return: The expanded path or *default*
        """
        path = self.get_default(section, option, None)
        if not path:
            # Path not found
            return default

        # Expand the path
        return expand_path(path)

    def get_paths_list(self, section, option, default=[]):
        """
        Retrieves a list of paths from the configuration.

        Same as ``get_list()``, but expanding the paths

        :param section: Configuration section
        :param option: Configuration option
        :param default: Default value
        :return: The expanded paths list or *default*
        """
        values = self.get_list(section, option, None)
        if not values:
            # No values found, return the default argument
            return default

        paths = []
        for path in values:
            # Expand the trimmed path
            path = expand_path(path.strip())
            if path:
                # Only keep valid paths
                paths.append(path)

        return paths
<|endoftext|>


<commit_before>#!/usr/bin/env python3

"""

Pipelines like GALES produce GFF3 models for protein-coding genes, then secondary
files for non-coding features like tRNAs and rRNAs.

This script combines these and does a bit of correction where the tools don't
precisely produce spec-GFF3.

Current inputs:

models: Any spec-compliant GFF3 describing the protein-coding genes (or more).  These
        features are just retained and passed forward.

barrnap: Optional. rRNA prediction 'gff' file from Barrnap

ARAGORN: Optional. tRNA, mtRNA, and tmRNA prediction.  Not actually GFF3 but convenient
         to include here.  Assumes you ARAGORN has been run with the -w option

"""

import argparse
import os
import re
import uuid

#import sys
#sys.path.insert(0, '/home/jorvis/git/biocode/lib')

from biocode import annotation, gff, things, utils

def main():
    parser = argparse.ArgumentParser( description='Put a description of your script here')

    ## output file to be written
    parser.add_argument('-m', '--model_gff', type=str, required=True, help='Input (pass-through) GFF file' )
    parser.add_argument('-o', '--output_gff', type=str, required=False, help='Output file to be written.  Default=STDOUT' )
    parser.add_argument('-b', '--barrnap_gff', type=str, required=False, help='GFF file from Barrnap prediction' )
    parser.add_argument('-a', '--aragorn_out', type=str, required=False, help='Raw output file (with -w) from ARAGORN prediction' )
    args = parser.parse_args()

    (assemblies, features) = gff.get_gff3_features(args.model_gff)

    if args.barrnap_gff:
        add_barrnap_features(assemblies, features, args.barrnap_gff)

    if args.aragorn_out:
        add_aragorn_features(assemblies, features, args.aragorn_out)

    utils.serialize_gff3(path=args.output_gff, assemblies=assemblies, features=features)

def add_aragorn_features(assemblies, features, aragorn_file):
    current_assembly_id = None

    for line in open(aragorn_file):
        line = line.rstrip()

        if line.startswith('>'):
            current_assembly_id = line[1:]
        else:
            cols = line.split()

            if len(cols) == 5:
                if cols[1].startswith('tRNA'):
                    feat_type = 'tRNA'
                elif cols[1].startswith('tmRNA'):
                    feat_type = 'tmRNA'
                elif cols[1].startswith('mtRNA'):
                    feat_type = 'mtRNA'
                else:
                    raise Exception("Unexpected type in ARAGORN, column value: {0}".format(cols[1]))

                feat_base = "{0}_{1}".format(feat_type, uuid.uuid4())
                gene_id = "{0}_gene".format(feat_base)
                RNA_id = "{0}_{1}".format(feat_base, feat_type)

                m = re.match('(c*)\[(\d+),(\d+)\]', cols[2])
                if m:
                    rfmin = int(m.group(2)) - 1
                    rfmax = int(m.group(3))

                    if m.group(1):
                        rstrand = -1
                    else:
                        rstrand = 1
                else:
                    raise Exception("ERROR: unexpected coordinate format: {0}".format(cols[2]))

                current_assembly = assemblies[current_assembly_id]
                gene = things.Gene(id=gene_id)
                gene.locate_on(target=current_assembly, fmin=rfmin, fmax=rfmax, strand=rstrand)
                features[gene_id] = gene
                current_assembly.add_gene(gene)

                if feat_type == 'tRNA':
                    RNA = things.tRNA(id=RNA_id, parent=gene, anticodon=cols[4][1:4].upper())
                    gene.add_tRNA(RNA)
                else:
                    RNA = things.tmRNA(id=RNA_id, parent=gene)
                    gene.add_tmRNA(RNA)

                RNA.locate_on(target=current_assembly, fmin=rfmin, fmax=rfmax, strand=rstrand)
                RNA.annotation = annotation.FunctionalAnnotation(product_name=cols[1])
                features[RNA_id] = RNA

def add_barrnap_features(assemblies, features, barrnap_gff):
    for line in open(barrnap_gff):
        if line.startswith('#'):
            continue

        cols = line.split("\t")

        if len(cols) == 9:
            if cols[0] in assemblies:
                current_assembly = assemblies[cols[0]]
            else:
                raise Exception("ERROR: encountered assembly ID ({0}) in Barrnap file that was not found in the model GFF3 file".format(cols[0]))

            if cols[2] == 'rRNA':
                atts = gff.column_9_dict(cols[8])
                feat_base = "rRNA_{0}".format(uuid.uuid4())
                gene_id = "{0}_gene".format(feat_base)
                rRNA_id = "{0}_rRNA".format(feat_base)

                rfmin = int(cols[3]) - 1
                rfmax = int(cols[4])

                if cols[6] == '-':
                    rstrand = -1
                elif cols[6] == '+':
                    rstrand = 1
                else:
                    rstrand = 0

                gene = things.Gene(id=gene_id)
                gene.locate_on(target=current_assembly, fmin=rfmin, fmax=rfmax, strand=rstrand)
                features[gene_id] = gene
                current_assembly.add_gene(gene)

                rRNA = things.rRNA(id=rRNA_id, parent=gene)
                rRNA.locate_on(target=current_assembly, fmin=rfmin, fmax=rfmax, strand=rstrand)
                gene.add_rRNA(rRNA)
                rRNA.annotation = gff.parse_annotation_from_column_9(cols[8])
                features[rRNA_id] = rRNA

if __name__ == '__main__':
    main()

<commit_msg>Fixed bug which caused tool to merge if FASTA headers had more than just an ID<commit_after>#!/usr/bin/env python3

"""

Pipelines like GALES produce GFF3 models for protein-coding genes, then secondary
files for non-coding features like tRNAs and rRNAs.

This script combines these and does a bit of correction where the tools don't
precisely produce spec-GFF3.

Current inputs:

models: Any spec-compliant GFF3 describing the protein-coding genes (or more).  These
        features are just retained and passed forward.

barrnap: Optional. rRNA prediction 'gff' file from Barrnap

ARAGORN: Optional. tRNA, mtRNA, and tmRNA prediction.  Not actually GFF3 but convenient
         to include here.  Assumes you ARAGORN has been run with the -w option

"""

import argparse
import os
import re
import uuid

#import sys
#sys.path.insert(0, '/home/jorvis/git/biocode/lib')

from biocode import annotation, gff, things, utils

def main():
    parser = argparse.ArgumentParser( description='Put a description of your script here')

    ## output file to be written
    parser.add_argument('-m', '--model_gff', type=str, required=True, help='Input (pass-through) GFF file' )
    parser.add_argument('-o', '--output_gff', type=str, required=False, help='Output file to be written.  Default=STDOUT' )
    parser.add_argument('-b', '--barrnap_gff', type=str, required=False, help='GFF file from Barrnap prediction' )
    parser.add_argument('-a', '--aragorn_out', type=str, required=False, help='Raw output file (with -w) from ARAGORN prediction' )
    args = parser.parse_args()

    (assemblies, features) = gff.get_gff3_features(args.model_gff)

    if args.barrnap_gff:
        add_barrnap_features(assemblies, features, args.barrnap_gff)

    if args.aragorn_out:
        add_aragorn_features(assemblies, features, args.aragorn_out)

    utils.serialize_gff3(path=args.output_gff, assemblies=assemblies, features=features)

def add_aragorn_features(assemblies, features, aragorn_file):
    current_assembly_id = None

    for line in open(aragorn_file):
        line = line.rstrip()

        if line.startswith('>'):
            m = re.match('>(\S+)', line)
            current_assembly_id = m.group(1)
        else:
            cols = line.split()

            if len(cols) == 5:
                if cols[1].startswith('tRNA'):
                    feat_type = 'tRNA'
                elif cols[1].startswith('tmRNA'):
                    feat_type = 'tmRNA'
                elif cols[1].startswith('mtRNA'):
                    feat_type = 'mtRNA'
                else:
                    raise Exception("Unexpected type in ARAGORN, column value: {0}".format(cols[1]))

                feat_base = "{0}_{1}".format(feat_type, uuid.uuid4())
                gene_id = "{0}_gene".format(feat_base)
                RNA_id = "{0}_{1}".format(feat_base, feat_type)

                m = re.match('(c*)\[(\d+),(\d+)\]', cols[2])
                if m:
                    rfmin = int(m.group(2)) - 1
                    rfmax = int(m.group(3))

                    if m.group(1):
                        rstrand = -1
                    else:
                        rstrand = 1
                else:
                    raise Exception("ERROR: unexpected coordinate format: {0}".format(cols[2]))

                current_assembly = assemblies[current_assembly_id]
                gene = things.Gene(id=gene_id)
                gene.locate_on(target=current_assembly, fmin=rfmin, fmax=rfmax, strand=rstrand)
                features[gene_id] = gene
                current_assembly.add_gene(gene)

                if feat_type == 'tRNA':
                    RNA = things.tRNA(id=RNA_id, parent=gene, anticodon=cols[4][1:4].upper())
                    gene.add_tRNA(RNA)
                else:
                    RNA = things.tmRNA(id=RNA_id, parent=gene)
                    gene.add_tmRNA(RNA)

                RNA.locate_on(target=current_assembly, fmin=rfmin, fmax=rfmax, strand=rstrand)
                RNA.annotation = annotation.FunctionalAnnotation(product_name=cols[1])
                features[RNA_id] = RNA

def add_barrnap_features(assemblies, features, barrnap_gff):
    for line in open(barrnap_gff):
        if line.startswith('#'):
            continue

        cols = line.split("\t")

        if len(cols) == 9:
            if cols[0] in assemblies:
                current_assembly = assemblies[cols[0]]
            else:
                raise Exception("ERROR: encountered assembly ID ({0}) in Barrnap file that was not found in the model GFF3 file".format(cols[0]))

            if cols[2] == 'rRNA':
                atts = gff.column_9_dict(cols[8])
                feat_base = "rRNA_{0}".format(uuid.uuid4())
                gene_id = "{0}_gene".format(feat_base)
                rRNA_id = "{0}_rRNA".format(feat_base)

                rfmin = int(cols[3]) - 1
                rfmax = int(cols[4])

                if cols[6] == '-':
                    rstrand = -1
                elif cols[6] == '+':
                    rstrand = 1
                else:
                    rstrand = 0

                gene = things.Gene(id=gene_id)
                gene.locate_on(target=current_assembly, fmin=rfmin, fmax=rfmax, strand=rstrand)
                features[gene_id] = gene
                current_assembly.add_gene(gene)

                rRNA = things.rRNA(id=rRNA_id, parent=gene)
                rRNA.locate_on(target=current_assembly, fmin=rfmin, fmax=rfmax, strand=rstrand)
                gene.add_rRNA(rRNA)
                rRNA.annotation = gff.parse_annotation_from_column_9(cols[8])
                features[rRNA_id] = rRNA

if __name__ == '__main__':
    main()

<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#from __future__ import unicode_literals
"""Generate a model capable of detecting subject-verb agreement errors"""
print("loading libraries...")
import json
import numpy as np
import os
import requests
import textacy
import tensorflow as tf
from tflearn.data_utils import to_categorical
from test_sents import sentences as ts
import tflearn
from sva_rule_based import check_agreement

METHOD = 'ML_ONLY'
#METHOD = 'COMBINED'
#METHOD = 'RULE_BASED'

# Constants

VECTORIZE_API = os.environ.get('VECTORIZE_API_URI', 'http://localhost:10200')

# TODO: move this into a shared module -- it's shared accross build, eval, and
# others maybe.
def inflate(deflated_vector):
    """Given a defalated vector, inflate it into a np array and return it"""
    dv = json.loads(deflated_vector)
    #result = np.zeros(dv['reductions']) # some claim vector length 5555, others
    #5530. this could have occurred doing remote computations? or something.
    # anyhow, we will use 5555.  Let's just hard code it.  Gosh darnit.
    result = np.zeros(93540) # some claim vector length 5555, others
    for n in dv['indices']:
        result[int(n)] = dv['indices'][n]
    #print("Inflated vector. Length", len(result))
    return result[:93540]

def text_to_vector(sent_str):
    """Given a string, get it's defalted vector, inflate it, then return the
    inflated vector"""
    r = requests.get("{}/sva/vector".format(VECTORIZE_API), params={'s':sent_str})
    return inflate(r.text)

# Building TF Model #######################################################

print("Setting up tensorflow...")
vector_len = 93540 # TODO: this probably really should not be hardcoded
def build_model():
    # This resets all parameters and variables, leave this here
    tf.reset_default_graph()

    #### Your code ####
    net = tflearn.input_data([None, 93540])                          # Input
    net = tflearn.fully_connected(net, 200, activation='ReLU')      # Hidden
    net = tflearn.fully_connected(net, 25, activation='ReLU')      # Hidden
    net = tflearn.fully_connected(net, 2, activation='softmax')   # Output
    net = tflearn.regression(net, optimizer='sgd', learning_rate=0.1, loss='categorical_crossentropy')
    model = tflearn.DNN(net)

    return model

print("Building TF model...")
model = build_model()

# Loading TF Model #####################################################
print("Loading TF model...")
model.load('../../../models/james_subject_verb_agreement_model.tfl')

# Testing ##############################################################
print('Running tests against your model...')
# TODO: text_to_vector not currently included so tests can't run
def test_sentence(sentence, ans):
    """Returns true if correct"""
    correct = False

    # see number of verb phrases
    pattern = r'<VERB>?<ADV>*<VERB>+'
    doc = textacy.Doc(sentence, lang='en_core_web_lg')
    vps = textacy.extract.pos_regex_matches(doc, pattern)

    #if len([x for x in vps]) < 2:
    if (METHOD == 'COMBINED' and len([x for x in vps]) < 2) or METHOD == 'RULE_BASED':
        print("Simple sentence, using rule based checker")
        return ans != check_agreement(sentence)

    # Use ML on more complex sentences

    positive_prob = model.predict([text_to_vector(sentence)])[0][1]
    print('---{}---'.format(sentence))
    print('Does this sentence have a subject-verb agreement error?\n {}'.format(sentence))
    print('P(positive) = {:.3f} :'.format(positive_prob),
          'Yes' if positive_prob > 0.5 else 'No')
    correct = (positive_prob > 0.5) == ans
    print("Is correct?", correct)
    print('-------------------------------------------')
    return correct

while True:
    s = input('sent: ')
    positive_prob = model.predict([text_to_vector(s)])[0][1]
    print(positive_prob)
    if positive_prob > .5:
        print('Incorrect')
    else:
        print('Correct')

total_sents = len(ts)
total_right = 0.0
overly_strict = 0.0
overly_lenient = 0.0
too_strict_sents = []
for sent, ans in ts:
    correct =  test_sentence(sent, ans)
    if correct:
        total_right += 1

    if not correct and not ans:
        # There is not an error but one was marked
        overly_strict += 1
        too_strict_sents.append(sent)
        pass
    if not correct and ans:
        # There is an error but none was marked
        overly_lenient += 1
        pass

print("Too strict sents: ")
print("\n")
for too_s in too_strict_sents:
    print(" > " + too_s)

print("Success rate was {}%".format(total_right/total_sents * 100))
print("\n")
print("Error rate {}%".format(100 - total_right/total_sents * 100))
print("Too strict {}%".format(overly_strict/total_sents * 100))
print("Too lenient {}%".format(overly_lenient/total_sents * 100))

print('done. 🎉')
<commit_msg>eval changes<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#from __future__ import unicode_literals
"""Generate a model capable of detecting subject-verb agreement errors"""
print("loading libraries...")
import json
import numpy as np
import os
import requests
import tensorflow as tf
from tflearn.data_utils import to_categorical
import tflearn

METHOD = 'ML_ONLY'
#METHOD = 'COMBINED'
#METHOD = 'RULE_BASED'

# Constants

VECTORIZE_API = os.environ.get('VECTORIZE_API_URI', 'http://localhost:10200')

# TODO: move this into a shared module -- it's shared accross build, eval, and
# others maybe.
def inflate(deflated_vector):
    """Given a defalated vector, inflate it into a np array and return it"""
    dv = json.loads(deflated_vector)
    #result = np.zeros(dv['reductions']) # some claim vector length 5555, others
    #5530. this could have occurred doing remote computations? or something.
    # anyhow, we will use 5555.  Let's just hard code it.  Gosh darnit.
    result = np.zeros(93540) # some claim vector length 5555, others
    for n in dv['indices']:
        result[int(n)] = dv['indices'][n]
    #print("Inflated vector. Length", len(result))
    return result[:93540]

def text_to_vector(sent_str):
    """Given a string, get it's defalted vector, inflate it, then return the
    inflated vector"""
    r = requests.get("{}/sva/vector".format(VECTORIZE_API), params={'s':sent_str})
    return inflate(r.text)

# Building TF Model #######################################################

print("Setting up tensorflow...")
vector_len = 93540 # TODO: this probably really should not be hardcoded
def build_model():
    # This resets all parameters and variables, leave this here
    tf.reset_default_graph()

    #### Your code ####
    net = tflearn.input_data([None, 93540])                          # Input
    net = tflearn.fully_connected(net, 200, activation='ReLU')      # Hidden
    net = tflearn.fully_connected(net, 25, activation='ReLU')      # Hidden
    net = tflearn.fully_connected(net, 2, activation='softmax')   # Output
    net = tflearn.regression(net, optimizer='sgd', learning_rate=0.1, loss='categorical_crossentropy')
    model = tflearn.DNN(net)

    return model

print("Building TF model...")
model = build_model()

# Loading TF Model #####################################################
print("Loading TF model...")
model.load('../../../models/james_sva/james_subject_verb_agreement_model.tfl')

# Testing ##############################################################
print('Running tests against your model...')
# TODO: text_to_vector not currently included so tests can't run
def test_sentence(sentence, ans):
    """Returns true if correct"""
    correct = False

    # see number of verb phrases
    pattern = r'<VERB>?<ADV>*<VERB>+'
    doc = textacy.Doc(sentence, lang='en_core_web_lg')
    vps = textacy.extract.pos_regex_matches(doc, pattern)

    #if len([x for x in vps]) < 2:
    if (METHOD == 'COMBINED' and len([x for x in vps]) < 2) or METHOD == 'RULE_BASED':
        print("Simple sentence, using rule based checker")
        return ans != check_agreement(sentence)

    # Use ML on more complex sentences

    positive_prob = model.predict([text_to_vector(sentence)])[0][1]
    print('---{}---'.format(sentence))
    print('Does this sentence have a subject-verb agreement error?\n {}'.format(sentence))
    print('P(positive) = {:.3f} :'.format(positive_prob),
          'Yes' if positive_prob > 0.5 else 'No')
    correct = (positive_prob > 0.5) == ans
    print("Is correct?", correct)
    print('-------------------------------------------')
    return correct

while True:
    s = input('sent: ')
    print('Calculating...')
    positive_prob = model.predict([text_to_vector(s)])[0][1]
    print(positive_prob)
    if positive_prob > .6:
        print('Incorrect')
    else:
        print('Correct')

<|endoftext|>


<commit_before>#!/usr/bin/env python
"""
Convert genome annotation data in GFF/GTF to a 12 column BED format.
BED format typically represents the transcript models.

Usage: python gff_to_bed_conv.py in.gff > out.bed
"""

import sys
import re

def stop_err(fmsg):

    sys.stderr.write('%s\n' % fmsg)
    sys.exit(-1)

def WriteBED(tinfo, einfo, bed_fh):
    """BED writter"""

    for contig_id, feature in tinfo.items():
        for tid, tloc in feature.items():
            if tid in einfo: # get corresponding exon info
                exon_cnt, exon_len, exon_cod, fex, rstart = 0, '', '', 0, None
                if tloc[-1] == '-':
                    if einfo[tid][0][1] > einfo[tid][-1][1]:einfo[tid].sort()
                for ex_ele in einfo[tid]:
                    if ex_ele[0] != contig_id:continue
                    exon_cnt += 1
                    exon_len += str(int(ex_ele[2])-int(ex_ele[1])+1) + ','
                    if fex == 0: # calculate the relative exon start
                        exon_cod += '0,'
                        fex = 1
                        rstart = int(ex_ele[1])
                    else:
                        exon_cod += str(int(ex_ele[1])-rstart) + ','
                if exon_len:
                    pline = [str(contig_id),
                            tloc[0],
                            tloc[1],
                            tid,
                            tloc[2],
                            tloc[-1],
                            tloc[0],
                            tloc[1],
                            '0',
                            str(exon_cnt),
                            exon_len,
                            exon_cod]
                    bed_fh.write('\t'.join(pline) + '\n')
    bed_fh.close()

def ParseAnno(gff_fh):
    """Reading GFF3 file to get feature annotation"""

    tinfo, einfo = dict(), dict()
    features = ['transcript', 'scRNA', 'mRNA', 'ncRNA', 'miRNA', 'rRNA', 'snoRNA', 'snRNA', 'tRNA', 'pseudogenic_transcript'] ## TODO: Growing list of RNA species.

    for gff_line in gff_fh:
        gff_line = gff_line.strip('\n\r').split('\t')
        if re.match(r'#', gff_line[0]) or re.match(r'>', gff_line[0]) or len(gff_line) == 1:continue ## not considering commented and FASTA  lines from GFF
        if '' in gff_line:continue ## empty fields in any line ?
        assert len(gff_line) == 9, '\t'.join(gff_line) ## a valid GFF line contains only 9 tab-delimited fields

        if gff_line[2] in features:
            tid = None
            for ele in gff_line[-1].split(';'):
                if re.search(r'ID=', ele):
                    tid = re.search(r'ID=(.+)', ele).group(1)
                    break
            if gff_line[0] in tinfo:
                tinfo[gff_line[0]][tid] = (gff_line[3], gff_line[4], gff_line[5], gff_line[6])
            else:
                tinfo[gff_line[0]] = {tid:(gff_line[3], gff_line[4], gff_line[5], gff_line[6])}
        elif gff_line[2] == 'exon':
            pid = None
            for ele in gff_line[-1].split(';'):
                if re.search(r'Parent=', ele):
                    pid = re.search(r'Parent=(.+)', ele).group(1)
                    break
            if pid in einfo:
                einfo[pid].append((gff_line[0], int(gff_line[3]), int(gff_line[4])))
            else:
                einfo[pid] = [(gff_line[0], int(gff_line[3]), int(gff_line[4]))]
    gff_fh.close()
    return tinfo, einfo

def __main__():

    cmd_arg = OptionParser()
    cmd_arg.add_option('', '-q', dest='query_file', help='Data in GFF3 file')
    cmd_arg.add_option('', '-o', dest='result_file', help='BED file name')

    options, args = cmd_arg.parse_args()
    if len(sys.argv) < 2:cmd_arg.print_help();sys.exit(-1)

    if options.query_file != None and options.result_file != None:
        try:
            gff_fh = open(options.query_file, 'rU')
        except Exception, erm:
            stop_err('Error reading query file ' + str(erm))

        tinfo, einfo = ParseAnno(gff_fh) ## get transcript annotation from GFF3 file
        try:
            bed_fh = open(options.result_file, 'w')
        except Exception, erm:
            stop_err('Error writing result file ' + str(erm))
        WriteBED(tinfo, einfo, bed_fh) ## BED writter

if __name__ == "__main__": __main__()
<commit_msg>checked the compactibility of GFFParser module<commit_after>#!/usr/bin/env python
"""
Convert genome annotation data in GFF/GTF to a 12 column BED format.
BED format typically represents the transcript models.

Usage: python gff_to_bed_conv.py in.gff > out.bed
"""

import re
import sys
import GFFParser

def WriteBED(tinfo):
    """
    writing result files in bed format
    """
    for ent1 in tinfo:
        for idx, tid in enumerate(ent1['transcripts']):
            #print tid[0]
            #print ent1['exons'][idx]

        break

#    for contig_id, feature in tinfo.items():
#        for tid, tloc in feature.items():
#            if tid in einfo: # get corresponding exon info
#                exon_cnt, exon_len, exon_cod, fex, rstart = 0, '', '', 0, None
#                if tloc[-1] == '-':
#                    if einfo[tid][0][1] > einfo[tid][-1][1]:einfo[tid].sort()
#                for ex_ele in einfo[tid]:
#                    if ex_ele[0] != contig_id:continue
#                    exon_cnt += 1
#                    exon_len += str(int(ex_ele[2])-int(ex_ele[1])+1) + ','
#                    if fex == 0: # calculate the relative exon start
#                        exon_cod += '0,'
#                        fex = 1
#                        rstart = int(ex_ele[1])
#                    else:
#                        exon_cod += str(int(ex_ele[1])-rstart) + ','
#                if exon_len:
#                    pline = [str(contig_id),
#                            tloc[0],
#                            tloc[1],
#                            tid,
#                            tloc[2],
#                            tloc[-1],
#                            tloc[0],
#                            tloc[1],
#                            '0',
#                            str(exon_cnt),
#                            exon_len,
#                            exon_cod]
#                    bed_fh.write('\t'.join(pline) + '\n')

def ParseAnno(gff_fh):
    """Reading GFF3 file to get feature annotation"""

    tinfo, einfo = dict(), dict()

    for gff_line in gff_fh:

        if gff_line[2] in features:
            tid = None
            for ele in gff_line[-1].split(';'):
                if re.search(r'ID=', ele):
                    tid = re.search(r'ID=(.+)', ele).group(1)
                    break
            if gff_line[0] in tinfo:
                tinfo[gff_line[0]][tid] = (gff_line[3], gff_line[4], gff_line[5], gff_line[6])
            else:
                tinfo[gff_line[0]] = {tid:(gff_line[3], gff_line[4], gff_line[5], gff_line[6])}
        elif gff_line[2] == 'exon':
            pid = None
            for ele in gff_line[-1].split(';'):
                if re.search(r'Parent=', ele):
                    pid = re.search(r'Parent=(.+)', ele).group(1)
                    break
            if pid in einfo:
                einfo[pid].append((gff_line[0], int(gff_line[3]), int(gff_line[4])))
            else:
                einfo[pid] = [(gff_line[0], int(gff_line[3]), int(gff_line[4]))]
    gff_fh.close()
    return tinfo, einfo

def __main__():

    try:
        query_file = sys.argv[1]
    except:
        print __doc__
        sys.exit(-1)

    Transcriptdb = GFFParser.Parse(query_file)

    WriteBED(Transcriptdb)

#    if options.query_file != None and options.result_file != None:
#        try:
#            gff_fh = open(options.query_file, 'rU')
#        except Exception, erm:
#            stop_err('Error reading query file ' + str(erm))
#
#        tinfo, einfo = ParseAnno(gff_fh) ## get transcript annotation from GFF3 file
#        try:
#            bed_fh = open(options.result_file, 'w')
#        except Exception, erm:
#            stop_err('Error writing result file ' + str(erm))
#        WriteBED(tinfo, einfo, bed_fh) ## BED writter

if __name__ == "__main__": __main__()
<|endoftext|>