<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 time

from telemetry.results import progress_reporter
from telemetry.value import failure
from telemetry.value import skip

class GTestProgressReporter(progress_reporter.ProgressReporter):
  """A progress reporter that outputs the progress report in gtest style."""

  def __init__(self, output_stream, output_skipped_tests_summary=False):
    super(GTestProgressReporter, self).__init__()
    self._output_stream = output_stream
    self._timestamp = None
    self._output_skipped_tests_summary = output_skipped_tests_summary

  def _GetMs(self):
    assert self._timestamp is not None, 'Did not call WillRunPage.'
    return (time.time() - self._timestamp) * 1000

  def DidAddValue(self, value):
    super(GTestProgressReporter, self).DidAddValue(value)
    if isinstance(value, failure.FailureValue):
      print >> self._output_stream, failure.GetStringFromExcInfo(
          value.exc_info)
      self._output_stream.flush()
    elif isinstance(value, skip.SkipValue):
      print >> self._output_stream, '===== SKIPPING TEST %s: %s =====' % (
          value.page.display_name, value.reason)
    # TODO(chrishenry): Consider outputting metric values as well. For
    # e.g., it can replace BuildbotOutputFormatter in
    # --output-format=html, which we used only so that users can grep
    # the results without opening results.html.

  def WillRunPage(self, page_test_results):
    super(GTestProgressReporter, self).WillRunPage(page_test_results)
    print >> self._output_stream, '[ RUN      ]', (
        page_test_results.current_page.display_name)
    self._output_stream.flush()
    self._timestamp = time.time()

  def DidRunPage(self, page_test_results):
    super(GTestProgressReporter, self).DidRunPage(page_test_results)
    page = page_test_results.current_page
    if page_test_results.current_page_run.failed:
      print >> self._output_stream, '[  FAILED  ]', page.display_name, (
          '(%0.f ms)' % self._GetMs())
    else:
      print >> self._output_stream, '[       OK ]', page.display_name, (
          '(%0.f ms)' % self._GetMs())
    self._output_stream.flush()

  def WillAttemptPageRun(self, page_test_results, attempt_count, max_attempts):
    super(GTestProgressReporter, self).WillAttemptPageRun(
        page_test_results, attempt_count, max_attempts)
    # A failed attempt will have at least 1 value.
    if attempt_count != 1:
      print >> self._output_stream, (
          '===== RETRYING PAGE RUN (attempt %s out of %s allowed) =====' % (
              attempt_count, max_attempts))
      print >> self._output_stream, (
          'Page run attempt failed and will be retried. '
          'Discarding previous results.')

  def DidFinishAllTests(self, page_test_results):
    super(GTestProgressReporter, self).DidFinishAllTests(page_test_results)
    successful_runs = []
    failed_runs = []
    for run in page_test_results.all_page_runs:
      if run.failed:
        failed_runs.append(run)
      else:
        successful_runs.append(run)

    unit = 'test' if len(successful_runs) == 1 else 'tests'
    print >> self._output_stream, '[  PASSED  ]', (
        '%d %s.' % (len(successful_runs), unit))
    if len(failed_runs) > 0:
      unit = 'test' if len(failed_runs) == 1 else 'tests'
      print >> self._output_stream, '[  FAILED  ]', (
          '%d %s, listed below:' % (len(page_test_results.failures), unit))
      for failed_run in failed_runs:
        print >> self._output_stream, '[  FAILED  ] ', (
            failed_run.page.display_name)
      print >> self._output_stream
      count = len(failed_runs)
      unit = 'TEST' if count == 1 else 'TESTS'
      print >> self._output_stream, '%d FAILED %s' % (count, unit)
    print >> self._output_stream

    if self._output_skipped_tests_summary:
      if len(page_test_results.skipped_values) > 0:
        print >> self._output_stream, 'Skipped pages:\n%s\n' % ('\n'.join(
            v.page.display_name for v in page_test_results.skipped_values))

    self._output_stream.flush()
<commit_msg>Makes sure telemetry test result report is one string (no interruption)<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 time

from telemetry.results import progress_reporter
from telemetry.value import failure
from telemetry.value import skip

class GTestProgressReporter(progress_reporter.ProgressReporter):
  """A progress reporter that outputs the progress report in gtest style.

  Be careful each print should only handle one string. Otherwise, the output
  might be interrupted by Chrome logging, and the output interpretation might
  be incorrect. For example:
      print >> self._output_stream, "[ OK ]", testname
  should be written as
      print >> self._output_stream, "[ OK ] %s" % testname
  """

  def __init__(self, output_stream, output_skipped_tests_summary=False):
    super(GTestProgressReporter, self).__init__()
    self._output_stream = output_stream
    self._timestamp = None
    self._output_skipped_tests_summary = output_skipped_tests_summary

  def _GetMs(self):
    assert self._timestamp is not None, 'Did not call WillRunPage.'
    return (time.time() - self._timestamp) * 1000

  def DidAddValue(self, value):
    super(GTestProgressReporter, self).DidAddValue(value)
    if isinstance(value, failure.FailureValue):
      print >> self._output_stream, failure.GetStringFromExcInfo(
          value.exc_info)
      self._output_stream.flush()
    elif isinstance(value, skip.SkipValue):
      print >> self._output_stream, '===== SKIPPING TEST %s: %s =====' % (
          value.page.display_name, value.reason)
    # TODO(chrishenry): Consider outputting metric values as well. For
    # e.g., it can replace BuildbotOutputFormatter in
    # --output-format=html, which we used only so that users can grep
    # the results without opening results.html.

  def WillRunPage(self, page_test_results):
    super(GTestProgressReporter, self).WillRunPage(page_test_results)
    print >> self._output_stream, '[ RUN      ] %s' % (
        page_test_results.current_page.display_name)
    self._output_stream.flush()
    self._timestamp = time.time()

  def DidRunPage(self, page_test_results):
    super(GTestProgressReporter, self).DidRunPage(page_test_results)
    page = page_test_results.current_page
    if page_test_results.current_page_run.failed:
      print >> self._output_stream, '[  FAILED  ] %s (%0.f ms)' % (
          page.display_name, self._GetMs())
    else:
      print >> self._output_stream, '[       OK ] %s (%0.f ms)' % (
          page.display_name, self._GetMs())
    self._output_stream.flush()

  def WillAttemptPageRun(self, page_test_results, attempt_count, max_attempts):
    super(GTestProgressReporter, self).WillAttemptPageRun(
        page_test_results, attempt_count, max_attempts)
    # A failed attempt will have at least 1 value.
    if attempt_count != 1:
      print >> self._output_stream, (
          '===== RETRYING PAGE RUN (attempt %s out of %s allowed) =====' % (
              attempt_count, max_attempts))
      print >> self._output_stream, (
          'Page run attempt failed and will be retried. '
          'Discarding previous results.')

  def DidFinishAllTests(self, page_test_results):
    super(GTestProgressReporter, self).DidFinishAllTests(page_test_results)
    successful_runs = []
    failed_runs = []
    for run in page_test_results.all_page_runs:
      if run.failed:
        failed_runs.append(run)
      else:
        successful_runs.append(run)

    unit = 'test' if len(successful_runs) == 1 else 'tests'
    print >> self._output_stream, '[  PASSED  ] %d %s.' % (
        (len(successful_runs), unit))
    if len(failed_runs) > 0:
      unit = 'test' if len(failed_runs) == 1 else 'tests'
      print >> self._output_stream, '[  FAILED  ] %d %s, listed below:' % (
          (len(page_test_results.failures), unit))
      for failed_run in failed_runs:
        print >> self._output_stream, '[  FAILED  ]  %s' % (
            failed_run.page.display_name)
      print >> self._output_stream
      count = len(failed_runs)
      unit = 'TEST' if count == 1 else 'TESTS'
      print >> self._output_stream, '%d FAILED %s' % (count, unit)
    print >> self._output_stream

    if self._output_skipped_tests_summary:
      if len(page_test_results.skipped_values) > 0:
        print >> self._output_stream, 'Skipped pages:\n%s\n' % ('\n'.join(
            v.page.display_name for v in page_test_results.skipped_values))

    self._output_stream.flush()
<|endoftext|>


<commit_before>import os.path
import logging

query_cache = {}
filter_cache = {}

def get_userquery(query_folder, query_name):
    try:
        return query_cache[query_name]
    except KeyError:
        pass

    # get the query from file
    query_dir = os.path.join(query_folder, query_name)
    if os.path.exists(query_dir) and os.path.isdir(query_dir)
        and os.path.exists(os.path.join(query_dir, 'query.txt')):
        with open(os.path.join(query_dir, 'query.txt'), 'r') as query_handle:
            query_cache[query_name] = query_handle.read()
    else:
        return '{{}}'

    #logging.debug("query_cache[{}]: {}".format(query_name, query_cache[query_name]))

    return query_cache[query_name]

def get_userfilter(query_folder, query_name):
    try:
        return filter_cache[query_name]
    except KeyError:
        pass

    query_dir = os.path.join(query_folder, query_name)
    if os.path.exists(query_dir) and os.path.isdir(query_dir)
        and os.path.exists(os.path.join(query_dir, 'filter.txt')):
        with open(os.path.join(query_dir, 'filter.txt'), 'r') as filter_handle:
            filter_cache[query_name] = filter_handle.read()
    else:
        return '{{}}'

    #logging.debug("filter_cache[{}]: {}".format(query_name, filter_cache[query_name]))

    return filter_cache[query_name]
<commit_msg>Fixing some syntax errors, returning a null dict if no filter.txt file.<commit_after>import os.path
#import logging

query_cache = {}
filter_cache = {}

def get_userquery(query_folder, query_name):
    try:
        return query_cache[query_name]
    except KeyError:
        pass

    # get the query from file
    query_dir = os.path.join(query_folder, query_name)
    if (os.path.exists(query_dir) and os.path.isdir(query_dir)
        and os.path.exists(os.path.join(query_dir, 'query.txt'))):
        with open(os.path.join(query_dir, 'query.txt'), 'r') as query_handle:
            query_cache[query_name] = query_handle.read()
    else:
        return '{{}}'

    #logging.debug("query_cache[{}]: {}".format(query_name, query_cache[query_name]))

    return query_cache[query_name]

def get_userfilter(query_folder, query_name):
    try:
        return filter_cache[query_name]
    except KeyError:
        pass

    query_dir = os.path.join(query_folder, query_name)
    if (os.path.exists(query_dir) and os.path.isdir(query_dir)
        and os.path.exists(os.path.join(query_dir, 'filter.txt'))):
        with open(os.path.join(query_dir, 'filter.txt'), 'r') as filter_handle:
            filter_cache[query_name] = filter_handle.read()
    else:
        return '{}'

    #logging.debug("filter_cache[{}]: {}".format(query_name, filter_cache[query_name]))

    return filter_cache[query_name]
<|endoftext|>


<commit_before>__all__ = ['EgnyteClient']

from egnyte.client import EgnyteClient
<commit_msg>Change absolute import to relative import<commit_after>__all__ = ['EgnyteClient']

from .client import EgnyteClient
<|endoftext|>


<commit_before>from django.db import models
from django.db.models import Q,Count
from django.core.validators import  RegexValidator

from event_cal.models import CalendarEvent
from mig_main.pdf_field import ContentTypeRestrictedFileField,pdf_types
from requirements.models import EventCategory
from mig_main.models import AcademicTerm,MemberProfile

def electee_stopped_electing(profile):
    profile.still_electing=False
    profile.save()
    future_events = CalendarEvent.objects.filter(eventshift__attendees=profile,completed=False)
    for event in future_events:
        for shift in event.eventshift_set.all():
            shift.attendees.remove(profile)
            shift.save()
    for group in ElecteeGroup.objects.filter(term=AcademicTerm.get_current_term(),members=profile):
        group.members.remove(profile)
        group.save()

def add_group_threshold_pts(grp):
    evts = CalendarEvent.objects.filter(completed=True,term=grp.term)
    num_members = grp.members.all().count()
    service_cat = EventCategory.objects.get(name='Service Hours')
    social_cat = EventCategory.objects.get(name='Social Credits')
    service_query = service_cat.get_children(Q())
    social_query = social_cat.get_children(Q())

    threshold_evts = grp.electeegroupevent_set.filter(~Q(related_event_id=None))
    threshold_evts.delete()
    #service
    for evt in evts.filter(service_query):
        if grp.electeegroupevent_set.filter(related_event_id = evt.id).count()>0:
            continue
        num_attendees = 0
        for group_member in grp.members.all():
            if evt.eventshift_set.filter(attendees=group_member).exists():
                num_attendees+=1
        if num_attendees==num_members:
            grp_pts = grp.electeegroupevent_set.create(description='Threshold Attendance Event: '+evt.name,points = 30,related_event_id=evt.id)
            grp_pts.save()
        elif num_attendees>=num_members/2.0:
            grp_pts = grp.electeegroupevent_set.create(description='Threshold Attendance Event: '+evt.name,points = 15,related_event_id=evt.id)
            grp_pts.save()
    #social
    for evt in evts.filter(social_query):
        if grp.electeegroupevent_set.filter(related_event_id = evt.id).count()>0:
            continue
        num_attendees = 0
        for group_member in grp.members.all():
            if evt.eventshift_set.filter(attendees=group_member).exists():
                num_attendees+=1
        if num_attendees==num_members:
            grp_pts = grp.electeegroupevent_set.create(description='Threshold Attendance Event: '+evt.name,points = 10,related_event_id=evt.id)
            grp_pts.save()
        elif num_attendees>=num_members/2.0:
            grp_pts = grp.electeegroupevent_set.create(description='Threshold Attendance Event: '+evt.name,points = 5,related_event_id=evt.id)
            grp_pts.save()
    grp.electeegroupevent_set.filter(description='half_members_3_socials').delete()
    grp.electeegroupevent_set.filter(description='All_members_3_socials').delete()
    num_w_three = 0
    for group_member in grp.members.all():
        socials_attended = evts.filter(social_query).filter(eventshift__attendees=group_member).distinct().count()
        if socials_attended>=3:
            num_w_three+=1
    if num_w_three == num_members:
        grp_pts = grp.electeegroupevent_set.create(description='All_members_3_socials',points=40)
        grp_pts.save()
    elif num_w_three >=num_members/2.0:
        grp_pts = grp.electeegroupevent_set.create(description='half_members_3_socials',points=20)
        grp_pts.save()

# Create your models here.
class ElecteeGroup(models.Model):
    group_name  = models.CharField('Team Name',max_length=50)
    leaders     = models.ManyToManyField('mig_main.MemberProfile',
                                         related_name ="electee_group_leaders")
    officers    = models.ManyToManyField('mig_main.MemberProfile',
                                          related_name ="electee_group_officers")
    members     = models.ManyToManyField('mig_main.MemberProfile',
                                          related_name ="electee_group_members")
    points      = models.PositiveSmallIntegerField(default=0)
    term        = models.ForeignKey('mig_main.AcademicTerm')
    @classmethod
    def get_current_leaders(cls):
        current_groups = cls.objects.filter(term=AcademicTerm.get_current_term())
        return MemberProfile.objects.filter(Q(electee_group_leaders__in=current_groups)|Q(electee_group_officers__in=current_groups)).distinct().order_by('last_name','first_name','uniqname')
    def __unicode__(self):
        return unicode(self.term)+": "+self.group_name
    def add_threshold_attendance_points(self):
        add_group_threshold_pts(self)

    def sum_group_points(self):
        evts = self.electeegroupevent_set.all()
        temp_pts = 0
        for evt in evts:
            temp_pts +=evt.points
        self.points = temp_pts
        self.save()
    def get_points(self):
        self.add_threshold_attendance_points()
        self.sum_group_points()
        return self.points
    def get_ranking(self):
        res= ElecteeGroup.objects.filter(term=self.term,points__gt=self.points).aggregate(ranking=Count('points'))
        return res['ranking']+1
class ElecteeGroupEvent(models.Model):
    electee_group = models.ForeignKey(ElecteeGroup,verbose_name='Electee Team')
    description = models.TextField()
    points      = models.PositiveSmallIntegerField()
    related_event_id = models.PositiveIntegerField(null=True,blank=True,default=None)

class ElecteeResourceType(models.Model):
    name = models.CharField(max_length = 128)
    is_packet = models.BooleanField(default=False)

    def __unicode__(self):
        return self.name

class ElecteeResource(models.Model):
    resource_type = models.ForeignKey(ElecteeResourceType)
    term = models.ForeignKey('mig_main.AcademicTerm')

    resource_file          = ContentTypeRestrictedFileField(
        upload_to='electee_resources',
        content_types=pdf_types,
        max_upload_size=104857600,
        blank=False
    )

    def __unicode__(self):
        return str(self.resource_type)+' for '+str(self.term)

class EducationalBackgroundForm(models.Model):
    degree_type=models.CharField(max_length = 16)
    member = models.ForeignKey('mig_main.MemberProfile')
    term = models.ForeignKey('mig_main.AcademicTerm')

class BackgroundInstitution(models.Model):
    form = models.ForeignKey(EducationalBackgroundForm)
    name = models.CharField(max_length=128,verbose_name='Institution Name')
    degree_type = models.CharField(max_length = 16)
    major = models.CharField(max_length = 128)
    degree_start_date = models.DateField()
    degree_end_date = models.DateField()

class SurveyPart(models.Model):
    VISIBILITY_OPTIONS = (
        ('E','Fellow Electees'),
        ('A','Active Members'),
        ('M','All Members'),
        ('R','Only Admins/VPs'),
    )
    title = models.CharField(max_length=128)
    number_of_required_questions = models.PositiveSmallIntegerField(blank=True,null=True)
    display_order = models.PositiveSmallIntegerField(default=1)
    visibility = models.CharField(max_length=1,choices=VISIBILITY_OPTIONS, default='R')
    instructions = models.TextField(blank=True,null=True)
    def __unicode__(self):
        return self.title
    def __gt__(self,part2):
        if not hasattr(part2,'display_order') or not hasattr(part2,'id'):
            return True
        if self.display_order > part2.display_order:
            return True
        if (self.display_order == part2.display_order) and self.id > part2.id:
            return True
        return False

    def __lt__(self,part2):
        if not hasattr(part2,'display_order') or not hasattr(part2,'id'):
            return False
        if self.display_order < part2.display_order:
            return True
        if (self.display_order == part2.display_order) and self.id < part2.id:
            return True
        return False
    def __le__(self,part2):
        return not self > part2
    def __ge__(self,part2):
        return not self < part2
class SurveyQuestion(models.Model):
    short_name = models.CharField(max_length=64)
    text = models.TextField()
    part = models.ForeignKey(SurveyPart)
    max_words = models.PositiveSmallIntegerField(blank=True,null=True)
    display_order = models.PositiveSmallIntegerField(default=1)

    def __unicode__(self):
        return self.short_name+'('+unicode(self.part)+')'

class SurveyAnswer(models.Model):
    term = models.ForeignKey('mig_main.AcademicTerm')
    question = models.ForeignKey(SurveyQuestion)
    submitter = models.ForeignKey('mig_main.MemberProfile')
    answer = models.TextField()

    def __unicode__(self):
        return self.submitter.uniqname+'\'s answer to '+unicode(self.question)

class ElecteeInterviewSurvey(models.Model):
    term = models.ForeignKey('mig_main.AcademicTerm')
    questions= models.ManyToManyField(SurveyQuestion)
    due_date = models.DateField()
    instructions = models.TextField(blank=True,null=True)
    def __unicode__(self):
        return 'Electee Survey for '+unicode(self.term)

    def check_if_electee_completed(self,electee):
        parts = SurveyPart.objects.filter(surveyquestion__in=self.questions.all()).distinct()
        completed = True
        for part in parts:
            num_req = part.number_of_required_questions
            part_answers = SurveyAnswer.objects.filter(submitter=electee,term=self.term,question__part=part).count()
            if (not num_req is None) and part_answers < num_req:
                return False
        if not electee.resume:
            return False
        return True

class ElecteeInterviewFollowup(models.Model):
    RECOMMENDATION_CHOICES = (
        ('Y','Recommend'),
        ('M','Not Sure'),
        ('N','Do not recommend'),
    )

    language_barrier = models.BooleanField(default=False)
    recommendation = models.CharField(max_length=1,choices=RECOMMENDATION_CHOICES)
    comments = models.TextField(blank=True)
    member =models.ForeignKey('mig_main.MemberProfile')
    interview = models.ForeignKey('event_cal.InterviewShift')

class ElecteeProcessVisibility(models.Model):
    term = models.ForeignKey('mig_main.AcademicTerm',unique=True)
    followups_visible = models.BooleanField(default=False)
<commit_msg>simplify electee group rank calculation<commit_after>from django.db import models
from django.db.models import Q,Count
from django.core.validators import  RegexValidator

from event_cal.models import CalendarEvent
from mig_main.pdf_field import ContentTypeRestrictedFileField,pdf_types
from requirements.models import EventCategory
from mig_main.models import AcademicTerm,MemberProfile

def electee_stopped_electing(profile):
    profile.still_electing=False
    profile.save()
    future_events = CalendarEvent.objects.filter(eventshift__attendees=profile,completed=False)
    for event in future_events:
        for shift in event.eventshift_set.all():
            shift.attendees.remove(profile)
            shift.save()
    for group in ElecteeGroup.objects.filter(term=AcademicTerm.get_current_term(),members=profile):
        group.members.remove(profile)
        group.save()

def add_group_threshold_pts(grp):
    evts = CalendarEvent.objects.filter(completed=True,term=grp.term)
    num_members = grp.members.all().count()
    service_cat = EventCategory.objects.get(name='Service Hours')
    social_cat = EventCategory.objects.get(name='Social Credits')
    service_query = service_cat.get_children(Q())
    social_query = social_cat.get_children(Q())

    threshold_evts = grp.electeegroupevent_set.filter(~Q(related_event_id=None))
    threshold_evts.delete()
    #service
    for evt in evts.filter(service_query):
        if grp.electeegroupevent_set.filter(related_event_id = evt.id).count()>0:
            continue
        num_attendees = 0
        for group_member in grp.members.all():
            if evt.eventshift_set.filter(attendees=group_member).exists():
                num_attendees+=1
        if num_attendees==num_members:
            grp_pts = grp.electeegroupevent_set.create(description='Threshold Attendance Event: '+evt.name,points = 30,related_event_id=evt.id)
            grp_pts.save()
        elif num_attendees>=num_members/2.0:
            grp_pts = grp.electeegroupevent_set.create(description='Threshold Attendance Event: '+evt.name,points = 15,related_event_id=evt.id)
            grp_pts.save()
    #social
    for evt in evts.filter(social_query):
        if grp.electeegroupevent_set.filter(related_event_id = evt.id).count()>0:
            continue
        num_attendees = 0
        for group_member in grp.members.all():
            if evt.eventshift_set.filter(attendees=group_member).exists():
                num_attendees+=1
        if num_attendees==num_members:
            grp_pts = grp.electeegroupevent_set.create(description='Threshold Attendance Event: '+evt.name,points = 10,related_event_id=evt.id)
            grp_pts.save()
        elif num_attendees>=num_members/2.0:
            grp_pts = grp.electeegroupevent_set.create(description='Threshold Attendance Event: '+evt.name,points = 5,related_event_id=evt.id)
            grp_pts.save()
    grp.electeegroupevent_set.filter(description='half_members_3_socials').delete()
    grp.electeegroupevent_set.filter(description='All_members_3_socials').delete()
    num_w_three = 0
    for group_member in grp.members.all():
        socials_attended = evts.filter(social_query).filter(eventshift__attendees=group_member).distinct().count()
        if socials_attended>=3:
            num_w_three+=1
    if num_w_three == num_members:
        grp_pts = grp.electeegroupevent_set.create(description='All_members_3_socials',points=40)
        grp_pts.save()
    elif num_w_three >=num_members/2.0:
        grp_pts = grp.electeegroupevent_set.create(description='half_members_3_socials',points=20)
        grp_pts.save()

# Create your models here.
class ElecteeGroup(models.Model):
    group_name  = models.CharField('Team Name',max_length=50)
    leaders     = models.ManyToManyField('mig_main.MemberProfile',
                                         related_name ="electee_group_leaders")
    officers    = models.ManyToManyField('mig_main.MemberProfile',
                                          related_name ="electee_group_officers")
    members     = models.ManyToManyField('mig_main.MemberProfile',
                                          related_name ="electee_group_members")
    points      = models.PositiveSmallIntegerField(default=0)
    term        = models.ForeignKey('mig_main.AcademicTerm')
    @classmethod
    def get_current_leaders(cls):
        current_groups = cls.objects.filter(term=AcademicTerm.get_current_term())
        return MemberProfile.objects.filter(Q(electee_group_leaders__in=current_groups)|Q(electee_group_officers__in=current_groups)).distinct().order_by('last_name','first_name','uniqname')
    def __unicode__(self):
        return unicode(self.term)+": "+self.group_name
    def add_threshold_attendance_points(self):
        add_group_threshold_pts(self)

    def sum_group_points(self):
        evts = self.electeegroupevent_set.all()
        temp_pts = 0
        for evt in evts:
            temp_pts +=evt.points
        self.points = temp_pts
        self.save()
    def get_points(self):
        self.add_threshold_attendance_points()
        self.sum_group_points()
        return self.points
    def get_ranking(self):
        return ElecteeGroup.objects.filter(term=self.term,points__gt=self.points).count()+1

class ElecteeGroupEvent(models.Model):
    electee_group = models.ForeignKey(ElecteeGroup,verbose_name='Electee Team')
    description = models.TextField()
    points      = models.PositiveSmallIntegerField()
    related_event_id = models.PositiveIntegerField(null=True,blank=True,default=None)

class ElecteeResourceType(models.Model):
    name = models.CharField(max_length = 128)
    is_packet = models.BooleanField(default=False)

    def __unicode__(self):
        return self.name

class ElecteeResource(models.Model):
    resource_type = models.ForeignKey(ElecteeResourceType)
    term = models.ForeignKey('mig_main.AcademicTerm')

    resource_file          = ContentTypeRestrictedFileField(
        upload_to='electee_resources',
        content_types=pdf_types,
        max_upload_size=104857600,
        blank=False
    )

    def __unicode__(self):
        return str(self.resource_type)+' for '+str(self.term)

class EducationalBackgroundForm(models.Model):
    degree_type=models.CharField(max_length = 16)
    member = models.ForeignKey('mig_main.MemberProfile')
    term = models.ForeignKey('mig_main.AcademicTerm')

class BackgroundInstitution(models.Model):
    form = models.ForeignKey(EducationalBackgroundForm)
    name = models.CharField(max_length=128,verbose_name='Institution Name')
    degree_type = models.CharField(max_length = 16)
    major = models.CharField(max_length = 128)
    degree_start_date = models.DateField()
    degree_end_date = models.DateField()

class SurveyPart(models.Model):
    VISIBILITY_OPTIONS = (
        ('E','Fellow Electees'),
        ('A','Active Members'),
        ('M','All Members'),
        ('R','Only Admins/VPs'),
    )
    title = models.CharField(max_length=128)
    number_of_required_questions = models.PositiveSmallIntegerField(blank=True,null=True)
    display_order = models.PositiveSmallIntegerField(default=1)
    visibility = models.CharField(max_length=1,choices=VISIBILITY_OPTIONS, default='R')
    instructions = models.TextField(blank=True,null=True)
    def __unicode__(self):
        return self.title
    def __gt__(self,part2):
        if not hasattr(part2,'display_order') or not hasattr(part2,'id'):
            return True
        if self.display_order > part2.display_order:
            return True
        if (self.display_order == part2.display_order) and self.id > part2.id:
            return True
        return False

    def __lt__(self,part2):
        if not hasattr(part2,'display_order') or not hasattr(part2,'id'):
            return False
        if self.display_order < part2.display_order:
            return True
        if (self.display_order == part2.display_order) and self.id < part2.id:
            return True
        return False
    def __le__(self,part2):
        return not self > part2
    def __ge__(self,part2):
        return not self < part2
class SurveyQuestion(models.Model):
    short_name = models.CharField(max_length=64)
    text = models.TextField()
    part = models.ForeignKey(SurveyPart)
    max_words = models.PositiveSmallIntegerField(blank=True,null=True)
    display_order = models.PositiveSmallIntegerField(default=1)

    def __unicode__(self):
        return self.short_name+'('+unicode(self.part)+')'

class SurveyAnswer(models.Model):
    term = models.ForeignKey('mig_main.AcademicTerm')
    question = models.ForeignKey(SurveyQuestion)
    submitter = models.ForeignKey('mig_main.MemberProfile')
    answer = models.TextField()

    def __unicode__(self):
        return self.submitter.uniqname+'\'s answer to '+unicode(self.question)

class ElecteeInterviewSurvey(models.Model):
    term = models.ForeignKey('mig_main.AcademicTerm')
    questions= models.ManyToManyField(SurveyQuestion)
    due_date = models.DateField()
    instructions = models.TextField(blank=True,null=True)
    def __unicode__(self):
        return 'Electee Survey for '+unicode(self.term)

    def check_if_electee_completed(self,electee):
        parts = SurveyPart.objects.filter(surveyquestion__in=self.questions.all()).distinct()
        completed = True
        for part in parts:
            num_req = part.number_of_required_questions
            part_answers = SurveyAnswer.objects.filter(submitter=electee,term=self.term,question__part=part).count()
            if (not num_req is None) and part_answers < num_req:
                return False
        if not electee.resume:
            return False
        return True

class ElecteeInterviewFollowup(models.Model):
    RECOMMENDATION_CHOICES = (
        ('Y','Recommend'),
        ('M','Not Sure'),
        ('N','Do not recommend'),
    )

    language_barrier = models.BooleanField(default=False)
    recommendation = models.CharField(max_length=1,choices=RECOMMENDATION_CHOICES)
    comments = models.TextField(blank=True)
    member =models.ForeignKey('mig_main.MemberProfile')
    interview = models.ForeignKey('event_cal.InterviewShift')

class ElecteeProcessVisibility(models.Model):
    term = models.ForeignKey('mig_main.AcademicTerm',unique=True)
    followups_visible = models.BooleanField(default=False)
<|endoftext|>


<commit_before>import copy
import os
import warnings
from binascii import unhexlify

import gevent
import structlog
from eth_utils import encode_hex, to_canonical_address, to_checksum_address
from gevent.lock import Semaphore
from pkg_resources import DistributionNotFound
from requests import ConnectTimeout
from web3 import Web3
from web3.gas_strategies.rpc import rpc_gas_price_strategy
from web3.middleware import geth_poa_middleware

from raiden.constants import NULL_ADDRESS
from raiden.exceptions import AddressWithoutCode, EthNodeCommunicationError
from raiden.network.rpc.middleware import block_hash_cache_middleware, connection_test_middleware
from raiden.network.rpc.smartcontract_proxy import ContractProxy
from raiden.utils import is_supported_client, pex, privatekey_to_address
from raiden.utils.filters import StatelessFilter
from raiden.utils.solc import (
    solidity_library_symbol,
    solidity_resolve_symbols,
    solidity_unresolved_symbols,
)
from raiden.utils.typing import Address, BlockSpecification, Callable, Dict, List

try:
    from eth_tester.exceptions import BlockNotFound
except (ModuleNotFoundError, DistributionNotFound):
    class BlockNotFound(Exception):
        pass

log = structlog.get_logger(__name__)  # pylint: disable=invalid-name

def check_address_has_code(
        client: 'JSONRPCClient',
        address: Address,
        contract_name: str = '',
):
    """ Checks that the given address contains code. """
    result = client.web3.eth.getCode(to_checksum_address(address), 'latest')

    if not result:
        if contract_name:
            formated_contract_name = '[{}]: '.format(contract_name)
        else:
            formated_contract_name = ''

        raise AddressWithoutCode('{}Address {} does not contain code'.format(
            formated_contract_name,
            to_checksum_address(address),
        ))

def deploy_dependencies_symbols(all_contract):
    dependencies = {}

    symbols_to_contract = dict()
    for contract_name in all_contract:
        symbol = solidity_library_symbol(contract_name)

        if symbol in symbols_to_contract:
            raise ValueError('Conflicting library names.')

        symbols_to_contract[symbol] = contract_name

    for contract_name, contract in all_contract.items():
        unresolved_symbols = solidity_unresolved_symbols(contract['bin'])
        dependencies[contract_name] = [
            symbols_to_contract[unresolved]
            for unresolved in unresolved_symbols
        ]

    return dependencies

def dependencies_order_of_build(target_contract, dependencies_map):
    """ Return an ordered list of contracts that is sufficient to successfully
    deploy the target contract.

    Note:
        This function assumes that the `dependencies_map` is an acyclic graph.
    """
    if not dependencies_map:
        return [target_contract]

    if target_contract not in dependencies_map:
        raise ValueError('no dependencies defined for {}'.format(target_contract))

    order = [target_contract]
    todo = list(dependencies_map[target_contract])

    while todo:
        target_contract = todo.pop(0)
        target_pos = len(order)

        for dependency in dependencies_map[target_contract]:
            # we need to add the current contract before all its depedencies
            if dependency in order:
                target_pos = order.index(dependency)
            else:
                todo.append(dependency)

        order.insert(target_pos, target_contract)

    order.reverse()
    return order

def monkey_patch_web3(web3, gas_price_strategy):
    try:
        # install caching middleware
        web3.middleware_stack.add(block_hash_cache_middleware)

        # set gas price strategy
        web3.eth.setGasPriceStrategy(gas_price_strategy)

        # we use a PoA chain for smoketest, use this middleware to fix this
        web3.middleware_stack.inject(geth_poa_middleware, layer=0)
    except ValueError:
        # `middleware_stack.inject()` raises a value error if the same middleware is
        # injected twice. This happens with `eth-tester` setup where a single session
        # scoped web3 instance is used for all clients
        pass

    # create the connection test middleware (but only for non-tester chain)
    if not hasattr(web3, 'testing'):
        web3.middleware_stack.inject(connection_test_middleware, layer=0)

class JSONRPCClient:
    """ Ethereum JSON RPC client.

    Args:
        host: Ethereum node host address.
        port: Ethereum node port number.
        privkey: Local user private key, used to sign transactions.
        nonce_offset: Network's default base nonce number.
    """

    def __init__(
            self,
            web3: Web3,
            privkey: bytes,
            gas_price_strategy: Callable = rpc_gas_price_strategy,
            nonce_offset: int = 0,
    ):
        if privkey is None or len(privkey) != 32:
            raise ValueError('Invalid private key')

        monkey_patch_web3(web3, gas_price_strategy)

        try:
            version = web3.version.node
        except ConnectTimeout:
            raise EthNodeCommunicationError('couldnt reach the ethereum node')

        _, eth_node = is_supported_client(version)

        sender = privatekey_to_address(privkey)
        transaction_count = web3.eth.getTransactionCount(to_checksum_address(sender), 'pending')
        _available_nonce = transaction_count + nonce_offset

        self.eth_node = eth_node
        self.privkey = privkey
        self.sender = sender
        self.web3 = web3

        self._available_nonce = _available_nonce
        self._nonce_lock = Semaphore()
        self._nonce_offset = nonce_offset

        log.debug(
            'JSONRPCClient created',
            sender=pex(self.sender),
            available_nonce=_available_nonce,
        )

    def __repr__(self):
        return f'<JSONRPCClient node:{pex(self.sender)} nonce:{self._available_nonce}>'

    def block_number(self):
        """ Return the most recent block. """
        return self.web3.eth.blockNumber

    def balance(self, account: Address):
        """ Return the balance of the account of given address. """
        return self.web3.eth.getBalance(to_checksum_address(account), 'pending')

    def gaslimit(self, location='latest') -> int:
        gas_limit = self.web3.eth.getBlock(location)['gasLimit']
        return gas_limit * 8 // 10

    def gas_price(self) -> int:
        # generateGasPrice takes the transaction to be send as an optional argument
        # but both strategies that we are using (time-based and rpc-based) don't make
        # use of this argument. It is therefore safe to not provide it at the moment.
        # This needs to be reevaluated if we use different gas price strategies
        return int(self.web3.eth.generateGasPrice())

    def check_startgas(self, startgas):
        if not startgas:
            return self.gaslimit()
        return startgas

    def new_contract_proxy(self, contract_interface, contract_address: Address):
        """ Return a proxy for interacting with a smart contract.

        Args:
            contract_interface: The contract interface as defined by the json.
            address: The contract's address.
        """
        return ContractProxy(
            self,
            contract=self.new_contract(contract_interface, contract_address),
        )

    def new_contract(self, contract_interface: Dict, contract_address: Address):
        return self.web3.eth.contract(
            abi=contract_interface,
            address=to_checksum_address(contract_address),
        )

    def get_transaction_receipt(self, tx_hash: bytes):
        return self.web3.eth.getTransactionReceipt(encode_hex(tx_hash))

    def deploy_solidity_contract(
            self,  # pylint: disable=too-many-locals
            contract_name,
            all_contracts,
            libraries=None,
            constructor_parameters=None,
            contract_path=None,
            confirmations=None,
    ):
        """
        Deploy a solidity contract.
        Args:
            sender (address): the sender address
            contract_name (str): the name of the contract to compile
            all_contracts (dict): the json dictionary containing the result of compiling a file
            libraries (list): A list of libraries to use in deployment
            constructor_parameters (tuple): A tuple of arguments to pass to the constructor
            contract_path (str): If we are dealing with solc >= v0.4.9 then the path
                                 to the contract is a required argument to extract
                                 the contract data from the `all_contracts` dict.
        """
        libraries = dict(libraries) or dict()
        constructor_parameters = constructor_parameters or list()
        all_contracts = copy.deepcopy(all_contracts)

        if contract_name in all_contracts:
            contract_key = contract_name

        elif contract_path is not None:
            contract_key = os.path.basename(contract_path) + ':' + contract_name

            if contract_key not in all_contracts:
                raise ValueError('Unknown contract {}'.format(contract_name))
        else:
            raise ValueError(
                'Unknown contract {} and no contract_path given'.format(contract_name),
            )

        contract = all_contracts[contract_key]
        contract_interface = contract['abi']
        symbols = solidity_unresolved_symbols(contract['bin'])

        if symbols:
            available_symbols = list(map(solidity_library_symbol, all_contracts.keys()))

            unknown_symbols = set(symbols) - set(available_symbols)
            if unknown_symbols:
                msg = 'Cannot deploy contract, known symbols {}, unresolved symbols {}.'.format(
                    available_symbols,
                    unknown_symbols,
                )
                raise Exception(msg)

            dependencies = deploy_dependencies_symbols(all_contracts)
            deployment_order = dependencies_order_of_build(contract_key, dependencies)

            deployment_order.pop()  # remove `contract_name` from the list

            log.debug('Deploying dependencies: {}'.format(str(deployment_order)))

            for deploy_contract in deployment_order:
                dependency_contract = all_contracts[deploy_contract]

                hex_bytecode = solidity_resolve_symbols(dependency_contract['bin'], libraries)
                bytecode = unhexlify(hex_bytecode)

                dependency_contract['bin'] = bytecode

                transaction_hash = self.send_transaction(
                    to=Address(b''),
                    data=bytecode,
                )

                self.poll(transaction_hash)
                receipt = self.get_transaction_receipt(transaction_hash)

                contract_address = receipt['contractAddress']
                # remove the hexadecimal prefix 0x from the address
                contract_address = contract_address[2:]

                libraries[deploy_contract] = contract_address

                deployed_code = self.web3.eth.getCode(to_checksum_address(contract_address))

                if not deployed_code:
                    raise RuntimeError('Contract address has no code, check gas usage.')

            hex_bytecode = solidity_resolve_symbols(contract['bin'], libraries)
            bytecode = unhexlify(hex_bytecode)

            contract['bin'] = bytecode

        if isinstance(contract['bin'], str):
            contract['bin'] = unhexlify(contract['bin'])

        if not constructor_parameters:
            constructor_parameters = ()

        contract = self.web3.eth.contract(abi=contract['abi'], bytecode=contract['bin'])
        contract_transaction = contract.constructor(*constructor_parameters).buildTransaction()
        transaction_hash = self.send_transaction(
            to=Address(b''),
            data=contract_transaction['data'],
            startgas=contract_transaction['gas'],
        )

        self.poll(transaction_hash, confirmations)
        receipt = self.get_transaction_receipt(transaction_hash)
        contract_address = receipt['contractAddress']

        deployed_code = self.web3.eth.getCode(to_checksum_address(contract_address))

        if not deployed_code:
            raise RuntimeError(
                'Deployment of {} failed. Contract address has no code, check gas usage.'.format(
                    contract_name,
                ),
            )

        return self.new_contract_proxy(
            contract_interface,
            contract_address,
        )

    def send_transaction(
            self,
            to: Address,
            value: int = 0,
            data: bytes = b'',
            startgas: int = None,
    ) -> bytes:
        """ Helper to send signed messages.

        This method will use the `privkey` provided in the constructor to
        locally sign the transaction. This requires an extended server
        implementation that accepts the variables v, r, and s.
        """
        if to == to_canonical_address(NULL_ADDRESS):
            warnings.warn('For contract creation the empty string must be used.')

        with self._nonce_lock:
            nonce = self._available_nonce
            startgas = self.check_startgas(startgas)
            gas_price = self.gas_price()

            transaction = {
                'data': data,
                'gas': startgas,
                'nonce': nonce,
                'value': value,
                'gasPrice': gas_price,
            }
            node_gas_price = self.web3.eth.gasPrice
            log.debug(
                'Calculated gas price for transaction',
                calculated_gas_price=gas_price,
                node_gas_price=node_gas_price,
            )

            # add the to address if not deploying a contract
            if to != b'':
                transaction['to'] = to_checksum_address(to)

            signed_txn = self.web3.eth.account.signTransaction(transaction, self.privkey)

            log_details = {
                'account': to_checksum_address(self.sender),
                'nonce': transaction['nonce'],
                'gasLimit': transaction['gas'],
                'gasPrice': transaction['gasPrice'],
            }
            log.debug('send_raw_transaction called', **log_details)

            tx_hash = self.web3.eth.sendRawTransaction(signed_txn.rawTransaction)
            self._available_nonce += 1

            log.debug('send_raw_transaction returned', tx_hash=encode_hex(tx_hash), **log_details)
            return tx_hash

    def poll(self, transaction_hash: bytes, confirmations: int = None):
        """ Wait until the `transaction_hash` is applied or rejected.

        Args:
            transaction_hash: Transaction hash that we are waiting for.
            confirmations: Number of block confirmations that we will
                wait for.
        """
        if len(transaction_hash) != 32:
            raise ValueError(
                'transaction_hash must be a 32 byte hash',
            )

        transaction_hash = encode_hex(transaction_hash)

        # used to check if the transaction was removed, this could happen
        # if gas price is too low:
        #
        # > Transaction (acbca3d6) below gas price (tx=1 Wei ask=18
        # > Shannon). All sequential txs from this address(7d0eae79)
        # > will be ignored
        #
        last_result = None

        while True:
            # Could return None for a short period of time, until the
            # transaction is added to the pool
            transaction = self.web3.eth.getTransaction(transaction_hash)

            # if the transaction was added to the pool and then removed
            if transaction is None and last_result is not None:
                raise Exception('invalid transaction, check gas price')

            # the transaction was added to the pool and mined
            if transaction and transaction['blockNumber'] is not None:
                break

            last_result = transaction
            gevent.sleep(.5)

        if confirmations:
            # this will wait for both APPLIED and REVERTED transactions
            transaction_block = transaction['blockNumber']
            confirmation_block = transaction_block + confirmations

            block_number = self.block_number()

            while block_number < confirmation_block:
                gevent.sleep(.5)
                block_number = self.block_number()

    def new_filter(
            self,
            contract_address: Address,
            topics: List[str] = None,
            from_block: BlockSpecification = 0,
            to_block: BlockSpecification = 'latest',
    ) -> StatelessFilter:
        """ Create a filter in the ethereum node. """
        return StatelessFilter(
            self.web3,
            {
                'fromBlock': from_block,
                'toBlock': to_block,
                'address': to_checksum_address(contract_address),
                'topics': topics,
            },
        )

    def get_filter_events(
            self,
            contract_address: Address,
            topics: List[str] = None,
            from_block: BlockSpecification = 0,
            to_block: BlockSpecification = 'latest',
    ) -> List[Dict]:
        """ Get events for the given query. """
        try:
            return self.web3.eth.getLogs({
                'fromBlock': from_block,
                'toBlock': to_block,
                'address': to_checksum_address(contract_address),
                'topics': topics,
            })
        except BlockNotFound:
            return []
<commit_msg>bugfix: libraries could be None<commit_after>import copy
import os
import warnings
from binascii import unhexlify

import gevent
import structlog
from eth_utils import encode_hex, to_canonical_address, to_checksum_address
from gevent.lock import Semaphore
from pkg_resources import DistributionNotFound
from requests import ConnectTimeout
from web3 import Web3
from web3.gas_strategies.rpc import rpc_gas_price_strategy
from web3.middleware import geth_poa_middleware

from raiden.constants import NULL_ADDRESS
from raiden.exceptions import AddressWithoutCode, EthNodeCommunicationError
from raiden.network.rpc.middleware import block_hash_cache_middleware, connection_test_middleware
from raiden.network.rpc.smartcontract_proxy import ContractProxy
from raiden.utils import is_supported_client, pex, privatekey_to_address
from raiden.utils.filters import StatelessFilter
from raiden.utils.solc import (
    solidity_library_symbol,
    solidity_resolve_symbols,
    solidity_unresolved_symbols,
)
from raiden.utils.typing import Address, BlockSpecification, Callable, Dict, List

try:
    from eth_tester.exceptions import BlockNotFound
except (ModuleNotFoundError, DistributionNotFound):
    class BlockNotFound(Exception):
        pass

log = structlog.get_logger(__name__)  # pylint: disable=invalid-name

def check_address_has_code(
        client: 'JSONRPCClient',
        address: Address,
        contract_name: str = '',
):
    """ Checks that the given address contains code. """
    result = client.web3.eth.getCode(to_checksum_address(address), 'latest')

    if not result:
        if contract_name:
            formated_contract_name = '[{}]: '.format(contract_name)
        else:
            formated_contract_name = ''

        raise AddressWithoutCode('{}Address {} does not contain code'.format(
            formated_contract_name,
            to_checksum_address(address),
        ))

def deploy_dependencies_symbols(all_contract):
    dependencies = {}

    symbols_to_contract = dict()
    for contract_name in all_contract:
        symbol = solidity_library_symbol(contract_name)

        if symbol in symbols_to_contract:
            raise ValueError('Conflicting library names.')

        symbols_to_contract[symbol] = contract_name

    for contract_name, contract in all_contract.items():
        unresolved_symbols = solidity_unresolved_symbols(contract['bin'])
        dependencies[contract_name] = [
            symbols_to_contract[unresolved]
            for unresolved in unresolved_symbols
        ]

    return dependencies

def dependencies_order_of_build(target_contract, dependencies_map):
    """ Return an ordered list of contracts that is sufficient to successfully
    deploy the target contract.

    Note:
        This function assumes that the `dependencies_map` is an acyclic graph.
    """
    if not dependencies_map:
        return [target_contract]

    if target_contract not in dependencies_map:
        raise ValueError('no dependencies defined for {}'.format(target_contract))

    order = [target_contract]
    todo = list(dependencies_map[target_contract])

    while todo:
        target_contract = todo.pop(0)
        target_pos = len(order)

        for dependency in dependencies_map[target_contract]:
            # we need to add the current contract before all its depedencies
            if dependency in order:
                target_pos = order.index(dependency)
            else:
                todo.append(dependency)

        order.insert(target_pos, target_contract)

    order.reverse()
    return order

def monkey_patch_web3(web3, gas_price_strategy):
    try:
        # install caching middleware
        web3.middleware_stack.add(block_hash_cache_middleware)

        # set gas price strategy
        web3.eth.setGasPriceStrategy(gas_price_strategy)

        # we use a PoA chain for smoketest, use this middleware to fix this
        web3.middleware_stack.inject(geth_poa_middleware, layer=0)
    except ValueError:
        # `middleware_stack.inject()` raises a value error if the same middleware is
        # injected twice. This happens with `eth-tester` setup where a single session
        # scoped web3 instance is used for all clients
        pass

    # create the connection test middleware (but only for non-tester chain)
    if not hasattr(web3, 'testing'):
        web3.middleware_stack.inject(connection_test_middleware, layer=0)

class JSONRPCClient:
    """ Ethereum JSON RPC client.

    Args:
        host: Ethereum node host address.
        port: Ethereum node port number.
        privkey: Local user private key, used to sign transactions.
        nonce_offset: Network's default base nonce number.
    """

    def __init__(
            self,
            web3: Web3,
            privkey: bytes,
            gas_price_strategy: Callable = rpc_gas_price_strategy,
            nonce_offset: int = 0,
    ):
        if privkey is None or len(privkey) != 32:
            raise ValueError('Invalid private key')

        monkey_patch_web3(web3, gas_price_strategy)

        try:
            version = web3.version.node
        except ConnectTimeout:
            raise EthNodeCommunicationError('couldnt reach the ethereum node')

        _, eth_node = is_supported_client(version)

        sender = privatekey_to_address(privkey)
        transaction_count = web3.eth.getTransactionCount(to_checksum_address(sender), 'pending')
        _available_nonce = transaction_count + nonce_offset

        self.eth_node = eth_node
        self.privkey = privkey
        self.sender = sender
        self.web3 = web3

        self._available_nonce = _available_nonce
        self._nonce_lock = Semaphore()
        self._nonce_offset = nonce_offset

        log.debug(
            'JSONRPCClient created',
            sender=pex(self.sender),
            available_nonce=_available_nonce,
        )

    def __repr__(self):
        return f'<JSONRPCClient node:{pex(self.sender)} nonce:{self._available_nonce}>'

    def block_number(self):
        """ Return the most recent block. """
        return self.web3.eth.blockNumber

    def balance(self, account: Address):
        """ Return the balance of the account of given address. """
        return self.web3.eth.getBalance(to_checksum_address(account), 'pending')

    def gaslimit(self, location='latest') -> int:
        gas_limit = self.web3.eth.getBlock(location)['gasLimit']
        return gas_limit * 8 // 10

    def gas_price(self) -> int:
        # generateGasPrice takes the transaction to be send as an optional argument
        # but both strategies that we are using (time-based and rpc-based) don't make
        # use of this argument. It is therefore safe to not provide it at the moment.
        # This needs to be reevaluated if we use different gas price strategies
        return int(self.web3.eth.generateGasPrice())

    def check_startgas(self, startgas):
        if not startgas:
            return self.gaslimit()
        return startgas

    def new_contract_proxy(self, contract_interface, contract_address: Address):
        """ Return a proxy for interacting with a smart contract.

        Args:
            contract_interface: The contract interface as defined by the json.
            address: The contract's address.
        """
        return ContractProxy(
            self,
            contract=self.new_contract(contract_interface, contract_address),
        )

    def new_contract(self, contract_interface: Dict, contract_address: Address):
        return self.web3.eth.contract(
            abi=contract_interface,
            address=to_checksum_address(contract_address),
        )

    def get_transaction_receipt(self, tx_hash: bytes):
        return self.web3.eth.getTransactionReceipt(encode_hex(tx_hash))

    def deploy_solidity_contract(
            self,  # pylint: disable=too-many-locals
            contract_name,
            all_contracts,
            libraries=None,
            constructor_parameters=None,
            contract_path=None,
            confirmations=None,
    ):
        """
        Deploy a solidity contract.
        Args:
            sender (address): the sender address
            contract_name (str): the name of the contract to compile
            all_contracts (dict): the json dictionary containing the result of compiling a file
            libraries (list): A list of libraries to use in deployment
            constructor_parameters (tuple): A tuple of arguments to pass to the constructor
            contract_path (str): If we are dealing with solc >= v0.4.9 then the path
                                 to the contract is a required argument to extract
                                 the contract data from the `all_contracts` dict.
        """
        if libraries:
            libraries = dict(libraries)
        else:
            libraries = dict()

        constructor_parameters = constructor_parameters or list()
        all_contracts = copy.deepcopy(all_contracts)

        if contract_name in all_contracts:
            contract_key = contract_name

        elif contract_path is not None:
            contract_key = os.path.basename(contract_path) + ':' + contract_name

            if contract_key not in all_contracts:
                raise ValueError('Unknown contract {}'.format(contract_name))
        else:
            raise ValueError(
                'Unknown contract {} and no contract_path given'.format(contract_name),
            )

        contract = all_contracts[contract_key]
        contract_interface = contract['abi']
        symbols = solidity_unresolved_symbols(contract['bin'])

        if symbols:
            available_symbols = list(map(solidity_library_symbol, all_contracts.keys()))

            unknown_symbols = set(symbols) - set(available_symbols)
            if unknown_symbols:
                msg = 'Cannot deploy contract, known symbols {}, unresolved symbols {}.'.format(
                    available_symbols,
                    unknown_symbols,
                )
                raise Exception(msg)

            dependencies = deploy_dependencies_symbols(all_contracts)
            deployment_order = dependencies_order_of_build(contract_key, dependencies)

            deployment_order.pop()  # remove `contract_name` from the list

            log.debug('Deploying dependencies: {}'.format(str(deployment_order)))

            for deploy_contract in deployment_order:
                dependency_contract = all_contracts[deploy_contract]

                hex_bytecode = solidity_resolve_symbols(dependency_contract['bin'], libraries)
                bytecode = unhexlify(hex_bytecode)

                dependency_contract['bin'] = bytecode

                transaction_hash = self.send_transaction(
                    to=Address(b''),
                    data=bytecode,
                )

                self.poll(transaction_hash)
                receipt = self.get_transaction_receipt(transaction_hash)

                contract_address = receipt['contractAddress']
                # remove the hexadecimal prefix 0x from the address
                contract_address = contract_address[2:]

                libraries[deploy_contract] = contract_address

                deployed_code = self.web3.eth.getCode(to_checksum_address(contract_address))

                if not deployed_code:
                    raise RuntimeError('Contract address has no code, check gas usage.')

            hex_bytecode = solidity_resolve_symbols(contract['bin'], libraries)
            bytecode = unhexlify(hex_bytecode)

            contract['bin'] = bytecode

        if isinstance(contract['bin'], str):
            contract['bin'] = unhexlify(contract['bin'])

        if not constructor_parameters:
            constructor_parameters = ()

        contract = self.web3.eth.contract(abi=contract['abi'], bytecode=contract['bin'])
        contract_transaction = contract.constructor(*constructor_parameters).buildTransaction()
        transaction_hash = self.send_transaction(
            to=Address(b''),
            data=contract_transaction['data'],
            startgas=contract_transaction['gas'],
        )

        self.poll(transaction_hash, confirmations)
        receipt = self.get_transaction_receipt(transaction_hash)
        contract_address = receipt['contractAddress']

        deployed_code = self.web3.eth.getCode(to_checksum_address(contract_address))

        if not deployed_code:
            raise RuntimeError(
                'Deployment of {} failed. Contract address has no code, check gas usage.'.format(
                    contract_name,
                ),
            )

        return self.new_contract_proxy(
            contract_interface,
            contract_address,
        )

    def send_transaction(
            self,
            to: Address,
            value: int = 0,
            data: bytes = b'',
            startgas: int = None,
    ) -> bytes:
        """ Helper to send signed messages.

        This method will use the `privkey` provided in the constructor to
        locally sign the transaction. This requires an extended server
        implementation that accepts the variables v, r, and s.
        """
        if to == to_canonical_address(NULL_ADDRESS):
            warnings.warn('For contract creation the empty string must be used.')

        with self._nonce_lock:
            nonce = self._available_nonce
            startgas = self.check_startgas(startgas)
            gas_price = self.gas_price()

            transaction = {
                'data': data,
                'gas': startgas,
                'nonce': nonce,
                'value': value,
                'gasPrice': gas_price,
            }
            node_gas_price = self.web3.eth.gasPrice
            log.debug(
                'Calculated gas price for transaction',
                calculated_gas_price=gas_price,
                node_gas_price=node_gas_price,
            )

            # add the to address if not deploying a contract
            if to != b'':
                transaction['to'] = to_checksum_address(to)

            signed_txn = self.web3.eth.account.signTransaction(transaction, self.privkey)

            log_details = {
                'account': to_checksum_address(self.sender),
                'nonce': transaction['nonce'],
                'gasLimit': transaction['gas'],
                'gasPrice': transaction['gasPrice'],
            }
            log.debug('send_raw_transaction called', **log_details)

            tx_hash = self.web3.eth.sendRawTransaction(signed_txn.rawTransaction)
            self._available_nonce += 1

            log.debug('send_raw_transaction returned', tx_hash=encode_hex(tx_hash), **log_details)
            return tx_hash

    def poll(self, transaction_hash: bytes, confirmations: int = None):
        """ Wait until the `transaction_hash` is applied or rejected.

        Args:
            transaction_hash: Transaction hash that we are waiting for.
            confirmations: Number of block confirmations that we will
                wait for.
        """
        if len(transaction_hash) != 32:
            raise ValueError(
                'transaction_hash must be a 32 byte hash',
            )

        transaction_hash = encode_hex(transaction_hash)

        # used to check if the transaction was removed, this could happen
        # if gas price is too low:
        #
        # > Transaction (acbca3d6) below gas price (tx=1 Wei ask=18
        # > Shannon). All sequential txs from this address(7d0eae79)
        # > will be ignored
        #
        last_result = None

        while True:
            # Could return None for a short period of time, until the
            # transaction is added to the pool
            transaction = self.web3.eth.getTransaction(transaction_hash)

            # if the transaction was added to the pool and then removed
            if transaction is None and last_result is not None:
                raise Exception('invalid transaction, check gas price')

            # the transaction was added to the pool and mined
            if transaction and transaction['blockNumber'] is not None:
                break

            last_result = transaction
            gevent.sleep(.5)

        if confirmations:
            # this will wait for both APPLIED and REVERTED transactions
            transaction_block = transaction['blockNumber']
            confirmation_block = transaction_block + confirmations

            block_number = self.block_number()

            while block_number < confirmation_block:
                gevent.sleep(.5)
                block_number = self.block_number()

    def new_filter(
            self,
            contract_address: Address,
            topics: List[str] = None,
            from_block: BlockSpecification = 0,
            to_block: BlockSpecification = 'latest',
    ) -> StatelessFilter:
        """ Create a filter in the ethereum node. """
        return StatelessFilter(
            self.web3,
            {
                'fromBlock': from_block,
                'toBlock': to_block,
                'address': to_checksum_address(contract_address),
                'topics': topics,
            },
        )

    def get_filter_events(
            self,
            contract_address: Address,
            topics: List[str] = None,
            from_block: BlockSpecification = 0,
            to_block: BlockSpecification = 'latest',
    ) -> List[Dict]:
        """ Get events for the given query. """
        try:
            return self.web3.eth.getLogs({
                'fromBlock': from_block,
                'toBlock': to_block,
                'address': to_checksum_address(contract_address),
                'topics': topics,
            })
        except BlockNotFound:
            return []
<|endoftext|>


<commit_before>from rdflib import QueryResult

class SPARQLQueryResult(QueryResult.QueryResult):
    """
    Query result class for SPARQL

    xml   : as an XML string conforming to the SPARQL XML result format: http://www.w3.org/TR/rdf-sparql-XMLres/
    python: as Python objects
    json  : as JSON
    graph : as an RDFLib Graph - for CONSTRUCT and DESCRIBE queries
    """
    def __init__(self,pythonResult):
        """
        The constructor is the result straight from sparql-p, which is a list of tuples
        (in select order, each item is the valid binding for the corresponding variable or 'None') for SELECTs
        , a SPARQLGraph for DESCRIBE/CONSTRUCT, and boolean for ASK
        """
        self.rt = pythonResult

    def __iter__(self):
        """Iterates over the result entries"""
        if isinstance(self.rt,list):
            for item in self.rt:
                yield item
        else:
            yield self.rt

    def serialize(self,format='xml'):
        if format == 'python':
            return self.rt
        else:
            raise Exception("Result format not implemented: %s"%format) <commit_msg>SPARQL result normalized to a list of tuples (variable bindings)<commit_after>from rdflib import QueryResult

class SPARQLQueryResult(QueryResult.QueryResult):
    """
    Query result class for SPARQL

    xml   : as an XML string conforming to the SPARQL XML result format: http://www.w3.org/TR/rdf-sparql-XMLres/
    python: as Python objects
    json  : as JSON
    graph : as an RDFLib Graph - for CONSTRUCT and DESCRIBE queries
    """
    def __init__(self,pythonResult):
        """
        The constructor is the result straight from sparql-p, which is a list of tuples
        (in select order, each item is the valid binding for the corresponding variable or 'None') for SELECTs
        , a SPARQLGraph for DESCRIBE/CONSTRUCT, and boolean for ASK
        """
        self.rt = pythonResult

    def __iter__(self):
        """Iterates over the result entries"""
        if isinstance(self.rt,list):
            for item in self.rt:
                if not isinstance(item,(tuple,basestring)):
                    yield tuple(item)
                elif isinstance(item,basestring):
                    yield (item,)
                else:
                    yield item
        else:
            yield self.rt

    def serialize(self,format='xml'):
        if format == 'python':
            return self.rt
        else:
            raise Exception("Result format not implemented: %s"%format) <|endoftext|>