Exemplo n.º 1
0
def json_decode(raw_json):
    '''
    Recusively scan the de-serialised JSON tree for objects that correspond to
    standard Haystack types and return the Python equivalents.
    '''
    # Simple cases
    if raw_json is None:
        return None
    elif raw_json == MARKER:
        return hszinc.MARKER
    elif isinstance(raw_json, dict):
        result = {}
        for key, value in raw_json.items():
            result[json_decode(key)] = json_decode(value)
        return result
    elif isinstance(raw_json, list):
        return list(map(json_decode, raw_json))
    elif isinstance(raw_json, tuple):
        return tuple(map(json_decode, raw_json))

    # Is it a number?
    match = NUMBER_RE.match(raw_json)
    if match:
        # We'll get a value and a unit, amongst other tokens.
        matched = match.groups()
        value = float(matched[0])
        if matched[-1] is not None:
            # It's a quantity
            return hszinc.Quantity(value, matched[-1])
        else:
            # It's a raw value
            return value

    # Is it a string?
    match = STR_RE.match(raw_json)
    if match:
        return match.group(1)

    # Is it a reference?
    match = REF_RE.match(raw_json)
    if match:
        matched = match.groups()
        if matched[-1] is not None:
            return hszinc.Ref(matched[0], matched[-1], has_value=True)
        else:
            return hszinc.Ref(matched[0])

    # Is it a date?
    match = DATE_RE.match(raw_json)
    if match:
        (year, month, day) = match.groups()
        return datetime.date(year=int(year), month=int(month), day=int(day))

    # Is it a time?
    match = TIME_RE.match(raw_json)
    if match:
        (hour, minute, second) = match.groups()
        # Convert second to seconds and microseconds
        second = float(second)
        int_second = int(second)
        second -= int_second
        microsecond = second * 1e6
        return datetime.time(hour=int(hour), minute=int(minute),
                second=int_second, microseconds=microsecond)

    # Is it a date/time?
    match = DATETIME_RE.match(raw_json)
    if match:
        matches = match.groups()
        # Parse ISO8601 component
        isodate = iso8601.parse_date(matches[0])
        # Parse timezone
        tzname  = matches[-1]
        if tzname is None:
            return isodate  # No timezone given
        else:
            try:
                tz = zoneinfo.timezone(tzname)
                return isodate.astimezone(tz)
            except:
                return isodate

    # Is it a URI?
    match = URI_RE.match(raw_json)
    if match:
        return hszinc.Uri(match.group(1))

    # Is it a Bin?
    match = BIN_RE.match(raw_json)
    if match:
        return hszinc.Bin(match.group(1))

    # Is it a co-ordinate?
    match = COORD_RE.match(raw_json)
    if match:
        (lat,lng) = match.groups()
        return hszinc.Coord(lat,lng)

    # Maybe it's a bare string?
    return raw_json
Exemplo n.º 2
0
def test_invalid_timezone():
    try:
        zoneinfo.timezone('ThisDoesNotExist')
        assert False, 'Returned a value for invalid time zone'
    except ValueError:
        pass
Exemplo n.º 3
0
def test_valid_timezone():
    assert zoneinfo.timezone('Brisbane') is pytz.timezone('Australia/Brisbane')
Exemplo n.º 4
0
def json_decode(raw_json):
    '''
    Recusively scan the de-serialised JSON tree for objects that correspond to
    standard Haystack types and return the Python equivalents.
    '''
    # Simple cases
    if raw_json is None:
        return None
    elif raw_json == MARKER:
        return hszinc.MARKER
    elif isinstance(raw_json, dict):
        result = {}
        for key, value in raw_json.items():
            result[json_decode(key)] = json_decode(value)
        return result
    elif isinstance(raw_json, list):
        return list(map(json_decode, raw_json))
    elif isinstance(raw_json, tuple):
        return tuple(map(json_decode, raw_json))

    # Is it a number?
    match = NUMBER_RE.match(raw_json)
    if match:
        # We'll get a value and a unit, amongst other tokens.
        matched = match.groups()
        value = float(matched[0])
        if matched[-1] is not None:
            # It's a quantity
            return hszinc.Quantity(value, matched[-1])
        else:
            # It's a raw value
            return value

    # Is it a string?
    match = STR_RE.match(raw_json)
    if match:
        return match.group(1)

    # Is it a reference?
    match = REF_RE.match(raw_json)
    if match:
        matched = match.groups()
        if matched[-1] is not None:
            return hszinc.Ref(matched[0], matched[-1], has_value=True)
        else:
            return hszinc.Ref(matched[0])

    # Is it a date?
    match = DATE_RE.match(raw_json)
    if match:
        (year, month, day) = match.groups()
        return datetime.date(year=int(year), month=int(month), day=int(day))

    # Is it a time?
    match = TIME_RE.match(raw_json)
    if match:
        (hour, minute, second) = match.groups()
        # Convert second to seconds and microseconds
        second = float(second)
        int_second = int(second)
        second -= int_second
        microsecond = second * 1e6
        return datetime.time(hour=int(hour),
                             minute=int(minute),
                             second=int_second,
                             microseconds=microsecond)

    # Is it a date/time?
    match = DATETIME_RE.match(raw_json)
    if match:
        matches = match.groups()
        # Parse ISO8601 component
        isodate = iso8601.parse_date(matches[0])
        # Parse timezone
        tzname = matches[-1]
        if tzname is None:
            return isodate  # No timezone given
        else:
            try:
                tz = zoneinfo.timezone(tzname)
                return isodate.astimezone(tz)
            except:
                return isodate

    # Is it a URI?
    match = URI_RE.match(raw_json)
    if match:
        return hszinc.Uri(match.group(1))

    # Is it a Bin?
    match = BIN_RE.match(raw_json)
    if match:
        return hszinc.Bin(match.group(1))

    # Is it a co-ordinate?
    match = COORD_RE.match(raw_json)
    if match:
        (lat, lng) = match.groups()
        return hszinc.Coord(lat, lng)

    # Maybe it's a bare string?
    return raw_json
Exemplo n.º 5
0
def test_filter_tag_equal_values():
    result = hs_filter.parseString('bool == true',
                                   parseAll=True)[0]  # Zinc use TF
    assert isinstance(result.right, bool)
    assert result.right is True

    result = hs_filter.parseString('number == 1', parseAll=True)[0]
    assert isinstance(result.right, float)
    assert result.right == 1.0

    result = hs_filter.parseString('number == 1.0', parseAll=True)[0]
    assert isinstance(result.right, float)
    assert result.right == 1.0

    result = hs_filter.parseString('str == "str"', parseAll=True)[0]
    assert isinstance(result.right, str)
    assert result.right == "str"

    result = hs_filter.parseString('uri == `uri`', parseAll=True)[0]
    assert isinstance(result.right, Uri)
    assert result.right == Uri("uri")

    result = hs_filter.parseString('date == 1977-04-22', parseAll=True)[0]
    assert isinstance(result.right, date)
    assert result.right == date(1977, 4, 22)

    result = hs_filter.parseString('time == 11:11:11', parseAll=True)[0]
    assert isinstance(result.right, time)
    assert result.right == time(11, 11, 11)

    result = hs_filter.parseString('date_time == 1977-04-22T01:00:00-05:00',
                                   parseAll=True)[0]
    assert isinstance(result.right, datetime)
    assert result.right == iso8601.parse_date("1977-04-22T01:00:00-05:00")

    result = hs_filter.parseString('date_time == 1977-04-22T01:00:00 GMT+1',
                                   parseAll=True)[0]
    assert isinstance(result.right, datetime)
    assert result.right == iso8601.parse_date(
        "1977-04-22T01:00:00").astimezone(timezone("GMT+1"))

    result = hs_filter.parseString('date_time == 1977-04-22T01:00:00 Paris',
                                   parseAll=True)[0]
    assert isinstance(result.right, datetime)
    assert result.right == iso8601.parse_date(
        "1977-04-22T01:00:00").astimezone(timezone("Paris"))

    # --- Extended syntax (types not in specification)
    result = hs_filter.parseString('ref == @abc', parseAll=True)[0]
    assert isinstance(result.right, Ref)
    assert result.right == Ref("abc")

    result = hs_filter.parseString('geo == @abc "a"', parseAll=True)[0]
    assert isinstance(result.right, Ref)
    assert result.right == Ref("abc", "a")

    result = hs_filter.parseString('ref == C(1.0,-1.0)', parseAll=True)[0]
    assert isinstance(result.right, Coordinate)
    assert result.right == Coordinate(1.0, -1.0)

    result = hs_filter.parseString('list == [ 1, 2 ]', parseAll=True)[0]
    assert isinstance(result.right, list)
    assert result.right == [1, 2]

    result = hs_filter.parseString('dict == { a b:2 }', parseAll=True)[0]
    assert isinstance(result.right, dict)
    assert result.right == {"a": MARKER, "b": 2.0}

    result = hs_filter.parseString('map == hex("010203")', parseAll=True)[0]
    assert isinstance(result.right, XStr)
    assert result.right == XStr("hex", "010203")
Exemplo n.º 6
0
    def __init__(self, session, hisId, dateTimeRange='today'):
        """
        GET data from server and fill this object with historical info
        """
        # Grab logger child from session
        self._log = session._log.getChild('hisRecord.%s' % hisId)

        # Grab metadata
        self._meta = session.getHistMeta(hisId)
        self.tz_name = self._meta['tz']
        self.tz = zoneinfo.timezone(self.tz_name)

        # Is dateTimeRange a tuple object?
        if isinstance(dateTimeRange, tuple):
            (dtStart, dtEnd) = dateTimeRange

            # Convert these to native time
            def _to_native(dt):
                self._log.debug('Converting %s to native time', dt)
                if isinstance(dt, datetime.datetime):
                    if dt.tzinfo is None:
                        # Assume time is already local
                        self._log.debug('Localise to timezone %s',
                                        self.tz_name)
                        dt = self.tz.localize(dt)
                    else:
                        self._log.debug('Convert to timezone %s', self.tz_name)
                        dt = dt.astimezone(self.tz)

                    return '%s %s' % (dt.isoformat(), self.tz_name)
                elif isinstance(dt, datetime.date):
                    return dt.isoformat()
                else:
                    return dt

            dateTimeRange = '%s,%s' % (_to_native(dtStart), _to_native(dtEnd))

        self.hisId = hisId
        self.name = self._meta.get('name')

        result = session._get_grid('hisRead',
                                   id='@%s' % self.hisId,
                                   range=dateTimeRange)
        self._log.debug('Received result set: %s', result)
        # Convert the list of {ts: foo, val: bar} dicts to a pair of
        # lists.
        if bool(result):
            strip_unit = lambda v: v.value if isinstance(v, Quantity) else v
            (index, values) = zip(*map(lambda row : \
                            (row['ts'], strip_unit(row['val'])), result))
        else:
            # No data
            (index, values) = ([], [])

        try:
            #Declare Series converted to local time for session
            self.data = Series(values,
                               index=index).tz_convert(session.timezone)
            #Renaming index so the name will be part of the serie
            self.data = self.data.reindex(self.data.index.rename([self.name]))
        except:
            self._log.error('%s is an Unknown history type', self.hisId)
            raise