Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Huge code improvements #248

Closed
bilelmoussaoui opened this issue Dec 14, 2016 · 26 comments
Closed

Huge code improvements #248

bilelmoussaoui opened this issue Dec 14, 2016 · 26 comments

Comments

@bilelmoussaoui
Copy link
Owner

I was taking a look on how we can make the code as perfect as possible, understandable and without too many conditions/loops;
The best way to do so, is to rewrite almost all the script and i have some ideas to share here
Let's start with the structure of the code

  • The first thing in order to allow support of all the possible ways to convert SVG icons to other formats using Inkscape, Cairo, ImageMagick...
class SVG:
    def __init__(self, file_name):
        
    def get_size():

    def resize(w, h):

    def convert_to_png():

    def convert_to_bin():

    def is_installed():

This main class that all the possible way to convert from SVG to PNG will inherit it. For example

class Inkscape(SVG):

class Cairo(SVG):
  • One of the most complicated parts of the code is the different kind of applications that we support
    so I imagined something more structured, easy to fix and debug and make the code more structured
class Application:
    step = 0
    def __init__(self, json_file):

    def is_installed(self):
        # Detects if the application is installed 
    def install(self):
        # Install application structure
    def backup():
        # Backup files

    def restore():
        # Restore original files
    
    def uninstall():
        # Uninstall icons

And we can have something like this in order for all the kind of the applications that we will support

class ElectronApplication(Application):

class QtApplication(Application):

class PakApplication(Application):

class ZipApplication(Application):

class BinaryApplication(Application):

class NwJSApplication(Application):

Whenever we want to add a new application support, instead of changing the whole code source in order to implement it. Just add a new class that will inherit the main Class(or Interface) and add/modify the all functions you need.

  • As we do too many operations with files, and we have created some small functions to avoid Exceptions when a file does not exists or something like this; i thought about creating a small class that will have all possible operations we can do with them.
class FileManager:
    def __init__(self, file_name):
    @staticmethod
    def create_directory(dir_name, recursive=True):
   
    @staticmethod
    def remove_directory(dir_name, recursive=False):

    def copy_file(self, dest):

    def move_file(self, dest):

    def remove_file(self):

    def symlink_file(self, symlink_name):    

    def read_file(binary_mode=False):

    def update_file(self, content):
        

This structure might change if we really do want to implement it. Doing so, will just get ride of all the scripts files in the future. Imagine, spotify case? We use ZipApplication class for it and things are done. The dropbox issue can be fixed easily, by creating a specific class for Dropbox that will inherits all the functions from Application and will only apply the code we have in the dropbox.py file to the paths;
The structure of folders/files can be like this :

- modules
    - applications
        - application.py
        - electron.py
        - qt.py
        - pak.py
        - zip.py
        - binary.py
        - nwjs.py
        - specific
            - dropbox.py
    - filemanager.py
    - conversion
        - svg.py
        - inkscape.py
        - cairo.py
        - imagemagick.py
        ...
- database
    - ...json
- install.sh
- uninstall.sh
- hardcode-tray
- script.py
@bilelmoussaoui
Copy link
Owner Author

@wa4557 I would like to have your opinion on this. I would like to work on it and release it before the end of this year. As this will fix #157 #244 #152 and #228 if i can implement a general way for binary icons.

@andia89
Copy link
Collaborator

andia89 commented Dec 14, 2016

Yeah, object oriented programming makes sense here to a certain degree. I'm a bit reluctant regarding stuff like the FileManager class, because I don't see why there is an improvement over simple functions (i.e. taking one argument returning something else). This is for me a prime example for functional programming.

An Application class makes a lot of sense though 👍

@bilelmoussaoui
Copy link
Owner Author

I'm okey with not creating a FileManager class, but the idea was having the possibilty to do something like FileManager('test.py').copy('/tmp').remove() but yeah we don't really need that.

@andia89
Copy link
Collaborator

andia89 commented Dec 14, 2016

I think the most important thing for now, would be to make this application class, the rest we can always add afterwards if we want. Before we start this, I think it makes a lot of sense to figure out how to deal with nw.js applications, and check if sni-qt works as expected. (And I also want to fix **** teamviewer)

@bilelmoussaoui
Copy link
Owner Author

@wa4557 I have worked recently with a guy from Gitter to fix a little issue on the application, i can try to contact him and see if we can get some information about how to fix nwjs issue. I will install xubuntu today and see if everything works as expected.

@bilelmoussaoui
Copy link
Owner Author

@wa4557 @varlesh a demo version is on the branch wip/modules! can you guys test it and see if you have any bugs?

@varlesh
Copy link
Collaborator

varlesh commented Dec 28, 2016

i'm run on eOS, but more apps not fixed because this branch have old base:
image
Log not have errors:

alex@eos:~/Git/tmp/Hardcode-Tray-wip-modules$ sudo -E python3 script.py
[sudo] пароль для alex: 
Welcome to the tray icons hardcoder fixer!
Your indicator icon size is : 24
Your current icon theme is : ePapirus
Svg to png functions are : Enabled
Conversion tool : Inkscape
Applications will be fixed : All
1 - Apply
2 - Revert
Please choose: 1
Applying now..

Clementine
Google Chrome
My Weather Indicator
Skype
Wire
Clementine               [#.......................................] 1/53 1.9%
Google Chrome            [##......................................] 2/53 3.8%
My Weather Indicator     [################........................] 21/53 39.6%
alex@eos:~/Git/tmp/Hardcode-Tray-wip-modules$ 

@varlesh
Copy link
Collaborator

varlesh commented Dec 28, 2016

Also Wire deleted on my system, but file /opt/Wire/resources/app.asar exist.... maybe it's bug DEB-package, i don't know. Also this bug reprodused with master-branch

@bilelmoussaoui
Copy link
Owner Author

It must be that the deb package uses an other path.... i'm still looking for a way to fix the issues with not all the applications being fixed.

@varlesh
Copy link
Collaborator

varlesh commented Dec 28, 2016

It must be that the deb package uses an other path.... i'm still looking for a way to fix the issues with not all the applications being fixed.

No, wire fixed with master-branch, but after remove wire package /opt/Wire directory not deleted

@bilelmoussaoui
Copy link
Owner Author

Ah :p i might look later for a better way to detect installed packages! but for now i will keep it that way..

@bilelmoussaoui
Copy link
Owner Author

Fixed the issue where not all the applications are not being fixed!

@bilelmoussaoui
Copy link
Owner Author

Done on master now!

@andia89
Copy link
Collaborator

andia89 commented Dec 29, 2016

I get this:

Traceback (most recent call last):
  File "./script.py", line 288, in <module>
    install(fix_only, icon_path)
  File "./script.py", line 180, in install
    app.install()
  File "/home/andreas/Diverses/Hardcode-Tray/modules/applications/pak.py", line 48, in install
    self.install_icon(icon, icon_path)
  File "/home/andreas/Diverses/Hardcode-Tray/modules/applications/pak.py", line 63, in install_icon
    _data_pack = data_pack.read_data_pack(filename)
  File "/home/andreas/Diverses/Hardcode-Tray/modules/applications/../data_pack.py", line 51, in read_data_pack
    data = read_file(input_file, BINARY)
  File "/home/andreas/Diverses/Hardcode-Tray/modules/applications/../data_pack.py", line 41, in read_file
    with open(filename, mode) as file_object:
FileNotFoundError: [Errno 2] Datei oder Verzeichnis nicht gefunden: '/usr/lib/chromium-browser/resources.pak'

@bilelmoussaoui
Copy link
Owner Author

I should add a verification if the binary file exists too. Totally forgot that

@bilelmoussaoui
Copy link
Owner Author

Fixed!

@andia89
Copy link
Collaborator

andia89 commented Dec 31, 2016

Not fixed. Still get the same error

@bilelmoussaoui
Copy link
Owner Author

Using the git version?

@andia89
Copy link
Collaborator

andia89 commented Dec 31, 2016

Yeah, updated master branch

@bilelmoussaoui
Copy link
Owner Author

How it's even possible ? :( see here https://github.com/bil-elmoussaoui/Hardcode-Tray/blob/master/modules/data.py#L126
I do check if the pak files exists if the application uses one....

@andia89
Copy link
Collaborator

andia89 commented Jan 1, 2017

Let mehr think. I habe to get my head around the new code

@bilelmoussaoui
Copy link
Owner Author

Already found what was not working :p just update to master ;)

@andia89
Copy link
Collaborator

andia89 commented Jan 1, 2017

Now I get this:

Traceback (most recent call last):
  File "script.py", line 302, in <module>
    install(fix_only, icon_path)
  File "script.py", line 186, in install
    app.install()
  File "/opt/Hardcode-Tray/modules/applications/electron.py", line 47, in install
    self.install_icon(icon, icon_path)
  File "/opt/Hardcode-Tray/modules/applications/electron.py", line 75, in install_icon
    offset0 = int(fileinfo['offset'])
KeyError: 'offset'

@bilelmoussaoui
Copy link
Owner Author

I cannot reproduce that here in order to fix it. It's due to an electron application that does not have an icon that we support (for older/newer version). I pushed a fix, i hope that this will work. Otherwise, we will have to rewrite the get_from_dict function

@andia89
Copy link
Collaborator

andia89 commented Jan 1, 2017

Works 👍

@bilelmoussaoui
Copy link
Owner Author

Perfect! The code still need some more improvements ;) Especially the electron script :p https://www.codacy.com/app/bil-elmoussaoui/Hardcode-Tray/file/4849926842/issues/source?bid=3912774&fileBranchId=3912774#l49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants