@@ -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
87107if __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