<commit_before>"""
MachOlib fix
============

Monkey-patch macholib to get around error in v1.7 and earlier, which
gives::

    TypeError: dyld_find() got an unexpected keyword argument 'loader'

Add the following to the top of your setup_py2app to work around this::

    import macholib_patch
"""

import macholib
#print("~"*60 + "macholib verion: "+macholib.__version__)
if macholib.__version__ <= "1.7":
    print("Applying macholib patch...")
    import macholib.dyld
    import macholib.MachOGraph
    dyld_find_1_7 = macholib.dyld.dyld_find
    def dyld_find(name, loader=None, **kwargs):
        #print("~"*60 + "calling alternate dyld_find")
        if loader is not None:
            kwargs['loader_path'] = loader
        return dyld_find_1_7(name, **kwargs)
    macholib.MachOGraph.dyld_find = dyld_find
<commit_msg>Removed macholib_patch - even if we still use macholib (don't think so) it will be > V1.7<commit_after><|endoftext|>


<commit_before>import json
import requests
try:
    import yara
except (OSError, ModuleNotFoundError):
    print("yara is missing, use 'pip3 install yara' to install it.")

misperrors = {'error': 'Error'}
mispattributes = {'input': ['yara'], 'output': ['text']}
moduleinfo = {'version': '0.1', 'author': 'Dennis Rand', 'description': 'An expansion hover module to perform a syntax check on if yara rules are valid or not.', 'module-type': ['hover']}
moduleconfig = []

def handler(q=False):
    if q is False:
        return False
    request = json.loads(q)
    if not request.get('yara'):
        misperrors['error'] = 'Yara rule missing'
        return misperrors

    try:
        rules = yara.compile(source=request.get('yara'))
        summary = ("Syntax valid")
    except Exception as e:
        summary = ("Syntax error: " + str(e))

    r = {'results': [{'types': mispattributes['output'], 'values': summary}]}
    return r

def introspection():
    return mispattributes

def version():
    moduleinfo['config'] = moduleconfig
    return moduleinfo
<commit_msg>fix: Fixed exception type for python 3.5<commit_after>import json
import requests
try:
    import yara
except (OSError, ImportError):
    print("yara is missing, use 'pip3 install yara' to install it.")

misperrors = {'error': 'Error'}
mispattributes = {'input': ['yara'], 'output': ['text']}
moduleinfo = {'version': '0.1', 'author': 'Dennis Rand', 'description': 'An expansion hover module to perform a syntax check on if yara rules are valid or not.', 'module-type': ['hover']}
moduleconfig = []

def handler(q=False):
    if q is False:
        return False
    request = json.loads(q)
    if not request.get('yara'):
        misperrors['error'] = 'Yara rule missing'
        return misperrors

    try:
        rules = yara.compile(source=request.get('yara'))
        summary = ("Syntax valid")
    except Exception as e:
        summary = ("Syntax error: " + str(e))

    r = {'results': [{'types': mispattributes['output'], 'values': summary}]}
    return r

def introspection():
    return mispattributes

def version():
    moduleinfo['config'] = moduleconfig
    return moduleinfo
<|endoftext|>


<commit_before>''' -- imports from installed packages -- '''
from django.http import StreamingHttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, render
from django.template import RequestContext
from django.contrib.auth.models import User
from django.contrib.auth.decorators import user_passes_test

from gnowsys_ndf.settings import LANGUAGES
from gnowsys_ndf.ndf.models import node_collection, triple_collection
from gnowsys_ndf.ndf.views.methods import *

import json
import datetime

@user_passes_test(lambda u: u.is_superuser)
def adminDesignerDashboardClass(request, class_name='GSystemType'):
    '''
    fetching class's objects
    '''
    if request.method=="POST":
        search = request.POST.get("search","")
        classtype = request.POST.get("class","")
        nodes = node_collection.find({'name':{'$regex':search,'$options': 'i' },'_type':classtype,'name':{'$nin':[u'Voluntary Teacher']}}).sort('last_update', -1)
        #nodes = node_collection.find({'name':{'$regex':search,'$options': 'i' },'_type':classtype}).sort('last_update', -1)
    else :
        nodes = node_collection.find({'_type':class_name,'name':{'$nin':[u'Voluntary Teacher']}}).sort('last_update', -1)
        #nodes = node_collection.find({'_type':class_name}).sort('last_update', -1)

    objects_details = []
    for each in nodes:
        member = []
        member_of_list = []
        collection_list = []
        attribute_type_set = []
        relation_type_set = []
        for e in each.member_of:
            member_of_list.append(node_collection.one({'_id':e}).name+" - "+str(e))

        for members in each.member_of:
            member.append(node_collection.one({ '_id': members}).name+" - "+str(members))

        # for coll in each.collection_set:
        #     collection_list.append(node_collection.one({ '_id': coll}).name+" - "+str(coll))

        if class_name in ("GSystemType"):
            for at_set in each.attribute_type_set:
                attribute_type_set.append(at_set.name+" - "+str(at_set._id))
            for rt_set in each.relation_type_set:
                relation_type_set.append(rt_set.name+" - "+str(rt_set._id))
            objects_details.append({"Id":each._id,"Title":each.name,"Type":",".join(member),"Author":User.objects.get(id=each.created_by).username,"Creation":each.created_at,'member_of':",".join(member_of_list), "collection_list":",".join(collection_list), "attribute_type_set":",".join(attribute_type_set), "relation_type_set":",".join(relation_type_set)})
        else :
		objects_details.append({"Id":each._id,"Title":each.name,"Type":",".join(member),"Author":User.objects.get(id=each.created_by).username,"Creation":each.created_at,'member_of':",".join(member_of_list), "collection_list":",".join(collection_list)})
    groups = []
    group = node_collection.find({'_type':"Group"})
    for each in group:
        groups.append({'id':each._id,"title":each.name})

    systemtypes = []
    #systemtype = node_collection.find({'_type':"GSystemType"})
    systemtype = node_collection.find({'_type':"GSystemType",'name':{'$nin':[u'Voluntary Teacher']}})
    for each in systemtype:
        systemtypes.append({'id':each._id,"title":each.name})

    meta_types = []
    meta_type = node_collection.find({'_type':"MetaType"})
    for each in meta_type:
        meta_types.append({'id':each._id,"title":each.name})

    groupid = ""
    group_obj= node_collection.find({'$and':[{"_type":u'Group'},{"name":u'home'}]})
    if group_obj:
	groupid = str(group_obj[0]._id)

    template = "ndf/adminDashboard.html"
    variable = RequestContext(request, {'class_name':class_name,"nodes":objects_details,"Groups":groups,"systemtypes":systemtypes,"url":"designer","groupid":groupid,'meta_types':meta_types,'group_id':groupid})
    return render_to_response(template, variable)

@user_passes_test(lambda u: u.is_superuser)
def adminDesignerDashboardClassCreate(request, class_name='GSystemType', node_id=None):
    '''
    delete class's objects
    '''
    global LANGUAGES
    new_instance_type = None
    LANGUAGES = '' if not LANGUAGES else LANGUAGES
    definitionlist = []
    contentlist = []
    dependencylist = []
    options = []
    translate=request.GET.get('translate','')
    if class_name == "AttributeType":
        definitionlist = ['name','altnames','language','subject_type','data_type','applicable_node_type','member_of','verbose_name','null','blank','help_text','max_digits','decimal_places','auto_now','auto_now_add','path','verify_exist','status']
        contentlist = ['content_org']
        dependencylist = ['prior_node']
        options = ['featured','created_at','start_publication','tags','url','last_update','login_required']
    elif class_name == "GSystemType":
        definitionlist = ['name','altnames','language','status','member_of','meta_type_set','attribute_type_set','relation_type_set','type_of']
        contentlist = ['content_org']
        dependencylist = ['prior_node']
        options = ['featured','created_at','start_publication','tags','url','last_update','login_required']
    elif class_name == "RelationType":
        definitionlist = ['name','inverse_name','altnames','language','subject_type','object_type','subject_cardinality','object_cardinality','subject_applicable_nodetype','object_applicable_nodetype','is_symmetric','is_reflexive','is_transitive','status','member_of']
        contentlist = ['content_org']
        dependencylist = ['prior_node']
        options = ['featured','created_at','start_publication','tags','url','last_update','login_required']
    else :
        definitionlist = []
        contentlist = []
        dependencylist = []
        options = []

    class_structure = eval(class_name).structure
    required_fields = eval(class_name).required_fields
    newdict = {}
    if node_id:
        new_instance_type = node_collection.one({'_type': unicode(class_name), '_id': ObjectId(node_id)})

    else:
        new_instance_type = eval("node_collection.collection"+"."+class_name)()

    if request.method=="POST":
        if translate:
            new_instance_type = eval("node_collection.collection"+"."+class_name)()

        for key,value in class_structure.items():
            if value == bool:
                if request.POST.get(key,""):
                    if request.POST.get(key,"") in ('1','2'):
                        if request.POST.get(key,"") == '1':
                            new_instance_type[key] = True
                        else :
                            new_instance_type[key] = False

            elif value == unicode:
                if request.POST.get(key,""):

                    if key == "content_org":
                        new_instance_type[key] = unicode(request.POST.get(key,""))
                        # Required to link temporary files with the current user who is modifying this document
                        usrname = request.user.username
                        filename = slugify(new_instance_type['name']) + "-" + usrname + "-"
                        new_instance_type['content'] = org2html(new_instance_type[key], file_prefix=filename)
                    else :
                        if translate:
                            if key in ("name","inverse_name"):
                                new_instance_type[key] = unicode(request.POST.get(key+"_trans",""))

                            else:
                                new_instance_type[key] = unicode(request.POST.get(key,""))
                        else:
                            new_instance_type[key] = unicode(request.POST.get(key,""))

            elif value == list:
                if request.POST.get(key,""):
                    new_instance_type[key] = request.POST.get(key,"").split(",")

            elif type(value) == list:
                if request.POST.get(key,""):
                    if key in ("tags","applicable_node_type"):
                        new_instance_type[key] = request.POST.get(key,"").split(",")
                    elif key in ["meta_type_set","attribute_type_set","relation_type_set"]:
                        listoflist = []
                        for each in request.POST.get(key,"").split(","):
                            listoflist.append(node_collection.one({"_id":ObjectId(each)}))
                        new_instance_type[key] = listoflist
                    else :
                        listoflist = []
                        for each in request.POST.get(key,"").split(","):
                            listoflist.append(ObjectId(each))
                        new_instance_type[key] = listoflist

            elif value == datetime.datetime:
                if key == "last_update":
                    new_instance_type[key] = datetime.datetime.now()

            elif key == "status":
                if request.POST.get(key,""):
                    new_instance_type[key] = unicode(request.POST.get(key,""))

            # elif key == "created_by":
            #     new_instance_type[key] = request.user.id

            elif value == int:
                if request.POST.get(key,""):
                    new_instance_type[key] = int(request.POST.get(key,""))

            else:
                if request.POST.get(key,""):
                    new_instance_type[key] = request.POST.get(key,"")

        user_id = request.user.id

        if not new_instance_type.has_key('_id'):
            new_instance_type.created_by = user_id

        new_instance_type.modified_by = user_id

        if user_id not in new_instance_type.contributors:
            new_instance_type.contributors.append(user_id)

        new_instance_type.save()
        if translate:
            relation_type=node_collection.one({'$and':[{'name':'translation_of'},{'_type':'RelationType'}]})
            grelation=node_collection.collection.GRelation()
            grelation.relation_type=relation_type
            grelation.subject=new_instance_type['_id']
            grelation.right_subject=ObjectId(node_id)
            grelation.name=u""
            grelation.save()

        return HttpResponseRedirect("/admin/designer/"+class_name)

    # If GET request ---------------------------------------------------------------------------------------

    for key,value in class_structure.items():
        if value == bool:
            # newdict[key] = "bool"
            newdict[key] = ["bool", new_instance_type[key]]
        elif value == unicode:
            if key == "language":
                newdict[key] = ["list", new_instance_type[key]]
            else:
                # newdict[key] = "unicode"
                newdict[key] = ["unicode", new_instance_type[key]]

        elif value == list:
            # newdict[key] = "list"

            newdict[key] = ["list", new_instance_type[key]]
        elif type(value) == list:
            # newdict[key] = "list"
            newdict[key] = ["list", new_instance_type[key]]
        elif value == datetime.datetime:
            # newdict[key] = "datetime"
            newdict[key] = ["datetime", new_instance_type[key]]
        elif value == int:
            # newdict[key] = "int"
            newdict[key] = ["int", new_instance_type[key]]
        elif key == "status":
            # newdict[key] = "status"
            newdict[key] = ["status", new_instance_type[key]]
        else:
            # newdict[key] = value
            newdict[key] = [value, new_instance_type[key]]

    class_structure = newdict
    groupid = ""
    group_obj= node_collection.find({'$and':[{"_type":u'Group'},{"name":u'home'}]})
    if group_obj:
	groupid = str(group_obj[0]._id)

    template = "ndf/adminDashboardCreate.html"

    variable =  None
    class_structure_with_values = {}
    if node_id:

        for key, value in class_structure.items():
            class_structure_with_values[key] = [class_structure[key][0], new_instance_type[key]]

        variable = RequestContext(request, {'node': new_instance_type,
                                            'class_name': class_name, 'class_structure': class_structure_with_values, 'url': "designer",
                                            'definitionlist': definitionlist, 'contentlist': contentlist, 'dependencylist': dependencylist,
                                            'options': options, 'required_fields': required_fields,"translate":translate,"lan":LANGUAGES,
                                            'groupid': groupid,'group_id':groupid
                                        })
    else:
        variable = RequestContext(request, {'class_name':class_name, "url":"designer", "class_structure":class_structure, 'definitionlist':definitionlist, 'contentlist':contentlist, 'dependencylist':dependencylist, 'options':options, "required_fields":required_fields,"groupid":groupid,"translate":translate,"lan":LANGUAGES,'group_id':groupid})

    return render_to_response(template, variable)

<commit_msg>mandotary fields autofilled<commit_after>''' -- imports from installed packages -- '''
from django.http import StreamingHttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, render
from django.template import RequestContext
from django.contrib.auth.models import User
from django.contrib.auth.decorators import user_passes_test

from gnowsys_ndf.settings import LANGUAGES
from gnowsys_ndf.ndf.models import node_collection, triple_collection
from gnowsys_ndf.ndf.views.methods import *

import json
import datetime

@user_passes_test(lambda u: u.is_superuser)
def adminDesignerDashboardClass(request, class_name='GSystemType'):
    '''
    fetching class's objects
    '''
    if request.method=="POST":
        search = request.POST.get("search","")
        classtype = request.POST.get("class","")
        nodes = node_collection.find({'name':{'$regex':search,'$options': 'i' },'_type':classtype,'name':{'$nin':[u'Voluntary Teacher']}}).sort('last_update', -1)
        #nodes = node_collection.find({'name':{'$regex':search,'$options': 'i' },'_type':classtype}).sort('last_update', -1)
    else :
        nodes = node_collection.find({'_type':class_name,'name':{'$nin':[u'Voluntary Teacher']}}).sort('last_update', -1)
        #nodes = node_collection.find({'_type':class_name}).sort('last_update', -1)

    objects_details = []
    for each in nodes:
        member = []
        member_of_list = []
        collection_list = []
        attribute_type_set = []
        relation_type_set = []
        for e in each.member_of:
            member_of_list.append(node_collection.one({'_id':e}).name+" - "+str(e))

        for members in each.member_of:
            member.append(node_collection.one({ '_id': members}).name+" - "+str(members))

        # for coll in each.collection_set:
        #     collection_list.append(node_collection.one({ '_id': coll}).name+" - "+str(coll))

        if class_name in ("GSystemType"):
            for at_set in each.attribute_type_set:
                attribute_type_set.append(at_set.name+" - "+str(at_set._id))
            for rt_set in each.relation_type_set:
                relation_type_set.append(rt_set.name+" - "+str(rt_set._id))
            objects_details.append({"Id":each._id,"Title":each.name,"Type":",".join(member),"Author":User.objects.get(id=each.created_by).username,"Creation":each.created_at,'member_of':",".join(member_of_list), "collection_list":",".join(collection_list), "attribute_type_set":",".join(attribute_type_set), "relation_type_set":",".join(relation_type_set)})
        else :
		objects_details.append({"Id":each._id,"Title":each.name,"Type":",".join(member),"Author":User.objects.get(id=each.created_by).username,"Creation":each.created_at,'member_of':",".join(member_of_list), "collection_list":",".join(collection_list)})
    groups = []
    group = node_collection.find({'_type':"Group"})
    for each in group:
        groups.append({'id':each._id,"title":each.name})

    systemtypes = []
    #systemtype = node_collection.find({'_type':"GSystemType"})
    systemtype = node_collection.find({'_type':"GSystemType",'name':{'$nin':[u'Voluntary Teacher']}})
    for each in systemtype:
        systemtypes.append({'id':each._id,"title":each.name})

    meta_types = []
    meta_type = node_collection.find({'_type':"MetaType"})
    for each in meta_type:
        meta_types.append({'id':each._id,"title":each.name})

    groupid = ""
    group_obj= node_collection.find({'$and':[{"_type":u'Group'},{"name":u'home'}]})
    if group_obj:
	groupid = str(group_obj[0]._id)

    template = "ndf/adminDashboard.html"
    variable = RequestContext(request, {'class_name':class_name,"nodes":objects_details,"Groups":groups,"systemtypes":systemtypes,"url":"designer","groupid":groupid,'meta_types':meta_types,'group_id':groupid})
    return render_to_response(template, variable)

@user_passes_test(lambda u: u.is_superuser)
def adminDesignerDashboardClassCreate(request, class_name='GSystemType', node_id=None):
    '''
    delete class's objects
    '''
    global LANGUAGES
    new_instance_type = None
    LANGUAGES = '' if not LANGUAGES else LANGUAGES
    definitionlist = []
    contentlist = []
    dependencylist = []
    options = []
    translate=request.GET.get('translate','')
    if class_name == "AttributeType":
        definitionlist = ['name','altnames','language','subject_type','data_type','applicable_node_type','member_of','verbose_name','null','blank','help_text','max_digits','decimal_places','auto_now','auto_now_add','path','verify_exist','status']
        contentlist = ['content_org']
        dependencylist = ['prior_node']
        options = ['featured','created_at','start_publication','tags','url','last_update','login_required']
    elif class_name == "GSystemType":
        definitionlist = ['name','altnames','language','status','member_of','meta_type_set','attribute_type_set','relation_type_set','type_of']
        contentlist = ['content_org']
        dependencylist = ['prior_node']
        options = ['featured','created_at','start_publication','tags','url','last_update','login_required']
    elif class_name == "RelationType":
        definitionlist = ['name','inverse_name','altnames','language','subject_type','object_type','subject_cardinality','object_cardinality','subject_applicable_nodetype','object_applicable_nodetype','is_symmetric','is_reflexive','is_transitive','status','member_of']
        contentlist = ['content_org']
        dependencylist = ['prior_node']
        options = ['featured','created_at','start_publication','tags','url','last_update','login_required']
    else :
        definitionlist = []
        contentlist = []
        dependencylist = []
        options = []

    class_structure = eval(class_name).structure
    required_fields = eval(class_name).required_fields
    newdict = {}
    if node_id:
        new_instance_type = node_collection.one({'_type': unicode(class_name), '_id': ObjectId(node_id)})

    else:
        new_instance_type = eval("node_collection.collection"+"."+class_name)()

    if request.method=="POST":
        if translate:
            new_instance_type = eval("node_collection.collection"+"."+class_name)()

        for key,value in class_structure.items():
            if value == bool:
                if request.POST.get(key,""):
                    if request.POST.get(key,"") in ('1','2'):
                        if request.POST.get(key,"") == '1':
                            new_instance_type[key] = True
                        else :
                            new_instance_type[key] = False

            elif value == unicode:
                if request.POST.get(key,""):

                    if key == "content_org":
                        new_instance_type[key] = unicode(request.POST.get(key,""))
                        # Required to link temporary files with the current user who is modifying this document
                        usrname = request.user.username
                        filename = slugify(new_instance_type['name']) + "-" + usrname + "-"
                        new_instance_type['content'] = org2html(new_instance_type[key], file_prefix=filename)
                    else :
                        if translate:
                            if key in ("name","inverse_name"):
                                new_instance_type[key] = unicode(request.POST.get(key+"_trans",""))
                                language= request.POST.get('lan')
                                new_instance_type.language=language

                            else:
                                new_instance_type[key] = unicode(request.POST.get(key,""))
                        else:
                            new_instance_type[key] = unicode(request.POST.get(key,""))

            elif value == list:
                if request.POST.get(key,""):
                    new_instance_type[key] = request.POST.get(key,"").split(",")

            elif type(value) == list:
                if request.POST.get(key,""):
                    if key in ("tags","applicable_node_type"):
                        new_instance_type[key] = request.POST.get(key,"").split(",")
                    elif key in ["meta_type_set","attribute_type_set","relation_type_set"]:
                        listoflist = []
                        for each in request.POST.get(key,"").split(","):
                            listoflist.append(node_collection.one({"_id":ObjectId(each)}))
                        new_instance_type[key] = listoflist
                    else :
                        listoflist = []
                        for each in request.POST.get(key,"").split(","):
                            listoflist.append(ObjectId(each))
                        new_instance_type[key] = listoflist

            elif value == datetime.datetime:
                if key == "last_update":
                    new_instance_type[key] = datetime.datetime.now()

            elif key == "status":
                if request.POST.get(key,""):
                    new_instance_type[key] = unicode(request.POST.get(key,""))

            # elif key == "created_by":
            #     new_instance_type[key] = request.user.id

            elif value == int:
                if request.POST.get(key,""):
                    new_instance_type[key] = int(request.POST.get(key,""))

            else:
                if request.POST.get(key,""):
                    new_instance_type[key] = request.POST.get(key,"")

        user_id = request.user.id

        if not new_instance_type.has_key('_id'):
            new_instance_type.created_by = user_id

        new_instance_type.modified_by = user_id

        if user_id not in new_instance_type.contributors:
            new_instance_type.contributors.append(user_id)
        parent_node=node_collection.one({'_id':ObjectId(node_id)})
        if translate and class_name == "RelationType":
            new_instance_type.subject_type = parent_node.subject_type
            new_instance_type.object_type = parent_node.object_type
        if translate and class_name == "AttributeType":
            new_instance_type.data_type = parent_node.data_type
            new_instance_type.subject_type = parent_node.subject_type

        new_instance_type.save()
        if translate:
            relation_type=node_collection.one({'$and':[{'name':'translation_of'},{'_type':'RelationType'}]})
            grelation=node_collection.collection.GRelation()
            grelation.relation_type=relation_type
            grelation.subject=new_instance_type['_id']
            grelation.right_subject=ObjectId(node_id)
            grelation.name=u""
            grelation.save()

        return HttpResponseRedirect("/admin/designer/"+class_name)

    # If GET request ---------------------------------------------------------------------------------------

    for key,value in class_structure.items():
        if value == bool:
            # newdict[key] = "bool"
            newdict[key] = ["bool", new_instance_type[key]]
        elif value == unicode:
            if key == "language":
                newdict[key] = ["list", new_instance_type[key]]
            else:
                # newdict[key] = "unicode"
                newdict[key] = ["unicode", new_instance_type[key]]

        elif value == list:
            # newdict[key] = "list"

            newdict[key] = ["list", new_instance_type[key]]
        elif type(value) == list:
            # newdict[key] = "list"
            newdict[key] = ["list", new_instance_type[key]]
        elif value == datetime.datetime:
            # newdict[key] = "datetime"
            newdict[key] = ["datetime", new_instance_type[key]]
        elif value == int:
            # newdict[key] = "int"
            newdict[key] = ["int", new_instance_type[key]]
        elif key == "status":
            # newdict[key] = "status"
            newdict[key] = ["status", new_instance_type[key]]
        else:
            # newdict[key] = value
            newdict[key] = [value, new_instance_type[key]]

    class_structure = newdict
    groupid = ""
    group_obj= node_collection.find({'$and':[{"_type":u'Group'},{"name":u'home'}]})
    if group_obj:
	groupid = str(group_obj[0]._id)

    template = "ndf/adminDashboardCreate.html"

    variable =  None
    class_structure_with_values = {}
    if node_id:

        for key, value in class_structure.items():
            class_structure_with_values[key] = [class_structure[key][0], new_instance_type[key]]

        variable = RequestContext(request, {'node': new_instance_type,
                                            'class_name': class_name, 'class_structure': class_structure_with_values, 'url': "designer",
                                            'definitionlist': definitionlist, 'contentlist': contentlist, 'dependencylist': dependencylist,
                                            'options': options, 'required_fields': required_fields,"translate":translate,"lan":LANGUAGES,
                                            'groupid': groupid,'group_id':groupid
                                        })
    else:
        variable = RequestContext(request, {'class_name':class_name, "url":"designer", "class_structure":class_structure, 'definitionlist':definitionlist, 'contentlist':contentlist, 'dependencylist':dependencylist, 'options':options, "required_fields":required_fields,"groupid":groupid,"translate":translate,"lan":LANGUAGES,'group_id':groupid})

    return render_to_response(template, variable)

<|endoftext|>


<commit_before>#!/usr/bin/env python
#! -*- encoding: UTF-8 -*-

# Copyright (c) 2002-2015 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
This module contains the main Bolt driver components as well as several
helper and exception classes. The main entry point is the `GraphDatabase`
class which can be used to obtain `Driver` instances that are used for
managing sessions.
"""

from __future__ import division

from collections import namedtuple
from io import BytesIO
import logging
from os import environ
from select import select
from socket import create_connection, SHUT_RDWR
from struct import pack as struct_pack, unpack as struct_unpack, unpack_from as struct_unpack_from

from .compat import integer, perf_counter, secure_socket, string, urlparse

# Serialisation and deserialisation routines plus the structure data type
from .packstream import Packer, Unpacker, Structure

# Hydration function for turning structures into their actual types
from .typesystem import hydrated

DEFAULT_PORT = 7687

# Signature bytes for each message type
INIT = b"\x01"             # 0000 0001 // INIT <user_agent>
ACK_FAILURE = b"\x0F"      # 0000 1111 // ACK_FAILURE
RUN = b"\x10"              # 0001 0000 // RUN <statement> <parameters>
DISCARD_ALL = b"\x2F"      # 0010 1111 // DISCARD *
PULL_ALL = b"\x3F"         # 0011 1111 // PULL *
SUCCESS = b"\x70"          # 0111 0000 // SUCCESS <metadata>
RECORD = b"\x71"           # 0111 0001 // RECORD <value>
IGNORED = b"\x7E"          # 0111 1110 // IGNORED <metadata>
FAILURE = b"\x7F"          # 0111 1111 // FAILURE <metadata>

# Set up logger
log = logging.getLogger("neo4j")
log_debug = log.debug
log_info = log.info
log_warning = log.warning
log_error = log.error

Latency = namedtuple("Latency", ["overall", "network", "wait"])

class ProtocolError(Exception):

    pass

class CypherError(Exception):

    code = None
    message = None

    def __init__(self, data):
        super(CypherError, self).__init__(data.get("message"))
        for key, value in data.items():
            if not key.startswith("_"):
                setattr(self, key, value)

class Record(object):
    """ Record object for storing result values along with field names.
    """

    def __init__(self, fields, values):
        self.__fields__ = fields
        self.__values__ = values

    def __repr__(self):
        values = self.__values__
        s = []
        for i, field in enumerate(self.__fields__):
            s.append("%s=%r" % (field, values[i]))
        return "<Record %s>" % " ".join(s)

    def __eq__(self, other):
        try:
            return vars(self) == vars(other)
        except TypeError:
            return tuple(self) == tuple(other)

    def __ne__(self, other):
        return not self.__eq__(other)

    def __len__(self):
        return self.__fields__.__len__()

    def __getitem__(self, item):
        if isinstance(item, string):
            return getattr(self, item)
        elif isinstance(item, integer):
            return getattr(self, self.__fields__[item])
        else:
            raise TypeError(item)

    def __getattr__(self, item):
        try:
            i = self.__fields__.index(item)
        except ValueError:
            raise AttributeError("No field %r" % item)
        else:
            return self.__values__[i]

class ChunkWriter(object):
    """ Writer for chunked data.
    """

    max_chunk_size = 65535

    def __init__(self):
        self.raw = BytesIO()
        self.output_buffer = []
        self.output_size = 0

    def write(self, b):
        """ Write some bytes, splitting into chunks if necessary.
        """
        max_chunk_size = self.max_chunk_size
        output_buffer = self.output_buffer
        while b:
            size = len(b)
            future_size = self.output_size + size
            if future_size >= max_chunk_size:
                end = max_chunk_size - self.output_size
                output_buffer.append(b[:end])
                self.output_size = max_chunk_size
                b = b[end:]
                self.flush()
            else:
                output_buffer.append(b)
                self.output_size = future_size
                b = b""

    def flush(self, zero_chunk=False):
        """ Flush everything written since the last chunk to the
        stream, followed by a zero-chunk if required.
        """
        output_buffer = self.output_buffer
        if output_buffer:
            lines = [struct_pack(">H", self.output_size)] + output_buffer
        else:
            lines = []
        if zero_chunk:
            lines.append(b"\x00\x00")
        if lines:
            self.raw.writelines(lines)
            self.raw.flush()
            del output_buffer[:]
            self.output_size = 0

    def to_bytes(self):
        """ Extract the written data as bytes.
        """
        return self.raw.getvalue()

    def close(self, zero_chunk=False):
        """ Close the stream.
        """
        self.flush(zero_chunk=zero_chunk)
        self.raw.close()

class SessionV1(object):
    """ Server connection through which all protocol messages
    are sent and received. This class is designed for protocol
    version 1.
    """

    def __init__(self, s, **config):
        self.socket = s
        self._recv_buffer = b""
        self.init("neo4j-python/0.0")
        if config.get("bench"):
            self._bench = []
        else:
            self._bench = None

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.close()

    def _send_messages(self, *messages):
        """ Send one or more messages to the server.
        """
        raw = ChunkWriter()
        packer = Packer(raw)
        pack_struct_header = packer.pack_struct_header
        pack = packer.pack
        flush = raw.flush

        for signature, fields in messages:
            pack_struct_header(len(fields), signature)
            for field in fields:
                pack(field)
            flush(zero_chunk=True)

        data = raw.to_bytes()
        if __debug__: log_debug("C: %r", data)
        t1 = perf_counter()
        self.socket.sendall(data)
        t2 = perf_counter()

        raw.close()

        return t1, t2

    def _recv_message(self, times=None):
        """ Receive exactly one message from the server.
        """
        raw = BytesIO()
        unpack = Unpacker(raw).unpack

        socket = self.socket
        recv = socket.recv
        write = raw.write

        start_recv = None

        # Receive chunks of data until chunk_size == 0
        chunk_size = None
        while chunk_size != 0:
            # Determine how much to read depending on header or data
            size = 2 if chunk_size is None else chunk_size

            # If data is needed, keep reading until all bytes have been received
            remaining = size - len(self._recv_buffer)
            ready_to_read = None
            while remaining > 0:
                # Read up to the required amount remaining
                b = recv(8192)
                if b:
                    if start_recv is None:
                        start_recv = perf_counter()
                    if __debug__: log_debug("S: %r", b)
                else:
                    if ready_to_read is not None:
                        raise ProtocolError("Server closed connection")
                remaining -= len(b)
                self._recv_buffer += b

                # If more is required, wait for available network data
                if remaining > 0:
                    ready_to_read, _, _ = select((socket,), (), (), 0)
                    while not ready_to_read:
                        ready_to_read, _, _ = select((socket,), (), (), 0)

            # Split off the amount of data required and keep the rest in the buffer
            data, self._recv_buffer = self._recv_buffer[:size], self._recv_buffer[size:]

            if chunk_size is None:
                # Interpret data as chunk header
                chunk_size, = struct_unpack_from(">H", data)
            elif chunk_size > 0:
                # Interpret data as chunk
                write(data)
                chunk_size = None

        if times:
            times[3] = start_recv
            times[4] = perf_counter()

        # Unpack the message structure from the raw byte stream
        # (there should be only one)
        raw.seek(0)
        message = next(unpack())
        if not isinstance(message, Structure):
            # Something other than a message has been received
            log_error("S: @*#!")
            raise ProtocolError("Non-message data received from server")
        signature, fields = message
        raw.close()

        # Acknowledge any failures immediately
        if signature == FAILURE:
            self.ack_failure()

        return signature, fields

    def init(self, user_agent):
        """ Initialise a connection with a user agent string.
        """

        # Ensure the user agent is a Unicode value
        if isinstance(user_agent, bytes):
            user_agent = user_agent.decode("UTF-8")

        if __debug__: log_info("C: INIT %r", user_agent)
        self._send_messages((INIT, (user_agent,)))

        signature, (data,) = self._recv_message()
        if signature == SUCCESS:
            if __debug__: log_info("S: SUCCESS %r", data)
        else:
            if __debug__: log_info("S: FAILURE %r", data)
            raise ProtocolError("Initialisation failed")

    def run(self, statement, parameters=None):
        """ Run a parameterised Cypher statement.
        """
        # Collect six checkpoint times for each run
        # INIT | START_SEND | END_SEND | START_RECV | END_RECV | RETURN
        t = [perf_counter(), None, None, None, None, None]

        recv_message = self._recv_message

        # Ensure the statement is a Unicode value
        if isinstance(statement, bytes):
            statement = statement.decode("UTF-8")

        parameters = dict(parameters or {})
        if __debug__:
            log_info("C: RUN %r %r", statement, parameters)
            log_info("C: PULL_ALL")
        t[1], t[2] = self._send_messages((RUN, (statement, parameters)),
                                         (PULL_ALL, ()))

        signature, (data,) = recv_message(t)
        if signature == SUCCESS:
            fields = data["fields"]
            if __debug__: log_info("S: SUCCESS %r", data)
        else:
            if __debug__: log_info("S: FAILURE %r", data)
            raise CypherError(data)

        records = []
        append = records.append
        more = True
        while more:
            signature, (data,) = recv_message()
            if signature == RECORD:
                if __debug__: log_info("S: RECORD %r", data)
                append(Record(fields, tuple(map(hydrated, data))))
            elif signature == SUCCESS:
                if __debug__: log_info("S: SUCCESS %r", data)
                more = False
            else:
                if __debug__: log_info("S: FAILURE %r", data)
                raise CypherError(data)

        bench = self._bench
        if bench is not None:
            t[5] = perf_counter()
            bench.append(t)

        return records

    def ack_failure(self):
        """ Send an acknowledgement for a previous failure.
        """
        if __debug__: log_info("C: ACK_FAILURE")
        self._send_messages((ACK_FAILURE, ()))

        # Skip any ignored responses
        signature, fields = self._recv_message()
        while signature == IGNORED:
            if __debug__: log_info("S: IGNORED")
            signature, fields = self._recv_message()

        # Check the acknowledgement was successful
        data, = fields
        if signature == SUCCESS:
            if __debug__: log_info("S: SUCCESS %r", data)
        else:
            if __debug__: log_info("S: FAILURE %r", data)
            raise ProtocolError("Could not acknowledge failure")

    def close(self):
        """ Shut down and close the connection.
        """
        if __debug__: log_info("~~ [CLOSE]")
        socket = self.socket
        socket.shutdown(SHUT_RDWR)
        socket.close()

    @property
    def bench(self):
        if self._bench:
            return [Latency(return_ - init, end_recv - start_send, start_recv - end_send)
                    for init, start_send, end_send, start_recv, end_recv, return_ in self._bench]

class Driver(object):
    """ Accessor for a specific graph database resource.
    """

    def __init__(self, url, **config):
        self.url = url
        parsed = urlparse(self.url)
        if parsed.scheme == "bolt":
            self.host = parsed.hostname
            self.port = parsed.port
        else:
            raise ProtocolError("Unsupported URL scheme: %s" % parsed.scheme)
        self.config = config

    def session(self, **config):
        """ Connect and perform a handshake in order to return a valid
        Connection object, assuming a protocol version can be agreed.
        """
        config = dict(self.config, **config)

        # Establish a connection to the host and port specified
        host = self.host
        port = self.port or DEFAULT_PORT
        if __debug__: log_info("~~ [CONNECT] %s %d", host, port)
        s = create_connection((host, port))

        # Secure the connection if so requested
        try:
            secure = environ["NEO4J_SECURE"]
        except KeyError:
            secure = config.get("secure", False)
        if secure:
            if __debug__: log_info("~~ [SECURE] %s", host)
            s = secure_socket(s, host)

        # Send details of the protocol versions supported
        supported_versions = [1, 0, 0, 0]
        if __debug__: log_info("C: [HANDSHAKE] %r", supported_versions)
        data = b"".join(struct_pack(">I", version) for version in supported_versions)
        if __debug__: log_debug("C: %r", data)
        s.sendall(data)

        # Handle the handshake response
        ready_to_read, _, _ = select((s,), (), (), 0)
        while not ready_to_read:
            ready_to_read, _, _ = select((s,), (), (), 0)
        data = s.recv(4)
        data_size = len(data)
        if data_size == 0:
            # If no data is returned after a successful select
            # response, the server has closed the connection
            log_error("S: [CLOSE]")
            raise ProtocolError("Server closed connection without responding to handshake")
        if data_size == 4:
            if __debug__: log_debug("S: %r", data)
        else:
            # Some other garbled data has been received
            log_error("S: @*#!")
            raise ProtocolError("Expected four byte handshake response, received %r instead" % data)
        agreed_version, = struct_unpack(">I", data)
        if __debug__: log_info("S: [HANDSHAKE] %d", agreed_version)
        if agreed_version == 0:
            if __debug__: log_debug("~~ [CLOSE]")
            s.shutdown(SHUT_RDWR)
            s.close()
        else:
            return SessionV1(s, **config)

class GraphDatabase(object):
    """ Top level accessor for all graph database functionality.
    """

    @classmethod
    def driver(cls, url, **config):
        """ Acquire a driver instance for the given URL and configuration.
        """
        return Driver(url, **config)
<commit_msg>Better debug output<commit_after>#!/usr/bin/env python
#! -*- encoding: UTF-8 -*-

# Copyright (c) 2002-2015 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
This module contains the main Bolt driver components as well as several
helper and exception classes. The main entry point is the `GraphDatabase`
class which can be used to obtain `Driver` instances that are used for
managing sessions.
"""

from __future__ import division

from collections import namedtuple
from io import BytesIO
import logging
from os import environ
from select import select
from socket import create_connection, SHUT_RDWR
from struct import pack as struct_pack, unpack as struct_unpack, unpack_from as struct_unpack_from

from .compat import integer, perf_counter, secure_socket, string, urlparse

# Serialisation and deserialisation routines plus the structure data type
from .packstream import Packer, Unpacker, Structure

# Hydration function for turning structures into their actual types
from .typesystem import hydrated

DEFAULT_PORT = 7687

# Signature bytes for each message type
INIT = b"\x01"             # 0000 0001 // INIT <user_agent>
ACK_FAILURE = b"\x0F"      # 0000 1111 // ACK_FAILURE
RUN = b"\x10"              # 0001 0000 // RUN <statement> <parameters>
DISCARD_ALL = b"\x2F"      # 0010 1111 // DISCARD *
PULL_ALL = b"\x3F"         # 0011 1111 // PULL *
SUCCESS = b"\x70"          # 0111 0000 // SUCCESS <metadata>
RECORD = b"\x71"           # 0111 0001 // RECORD <value>
IGNORED = b"\x7E"          # 0111 1110 // IGNORED <metadata>
FAILURE = b"\x7F"          # 0111 1111 // FAILURE <metadata>

# Set up logger
log = logging.getLogger("neo4j")
log_debug = log.debug
log_info = log.info
log_warning = log.warning
log_error = log.error

Latency = namedtuple("Latency", ["overall", "network", "wait"])

def hex2(x):
    if x < 0x10:
        return "0" + hex(x)[2:].upper()
    else:
        return hex(x)[2:].upper()

class ProtocolError(Exception):

    pass

class CypherError(Exception):

    code = None
    message = None

    def __init__(self, data):
        super(CypherError, self).__init__(data.get("message"))
        for key, value in data.items():
            if not key.startswith("_"):
                setattr(self, key, value)

class Record(object):
    """ Record object for storing result values along with field names.
    """

    def __init__(self, fields, values):
        self.__fields__ = fields
        self.__values__ = values

    def __repr__(self):
        values = self.__values__
        s = []
        for i, field in enumerate(self.__fields__):
            s.append("%s=%r" % (field, values[i]))
        return "<Record %s>" % " ".join(s)

    def __eq__(self, other):
        try:
            return vars(self) == vars(other)
        except TypeError:
            return tuple(self) == tuple(other)

    def __ne__(self, other):
        return not self.__eq__(other)

    def __len__(self):
        return self.__fields__.__len__()

    def __getitem__(self, item):
        if isinstance(item, string):
            return getattr(self, item)
        elif isinstance(item, integer):
            return getattr(self, self.__fields__[item])
        else:
            raise TypeError(item)

    def __getattr__(self, item):
        try:
            i = self.__fields__.index(item)
        except ValueError:
            raise AttributeError("No field %r" % item)
        else:
            return self.__values__[i]

class ChunkWriter(object):
    """ Writer for chunked data.
    """

    max_chunk_size = 65535

    def __init__(self):
        self.raw = BytesIO()
        self.output_buffer = []
        self.output_size = 0

    def write(self, b):
        """ Write some bytes, splitting into chunks if necessary.
        """
        max_chunk_size = self.max_chunk_size
        output_buffer = self.output_buffer
        while b:
            size = len(b)
            future_size = self.output_size + size
            if future_size >= max_chunk_size:
                end = max_chunk_size - self.output_size
                output_buffer.append(b[:end])
                self.output_size = max_chunk_size
                b = b[end:]
                self.flush()
            else:
                output_buffer.append(b)
                self.output_size = future_size
                b = b""

    def flush(self, zero_chunk=False):
        """ Flush everything written since the last chunk to the
        stream, followed by a zero-chunk if required.
        """
        output_buffer = self.output_buffer
        if output_buffer:
            lines = [struct_pack(">H", self.output_size)] + output_buffer
        else:
            lines = []
        if zero_chunk:
            lines.append(b"\x00\x00")
        if lines:
            self.raw.writelines(lines)
            self.raw.flush()
            del output_buffer[:]
            self.output_size = 0

    def to_bytes(self):
        """ Extract the written data as bytes.
        """
        return self.raw.getvalue()

    def close(self, zero_chunk=False):
        """ Close the stream.
        """
        self.flush(zero_chunk=zero_chunk)
        self.raw.close()

class SessionV1(object):
    """ Server connection through which all protocol messages
    are sent and received. This class is designed for protocol
    version 1.
    """

    def __init__(self, s, **config):
        self.socket = s
        self._recv_buffer = b""
        self.init("neo4j-python/0.0")
        if config.get("bench"):
            self._bench = []
        else:
            self._bench = None

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.close()

    def _send_messages(self, *messages):
        """ Send one or more messages to the server.
        """
        raw = ChunkWriter()
        packer = Packer(raw)
        pack_struct_header = packer.pack_struct_header
        pack = packer.pack
        flush = raw.flush

        for signature, fields in messages:
            pack_struct_header(len(fields), signature)
            for field in fields:
                pack(field)
            flush(zero_chunk=True)

        data = raw.to_bytes()
        if __debug__: log_debug("C: %s", ":".join(map(hex2, data)))
        t1 = perf_counter()
        self.socket.sendall(data)
        t2 = perf_counter()

        raw.close()

        return t1, t2

    def _recv_message(self, times=None):
        """ Receive exactly one message from the server.
        """
        raw = BytesIO()
        unpack = Unpacker(raw).unpack

        socket = self.socket
        recv = socket.recv
        write = raw.write

        start_recv = None

        # Receive chunks of data until chunk_size == 0
        chunk_size = None
        while chunk_size != 0:
            # Determine how much to read depending on header or data
            size = 2 if chunk_size is None else chunk_size

            # If data is needed, keep reading until all bytes have been received
            remaining = size - len(self._recv_buffer)
            ready_to_read = None
            while remaining > 0:
                # Read up to the required amount remaining
                b = recv(8192)
                if b:
                    if start_recv is None:
                        start_recv = perf_counter()
                    if __debug__: log_debug("S: %s", ":".join(map(hex2, b)))
                else:
                    if ready_to_read is not None:
                        raise ProtocolError("Server closed connection")
                remaining -= len(b)
                self._recv_buffer += b

                # If more is required, wait for available network data
                if remaining > 0:
                    ready_to_read, _, _ = select((socket,), (), (), 0)
                    while not ready_to_read:
                        ready_to_read, _, _ = select((socket,), (), (), 0)

            # Split off the amount of data required and keep the rest in the buffer
            data, self._recv_buffer = self._recv_buffer[:size], self._recv_buffer[size:]

            if chunk_size is None:
                # Interpret data as chunk header
                chunk_size, = struct_unpack_from(">H", data)
            elif chunk_size > 0:
                # Interpret data as chunk
                write(data)
                chunk_size = None

        if times:
            times[3] = start_recv
            times[4] = perf_counter()

        # Unpack the message structure from the raw byte stream
        # (there should be only one)
        raw.seek(0)
        message = next(unpack())
        if not isinstance(message, Structure):
            # Something other than a message has been received
            log_error("S: @*#!")
            raise ProtocolError("Non-message data received from server")
        signature, fields = message
        raw.close()

        # Acknowledge any failures immediately
        if signature == FAILURE:
            self.ack_failure()

        return signature, fields

    def init(self, user_agent):
        """ Initialise a connection with a user agent string.
        """

        # Ensure the user agent is a Unicode value
        if isinstance(user_agent, bytes):
            user_agent = user_agent.decode("UTF-8")

        if __debug__: log_info("C: INIT %r", user_agent)
        self._send_messages((INIT, (user_agent,)))

        signature, (data,) = self._recv_message()
        if signature == SUCCESS:
            if __debug__: log_info("S: SUCCESS %r", data)
        else:
            if __debug__: log_info("S: FAILURE %r", data)
            raise ProtocolError("Initialisation failed")

    def run(self, statement, parameters=None):
        """ Run a parameterised Cypher statement.
        """
        # Collect six checkpoint times for each run
        # INIT | START_SEND | END_SEND | START_RECV | END_RECV | RETURN
        t = [perf_counter(), None, None, None, None, None]

        recv_message = self._recv_message

        # Ensure the statement is a Unicode value
        if isinstance(statement, bytes):
            statement = statement.decode("UTF-8")

        parameters = dict(parameters or {})
        if __debug__:
            log_info("C: RUN %r %r", statement, parameters)
            log_info("C: PULL_ALL")
        t[1], t[2] = self._send_messages((RUN, (statement, parameters)),
                                         (PULL_ALL, ()))

        signature, (data,) = recv_message(t)
        if signature == SUCCESS:
            fields = data["fields"]
            if __debug__: log_info("S: SUCCESS %r", data)
        else:
            if __debug__: log_info("S: FAILURE %r", data)
            raise CypherError(data)

        records = []
        append = records.append
        more = True
        while more:
            signature, (data,) = recv_message()
            if signature == RECORD:
                if __debug__: log_info("S: RECORD %r", data)
                append(Record(fields, tuple(map(hydrated, data))))
            elif signature == SUCCESS:
                if __debug__: log_info("S: SUCCESS %r", data)
                more = False
            else:
                if __debug__: log_info("S: FAILURE %r", data)
                raise CypherError(data)

        bench = self._bench
        if bench is not None:
            t[5] = perf_counter()
            bench.append(t)

        return records

    def ack_failure(self):
        """ Send an acknowledgement for a previous failure.
        """
        if __debug__: log_info("C: ACK_FAILURE")
        self._send_messages((ACK_FAILURE, ()))

        # Skip any ignored responses
        signature, fields = self._recv_message()
        while signature == IGNORED:
            if __debug__: log_info("S: IGNORED")
            signature, fields = self._recv_message()

        # Check the acknowledgement was successful
        data, = fields
        if signature == SUCCESS:
            if __debug__: log_info("S: SUCCESS %r", data)
        else:
            if __debug__: log_info("S: FAILURE %r", data)
            raise ProtocolError("Could not acknowledge failure")

    def close(self):
        """ Shut down and close the connection.
        """
        if __debug__: log_info("~~ [CLOSE]")
        socket = self.socket
        socket.shutdown(SHUT_RDWR)
        socket.close()

    @property
    def bench(self):
        if self._bench:
            return [Latency(return_ - init, end_recv - start_send, start_recv - end_send)
                    for init, start_send, end_send, start_recv, end_recv, return_ in self._bench]

class Driver(object):
    """ Accessor for a specific graph database resource.
    """

    def __init__(self, url, **config):
        self.url = url
        parsed = urlparse(self.url)
        if parsed.scheme == "bolt":
            self.host = parsed.hostname
            self.port = parsed.port
        else:
            raise ProtocolError("Unsupported URL scheme: %s" % parsed.scheme)
        self.config = config

    def session(self, **config):
        """ Connect and perform a handshake in order to return a valid
        Connection object, assuming a protocol version can be agreed.
        """
        config = dict(self.config, **config)

        # Establish a connection to the host and port specified
        host = self.host
        port = self.port or DEFAULT_PORT
        if __debug__: log_info("~~ [CONNECT] %s %d", host, port)
        s = create_connection((host, port))

        # Secure the connection if so requested
        try:
            secure = environ["NEO4J_SECURE"]
        except KeyError:
            secure = config.get("secure", False)
        if secure:
            if __debug__: log_info("~~ [SECURE] %s", host)
            s = secure_socket(s, host)

        # Send details of the protocol versions supported
        supported_versions = [1, 0, 0, 0]
        if __debug__: log_info("C: [HANDSHAKE] %r", supported_versions)
        data = b"".join(struct_pack(">I", version) for version in supported_versions)
        if __debug__: log_debug("C: %s", ":".join(map(hex2, data)))
        s.sendall(data)

        # Handle the handshake response
        ready_to_read, _, _ = select((s,), (), (), 0)
        while not ready_to_read:
            ready_to_read, _, _ = select((s,), (), (), 0)
        data = s.recv(4)
        data_size = len(data)
        if data_size == 0:
            # If no data is returned after a successful select
            # response, the server has closed the connection
            log_error("S: [CLOSE]")
            raise ProtocolError("Server closed connection without responding to handshake")
        if data_size == 4:
            if __debug__: log_debug("S: %s", ":".join(map(hex2, data)))
        else:
            # Some other garbled data has been received
            log_error("S: @*#!")
            raise ProtocolError("Expected four byte handshake response, received %r instead" % data)
        agreed_version, = struct_unpack(">I", data)
        if __debug__: log_info("S: [HANDSHAKE] %d", agreed_version)
        if agreed_version == 0:
            if __debug__: log_info("~~ [CLOSE]")
            s.shutdown(SHUT_RDWR)
            s.close()
        else:
            return SessionV1(s, **config)

class GraphDatabase(object):
    """ Top level accessor for all graph database functionality.
    """

    @classmethod
    def driver(cls, url, **config):
        """ Acquire a driver instance for the given URL and configuration.
        """
        return Driver(url, **config)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import os
import sys

from flask import Flask, request, session, g, redirect, url_for, abort, render_template, Blueprint, jsonify
from resources import Resources

public = Blueprint('public', __name__)
resource = Resources('static/resources', 'static/resources/system/settings')

@public.route('/')
def home():
    global resource
    lang = request.args.get('l') or 'bg'

    content = {
        "title": resource.title[lang],
        "heading": resource.heading[lang],
        "keywords": resource.keywords[lang],
        "description": resource.description[lang],
        "navigation": resource.get_navigations(lang),
        "sections": resource.get_sections(lang),
        "analytics": resource.analytics_tracking
    }

    return render_template('public/index.html', content=content)
<commit_msg>disabling analytics and other settings from template output<commit_after># -*- coding: utf-8 -*-
import os
import sys

from flask import Flask, request, session, g, redirect, url_for, abort, render_template, Blueprint, jsonify
from resources import Resources

public = Blueprint('public', __name__)
resource = Resources('static/resources', 'static/resources/system/settings')

@public.route('/')
def home():
    global resource
    lang = request.args.get('l') or 'bg'

    content = {
        "title": resource.title[lang],
        "heading": resource.heading[lang],
        "keywords": resource.keywords[lang],
        "description": resource.description[lang],
        "navigation": resource.get_navigations(lang),
        "pages": resource.get_pages(lang),
        #"sections": resource.get_sections(lang),
        #"analytics": resource.analytics_tracking
    }

    return render_template('public/index.html', content=content)
<|endoftext|>


<commit_before>from pox.core import core
from pox.messenger.messenger import *
from pox.lib.revent.revent import autoBindEvents
import logging
import traceback

log = core.getLogger()

# These attributes are copied verbatim from the log record
_attributes = [
  'created','filename','funcName','levelname','levelno','lineno',
  'module','msecs','name','pathname','process','processName',
  'relativeCreated','thread','threadName',
]

class LogMessenger (logging.Handler):
  """
  A Python logging.Handler that is a messenger service

  Accepts dictionaries with configuration info:
  KEY         VALUE
  level       Minimum log level to output (probably one of CRITICAL, ERROR,
              WARNING, INFO or DEBUG)
  format      fmt argument to logging.Formatter
  dateFormat  datefmt argument to logging.Formatter
  json        true if you want a bunch of attributes from the LogRecord to be
              included.  In some cases, these are stringized versions since the
              originals are objects and we don't pickle/jsonpickle them.
  """
  def __init__ (self, connection, params):
    logging.Handler.__init__(self)
    self.connection = connection
    connection._newlines = params.get("newlines", True) == True #HACK
    self._json = False
    self._format = False # Not valid, should never be set
    self._dateFormat = None
    if "format" not in params:
      params["format"] = None # Force update
    self._processParameters(params)
    if "opaque" in params:
      self._opaque = params["opaque"]
    else:
      self._opaque = None
    logging.getLogger().addHandler(self)
    self._listeners = autoBindEvents(self, connection) #GC?  Weak?

  def _processParameters (self, params):
    if "level" in params:
      self.setLevel(params["level"])
    if "json" in params:
      self._json = params['json']

    doFormat = False
    if "format" in params:
      fmt = params['format']
      if fmt is not self._format:
        self._format = fmt
        doFormat = True
    if "dateFormat" in params:
      dateFormat = params['dateFormat']
      if dateFormat is not self._dateFormat:
        self._dateFormat = dateFormat
        doFormat = True

    if doFormat:
      self.setFormatter(logging.Formatter(self._format, self._dateFormat))

  def _handle_MessageRecieved (self, event):
    if event.con.isReadable():
      r = event.con.read()
      if type(r) is dict:
        self._processParameters(r)
        if "bye" in r:
          event.con.close()

  def _handle_ConnectionClosed (self, event):
    logging.getLogger().removeHandler(self)

  def emit (self, record):
    o = {'message' : self.format(record)}
    if self._opaque is not None:
      o.update(self._opaque)
    #o['message'] = record.getMessage()
    if self._json:
      for attr in _attributes:
        o[attr] = getattr(record, attr)
      o['args'] = [str(x) for x in record.args]
      o['asctime'] = self.formatter.formatTime(record, self._dateFormat)
      if record.exc_info:
        o['exc_info'] = [str(record.exc_info[0]),
                         str(record.exc_info[1]),
                         traceback.format_tb(record.exc_info[2],1)]
        o['exc'] = traceback.format_exception(*record.exc_info)
    self.connection.send(o)

class LogMessengerListener (object):
  """
  Takes care of spawning individual LogMessengers

  Hello message is like:
  {"hello":"logger"}
  You can also include any of the config parameters for LogMessenger
  (like "level").
  """
  def __init__ (self):
    core.messenger.addListener(MessageRecieved, self._handle_global_MessageRecieved)

  def _handle_global_MessageRecieved (self, event):
    try:
      n = event.con.read()
      json = False
      if n['hello'] == 'logger':
        # It's for me!
        try:
          LogMessenger(event.con, n)
          event.claim()
        except:
          traceback.print_exc()
    except:
      pass

def launch ():
  def realStart (event=None):
    if not core.hasComponent("messenger"):
      if event is None:
        # Only do this the first time
        log.warning("Deferring firing up LogMessenger because Messenger isn't up yet")
      core.addListenerByName("ComponentRegistered", realStart, once=True)
      return
    global logMessengerListener
    logMessengerListener = LogMessengerListener()
    log.info("Up...")

  realStart()
<commit_msg>log_messenger: Better handling of record.args<commit_after>from pox.core import core
from pox.messenger.messenger import *
from pox.lib.revent.revent import autoBindEvents
import logging
import traceback

log = core.getLogger()

# These attributes are copied verbatim from the log record
_attributes = [
  'created','filename','funcName','levelname','levelno','lineno',
  'module','msecs','name','pathname','process','processName',
  'relativeCreated','thread','threadName','args',
]

class LogMessenger (logging.Handler):
  """
  A Python logging.Handler that is a messenger service

  Accepts dictionaries with configuration info:
  KEY         VALUE
  level       Minimum log level to output (probably one of CRITICAL, ERROR,
              WARNING, INFO or DEBUG)
  format      fmt argument to logging.Formatter
  dateFormat  datefmt argument to logging.Formatter
  json        true if you want a bunch of attributes from the LogRecord to be
              included.  In some cases, these are stringized versions since the
              originals are objects and we don't pickle/jsonpickle them.
  """
  def __init__ (self, connection, params):
    logging.Handler.__init__(self)
    self.connection = connection
    connection._newlines = params.get("newlines", True) == True #HACK
    self._json = False
    self._format = False # Not valid, should never be set
    self._dateFormat = None
    if "format" not in params:
      params["format"] = None # Force update
    self._processParameters(params)
    if "opaque" in params:
      self._opaque = params["opaque"]
    else:
      self._opaque = None
    logging.getLogger().addHandler(self)
    self._listeners = autoBindEvents(self, connection) #GC?  Weak?

  def _processParameters (self, params):
    if "level" in params:
      self.setLevel(params["level"])
    if "json" in params:
      self._json = params['json']

    doFormat = False
    if "format" in params:
      fmt = params['format']
      if fmt is not self._format:
        self._format = fmt
        doFormat = True
    if "dateFormat" in params:
      dateFormat = params['dateFormat']
      if dateFormat is not self._dateFormat:
        self._dateFormat = dateFormat
        doFormat = True

    if doFormat:
      self.setFormatter(logging.Formatter(self._format, self._dateFormat))

  def _handle_MessageRecieved (self, event):
    if event.con.isReadable():
      r = event.con.read()
      if type(r) is dict:
        self._processParameters(r)
        if "bye" in r:
          event.con.close()

  def _handle_ConnectionClosed (self, event):
    logging.getLogger().removeHandler(self)

  def emit (self, record):
    o = {'message' : self.format(record)}
    if self._opaque is not None:
      o.update(self._opaque)
    #o['message'] = record.getMessage()
    if self._json:
      for attr in _attributes:
        o[attr] = getattr(record, attr)
      o['asctime'] = self.formatter.formatTime(record, self._dateFormat)
      if record.exc_info:
        o['exc_info'] = [str(record.exc_info[0]),
                         str(record.exc_info[1]),
                         traceback.format_tb(record.exc_info[2],1)]
        o['exc'] = traceback.format_exception(*record.exc_info)
    self.connection.send(o, default=str)

class LogMessengerListener (object):
  """
  Takes care of spawning individual LogMessengers

  Hello message is like:
  {"hello":"logger"}
  You can also include any of the config parameters for LogMessenger
  (like "level").
  """
  def __init__ (self):
    core.messenger.addListener(MessageRecieved, self._handle_global_MessageRecieved)

  def _handle_global_MessageRecieved (self, event):
    try:
      n = event.con.read()
      json = False
      if n['hello'] == 'logger':
        # It's for me!
        try:
          LogMessenger(event.con, n)
          event.claim()
        except:
          traceback.print_exc()
    except:
      pass

def launch ():
  def realStart (event=None):
    if not core.hasComponent("messenger"):
      if event is None:
        # Only do this the first time
        log.warning("Deferring firing up LogMessenger because Messenger isn't up yet")
      core.addListenerByName("ComponentRegistered", realStart, once=True)
      return
    global logMessengerListener
    logMessengerListener = LogMessengerListener()
    log.info("Up...")

  realStart()
<|endoftext|>


<commit_before>import scrapy

from example.items import DmozItem

class DmozSpider(scrapy.Spider):
    name = "dmoz"
    allowed_domains = ["dmoz.org"]
    start_urls = [
    "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
    "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"
    ]

    def parse(self, response):
        for sel in response.xpath('//ul/li'):
            item = DmozItem()
            item['title'] = sel.xpath('a/text()').extract()
            item['link'] = sel.xpath('a/@href').extract()
            item['desc'] = sel.xpath('text()').extract()
            yield item

<commit_msg>Delete unnecessary example Spider<commit_after><|endoftext|>


<commit_before>#!/usr/bin/python

"""
This callback plugin verifies the required minimum version of Ansible
is installed for proper operation of the OpenShift Ansible Installer.
The plugin is named with leading `aa_` to ensure this plugin is loaded
first (alphanumerically) by Ansible.
"""
import sys
from pkg_resources import parse_version

from ansible import __version__
from ansible.plugins.callback import CallbackBase
from ansible.utils.display import Display

def display(*args, **kwargs):
    """Set up display function for Ansible v2"""
    display_instance = Display()
    display_instance.display(*args, **kwargs)

# Set to minimum required Ansible version
REQUIRED_VERSION = parse_version('2.7.8')
DESCRIPTION = "Supported versions: %s or newer" % REQUIRED_VERSION

class CallbackModule(CallbackBase):
    """
    Ansible callback plugin
    """

    CALLBACK_VERSION = 1.0
    CALLBACK_NAME = 'version_requirement'

    def __init__(self):
        """
        Version verification is performed in __init__ to catch the
        requirement early in the execution of Ansible and fail gracefully
        """
        super(CallbackModule, self).__init__()

        if not parse_version(__version__) >= REQUIRED_VERSION:
            display(
                'FATAL: Current Ansible version (%s) is not supported. %s'
                % (__version__, DESCRIPTION), color='red')
            sys.exit(1)
<commit_msg>Only use parse_version in compare<commit_after>#!/usr/bin/python

"""
This callback plugin verifies the required minimum version of Ansible
is installed for proper operation of the OpenShift Ansible Installer.
The plugin is named with leading `aa_` to ensure this plugin is loaded
first (alphanumerically) by Ansible.
"""
import sys
from pkg_resources import parse_version

from ansible import __version__
from ansible.plugins.callback import CallbackBase
from ansible.utils.display import Display

def display(*args, **kwargs):
    """Set up display function for Ansible v2"""
    display_instance = Display()
    display_instance.display(*args, **kwargs)

# Set to minimum required Ansible version
REQUIRED_VERSION = '2.7.8'
DESCRIPTION = "Supported versions: %s or newer" % REQUIRED_VERSION

class CallbackModule(CallbackBase):
    """
    Ansible callback plugin
    """

    CALLBACK_VERSION = 1.0
    CALLBACK_NAME = 'version_requirement'

    def __init__(self):
        """
        Version verification is performed in __init__ to catch the
        requirement early in the execution of Ansible and fail gracefully
        """
        super(CallbackModule, self).__init__()

        if not parse_version(__version__) >= parse_version(REQUIRED_VERSION):
            display(
                'FATAL: Current Ansible version (%s) is not supported. %s'
                % (__version__, DESCRIPTION), color='red')
            sys.exit(1)
<|endoftext|>


<commit_before>import re

# Two consecutive inserts where the first one matches this regular
# expression are merged together
COALESCE_RE = re.compile(r'^\S+$')

class _InsertDeleteOp(object):
    def __init__(self, start, end, text):
        self.start = start
        self.end = end
        self.text = text

    def _insert(self, buffer):
        start = buffer._get_iter_at_nr_pos(self.start)
        buffer.insert_interactive(start, self.text, True)
        buffer.place_cursor(start)

    def _delete(self, buffer):
        start = buffer._get_iter_at_nr_pos(self.start)
        end = buffer._get_iter_at_nr_pos(self.end)
        buffer.delete_interactive(start, end, True)
        buffer.place_cursor(start)

class InsertOp(_InsertDeleteOp):
    def redo(self, buffer):
        self._insert(buffer)

    def undo(self, buffer):
        self._delete(buffer)

    def __repr__(self):
        return "InsertOp(%s, %s, %s)" % (self.start, self.end, repr(self.text))

class DeleteOp(_InsertDeleteOp):
    def redo(self, buffer):
        self._delete(buffer)

    def undo(self, buffer):
        self._insert(buffer)

    def __repr__(self):
        return "DeleteOp(%s, %s, %s)" % (self.start, self.end, repr(self.text))

class BeginActionOp(object):
    def __repr__(self):
        return "BeginActionOp()"

class EndActionOp(object):
    def __repr__(self):
        return "EndActionOp()"

class UndoStack(object):
    def __init__(self, buffer):
        self.__buffer = buffer
        self.__position = 0
        # The position at which we last pruned the stack; everything after
        # this has been inserted consecutively without any intervening
        # undos and redos
        self.__prune_position = 0
        self.__stack = []
        self.__applying_undo = False
        self.__user_action_count = 0
        self.__action_ops = 0

    def __apply_op(self, op, reverse):
        self.__applying_undo = True
        try:
            self.__buffer._apply_undo_op(op, reverse)
        finally:
            self.__applying_undo = False

    def undo(self):
        if self.__position == 0:
            return

        self.__position -= 1

        self.__applying_undo = True
        try:
            if isinstance(self.__stack[self.__position], EndActionOp):
                self.__position -= 1
                while not isinstance(self.__stack[self.__position], BeginActionOp):
                    self.__stack[self.__position].undo(self.__buffer)
                    self.__position -= 1
            else:
                self.__stack[self.__position].undo(self.__buffer)
        finally:
            self.__applying_undo = False

    def redo(self):
        if self.__position == len(self.__stack):
            return

        self.__position += 1
        self.__applying_undo = True
        try:
            if isinstance(self.__stack[self.__position - 1], BeginActionOp):
                self.__position += 1
                while not isinstance(self.__stack[self.__position - 1], EndActionOp):
                    self.__stack[self.__position - 1].redo(self.__buffer)
                    self.__position += 1
            else:
                self.__stack[self.__position - 1].redo(self.__buffer)
        finally:
            self.__applying_undo = False

    def __check_coalesce(self):
        assert self.__position == len(self.__stack)

        # Don't coalesce two ops unless they are actually adjacent in time
        if self.__position < self.__prune_position + 2:
            return

        cur = self.__stack[-1]
        prev = self.__stack[-2]
        if isinstance(cur, InsertOp) and isinstance(prev, InsertOp) and \
                cur.start == prev.end and COALESCE_RE.match(prev.text):
            prev.end = cur.end
            prev.text += cur.text
            self.__stack.pop()
            self.__position -= 1

    def append_op(self, op):
        if self.__applying_undo:
            return

        if self.__position < len(self.__stack):
            assert self.__action_ops == 0
            self.__stack[self.__position:] = []
            self.__prune_position = self.__position

        if self.__user_action_count > 0:
            self.__action_ops += 1
        else:
            self.__check_coalesce()

        self.__stack.append(op)
        self.__position += 1

    def begin_user_action(self):
        self.__user_action_count += 1

    def end_user_action(self):
        self.__user_action_count -= 1
        if self.__user_action_count == 0:
            if self.__action_ops > 1:
                self.__stack.insert(len(self.__stack) - self.__action_ops, BeginActionOp())
                self.__stack.append(EndActionOp())
                self.__position += 2
            elif self.__action_ops == 1:
                self.__check_coalesce()
            self.__action_ops = 0

    def clear(self):
        self.__stack = []
        self.__position = 0

    def __repr__(self):
        return "UndoStack(stack=%s, position=%d)" % (self.__stack, self.__position)

<commit_msg>Improve the undo-coalescing not to coalesce newlines onto previous text<commit_after>import re

# Two consecutive inserts are merged together if the sum of the
# two matches this. The (?!\n) is to defeat the normal regular
# expression behavior where 'a$' matches 'a\n' because $ matches
# before the last newline in the string
COALESCE_RE = re.compile(r'^\S+ *(?!\n)$')

class _InsertDeleteOp(object):
    def __init__(self, start, end, text):
        self.start = start
        self.end = end
        self.text = text

    def _insert(self, buffer):
        start = buffer._get_iter_at_nr_pos(self.start)
        buffer.insert_interactive(start, self.text, True)
        buffer.place_cursor(start)

    def _delete(self, buffer):
        start = buffer._get_iter_at_nr_pos(self.start)
        end = buffer._get_iter_at_nr_pos(self.end)
        buffer.delete_interactive(start, end, True)
        buffer.place_cursor(start)

class InsertOp(_InsertDeleteOp):
    def redo(self, buffer):
        self._insert(buffer)

    def undo(self, buffer):
        self._delete(buffer)

    def __repr__(self):
        return "InsertOp(%s, %s, %s)" % (self.start, self.end, repr(self.text))

class DeleteOp(_InsertDeleteOp):
    def redo(self, buffer):
        self._delete(buffer)

    def undo(self, buffer):
        self._insert(buffer)

    def __repr__(self):
        return "DeleteOp(%s, %s, %s)" % (self.start, self.end, repr(self.text))

class BeginActionOp(object):
    def __repr__(self):
        return "BeginActionOp()"

class EndActionOp(object):
    def __repr__(self):
        return "EndActionOp()"

class UndoStack(object):
    def __init__(self, buffer):
        self.__buffer = buffer
        self.__position = 0
        # The position at which we last pruned the stack; everything after
        # this has been inserted consecutively without any intervening
        # undos and redos
        self.__prune_position = 0
        self.__stack = []
        self.__applying_undo = False
        self.__user_action_count = 0
        self.__action_ops = 0

    def __apply_op(self, op, reverse):
        self.__applying_undo = True
        try:
            self.__buffer._apply_undo_op(op, reverse)
        finally:
            self.__applying_undo = False

    def undo(self):
        if self.__position == 0:
            return

        self.__position -= 1

        self.__applying_undo = True
        try:
            if isinstance(self.__stack[self.__position], EndActionOp):
                self.__position -= 1
                while not isinstance(self.__stack[self.__position], BeginActionOp):
                    self.__stack[self.__position].undo(self.__buffer)
                    self.__position -= 1
            else:
                self.__stack[self.__position].undo(self.__buffer)
        finally:
            self.__applying_undo = False

    def redo(self):
        if self.__position == len(self.__stack):
            return

        self.__position += 1
        self.__applying_undo = True
        try:
            if isinstance(self.__stack[self.__position - 1], BeginActionOp):
                self.__position += 1
                while not isinstance(self.__stack[self.__position - 1], EndActionOp):
                    self.__stack[self.__position - 1].redo(self.__buffer)
                    self.__position += 1
            else:
                self.__stack[self.__position - 1].redo(self.__buffer)
        finally:
            self.__applying_undo = False

    def __check_coalesce(self):
        assert self.__position == len(self.__stack)

        # Don't coalesce two ops unless they are actually adjacent in time
        if self.__position < self.__prune_position + 2:
            return

        cur = self.__stack[-1]
        prev = self.__stack[-2]
        if isinstance(cur, InsertOp) and isinstance(prev, InsertOp) and \
                cur.start == prev.end and COALESCE_RE.match(prev.text + cur.text):
            prev.end = cur.end
            prev.text += cur.text
            self.__stack.pop()
            self.__position -= 1

    def append_op(self, op):
        if self.__applying_undo:
            return

        if self.__position < len(self.__stack):
            assert self.__action_ops == 0
            self.__stack[self.__position:] = []
            self.__prune_position = self.__position

        if self.__user_action_count > 0:
            self.__action_ops += 1
        else:
            self.__check_coalesce()

        self.__stack.append(op)
        self.__position += 1

    def begin_user_action(self):
        self.__user_action_count += 1

    def end_user_action(self):
        self.__user_action_count -= 1
        if self.__user_action_count == 0:
            if self.__action_ops > 1:
                self.__stack.insert(len(self.__stack) - self.__action_ops, BeginActionOp())
                self.__stack.append(EndActionOp())
                self.__position += 2
            elif self.__action_ops == 1:
                self.__check_coalesce()
            self.__action_ops = 0

    def clear(self):
        self.__stack = []
        self.__position = 0

    def __repr__(self):
        return "UndoStack(stack=%s, position=%d)" % (self.__stack, self.__position)

<|endoftext|>