def update(self): checks = UpdateChecks() config = Settings.Config() if checks.GetState('error') is True: checks.SetFalse('error') #self.Source.update() try: #pass self.Source.update() except Exception as e: Warn("Source update failed: " + str(e)) color = (148, 14, 14) static = RowStatic() self.ErrorScreen = static.HeaderFont.render("Error: Failed to retrieve update!", True, color).convert_alpha() checks.SetTrue('error') exit() ## This makes debugging easier #raise e if checks.GetState('dirty_source') is True: checks.SetFalse('dirty_source') self.List.clear() lst = self.Source.Arrivals if config.DisplayScreen == "departures": lst = self.Source.Departures for d in lst: self.List.append(BusRow(d)) if len(self.List) <= self.RowsPerPage: checks.SetTrue('dirty_screen') self.CachedList = self.List Debug("Source updated with one screen of buses, scheduling screen update. (" + str(len(self.CachedList)) + ":" + str(self.RowsPerPage) + ")") if len(self.List) > self.RowsPerPage: if checks.TimeToUpdate('page_delay') is True: # It's time to paginate checks.Update('page_delay') checks.SetTrue('dirty_screen') self.FirstRow = self.CurrentPage * self.RowsPerPage self.CurrentPage += 1 if len(self.CachedList) / self.RowsPerPage < self.CurrentPage: Debug("Updating CachedList") self.CurrentPage = 0 #self.FirstRow = 0 self.CachedList = self.List Debug("CurrentPage: " + str(self.CurrentPage) + "; FirstRow: " + str(self.FirstRow) + "; len(CachedList): " + str(len(self.CachedList))) if len(self.CachedList) == 0: Debug("CachedList is empty. Updating.") self.CachedList = self.List self.CurrentPage = 0 checks.SetTrue('dirty_screen') if (self.FirstRow / self.RowsPerPage > len(self.CachedList) / self.RowsPerPage): Debug("Past the last page. Resetting.") self.FirstRow = 0 self.CurrentPage = 0
def ParseSource(self): Debug('Entering JSONSource.ParseSource()') jfile = open(self.LocalSource, 'r') try: Debug('Attempting to load jfile...') json_dict = json.load(jfile) except Exception as ex: Error("Unable to load jfile.\n" + str(jfile)) exit() jfile.close() self.Arrivals.clear() self.Departures.clear() for arrival in json_dict['arrivals']: self.Arrivals.append( Data.BusArrival( arrival['company'], arrival['city'], arrival['time'], arrival['status'] ) ) for departure in json_dict['departures']: self.Departures.append( Data.BusDeparture( departure['company'], departure['city'], departure['time'], departure['status'], departure['gate'], departure['busnum'] ) )
def AddTimer(self, id, interval=None): if interval is None: self.TimerDict[id] = TimerEvent(id) Debug("Adding timer '" + str(id) + "' with default interval") else: self.TimerDict[id] = TimerEvent(id, interval) Debug("Adding timer '" + str(id) + "' with interval " + str(interval))
def AddState(self, id, initial=None): if initial is None: self.StateDict[id] = False Debug("Adding state '" + str(id) + "' with default value of False") else: self.StateDict[id] = initial Debug("Adding stat '" + str(id) + "' with value " + str(initial))
def __init__(self, company, city, time, status): if company == "Company": Debug("Setting data for header") self.Company = company self.City = city ## Destination/Origin self.Time = time ## Departure/Arrival time self.Status = status
def SetFalse(self, id): if id in self.StateDict.keys(): self.StateDict[id] = False Debug("Setting state with ID '" + str(id) + "' to False") else: raise KeyError("State with ID '" + str(id) + "' does not exist")
def __init__(self, width = 1280, height = 720): config = Settings.Config() if config.DisplayFullscreen is False: os.environ['SDL_VIDEO_CENTERED'] = '1' self.screen = pygame.display.set_mode((width, height)) else: self.screen = pygame.display.set_mode((width, height), pygame.FULLSCREEN | pygame.HWSURFACE | pygame.DOUBLEBUF) pygame.init() self.Background = TileImage("img/bg-dark.gif", self.screen) self.screen.blit(self.Background, (0,0)) pygame.mouse.set_visible(False) pygame.display.flip() checks = UpdateChecks() checks.AddTimer('page_delay', config.PageInterval) checks.AddTimer('source_update', config.UpdateInterval) checks.Update('page_delay') checks.AddState('dirty_source', True) checks.AddState('dirty_screen', True) checks.AddState('dirty_page', False) checks.AddState('error', False) self.DrawArrivals = True if config.DisplayScreen == 'departures': self.DrawArrivals = False self.CurrentPage = 1 self.RowsPerPage = 10 # 12 rows minus the header self.FirstRow = 0 # First row to draw # FIXME: proper error handling and checking needed here. if config.SourceURI[-4:] == '.xml': self.Source = ds.JSONSource() Debug('XML source found.') else: self.Source = ds.XMLSource() Debug('JSON source found.') self.CachedList = Data.BusList() self.List = Data.BusList() self.ArrivalHeader = HeaderRow(Data.BusArrival("Company", "City", "Time", "Status")) self.DepartureHeader = HeaderRow(Data.BusDeparture("Company", "City", "Time", "Status", "Gate", "Bus #")) self.ErrorScreen = None
def SetUpdateInterval(self, id, interval): if id in self.TimerDict.keys(): self.TimerDict[id].UpdateInterval = interval Debug("Updating update interval of TimerEvent '" + str(id) + "' to " + str(interval)) else: raise KeyError("TimeEvent with ID '" + str(id) + "' does not exist")
def LoadImage(path): """ Return a surface containing the image at the given path. """ surf = None Debug("Loading image at path '" + str(path) + "'") #path = FixPath(path) try: surf = pygame.Surface.convert_alpha(pygame.image.load(path)) except Exception as exc: Warn("Unable to load image at path '" + path + "': " + str(exc)) return surf
def update(self): checks = UpdateChecks() if checks.TimeToUpdate('source_update'): Debug('Updating source') dlfile = urllib.URLopener() try: dlfile.retrieve(self.RemoteSource, self.LocalSource) except Exception as e: Warn("DL of '" + self.RemoteSource + "' failed: " + str(e)) self.ParseSource() ## This is just because I'm lazy... #json_string = json.dumps( {"arrivals": self.Arrivals.get_dict(), "departures": self.Departures.get_dict()} ) #print "json > " + json_string + " <" #exit(1) checks.Update('source_update') checks.SetTrue('dirty_source')
def draw(self, screen, current_page, total_pages): #print "page " + str(current_page + 1) + " of " + str(total_pages + 1) titletext = "Arrivals" if self.Data.is_departure() is True: titletext = "Departures" timestruct = time.localtime() months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] days = "Sun Mon Tues Wed Thurs Fri Sat".split(" ") ampm = 'am' hour_12 = timestruct.tm_hour if hour_12 > 12: ampm = 'pm' hour_12 -= 12 elif hour_12 == 0: hour_12 = 12 minute = timestruct.tm_min if minute < 10: minute = '0' + str(minute) Debug("{Header} current_page: " + str(current_page) + "; total_pages: " + str(total_pages)) pagestring = "Page " + str(current_page + 1) + " of " + str(total_pages + 1) timestring = days[timestruct.tm_wday] + ", " + months[timestruct.tm_mon-1] + " " + str(timestruct.tm_mday) + ", " + str(hour_12) + ':' + str(minute) + ampm static = RowStatic() BusRow.draw(self, self.image, -1) midoffset = self.image.get_height() / 4 rendered_title = self.font.render(titletext, True, self.fontcolor).convert_alpha() rendered_time = self.timefont.render(timestring, True, self.fontcolor).convert_alpha() rendered_page = self.timefont.render(pagestring, True, self.fontcolor).convert_alpha() screen.blit(self.image, (screen.get_width()/2 - self.image.get_width()/2, 0)) screen.blit(rendered_title, ((self.image.get_width()/2) - (rendered_title.get_width()/2), (midoffset - rendered_title.get_height()/2))) screen.blit(rendered_time, (20, (midoffset - rendered_time.get_height()/2))) screen.blit(rendered_page, (screen.get_width() - rendered_page.get_width() - 20, (midoffset - rendered_time.get_height()/2)))
def draw(self): checks = UpdateChecks() config = Settings.Config() if checks.GetState('error') is True: self.screen.blit(self.Background, (0,0)) self.screen.blit(self.ErrorScreen, ((self.screen.get_width() / 2) - (self.ErrorScreen.get_width()/2), (self.screen.get_height() / 2) - (self.ErrorScreen.get_height()/2))) return if checks.GetState('dirty_screen') is True: self.screen.blit(self.Background, (0,0)) total_pages = len(self.CachedList) / self.RowsPerPage if len(self.CachedList) == self.RowsPerPage: total_pages = 0 if config.DisplayScreen == "arrivals": self.ArrivalHeader.draw(self.screen, (self.FirstRow / self.RowsPerPage), total_pages) else: self.DepartureHeader.draw(self.screen, (self.FirstRow / self.RowsPerPage), total_pages) if len(self.CachedList) == 0: Debug("Empty Cached List! Drawing error message.") static = RowStatic() emptymsg = static.HeaderFont.render("No Buses!", True, static.HeaderFontColor) self.screen.blit(emptymsg, ((self.screen.get_width()/2)-(emptymsg.get_width()/2),(self.screen.get_height()/2)-(emptymsg.get_height()/2))) else: row = 2 x = 0 for b in self.CachedList[self.FirstRow:(self.FirstRow + self.RowsPerPage)]: x += 1 b.draw(self.screen, row) row += 1 if checks.GetState('dirty_screen') is True: checks.SetFalse('dirty_screen') pygame.display.flip()
def __init__(self): self.TimerDict = {} self.StateDict = {} Debug('UpdateChecks() singleton init\'d')
def update(self): Debug("Updated '" + str(self.ID) + "'") self.LastUpdate = time.time()
def time_to_update(self): if time.time() - self.LastUpdate > self.UpdateInterval: Debug("It's time to update '" + str(self.ID) + "'") return True return False
def __init__(self): BaseSource.__init__(self) Debug('JSONSource init\'d')