Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make code work on modern Python systems #4

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 45 additions & 35 deletions mkwvconf.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
#!/usr/bin/python2
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import string
import os
from xml import xpath
from xml.dom.minidom import parse

try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET

try:
raw_input
except NameError:
raw_input = input

try:
from string import atoi
except ImportError:
atoi = int

class Mkwvconf:

Expand Down Expand Up @@ -34,24 +46,24 @@ class Mkwvconf:
#########

def __init__(self):
self.doc = parse(self.xmlPath)
self.doc = ET.parse(self.xmlPath)

def displayIntro(self):
os.system('clear')
print self.introMessage
print(self.introMessage)

def getCountryCodes(self):
"""returns a list of all country codes"""
return [ str(n.value) for n in self.getNodesFromXml("country/@code") ]
return [ str(n.get('code')) for n in self.getNodesFromXml("country[@code]") ]


def selectCountryCode(self):
"""lets user choose a country code and returns the chosen value"""

l = self.getCountryCodes()

print "\nAvailable country codes:\n"
print l
print("\nAvailable country codes:\n")
print(l)

country = ""

Expand All @@ -62,12 +74,12 @@ def selectCountryCode(self):

def getNodesFromXml(self, xquery):
"""returns results of xquery as a list"""
return xpath.Evaluate(xquery, self.doc.documentElement)
return self.doc.findall(xquery)

def getProviders(self, countryCode):
"""returns list of providers for countryCode"""
nodes = self.getNodesFromXml('country[@code=\'' + countryCode + '\']/provider/name')
return [ n.firstChild.nodeValue for n in nodes ]
return [ n.text for n in nodes ]

def selectProvider(self, countryCode):
"""lets user choose a provider and returns the chosen provider name"""
Expand All @@ -79,9 +91,9 @@ def selectProvider(self, countryCode):

def selectApn(self, node):
"""takes a provider node, lets user select one apn (if several exist) and returns the chosen node"""
apnNode = node.getElementsByTagName("apn")[0]
apns = node.getElementsByTagName("apn")
apnnames = [ n.getAttribute("value") for n in apns ]
apnNode = node.findall("*/apn")[0]
apns = node.findall("*/apn")
apnnames = [ n.get("value") for n in apns ]

apncount = len(apns)
if apncount == 1:
Expand All @@ -104,13 +116,13 @@ def makeConfig(self, countryCode, provider):
if editConf in ["", "Y", "y"]:
self.writeConfig(parameters)
else:
print "\n\nDone. Insert the following into " + self.configPath + " and run 'wvdial " + parameters["profileName"] + "' to start the connection.\n\n"
print self.formatConfig(parameters)
print("\n\nDone. Insert the following into " + self.configPath + " and run 'wvdial " + parameters["profileName"] + "' to start the connection.\n\n")
print(self.formatConfig(parameters))

def writeConfig(self, parameters):
"""append or replace the configuration section to wvdial.conf"""
if not os.path.exists(self.configPath):
print "\nWarning: " + self.configPath + " doesn't exist, creating new file."
print("\nWarning: " + self.configPath + " doesn't exist, creating new file.")
f = open(self.configPath, 'w')
f.close()

Expand All @@ -123,11 +135,11 @@ def writeConfig(self, parameters):
snippetStart = text.find("[Dialer %(profileName)s]" % parameters)
if snippetStart != -1:
snippetEnd = text.find("[Dialer ", snippetStart+1)
print "\nThe following part of wvdial.conf will be replaced: \n\n" + text[snippetStart:snippetEnd]
print "by: \n\n" + section
print("\nThe following part of wvdial.conf will be replaced: \n\n" + text[snippetStart:snippetEnd])
print("by: \n\n" + section)
text = text.replace(text[snippetStart:snippetEnd], section)
else:
print "\nThe following will be appended to wvdial.conf: \n\n" + section
print("\nThe following will be appended to wvdial.conf: \n\n" + section)
text += "\n" + section

editConf = raw_input("Write to file? Y/n: ")
Expand All @@ -136,7 +148,7 @@ def writeConfig(self, parameters):
f.write(text)
f.close()

print "wvdial.conf edited successfully, run 'wvdial " + parameters["profileName"] + "' to start the connection.\n\n"
print("wvdial.conf edited successfully, run 'wvdial " + parameters["profileName"] + "' to start the connection.\n\n")

def formatConfig(self, parameters):
"""formats the information contained in parameters into a valid wvdial.conf format"""
Expand Down Expand Up @@ -177,24 +189,24 @@ def getModemDevice(self):
def getUserChoice(self, l, header, prompt):
"""takes a string list, a text prompt and a header, and returns user choice"""

print
print header
print
print('')
print(header)
print('')

count = len(l)
for k, v in zip(range(count), l):
print str(k) + ": " + v
print(str(k) + ": " + v)

choice = -1
while choice >= count or choice < 0:
inputStr = self.getUserInput(prompt + " [0-" + str(count - 1) + "]:")
try:
choice = string.atoi(inputStr)
choice = atoi(inputStr)
if choice < 0 or choice >= count:
print "Input needs to be between 0 and " + str(count - 1)
print("Input needs to be between 0 and " + str(count - 1))
except:
choice = -1
print "Input needs to be an integer."
print("Input needs to be an integer.")

return int(choice)

Expand All @@ -213,17 +225,15 @@ def parseProviderNode(self, apnNode):
"""return initially filled parameter dictionary from provider xml node"""
parameters = {}

apn = apnNode.getAttribute("value")
apn = apnNode.get("value")
parameters["apn"] = apn

usrNodes = apnNode.getElementsByTagName("username")
if len(usrNodes) != 0:
usr = usrNodes[0].firstChild.nodeValue
usr = apnNode.findtext("username")
if usr:
parameters["usr"] = usr

pwNodes = apnNode.getElementsByTagName("password")
if len(pwNodes) != 0:
pw = pwNodes[0].firstChild.nodeValue
pw = apnNode.findtext("password")
if pw:
parameters["pw"] = pw

return parameters
Expand Down