def get_hosts(self, make_cfg): """ Make the new hosts file by the configuration defined by `make_cfg` from function list on the main dialog. :param make_cfg: Module settings in byte word format. :type make_cfg: dict .. seealso:: :attr:`make_cfg` in :class:`~tui.curses_d.CursesDaemon` class. """ for part_id in sorted(make_cfg.keys()): mod_cfg = make_cfg[part_id] if not RetrieveData.chk_mutex(part_id, mod_cfg): return mods = RetrieveData.get_ids(mod_cfg) for mod_id in mods: self.mod_num += 1 hosts, mod_name = RetrieveData.get_host(part_id, mod_id) if part_id == 0x02: self.write_localhost_mod(hosts) elif part_id == 0x04: self.write_customized() else: self.write_common_mod(hosts, mod_name)
def make(self): """ Start operations to retrieve data from the data file and make the new hosts file for the current operating system. """ RetrieveData.connect_db() self.make_time = time.time() self.write_head() self.write_info() self.get_hosts(self.make_cfg) self.hosts_file.close() RetrieveData.disconnect_db()
def init_main(self): # Set mirrors self.mirrors = Utilities.set_network("network.conf") self.set_platform() # Read data file and set function list try: RetrieveData.unpack() RetrieveData.connect_db() self.set_func_list() self.set_info() except IOError: pass except BadZipfile: pass # Check if current session have root privileges self.check_root()
def set_info(self): """Set data file info - Public Method Set the information of the current local data file. """ info = RetrieveData.get_info() ver = info["Version"] build = info["Buildtime"] build = Utilities.timestamp_to_date(build) self.hostsinfo = [ver, build]
def set_func_list(self): for ip in range(2): choice, defaults, slices = RetrieveData.get_choice(ip) self.choice[ip] = choice self.slices[ip] = slices funcs = [] for func in choice: if func[1] in defaults[func[0]]: funcs.append(1) else: funcs.append(0) self._funcs[ip] = funcs
def write_info(self): """ Write the information part into the new hosts file. """ info = RetrieveData.get_info() info_lines = [ "#", "# %s: %s" % ("Version", info["Version"]), "# %s: %s" % ("BuildTime", info["Buildtime"]), "# %s: %s" % ("ApplyTime", int(self.make_time)), "#" ] for line in info_lines: self.hosts_file.write("%s%s" % (line, self.eol))
def write_head(self): """ Write the head part into the new hosts file. """ for head_str in RetrieveData.get_head(): self.hosts_file.write("%s%s" % (head_str[0], self.eol))
def select_func(self, pos=None, key_in=None): screen = self.__stdscr.subwin(18, 26, 2, 26) screen.bkgd(" ", curses.color_pair(4)) # Set local variable normal = curses.A_NORMAL select = curses.color_pair(5) select += curses.A_BOLD list_height = 15 ip = self.settings[1][1] # Key Press Operations item_len = len(self.func_items[ip]) item_sup, item_inf = self.item_sup, self.item_inf if pos != None: if item_len > list_height: if pos <= 1: item_sup = 0 item_inf = list_height - 1 elif pos >= item_len - 2: item_sup = item_len - list_height + 1 item_inf = item_len else: item_sup = 0 item_inf = item_len if key_in == curses.KEY_DOWN: pos += 1 if pos >= item_len: pos = 0 if pos not in range(item_sup, item_inf): item_sup += 2 if item_sup == 0 else 1 item_inf += 1 elif key_in == curses.KEY_UP: pos -= 1 if pos < 0: pos = item_len - 1 if pos not in range(item_sup, item_inf): item_inf -= 2 if item_inf == item_len else 1 item_sup -= 1 elif key_in in [10, 32]: self.func_selec[ip][pos] = not self.func_selec[ip][pos] mutex = RetrieveData.get_ids(self.func_items[ip][pos][2]) for c_id, c in enumerate(self.func_items[ip]): if c[0] == self.func_items[ip][pos][0]: if c[1] in mutex and self.func_selec[ip][c_id] == 1: self.func_selec[ip][c_id] = 0 self.info(pos, 1) else: item_sup = 0 if item_len > list_height: item_inf = list_height - 1 else: item_inf = item_len # Function list items_show = self.func_items[ip][item_sup:item_inf] items_selec = self.func_selec[ip][item_sup:item_inf] for p, item in enumerate(items_show): sel_ch = "+" if items_selec[p] else " " item_str = ("[%s] %s" % (sel_ch, item[3])).ljust(23) item_pos = pos - item_sup if pos != None else None highlight = select if p == item_pos else normal if item_len > list_height: if item_inf - item_sup == list_height - 2: screen.addstr(4 + p, 2, item_str, highlight) elif item_inf == item_len: screen.addstr(4 + p, 2, item_str, highlight) elif item_sup == 0: screen.addstr(3 + p, 2, item_str, highlight) else: screen.addstr(3 + p, 2, item_str, highlight) if item_len > list_height: if item_inf - item_sup == list_height - 2: screen.addstr(3, 2, " More ".center(23, "."), normal) screen.addch(3, 15, curses.ACS_UARROW) screen.addstr(17, 2, " More ".center(23, "."), normal) screen.addch(17, 15, curses.ACS_DARROW) elif item_inf == item_len: screen.addstr(3, 2, " More ".center(23, "."), normal) screen.addch(3, 15, curses.ACS_UARROW) elif item_sup == 0: screen.addstr(17, 2, " More ".center(23, "."), normal) screen.addch(17, 15, curses.ACS_DARROW) else: for line_i in range(list_height - item_len): screen.addstr(17 - line_i, 2, " " * 23, normal) screen.refresh() self.item_sup, self.item_inf = item_sup, item_inf return pos