Skip to content

Commit 525001c

Browse files
committed
Added conf location and generation
Changed config file location to fit xdg specs and sit at .config for per user configuration Now the configuration file is automaticly created with default values at the new path and the broadcast adress is added dynamicly.
1 parent 09c151b commit 525001c

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@ Configuration File
2323

2424
The configuration file is just a basic INI file, containing one section per host;
2525

26+
The configuration file is located at `~/.config/bentasker.Wake-On-Lan-Python/wol_config.ini`
27+
28+
The following is an example of hosts save in `wol_config.ini`
29+
2630
[General]
2731
broadcast=192.168.1.255
28-
32+
2933
[MyPc]
3034
mac=00:13:0d:e4:60:61
3135

32-
33-
36+
37+
3438
License
3539
--------
3640

37-
PSF v2, see [LICENSE](LICENSE)
41+
PSF v2, see [LICENSE](LICENSE)

wol.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def wake_on_lan(host):
3939
macaddress = macaddress.replace(macaddress[2], '')
4040
else:
4141
raise ValueError('Incorrect MAC address format')
42-
42+
4343
# Pad the synchronization stream.
4444
data = ''.join(['FFFFFFFFFFFF', macaddress * 20])
4545
send_data = b''
@@ -56,14 +56,34 @@ def wake_on_lan(host):
5656
return True
5757

5858

59-
def loadConfig():
60-
""" Read in the Configuration file to get CDN specific settings
59+
def writeConfig(conf):
60+
""" Write configuration file to save local settings """
61+
global conf_path
62+
conf.write(open(conf_path+'/wol_config.ini', 'w'))
63+
6164

62-
"""
63-
global mydir
64-
global myconfig
65-
Config = configparser.ConfigParser()
66-
Config.read(mydir+"/.wol_config.ini")
65+
def loadConfig():
66+
""" Read in the Configuration file to get CDN specific settings """
67+
global conf_path
68+
global myconfig
69+
Config = configparser.ConfigParser()
70+
# Create conf path if does not exists
71+
if not os.path.exists(conf_path):
72+
os.makedirs(conf_path, exist_ok=True)
73+
# generate default config file if does not exists
74+
if not os.path.exists(conf_path+'/wol_config.ini'):
75+
# get broadcast ip dynamicly
76+
local_ip = socket.gethostbyname(socket.gethostname())
77+
local_ip = local_ip.rsplit('.', 1)
78+
local_ip[1] = '255'
79+
broadcast_ip = '.'.join(local_ip)
80+
# Load default values to new conf file
81+
Config['General'] = {'broadcast': broadcast_ip}
82+
# two examples for devices
83+
Config['myPC'] = {'mac': '00:2a:a0:cf:83:15'}
84+
Config['myLaptop'] = {'mac': '00:13:0d:e4:60:61'}
85+
writeConfig(Config) # Generate default conf file
86+
Config.read(conf_path+"/wol_config.ini")
6787
sections = Config.sections()
6888
dict1 = {}
6989
for section in sections:
@@ -85,7 +105,7 @@ def usage():
85105

86106

87107
if __name__ == '__main__':
88-
mydir = os.path.dirname(os.path.abspath(__file__))
108+
conf_path = os.path.expanduser('~/.config/bentasker.Wake-On-Lan-Python')
89109
conf = loadConfig()
90110
try:
91111
# Use macaddresses with any seperators.
@@ -102,7 +122,3 @@ def usage():
102122
print('Magic packet should be winging its way')
103123
except:
104124
usage()
105-
106-
107-
108-

0 commit comments

Comments
 (0)