Skip to content

Commit 1f0d25c

Browse files
committed
Merge branch 'jNullj-config-path-update'
Closes !10
2 parents 09c151b + b4a31d5 commit 1f0d25c

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
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: 40 additions & 26 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,36 +56,54 @@ def wake_on_lan(host):
5656
return True
5757

5858

59-
def loadConfig():
60-
""" Read in the Configuration file to get CDN specific settings
61-
62-
"""
63-
global mydir
64-
global myconfig
65-
Config = configparser.ConfigParser()
66-
Config.read(mydir+"/.wol_config.ini")
67-
sections = Config.sections()
68-
dict1 = {}
69-
for section in sections:
70-
options = Config.options(section)
71-
72-
sectkey = section
73-
myconfig[sectkey] = {}
74-
75-
76-
for option in options:
77-
myconfig[sectkey][option] = Config.get(section,option)
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'))
7863

7964

80-
return myconfig # Useful for testing
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")
87+
sections = Config.sections()
88+
dict1 = {}
89+
for section in sections:
90+
options = Config.options(section)
91+
92+
sectkey = section
93+
myconfig[sectkey] = {}
94+
95+
for option in options:
96+
myconfig[sectkey][option] = Config.get(section, option)
97+
98+
return myconfig # Useful for testing
8199

82100
def usage():
83101
print('Usage: wol.py [hostname]')
84102

85103

86104

87105
if __name__ == '__main__':
88-
mydir = os.path.dirname(os.path.abspath(__file__))
106+
conf_path = os.path.expanduser('~/.config/bentasker.Wake-On-Lan-Python')
89107
conf = loadConfig()
90108
try:
91109
# Use macaddresses with any seperators.
@@ -102,7 +120,3 @@ def usage():
102120
print('Magic packet should be winging its way')
103121
except:
104122
usage()
105-
106-
107-
108-

0 commit comments

Comments
 (0)