Skip to content

Instantly share code, notes, and snippets.

@Klowner
Last active December 16, 2015 01:49
Show Gist options
  • Save Klowner/5357482 to your computer and use it in GitHub Desktop.
Save Klowner/5357482 to your computer and use it in GitHub Desktop.
Simple Python script for converting netcfg configs over to netctl configs, probably doesn't fix all cases but it at least removes most of the grunt work
#!/usr/bin/env python3
# NOTICE if you use this on a directory, it'll function recursively, back up your files first!
import os
import sys
WORDLIST = [
'addr',
'address',
'address6',
'binds',
'connection',
'post',
'pre',
'up',
'down',
'to',
'description',
'dns',
'domain',
'search',
'essid',
'gateway',
'group',
'hostname',
'interface',
'ip',
'ip6',
'custom',
'key',
'mode',
'password',
'remote',
'routes6',
'security',
'user',
'vlanid',
'wpa',
'file',
'config',
]
REPLACE = {
'addr': 'address'
}
UPPER = [
'ip',
'essid',
'fwd',
'dns',
'dhcp',
'ip6',
'vlanid',
'wpa'
]
def fixname(name):
words = []
buff = ''
for char in name:
if char == '_':
continue
buff += char.lower()
if buff in WORDLIST:
if buff in REPLACE:
buff = REPLACE[buff]
if buff in UPPER:
words.append(buff.upper())
else:
words.append(buff.capitalize())
buff = ''
return ''.join(words) + buff if len(words) else buff.upper()
def fixvalue(value):
if ' ' in value:
return value
else:
return value.strip("'\"")
def fixfile(fn):
output = []
content = open(fn,'r').read()
for row in content.split('\n'):
if row.startswith('#'):
output.append(row)
continue
try:
(name, value) = row.split('=', 1)
output.append( '='.join((fixname(name), fixvalue(value))) )
except ValueError:
output.append(row)
result = ('\n'.join(output))
fd = open(fn, 'w')
fd.write(result)
fd.close()
def fixdir(path):
for r,d,f in os.walk(path):
for directory in d:
fixdir(directory)
for filename in f:
fixed_file = fixfile(os.path.join(r, filename))
if (len(sys.argv) > 1):
path = os.path.normpath(sys.argv[1])
if os.path.isfile(path):
fixfile(path)
else:
fixdir(path)
else:
print("Usage: %s file||path" % sys.argv[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment