コード例 #1
0
ファイル: calendar_events.py プロジェクト: techsol/hackPump
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
コード例 #2
0
ファイル: calendar_events.py プロジェクト: Br3nda/hackPump
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
コード例 #3
0
def get_period_time(interval):
    period = dict()
    start_time = datetime.datetime.utcnow() - datetime.timedelta(
        minutes=interval)
    start_time = generate(start_time.replace(tzinfo=pytz.utc))
    end_time = generate(datetime.datetime.utcnow().replace(tzinfo=pytz.utc))
    period['start'] = start_time
    period['end'] = end_time
    return period
コード例 #4
0
ファイル: georeportv2.py プロジェクト: slinkp/openblock
 def service_requests_url(self, start_date, end_date):
     params = dict(self.standard_params)
     params['start_date'] = pyrfc3339.generate(start_date,
                                               utc=True,
                                               accept_naive=True)
     params['end_date'] = pyrfc3339.generate(end_date,
                                             utc=True,
                                             accept_naive=True)
     return self.api_url + 'requests.xml?' + urllib.urlencode(params)
コード例 #5
0
def test_handler_time_format_timezone(s3_bucket, single_calendar_mock,
                                      with_frozen_time):
    start_time_us = datetime(2019, 6, 15, 4, 0)  # GMT - 4
    us_time_zone = pytz_timezone('US/Eastern')
    oslo_time_zone = pytz_timezone('Europe/Oslo')
    start_time_us = us_time_zone.localize(start_time_us)
    start_time_us_rcf3339 = pyrfc3339.generate(start_time_us, utc=False)

    start_time_oslo = datetime(2019, 6, 15, 10, 0)

    end_time_oslo = datetime(2019, 6, 15, 13, 30)  # GM + 2
    end_time_oslo = oslo_time_zone.localize(end_time_oslo)
    end_time_1_rfc3339 = pyrfc3339.generate(end_time_oslo, utc=False)

    single_calendar_mock(
        {
            'items': [{
                'id': '1234',
                'start': {
                    'dateTime': start_time_us_rcf3339
                },
                'end': {
                    'dateTime': end_time_1_rfc3339
                },
                'location': 'Enheter-box1'
            }]
        }, {})

    handler(None, None)

    expected = [{
        'event_id': '1234',
        'calendar_id': '1',
        'timestamp_from': int(start_time_oslo.timestamp()),
        'timestamp_to': int(end_time_oslo.timestamp()),
        'event_summary': '',
        'event_button_names': ['box1'],
        'creator': ''
    }, {
        'event_id': '1234',
        'calendar_id': '2',
        'timestamp_from': int(start_time_oslo.timestamp()),
        'timestamp_to': int(end_time_oslo.timestamp()),
        'event_summary': '',
        'event_button_names': ['box1'],
        'creator': ''
    }]

    response = s3_bucket.Object(next(iter(s3_bucket.objects.all())).key).get()
    data = loads(response['Body'].read())

    assert [i for i in data['data'] if i not in expected] == []
コード例 #6
0
    def run(cls, info):
        import pyrfc3339
        from datetime import datetime
        import pytz
        labels = {}
        labels['name'] = info.manifest.name.format(**info.manifest_vars)
        # Inspired by https://github.com/projectatomic/ContainerApplicationGenericLabels
        # See here for the discussion on the debian-cloud mailing list
        # https://lists.debian.org/debian-cloud/2015/05/msg00071.html
        labels['architecture'] = info.manifest.system['architecture']
        labels['build-date'] = pyrfc3339.generate(
            datetime.utcnow().replace(tzinfo=pytz.utc))
        if 'labels' in info.manifest.provider:
            for label, value in info.manifest.provider['labels'].items():
                labels[label] = value.format(**info.manifest_vars)

        # pipes.quote converts newlines into \n rather than just prefixing
        # it with a backslash, so we need to escape manually
        def escape(value):
            value = value.replace('"', '\\"')
            value = value.replace('\n', '\\\n')
            value = '"' + value + '"'
            return value

        kv_pairs = [
            label + '=' + escape(value) for label, value in labels.items()
        ]
        # Add some nice newlines and indentation
        info._docker['dockerfile'] += 'LABEL ' + ' \\\n      '.join(
            kv_pairs) + '\n'
コード例 #7
0
ファイル: edn_dump.py プロジェクト: konr/edn_format
def udump(obj,
          string_encoding=DEFAULT_INPUT_ENCODING,
          keyword_keys=False,
          sort_keys=False,
          sort_sets=False):

    kwargs = {
        "string_encoding": string_encoding,
        "keyword_keys": keyword_keys,
        "sort_keys": sort_keys,
        "sort_sets": sort_sets,
    }

    if obj is None:
        return 'nil'
    elif isinstance(obj, bool):
        return 'true' if obj else 'false'
    elif isinstance(obj, (int, long, float)):
        return unicode(obj)
    elif isinstance(obj, decimal.Decimal):
        return '{}M'.format(obj)
    elif isinstance(obj, (Keyword, Symbol)):
        return unicode(obj)
    # CAVEAT LECTOR! In Python 3 'basestring' is alised to 'str' above.
    # Furthermore, in Python 2 bytes is an instance of 'str'/'basestring' while
    # in Python 3 it is not.
    elif isinstance(obj, bytes):
        return unicode_escape(obj.decode(string_encoding))
    elif isinstance(obj, basestring):
        return unicode_escape(obj)
    elif isinstance(obj, tuple):
        return '({})'.format(seq(obj, **kwargs))
    elif isinstance(obj, (list, ImmutableList)):
        return '[{}]'.format(seq(obj, **kwargs))
    elif isinstance(obj, set) or isinstance(obj, frozenset):
        if sort_sets:
            obj = sorted(obj)
        return '#{{{}}}'.format(seq(obj, **kwargs))
    elif isinstance(obj, dict) or isinstance(obj, ImmutableDict):
        pairs = obj.items()
        if sort_keys:
            pairs = sorted(pairs, key=lambda p: str(p[0]))
        if keyword_keys:
            pairs = ((Keyword(k) if isinstance(k,
                                               (bytes, basestring)) else k, v)
                     for k, v in pairs)

        return '{{{}}}'.format(
            seq(itertools.chain.from_iterable(pairs), **kwargs))
    elif isinstance(obj, datetime.datetime):
        return '#inst "{}"'.format(pyrfc3339.generate(obj, microseconds=True))
    elif isinstance(obj, datetime.date):
        return '#inst "{}"'.format(obj.isoformat())
    elif isinstance(obj, uuid.UUID):
        return '#uuid "{}"'.format(obj)
    elif isinstance(obj, TaggedElement):
        return unicode(obj)
    raise NotImplementedError(
        u"encountered object of type '{}' for which no known encoding is available: {}"
        .format(type(obj), repr(obj)))
コード例 #8
0
def autoset_timestamp_end(params: dict):
    scale = params.get("scale")
    if scale is None:
        logging.error("scale is not set!!")
        return

    expected_test_duration = params.get("expected-test-duration")
    if expected_test_duration is None:
        logging.info("auto set timestamp_end is disabled")
        return

    qps = 300 * 1000
    if params.get("limiter-max-qps") is not None and params.get(
            "use-qps-limiter"
    ) is not None and params.get("use-qps-limiter") is True:
        qps = params.get("limiter-max-qps")
    usecase = params.get("use-case") if params.get(
        "use-case") in use_case_metrics_count.keys() else "devops"
    log_interval = params.get("log_interval") if params.get(
        "log_interval") is not None else 10
    timestamp_start = params.get("timestamp-start") if params.get(
        "timestamp-start") is not None else "2020-01-01T00:00:00Z"

    timestamp_start = parse(timestamp_start)
    delta_time = log_interval * expected_test_duration * qps / (
        use_case_metrics_count[usecase] * scale) + 1

    params["timestamp-end"] = generate(timestamp_start +
                                       timedelta(seconds=delta_time))
    logging.info(params)
コード例 #9
0
ファイル: report.py プロジェクト: kurtraschke/cadors-parse
def display_report(report, format):
    report = CadorsReport.query.get_or_404(report)

    if format == 'atom':
        reports = process_report_atom([report])
        feed_timestamp = generate(report.last_updated, accept_naive=True)
        response = make_response(
            render_template('feed.xml',
                            reports=reports,
                            feed_timestamp=feed_timestamp))
        response.mimetype = "application/atom+xml"
    elif format == 'json':
        response = make_response(json.dumps(
                {'report': report},
                indent=None if request.is_xhr else 2,
                default=json_default))
        response.mimetype = "application/json"
    elif format == 'html':
        response = make_response(
            render_template('single_report.html',
                            report=report))
    elif format == 'kml':
        response = make_response(render_template('kml.xml', report=report))
        response.mimetype = "application/vnd.google-earth.kml+xml"
        
    response.last_modified = report.last_updated
    return prepare_response(response, 43200)
コード例 #10
0
ファイル: packager.py プロジェクト: ammarun11/tng-sdk-package
    def _pack_create_napdr(self, pp, pd):
        """
        Creates a NAPDR for the new package based on the given
        project path (pp) and project descriptor (pd).

        NAPDR is annotated with info. like '_project_source' to
        temp. link between project files and target package.
        """
        # create initial NAPDR with package contents of project (name etc.)
        napdr = NapdRecord(**pd.get("package"))
        # add release date and time
        napdr.release_date_time = pyrfc3339.generate(datetime.datetime.now(),
                                                     accept_naive=True)
        # add package content
        for f in pd.get("files"):
            r = {
                "source": self._pack_package_source_path(f),
                "algorithm": "SHA-256",
                "hash": file_hash(os.path.join(pp, f.get("path"))),
                "content-type": f.get("type", "text/plain"),
                "tags": f.get("tags", list()),
                "_project_source": f.get("path")
            }
            napdr.package_content.append(r)
        return napdr
コード例 #11
0
def py_to_go_cookie(py_cookie):
    '''Convert a python cookie to the JSON-marshalable Go-style cookie form.'''
    # TODO (perhaps):
    #   HttpOnly
    #   Creation
    #   LastAccess
    #   Updated
    # not done properly: CanonicalHost.
    go_cookie = {
        'Name': py_cookie.name,
        'Value': py_cookie.value,
        'Domain': py_cookie.domain,
        'HostOnly': not py_cookie.domain_specified,
        'Persistent': not py_cookie.discard,
        'Secure': py_cookie.secure,
        'CanonicalHost': py_cookie.domain,
    }
    if py_cookie.path_specified:
        go_cookie['Path'] = py_cookie.path
    if py_cookie.expires is not None:
        unix_time = datetime.datetime.fromtimestamp(py_cookie.expires)
        # Note: fromtimestamp bizarrely produces a time without
        # a time zone, so we need to use accept_naive.
        go_cookie['Expires'] = pyrfc3339.generate(unix_time, accept_naive=True)
    return go_cookie
コード例 #12
0
ファイル: influx_telemetry.py プロジェクト: noradtux/evnotipi
    def data_callback(self, data):
        """ Callback to receive data from "car" """
        self._log.debug("Enqeue...")
        p = {
            "measurement": "telemetry",
            "tags": {
                "cartype": self._cartype,
                "akey": self._evn_akey,
            }
        }
        fields = {
            k: v if k in INT_FIELD_LIST else float(v)
            for k, v in data.items() if v is not None
        }

        if 'gps_device' in data:
            p['tags']['gps_device'] = data['gps_device']

        p["time"] = pyrfc3339.generate(
            datetime.fromtimestamp(data['timestamp'], timezone.utc))
        p["fields"] = fields

        try:
            self._iwrite.write(bucket=self._config['bucket'],
                               org=self._config['org'],
                               record=[p])
        except Exception as e:
            self._log.warning(str(e))
コード例 #13
0
ファイル: packager.py プロジェクト: ammarun11/tng-sdk-package
 def _update_nr_with_tosca(self, tosca_meta, nr=None):
     """
     Creates a NapdRecord and fills it with TOSCA
     meta data. Mapping/translation is done manually since many
     required fields are not in TOSCA.
     """
     if nr is None:
         nr = NapdRecord()
     # TOSCA Created-By becomes vendor
     nr.vendor = save_name(tosca_meta[0].get("Created-By"))
     # TOSCA has no name field: Use UUID of process instead
     nr.name = str(uuid.uuid4())[:8]
     # TOSCA has no package version. Use 1.0
     nr.version = "1.0"
     # Package type = application/vnd.tosca.package
     nr.package_type = "application/vnd.tosca.package"
     # Maintainer = Created By
     nr.maintainer = tosca_meta[0].get("Created-By")
     # TOSCA has no create date/time: Use current time
     nr.release_date_time = pyrfc3339.generate(datetime.datetime.now(),
                                               accept_naive=True)
     # add raw TOSCA metadata
     nr.metadata["tosca"] = tosca_meta
     # LOG.debug("Added TOSCA meta data to {}".format(nr))
     return nr
コード例 #14
0
ファイル: edn_dump.py プロジェクト: Roger/y
def dump(obj):
    def seq(obj):
        return " ".join([dump(i) for i in obj])

    if isinstance(obj, TaggedElement):
        return str(obj)
    elif obj is None:
        return "nil"
    elif isinstance(obj, bool):
        if obj:
            return "true"
        else:
            return "false"
    elif isinstance(obj, (int, long, float)):
        return str(obj)
    elif isinstance(obj, decimal.Decimal):
        return "{}M".format(obj)
    elif isinstance(obj, (Keyword, Symbol)):
        return str(obj)
    elif isinstance(obj, basestring):
        return '"{}"'.format(obj)
    elif isinstance(obj, tuple):
        return "({})".format(seq(obj))
    elif isinstance(obj, list):
        return "[{}]".format(seq(obj))
    elif isinstance(obj, set):
        return "#{{{}}}".format(seq(obj))
    elif isinstance(obj, dict):
        return "{{{}}}".format(seq(itertools.chain.from_iterable(obj.items())))
    elif isinstance(obj, datetime.datetime):
        return '#inst "{}"'.format(pyrfc3339.generate(obj))
    elif isinstance(obj, uuid.UUID):
        return '#uuid "{}"'.format(obj)
    else:
        raise NotImplementedError("Don't know how to handle {} : {}", type(obj), obj)
コード例 #15
0
ファイル: image.py プロジェクト: KellerFuchs/bootstrap-vz
	def run(cls, info):
		import pyrfc3339
		from datetime import datetime
		import pytz
		labels = {}
		labels['name'] = info.manifest.name.format(**info.manifest_vars)
		# Inspired by https://github.com/projectatomic/ContainerApplicationGenericLabels
		# See here for the discussion on the debian-cloud mailing list
		# https://lists.debian.org/debian-cloud/2015/05/msg00071.html
		labels['architecture'] = info.manifest.system['architecture']
		labels['build-date'] = pyrfc3339.generate(datetime.utcnow().replace(tzinfo=pytz.utc))
		if 'labels' in info.manifest.provider:
			for label, value in info.manifest.provider['labels'].items():
				labels[label] = value.format(**info.manifest_vars)

		# pipes.quote converts newlines into \n rather than just prefixing
		# it with a backslash, so we need to escape manually
		def escape(value):
			value = value.replace('"', '\\"')
			value = value.replace('\n', '\\\n')
			value = '"' + value + '"'
			return value
		kv_pairs = [label + '=' + escape(value) for label, value in labels.items()]
		# Add some nice newlines and indentation
		info._docker['dockerfile'] += 'LABEL ' + ' \\\n      '.join(kv_pairs) + '\n'
コード例 #16
0
    def create_project(self, client_cert, credentials, fields, options):
        """
        Create a project object.

        Generate fields for a new object:
            * PROJECT_URN: retrieve the hostname from the Flask AMsoil plugin
                and form into a valid URN
            * PROJECT_UID: generate a new UUID4 value
            * PROJECT_CREATION: get the time now and convert it into RFC3339 form
            * PROJECT_EXPIRED: project object has just been created, so it is
                has not yet expired

        """
        config = pm.getService('config')
        hostname = config.get('flask.hostname')

        fields[
            'PROJECT_URN'] = 'urn:publicid+IDN+' + hostname + '+project+' + fields.get(
                'PROJECT_NAME')
        fields['PROJECT_UID'] = str(uuid.uuid4())
        fields['PROJECT_CREATION'] = pyrfc3339.generate(
            datetime.datetime.utcnow().replace(tzinfo=pytz.utc))
        fields['PROJECT_EXPIRED'] = False
        return self._resource_manager_tools.object_create(
            self.AUTHORITY_NAME, fields, 'project')
コード例 #17
0
ファイル: views.py プロジェクト: bittukumarray/YouFetch
def dataFetcher():
    print("fetching...")
    now = datetime.now()
    timestamp = datetime.timestamp(now)
    timestamp -= 10000
    dt_object = datetime.fromtimestamp(timestamp)
    publishedAfterDate = generate(dt_object.replace(tzinfo=pytz.utc))
    maxResults = 100000
    q = "cricket"
    apiKey = "AIzaSyD1qMTlkWaB-GlsJMuhFVgQ27CeFK92oi0"
    url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&order=date&type=video'
    full_url = url + "&maxResults=" + str(maxResults) + "&q=" + str(
        q) + "&publishedAfter=" + publishedAfterDate + "&key=" + apiKey
    rqstData = requests.get(full_url)
    rqstDataJson = rqstData.json()
    for data in rqstDataJson["items"]:
        try:
            dataObj = Video.objects.create(
                videoId=data["id"]["videoId"],
                title=data["snippet"]["title"],
                description=data["snippet"]["description"],
                publishedDate=data["snippet"]["publishedAt"],
                thumb_default_url=data["snippet"]["thumbnails"]["default"]
                ["url"],
                thumb_high_url=data["snippet"]["thumbnails"]["high"]["url"],
                thumb_medium_url=data["snippet"]["thumbnails"]["medium"]
                ["url"])
            dataObj.save()
        except:
            pass
コード例 #18
0
    def block_to_json(self, html, month, year):
        """
        info index information:
        0 = day number      e.g. 19
        1 = start - end     e.g. (18:00 - 21:00) AH 152218:00 ~ 21:00geautoriseerd
        2 = start date      e.g. 18:00
        3 = end date        e.g. 21:00

        :param html: Html text that contains the information from ah.get_blocks()
        :param month: as 3 lettered string e.g. 'Apr'
        :param year: as 4 digit int.
        :return: json representation of the html.
        """
        soup = BeautifulSoup(str(html), 'html.parser')
        if not any(w in html for w in
                   ['calendarCellRegularPast', 'calendarCellRegularFuture']):
            return None

        # Remove spaces
        info = [
            span.text.replace('\t', '').replace('\n', '')
            for span in soup.find_all('span')
        ]

        # Convert the dates
        year = int(year)
        month = [
            "", "Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Aug", "Sep",
            "Okt", "Nov", "Dec"
        ].index(month[:3])
        day = int(info[0])
        # TODO: clean this up if possible
        starthour, startmin = [int(x) for x in info[2].split(":")]
        endhour, endmin = [int(x) for x in info[3].split(":")]

        # Generate RFC-3339 dates
        # TODO: clean this up if possible
        startdate = rfc.generate(self.timezone.localize(
            datetime(year, month, day, starthour, startmin)),
                                 utc=False)
        enddate = rfc.generate(self.timezone.localize(
            datetime(year, month, day, endhour, endmin)),
                               utc=False)

        return json.loads(
            self.jsonformat.replace('_start',
                                    startdate).replace('_end', enddate))
コード例 #19
0
ファイル: edn_dump.py プロジェクト: swaroopch/edn_format
def udump(obj,
          string_encoding=DEFAULT_INPUT_ENCODING,
          keyword_keys=False,
          sort_keys=False,
          sort_sets=False):

    kwargs = {
        "string_encoding": string_encoding,
        "keyword_keys": keyword_keys,
        "sort_keys": sort_keys,
        "sort_sets": sort_sets,
    }

    if obj is None:
        return 'nil'
    elif isinstance(obj, bool):
        return 'true' if obj else 'false'
    elif isinstance(obj, (int, long, float)):
        return unicode(obj)
    elif isinstance(obj, decimal.Decimal):
        return '{}M'.format(obj)
    elif isinstance(obj, (Keyword, Symbol)):
        return unicode(obj)
    # CAVEAT LECTOR! In Python 3 'basestring' is alised to 'str' above.
    # Furthermore, in Python 2 bytes is an instance of 'str'/'basestring' while
    # in Python 3 it is not.
    elif isinstance(obj, bytes):
        return unicode_escape(obj.decode(string_encoding))
    elif isinstance(obj, basestring):
        return unicode_escape(obj)
    elif isinstance(obj, tuple):
        return '({})'.format(seq(obj, **kwargs))
    elif isinstance(obj, (list, ImmutableList)):
        return '[{}]'.format(seq(obj, **kwargs))
    elif isinstance(obj, set) or isinstance(obj, frozenset):
        if sort_sets:
            obj = sorted(obj)
        return '#{{{}}}'.format(seq(obj, **kwargs))
    elif isinstance(obj, dict) or isinstance(obj, ImmutableDict):
        pairs = obj.items()
        if sort_keys:
            pairs = sorted(pairs, key=lambda p: str(p[0]))
        if keyword_keys:
            pairs = ((Keyword(k) if isinstance(k, (bytes, basestring)) else k, v) for k, v in pairs)

        return '{{{}}}'.format(seq(itertools.chain.from_iterable(pairs), **kwargs))
    elif isinstance(obj, fractions.Fraction):
        return '{}/{}'.format(obj.numerator, obj.denominator)
    elif isinstance(obj, datetime.datetime):
        return '#inst "{}"'.format(pyrfc3339.generate(obj, microseconds=True))
    elif isinstance(obj, datetime.date):
        return '#inst "{}"'.format(obj.isoformat())
    elif isinstance(obj, uuid.UUID):
        return '#uuid "{}"'.format(obj)
    elif isinstance(obj, TaggedElement):
        return unicode(obj)
    raise NotImplementedError(
        u"encountered object of type '{}' for which no known encoding is available: {}".format(
            type(obj), repr(obj)))
コード例 #20
0
    def test_generate_microseconds(self):
        '''
        Test generating timestamps with microseconds.

        '''
        dt = datetime(2009, 1, 1, 10, 2, 3, 500000, pytz.utc)
        timestamp = generate(dt, microseconds=True)
        eq_(timestamp, '2009-01-01T10:02:03.500000Z')
コード例 #21
0
ファイル: util.py プロジェクト: kurtraschke/cadors-parse
def process_report_atom(reports):
    out = []
    for report in reports:
        report.authors = set([narrative.author_name for narrative \
                                  in report.narrative_parts])
        report.atom_ts = generate(report.last_updated, accept_naive=True)
        out.append(report)
    return out
コード例 #22
0
ファイル: views.py プロジェクト: mesrut/openblock
def serialize_date_or_time(obj):
    if isinstance(obj, datetime.datetime):
        obj = normalize_datetime(obj)
        return pyrfc3339.generate(obj, utc=False)
    elif isinstance(obj, datetime.date):
        return obj.strftime('%Y-%m-%d')
    elif isinstance(obj, datetime.time):
        # XXX super ugly
        if obj.tzinfo is None:
            obj = obj.replace(tzinfo=LOCAL_TZ)
        dd = datetime.datetime.now()
        dd = dd.replace(hour=obj.hour, minute=obj.minute,
                        second=obj.second, tzinfo=obj.tzinfo)
        dd = normalize_datetime(dd)
        ss = pyrfc3339.generate(dd, utc=False)
        return ss.split('T', 1)[1]
    else:
        return None
コード例 #23
0
    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)
コード例 #24
0
def serialize_date_or_time(obj):
    if isinstance(obj, datetime.datetime):
        obj = normalize_datetime(obj)
        return pyrfc3339.generate(obj, utc=False)
    elif isinstance(obj, datetime.date):
        return obj.strftime('%Y-%m-%d')
    elif isinstance(obj, datetime.time):
        # XXX super ugly
        if obj.tzinfo is None:
            obj = obj.replace(tzinfo=LOCAL_TZ)
        dd = datetime.datetime.now()
        dd = dd.replace(hour=obj.hour, minute=obj.minute,
                        second=obj.second, tzinfo=obj.tzinfo)
        dd = normalize_datetime(dd)
        ss = pyrfc3339.generate(dd, utc=False)
        return ss[ss.index('T') + 1:]
    else:
        return None
コード例 #25
0
def time_before_caveat(t):
    '''Return a caveat that specifies that the time that it is checked at
    should be before t.
    :param t is a a UTC date in - use datetime.utcnow, not datetime.now
    '''

    return _first_party(COND_TIME_BEFORE,
                        pyrfc3339.generate(t, accept_naive=True,
                                           microseconds=True))
コード例 #26
0
def time_before_caveat(t):
    '''Return a caveat that specifies that the time that it is checked at
    should be before t.
    :param t is a a UTC date in - use datetime.utcnow, not datetime.now
    '''

    return _first_party(
        COND_TIME_BEFORE,
        pyrfc3339.generate(t, accept_naive=True, microseconds=True))
コード例 #27
0
    def create_slice(self, client_cert, credentials, fields, options):
        """
        Create a slice object.

        Generate fields for a new object:
            * SLICE_URN: retrieve the hostname from the Flask AMsoil plugin
                and form into a valid URN
            * SLICE_UID: generate a new UUID4 value
            * SLICE_CREATION: get the time now and convert it into RFC3339 form
            * SLICE_EXPIRED: slice object has just been created, so it is has not
                yet expired
        """

        self._resource_manager_tools.validate_credentials(credentials)
        geniutil = pm.getService('geniutil')

        #<UT> Shall we enforce existence of project to which this new slice would belong?
        #The information about project is sent in fields under SLICE_PROJECT_URN key
        config = pm.getService('config')
        hostname = config.get('flask.cbas_hostname')

        slice_urn = geniutil.encode_urn(hostname, 'slice', str(fields.get('SLICE_NAME')))
        fields['SLICE_URN'] =  slice_urn
        fields['SLICE_UID'] = str(uuid.uuid4())
        fields['SLICE_CREATION'] = pyrfc3339.generate(datetime.datetime.utcnow().replace(tzinfo=pytz.utc))
        fields['SLICE_EXPIRED'] = False

        #Generating Slice certificate
        s_cert, s_pu, s_pr = geniutil.create_certificate(slice_urn, self._sa_pr, self._sa_c)
        fields['SLICE_CERTIFICATE'] = s_cert

        #Try to get the user credentials for use as owner
        user_cert = geniutil.extract_owner_certificate(credentials)

        #Extract user info from his certificate
        user_urn, user_uuid, user_email = geniutil.extract_certificate_info(user_cert)
        #Get the privileges user would get as owner in the slice credential
        user_pri = self._delegate_tools.get_default_privilege_list(role_='LEAD', context_='SLICE')
        #Create slice cred for owner
        slice_cred = geniutil.create_credential_ex(owner_cert=user_cert, target_cert=s_cert, issuer_key=self._sa_pr,
                                                   issuer_cert=self._sa_c, privileges_list=user_pri, expiration=self.CRED_EXPIRY)

        #Let's make the owner as LEAD
        fields['SLICE_LEAD'] = user_urn

        #Finally, create slice object
        ret_values = self._resource_manager_tools.object_create(self.AUTHORITY_NAME, fields, 'slice')

        #Add slice credentials to the return values
        ret_values['SLICE_CREDENTIALS'] = slice_cred

        #Create SLICE_MEMBER object
        options = {'members_to_add' : [{'SLICE_MEMBER' : user_urn, 'SLICE_CREDENTIALS': slice_cred, 'SLICE_CERTIFICATE': s_cert, 'SLICE_ROLE': 'LEAD'}]}
        self._resource_manager_tools.member_modify(self.AUTHORITY_NAME, 'slice_member', slice_urn, options, 'SLICE_MEMBER', 'SLICE_URN')

        return ret_values
コード例 #28
0
ファイル: utils.py プロジェクト: text2gene/text2gene
 def default(self, obj):
     if hasattr(obj, 'strftime'):
         try:
             return generate(obj.replace(tzinfo=pytz.utc))
         except TypeError:
             # a time-zone-less date.
             return datetime.strftime(obj, '%Y-%m-%d')
     else:
         return str(obj)
     return json.JSONEncoder.default(self, obj)
コード例 #29
0
ファイル: tests.py プロジェクト: slinkp/openblock
    def test_items_filter_daterange_rfc3339(self):
        import pyrfc3339
        import pytz
        zone = 'US/Pacific'
        local_tz = pytz.timezone(zone)
        with self.settings(TIME_ZONE=zone):
            # create some items, they will have
            # dates spaced apart by one day, newest first
            schema1 = Schema.objects.get(slug='type1')
            items = _make_items(4, schema1)
            for item in items:
                item.save()

            # filter out the first and last item by constraining
            # the date range to the inner two items.
            # (Use local timezone for consistency with _make_items())
            startdate = pyrfc3339.generate(
                items[2].pub_date.replace(tzinfo=local_tz))
            enddate = pyrfc3339.generate(
                items[1].pub_date.replace(tzinfo=local_tz))
            # filter both ends
            qs = "?startdate=%s&enddate=%s" % (startdate, enddate)
            response = self.client.get(reverse('items_json') + qs)
            self.assertEqual(response.status_code, 200)
            ritems = simplejson.loads(response.content)
            self.assertEqual(len(ritems['features']), 2)
            assert self._items_exist_in_result(items[1:3], ritems)

            # startdate only
            qs = "?startdate=%s" % startdate
            response = self.client.get(reverse('items_json') + qs)
            self.assertEqual(response.status_code, 200)
            ritems = simplejson.loads(response.content)
            assert len(ritems['features']) == 3
            assert self._items_exist_in_result(items[:-1], ritems)

            # enddate only
            qs = "?enddate=%s" % enddate
            response = self.client.get(reverse('items_json') + qs)
            self.assertEqual(response.status_code, 200)
            ritems = simplejson.loads(response.content)
            assert len(ritems['features']) == 3
            assert self._items_exist_in_result(items[1:], ritems)
コード例 #30
0
    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)
コード例 #31
0
    def create_slice(self, client_cert, credentials, fields, options):
        """
        Create a slice object.

        Generate fields for a new object:
            * SLICE_URN: retrieve the hostname from the Flask AMsoil plugin
                and form into a valid URN
            * SLICE_UID: generate a new UUID4 value
            * SLICE_CREATION: get the time now and convert it into RFC3339 form
            * SLICE_EXPIRED: slice object has just been created, so it is has not
                yet expired
        """

        u_c = None
        #credentials_testing = credentials #self._resource_manager_tools.read_file(OSliceAuthorityResourceManager.KEY_PATH + "credentials_test")
        root = ET.fromstring(
            credentials[0]['SFA']
        )  #FIXME: short-term solution to fix string handling, take first credential of SFA format

        #print '-->'
        #print credentials
        #print '<--'

        self._resource_manager_tools.validate_credentials(credentials)
        config = pm.getService('config')
        geniutil = pm.getService('geniutil')
        hostname = config.get('flask.hostname')

        fields['SLICE_URN'] = geniutil.encode_urn(
            OSliceAuthorityResourceManager.AUTHORITY_NAME, 'slice',
            str(fields.get('SLICE_NAME')))
        fields['SLICE_UID'] = str(uuid.uuid4())
        fields['SLICE_CREATION'] = pyrfc3339.generate(
            datetime.datetime.utcnow().replace(tzinfo=pytz.utc))
        fields['SLICE_EXPIRED'] = False

        #Generating Slice Credentials
        s_c, s_pu, s_pr = geniutil.create_certificate(fields['SLICE_URN'],
                                                      self._sa_pr, self._sa_c)

        #default owner is slice itself
        u_c = s_c

        #Try to get the user credentials for use as owner
        for child in root:
            if child.tag == 'credential':
                u_c = child[2].text
                break

        fields['SLICE_CREDENTIALS'] = geniutil.create_credential(
            u_c, s_c, self._sa_pr, self._sa_c, "slice",
            OSliceAuthorityResourceManager.CRED_EXPIRY)

        return self._resource_manager_tools.object_create(
            self.AUTHORITY_NAME, fields, 'slice')
コード例 #32
0
ファイル: tests.py プロジェクト: horshacktest/openblock
    def test_items_filter_daterange_rfc3339(self):
        import pyrfc3339
        import pytz

        zone = "US/Pacific"
        local_tz = pytz.timezone(zone)
        with self.settings(TIME_ZONE=zone):
            # create some items, they will have
            # dates spaced apart by one day, newest first
            schema1 = Schema.objects.get(slug="type1")
            items = _make_items(4, schema1)
            for item in items:
                item.save()

            # filter out the first and last item by constraining
            # the date range to the inner two items.
            # (Use local timezone for consistency with _make_items())
            startdate = pyrfc3339.generate(items[2].pub_date.replace(tzinfo=local_tz))
            enddate = pyrfc3339.generate(items[1].pub_date.replace(tzinfo=local_tz))
            # filter both ends
            qs = "?startdate=%s&enddate=%s" % (startdate, enddate)
            response = self.client.get(reverse("items_json") + qs)
            self.assertEqual(response.status_code, 200)
            ritems = simplejson.loads(response.content)
            self.assertEqual(len(ritems["features"]), 2)
            assert self._items_exist_in_result(items[1:3], ritems)

            # startdate only
            qs = "?startdate=%s" % startdate
            response = self.client.get(reverse("items_json") + qs)
            self.assertEqual(response.status_code, 200)
            ritems = simplejson.loads(response.content)
            assert len(ritems["features"]) == 3
            assert self._items_exist_in_result(items[:-1], ritems)

            # enddate only
            qs = "?enddate=%s" % enddate
            response = self.client.get(reverse("items_json") + qs)
            self.assertEqual(response.status_code, 200)
            ritems = simplejson.loads(response.content)
            assert len(ritems["features"]) == 3
            assert self._items_exist_in_result(items[1:], ritems)
コード例 #33
0
    def rfc3339(cls, datetime):
        """Returns ``datetime`` formatted as a rfc3339 string.

        :param datetime datetime: The date time to convert

        :returns: the rfc3339 formatted string
        :rtype: str
        """

        datetime_localized = cls.EASTERN.localize(datetime)
        return generate(datetime_localized, utc=False)
コード例 #34
0
    def rfc3339(klass, datetime):
        """Returns ``datetime`` formatted as a rfc3339 string.

        :param datetime datetime: The date time to convert

        :returns: the rfc3339 formatted string
        :rtype: str
        """

        datetime_localized = klass.EASTERN.localize(datetime)
        return generate(datetime_localized, utc=False)
コード例 #35
0
    def create_project(self, client_cert, credentials, fields, options):
        """
        Create a project object.

        Generate fields for a new object:
            * PROJECT_URN: retrieve the hostname from the Flask AMsoil plugin
                and form into a valid URN
            * PROJECT_UID: generate a new UUID4 value
            * PROJECT_CREATION: get the time now and convert it into RFC3339 form
            * PROJECT_EXPIRED: project object has just been created, so it is
                has not yet expired

        """
        config = pm.getService('config')
        hostname = config.get('flask.cbas_hostname')
        p_urn = 'urn:publicid:IDN+' + hostname + '+project+' + fields.get('PROJECT_NAME')

        fields['PROJECT_URN'] = p_urn
        fields['PROJECT_UID'] = str(uuid.uuid4())
        fields['PROJECT_CREATION'] = pyrfc3339.generate(datetime.datetime.utcnow().replace(tzinfo=pytz.utc))
        fields['PROJECT_EXPIRED'] = False


        #<UT>
        geniutil = pm.getService('geniutil')
        #Generating Project Certificate
        p_cert, p_pu, p_pr = geniutil.create_certificate(p_urn, self._sa_pr, self._sa_c)
        fields['PROJECT_CERTIFICATE'] = p_cert

        #Try to get the user credentials for use as owner
        user_cert = geniutil.extract_owner_certificate(credentials)

        #Extract user info from his certificate
        user_urn, user_uuid, user_email = geniutil.extract_certificate_info(user_cert)
        #Get the privileges user would get as owner in the project credential
        user_pri = self._delegate_tools.get_default_privilege_list(role_='LEAD', context_='PROJECT')
        #Create project cred for owner
        p_creds = geniutil.create_credential_ex(owner_cert=user_cert, target_cert=p_cert, issuer_key=self._sa_pr, issuer_cert=self._sa_c, privileges_list=user_pri,
                                                    expiration=OSliceAuthorityResourceManager.CRED_EXPIRY)

        #Let's make the owner as LEAD
        fields['PROJECT_LEAD'] = user_urn

        #Finally, create project object
        ret_values = self._resource_manager_tools.object_create(self.AUTHORITY_NAME, fields, 'project')
        #Add Project credentials to ret values
        ret_values['PROJECT_CREDENTIALS'] = p_creds

        #Create PROJECT_MEMBER object
        options = {'members_to_add' : [{'PROJECT_MEMBER': user_urn, 'PROJECT_CREDENTIALS': p_creds, 'PROJECT_ROLE': 'LEAD'}]}
        self._resource_manager_tools.member_modify(self.AUTHORITY_NAME, 'project_member', p_urn, options, 'PROJECT_MEMBER', 'PROJECT_URN')

        return ret_values
コード例 #36
0
    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)
コード例 #37
0
    def create_sliver_info(self, credentials, fields, options):
        """
        Create a sliver information object.
        """
        # Verify the uniqueness of sliver urn
        lookup_results = self._resource_manager_tools.object_lookup(self.AUTHORITY_NAME, 'sliver_info',
                                                                    {'SLIVER_INFO_URN': fields['SLIVER_INFO_URN']}, {})
        if len(lookup_results) > 0:
                raise self.gfed_ex.GFedv2DuplicateError("A sliver_info with specified urn already exists.")

        # Add creation info
        fields['SLIVER_INFO_CREATION'] = pyrfc3339.generate(datetime.datetime.utcnow().replace(tzinfo=pytz.utc))
        return self._resource_manager_tools.object_create(self.AUTHORITY_NAME, fields, 'sliver_info')
コード例 #38
0
ファイル: gapi.py プロジェクト: BaxterStockman/cloudendar
    def run_freebusy_query(self, users, start_time=None, end_time=None, postfix=None, **kwargs):
        """ Returns the result of executing a freebusy query

            @param users: collection of strings representing ONID usernames
            @param start_time: datetime object representing start of interval
            @param end_time: datetime object representing end of interval
            @param postfic: string reprenting email address postfix

            @returns: Returns a dictionary of Google freebusy calendars keyed to
                EMAIL ADDRESSES, not ONIDs.
        """
        if postfix is None:
            postfix = EMAIL_POSTFIX

        self.onids = [user + postfix for user in users]
        ids = [{"id": onid} for onid in self.onids]

        start_time, end_time = self._format_start_end(start_time, end_time)

        # Create strings from datetime objects so that we can pass them to the
        # Google freebusy API
        start_time_str = generate(start_time)
        end_time_str = generate(end_time)

        # Create the freebusy API and store in the object
        self.api = self.service.freebusy()

        # Create the freebusy query body
        query = self.build_freebusy_query(ids, start_time_str, end_time_str, **kwargs)

        # Create the request and store the result in the object
        self.request = self.api.query(body=query)

        # Return the executed request
        calendars = self.request.execute()

        # TODO: handle non-existent users, and other errors.

        return calendars
コード例 #39
0
 def _as_dict(self, value):
     try:
         return value.as_dict()
     except AttributeError:
         """ If we encounter a dict with all None-elements, we return None.
             This is because the Kubernetes-API does not support empty string values, or "null" in json.
         """
         if isinstance(value, dict):
             d = {k: v for k, v in value.items() if v is not None}
             return d if d else None
         elif datetime in (self.type, self.alt_type) and isinstance(value, datetime):
             return pyrfc3339.generate(value, accept_naive=True)
         else:
             return value
コード例 #40
0
    def create_sliver_info(self, credentials, fields, options):
        """
        Create a sliver information object.
        """
        # Verify the uniqueness of sliver urn
        lookup_results = self._resource_manager_tools.object_lookup(
            self.AUTHORITY_NAME, "sliver_info", {"SLIVER_INFO_URN": fields["SLIVER_INFO_URN"]}, {}
        )
        if len(lookup_results) > 0:
            raise self.gfed_ex.GFedv2DuplicateError("A sliver_info with specified urn already exists.")

        # Add creation info
        fields["SLIVER_INFO_CREATION"] = pyrfc3339.generate(datetime.datetime.utcnow().replace(tzinfo=pytz.utc))
        return self._resource_manager_tools.object_create(self.AUTHORITY_NAME, fields, "sliver_info")
コード例 #41
0
ファイル: getPaid.py プロジェクト: cmccorkle21/getPaid
def revenue(periodStart, periodEnd, payRate):  # Strings in form MM/DD/YY
    service = build('calendar', 'v3', credentials=creds)

    fullStart = (periodStart + " 00:00:00")
    fullEnd = (periodEnd + " 23:59:59")

    periodStartDateTime = datetime.strptime(fullStart, "%m/%d/%Y %H:%M:%S")
    periodEndDateTime = datetime.strptime(fullEnd, "%m/%d/%Y %H:%M:%S")

    rfcStart = generate(periodStartDateTime, accept_naive=True)
    rfcEnd = generate(periodEndDateTime, accept_naive=True)

    events_result = service.events().list(calendarId='primary',
                                          timeMin=rfcStart,
                                          timeMax=rfcEnd,
                                          maxResults=50,
                                          singleEvents=True,
                                          orderBy='startTime').execute()
    events = events_result.get('items', [])

    total = 0

    for event in events:
        if (event['summary'] == "Event Title"):
            startTime = event['start']['dateTime']
            endTime = event['end']['dateTime']

            startDateTime = parse(startTime)
            endDateTime = parse(endTime)

            dateTimeDifference = endDateTime - startDateTime
            dateTimeDifferenceInHours = dateTimeDifference.total_seconds(
            ) / 3600

            total += dateTimeDifferenceInHours
    return (total * payRate)
コード例 #42
0
    def create_slice(self, client_cert, credentials, fields, options):
        """
        Create a slice object.

        Generate fields for a new object:
            * SLICE_URN: retrieve the hostname from the Flask AMsoil plugin
                and form into a valid URN
            * SLICE_UID: generate a new UUID4 value
            * SLICE_CREATION: get the time now and convert it into RFC3339 form
            * SLICE_EXPIRED: slice object has just been created, so it is has not
                yet expired
        """

        u_c = None
        #credentials_testing = credentials #self._resource_manager_tools.read_file(OSliceAuthorityResourceManager.KEY_PATH + "credentials_test")
        root = ET.fromstring(credentials[0]['SFA']) #FIXME: short-term solution to fix string handling, take first credential of SFA format

        #print '-->'
        #print credentials
        #print '<--'

        self._resource_manager_tools.validate_credentials(credentials)
        config = pm.getService('config')
        geniutil = pm.getService('geniutil')
        hostname = config.get('flask.hostname')

        fields['SLICE_URN'] =  geniutil.encode_urn(OSliceAuthorityResourceManager.AUTHORITY_NAME, 'slice', str(fields.get('SLICE_NAME')))
        fields['SLICE_UID'] = str(uuid.uuid4())
        fields['SLICE_CREATION'] = pyrfc3339.generate(datetime.datetime.utcnow().replace(tzinfo=pytz.utc))
        fields['SLICE_EXPIRED'] = False

        #Generating Slice Credentials
        s_c, s_pu, s_pr = geniutil.create_certificate(fields['SLICE_URN'], self._sa_pr, self._sa_c)

        #default owner is slice itself
        u_c = s_c

        #Try to get the user credentials for use as owner
        for child in root:
            if child.tag == 'credential':
                u_c = child[2].text
                break

        fields['SLICE_CREDENTIALS'] = geniutil.create_credential(u_c, s_c, self._sa_pr, self._sa_c, "slice",
                                                OSliceAuthorityResourceManager.CRED_EXPIRY)

        return self._resource_manager_tools.object_create(self.AUTHORITY_NAME, fields, 'slice')
コード例 #43
0
    def write(self):
        """ 
        Writes time to bookmark file. Adds one second to event.
        """

        try:
            self.new_bookmark_p1 = self.new_bookmark + timedelta(0,1)
            self.new_bookmark_str = generate(self.new_bookmark_p1)
            try:
                with open(self.bmfile, 'w') as self.open_bmfile:
                    self.open_bmfile.write(self.new_bookmark_str)
                    self.open_bmfile.flush()
                    logging.debug('Updated bookmark file: %s', self.new_bookmark_str)
            except OSError:
                    logging.error('Bookmark file could not be written')
        except AttributeError:
            logging.debug('No new timestamps. Bookmark remains unchanged')
コード例 #44
0
ファイル: image.py プロジェクト: zgrid/bootstrap-vz
    def run(cls, info):
        import pyrfc3339
        from datetime import datetime
        import pytz
        labels = {}
        labels['name'] = info.manifest.name.format(**info.manifest_vars)
        # Inspired by https://github.com/projectatomic/ContainerApplicationGenericLabels
        # See here for the discussion on the debian-cloud mailing list
        # https://lists.debian.org/debian-cloud/2015/05/msg00071.html
        labels['architecture'] = info.manifest.system['architecture']
        labels['build-date'] = pyrfc3339.generate(datetime.utcnow().replace(tzinfo=pytz.utc))
        if 'labels' in info.manifest.provider:
            for label, value in info.manifest.provider['labels'].items():
                labels[label] = value.format(**info.manifest_vars)

        from pipes import quote
        for label, value in labels.items():
            info._docker['dockerfile'].append('LABEL {}={}'.format(label, quote(value)))
コード例 #45
0
    def fetch_videos(self, max_results=10):
        query = self.query_term
        api_key = self.get_key()
        url = self.get_url()

        published_after = self.get_published_after()
        published_after_rfc3339_format = pyrfc3339.generate(published_after)

        params = {
            "part": "snippet",
            "maxResults": max_results,
            "q": query,
            "key": api_key,
            "publishedAfter": published_after_rfc3339_format,
            "type": "video",
        }
        print(params)
        response = requests.get(url, params=params)
        print(f"Response from youtube api with status: {response.status_code}")
        return response
コード例 #46
0
    def create_project(self, client_cert, credentials, fields, options):
        """
        Create a project object.

        Generate fields for a new object:
            * PROJECT_URN: retrieve the hostname from the Flask AMsoil plugin
                and form into a valid URN
            * PROJECT_UID: generate a new UUID4 value
            * PROJECT_CREATION: get the time now and convert it into RFC3339 form
            * PROJECT_EXPIRED: project object has just been created, so it is
                has not yet expired

        """
        config = pm.getService('config')
        hostname = config.get('flask.hostname')

        fields['PROJECT_URN'] = 'urn:publicid+IDN+' + hostname + '+project+' + fields.get('PROJECT_NAME')
        fields['PROJECT_UID'] = str(uuid.uuid4())
        fields['PROJECT_CREATION'] = pyrfc3339.generate(datetime.datetime.utcnow().replace(tzinfo=pytz.utc))
        fields['PROJECT_EXPIRED'] = False
        return self._resource_manager_tools.object_create(self.AUTHORITY_NAME, fields, 'project')
コード例 #47
0
ファイル: util.py プロジェクト: kurtraschke/cadors-parse
def json_default(obj):
    if isinstance(obj, CadorsReport):
        result = dict(obj)
        del result['narrative_agg']
        result['categories'] = obj.categories
        result['aircraft'] = obj.aircraft
        result['narrative_parts'] = obj.narrative_parts
        result['locations'] = list(obj.locations)
        return result
    if isinstance(obj, ReportCategory):
        return obj.text
    if isinstance(obj, Aircraft):
        result = dict(obj)
        del result['aircraft_id']
        del result['cadors_number']
        return result
    if isinstance(obj, NarrativePart):
        result = dict(obj)
        del result['cadors_number']
        del result['narrative_html']
        del result['narrative_xml']
        del result['narrative_part_id']
        return result
    if isinstance(obj, LocationBase):
        result = dict(obj)
        del result['location_id']
        del result['discriminator']
        if 'blacklist' in result.keys():
            del result['blacklist']
        return result
    if isinstance(obj, UUID):
        return str(obj)
    if isinstance(obj, datetime):
        return generate(obj, accept_naive=True)
    if isinstance(obj, PersistentSpatialElement):
        return {"type": "Point",
                "coordinates": [
                db.session.query(obj.x).scalar(),
                db.session.query(obj.y).scalar()]}
コード例 #48
0
ファイル: edn_dump.py プロジェクト: hcarvalhoalves/edn_format
def udump(obj, string_encoding = DEFAULT_INPUT_ENCODING):
    if obj is None:
        return 'nil'
    elif isinstance(obj, bool):
        return 'true' if obj else 'false'
    elif isinstance(obj, (int, long, float)):
        return unicode(obj)
    elif isinstance(obj, decimal.Decimal):
        return '{}M'.format(obj)
    elif isinstance(obj, (Keyword, Symbol)):
        return unicode(obj)
    # CAVEAT EMPTOR! In Python 3 'basestring' is alised to 'str' above.
    # Furthermore, in Python 2 bytes is an instance of 'str'/'basestring' while
    # in Python 3 it is not.
    elif isinstance(obj, bytes):
        return unicode_escape(obj.decode(string_encoding))
    elif isinstance(obj, basestring):
        return unicode_escape(obj)
    elif isinstance(obj, tuple):
        return '({})'.format(seq(obj, string_encoding))
    elif isinstance(obj, list):
        return '[{}]'.format(seq(obj, string_encoding))
    elif isinstance(obj, set) or isinstance(obj, frozenset):
        return '#{{{}}}'.format(seq(obj, string_encoding))
    elif isinstance(obj, dict) or isinstance(obj, ImmutableDict):
        return '{{{}}}'.format(seq(itertools.chain.from_iterable(obj.items()),
            string_encoding))
    elif isinstance(obj, datetime.datetime):
        return '#inst "{}"'.format(pyrfc3339.generate(obj, microseconds=True))
    elif isinstance(obj, datetime.date):
        return '#inst "{}"'.format(obj.isoformat())
    elif isinstance(obj, uuid.UUID):
        return '#uuid "{}"'.format(obj)
    elif isinstance(obj, TaggedElement):
        return unicode(obj)
    raise NotImplementedError(
        u"encountered object of type '{}' for which no known encoding is available: {}".format(
            type(obj), repr(obj)))
コード例 #49
0
ファイル: views.py プロジェクト: frankk00/openblock
def _items_json(items):
    result = {
        'type': 'FeatureCollection',
        'features': []
    }
    for i in items:
        if i.location is None: 
            continue
        geom = simplejson.loads(i.location.geojson)
        item = {
            # 'id': i.id, # XXX ?
            'type': 'Feature',
            'geometry': geom,
        }
        props = {}
        # Quirk: the attributes property is an empty dict until you do
        # something that triggers populating it! calling items() is
        # sufficient.
        props = dict(i.attributes.items())
        props.update({
            'type': i.schema.slug,
            'title': i.title,
            'description': i.description,
            'url': i.url,
            'pub_date': pyrfc3339.generate(normalize_datetime(i.pub_date), utc=False),
            'item_date': i.item_date,
        })
        item['properties'] = props
        result['features'].append(item)

    def _serialize_unknown(obj):
        if (isinstance(obj, datetime.datetime)
            or isinstance(obj, datetime.date)
            or isinstance(obj, datetime.time)):
            return serialize_date_or_time(obj)
        return None
    return simplejson.dumps(result, default=_serialize_unknown, indent=1)
コード例 #50
0
ファイル: util.py プロジェクト: kurtraschke/cadors-parse
def render_list(pagination, title, format):
    if format == 'json':
        response = make_response(json.dumps(
                {'reports': pagination.items},
                indent=None if request.is_xhr else 2,
                default=json_default))
        response.mimetype = "application/json"
    elif format == 'atom':
        reports = process_report_atom(pagination.items)
        feed_timestamp = generate(max(pagination.items,
                                      key=lambda r:r.last_updated).last_updated,
                                  accept_naive=True)
        next = modified_url_for(page=pagination.next_num,
                                _external=True
                                ) if pagination.has_next else False
        prev = modified_url_for(page=pagination.prev_num,
                                _external=True
                                ) if pagination.has_prev else False

        response = make_response(
            render_template('feed.xml',
                            reports=reports,
                            feed_timestamp=feed_timestamp,
                            next=next,
                            prev=prev,
                            title=title))
        response.mimetype = "application/atom+xml"
    elif format == 'html':
        response = make_response(render_template('list.html',
                                                 reports=pagination.items,
                                                 pagination=pagination,
                                                 title=title))
    else:
        abort(400)

    return prepare_response(response, 3600)
コード例 #51
0
def now():
    now_utc = datetime.datetime.utcnow()
    return pyrfc3339.generate(now_utc.replace(tzinfo=pytz.utc))
コード例 #52
0
ファイル: account.py プロジェクト: certbot/certbot
 def slug(self):
     """Short account identification string, useful for UI."""
     return "{1}@{0} ({2})".format(pyrfc3339.generate(
         self.meta.creation_dt), self.meta.creation_host, self.id[:4])
コード例 #53
0
ファイル: credential.py プロジェクト: wvdemeer/C-BAS
    def encode(self):
        # Create the XML document
        doc = Document()
        signed_cred = doc.createElement("signed-credential")

# Declare namespaces
# Note that credential/policy.xsd are really the PG schemas
# in a PL namespace.
# Note that delegation of credentials between the 2 only really works
# cause those schemas are identical.
# Also note these PG schemas talk about PG tickets and CM policies.
        signed_cred.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
        signed_cred.setAttribute("xsi:noNamespaceSchemaLocation", "http://www.planet-lab.org/resources/sfa/credential.xsd")
        signed_cred.setAttribute("xsi:schemaLocation", "http://www.planet-lab.org/resources/sfa/ext/policy/1 http://www.planet-lab.org/resources/sfa/ext/policy/1/policy.xsd")

# PG says for those last 2:
#        signed_cred.setAttribute("xsi:noNamespaceSchemaLocation", "http://www.protogeni.net/resources/credential/credential.xsd")
#        signed_cred.setAttribute("xsi:schemaLocation", "http://www.protogeni.net/resources/credential/ext/policy/1 http://www.protogeni.net/resources/credential/ext/policy/1/policy.xsd")

        doc.appendChild(signed_cred)  
        
        # Fill in the <credential> bit        
        cred = doc.createElement("credential")
        cred.setAttribute("xml:id", self.get_refid())
        signed_cred.appendChild(cred)
        append_sub(doc, cred, "type", "privilege")
        append_sub(doc, cred, "serial", "8")
        append_sub(doc, cred, "owner_gid", self.gidCaller.save_to_string())
        append_sub(doc, cred, "owner_urn", self.gidCaller.get_urn())
        append_sub(doc, cred, "target_gid", self.gidObject.save_to_string())
        append_sub(doc, cred, "target_urn", self.gidObject.get_urn())
        append_sub(doc, cred, "uuid", "")

        #was: if not self.expiration:
        #was:    self.set_expiration(datetime.datetime.utcnow() + datetime.timedelta(seconds=DEFAULT_CREDENTIAL_LIFETIME))
        if not self.expiration:
            logger.error('credential.py usage bug: credential was created without expire')
            raise RuntimeError('credential.py usage bug: credential was created without expire')
        if (not isinstance (self.expiration, datetime.datetime)) or self.expiration.tzinfo is None or self.expiration.tzinfo.utcoffset(self.expiration) is None:
            #credential.py bug because set_expiration should have handled this
            logger.error('credential.py bug: credential was has expire time missing timezone info')
            raise RuntimeError('credential.py bug: credential was has expire time missing timezone info')
        else:
            self.expiration = self.expiration.replace(microsecond=0)
            append_sub(doc, cred, "expires", pyrfc3339.generate(self.expiration))
            #was: append_sub(doc, cred, "expires", self.expiration.isoformat())

        privileges = doc.createElement("privileges")
        cred.appendChild(privileges)

        if self.privileges:
            rights = self.get_privileges()
            for right in rights.rights:
                priv = doc.createElement("privilege")
                append_sub(doc, priv, "name", right.kind)
                append_sub(doc, priv, "can_delegate", str(right.delegate).lower())
                privileges.appendChild(priv)

        # Add the parent credential if it exists
        if self.parent:
            sdoc = parseString(self.parent.get_xml())
            # If the root node is a signed-credential (it should be), then
            # get all its attributes and attach those to our signed_cred
            # node.
            # Specifically, PG and PLadd attributes for namespaces (which is reasonable),
            # and we need to include those again here or else their signature
            # no longer matches on the credential.
            # We expect three of these, but here we copy them all:
#        signed_cred.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
# and from PG (PL is equivalent, as shown above):
#        signed_cred.setAttribute("xsi:noNamespaceSchemaLocation", "http://www.protogeni.net/resources/credential/credential.xsd")
#        signed_cred.setAttribute("xsi:schemaLocation", "http://www.protogeni.net/resources/credential/ext/policy/1 http://www.protogeni.net/resources/credential/ext/policy/1/policy.xsd")

            # HOWEVER!
            # PL now also declares these, with different URLs, so
            # the code notices those attributes already existed with
            # different values, and complains.
            # This happens regularly on delegation now that PG and
            # PL both declare the namespace with different URLs.
            # If the content ever differs this is a problem,
            # but for now it works - different URLs (values in the attributes)
            # but the same actual schema, so using the PG schema
            # on delegated-to-PL credentials works fine.

            # Note: you could also not copy attributes
            # which already exist. It appears that both PG and PL
            # will actually validate a slicecred with a parent
            # signed using PG namespaces and a child signed with PL
            # namespaces over the whole thing. But I don't know
            # if that is a bug in xmlsec1, an accident since
            # the contents of the schemas are the same,
            # or something else, but it seems odd. And this works.
            parentRoot = sdoc.documentElement
            if parentRoot.tagName == "signed-credential" and parentRoot.hasAttributes():
                for attrIx in range(0, parentRoot.attributes.length):
                    attr = parentRoot.attributes.item(attrIx)
                    # returns the old attribute of same name that was
                    # on the credential
                    # Below throws InUse exception if we forgot to clone the attribute first
                    oldAttr = signed_cred.setAttributeNode(attr.cloneNode(True))
                    if oldAttr and oldAttr.value != attr.value:
                        msg = "Delegating cred from owner %s to %s over %s:\n - Replaced attribute %s value '%s' with '%s'" % (self.parent.gidCaller.get_urn(), self.gidCaller.get_urn(), self.gidObject.get_urn(), oldAttr.name, oldAttr.value, attr.value)
                        logger.warn(msg)
                        #raise CredentialNotVerifiable("Can't encode new valid delegated credential: %s" % msg)

            p_cred = doc.importNode(sdoc.getElementsByTagName("credential")[0], True)
            p = doc.createElement("parent")
            p.appendChild(p_cred)
            cred.appendChild(p)
        # done handling parent credential

        # Create the <signatures> tag
        signatures = doc.createElement("signatures")
        signed_cred.appendChild(signatures)

        # Add any parent signatures
        if self.parent:
            for cur_cred in self.get_credential_list()[1:]:
                sdoc = parseString(cur_cred.get_signature().get_xml())
                ele = doc.importNode(sdoc.getElementsByTagName("Signature")[0], True)
                signatures.appendChild(ele)
                
        # Get the finished product
        self.xml = doc.toxml()
コード例 #54
0
ファイル: clj.py プロジェクト: bhagany/pyclj
    def __do_encode(self, d):
        fd = self.fd
        t,coll = self.get_type(d)

        if coll:
            refid = id(d)
            if refid in self.circular:
                raise ValueError('Circular reference detected')
            else:
                self.circular[refid] = True

            if t == "dict":
                fd.write("{")
                if len(d) > 0:
                    for k,v in d.items():
                        self.__do_encode(k)
                        fd.write(" ")
                        self.__do_encode(v)
                        fd.write(" ")
                    fd.seek(-1, os.SEEK_CUR)
                fd.write("}")
            elif t == "list":
                fd.write("[")
                if len(d) > 0:
                    for v in d:
                        self.__do_encode(v)
                        fd.write(" ")
                    fd.seek(-1, os.SEEK_CUR)
                fd.write("]")
            elif t == "set":
                fd.write("#{")
                if len(d) > 0:
                    for v in d:
                        self.__do_encode(v)
                        fd.write(" ")
                    fd.seek(-1, os.SEEK_CUR)
                fd.write("}")
        else:
            if t == "number":
                fd.write(str(d))
            elif t == "decimal":
                fd.write(str(d) + 'M')
            elif t == "string":
                s = json.encoder.py_encode_basestring_ascii(unicode(d))
                fd.write(s)
            elif t == "boolean":
                if d:
                    fd.write('true')
                else:
                    fd.write('false')
            elif t == 'None':
                fd.write('nil')
            elif t == 'datetime':
                if not d.tzinfo:
                    ## replace naive datetime
                    d = d.replace(tzinfo=pytz.utc)
                s = pyrfc3339.generate(d)
                fd.write("#inst \"%s\"" % s)
            elif t == 'uuid':
                s = str(d)
                fd.write("#uuid \"%s\"" % s)
            else:
                s = json.encoder.py_encode_basestring_ascii(unicode(d))
                fd.write(s)