Example #1
0
    def schedule_message(self, data, time_slot):
        now_obj = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
        min_time_to_start = datetime.timedelta(hours=time_slot*4-4)
        max_time_to_start = datetime.timedelta(hours=time_slot*4)

        if time_slot == 0:
            intro = '**Current Rotation (Ends in {} hours and {} minutes):**\n'
        else:
            intro = '**In {} hours and {} minutes:**\n'

        for data_set in data['schedule']:
            start = pyrfc3339.parse(data_set['datetime_begin'])
            end = pyrfc3339.parse(data_set['datetime_end'])
            time_to_start = start - now_obj
            time_to_end = end - now_obj
            # print(min_time_to_start.total_seconds(), time_to_start.total_seconds(), max_time_to_start.total_seconds())
            if min_time_to_start <= time_to_start <= max_time_to_start:
                reg_1 = data_set['stages']['regular'][0]['name']
                reg_2 = data_set['stages']['regular'][1]['name']
                rank_1 = data_set['stages']['gachi'][0]['name']
                rank_2 = data_set['stages']['gachi'][1]['name']
                mode = data_set['gachi_rule']
                if time_slot == 0:
                    time_left = time_to_end
                else:
                    time_left = time_to_start
                hours = int(time_left.total_seconds() / 3600)
                minutes = int(time_left.total_seconds()/60) % 60
                msg = intro + "Turf War is {} and {}\n{} is {} and {}"
                return msg.format(hours, minutes, reg_1, reg_2, mode, rank_1, rank_2)

        return "There is no data currently for this time slot."
Example #2
0
def current_calendar_events(calId, time_window=1):

    http = httplib2.Http()
    service = build(serviceName='calendar', version='v3', http=http,
                    developerKey='AIzaSyA96dI1CPIUEuzgi3-_H8dQVyM34rak5vE')

    # get a list of all events +/- the specified number of days from now
    now = datetime.utcnow().replace(tzinfo=pytz.utc)
    diffTime = timedelta(days=time_window)
    queryStart = now - diffTime
    queryEnd = now + diffTime

    dayStartString = pyrfc3339.generate(queryStart)
    dayEndString = pyrfc3339.generate(queryEnd)

    events = service.events().list(calendarId=calId, singleEvents=True, timeMin=dayStartString, timeMax=dayEndString, orderBy='updated').execute()

    eventList = []
    for event in events['items']:
        endTime = pyrfc3339.parse(event['end']['dateTime'])
        startTime = pyrfc3339.parse(event['start']['dateTime'])
        
        if now > startTime and now < endTime:
            eventList.append(event)
        
    return eventList
def _daterange_filter(query, params, state):
    """
    handles filtering by start and end date
    paramters: startdate, enddate
    """
    startdate = params.get('startdate')
    if startdate is not None:
        try:
            del params['startdate']
            try:
                startdate = parse_date(startdate, '%Y-%m-%d')
            except ValueError:
                startdate = pyrfc3339.parse(startdate)
            query = query.filter(pub_date__gte=startdate)
        except ValueError:
            raise QueryError('Invalid start date "%s", must be YYYY-MM-DD or rfc3339' % startdate)
    
    enddate = params.get('enddate')
    if enddate is not None:
        try:
            del params['enddate']
            try:
                enddate = parse_date(enddate, '%Y-%m-%d')
            except ValueError: 
                enddate = pyrfc3339.parse(enddate)
            query = query.filter(pub_date__lte=enddate)
        except ValueError:
            raise QueryError('Invalid end date "%s", must be YYYY-MM-DD or rfc3339' % enddate)

    return query, params, state
Example #4
0
    def validate_expiration_time(self, original_value, value_in_question, type_=None):
        """
        Validate the expiration time value passed to Update or Create Methods.

        Args:
            original_value: The original value that needs to be compared (e.g., SLICE creation date)
            value_in_question: The value that is doubted for correctness (e.g., Expiry time update date)

        Returns:
            a boolean value to indicate whether the expiration time valid or not
        """
        parsed_original_value = pyrfc3339.parse(original_value)
        parsed_value_in_question = pyrfc3339.parse(value_in_question)
        now = pytz.timezone("UTC").localize(datetime.datetime.utcnow())

        # Check if the object has already expired
        if now > parsed_original_value:
            raise GFedv2ArgumentError("Update is not possible because the object has already expired: "+str(now)+" > "+str(parsed_original_value))

        if type_:
            maximum_expansion_duration = self.STATIC['CONFIG'][type_]['max_%s_extension_time' %type_.lower()]
            configuration_delta = datetime.timedelta(days=maximum_expansion_duration)
            delta_time_days =  parsed_value_in_question - parsed_original_value
            return True if parsed_original_value <= parsed_value_in_question and delta_time_days < configuration_delta else False
        else:
            return parsed_original_value <= parsed_value_in_question
    def execute(self):

        if self.__mode == 'eventview':
            if self.__updateSec > 60:
              now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
              try:
                eventsResult = self.__service.events().list(
                calendarId=self.__calendarID, timeMin=now, maxResults=5, singleEvents=True,
                orderBy='startTime').execute()
              except HttpError, err:
                if err.resp.status in [403, 500, 503]:
                  time.sleep(5)
                else: 
                  if err.resp.get('content-type', '').startswith('application/json'):
                    reason = json.loads(err.content).reason
                    print reason
                  raise
              
              events = eventsResult.get('items', [])
              self.__lg19.load_text("Calendar", 1,True, center=True, color="yellow")
              
              i = 3;
              if not events:
                  self.__lg19.load_text("No upcoming events found.", 2)
              for event in events:
                  if event['start'].get('dateTime') == None:
                      start = datetime.datetime.strptime(event['start'].get('date'),"%Y-%m-%d")
                      end = datetime.datetime.strptime(event['start'].get('date'),"%Y-%m-%d")
                      now = datetime.datetime.today()
                      prefix = " "
                      if start <= now <= end:
                          prefix = "-"
                      self.__lg19.load_text(prefix + start.strftime("%d/%m") + " " +event['summary'], i)
                  else:
                      start = parse(event['start'].get('dateTime'), utc=True)
                      end = parse(event['end'].get('dateTime'), utc=True)
                      now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
                      inhour = now + datetime.timedelta(hours=1)
                      late = now + datetime.timedelta(hours=6)
                      color = "white"
                      if start >= late:
                          color = "DarkGray"
                      if start <= inhour <= end:
                          color = "gold"
                      if start <= now <= end:
                          color = "red"
                      start = start.astimezone(get_localzone())
                      self.__lg19.load_text(start.strftime("%d/%m %H:%M") + " " +event['summary'], i, color=color)
                  i += 1
              
              self.__lg19.set_text()
              self.__updateSec = 0
            self.__updateSec += 1
            time.sleep(1)
Example #6
0
File: tests.py Project: Roger/y
    def test_zero_offset(self):
        '''
        Both +00:00 and -00:00 are equivalent to the offset 'Z' (UTC).

        '''
        timestamp = '2009-01-01T10:02:03+00:00'
        dt = parse(timestamp)
        eq_(dt.tzinfo, pytz.utc)

        timestamp = '2009-01-01T10:02:03-00:00'
        dt = parse(timestamp)
        eq_(dt.tzinfo, pytz.utc)
Example #7
0
def cancelEvent(evt_id, user_list, mail_list):
  service = initService()
  event = service.events().get(calendarId=calId, eventId=evt_id).execute()

  if not event.has_key('description') or event['description'].strip() == '':
    return 'CANCEL_NULL'

  user_str = event['description']

  for x in user_list:
    if user_str.find(x) != -1:
      event['description'] = ''
      update_event = service.events().update(calendarId=calId, eventId=event['id'], body=event).execute()
      
      creator_email = event['creator']['email']
      creator = event['summary']
      
      mail_list.append(creator_email)

      dtstart = parse(event['start']['dateTime']).strftime('%Y-%m-%d %H:%M:%S')
      dtend = parse(event['end']['dateTime']).strftime('%Y-%m-%d %H:%M:%S')

      for x in mail_list:
        if x.strip() != '':
          mail.send_mail(sender=creator_email, 
                            to=x,
                            subject="Cancelation of Private Tutoring Reservation",
                            body="""
          Hi,

          Instructor: %(instructor)s
          Start Time: %(dtstart)s
          End Time: %(dtend)s
          Participants: %(partici)s

          This mail confirms you that your request of Private Tutoring
          has been canceled.

          Best,

          %(creator)s
          """ % {'instructor':creator, 
                'dtstart':dtstart,
                'dtend':dtend,
                'partici':user_str,
                'creator':creator})

      return 'CANCEL_OK'
  
  return 'CANCEL_INVALID'
Example #8
0
    def check_datetime(value):
        """
        Check if value is a valid RFC3339 string.

        See RFC3339 for more details: http://www.ietf.org/rfc/rfc3339.txt

        Args:
            value: item to check

        Raises:
            Exception: value is not of valid RFC3339 string

        """
        pyrfc3339.parse(value)
Example #9
0
def p_expression_tagged_element(p):
    """expression : TAG expression"""
    tag = p[1]
    element = p[2]

    if tag == 'inst':
        length = len(element)
        hyphens_count = element.count('-')

        if length == 10 and hyphens_count == 2:
            output = datetime.datetime.strptime(element, '%Y-%m-%d').date()
        elif length == 7 and hyphens_count == 1:
            output = datetime.datetime.strptime(element, '%Y-%m').date()
        elif length == 4 and hyphens_count == 0:
            output = datetime.datetime.strptime(element, '%Y').date()
        else:
            output = pyrfc3339.parse(element)
    elif tag == 'uuid':
        output = uuid.UUID(element)
    elif tag in _serializers:
        output = _serializers[tag](element)
    else:
        raise NotImplementedError(
            u"Don't know how to handle tag ImmutableDict({})".format(tag))

    p[0] = output
Example #10
0
def _parse_time(input):
    """
    :param input: Either a number as milliseconds since Unix Epoch, or a string as a valid RFC3339 timestamp
    :return: milliseconds since Unix epoch, or None if input was invalid.
    """

    # bool is a subtype of int, and we don't want to try and compare it as a time.
    if isinstance(input, bool):
        log.warn("Got unexpected bool type when attempting to parse time")
        return None

    if isinstance(input, Number):
        return float(input)

    if isinstance(input, six.string_types):
        try:
            parsed_time = pyrfc3339.parse(input)
            timestamp = (parsed_time - epoch).total_seconds()
            return timestamp * 1000.0
        except Exception as e:
            log.warn("Couldn't parse timestamp:" + str(input) + " with message: " + str(e))
            return None

    log.warn("Got unexpected type: " + type(input) + " with value: " + str(input) + " when attempting to parse time")
    return None
Example #11
0
def _notAfterBefore(cert_path, method):
    """Internal helper function for finding notbefore/notafter.

    :param str cert_path: path to a cert in PEM format
    :param function method: one of ``OpenSSL.crypto.X509.get_notBefore``
        or ``OpenSSL.crypto.X509.get_notAfter``

    :returns: the notBefore or notAfter value from the cert at cert_path
    :rtype: :class:`datetime.datetime`

    """
    with open(cert_path) as f:
        x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, f.read())
    timestamp = method(x509)
    reformatted_timestamp = [
        timestamp[0:4],
        "-",
        timestamp[4:6],
        "-",
        timestamp[6:8],
        "T",
        timestamp[8:10],
        ":",
        timestamp[10:12],
        ":",
        timestamp[12:],
    ]
    return pyrfc3339.parse("".join(reformatted_timestamp))
    def fetchPredictions(self, stopTag):
        r = requests.get(
            self.baseURL
            + "/rtt/public/utility/file.aspx?contenttype=SQLXML&Name=RoutePositionET.xml&PlatformTag="
            + stopTag
        )
        r.raise_for_status()

        predictionsTree = etree.fromstring(r.content)

        predictionData = {
            "agency": self.agency,
            "tag": stopTag,
            "expires": parse(
                predictionsTree.xpath("/c:RoutePositionET/c:Content/@Expires", namespaces=NSMAP)[0], utc=True
            ),
            "predictions": [],
        }

        for trip in predictionsTree.xpath(
            "/c:RoutePositionET/c:Platform/c:Route/c:Destination/c:Trip", namespaces=NSMAP
        ):
            predictionData["predictions"].append(
                {
                    "minutes": int(trip.attrib["ETA"]),
                    "destination": trip.xpath("../@Name")[0],
                    "route": trip.xpath("../../@RouteNo")[0],
                }
            )

        predictionData["predictions"].sort(key=lambda x: x["minutes"])

        return predictionData
def go_to_py_cookie(go_cookie):
    '''Convert a Go-style JSON-unmarshaled cookie into a Python cookie'''
    expires = None
    if go_cookie.get('Expires') is not None:
        t = pyrfc3339.parse(go_cookie['Expires'])
        expires = t.timestamp()
    return cookiejar.Cookie(
        version=0,
        name=go_cookie['Name'],
        value=go_cookie['Value'],
        port=None,
        port_specified=False,
        # Unfortunately Python cookies don't record the original
        # host that the cookie came from, so we'll just use Domain
        # for that purpose, and record that the domain was specified,
        # even though it probably was not. This means that
        # we won't correctly record the CanonicalHost entry
        # when writing the cookie file after reading it.
        domain=go_cookie['Domain'],
        domain_specified=not go_cookie['HostOnly'],
        domain_initial_dot=False,
        path=go_cookie['Path'],
        path_specified=True,
        secure=go_cookie['Secure'],
        expires=expires,
        discard=False,
        comment=None,
        comment_url=None,
        rest=None,
        rfc2109=False,
    )
Example #14
0
def _notAfterBefore(cert_path, method):
    """Internal helper function for finding notbefore/notafter.

    :param str cert_path: path to a cert in PEM format
    :param function method: one of ``OpenSSL.crypto.X509.get_notBefore``
        or ``OpenSSL.crypto.X509.get_notAfter``

    :returns: the notBefore or notAfter value from the cert at cert_path
    :rtype: :class:`datetime.datetime`

    """
    # pylint: disable=redefined-outer-name
    with open(cert_path) as f:
        x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
                                               f.read())
    # pyopenssl always returns bytes
    timestamp = method(x509)
    reformatted_timestamp = [timestamp[0:4], b"-", timestamp[4:6], b"-",
                             timestamp[6:8], b"T", timestamp[8:10], b":",
                             timestamp[10:12], b":", timestamp[12:]]
    timestamp_str = b"".join(reformatted_timestamp)
    # pyrfc3339 uses "native" strings. That is, bytes on Python 2 and unicode
    # on Python 3
    if six.PY3:
        timestamp_str = timestamp_str.decode('ascii')
    return pyrfc3339.parse(timestamp_str)
Example #15
0
def expiry_time(ns, cavs):
    ''' Returns the minimum time of any time-before caveats found
    in the given list or None if no such caveats were found.

    The ns parameter is
    :param ns: used to determine the standard namespace prefix - if
    the standard namespace is not found, the empty prefix is assumed.
    :param cavs: a list of pymacaroons.Caveat
    :return: datetime.DateTime or None.
    '''
    prefix = ns.resolve(STD_NAMESPACE)
    time_before_cond = condition_with_prefix(
        prefix, COND_TIME_BEFORE)
    t = None
    for cav in cavs:
        if not cav.first_party():
            continue
        cav = cav.caveat_id_bytes.decode('utf-8')
        name, rest = parse_caveat(cav)
        if name != time_before_cond:
            continue
        try:
            et = pyrfc3339.parse(rest)
            if t is None or et < t:
                t = et
        except ValueError:
            continue
    return t
Example #16
0
File: tests.py Project: Roger/y
    def test_parse_naive_utc(self):
        '''
        Test parsing a UTC timestamp to a naive datetime.

        '''
        dt1 = parse('2009-01-01T10:01:02Z', produce_naive=True)
        eq_(dt1.tzinfo, None)
Example #17
0
def updateEvent(evt_id, user_str, mail_list):
  service = initService()
  event = service.events().get(calendarId=calId, eventId=evt_id).execute()

  #return event['id']
  creator_email = event['creator']['email']
  creator = event['summary']

  if event.has_key('description') and event['description'].strip() != '':
    return 'RESERVED'

  event['description'] = user_str

  update_event = service.events().update(calendarId=calId, eventId=event['id'], body=event).execute()

  mail_list.append(creator_email)

  dtstart = parse(event['start']['dateTime']).strftime('%Y-%m-%d %H:%M:%S')
  dtend = parse(event['end']['dateTime']).strftime('%Y-%m-%d %H:%M:%S')

  for x in mail_list:
    if x.strip() != '':
      mail.send_mail(sender=creator_email, 
                        to=x,
                        subject="Confirmation of Private Tutoring Reservation",
                        body="""
      Hi,

      Instructor: %(instructor)s
      Start Time: %(dtstart)s
      End Time: %(dtend)s
      Participants: %(partici)s

      This mail confirms you that your request of Private Tutoring
      has been received by GSI. Once location is determined, GSI 
      will contact you with details.

      Best,

      %(creator)s
      """ % {'instructor':creator, 
            'dtstart':dtstart,
            'dtend':dtend,
            'partici':user_str,
            'creator':creator})

  return 'RESERVE_OK'
Example #18
0
File: tests.py Project: Roger/y
    def test_parse_microseconds(self):
        '''
        Test parsing timestamps with microseconds.

        '''
        timestamp = '2009-01-01T10:02:03.25Z'
        dt = parse(timestamp)
        eq_(dt.microsecond, 250000)
Example #19
0
    def test_deepcopy(self):
        '''
        Tests that deepcopy works and doesn't crash

        '''
        timestamp = '2009-01-01T10:02:03+02:00'
        dt = parse(timestamp)
        deepcopy(dt)
Example #20
0
def get_execution_time(text):
    soup = BeautifulSoup(text)
    execution_time = soup.find("executiontime")
    if not execution_time:
        error = handle_error(text)
        raise ValueError(error)
    else:
        return parse(execution_time.text)
Example #21
0
File: tests.py Project: Roger/y
    def test_generate_local_parse_local(self):
        '''
        Generate a local timestamp and parse it into a local datetime.

        '''
        eastern = pytz.timezone('US/Eastern')
        dt1 = eastern.localize(datetime.utcnow())
        dt2 = parse(generate(dt1, utc=False, microseconds=True), utc=False)
        eq_(dt1, dt2)
Example #22
0
File: tests.py Project: Roger/y
    def test_generate_utc_parse_utc(self):
        '''
        Generate a UTC timestamp and parse it into a UTC datetime.

        '''
        dt1 = datetime.utcnow()
        dt1 = dt1.replace(tzinfo=pytz.utc)

        dt2 = parse(generate(dt1, microseconds=True))
        eq_(dt1, dt2)
Example #23
0
File: tests.py Project: Roger/y
    def test_mixed_case(self):
        '''
        Timestamps may use either 'T' or 't' and either 'Z' or 'z'
        according to :RFC:`3339`.

        '''
        dt1 = parse('2009-01-01t10:01:02z')
        dt2 = datetime(2009, 1, 1, 10, 1, 2, tzinfo=pytz.utc)

        eq_(dt1, dt2)
Example #24
0
File: tests.py Project: Roger/y
    def utc_roundtrip(self, tz_name):
        '''
        Generates a local datetime using the given timezone,
        produces a local timestamp from the datetime, parses the timestamp
        to a UTC datetime, and verifies that the two datetimes are equal.

        '''
        tzinfo = pytz.timezone(tz_name)
        dt1 = tzinfo.localize(datetime.utcnow())
        timestamp = generate(dt1, utc=False, microseconds=True)
        dt2 = parse(timestamp)
        eq_(dt1, dt2)
Example #25
0
 def _notafterbefore(self, method, version):
     """Internal helper function for finding notbefore/notafter."""
     if version is None:
         target = self.current_target("cert")
     else:
         target = self.version("cert", version)
     pem = open(target).read()
     x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
                                            pem)
     i = method(x509)
     return pyrfc3339.parse(i[0:4] + "-" + i[4:6] + "-" + i[6:8] + "T" +
                            i[8:10] + ":" + i[10:12] + ":" + i[12:])
def _check_time_before(ctx, cond, arg):
    clock = ctx.get(TIME_KEY)
    if clock is None:
        now = pytz.UTC.localize(datetime.utcnow())
    else:
        now = clock.utcnow()

    try:
        if pyrfc3339.parse(arg) <= now:
            return 'macaroon has expired'
    except ValueError:
        return 'cannot parse "{}" as RFC 3339'.format(arg)
    return None
Example #27
0
    def validate_expiration_time(self, original_value, value_in_question, type_=None):
        """
        Validate the expiration time value passed to Update or Create Methods.

        Args:
            original_value: The original value that needs to be compared (e.g., SLICE creation date)
            value_in_question: The value that is doubted for correctness (e.g., Expiry time update date)

        Returns:
            a boolean value to indicate whether the expiration time valid or not
        """
        parsed_original_value = pyrfc3339.parse(original_value)
        parsed_value_in_question = pyrfc3339.parse(value_in_question)

        if type_:
            maximum_expansion_duration = self.STATIC['CONFIG'][type_]['max_%s_extension_time' %type_.lower()]
            configuration_delta = datetime.timedelta(days=maximum_expansion_duration)
            delta_time_days =  parsed_value_in_question - parsed_original_value

            return True if parsed_original_value < parsed_value_in_question and delta_time_days < configuration_delta  else False
        else:
            return parsed_original_value < parsed_value_in_question
Example #28
0
 def splatfest_message(self, data):
     now_obj = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
     msg = '**Splatfest Time! (Ends in {} hours and {} minutes)**\n' \
           'Teams are **{}** and **{}**\n' \
           'Maps are {}, {}, and {}'
     end = pyrfc3339.parse(data['schedule'][0]['datetime_end'])
     time_to_end = end - now_obj
     hours = int(time_to_end.total_seconds() / 3600)
     minutes = int(time_to_end.total_seconds()/60) % 60
     team_a = data['schedule'][0]['team_alpha_name']
     team_b = data['schedule'][0]['team_bravo_name']
     stage_1 = data['schedule'][0]['stages'][0]['name']
     stage_2 = data['schedule'][0]['stages'][1]['name']
     stage_3 = data['schedule'][0]['stages'][2]['name']
     return msg.format(hours, minutes, team_a, team_b, stage_1, stage_2, stage_3)
Example #29
0
File: server.py Project: di-y/opti
def cred_from_cred_id(cred_id):
  cred_from_db = r.table('credentials').get(cred_id).run(flask.g.db_conn)
  credentials = client.OAuth2Credentials(
    cred_from_db['access_token'],
    cred_from_db['client_id'],
    cred_from_db['client_secret'],
    cred_from_db['refresh_token'],
    pyrfc3339.parse(cred_from_db['token_expiry']).replace(tzinfo=None),
    cred_from_db['token_uri'],
    cred_from_db['user_agent'],
    revoke_uri=cred_from_db['revoke_uri'],
    id_token=cred_from_db['id_token'],
    token_response=cred_from_db['token_response']
  )
  return credentials
Example #30
0
File: edn_parse.py Project: Roger/y
def p_expression_tagged_element(p):
    """expression : TAG expression"""
    tag = p[1]
    element = p[2]

    if tag == 'inst':
        output = pyrfc3339.parse(element)
    elif tag == 'uuid':
        output = uuid.UUID(element)
    elif tag in _serializers:
        output = _serializers[tag](element)
    else:
        raise NotImplementedError("Don't know how to handle tag {}".format(tag))

    p[0] = output