def CheckFixedBlock(ws, params, status, logger): fixed_block = ws.get_fixed_block(unbuffered=True) # check clocks try: s_time = DataStore.safestrptime( fixed_block['date_time'], '%Y-%m-%d %H:%M') except Exception: s_time = None if s_time: c_time = datetime.now().replace(second=0, microsecond=0) diff = abs(s_time - c_time) if diff > timedelta(minutes=2): logger.warning( "Computer and weather station clocks disagree by %s (H:M:S).", str(diff)) # store weather station type params.set('config', 'ws type', ws.ws_type) # store info from fixed block pressure_offset = fixed_block['rel_pressure'] - fixed_block['abs_pressure'] old_offset = eval(status.get('fixed', 'pressure offset', 'None')) if old_offset and abs(old_offset - pressure_offset) > 0.01: # re-read fixed block, as can get incorrect values logger.warning('Re-read fixed block') fixed_block = ws.get_fixed_block(unbuffered=True) if not fixed_block: return None pressure_offset = fixed_block['rel_pressure'] - fixed_block['abs_pressure'] if old_offset and abs(old_offset - pressure_offset) > 0.01: logger.warning( 'Pressure offset change: %g -> %g', old_offset, pressure_offset) params.unset('fixed', 'pressure offset') params.unset('fixed', 'fixed block') status.set('fixed', 'pressure offset', '%g' % (pressure_offset)) status.set('fixed', 'fixed block', str(fixed_block)) return fixed_block
def check_fixed_block(self): fixed_block = self.ws.get_fixed_block(unbuffered=True) # check clocks try: s_time = DataStore.safestrptime( fixed_block['date_time'], '%Y-%m-%d %H:%M') except Exception: s_time = None if s_time: c_time = datetime.now().replace(second=0, microsecond=0) diff = abs(s_time - c_time) if diff > timedelta(minutes=2): self.logger.warning( "Computer and weather station clocks disagree by %s (H:M:S).", str(diff)) # store weather station type self.params.set('config', 'ws type', self.ws.ws_type) # store info from fixed block self.status.unset('fixed', 'pressure offset') if not self.params.get('config', 'pressure offset'): self.params.set('config', 'pressure offset', '%g' % ( fixed_block['rel_pressure'] - fixed_block['abs_pressure'])) self.params.unset('fixed', 'fixed block') self.status.set('fixed', 'fixed block', str(fixed_block)) return fixed_block
def process(self, live_data, template_file): def jump(idx, count): while count > 0: new_idx = data_set.after(idx + SECOND) if new_idx == None: break idx = new_idx count -= 1 while count < 0: new_idx = data_set.before(idx) if new_idx == None: break idx = new_idx count += 1 return idx, count == 0 params = self.params if not live_data: idx = self.calib_data.before(datetime.max) if not idx: self.logger.error("No calib data - run Process.py first") return live_data = self.calib_data[idx] pressure_trend_text = WeatherStation.pressure_trend_text wind_dir_text = WeatherStation.get_wind_dir_text() dew_point = WeatherStation.dew_point wind_chill = WeatherStation.wind_chill apparent_temp = WeatherStation.apparent_temp rain_hour = self._rain_hour rain_day = self._rain_day pressure_offset = eval(self.params.get('fixed', 'pressure offset')) fixed_block = eval(self.params.get('fixed', 'fixed block')) # start off with no time rounding round_time = None # start off in hourly data mode data_set = self.hourly_data # start off in utc time_zone = utc # jump to last item idx, valid_data = jump(datetime.max, -1) if not valid_data: self.logger.error("No summary data - run Process.py first") return data = data_set[idx] # open template file file if sys.version_info[0] >= 3: tmplt = open(template_file, 'r', encoding=self.encoding) else: tmplt = open(template_file, 'r') # do the text processing while True: line = tmplt.readline() if line == '': break parts = line.split('#') for i in range(len(parts)): if i % 2 == 0: # not a processing directive if i == 0 or parts[i] != '\n': yield parts[i] continue if parts[i] and parts[i][0] == '!': # comment continue command = shlex.split(parts[i]) if command == []: # empty command == print a single '#' yield '#' elif command[0] in data.keys() + ['calc']: # output a value if not valid_data: continue # format is: key fmt_string no_value_string conversion # get value if command[0] == 'calc': x = eval(command[1]) del command[1] else: x = data[command[0]] # adjust time if isinstance(x, datetime): if round_time: x += round_time x = x.replace(tzinfo=utc) x = x.astimezone(time_zone) # convert data if x != None and len(command) > 3: x = eval(command[3]) # get format fmt = '%s' if len(command) > 1: fmt = command[1] # write output if x == None: if len(command) > 2: yield command[2] elif isinstance(x, datetime): yield x.strftime(fmt) elif not self.use_locale: yield fmt % (x) elif sys.version_info >= (2, 7) or '%%' not in fmt: yield locale.format_string(fmt, x) else: yield locale.format_string( fmt.replace('%%', '##'), x).replace('##', '%') elif command[0] == 'monthly': data_set = self.monthly_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'daily': data_set = self.daily_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'hourly': data_set = self.hourly_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'raw': data_set = self.calib_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'live': data_set = self.calib_data idx = datetime.max valid_data = True data = live_data elif command[0] == 'timezone': if command[1] == 'utc': time_zone = utc elif command[1] == 'local': time_zone = Local else: self.logger.error("Unknown time zone: %s", command[1]) return elif command[0] == 'roundtime': if eval(command[1]): round_time = timedelta(seconds=30) else: round_time = None elif command[0] == 'jump': prevdata = data idx, valid_data = jump(idx, int(command[1])) data = data_set[idx] elif command[0] == 'goto': prevdata = data time_str = command[1] if '%' in time_str: lcl = idx.replace(tzinfo=utc).astimezone(time_zone) time_str = lcl.strftime(time_str) new_idx = DataStore.safestrptime(time_str) new_idx = new_idx.replace(tzinfo=time_zone).astimezone(utc) new_idx = data_set.after(new_idx.replace(tzinfo=None)) if new_idx: idx = new_idx data = data_set[idx] valid_data = True else: valid_data = False elif command[0] == 'loop': loop_count = int(command[1]) loop_start = tmplt.tell() elif command[0] == 'endloop': loop_count -= 1 if valid_data and loop_count > 0: tmplt.seek(loop_start, 0) else: self.logger.error( "Unknown processing directive: #%s#", parts[i]) return tmplt.close() return
in_name = args[0] out_name = args[1] # open input in_file = open(in_name, 'r') # open data file store ds = DataStore.data_store(out_name) # get time to go forward to first_stored = ds.after(datetime.min) if first_stored == None: first_stored = datetime.max # copy any missing data last_date = None count = 0 for line in in_file: items = line.split(',') local_date = DataStore.safestrptime(items[2].strip(), '%Y-%m-%d %H:%M:%S') local_date = local_date.replace(tzinfo=TimeZone.Local) date = local_date.astimezone(TimeZone.utc) if last_date and date < last_date: date = date + timedelta(hours=1) print "Corrected DST ambiguity %s %s -> %s" % ( local_date, local_date.tzname(), date) last_date = date date = date.replace(tzinfo=None) # get data data = {} data['delay'] = int(items[3]) data['hum_in'] = int(items[4]) data['temp_in'] = float(items[5]) try: data['hum_out'] = int(items[6])
in_name = args[0] out_name = args[1] # open input in_file = open(in_name, 'r') # open data file store ds = DataStore.data_store(out_name) # get time to go forward to first_stored = ds.after(datetime.min) if first_stored == None: first_stored = datetime.max # copy any missing data last_date = None count = 0 for line in in_file: items = line.split(',') local_date = DataStore.safestrptime(items[2].strip(), '%Y-%m-%d %H:%M:%S') local_date = local_date.replace(tzinfo=TimeZone.Local) date = local_date.astimezone(TimeZone.utc) if last_date and date < last_date: date = date + timedelta(hours=1) print "Corrected DST ambiguity %s %s -> %s" % ( local_date, local_date.tzname(), date) last_date = date date = date.replace(tzinfo=None) # get data data = {} data['delay'] = int(items[3]) data['hum_in'] = int(items[4]) data['temp_in'] = float(items[5]) try: data['hum_out'] = int(items[6])
def process(self, live_data, template_file): def jump(idx, count): while count > 0: new_idx = data_set.after(idx + SECOND) if new_idx == None: break idx = new_idx count -= 1 while count < 0: new_idx = data_set.before(idx) if new_idx == None: break idx = new_idx count += 1 return idx, count == 0 params = self.params if not live_data: idx = self.calib_data.before(datetime.max) if not idx: self.logger.error("No calib data - run Process.py first") return live_data = self.calib_data[idx] pressure_trend_text = WeatherStation.pressure_trend_text wind_dir_text = WeatherStation.get_wind_dir_text() dew_point = WeatherStation.dew_point wind_chill = WeatherStation.wind_chill apparent_temp = WeatherStation.apparent_temp rain_hour = self._rain_hour rain_day = self._rain_day pressure_offset = eval(self.params.get('fixed', 'pressure offset')) fixed_block = eval(self.params.get('fixed', 'fixed block')) # start off with no time rounding round_time = None # start off in hourly data mode data_set = self.hourly_data # start off in utc time_zone = utc # jump to last item idx, valid_data = jump(datetime.max, -1) if not valid_data: self.logger.error("No summary data - run Process.py first") return data = data_set[idx] # open template file file if sys.version_info[0] >= 3: tmplt = open(template_file, 'r', encoding=self.encoding) else: tmplt = open(template_file, 'r') # do the text processing while True: line = tmplt.readline() if line == '': break parts = line.split('#') for i in range(len(parts)): if i % 2 == 0: # not a processing directive if i == 0 or parts[i] != '\n': yield parts[i] continue if parts[i] and parts[i][0] == '!': # comment continue command = shlex.split(parts[i]) if command == []: # empty command == print a single '#' yield '#' elif command[0] in data.keys() + ['calc']: # output a value if not valid_data: continue # format is: key fmt_string no_value_string conversion # get value if command[0] == 'calc': x = eval(command[1]) del command[1] else: x = data[command[0]] # adjust time if isinstance(x, datetime): if round_time: x += round_time x = x.replace(tzinfo=utc) x = x.astimezone(time_zone) # convert data if x != None and len(command) > 3: x = eval(command[3]) # get format fmt = '%s' if len(command) > 1: fmt = command[1] # write output if x == None: if len(command) > 2: yield command[2] elif isinstance(x, datetime): yield x.strftime(fmt) elif not self.use_locale: yield fmt % (x) elif sys.version_info >= (2, 7) or '%%' not in fmt: yield locale.format_string(fmt, x) else: yield locale.format_string(fmt.replace('%%', '##'), x).replace('##', '%') elif command[0] == 'monthly': data_set = self.monthly_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'daily': data_set = self.daily_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'hourly': data_set = self.hourly_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'raw': data_set = self.calib_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'live': data_set = self.calib_data idx = datetime.max valid_data = True data = live_data elif command[0] == 'timezone': if command[1] == 'utc': time_zone = utc elif command[1] == 'local': time_zone = Local else: self.logger.error("Unknown time zone: %s", command[1]) return elif command[0] == 'roundtime': if eval(command[1]): round_time = timedelta(seconds=30) else: round_time = None elif command[0] == 'jump': prevdata = data idx, valid_data = jump(idx, int(command[1])) data = data_set[idx] elif command[0] == 'goto': prevdata = data time_str = command[1] if '%' in time_str: lcl = idx.replace(tzinfo=utc).astimezone(time_zone) time_str = lcl.strftime(time_str) new_idx = DataStore.safestrptime(time_str) new_idx = new_idx.replace(tzinfo=time_zone).astimezone(utc) new_idx = data_set.after(new_idx.replace(tzinfo=None)) if new_idx: idx = new_idx data = data_set[idx] valid_data = True else: valid_data = False elif command[0] == 'loop': loop_count = int(command[1]) loop_start = tmplt.tell() elif command[0] == 'endloop': loop_count -= 1 if valid_data and loop_count > 0: tmplt.seek(loop_start, 0) else: self.logger.error("Unknown processing directive: #%s#", parts[i]) return tmplt.close() return
def process(self, live_data, template_file): def jump(idx, count): while count > 0: new_idx = data_set.after(idx + SECOND) if new_idx == None: break idx = new_idx count -= 1 while count < 0: new_idx = data_set.before(idx) if new_idx == None: break idx = new_idx count += 1 return idx, count == 0 params = self.params if not live_data: idx = self.calib_data.before(datetime.max) if not idx: self.logger.error("No calib data - run pywws.Process first") return live_data = self.calib_data[idx] # get default character encoding of template input & output files self.encoding = params.get('config', 'template encoding', 'iso-8859-1') file_encoding = self.encoding if file_encoding == 'html': file_encoding = 'ascii' # get conversions module to create its 'private' wind dir text # array, then copy it to deprecated wind_dir_text variable winddir_text(0) wind_dir_text = conversions._winddir_text_array hour_diff = self._hour_diff rain_hour = self._rain_hour rain_day = self._rain_day pressure_offset = eval(self.params.get('config', 'pressure offset')) fixed_block = eval(self.status.get('fixed', 'fixed block')) # start off with no time rounding round_time = None # start off in hourly data mode data_set = self.hourly_data # start off in utc time_zone = utc # start off with default use_locale setting use_locale = self.use_locale # jump to last item idx, valid_data = jump(datetime.max, -1) if not valid_data: self.logger.error("No summary data - run pywws.Process first") return data = data_set[idx] # open template file, if not already a file(like) object if hasattr(template_file, 'readline'): tmplt = template_file else: tmplt = open(template_file, 'rb') # do the text processing while True: line = tmplt.readline().decode(file_encoding) if not line: break parts = line.split('#') for i in range(len(parts)): if i % 2 == 0: # not a processing directive if i == 0 or parts[i] != '\n': yield parts[i] continue if parts[i] and parts[i][0] == '!': # comment continue # Python 2 shlex can't handle unicode if sys.version_info[0] < 3: parts[i] = parts[i].encode(file_encoding) command = shlex.split(parts[i]) if sys.version_info[0] < 3: command = map(lambda x: x.decode(file_encoding), command) if command == []: # empty command == print a single '#' yield u'#' elif command[0] in data.keys() + ['calc']: # output a value if not valid_data: continue # format is: key fmt_string no_value_string conversion # get value if command[0] == 'calc': x = eval(command[1]) del command[1] else: x = data[command[0]] # adjust time if isinstance(x, datetime): if round_time: x += round_time x = x.replace(tzinfo=utc) x = x.astimezone(time_zone) # convert data if x is not None and len(command) > 3: x = eval(command[3]) # get format fmt = u'%s' if len(command) > 1: fmt = command[1] # write output if x is None: if len(command) > 2: yield command[2] elif isinstance(x, datetime): if sys.version_info[0] < 3: fmt = fmt.encode(file_encoding) x = x.strftime(fmt) if sys.version_info[0] < 3: if self.encoding == 'html': x = x.decode('ascii', errors='xmlcharrefreplace') else: x = x.decode(file_encoding) yield x elif not use_locale: yield fmt % (x) elif sys.version_info >= (2, 7) or '%%' not in fmt: yield locale.format_string(fmt, x) else: yield locale.format_string(fmt.replace('%%', '##'), x).replace('##', '%') elif command[0] == 'monthly': data_set = self.monthly_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'daily': data_set = self.daily_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'hourly': data_set = self.hourly_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'raw': data_set = self.calib_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'live': data_set = self.calib_data idx = datetime.max valid_data = True data = live_data elif command[0] == 'timezone': if command[1] == 'utc': time_zone = utc elif command[1] == 'local': time_zone = Local else: self.logger.error("Unknown time zone: %s", command[1]) return elif command[0] == 'locale': use_locale = eval(command[1]) elif command[0] == 'encoding': self.encoding = command[1] file_encoding = self.encoding if file_encoding == 'html': file_encoding = 'ascii' elif command[0] == 'roundtime': if eval(command[1]): round_time = timedelta(seconds=30) else: round_time = None elif command[0] == 'jump': prevdata = data idx, valid_data = jump(idx, int(command[1])) data = data_set[idx] elif command[0] == 'goto': prevdata = data time_str = command[1] if '%' in time_str: lcl = idx.replace(tzinfo=utc).astimezone(time_zone) time_str = lcl.strftime(time_str) new_idx = DataStore.safestrptime(time_str) new_idx = new_idx.replace(tzinfo=time_zone).astimezone(utc) new_idx = data_set.after(new_idx.replace(tzinfo=None)) if new_idx: idx = new_idx data = data_set[idx] valid_data = True else: valid_data = False elif command[0] == 'loop': loop_count = int(command[1]) loop_start = tmplt.tell() elif command[0] == 'endloop': loop_count -= 1 if valid_data and loop_count > 0: tmplt.seek(loop_start, 0) else: self.logger.error("Unknown processing directive: #%s#", parts[i]) return
def catchup(self, last_date, last_ptr): fixed_block = self.ws.get_fixed_block(unbuffered=True) # get time to go back to last_stored = self.raw_data.before(datetime.max) if not last_stored: last_stored = datetime.min if self.status.get('data', 'ptr'): saved_ptr, saved_date = self.status.get('data', 'ptr').split(',') saved_ptr = int(saved_ptr, 16) saved_date = DataStore.safestrptime(saved_date) saved_date = self.raw_data.nearest(saved_date) while saved_date < last_stored: saved_date = self.raw_data.after(saved_date + SECOND) saved_ptr = self.ws.inc_ptr(saved_ptr) else: saved_ptr = None saved_date = None last_stored += timedelta(seconds=fixed_block['read_period'] * 30) if last_date <= last_stored: # nothing to do return self.status.set( 'data', 'ptr', '%06x,%s' % (last_ptr, last_date.isoformat(' '))) # data_count includes record currently being updated every 48 seconds max_count = fixed_block['data_count'] - 1 count = 0 duplicates = [] while last_date > last_stored and count < max_count: data = self.ws.get_data(last_ptr) if last_ptr == saved_ptr: if any(data[key] != self.raw_data[saved_date][key] for key in ( 'hum_in', 'temp_in', 'hum_out', 'temp_out', 'abs_pressure', 'wind_ave', 'wind_gust', 'wind_dir', 'rain', 'status')): # pointer matches but data is different, so no duplicates duplicates = None saved_ptr = None saved_date = None else: # potential duplicate data duplicates.append(last_date) saved_date = self.raw_data.before(saved_date) saved_ptr = self.ws.dec_ptr(saved_ptr) if (data['delay'] is None or data['delay'] > max(fixed_block['read_period'] * 2, 35)): self.logger.error('invalid data at %04x, %s', last_ptr, last_date.isoformat(' ')) last_date -= timedelta(minutes=fixed_block['read_period']) else: self.raw_data[last_date] = data count += 1 last_date -= timedelta(minutes=data['delay']) last_ptr = self.ws.dec_ptr(last_ptr) if duplicates: for d in duplicates: del self.raw_data[d] count -= len(duplicates) last_date = self.raw_data.nearest(last_date) next_date = self.raw_data.after(last_date + SECOND) if next_date: gap = (next_date - last_date).seconds // 60 gap -= fixed_block['read_period'] if gap > 0: self.logger.critical("%d minutes gap in data detected", gap) self.logger.info("%d catchup records", count)
def catchup(self, last_date, last_ptr): fixed_block = self.ws.get_fixed_block(unbuffered=True) # get time to go back to last_stored = self.raw_data.before(datetime.max) if not last_stored: last_stored = datetime.min if self.status.get('data', 'ptr'): saved_ptr, saved_date = self.status.get('data', 'ptr').split(',') saved_ptr = int(saved_ptr, 16) saved_date = DataStore.safestrptime(saved_date) saved_date = self.raw_data.nearest(saved_date) while saved_date < last_stored: saved_date = self.raw_data.after(saved_date + SECOND) saved_ptr = self.ws.inc_ptr(saved_ptr) else: saved_ptr = None saved_date = None last_stored += timedelta(seconds=fixed_block['read_period'] * 30) if last_date <= last_stored: # nothing to do return self.status.set('data', 'ptr', '%06x,%s' % (last_ptr, last_date.isoformat(' '))) # data_count includes record currently being updated every 48 seconds max_count = fixed_block['data_count'] - 1 count = 0 duplicates = [] while last_date > last_stored and count < max_count: data = self.ws.get_data(last_ptr) if last_ptr == saved_ptr: if any(data[key] != self.raw_data[saved_date][key] for key in ('hum_in', 'temp_in', 'hum_out', 'temp_out', 'abs_pressure', 'wind_ave', 'wind_gust', 'wind_dir', 'rain', 'status')): # pointer matches but data is different, so no duplicates duplicates = None saved_ptr = None saved_date = None else: # potential duplicate data duplicates.append(last_date) saved_date = self.raw_data.before(saved_date) saved_ptr = self.ws.dec_ptr(saved_ptr) if (data['delay'] is None or data['delay'] > max(fixed_block['read_period'] * 2, 35)): self.logger.error('invalid data at %04x, %s', last_ptr, last_date.isoformat(' ')) last_date -= timedelta(minutes=fixed_block['read_period']) else: self.raw_data[last_date] = data count += 1 last_date -= timedelta(minutes=data['delay']) last_ptr = self.ws.dec_ptr(last_ptr) if duplicates: for d in duplicates: del self.raw_data[d] count -= len(duplicates) last_date = self.raw_data.nearest(last_date) next_date = self.raw_data.after(last_date + SECOND) if next_date: gap = (next_date - last_date).seconds // 60 gap -= fixed_block['read_period'] if gap > 0: self.logger.critical("%d minutes gap in data detected", gap) self.logger.info("%d catchup records", count)
def process(self, live_data, template_file): def jump(idx, count): while count > 0: new_idx = data_set.after(idx + SECOND) if new_idx == None: break idx = new_idx count -= 1 while count < 0: new_idx = data_set.before(idx) if new_idx == None: break idx = new_idx count += 1 return idx, count == 0 params = self.params if not live_data: idx = self.calib_data.before(datetime.max) if not idx: self.logger.error("No calib data - run pywws.Process first") return live_data = self.calib_data[idx] # get default character encoding of template input & output files self.encoding = params.get('config', 'template encoding', 'iso-8859-1') file_encoding = self.encoding if file_encoding == 'html': file_encoding = 'ascii' # get conversions module to create its 'private' wind dir text # array, then copy it to deprecated wind_dir_text variable winddir_text(0) wind_dir_text = conversions._winddir_text_array hour_diff = self._hour_diff rain_hour = self._rain_hour rain_day = self._rain_day pressure_offset = eval(self.params.get('config', 'pressure offset')) fixed_block = eval(self.status.get('fixed', 'fixed block')) # start off with no time rounding round_time = None # start off in hourly data mode data_set = self.hourly_data # start off in utc time_zone = utc # start off with default use_locale setting use_locale = self.use_locale # jump to last item idx, valid_data = jump(datetime.max, -1) if not valid_data: self.logger.error("No summary data - run pywws.Process first") return data = data_set[idx] # open template file, if not already a file(like) object if hasattr(template_file, 'readline'): tmplt = template_file else: tmplt = open(template_file, 'rb') # do the text processing while True: line = tmplt.readline().decode(file_encoding) if not line: break parts = line.split('#') for i in range(len(parts)): if i % 2 == 0: # not a processing directive if i == 0 or parts[i] != '\n': yield parts[i] continue if parts[i] and parts[i][0] == '!': # comment continue # Python 2 shlex can't handle unicode if sys.version_info[0] < 3: parts[i] = parts[i].encode(file_encoding) command = shlex.split(parts[i]) if sys.version_info[0] < 3: command = map(lambda x: x.decode(file_encoding), command) if command == []: # empty command == print a single '#' yield u'#' elif command[0] in data.keys() + ['calc']: # output a value if not valid_data: continue # format is: key fmt_string no_value_string conversion # get value if command[0] == 'calc': x = eval(command[1]) del command[1] else: x = data[command[0]] # adjust time if isinstance(x, datetime): if round_time: x += round_time x = x.replace(tzinfo=utc) x = x.astimezone(time_zone) # convert data if x is not None and len(command) > 3: x = eval(command[3]) # get format fmt = u'%s' if len(command) > 1: fmt = command[1] # write output if x is None: if len(command) > 2: yield command[2] elif isinstance(x, datetime): if sys.version_info[0] < 3: fmt = fmt.encode(file_encoding) x = x.strftime(fmt) if sys.version_info[0] < 3: x = x.decode(file_encoding) yield x elif not use_locale: yield fmt % (x) elif sys.version_info >= (2, 7) or '%%' not in fmt: yield locale.format_string(fmt, x) else: yield locale.format_string( fmt.replace('%%', '##'), x).replace('##', '%') elif command[0] == 'monthly': data_set = self.monthly_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'daily': data_set = self.daily_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'hourly': data_set = self.hourly_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'raw': data_set = self.calib_data idx, valid_data = jump(datetime.max, -1) data = data_set[idx] elif command[0] == 'live': data_set = self.calib_data idx = datetime.max valid_data = True data = live_data elif command[0] == 'timezone': if command[1] == 'utc': time_zone = utc elif command[1] == 'local': time_zone = Local else: self.logger.error("Unknown time zone: %s", command[1]) return elif command[0] == 'locale': use_locale = eval(command[1]) elif command[0] == 'encoding': self.encoding = command[1] file_encoding = self.encoding if file_encoding == 'html': file_encoding = 'ascii' elif command[0] == 'roundtime': if eval(command[1]): round_time = timedelta(seconds=30) else: round_time = None elif command[0] == 'jump': prevdata = data idx, valid_data = jump(idx, int(command[1])) data = data_set[idx] elif command[0] == 'goto': prevdata = data time_str = command[1] if '%' in time_str: lcl = idx.replace(tzinfo=utc).astimezone(time_zone) time_str = lcl.strftime(time_str) new_idx = DataStore.safestrptime(time_str) new_idx = new_idx.replace(tzinfo=time_zone).astimezone(utc) new_idx = data_set.after(new_idx.replace(tzinfo=None)) if new_idx: idx = new_idx data = data_set[idx] valid_data = True else: valid_data = False elif command[0] == 'loop': loop_count = int(command[1]) loop_start = tmplt.tell() elif command[0] == 'endloop': loop_count -= 1 if valid_data and loop_count > 0: tmplt.seek(loop_start, 0) else: self.logger.error( "Unknown processing directive: #%s#", parts[i]) return