Пример #1
0
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
Пример #2
0
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
Пример #3
0
    def _read(self):
        """
            Read the json file and parse it.
        """
        from HardcodeTray.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")
Пример #4
0
 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.")
Пример #5
0
 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.")
Пример #6
0
    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))
Пример #7
0
 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
Пример #8
0
    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))
Пример #9
0
    def revert_icon(self, icon, icon_path):
        """Revert to the original icon."""
        target = B64ElectronApplication.get_real_path(self.file)
        backup_file = "|".join(target.split("/"))

        bytes = self.get_backup_file(backup_file)
        if bytes:
            self.set_icon(target, icon_path, bytes)
        else:
            Logger.error("Backup file of {0} was not found".format(self.name))
Пример #10
0
 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
Пример #11
0
    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))
Пример #12
0
    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))
Пример #13
0
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
Пример #14
0
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
Пример #15
0
    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
Пример #16
0
    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
Пример #17
0
    def install_icon(self, icon, icon_path):
        """Install the icon."""
        icon.icon_size = App.icon_size()

        png_bytes = get_pngbytes(icon)

        if png_bytes:
            # Read a target file
            asar = AsarFile(path.join(str(icon_path), self.binary))
            target = B64ElectronApplication.get_real_path(self.file)
            file_content = asar.read_file(target).decode()
            # Create new base64 binary file
            base64_icon = b64encode(App.svg().to_bin(icon.theme, icon.icon_size, icon.icon_size))
            # Build new icon
            new_icon = "data:image/png;base64," + base64_icon.decode()
            # Replace the original icon with newly built one
            file_content = file_content.replace(icon.original, new_icon)
            bytes = file_content.encode()
            self.set_icon(target, icon_path, bytes, True)
        else:
            Logger.error("Icon file was not found.")
Пример #18
0
    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]
Пример #19
0
def execute(command_list, verbose=True, shell=False, working_directory=None):
    """
    Run a command using subprocess.run().

    Args :
        command_list(list)
        verbose(bool)
    """
    Logger.debug("Executing command: {0}".format(" ".join(command_list)))
    if working_directory:
        cmd = run(command_list,
                  capture_output=True,
                  shell=False,
                  check=False,
                  cwd=working_directory)
    else:
        cmd = run(command_list, capture_output=True, shell=False, check=False)

    output, error = Popen.communicate()
    if verbose and error:
        Logger.error(error.decode('utf-8').strip())
    return output