def install_icon(self, icon, icon_path): """Install the icon.""" pngbytes = get_pngbytes(icon) if pngbytes: self.set_icon(icon.original, icon_path, pngbytes, True) else: Logger.error("PNG file was not found.")
def get_kde_scaling_factor(): """Return the widgets scaling factor on KDE.""" scaling_factor = None was_found = False try: with open(KDE_CONFIG_FILE, 'r') as kde_obj: data = kde_obj.readlines() for line in data: line = list(map(lambda x: x.strip(), line.split("="))) if len(line) == 1: was_found = match(r'\[Containments\]\[[0-9]+\]\[General\]', line[0]) if len(line) > 1 and was_found and line[0].lower() == "iconsize": scaling_factor = int(line[1]) break if scaling_factor: Logger.debug("Scaling Factor/KDE: {}".format(scaling_factor)) return scaling_factor except (FileNotFoundError, KeyError) as kde_error: Logger.debug("Scaling Factor/KDE not detected.") Logger.error(kde_error) return None
def _read(self): """ Read the json file and parse it. """ from src.app import App do_later = ["app_path", "icons_path", "icons"] try: with open(self._db_file, 'r') as db_obj: data = json.load(db_obj) for key, value in data.items(): if key not in do_later: setattr(self, key, value) except (FileNotFoundError, ValueError, KeyError): Logger.error("Application file is broken: {}".format( self._db_file)) self._parse_paths(data["app_path"], "app_path") self._parse_paths(data["icons_path"], "icons_path") self._parse_icons(data["icons"]) if len(App.get("only")) == 1 and App.path(): self.app_path.append(App.path()) found = self.icons and self.app_path if self.force_create_folder and found: for icon_path in self.icons_path: create_dir(str(icon_path)) self.dont_install = False else: self.dont_install = not (found and self.icons_path) # NWJS special case if self.get_type() == "nwjs" and not self.dont_install: self.dont_install = not App.get("nwjs")
def install_icon(self, icon, icon_path): """Install the icon.""" png_bytes = get_pngbytes(icon) if png_bytes: icon = ElectronApplication.get_real_path(icon.original) self.set_icon(icon, icon_path, png_bytes, True) else: Logger.error("PNG file was not found.")
def revert_icon(self, icon, icon_path): """Revert to the original icon.""" asar_icon_path = ElectronApplication.get_real_path(icon.original) backup_file = "|".join(asar_icon_path.split("/")) png_bytes = self.get_backup_file(backup_file) if png_bytes: self.set_icon(icon.original, icon_path, png_bytes) else: Logger.error("Backup file of {0} was not found".format(self.name))
def theme(): """Return a theme object.""" theme_name = Gtk.Settings.get_default().get_property( "gtk-icon-theme-name") theme = None if theme_name: Logger.debug("System/Theme: {}".format(theme_name)) theme = Theme(theme_name) else: Logger.error("System/Theme: Not detected.") return theme
def theme(): """Return a theme object.""" if SystemConfig._theme is None: source = Gio.SettingsSchemaSource.get_default() if source.lookup("org.gnome.desktop.interface", True): gsettings = Gio.Settings.new("org.gnome.desktop.interface") theme_name = gsettings.get_string("icon-theme") SystemConfig._theme = Theme(theme_name) Logger.debug("System/Theme: {}".format(theme_name)) else: Logger.error("System/Theme: Not detected.") return SystemConfig._theme
def set_icon(self, icon, icon_path, pngbytes, backup=False): """Update the icon bytes with the new one.""" self.set_binary_file(path.join(str(icon_path), self.binary)) if self.pak: icon_name = icon.original if pngbytes and self.pak.haskey(icon_name): if backup: self.backup.file(icon_name, self.pak.get_value(icon_name)) self.pak.set_value(icon_name, pngbytes) self.pak.write() else: Logger.error("Couldn't find a PNG file.") else: Logger.warning("The file {0} was not found".format( self.binary_file))
def _get_header(self): asarfile = open(self._asar_file, 'rb') try: asarfile.seek(4) # header size is stored in byte 12:16 self._hb[0] = unpack('I', asarfile.read(4))[0] self._hb[1] = unpack('I', asarfile.read(4))[0] self._hb[2] = unpack('I', asarfile.read(4))[0] self._zeros = (self._hb[1] - 4 - self._hb[2]) header = asarfile.read(self._hb[2]).decode('utf-8') self._header = loads(header) self._offset = asarfile.tell() + self._zeros asarfile.close() except StructError: Logger.error("Electron: Couldn't read asar file {}".format( self._asar_file)) self.success = False
def execute(command_list, verbose=True, shell=False, working_directory=None): """ Run a command using "Popen". Args : command_list(list) verbose(bool) """ Logger.debug("Executing command: {0}".format(" ".join(command_list))) if working_directory: cmd = Popen(command_list, stdout=PIPE, stderr=PIPE, shell=shell, cwd=working_directory) else: cmd = Popen(command_list, stdout=PIPE, stderr=PIPE, shell=shell) output, error = cmd.communicate() if verbose and error: Logger.error(error.decode("utf-8").strip()) return output
def _read(self): """Read a data pack file and returns a dictionary.""" with open(self._filename, 'rb') as file_object: data = file_object.read() file_object.close() original_data = data # Read the header. version, num_entries, _ = unpack('<IIB', data[:HEADER_LENGTH]) if version != PACK_FILE_VERSION: Logger.error('Wrong file version in {0!s}'.format(self._filename)) raise Exception if num_entries != 0: # Read the index and data. data = data[HEADER_LENGTH:] index_entry = 2 + 4 # Each entry is a uint16 and a uint32. for _ in range(num_entries): _id, offset = unpack('<HI', data[:index_entry]) data = data[index_entry:] next_offset = unpack('<HI', data[:index_entry])[1] self._resources[_id] = original_data[offset:next_offset]
def theme(): """Return a theme object.""" theme = None theme_settings = path.join(USERHOME, ".config/gtk-3.0/settings.ini") if DESKTOP_ENV in ["budgie", "unity", "gnome", "elementary", "other"]: source = Gio.SettingsSchemaSource.get_default() if source.lookup("org.gnome.desktop.interface", True): gsettings = Gio.Settings.new("org.gnome.desktop.interface") theme_name = gsettings.get_string("icon-theme") Logger.debug("System/Theme/GSettings: {}".format(theme_name)) theme = Theme(theme_name) elif path.exists(theme_settings): try: cnfg = ConfigParser() cnfg.read(theme_settings) theme_name = cnfg.get("Settings", "gtk-icon-theme-name") Logger.debug("System/Theme/Setting.ini: {}".format(theme_name)) theme = Theme(theme_name) except KeyError as err: Logger.error("System/Theme/Settings.ini: {}".format(err)) exit(err) else: Logger.error("System/Theme: Not detected.") return theme
def set_icon(self, icon_to_repl, binary_path, pngbytes, backup=False): """Set the icon into the electron binary file.""" icon_to_repl = ElectronApplication.get_real_path(icon_to_repl) binary_file = path.join(str(binary_path), self.binary) asarfile = open(binary_file, 'rb') try: asarfile.seek(4) # header size is stored in byte 12:16 len1 = unpack('I', asarfile.read(4))[0] len2 = unpack('I', asarfile.read(4))[0] len3 = unpack('I', asarfile.read(4))[0] header_size = len3 zeros_padding = (len2 - 4 - len3) header = asarfile.read(header_size).decode('utf-8') files = loads(header) originaloffset = asarfile.tell() + zeros_padding asarfile.close() keys = icon_to_repl.split("/") fileinfo = get_from_dict(files, keys) if isinstance(fileinfo, dict) and "offset" in fileinfo.keys(): offset0 = int(fileinfo['offset']) offset = offset0 + originaloffset size = int(fileinfo['size']) with open(binary_file, 'rb') as asarfile: bytearr = asarfile.read() if pngbytes: set_in_dict(files, keys + ['size'], len(pngbytes)) if backup: backup_file = "|".join(keys) self.backup.file(backup_file, bytearr[offset:offset + size]) newbytearr = pngbytes.join( [bytearr[:offset], bytearr[offset + size:]]) sizediff = len(pngbytes) - size newfiles = change_dict_vals(files, sizediff, offset0) newheader = dumps(newfiles).encode('utf-8') newheaderlen = len(newheader) bytearr2 = b''.join([ bytearr[:4], pack('I', newheaderlen + (len1 - len3)), pack('I', newheaderlen + (len2 - len3)), pack('I', newheaderlen), newheader, b'\x00' * zeros_padding, newbytearr[originaloffset:] ]) asarfile = open(binary_file, 'wb') asarfile.write(bytearr2) asarfile.close() except StructError: Logger.error("The asar file of {0} seems to be " "corrupted".format(self.name)) self.is_corrupted = True