<commit_before>from django.shortcuts import render, redirect, get_object_or_404, reverse
from django.core.exceptions import PermissionDenied
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.conf import settings

from ..models import Site
from ..forms import SiteForm, ProcessForm
from ..helpers import (reload_services, delete_site_files, create_config_files, create_process_config, restart_supervisor,
                       get_supervisor_status, delete_process_config, write_new_index_file, get_latest_commit, reload_nginx_config,
                       list_executable_files, update_supervisor)

from ...vms.models import VirtualMachine
from ...request.models import SiteRequest

from ....utils.emails import send_new_site_email

@login_required
def create_view(request):
    # Redirect the user if any approvals need to be done.
    if request.user.is_staff:
        if request.user.is_superuser and SiteRequest.objects.filter(teacher_approval=True, admin_approval__isnull=True).exists():
            return redirect("admin_site")
        if request.user.site_requests.filter(teacher_approval__isnull=True).exists():
            return redirect("approve_site")

    if request.method == "POST":
        form = SiteForm(request.POST, user=request.user)
        if form.is_valid():
            site = form.save()
            for user in site.group.users.filter(service=False):
                if not user == request.user:
                    send_new_site_email(user, site)
            if not site.category == "dynamic":
                write_new_index_file(site)
            reload_services()
            return redirect("info_site", site_id=site.id)
    else:
        form = SiteForm(user=request.user)

    context = {
        "form": form,
        "site": None,
        "project_domain": settings.PROJECT_DOMAIN
    }
    return render(request, "sites/create_site.html", context)

@login_required
def edit_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)
    if not request.user.is_superuser and not site.group.users.filter(id=request.user.id).exists():
        raise PermissionDenied
    if request.method == "POST":
        current_members = list(site.group.users.filter(service=False).values_list('id', flat=True))
        form = SiteForm(request.POST, instance=site, user=request.user)
        if form.is_valid():
            site = form.save()
            for user in site.group.users.filter(service=False).exclude(id__in=current_members):
                send_new_site_email(user, site)
            reload_services()
            return redirect("info_site", site_id=site_id)
    else:
        form = SiteForm(instance=site, user=request.user)
    context = {
        "form": form,
        "site": site,
        "project_domain": settings.PROJECT_DOMAIN
    }
    return render(request, "sites/create_site.html", context)

@login_required
def delete_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)

    if not request.user.is_superuser and not (site.purpose == "project" and site.group.users.filter(id=request.user.id).exists()):
        raise PermissionDenied

    if request.method == "POST":
        if not request.POST.get("confirm", None) == site.name:
            messages.error(request, "Delete confirmation failed!")
            return redirect("delete_site", site_id=site_id)
        site.delete()
        messages.success(request, "Site {} deleted!".format(site.name))
        return redirect("index")
    context = {
        "site": site
    }
    return render(request, "sites/delete_site.html", context)

@login_required
def process_status_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)
    if not request.user.is_superuser and not site.group.users.filter(id=request.user.id).exists():
        raise PermissionDenied

    return HttpResponse(get_supervisor_status(site))

@login_required
def modify_process_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)
    if not request.user.is_superuser and not site.group.users.filter(id=request.user.id).exists():
        raise PermissionDenied
    if site.category != "dynamic":
        messages.error(request, "You must set your site type to dynamic before adding a process.")
        return redirect("info_site", site_id=site_id)
    if request.method == "POST":
        try:
            form = ProcessForm(request.user, request.POST, instance=site.process)
        except Site.process.RelatedObjectDoesNotExist:
            form = ProcessForm(request.user, request.POST, initial={"site": site.id})
        if form.is_valid():
            proc = form.save()
            create_process_config(proc)
            update_supervisor()
            messages.success(request, "Process modified!")
            return redirect("info_site", site_id=proc.site.id)
    else:
        try:
            form = ProcessForm(request.user, instance=site.process)
        except Site.process.RelatedObjectDoesNotExist:
            form = ProcessForm(request.user, initial={"site": site.id})
    context = {
        "form": form,
        "site": site,
        "files": list_executable_files(site.path, level=3)
    }
    return render(request, "sites/create_process.html", context)

@login_required
def modify_vm_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)
    if not request.user.is_superuser and not site.group.users.filter(id=request.user.id).exists():
        raise PermissionDenied
    if not site.category == "vm":
        messages.error(request, "Not a VM site!")
        return redirect("info_site", site_id=site.id)

    if request.method == "POST":
        vm = request.POST.get("vm", None)
        if hasattr(site, "virtual_machine"):
            current_vm = site.virtual_machine
            current_vm.site = None
            current_vm.save()
        if vm is not None and vm != "__blank__":
            vm = int(vm)
            new_vm = VirtualMachine.objects.get(id=vm)
            new_vm.site = site
            new_vm.save()

        create_config_files(site)
        reload_nginx_config()

        messages.success(request, "Virtual machine successfully linked!")
        return redirect("info_site", site_id=site.id)

    if request.user.is_superuser:
        vms = VirtualMachine.objects.all().order_by("name")
    else:
        vms = VirtualMachine.objects.filter(users=request.user).order_by("name")

    context = {
        "site": site,
        "vms": vms
    }
    return render(request, "sites/edit_vm.html", context)

@login_required
def delete_process_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)
    if not request.user.is_superuser and not site.group.users.filter(id=request.user.id).exists():
        raise PermissionDenied
    if request.method == "POST":
        try:
            delete_process_config(site.process)
            update_supervisor()
            site.process.delete()
            messages.success(request, "Process deleted!")
        except Site.process.RelatedObjectDoesNotExist:
            messages.error(request, "Process not found.")
        return redirect("info_site", site_id=site.id)
    else:
        return render(request, "sites/delete_process.html", {"site": site})

@login_required
def restart_process_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)
    if not request.user.is_superuser and not site.group.users.filter(id=request.user.id).exists():
        raise PermissionDenied

    restart_supervisor(site)

    messages.success(request, "Restarted supervisor application!")
    return redirect("info_site", site_id=site_id)

@login_required
def info_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)
    if not request.user.is_superuser and not site.group.users.filter(id=request.user.id).exists():
        raise PermissionDenied

    context = {
        "site": site,
        "users": site.group.users.filter(service=False).order_by("username"),
        "status": get_supervisor_status(site),
        "latest_commit": get_latest_commit(site),
        "webhook_url": request.build_absolute_uri(reverse("git_webhook", kwargs={"site_id": site_id})).replace("http://", "https://")
    }
    return render(request, "sites/info_site.html", context)
<commit_msg>fix build<commit_after>from django.shortcuts import render, redirect, get_object_or_404, reverse
from django.core.exceptions import PermissionDenied
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.conf import settings

from ..models import Site
from ..forms import SiteForm, ProcessForm
from ..helpers import (reload_services, update_supervisor, create_config_files, create_process_config, restart_supervisor,
                       get_supervisor_status, delete_process_config, write_new_index_file, get_latest_commit, reload_nginx_config,
                       list_executable_files)

from ...vms.models import VirtualMachine
from ...request.models import SiteRequest

from ....utils.emails import send_new_site_email

@login_required
def create_view(request):
    # Redirect the user if any approvals need to be done.
    if request.user.is_staff:
        if request.user.is_superuser and SiteRequest.objects.filter(teacher_approval=True, admin_approval__isnull=True).exists():
            return redirect("admin_site")
        if request.user.site_requests.filter(teacher_approval__isnull=True).exists():
            return redirect("approve_site")

    if request.method == "POST":
        form = SiteForm(request.POST, user=request.user)
        if form.is_valid():
            site = form.save()
            for user in site.group.users.filter(service=False):
                if not user == request.user:
                    send_new_site_email(user, site)
            if not site.category == "dynamic":
                write_new_index_file(site)
            reload_services()
            return redirect("info_site", site_id=site.id)
    else:
        form = SiteForm(user=request.user)

    context = {
        "form": form,
        "site": None,
        "project_domain": settings.PROJECT_DOMAIN
    }
    return render(request, "sites/create_site.html", context)

@login_required
def edit_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)
    if not request.user.is_superuser and not site.group.users.filter(id=request.user.id).exists():
        raise PermissionDenied
    if request.method == "POST":
        current_members = list(site.group.users.filter(service=False).values_list('id', flat=True))
        form = SiteForm(request.POST, instance=site, user=request.user)
        if form.is_valid():
            site = form.save()
            for user in site.group.users.filter(service=False).exclude(id__in=current_members):
                send_new_site_email(user, site)
            reload_services()
            return redirect("info_site", site_id=site_id)
    else:
        form = SiteForm(instance=site, user=request.user)
    context = {
        "form": form,
        "site": site,
        "project_domain": settings.PROJECT_DOMAIN
    }
    return render(request, "sites/create_site.html", context)

@login_required
def delete_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)

    if not request.user.is_superuser and not (site.purpose == "project" and site.group.users.filter(id=request.user.id).exists()):
        raise PermissionDenied

    if request.method == "POST":
        if not request.POST.get("confirm", None) == site.name:
            messages.error(request, "Delete confirmation failed!")
            return redirect("delete_site", site_id=site_id)
        site.delete()
        messages.success(request, "Site {} deleted!".format(site.name))
        return redirect("index")
    context = {
        "site": site
    }
    return render(request, "sites/delete_site.html", context)

@login_required
def process_status_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)
    if not request.user.is_superuser and not site.group.users.filter(id=request.user.id).exists():
        raise PermissionDenied

    return HttpResponse(get_supervisor_status(site))

@login_required
def modify_process_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)
    if not request.user.is_superuser and not site.group.users.filter(id=request.user.id).exists():
        raise PermissionDenied
    if site.category != "dynamic":
        messages.error(request, "You must set your site type to dynamic before adding a process.")
        return redirect("info_site", site_id=site_id)
    if request.method == "POST":
        try:
            form = ProcessForm(request.user, request.POST, instance=site.process)
        except Site.process.RelatedObjectDoesNotExist:
            form = ProcessForm(request.user, request.POST, initial={"site": site.id})
        if form.is_valid():
            proc = form.save()
            create_process_config(proc)
            update_supervisor()
            messages.success(request, "Process modified!")
            return redirect("info_site", site_id=proc.site.id)
    else:
        try:
            form = ProcessForm(request.user, instance=site.process)
        except Site.process.RelatedObjectDoesNotExist:
            form = ProcessForm(request.user, initial={"site": site.id})
    context = {
        "form": form,
        "site": site,
        "files": list_executable_files(site.path, level=3)
    }
    return render(request, "sites/create_process.html", context)

@login_required
def modify_vm_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)
    if not request.user.is_superuser and not site.group.users.filter(id=request.user.id).exists():
        raise PermissionDenied
    if not site.category == "vm":
        messages.error(request, "Not a VM site!")
        return redirect("info_site", site_id=site.id)

    if request.method == "POST":
        vm = request.POST.get("vm", None)
        if hasattr(site, "virtual_machine"):
            current_vm = site.virtual_machine
            current_vm.site = None
            current_vm.save()
        if vm is not None and vm != "__blank__":
            vm = int(vm)
            new_vm = VirtualMachine.objects.get(id=vm)
            new_vm.site = site
            new_vm.save()

        create_config_files(site)
        reload_nginx_config()

        messages.success(request, "Virtual machine successfully linked!")
        return redirect("info_site", site_id=site.id)

    if request.user.is_superuser:
        vms = VirtualMachine.objects.all().order_by("name")
    else:
        vms = VirtualMachine.objects.filter(users=request.user).order_by("name")

    context = {
        "site": site,
        "vms": vms
    }
    return render(request, "sites/edit_vm.html", context)

@login_required
def delete_process_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)
    if not request.user.is_superuser and not site.group.users.filter(id=request.user.id).exists():
        raise PermissionDenied
    if request.method == "POST":
        try:
            delete_process_config(site.process)
            update_supervisor()
            site.process.delete()
            messages.success(request, "Process deleted!")
        except Site.process.RelatedObjectDoesNotExist:
            messages.error(request, "Process not found.")
        return redirect("info_site", site_id=site.id)
    else:
        return render(request, "sites/delete_process.html", {"site": site})

@login_required
def restart_process_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)
    if not request.user.is_superuser and not site.group.users.filter(id=request.user.id).exists():
        raise PermissionDenied

    restart_supervisor(site)

    messages.success(request, "Restarted supervisor application!")
    return redirect("info_site", site_id=site_id)

@login_required
def info_view(request, site_id):
    site = get_object_or_404(Site, id=site_id)
    if not request.user.is_superuser and not site.group.users.filter(id=request.user.id).exists():
        raise PermissionDenied

    context = {
        "site": site,
        "users": site.group.users.filter(service=False).order_by("username"),
        "status": get_supervisor_status(site),
        "latest_commit": get_latest_commit(site),
        "webhook_url": request.build_absolute_uri(reverse("git_webhook", kwargs={"site_id": site_id})).replace("http://", "https://")
    }
    return render(request, "sites/info_site.html", context)
<|endoftext|>


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

# Copyright(C) 2008-2011  Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.

import socket
from datetime import datetime
from dateutil.parser import parse as parse_dt

from weboob.tools.ordereddict import OrderedDict
from weboob.capabilities.contact import Contact as _Contact, ProfileNode
from weboob.tools.misc import html2text

class FieldBase:
    def __init__(self, key, key2=None):
        self.key = key
        self.key2 = key2

    def get_value(self, value, consts):
        raise NotImplementedError

class FieldStr(FieldBase):
    def get_value(self, profile, consts):
        return html2text(unicode(profile[self.key])).strip()

class FieldBool(FieldBase):
    def get_value(self, profile, consts):
        return bool(int(profile[self.key]))

class FieldDist(FieldBase):
    def get_value(self, profile, consts):
        return '%.2f km' % float(profile[self.key])

class FieldIP(FieldBase):
    def get_hostname(self, s):
        try:
            return socket.gethostbyaddr(s)[0]
        except (socket.gaierror, socket.herror):
            return s

    def get_value(self, profile, consts):
        s = self.get_hostname(profile[self.key])
        if profile[self.key] != profile[self.key2]:
            s += ' (first %s)' % self.get_hostname(profile[self.key2])
        return s

class FieldProfileURL(FieldBase):
    def get_value(self, profile, consts):
        id = int(profile[self.key])
        if id > 0:
            return 'http://www.adopteunmec.com/index.php/profile/%d' % id
        else:
            return ''

class FieldPopu(FieldBase):
    def get_value(self, profile, consts):
        return unicode(profile['popu'][self.key])

class FieldOld(FieldBase):
    def get_value(self, profile, consts):
        birthday = parse_dt(profile[self.key])
        return int((datetime.now() - birthday).days / 365.25)

class FieldSplit(FieldBase):
    def get_value(self, profile, consts):
        return [html2text(s).strip() for s in profile[self.key].split(self.key2) if len(s.strip()) > 0]

class FieldBMI(FieldBase):
    def __init__(self, key, key2, fat=False):
        FieldBase.__init__(self, key, key2)
        self.fat = fat

    def get_value(self, profile, consts):
        height = int(profile[self.key])
        weight = int(profile[self.key2])
        if height == 0 or weight == 0:
            return ''

        bmi = (weight/float(pow(height/100.0, 2)))
        if not self.fat:
            return bmi
        elif bmi < 15.5:
            return 'severely underweight'
        elif bmi < 18.4:
            return 'underweight'
        elif bmi < 24.9:
            return 'normal'
        elif bmi < 30:
            return 'overweight'
        else:
            return 'obese'

class FieldFlags(FieldBase):
    def get_value(self, profile, consts):
        i = int(profile[self.key])
        labels = []
        for d in consts[self.key]:
            if i & (1 << int(d['value'])):
                labels.append(html2text(d['label']).strip())
        return labels

class FieldList(FieldBase):
    def get_value(self, profile, consts):
        i = int(profile[self.key])
        for d in consts[self.key]:
            if i == int(d['value']):
                return html2text(d['label']).strip()
        return ''

class Contact(_Contact):
    TABLE = OrderedDict((
                 ('_info',        OrderedDict((
                                    ('title',               FieldStr('title')),
                                    ('IPaddr',              FieldIP('last_ip', 'first_ip')),
                                    ('admin',               FieldBool('admin')),
                                    ('ban',                 FieldBool('isBan')),
                                    ('first',               FieldStr('first_cnx')),
                                    ('godfather',           FieldProfileURL('godfather')),
                                  ))),
                 ('_stats',       OrderedDict((
                                    ('mails',               FieldPopu('mails')),
                                    ('baskets',             FieldPopu('contacts')),
                                    ('charms',              FieldPopu('flashs')),
                                    ('visites',             FieldPopu('visites')),
                                    ('invits',              FieldPopu('invits')),
                                    ('bonus',               FieldPopu('bonus')),
                                    ('score',               FieldPopu('popu')),
                                  ))),
                 ('details',      OrderedDict((
                                    ('old',                 FieldOld('birthday')),
                                    ('birthday',            FieldStr('birthday')),
                                    ('zipcode',             FieldStr('zip')),
                                    ('location',            FieldStr('city')),
                                    ('distance',            FieldDist('dist')),
                                    ('country',             FieldStr('country')),
                                    ('phone',               FieldStr('phone')),
                                    ('eyes',                FieldList('eyes')),
                                    ('hair_color',          FieldList('hair_color')),
                                    ('hair_size',           FieldList('hair_size')),
                                    ('height',              FieldList('size')),
                                    ('weight',              FieldList('weight')),
                                    ('BMI',                 FieldBMI('size', 'weight')),
                                    ('fat',                 FieldBMI('size', 'weight', fat=True)),
                                    ('shape',               FieldList('shape')),
                                    ('origins',             FieldList('origins')),
                                    ('signs',               FieldFlags('checks1')),
                                    ('job',                 FieldStr('job')),
                                    ('style',               FieldList('style')),
                                    ('food',                FieldList('food')),
                                    ('drink',               FieldList('drink')),
                                    ('smoke',               FieldList('smoke')),
                                  ))),
                ('tastes',        OrderedDict((
                                    ('hobbies',             FieldStr('hobbies')),
                                    ('music',               FieldSplit('music', '
')),
                                    ('cinema',              FieldSplit('cinema', '
')),
                                    ('books',               FieldSplit('books', '
')),
                                    ('tv',                  FieldSplit('tvs', '
')),
                                  ))),
                ('sex',           OrderedDict((
                                    ('underwear',           FieldFlags('checks7')),
                                    ('practices',           FieldFlags('checks5')),
                                    ('favorite',            FieldFlags('checks3')),
                                    ('toys',                FieldFlags('checks6')),
                                  ))),
                ('personality',   OrderedDict((
                                    ('snap',                FieldStr('texts1')),
                                    ('exciting',            FieldStr('texts2')),
                                    ('hate',                FieldStr('texts3')),
                                    ('vices',               FieldStr('texts4')),
                                    ('assets',              FieldStr('texts6')),
                                    ('fantasies',           FieldStr('texts5')),
                                    ('is',                  FieldFlags('checks2')),
                                  )))
        ))

    def parse_profile(self, profile, consts):
        if int(profile['cat']) == 1:
            self.status = Contact.STATUS_ONLINE
            self.status_msg = u'online'
            self.status_msg = u'since %s' % profile['last_cnx']
        elif int(profile['cat']) == 2:
            self.status = Contact.STATUS_AWAY
            self.status_msg = u'away'
            self.status_msg = u'connection at %s' % profile['last_cnx']
        elif int(profile['cat']) == 3:
            self.status = Contact.STATUS_OFFLINE
            self.status_msg = u'last connection %s' % profile['last_cnx']

        self.summary = html2text(profile['about1']).strip().replace('\n\n', '\n')
        if len(profile['about2']) > 0:
            self.summary += u'\n\nLooking for:\n%s' % html2text(profile['about2']).strip().replace('\n\n', '\n')

        for photo in profile['pictures']:
            self.set_photo(photo['url'].split('/')[-1],
                              url=photo['url'],
                              thumbnail_url=photo['url'].replace('image', 'thumb1_'),
                              hidden=photo['hidden'])
        self.profile = OrderedDict()

        for section, d in self.TABLE.iteritems():
            flags = ProfileNode.SECTION
            if section.startswith('_'):
                flags |= ProfileNode.HEAD
                section = section.lstrip('_')
            s = ProfileNode(section, section.capitalize(), OrderedDict(), flags=flags)

            for key, builder in d.iteritems():
                value = builder.get_value(profile, consts[int(profile['sex'])])
                s.value[key] = ProfileNode(key, key.capitalize().replace('_', ' '), value)

            self.profile[section] = s

        self.aum_profile = profile

    def get_text(self):
        def print_node(node, level=1):
            result = u''
            if node.flags & node.SECTION:
                result += u'\t' * level + node.label + '\n'
                for sub in node.value.itervalues():
                    result += print_node(sub, level+1)
            else:
                if isinstance(node.value, (tuple,list)):
                    value = ', '.join(unicode(v) for v in node.value)
                elif isinstance(node.value, float):
                    value = '%.2f' % node.value
                else:
                    value = node.value
                result += u'\t' * level + u'%-20s %s\n' % (node.label + ':', value)
            return result

        result = u'Nickname: %s\n' % self.name
        if self.status & Contact.STATUS_ONLINE:
            s = 'online'
        elif self.status & Contact.STATUS_OFFLINE:
            s = 'offline'
        elif self.status & Contact.STATUS_AWAY:
            s = 'away'
        else:
            s = 'unknown'
        result += u'Status: %s (%s)\n' % (s, self.status_msg)
        result += u'URL: %s\n' % self.url
        result += u'Photos:\n'
        for name, photo in self.photos.iteritems():
            result += u'\t%s%s\n' % (photo, ' (hidden)' if photo.hidden else '')
        result += u'\nProfile:\n'
        for head in self.profile.itervalues():
            result += print_node(head)
        result += u'Description:\n'
        for s in self.summary.split('\n'):
            result += u'\t%s\n' % s
        return result
<commit_msg>add a 'ratio' field<commit_after># -*- coding: utf-8 -*-

# Copyright(C) 2008-2011  Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.

import socket
from datetime import datetime
from dateutil.parser import parse as parse_dt

from weboob.tools.ordereddict import OrderedDict
from weboob.capabilities.contact import Contact as _Contact, ProfileNode
from weboob.tools.misc import html2text

class FieldBase:
    def __init__(self, key, key2=None):
        self.key = key
        self.key2 = key2

    def get_value(self, value, consts):
        raise NotImplementedError

class FieldStr(FieldBase):
    def get_value(self, profile, consts):
        return html2text(unicode(profile[self.key])).strip()

class FieldBool(FieldBase):
    def get_value(self, profile, consts):
        return bool(int(profile[self.key]))

class FieldDist(FieldBase):
    def get_value(self, profile, consts):
        return '%.2f km' % float(profile[self.key])

class FieldIP(FieldBase):
    def get_hostname(self, s):
        try:
            return socket.gethostbyaddr(s)[0]
        except (socket.gaierror, socket.herror):
            return s

    def get_value(self, profile, consts):
        s = self.get_hostname(profile[self.key])
        if profile[self.key] != profile[self.key2]:
            s += ' (first %s)' % self.get_hostname(profile[self.key2])
        return s

class FieldProfileURL(FieldBase):
    def get_value(self, profile, consts):
        id = int(profile[self.key])
        if id > 0:
            return 'http://www.adopteunmec.com/index.php/profile/%d' % id
        else:
            return ''

class FieldPopu(FieldBase):
    def get_value(self, profile, consts):
        return unicode(profile['popu'][self.key])

class FieldPopuRatio(FieldBase):
    def get_value(self, profile, consts):
        v1 = float(profile['popu'][self.key])
        v2 = float(profile['popu'][self.key2])
        if v2 == 0.0:
            return 'NaN'
        else:
            return '%.2f' % (v1 / v2)

class FieldOld(FieldBase):
    def get_value(self, profile, consts):
        birthday = parse_dt(profile[self.key])
        return int((datetime.now() - birthday).days / 365.25)

class FieldSplit(FieldBase):
    def get_value(self, profile, consts):
        return [html2text(s).strip() for s in profile[self.key].split(self.key2) if len(s.strip()) > 0]

class FieldBMI(FieldBase):
    def __init__(self, key, key2, fat=False):
        FieldBase.__init__(self, key, key2)
        self.fat = fat

    def get_value(self, profile, consts):
        height = int(profile[self.key])
        weight = int(profile[self.key2])
        if height == 0 or weight == 0:
            return ''

        bmi = (weight/float(pow(height/100.0, 2)))
        if not self.fat:
            return bmi
        elif bmi < 15.5:
            return 'severely underweight'
        elif bmi < 18.4:
            return 'underweight'
        elif bmi < 24.9:
            return 'normal'
        elif bmi < 30:
            return 'overweight'
        else:
            return 'obese'

class FieldFlags(FieldBase):
    def get_value(self, profile, consts):
        i = int(profile[self.key])
        labels = []
        for d in consts[self.key]:
            if i & (1 << int(d['value'])):
                labels.append(html2text(d['label']).strip())
        return labels

class FieldList(FieldBase):
    def get_value(self, profile, consts):
        i = int(profile[self.key])
        for d in consts[self.key]:
            if i == int(d['value']):
                return html2text(d['label']).strip()
        return ''

class Contact(_Contact):
    TABLE = OrderedDict((
                 ('_info',        OrderedDict((
                                    ('title',               FieldStr('title')),
                                    ('IPaddr',              FieldIP('last_ip', 'first_ip')),
                                    ('admin',               FieldBool('admin')),
                                    ('ban',                 FieldBool('isBan')),
                                    ('first',               FieldStr('first_cnx')),
                                    ('godfather',           FieldProfileURL('godfather')),
                                  ))),
                 ('_stats',       OrderedDict((
                                    ('mails',               FieldPopu('mails')),
                                    ('baskets',             FieldPopu('contacts')),
                                    ('charms',              FieldPopu('flashs')),
                                    ('visites',             FieldPopu('visites')),
                                    ('invits',              FieldPopu('invits')),
                                    ('bonus',               FieldPopu('bonus')),
                                    ('score',               FieldPopu('popu')),
                                    ('ratio',               FieldPopuRatio('mails', 'flashs')),
                                  ))),
                 ('details',      OrderedDict((
                                    ('old',                 FieldOld('birthday')),
                                    ('birthday',            FieldStr('birthday')),
                                    ('zipcode',             FieldStr('zip')),
                                    ('location',            FieldStr('city')),
                                    ('distance',            FieldDist('dist')),
                                    ('country',             FieldStr('country')),
                                    ('phone',               FieldStr('phone')),
                                    ('eyes',                FieldList('eyes')),
                                    ('hair_color',          FieldList('hair_color')),
                                    ('hair_size',           FieldList('hair_size')),
                                    ('height',              FieldList('size')),
                                    ('weight',              FieldList('weight')),
                                    ('BMI',                 FieldBMI('size', 'weight')),
                                    ('fat',                 FieldBMI('size', 'weight', fat=True)),
                                    ('shape',               FieldList('shape')),
                                    ('origins',             FieldList('origins')),
                                    ('signs',               FieldFlags('checks1')),
                                    ('job',                 FieldStr('job')),
                                    ('style',               FieldList('style')),
                                    ('food',                FieldList('food')),
                                    ('drink',               FieldList('drink')),
                                    ('smoke',               FieldList('smoke')),
                                  ))),
                ('tastes',        OrderedDict((
                                    ('hobbies',             FieldStr('hobbies')),
                                    ('music',               FieldSplit('music', '
')),
                                    ('cinema',              FieldSplit('cinema', '
')),
                                    ('books',               FieldSplit('books', '
')),
                                    ('tv',                  FieldSplit('tvs', '
')),
                                  ))),
                ('sex',           OrderedDict((
                                    ('underwear',           FieldFlags('checks7')),
                                    ('practices',           FieldFlags('checks5')),
                                    ('favorite',            FieldFlags('checks3')),
                                    ('toys',                FieldFlags('checks6')),
                                  ))),
                ('personality',   OrderedDict((
                                    ('snap',                FieldStr('texts1')),
                                    ('exciting',            FieldStr('texts2')),
                                    ('hate',                FieldStr('texts3')),
                                    ('vices',               FieldStr('texts4')),
                                    ('assets',              FieldStr('texts6')),
                                    ('fantasies',           FieldStr('texts5')),
                                    ('is',                  FieldFlags('checks2')),
                                  )))
        ))

    def parse_profile(self, profile, consts):
        if int(profile['cat']) == 1:
            self.status = Contact.STATUS_ONLINE
            self.status_msg = u'online'
            self.status_msg = u'since %s' % profile['last_cnx']
        elif int(profile['cat']) == 2:
            self.status = Contact.STATUS_AWAY
            self.status_msg = u'away'
            self.status_msg = u'connection at %s' % profile['last_cnx']
        elif int(profile['cat']) == 3:
            self.status = Contact.STATUS_OFFLINE
            self.status_msg = u'last connection %s' % profile['last_cnx']

        self.summary = html2text(profile['about1']).strip().replace('\n\n', '\n')
        if len(profile['about2']) > 0:
            self.summary += u'\n\nLooking for:\n%s' % html2text(profile['about2']).strip().replace('\n\n', '\n')

        for photo in profile['pictures']:
            self.set_photo(photo['url'].split('/')[-1],
                              url=photo['url'],
                              thumbnail_url=photo['url'].replace('image', 'thumb1_'),
                              hidden=photo['hidden'])
        self.profile = OrderedDict()

        for section, d in self.TABLE.iteritems():
            flags = ProfileNode.SECTION
            if section.startswith('_'):
                flags |= ProfileNode.HEAD
                section = section.lstrip('_')
            s = ProfileNode(section, section.capitalize(), OrderedDict(), flags=flags)

            for key, builder in d.iteritems():
                value = builder.get_value(profile, consts[int(profile['sex'])])
                s.value[key] = ProfileNode(key, key.capitalize().replace('_', ' '), value)

            self.profile[section] = s

        self.aum_profile = profile

    def get_text(self):
        def print_node(node, level=1):
            result = u''
            if node.flags & node.SECTION:
                result += u'\t' * level + node.label + '\n'
                for sub in node.value.itervalues():
                    result += print_node(sub, level+1)
            else:
                if isinstance(node.value, (tuple,list)):
                    value = ', '.join(unicode(v) for v in node.value)
                elif isinstance(node.value, float):
                    value = '%.2f' % node.value
                else:
                    value = node.value
                result += u'\t' * level + u'%-20s %s\n' % (node.label + ':', value)
            return result

        result = u'Nickname: %s\n' % self.name
        if self.status & Contact.STATUS_ONLINE:
            s = 'online'
        elif self.status & Contact.STATUS_OFFLINE:
            s = 'offline'
        elif self.status & Contact.STATUS_AWAY:
            s = 'away'
        else:
            s = 'unknown'
        result += u'Status: %s (%s)\n' % (s, self.status_msg)
        result += u'URL: %s\n' % self.url
        result += u'Photos:\n'
        for name, photo in self.photos.iteritems():
            result += u'\t%s%s\n' % (photo, ' (hidden)' if photo.hidden else '')
        result += u'\nProfile:\n'
        for head in self.profile.itervalues():
            result += print_node(head)
        result += u'Description:\n'
        for s in self.summary.split('\n'):
            result += u'\t%s\n' % s
        return result
<|endoftext|>


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

import synthtool as s
import synthtool.gcp as gcp
import subprocess
import logging

logging.basicConfig(level=logging.DEBUG)

gapic = gcp.GAPICMicrogenerator()
versions = ['v1beta1', 'v1', 'v1p1beta1', 'v1p2beta1', 'v1p4beta1']
name = 'asset'
for version in versions:
    library = gapic.typescript_library(
        name,
        proto_path=f'google/cloud/{name}/{version}',
        generator_args={
            'grpc-service-config': f'google/cloud/{name}/{version}/cloud{name}_grpc_service_config.json',
            'package-name': f'@google-cloud/{name}'
        },
        extra_proto_files=['google/cloud/common_resources.proto'],
        version=version)
    # skip index, protos, package.json, and README.md
    s.copy(
        library,
        excludes=['package.json', 'src/index.ts']
    )

# Copy common templates
common_templates = gcp.CommonTemplates()
templates = common_templates.node_library(source_location='build/src')
s.copy(templates)

# Node.js specific cleanup
subprocess.run(['npm', 'install'])
subprocess.run(['npm', 'run', 'fix'])
subprocess.run(['npx', 'compileProtos', 'src'])
<commit_msg>build: set AUTOSYNTH_MULTIPLE_COMMITS=true for context aware commits (#301)<commit_after># Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This script is used to synthesize generated parts of this library."""

import synthtool as s
import synthtool.gcp as gcp
import subprocess
import logging

logging.basicConfig(level=logging.DEBUG)

AUTOSYNTH_MULTIPLE_COMMITS = True

gapic = gcp.GAPICMicrogenerator()
versions = ['v1beta1', 'v1', 'v1p1beta1', 'v1p2beta1', 'v1p4beta1']
name = 'asset'
for version in versions:
    library = gapic.typescript_library(
        name,
        proto_path=f'google/cloud/{name}/{version}',
        generator_args={
            'grpc-service-config': f'google/cloud/{name}/{version}/cloud{name}_grpc_service_config.json',
            'package-name': f'@google-cloud/{name}'
        },
        extra_proto_files=['google/cloud/common_resources.proto'],
        version=version)
    # skip index, protos, package.json, and README.md
    s.copy(
        library,
        excludes=['package.json', 'src/index.ts']
    )

# Copy common templates
common_templates = gcp.CommonTemplates()
templates = common_templates.node_library(source_location='build/src')
s.copy(templates)

# Node.js specific cleanup
subprocess.run(['npm', 'install'])
subprocess.run(['npm', 'run', 'fix'])
subprocess.run(['npx', 'compileProtos', 'src'])
<|endoftext|>


<commit_before>from Bio import SeqIO
from Bio.Seq import Seq
from copy import copy
import itertools
from collections import Counter
import logging
import datetime
import math

def unique(seq):
    seen = set()
    seen_add = seen.add
    return [ x for x in seq if x not in seen and not seen_add(x)]

class Variant(object):

    def __init__(self, ref, pos, alt):
        self.ref = ref
        self.pos = pos
        self.alt = alt

    def __str__(self):
        return "".join([self.ref, str(self.pos), self.alt])

    def __repr__(self):
        return "".join([self.ref, str(self.pos), self.alt])

    def __gt__(self, other):
        return self.pos > other.pos

    def __lt__(self, other):
        return self.pos < other.pos

    def overlapping(self, other):
        return (other.pos in self.ref_range) or (self.pos in other.ref_range)

    @property
    def ref_range(self):
        return range(self.pos, self.pos + len(self.ref))

    @property
    def length(self):
        return abs(len(self.ref) - len(self.alt))

    @property
    def is_indel(self):
        """ Return whether or not the variant is an INDEL """
        if len(self.ref) > 1:
            return True
        if self.alt is None:
            return True
        elif len(self.alt) != len(self.ref):
            return True
        return False

    @property
    def is_deletion(self):
        """ Return whether or not the INDEL is a deletion """
        if self.is_indel:
            # just one alt allele
            if self.alt is None:
                return True
            if len(self.ref) > len(self.alt):
                return True
            else:
                return False
        else:
            return False

    @property
    def is_insertion(self):
        if self.is_indel:
            if self.alt is None:
                return False
            if len(self.alt) > len(self.ref):
                return True
            else:
                return False
        else:
            return False

class AlleleGenerator(object):
    """docstring for PanelGenerator"""
    def __init__(self, reference_filepath, kmer = 31):
        self.reference_filepath = reference_filepath
        self.kmer = kmer
        self.ref = []
        self.ref_length = 0
        self._read_reference()

    def _read_reference(self):
        for record in SeqIO.parse(self.reference_filepath, 'fasta'):
            self.ref += list(record.seq)
        self.ref_length = len(self.ref)

    def create(self, v, context = []):
        ## Position should be 1 based
        self._check_valid_variant(v)
        context = self._remove_contexts_spanning_del(v, context)
        context = self._remove_contexts_not_within_k(v, context)
        wild_type_reference = self._get_wildtype_reference(v.pos)
        alternates = self._generate_alternates_on_all_backgrounds(v,  context)
        return Panel(wild_type_reference, v.pos, alternates)

    def _check_valid_variant(self, v):
        index = v.pos - 1
        if "".join(self.ref[index:(index + len(v.ref))]) != v.ref:
            raise ValueError("""Cannot create alleles as ref at pos %i is not %s
                                (it's %s) are you sure you're using one-based
                                co-ordinates?
                             """ % (v.pos, v.ref, "".join(self.ref[index: (index + len(v.ref))])))
        if v.pos <= 0 :
            raise ValueError("Position should be 1 based")

    def _remove_contexts_spanning_del(self, v, context):
        """If there's a var within the range of an DEL remove from context"""
        if v.is_deletion:
            context = [c for c in context if not c.pos in v.ref_range]
        return context

    def _remove_contexts_not_within_k(self, v, context):
        new_context = []
        for c in context:
            if c.is_insertion:
                effective_pos = c.pos - c.length
            elif c.is_deletion:
                effective_pos = c.pos + c.length
            else:
                effective_pos = c.pos
            if abs(v.pos - effective_pos) < self.kmer:
                new_context.append(c)
        return new_context

    def _get_wildtype_reference(self, pos):
        i, start_index, end_index = self._get_start_end(pos)
        return self._get_reference_segment(start_index, end_index)

    def _get_reference_segment(self, start_index, end_index):
        return copy(self.ref[start_index:end_index])

    def _get_alternate_reference_segment(self, v, context):
        ref_segment_length_delta = self._calculate_length_delta_from_indels(v, context)
        i, start_index, end_index = self._get_start_end(v.pos, delta = ref_segment_length_delta)
        return self._get_reference_segment(start_index, end_index)

    def _generate_alternates_on_all_backgrounds(self, v, context):
        ## First, create all the context combinations
        context_combinations = self._get_all_context_combinations(context)
        ## For each context, create the background and alternate
        alternates = []
        for context_combo in context_combinations:
            ref_segment_length_delta = self._calculate_length_delta_from_indels(v, context_combo)
            i, start_index, end_index = self._get_start_end(v.pos, delta = ref_segment_length_delta)
            alternate_reference_segment = self._get_reference_segment(start_index, end_index)
            background = self._generate_background_using_context(i, v, alternate_reference_segment, context_combo)
            alternate = copy(background)
            i -=  self._calculate_length_delta_from_variant_list([c for c in context_combo if c.pos <= v.pos and c.is_indel])
            assert "".join(alternate[i:(i + len(v.ref))]) == v.ref
            alternate[i : i + len(v.ref)] = v.alt
            alternates.append(alternate)
        return alternates

    def _get_all_context_combinations(self, context):
        context_list = [[]]
        if context:
            contexts = self._create_multiple_contexts(context) ## Create contexts if multiple variants at given posision
            for context in contexts:
                context_combinations = self._get_combinations_of_backgrounds(context)
                context_list.extend(context_combinations)
        return context_list

    def _generate_background_using_context(self, i, v, alternate_reference_segment, context ):
        backgrounds = [alternate_reference_segment]
        new_background = copy(alternate_reference_segment)

        variants_added = []
        for variant in context:
            j = i + variant.pos - v.pos
            j -= self._calculate_length_delta_from_variant_list([c for c in variants_added if c.pos <= variant.pos and c.is_indel])
            assert "".join(new_background[j : j+ len(variant.ref)]) == variant.ref
            new_background[j : j + len(variant.ref)] = variant.alt
            variants_added.append(variant)
        return new_background

    def _create_multiple_contexts(self, context):
        new_contexts = self._recursive_context_creator([context])
        return new_contexts

    def _recursive_context_creator(self, contexts):
        ## This is only run when there are multiple variants at the same position
        compatiblity_of_contexts = [self._all_variants_are_combatible(context) for context in contexts]
        if self._are_contexts_all_valid(compatiblity_of_contexts):
            return contexts
        else:
            ## If there are variants that are incompatible (e.g. SNPs at the same position or overlapping INDELs)
            ## we need to distangle them into multiple contexts which are all compatible with each other
            ## Get the first incompatible context
            i = compatiblity_of_contexts.index(False)
            incompatible_context = contexts.pop(i)
            ## Split it into two smaller contexts by removing incompatible variants
            split_context = self._split_context(incompatible_context)
            contexts.extend(split_context)
            return self._recursive_context_creator(contexts)

    def _split_context(self, context):
        assert not self._all_variants_are_combatible(context)
        v1, v2 = self._get_first_two_incompatible_variants(context)
        nov1 = []
        nov2 = []
        for x in context:
            if not x is v1:
                nov1.append(x)
            if not x is v2:
                nov2.append(x)
        return [nov1, nov2]

    def _get_first_two_incompatible_variants(self, context):
        for pair in itertools.combinations(context, 2):
            if pair[0].overlapping(pair[1]):
                return pair

    def _are_contexts_all_valid(self, compatiblity_of_contexts):
        return all(compatiblity_of_contexts)

    def _all_variants_are_combatible(self, variants):
        pairs = list(itertools.combinations(variants, 2))
        return all([not v1.overlapping(v2) for v1,v2 in pairs])

    def _get_combinations_of_backgrounds(self, context):
        combination_context = []
        for L in range(1, len(context)+1):
          for subset in itertools.combinations(context, L):
            combination_context.append(list(subset))
        return combination_context

    def _get_start_end(self, pos, delta = 0):
        start_delta = int(math.floor(delta / 2))
        end_delta = int(math.ceil(delta / 2))
        start_index = pos - self.kmer - start_delta
        end_index =  pos + self.kmer + end_delta + 1
        i = self.kmer - 1 + start_delta
        if start_index < 0:
            diff = abs(start_index)
            start_index = 0
            end_index += diff
            i -= diff
        elif end_index > self.ref_length:
            diff = abs(end_index - self.ref_length)
            end_index = self.ref_length
            start_index -= diff
            i += diff
        return (i, start_index, end_index)

    def _calculate_length_delta_from_indels(self, v, context):
        """Calculates the change in required bases for given variant.
        For deletions we need extra bases to get same length flanks"""
        variants = context + [v]
        return self._calculate_length_delta_from_variant_list(variants)

    def _calculate_length_delta_from_variant_list(self, variants):
        deletions_length = [v.length for v in variants if v.is_deletion]
        insertions_length = [v.length for v in variants if v.is_insertion]
        return sum(deletions_length) - sum(insertions_length)

class Panel(object):

    def __init__(self, ref, pos, alts):
        self.ref = "".join(ref)
        self.pos = pos
        self.alts = unique(["".join(alt) for alt in alts])

        <commit_msg>fix bug in 2.7 du to float divisor<commit_after>from Bio import SeqIO
from Bio.Seq import Seq
from copy import copy
import itertools
from collections import Counter
import logging
import datetime
import math

def unique(seq):
    seen = set()
    seen_add = seen.add
    return [ x for x in seq if x not in seen and not seen_add(x)]

class Variant(object):

    def __init__(self, ref, pos, alt):
        self.ref = ref
        self.pos = pos
        self.alt = alt

    def __str__(self):
        return "".join([self.ref, str(self.pos), self.alt])

    def __repr__(self):
        return "".join([self.ref, str(self.pos), self.alt])

    def __gt__(self, other):
        return self.pos > other.pos

    def __lt__(self, other):
        return self.pos < other.pos

    def overlapping(self, other):
        return (other.pos in self.ref_range) or (self.pos in other.ref_range)

    @property
    def ref_range(self):
        return range(self.pos, self.pos + len(self.ref))

    @property
    def length(self):
        return abs(len(self.ref) - len(self.alt))

    @property
    def is_indel(self):
        """ Return whether or not the variant is an INDEL """
        if len(self.ref) > 1:
            return True
        if self.alt is None:
            return True
        elif len(self.alt) != len(self.ref):
            return True
        return False

    @property
    def is_deletion(self):
        """ Return whether or not the INDEL is a deletion """
        if self.is_indel:
            # just one alt allele
            if self.alt is None:
                return True
            if len(self.ref) > len(self.alt):
                return True
            else:
                return False
        else:
            return False

    @property
    def is_insertion(self):
        if self.is_indel:
            if self.alt is None:
                return False
            if len(self.alt) > len(self.ref):
                return True
            else:
                return False
        else:
            return False

class AlleleGenerator(object):
    """docstring for PanelGenerator"""
    def __init__(self, reference_filepath, kmer = 31):
        self.reference_filepath = reference_filepath
        self.kmer = kmer
        self.ref = []
        self.ref_length = 0
        self._read_reference()

    def _read_reference(self):
        for record in SeqIO.parse(self.reference_filepath, 'fasta'):
            self.ref += list(record.seq)
        self.ref_length = len(self.ref)

    def create(self, v, context = []):
        ## Position should be 1 based
        self._check_valid_variant(v)
        context = self._remove_contexts_spanning_del(v, context)
        context = self._remove_contexts_not_within_k(v, context)
        wild_type_reference = self._get_wildtype_reference(v.pos)
        alternates = self._generate_alternates_on_all_backgrounds(v,  context)
        return Panel(wild_type_reference, v.pos, alternates)

    def _check_valid_variant(self, v):
        index = v.pos - 1
        if "".join(self.ref[index:(index + len(v.ref))]) != v.ref:
            raise ValueError("""Cannot create alleles as ref at pos %i is not %s
                                (it's %s) are you sure you're using one-based
                                co-ordinates?
                             """ % (v.pos, v.ref, "".join(self.ref[index: (index + len(v.ref))])))
        if v.pos <= 0 :
            raise ValueError("Position should be 1 based")

    def _remove_contexts_spanning_del(self, v, context):
        """If there's a var within the range of an DEL remove from context"""
        if v.is_deletion:
            context = [c for c in context if not c.pos in v.ref_range]
        return context

    def _remove_contexts_not_within_k(self, v, context):
        new_context = []
        for c in context:
            if c.is_insertion:
                effective_pos = c.pos - c.length
            elif c.is_deletion:
                effective_pos = c.pos + c.length
            else:
                effective_pos = c.pos
            if abs(v.pos - effective_pos) < self.kmer:
                new_context.append(c)
        return new_context

    def _get_wildtype_reference(self, pos):
        i, start_index, end_index = self._get_start_end(pos)
        return self._get_reference_segment(start_index, end_index)

    def _get_reference_segment(self, start_index, end_index):
        return copy(self.ref[start_index:end_index])

    def _get_alternate_reference_segment(self, v, context):
        ref_segment_length_delta = self._calculate_length_delta_from_indels(v, context)
        i, start_index, end_index = self._get_start_end(v.pos, delta = ref_segment_length_delta)
        return self._get_reference_segment(start_index, end_index)

    def _generate_alternates_on_all_backgrounds(self, v, context):
        ## First, create all the context combinations
        context_combinations = self._get_all_context_combinations(context)
        ## For each context, create the background and alternate
        alternates = []
        for context_combo in context_combinations:
            ref_segment_length_delta = self._calculate_length_delta_from_indels(v, context_combo)
            i, start_index, end_index = self._get_start_end(v.pos, delta = ref_segment_length_delta)
            alternate_reference_segment = self._get_reference_segment(start_index, end_index)
            background = self._generate_background_using_context(i, v, alternate_reference_segment, context_combo)
            alternate = copy(background)
            i -=  self._calculate_length_delta_from_variant_list([c for c in context_combo if c.pos <= v.pos and c.is_indel])
            assert "".join(alternate[i:(i + len(v.ref))]) == v.ref
            alternate[i : i + len(v.ref)] = v.alt
            alternates.append(alternate)
        return alternates

    def _get_all_context_combinations(self, context):
        context_list = [[]]
        if context:
            contexts = self._create_multiple_contexts(context) ## Create contexts if multiple variants at given posision
            for context in contexts:
                context_combinations = self._get_combinations_of_backgrounds(context)
                context_list.extend(context_combinations)
        return context_list

    def _generate_background_using_context(self, i, v, alternate_reference_segment, context ):
        backgrounds = [alternate_reference_segment]
        new_background = copy(alternate_reference_segment)

        variants_added = []
        for variant in context:
            j = i + variant.pos - v.pos
            j -= self._calculate_length_delta_from_variant_list([c for c in variants_added if c.pos <= variant.pos and c.is_indel])
            assert "".join(new_background[j : j+ len(variant.ref)]) == variant.ref
            new_background[j : j + len(variant.ref)] = variant.alt
            variants_added.append(variant)
        return new_background

    def _create_multiple_contexts(self, context):
        new_contexts = self._recursive_context_creator([context])
        return new_contexts

    def _recursive_context_creator(self, contexts):
        ## This is only run when there are multiple variants at the same position
        compatiblity_of_contexts = [self._all_variants_are_combatible(context) for context in contexts]
        if self._are_contexts_all_valid(compatiblity_of_contexts):
            return contexts
        else:
            ## If there are variants that are incompatible (e.g. SNPs at the same position or overlapping INDELs)
            ## we need to distangle them into multiple contexts which are all compatible with each other
            ## Get the first incompatible context
            i = compatiblity_of_contexts.index(False)
            incompatible_context = contexts.pop(i)
            ## Split it into two smaller contexts by removing incompatible variants
            split_context = self._split_context(incompatible_context)
            contexts.extend(split_context)
            return self._recursive_context_creator(contexts)

    def _split_context(self, context):
        assert not self._all_variants_are_combatible(context)
        v1, v2 = self._get_first_two_incompatible_variants(context)
        nov1 = []
        nov2 = []
        for x in context:
            if not x is v1:
                nov1.append(x)
            if not x is v2:
                nov2.append(x)
        return [nov1, nov2]

    def _get_first_two_incompatible_variants(self, context):
        for pair in itertools.combinations(context, 2):
            if pair[0].overlapping(pair[1]):
                return pair

    def _are_contexts_all_valid(self, compatiblity_of_contexts):
        return all(compatiblity_of_contexts)

    def _all_variants_are_combatible(self, variants):
        pairs = list(itertools.combinations(variants, 2))
        return all([not v1.overlapping(v2) for v1,v2 in pairs])

    def _get_combinations_of_backgrounds(self, context):
        combination_context = []
        for L in range(1, len(context)+1):
          for subset in itertools.combinations(context, L):
            combination_context.append(list(subset))
        return combination_context

    def _get_start_end(self, pos, delta = 0):
        start_delta = int(math.floor(float(delta) / 2))
        end_delta = int(math.ceil(float(delta) / 2))
        start_index = pos - self.kmer - start_delta
        end_index =  pos + self.kmer + end_delta + 1
        i = self.kmer - 1 + start_delta
        if start_index < 0:
            diff = abs(start_index)
            start_index = 0
            end_index += diff
            i -= diff
        elif end_index > self.ref_length:
            diff = abs(end_index - self.ref_length)
            end_index = self.ref_length
            start_index -= diff
            i += diff
        return (i, start_index, end_index)

    def _calculate_length_delta_from_indels(self, v, context):
        """Calculates the change in required bases for given variant.
        For deletions we need extra bases to get same length flanks"""
        variants = context + [v]
        return self._calculate_length_delta_from_variant_list(variants)

    def _calculate_length_delta_from_variant_list(self, variants):
        deletions_length = [v.length for v in variants if v.is_deletion]
        insertions_length = [v.length for v in variants if v.is_insertion]
        return sum(deletions_length) - sum(insertions_length)

class Panel(object):

    def __init__(self, ref, pos, alts):
        self.ref = "".join(ref)
        self.pos = pos
        self.alts = unique(["".join(alt) for alt in alts])

        <|endoftext|>


<commit_before># Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import re

from telemetry import decorators
from telemetry.core.backends.chrome_inspector import devtools_http
from telemetry.core.backends.chrome_inspector import tracing_backend

class DevToolsClientBackend(object):
  def __init__(self, devtools_port):
    self._devtools_port = devtools_port
    self._devtools_http = devtools_http.DevToolsHttp(devtools_port)
    self._tracing_backend = None

  # TODO(chrishenry): This is temporarily exposed during DevTools code
  # refactoring. Please do not introduce new usage! crbug.com/423954
  @property
  def devtools_http(self):
    return self._devtools_http

  def IsAlive(self):
    """Whether the DevTools server is available and connectable."""
    try:
      self._devtools_http.Request('', timeout=.1)
    except devtools_http.DevToolsClientConnectionError:
      return False
    else:
      return True

  def Close(self):
    if self._tracing_backend:
      self._tracing_backend.Close()
      self._tracing_backend = None

  @decorators.Cache
  def GetChromeBranchNumber(self):
    # Detect version information.
    resp = self._devtools_http.RequestJson('version')
    if 'Protocol-Version' in resp:
      if 'Browser' in resp:
        branch_number_match = re.search(r'Chrome/\d+\.\d+\.(\d+)\.\d+',
                                        resp['Browser'])
      else:
        branch_number_match = re.search(
            r'Chrome/\d+\.\d+\.(\d+)\.\d+ (Mobile )?Safari',
            resp['User-Agent'])

      if branch_number_match:
        branch_number = int(branch_number_match.group(1))
        if branch_number:
          return branch_number

    # Branch number can't be determined, so fail any branch number checks.
    return 0

  # TODO(chrishenry): This is exposed tempoarily during DevTools code
  # refactoring. Instead, we should expose InspectorBackendList or
  # equivalent. crbug.com/423954.
  def ListInspectableContexts(self):
    return self._devtools_http.RequestJson('')

  def _CreateTracingBackendIfNeeded(self):
    if not self._tracing_backend:
      self._tracing_backend = tracing_backend.TracingBackend(
          self._devtools_port)

  def IsChromeTracingSupported(self):
    self._CreateTracingBackendIfNeeded()
    return self._tracing_backend.IsTracingSupported()

  def StartChromeTracing(
      self, trace_options, custom_categories=None, timeout=10):
    """
    Args:
        trace_options: An tracing_options.TracingOptions instance.
        custom_categories: An optional string containing a list of
                         comma separated categories that will be traced
                         instead of the default category set.  Example: use
                         "webkit,cc,disabled-by-default-cc.debug" to trace only
                         those three event categories.
    """
    assert trace_options and trace_options.enable_chrome_trace
    self._CreateTracingBackendIfNeeded()
    return self._tracing_backend.StartTracing(
        trace_options, custom_categories, timeout)

  def StopChromeTracing(self, timeout=30):
    assert self._tracing_backend
    return self._tracing_backend.StopTracing(timeout)
<commit_msg>Fix merge issue from https://codereview.chromium.org/819653004/.<commit_after># Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import re

from telemetry import decorators
from telemetry.core.backends.chrome_inspector import devtools_http
from telemetry.core.backends.chrome_inspector import tracing_backend

class DevToolsClientBackend(object):
  def __init__(self, devtools_port):
    self._devtools_port = devtools_port
    self._devtools_http = devtools_http.DevToolsHttp(devtools_port)
    self._tracing_backend = None

  # TODO(chrishenry): This is temporarily exposed during DevTools code
  # refactoring. Please do not introduce new usage! crbug.com/423954
  @property
  def devtools_http(self):
    return self._devtools_http

  def IsAlive(self):
    """Whether the DevTools server is available and connectable."""
    try:
      self._devtools_http.Request('', timeout=.1)
    except devtools_http.DevToolsClientConnectionError:
      return False
    else:
      return True

  def Close(self):
    if self._tracing_backend:
      self._tracing_backend.Close()
      self._tracing_backend = None

  @decorators.Cache
  def GetChromeBranchNumber(self):
    # Detect version information.
    resp = self._devtools_http.RequestJson('version')
    if 'Protocol-Version' in resp:
      if 'Browser' in resp:
        branch_number_match = re.search(r'Chrome/\d+\.\d+\.(\d+)\.\d+',
                                        resp['Browser'])
      else:
        branch_number_match = re.search(
            r'Chrome/\d+\.\d+\.(\d+)\.\d+ (Mobile )?Safari',
            resp['User-Agent'])

      if branch_number_match:
        branch_number = int(branch_number_match.group(1))
        if branch_number:
          return branch_number

    # Branch number can't be determined, so fail any branch number checks.
    return 0

  # TODO(chrishenry): This is exposed tempoarily during DevTools code
  # refactoring. Instead, we should expose InspectorBackendList or
  # equivalent. crbug.com/423954.
  def ListInspectableContexts(self):
    return self._devtools_http.RequestJson('')

  def _CreateTracingBackendIfNeeded(self):
    if not self._tracing_backend:
      self._tracing_backend = tracing_backend.TracingBackend(
          self._devtools_port)

  def IsChromeTracingSupported(self):
    self._CreateTracingBackendIfNeeded()
    return self._tracing_backend.IsTracingSupported()

  def StartChromeTracing(
      self, trace_options, custom_categories=None, timeout=10):
    """
    Args:
        trace_options: An tracing_options.TracingOptions instance.
        custom_categories: An optional string containing a list of
                         comma separated categories that will be traced
                         instead of the default category set.  Example: use
                         "webkit,cc,disabled-by-default-cc.debug" to trace only
                         those three event categories.
    """
    assert trace_options and trace_options.enable_chrome_trace
    self._CreateTracingBackendIfNeeded()
    return self._tracing_backend.StartTracing(
        trace_options, custom_categories, timeout)

  def StopChromeTracing(self, trace_data_builder, timeout=30):
    assert self._tracing_backend
    return self._tracing_backend.StopTracing(trace_data_builder, timeout)
<|endoftext|>


<commit_before>import logging
from torch.utils.data import Dataset
import numpy as np
import math

class JetDataset(Dataset):
    def __init__(self, jets, weights=None, problem=None, subproblem=None):
        super().__init__()
        self.jets = jets
        self.weights = weights
        self.problem = problem
        self.subproblem = subproblem

    def __len__(self):
        return len(self.jets)

    def __getitem__(self, idx):
        return self.jets[idx], self.jets[idx].y

    def shuffle(self):
        perm = np.random.permutation(len(self.jets))
        self.jets = [self.jets[i] for i in perm]
        #self.y = [self.y[i] for i in perm]
        if self.weights is not None:
            self.weights = [self.weights[i] for i in perm]

    @property
    def dim(self):
        return self.jets[0].constituents.shape[1]

    def extend(self, dataset):
        self.jets = self.jets + dataset.jets

    @classmethod
    def concatenate(cls, dataset1, dataset2):
        return cls(dataset1.jet + dataset2.jets)

    def crop(self):

        good_jets, bad_jets, w = self._crop()
        self.jets = good_jets
        self.weights = w
        return bad_jets

    def _crop(self):
        logging.info('Cropping dataset...')
        if self.problem == 'w-vs-qcd':
            return self._crop_w_vs_qcd()
        elif self.problem == 'quark-gluon':
            return self._crop_quark_gluon()
        else:
            raise ValueError('Only problems accepted are w-vs-qcd or quark-gluon (got {})'.format(self.problem))

    def _crop_quark_gluon(self):
        jets = self.jets
        #logging.warning("Cropping...")
        pt_min = 50
        eta_max = 1.5
        photon_pt_min = 100
        delta_phi_min = 2 * math.pi / 3

        good_jets = []
        bad_jets = []
        #good_indices = []
        pt_filter = 0
        eta_filter = 0
        photon_pt_filter = 0
        photon_eta_filter = 0
        delta_phi_filter = 0

        for i, j in enumerate(jets):
            good = True
            if j.pt <= pt_min:
                good = False
                pt_filter += 1
            if abs(j.eta) >= eta_max:
                good = False
                eta_filter += 1
            if abs(j.photon_eta) >= eta_max:
                good = False
                photon_eta_filter += 1
            if j.photon_pt <= photon_pt_min:
                good = False
                photon_pt_filter += 1

            delta_phi = abs(j.phi - j.photon_phi)
            if delta_phi > math.pi:
                delta_phi = delta_phi - math.pi
            if delta_phi <= delta_phi_min:
                good = False
                delta_phi_filter += 1

            if good:
                good_jets.append(j)
            else:
                bad_jets.append(j)

        logging.warning('applied cuts to {} jets'.format(len(jets)))

        logging.warning('bad pt = {}'.format(pt_filter))
        logging.warning('bad eta = {}'.format(eta_filter))
        logging.warning('bad photon_pt = {}'.format(photon_pt_filter))
        logging.warning('bad photon_eta = {}'.format(photon_eta_filter))
        logging.warning('bad delta_phi = {}'.format(delta_phi_filter))

        return good_jets, bad_jets, None

    def _crop_w_vs_qcd(self):
        #print("___CROP")

        jets = self.jets
        #logging.warning("Cropping...")
        if self.subproblem == 'antikt-kt-pileup':
            pt_min, pt_max, m_min, m_max = 300, 365, 150, 220
        elif self.subproblem == 'antikt-kt':
            pt_min, pt_max, m_min, m_max = 250, 300, 50, 110
        else:
            raise ValueError("Only subproblems accepted are antikt-kt or antikt-kt-pileup (got {})".format(self.subproblem))

        good_jets = []
        bad_jets = []
        #good_indices = []
        for i, j in enumerate(jets):
            if pt_min < j.pt < pt_max and m_min < j.mass < m_max:
                good_jets.append(j)
                #good_indices.append(i)
            else:
                bad_jets.append(j)

        # Weights for flatness in pt
        w = np.zeros(len(good_jets))
        y_ = np.array([jet.y for jet in good_jets])

        jets_0 = [jet for jet in good_jets if jet.y == 0]
        pdf, edges = np.histogram([j.pt for j in jets_0], density=True, range=[pt_min, pt_max], bins=50)
        pts = [j.pt for j in jets_0]
        indices = np.searchsorted(edges, pts) - 1
        inv_w = 1. / pdf[indices]
        inv_w /= inv_w.sum()
        #w[y_==0] = inv_w
        for i, (iw, jet) in enumerate(zip(inv_w, good_jets)):
            if jet.y == 0:
                w[i] = iw

        jets_1 = [jet for jet in good_jets if jet.y == 1]
        pdf, edges = np.histogram([j.pt for j in jets_1], density=True, range=[pt_min, pt_max], bins=50)
        pts = [j.pt for j in jets_1]
        indices = np.searchsorted(edges, pts) - 1
        inv_w = 1. / pdf[indices]
        inv_w /= inv_w.sum()
        #w[y_==1] = inv_w
        for i, (iw, jet) in enumerate(zip(inv_w, good_jets)):
            if jet.y == 1:
                w[i] = iw

        return good_jets, bad_jets, w
<commit_msg>debug<commit_after>import logging
from torch.utils.data import Dataset
import numpy as np
import math

from sklearn.preprocessing import RobustScaler
class JetDataset(Dataset):
    def __init__(self, jets, weights=None, problem=None, subproblem=None):
        super().__init__()
        self.jets = jets
        self.weights = weights
        self.problem = problem
        self.subproblem = subproblem

    def __len__(self):
        return len(self.jets)

    def __getitem__(self, idx):
        return self.jets[idx], self.jets[idx].y

    def shuffle(self):
        perm = np.random.permutation(len(self.jets))
        self.jets = [self.jets[i] for i in perm]
        #self.y = [self.y[i] for i in perm]
        if self.weights is not None:
            self.weights = [self.weights[i] for i in perm]

    @property
    def dim(self):
        return self.jets[0].constituents.shape[1]

    def extend(self, dataset):
        self.jets = self.jets + dataset.jets

    @classmethod
    def concatenate(cls, dataset1, dataset2):
        return cls(dataset1.jet + dataset2.jets)

    def get_scaler(self):
        self.tf = RobustScaler().fit(np.vstack([jet.constituents for jet in self.jets]))
        return self.tf

    def transform(self, tf=None):
        if tf is None:
            tf = self.get_scaler()
        for i, jet in enumerate(self.jets):
            jet.constituents = tf.transform(jet.constituents)
            self.jets[i] = jet
        #jet_dicts = new_jet_dicts

    def crop(self):

        good_jets, bad_jets, w = self._crop()
        self.jets = good_jets
        self.weights = w
        return bad_jets

    def _crop(self):
        logging.info('Cropping dataset...')
        if self.problem == 'w-vs-qcd':
            return self._crop_w_vs_qcd()
        elif self.problem == 'quark-gluon':
            return self._crop_quark_gluon()
        else:
            raise ValueError('Only problems accepted are w-vs-qcd or quark-gluon (got {})'.format(self.problem))

    def _crop_quark_gluon(self):
        jets = self.jets
        #logging.warning("Cropping...")
        pt_min = 50
        eta_max = 1.5
        photon_pt_min = 100
        delta_phi_min = 2 * math.pi / 3

        good_jets = []
        bad_jets = []
        #good_indices = []
        pt_filter = 0
        eta_filter = 0
        photon_pt_filter = 0
        photon_eta_filter = 0
        delta_phi_filter = 0

        for i, j in enumerate(jets):
            good = True
            if j.pt <= pt_min:
                good = False
                pt_filter += 1
            if abs(j.eta) >= eta_max:
                good = False
                eta_filter += 1
            if abs(j.photon_eta) >= eta_max:
                good = False
                photon_eta_filter += 1
            if j.photon_pt <= photon_pt_min:
                good = False
                photon_pt_filter += 1

            delta_phi = abs(j.phi - j.photon_phi)
            if delta_phi > math.pi:
                delta_phi = delta_phi - math.pi
            if delta_phi <= delta_phi_min:
                good = False
                delta_phi_filter += 1

            if good:
                good_jets.append(j)
            else:
                bad_jets.append(j)

        logging.warning('applied cuts to {} jets'.format(len(jets)))

        logging.warning('bad pt = {}'.format(pt_filter))
        logging.warning('bad eta = {}'.format(eta_filter))
        logging.warning('bad photon_pt = {}'.format(photon_pt_filter))
        logging.warning('bad photon_eta = {}'.format(photon_eta_filter))
        logging.warning('bad delta_phi = {}'.format(delta_phi_filter))

        return good_jets, bad_jets, None

    def _crop_w_vs_qcd(self):
        #print("___CROP")

        jets = self.jets
        #logging.warning("Cropping...")
        if self.subproblem == 'antikt-kt-pileup':
            pt_min, pt_max, m_min, m_max = 300, 365, 150, 220
        elif self.subproblem == 'antikt-kt':
            pt_min, pt_max, m_min, m_max = 250, 300, 50, 110
        else:
            raise ValueError("Only subproblems accepted are antikt-kt or antikt-kt-pileup (got {})".format(self.subproblem))

        good_jets = []
        bad_jets = []
        #good_indices = []
        for i, j in enumerate(jets):
            if pt_min < j.pt < pt_max and m_min < j.mass < m_max:
                good_jets.append(j)
                #good_indices.append(i)
            else:
                bad_jets.append(j)

        # Weights for flatness in pt
        w = np.zeros(len(good_jets))
        y_ = np.array([jet.y for jet in good_jets])

        jets_0 = [jet for jet in good_jets if jet.y == 0]
        pdf, edges = np.histogram([j.pt for j in jets_0], density=True, range=[pt_min, pt_max], bins=50)
        pts = [j.pt for j in jets_0]
        indices = np.searchsorted(edges, pts) - 1
        inv_w = 1. / pdf[indices]
        inv_w /= inv_w.sum()
        #w[y_==0] = inv_w
        for i, (iw, jet) in enumerate(zip(inv_w, good_jets)):
            if jet.y == 0:
                w[i] = iw

        jets_1 = [jet for jet in good_jets if jet.y == 1]
        pdf, edges = np.histogram([j.pt for j in jets_1], density=True, range=[pt_min, pt_max], bins=50)
        pts = [j.pt for j in jets_1]
        indices = np.searchsorted(edges, pts) - 1
        inv_w = 1. / pdf[indices]
        inv_w /= inv_w.sum()
        #w[y_==1] = inv_w
        for i, (iw, jet) in enumerate(zip(inv_w, good_jets)):
            if jet.y == 1:
                w[i] = iw

        return good_jets, bad_jets, w
<|endoftext|>


<commit_before>import os
import imp
import socket
import sys
from flask import Flask
from waitress import serve

# Make relative imports for the child handler
sys.path.append(os.path.dirname(os.path.realpath(sys.argv[2])))

# import handler module from absolute path
def importFromURI(uri, absl=False):
	if not absl:
		uri = os.path.normpath(os.path.join(os.path.dirname(__file__), uri))
	path, fname = os.path.split(uri)
	mname, ext = os.path.splitext(fname)

	no_ext = os.path.join(path, mname)
	if os.path.exists(no_ext + '.pyc'):
		try:
			return imp.load_compiled(mname, no_ext + '.pyc')
		except:
			pass
	if os.path.exists(no_ext + '.py'):
		try:
			return imp.load_source(mname, no_ext + '.py')
		except:
			pass

app = Flask(__name__)

# @app.route(sys.argv[1] + '/', defaults={'path': ''},  methods = ['GET', 'POST'])
@app.route(sys.argv[1] +'/', methods = ['GET', 'POST'])
def root(*args, **kwargs):
  module = importFromURI(sys.argv[2], True)
  return module.handler(*args, **kwargs)

# fetch a new free port
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 0))
port = sock.getsockname()[1]
sock.close()
os.write(4, bytes(str(port) + '\n', 'utf8'))
serve(app, host='0.0.0.0', port=port, _quiet=True)<commit_msg>longer traceback on python exception<commit_after>import os
import imp
import socket
import sys
import traceback
from flask import Flask
from waitress import serve

# Make relative imports for the child handler
sys.path.append(os.path.dirname(os.path.realpath(sys.argv[2])))

# import handler module from absolute path
def importFromURI(uri, absl=False):
	if not absl:
		uri = os.path.normpath(os.path.join(os.path.dirname(__file__), uri))
	path, fname = os.path.split(uri)
	mname, ext = os.path.splitext(fname)

	no_ext = os.path.join(path, mname)
	if os.path.exists(no_ext + '.pyc'):
		try:
			return imp.load_compiled(mname, no_ext + '.pyc')
		except:
			pass
	if os.path.exists(no_ext + '.py'):
		try:
			return imp.load_source(mname, no_ext + '.py')
		except:
			pass

app = Flask(__name__)

# @app.route(sys.argv[1] + '/', defaults={'path': ''},  methods = ['GET', 'POST'])
@app.route(sys.argv[1] +'/', methods = ['GET', 'POST'])
def root(*args, **kwargs):
	try:
    module = importFromURI(sys.argv[2], True)
  	return module.handler(*args, **kwargs)
  except Exception as ex:
    traceback.print_exc()

# fetch a new free port
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 0))
port = sock.getsockname()[1]
sock.close()
os.write(4, bytes(str(port) + '\n', 'utf8'))
serve(app, host='0.0.0.0', port=port, _quiet=True)<|endoftext|>