summaryrefslogtreecommitdiff
path: root/src/load_config.py
blob: 2c894a3ce41fa87cef2b7217d2a19f4e36d007a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from rich import print
import os


class systemConfig:
    """
    Class which checks for presence of configuration files for programs
    instant rice is compatible with. Additionally, loads the user's configuration
    file for instant rice.
    """
    
    i3_config = ""
    i3_lock_image = ""
    polybar_config = ""
    wallpaper_directory = ""
    rofi_config = ""
    username = ""
    rice_config = ""
    
    #instant rice configuration settings
    use_dmenu = False
    generate_i3_lock = False

    def __init__(self):
        print('[bold green]Loading configuration files')
        
        #Grab username to find configuration files
        self.username = os.getlogin()
        
        # check if config files present and make accessible to instantrice
        # if file not present do nothing. On user to initialize files
        if os.path.exists(f'/home/{self.username}/.config/i3/'):
            self.i3_config = f'/home/{self.username}/.config/i3/config'
            self.i3_lock_image = f'/home/{self.username}/.config/i3/lock.png'
        if os.path.exists(f'/home/{self.username}/.config/polybar/'):
            self.polybar_config = f'/home/{self.username}/.config/polybar/config.ini'
        if os.path.exists(f'/home/{self.username}/.config/rofi/config.rasi'):
            self.rofi_config = f'/home/{self.username}/.config/rofi/config.rasi'
        if os.path.exists(f'/home/{self.username}/.config/instantrice/'):
            self.rice_config = f'/home/{self.username}/.config/instantrice/config.rice'
            self.get_user_preferences()
        else:
            print(f'[bold red]InstantRice configuration not present.\nDrop default configuration file at /home/{self.username}/.config/InstantRice/config.rice?')
            response = input('(a)ccept (d)ecline> ')
            if response == "a":
                self.rice_config = f'/home/{self.username}/.config/instantrice/config.rice'
                self.copy_rice_config()

    def copy_rice_config(self):

        print(f'Creating instant rice configuration directory')
        os.makedirs(f'/home/{self.username}/.config/instantrice')

        with open('../data/default_config.rice') as file:
            data = file.readlines()
        with open(f'{self.rice_config}', 'w') as file:
            file.writelines(data)
    
    def get_user_preferences(self):

        with open(self.rice_config) as file:
            data = file.readlines()
        
        for i, line in enumerate(data):
            if "use_dmenu" in line:
                match = line.split(' ')
                self.use_dmenu = True if match[2] == 'True' else False
            if "generate_i3_lock" in line:
                match = line.split(' ')
                self.generate_i3_lock = True if match[2] == 'True' else False
            if "wallpaper_directory"  in line:
                match = line.split(' ')
                self.wallpaper_directory = match[2]