Skip to content

Commit

Permalink
Merge branch 'jNullj-config-path-update'
Browse files Browse the repository at this point in the history
Closes !10
  • Loading branch information
bentasker committed Aug 23, 2021
2 parents 09c151b + b4a31d5 commit 1f0d25c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 30 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ Configuration File

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

The configuration file is located at `~/.config/bentasker.Wake-On-Lan-Python/wol_config.ini`

The following is an example of hosts save in `wol_config.ini`

[General]
broadcast=192.168.1.255

[MyPc]
mac=00:13:0d:e4:60:61



License
--------

PSF v2, see [LICENSE](LICENSE)
PSF v2, see [LICENSE](LICENSE)
66 changes: 40 additions & 26 deletions wol.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def wake_on_lan(host):
macaddress = macaddress.replace(macaddress[2], '')
else:
raise ValueError('Incorrect MAC address format')

# Pad the synchronization stream.
data = ''.join(['FFFFFFFFFFFF', macaddress * 20])
send_data = b''
Expand All @@ -56,36 +56,54 @@ def wake_on_lan(host):
return True


def loadConfig():
""" Read in the Configuration file to get CDN specific settings
"""
global mydir
global myconfig
Config = configparser.ConfigParser()
Config.read(mydir+"/.wol_config.ini")
sections = Config.sections()
dict1 = {}
for section in sections:
options = Config.options(section)

sectkey = section
myconfig[sectkey] = {}


for option in options:
myconfig[sectkey][option] = Config.get(section,option)
def writeConfig(conf):
""" Write configuration file to save local settings """
global conf_path
conf.write(open(conf_path+'/wol_config.ini', 'w'))


return myconfig # Useful for testing
def loadConfig():
""" Read in the Configuration file to get CDN specific settings """
global conf_path
global myconfig
Config = configparser.ConfigParser()
# Create conf path if does not exists
if not os.path.exists(conf_path):
os.makedirs(conf_path, exist_ok=True)
# generate default config file if does not exists
if not os.path.exists(conf_path+'/wol_config.ini'):
# get broadcast ip dynamicly
local_ip = socket.gethostbyname(socket.gethostname())
local_ip = local_ip.rsplit('.', 1)
local_ip[1] = '255'
broadcast_ip = '.'.join(local_ip)
# Load default values to new conf file
Config['General'] = {'broadcast': broadcast_ip}
# two examples for devices
Config['myPC'] = {'mac': '00:2a:a0:cf:83:15'}
Config['myLaptop'] = {'mac': '00:13:0d:e4:60:61'}
writeConfig(Config) # Generate default conf file
Config.read(conf_path+"/wol_config.ini")
sections = Config.sections()
dict1 = {}
for section in sections:
options = Config.options(section)

sectkey = section
myconfig[sectkey] = {}

for option in options:
myconfig[sectkey][option] = Config.get(section, option)

return myconfig # Useful for testing

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



if __name__ == '__main__':
mydir = os.path.dirname(os.path.abspath(__file__))
conf_path = os.path.expanduser('~/.config/bentasker.Wake-On-Lan-Python')
conf = loadConfig()
try:
# Use macaddresses with any seperators.
Expand All @@ -102,7 +120,3 @@ def usage():
print('Magic packet should be winging its way')
except:
usage()




0 comments on commit 1f0d25c

Please sign in to comment.