@@ -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
82100def usage ():
83101 print ('Usage: wol.py [hostname]' )
84102
85103
86104
87105if __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