def create_book_genre_items(self, genres, base_url): """ Create dictionary with genres :param genres: list of genres :param base_url: base url :return: dictionary with genres """ items = {} for i, g in enumerate(genres): state = State() state.name = g[0] state.genre = base_url + g[1] state.l_name = state.name state.bgr = self.config[COLORS][COLOR_DARK] state.img_x = None state.img_y = None state.auto_update = True state.show_bgr = True state.show_img = False state.show_label = True state.comparator_item = state.name state.index = i items[state.name] = state return items
def create_station_button(self, s, bb, action=None): """ Create station button :param s: button state :param bb: bounding box :param action: event listener :return: station logo button """ state = State() state.icon_base = s.icon_base state.index_in_page = s.index_in_page state.index = s.index state.genre = s.genre state.scaled = getattr(s, "scaled", False) state.icon_base_scaled = s.icon_base_scaled state.name = "station_menu." + s.name state.l_name = s.l_name state.url = s.url state.keyboard_key = kbd_keys[KEY_SELECT] state.bounding_box = bb state.img_x = bb.x state.img_y = bb.y state.auto_update = False state.show_bgr = True state.show_img = True state.image_align_v = V_ALIGN_BOTTOM button = Button(self.util, state) button.add_release_listener(action) return button
def load_m3u(self, path, folder, top_folder, items_per_page, default_icon_path): """ Load m3u playlist :param path: base path :param folder: main folder :param top_folder: top folder :param items_per_page: items per page :param default_icon_path: path to the default icon :return: list of State objects representing playlist """ items = [] lines = [] item_name = None index = 0 for encoding in ["utf8", "utf-8-sig", "utf-16"]: try: lines = codecs.open(path, "r", encoding).read().split("\n") break except Exception as e: logging.error(e) for line in lines: if len(line.rstrip()) == 0: continue if line.startswith("#") and not item_name: item_name = line[1:].rstrip() continue name = item_name.rstrip() path = os.path.join(folder, name + EXT_PNG) icon = self.image_util.load_image(path) if not icon: path = os.path.join(folder, name + EXT_JPG) icon = self.image_util.load_image(path) if not icon: icon = self.image_util.load_image(default_icon_path) state = State() state.index = index state.genre = top_folder state.url = line.rstrip() state.name = str(index) state.l_name = name state.icon_base = icon state.comparator_item = NAME state.index_in_page = index % items_per_page items.append(state) index += 1 item_name = None return items
def load_menu(self, names, comparator, disabled_items=None): """ Load menu items :param names: list of menu item names (should have corresponding filename) :param comparator: string used to sort items :param disabled_items: list of items which should be disabled :return: dictionary with menu items """ items = {} f = self.config[ICON_SIZE_FOLDER] for name in names: filename = name + EXT_PNG path = os.path.join(FOLDER_ICONS, f, filename) icon = self.load_image(path) filename = name + IMAGE_SELECTED_SUFFIX + EXT_PNG path_on = os.path.join(FOLDER_ICONS, f, filename) icon_on = self.load_image(path_on) filename = name + IMAGE_DISABLED_SUFFIX + EXT_PNG path_off = os.path.join(FOLDER_ICONS, f, filename) icon_off = self.load_image(path_off) state = State() state.name = name state.genre = name state.l_genre = self.config[LABELS][name] state.l_name = self.config[LABELS][name] state.icon_base = icon if icon_on: state.icon_selected = icon_on else: state.icon_selected = icon if not icon_off: state.icon_disabled = icon_on state.bgr = self.config[COLORS][COLOR_DARK] state.img_x = None state.img_y = None state.auto_update = True state.show_bgr = True state.show_img = True state.show_label = True if comparator == NAME: state.comparator_item = state.name elif comparator == GENRE: state.comparator_item = state.genre if disabled_items and name in disabled_items: state.enabled = False items[state.name] = state return items
def load_favorites(self, p, items_per_page, default_icon_path): """ Load m3u playlist :param p: base path :param items_per_page: items per page :param items_per_page: items per page :param default_icon_path: path to the default icon :return: list of State objects representing playlist """ favorites = [] lines = [] state = None index = 0 if p == None: return favorites try: path = os.path.join(p, FILE_FAVORITES) lines = codecs.open(path, "r", UTF8).read().split("\n") except Exception as e: pass for line in lines: if len(line.rstrip()) == 0: continue if line.startswith("#") and state == None: state = State() state.favorite = True state.index = index state.name = str(index) state.index_in_page = index % items_per_page state.genre = line[1:].rstrip() elif line.startswith("#") and state != None: state.l_name = line[1:].rstrip() path = os.path.join(p, state.genre, state.l_name + EXT_PNG) icon = self.image_util.load_image(path) if not icon: icon = self.image_util.load_image(default_icon_path) state.icon_base = icon state.comparator_item = NAME elif line.startswith("http"): state.url = line.rstrip() favorites.append(state) state = None index += 1 return favorites
def get_center_button(self, s): """ Create the center button :param s: button state :return: station logo button """ bb = Rect(self.layout.CENTER.x + 1, self.layout.CENTER.y + 1, self.layout.CENTER.w - 1, self.layout.CENTER.h - 1) if not hasattr(s, "icon_base"): self.util.add_icon(s) state = State() state.icon_base = s.icon_base self.factory.set_state_scaled_icons(s, bb) state.index = s.index state.genre = s.genre state.scaled = getattr(s, "scaled", False) state.icon_base_scaled = s.icon_base_scaled state.name = "station." + s.name state.l_name = s.l_name state.url = s.url state.keyboard_key = kbd_keys[KEY_SELECT] state.bounding_box = bb state.img_x = bb.x state.img_y = bb.y state.auto_update = False state.show_bgr = True state.show_img = True state.logo_image_path = s.image_path state.image_align_v = V_ALIGN_BOTTOM state.comparator_item = self.current_state.comparator_item button = Button(self.util, state) img = button.components[1] self.logo_button_content = (img.image_filename, img.content, img.content_x, img.content_y) return button
def load_stations(self, language, genre, stations_per_page): """ Load stations for specified language and genre :param language: the language :param genre: the genre :param stations_per_page: stations per page used to assign indexes :return: list of button state objects. State contains station icons, index, genre, name etc. """ stations = [] folder = os.path.join(FOLDER_STATIONS, language, genre) path = os.path.join(folder, genre + EXT_M3U) lines = [] try: lines = codecs.open(path, "r", UTF_8).read().split("\n") except Exception as e: logging.error(str(e)) pass for i in range(0, len(lines), 3): if len(lines[i].rstrip()) == 0: continue index = int(lines[i].rstrip()[1:]) localized_name = lines[i + 1][1:] url = lines[i + 2] icon = self.load_station_icon(folder, index) state = State() state.index = index state.genre = genre state.url = url.rstrip() state.name = str(index) state.l_name = localized_name.rstrip() state.icon_base = icon state.comparator_item = INDEX state.index_in_page = index % stations_per_page stations.append(state) return stations
def load_menu(self, names, comparator, disabled_items=None, v_align=None, bb=None, scale=1): """ Load menu items :param names: list of menu item names (should have corresponding filename) :param comparator: string used to sort items :param disabled_items: list of items which should be disabled :param v_align: vertical alignment :param bb: bounding box :param scale: image scale factor :return: dictionary with menu items """ items = {} i = 0 for name in names: icon = self.load_mono_svg_icon(name, self.COLOR_MAIN, bb, scale) icon_on = self.load_mono_svg_icon(name, self.COLOR_ON, bb, scale) if disabled_items and name in disabled_items: icon_off = self.load_mono_svg_icon(name, self.COLOR_OFF, bb, scale) else: icon_off = None state = State() state.name = name state.genre = name try: state.l_genre = self.config[LABELS][name] state.l_name = self.config[LABELS][name] except: state.l_genre = name state.l_name = name state.icon_base = icon if icon_on: state.icon_selected = icon_on else: state.icon_selected = icon if not icon_off: state.icon_disabled = icon_on else: state.icon_disabled = icon_off state.bgr = self.config[COLORS][COLOR_DARK] state.img_x = None state.img_y = None state.auto_update = True state.show_bgr = True state.show_img = True state.show_label = True state.v_align = v_align if comparator == NAME: state.comparator_item = state.name elif comparator == GENRE: state.comparator_item = state.genre if disabled_items and name in disabled_items: state.enabled = False state.index = i items[state.name] = state i += 1 return items