Example #1
0
    def test_timelib(self):
        mt = timelib.localtime_mutable()
        print(mt)
        mt.add_seconds(3600)
        print(mt)
        print(timelib.strftime("%Y-%m-%d", timelib.weekof(timelib.time())))

        t = timelib.now()
        for d in range(1, 60):
            week = timelib.weekof(t+(d*60*60*24))
            print(timelib.MutableTime(week))

        print("Local time:")
        print(timelib.localtimestamp())

        p = timespec.TimespecParser()
        for spec, secs in [
            ("0s", 0.0),
            ("3m", 180.0),
            ("3.0m", 180.0),
            ("3minute+2secs", 182.0),
            ("2h 3minute+2.2secs", 7382.2),
            ("-3m", -180.0),
            ("-3.0m", -180.0),
            ("1h3m", 3780.0),
            ("1h-3m", 3420.0),
            ("1d 3m", 86580.0)]:
            p.parse(spec)
            self.assert_(p.seconds == secs)

        self.assertRaises(ValueError, p.parse, "12m -m")
Example #2
0
    def test_timelib(self):
        mt = timelib.localtime_mutable()
        print mt
        mt.add_seconds(3600)
        print mt
        print timelib.strftime("%Y-%m-%d", timelib.weekof(timelib.time()))

        t = timelib.now()
        for d in range(1, 60):
            week = timelib.weekof(t+(d*60*60*24))
            print timelib.MutableTime(week)

        print "Local time:"
        print timelib.localtimestamp()

        p = timelib.TimespecParser()
        for spec, secs in [
            ("0s", 0.0),
            ("3m", 180.0),
            ("3.0m", 180.0),
            ("3minute+2secs", 182.0),
            ("2h 3minute+2.2secs", 7382.2),
            ("-3m", -180.0),
            ("-3.0m", -180.0),
            ("1h3m", 3780.0),
            ("1h-3m", 3420.0),
            ("1d 3m", 86580.0)]:
            p.parse(spec)
            self.assert_(p.seconds == secs)

        self.assertRaises(ValueError, p.parse, "12m -m")
Example #3
0
 def __str__(self):
     mean, maxi, mini, median, cf = self.GetStats()
     ts = timelib.localtime_mutable(self.samplestart)
     ts.set_format("%a, %d %b %Y %H:%M:%S %Z")
     offset = GetHMSString(self.starttime - self.samplestart)
     span = GetHMSString(self.endtime - self.starttime)
     s = [
         "Samples started at %s.\n"
         "Data offset %s after sampling started and spans %s." %
         (ts, offset, span)
     ]
     s.append(str(self.metadata))
     s.append("")
     s.append("%s %s samples:" %
              (len(self.measurements), self.sampleperiod))
     s.append(" Maximum: %s" % maxi)
     s.append(" Minimum: %s" % mini)
     s.append("    Mean: %s" % mean)
     s.append("  Median: %s" % median)
     s.append("      CF: %s" % cf)
     return "\n".join(s)
Example #4
0
def decode_filename(pathname):
    """Decode a metadata encoded file name.

    Returns a DatFileData object populated with metadata.
    """
    data = DataFileData()
    pathname = os.path.abspath(pathname)
    dirname, fname = os.path.split(pathname)
    fname, ext = os.path.splitext(fname)
    nameparts = fname.split("+")
    data.directory = dirname
    data.name = nameparts[0]
    offset = 1
    try:
        mt = timelib.strptime_mutable(nameparts[offset], "%Y%m%d%H%M%S")
    except:
        mt = timelib.localtime_mutable(os.path.getmtime(pathname))
    else:
        offset += 1
    mt.set_format("%a, %d %b %Y %H:%M:%S %Z")
    data.timestamp = mt
    for part in nameparts[offset:]:
        mo = _STATE_RE.match(part)
        if mo:
            data[mo.group(1)] = _STATEMAP[mo.group(2)]
        else:
            mo = _OTHERDATA_RE.match(part)
            if mo:
                valuestring = mo.group(2)
                try:
                    value = eval(valuestring, {}, {})
                    if type(value) is str:
                        value = value.lower()
                except:
                    value = valuestring
                data[mo.group(1)] = value
            else:
                print("name component not matched: %r" % (part, ),
                      file=sys.stderr)
    return data
Example #5
0
def decode_filename(pathname):
    """Decode a metadata encoded file name.

    Returns a DatFileData object populated with metadata.
    """
    data = DataFileData()
    pathname = os.path.abspath(pathname)
    dirname, fname = os.path.split(pathname)
    fname, ext = os.path.splitext(fname)
    nameparts = fname.split("+")
    data.directory = dirname
    data.name = nameparts[0]
    offset = 1
    try:
        mt = timelib.strptime_mutable(nameparts[offset], "%Y%m%d%H%M%S")
    except:
        mt = timelib.localtime_mutable(os.path.getmtime(pathname))
    else:
        offset += 1
    mt.set_format("%a, %d %b %Y %H:%M:%S %Z")
    data.timestamp = mt
    for part in nameparts[offset:]:
        mo = _STATE_RE.match(part)
        if mo:
            data[mo.group(1)] = _STATEMAP[mo.group(2)]
        else:
            mo = _OTHERDATA_RE.match(part)
            if mo:
                valuestring = mo.group(2)
                try:
                    value = eval(valuestring, {}, {})
                    if type(value) is str:
                        value = value.lower()
                except:
                    value = valuestring
                data[mo.group(1)] = value
            else:
                print("name component not matched: %r" % (part,), file=sys.stderr)
    return data
Example #6
0
 def set_timestamp(self, time=None):
     if time is None:
         time = timelib.now()
     self["timestamp"] = timelib.localtime_mutable(time)
Example #7
0
def DecodeFullPathName(pathname):
  data = DataFileData()
  data.build = dictlib.AttrDict()
  data.pathname = pathname
  data.rollup = 0
  data.voltage = 0.0
  data.samples = 0
  pathname = os.path.abspath(pathname)
  dirname, fname = os.path.split(pathname)
  nameparts = fname.split("-")
  dirparts = dirname.split("/")
  if "data" in dirparts:
    data.build.id = dirparts[-1]
    data.build.type = dirparts[-2]
    data.build.product = dirparts[-3]
  else:
    data.build = None
  try:
    mt = timelib.strptime_mutable(nameparts[1], "%m%d%H%M%S")
  except:
    data.timestamp = timelib.localtime_mutable(os.path.getmtime(pathname))
  else:
    mt.set_format("%a, %d %b %Y %H:%M:%S %Z")
    # year info is not encoded in the timestamp, so get it from file system.
    mtime = timelib.localtime_mutable(os.path.getmtime(pathname))
    mt.year = mtime.tm_year
    data.timestamp = mt
  data.testcase = nameparts[0]

  if len(nameparts) > 1:
    endoffset = -1
    try:
      data.voltage = float(nameparts[endoffset].split(".")[0]) / 100.0
    except (ValueError, TypeError, IndexError):
      try:
        data.rollup = timespec.parse_timespan(nameparts[endoffset].split(".")[0])
        endoffset -= 1
      except ValueError:
        data.rollup = 0
      try:
        data.voltage = float(nameparts[endoffset].split(".")[0]) / 100.0
      except (ValueError, TypeError, IndexError):
        data.voltage = 0.0
    endoffset -= 1
    try:
      data.samples = int(nameparts[endoffset])
    except (ValueError, TypeError, IndexError):
      data.samples = 0

    for part in nameparts[2:endoffset]:
      mo = _STATE_RE.match(part)
      if mo:
        data[mo.group(1)] = _STATEMAP[mo.group(2)]
      else:
        mo = _DATA_RE.match(part)
        if mo:
          data[mo.group(1)] = mo.group(2)
        else:
          print >>sys.stderr, "Warning: name component not matched: %r" % (part,)

  return data
Example #8
0
 def set_timestamp(self, time=None):
     if time is None:
         time = timelib.now()
     self["timestamp"] = timelib.localtime_mutable(time)