mirror of
https://github.com/gryf/wicd.git
synced 2025-12-20 04:48:00 +01:00
* Completely reworked the gui/tray system. gui.py and edgy/dapper/tray.py are now all run from the same wicd.py file.
* Added a connection_lost_counter to prevent the wicd frontend from trying to automatically reconnect too quickly if signal strength is briefly lost. * Added some code to hopefully fix some of the dbus-related encoding problems caused by essids with weird characters. (Might still need work). * The tray/gui will now show up in the process manager under the name wicd (along with the wicd icon), instead of just python. * Added a GetCurrentInterface() method to the daemon that will eventually be used in the VPN plugin. * Fixed a possible crash caused by signal strength not being returned correctly. * Split the Wired Profile Chooser from the appGui class, so they are now called separately within wicd.py. When the profile chooser is called from the daemon, it sets a flag as well as sending a dbus signal, so the chooser will still launch if the wicd frontend isn't running yet. * Added some docstrings, comments, etc. Probably a few other small changes I'm forgetting.
This commit is contained in:
145
misc.py
145
misc.py
@@ -1,7 +1,7 @@
|
||||
''' Misc - miscellaneous functions for wicd '''
|
||||
|
||||
#pretty much useless to anyone else...
|
||||
#but if it is useful, feel free to use under the terms of the GPL
|
||||
# Pretty much useless to anyone else...
|
||||
# But if it is useful, feel free to use under the terms of the GPL
|
||||
#
|
||||
# This is released under the
|
||||
# GNU General Public License
|
||||
@@ -13,15 +13,21 @@
|
||||
|
||||
import os
|
||||
import wpath
|
||||
import locale
|
||||
import gettext
|
||||
import time
|
||||
import sys
|
||||
|
||||
if __name__ == '__main__':
|
||||
wpath.chdir(__file__)
|
||||
def Run(cmd,include_std_error=False):
|
||||
|
||||
def Run(cmd, include_std_error = False):
|
||||
''' Run a command '''
|
||||
if not include_std_error:
|
||||
f = os.popen( cmd , "r")
|
||||
return f.read()
|
||||
else:
|
||||
input,out_err = os.popen4( cmd, 'r')
|
||||
input, out_err = os.popen4( cmd, 'r')
|
||||
return out_err.read()
|
||||
|
||||
def IsValidIP(ip):
|
||||
@@ -40,13 +46,17 @@ def PromptToStartDaemon():
|
||||
print 'You need to start the daemon before using the gui or tray. Use \
|
||||
the command \'sudo /etc/init.d/wicd start\'.'
|
||||
|
||||
def RunRegex(regex,string):
|
||||
def RunRegex(regex, string):
|
||||
''' runs a regex search on a string '''
|
||||
m = regex.search(string)
|
||||
if m:
|
||||
return m.groups()[0]
|
||||
else:
|
||||
return None
|
||||
|
||||
def log(text):
|
||||
log = self.LogWriter()
|
||||
log.write(text + "\n")
|
||||
|
||||
def WriteLine(my_file, text):
|
||||
''' write a line to a file '''
|
||||
@@ -117,43 +127,43 @@ def ParseEncryption(network):
|
||||
# Loop through the lines in the template, selecting ones to use
|
||||
for x in template:
|
||||
x = x.strip("\n")
|
||||
if y>4:
|
||||
#blah blah replace stuff
|
||||
if y > 4:
|
||||
# blah blah replace stuff
|
||||
x = x.replace("$_SCAN","0")
|
||||
for t in network:
|
||||
# Don't bother if z's value is None cause it will cause errors
|
||||
if Noneify(network[t]) != None:
|
||||
x = x.replace("$_" + str(t).upper(),str(network[t]))
|
||||
x = x.replace("$_" + str(t).upper(), str(network[t]))
|
||||
z = z + "\n" + x
|
||||
y+=1
|
||||
y += 1
|
||||
# Write the data to the files
|
||||
#then chmod them so they can't be read by evil little munchkins
|
||||
# then chmod them so they can't be read by evil little munchkins
|
||||
fileness = open(wpath.networks + network["bssid"].replace(":", "").lower(),
|
||||
"w")
|
||||
os.chmod(wpath.networks + network["bssid"].replace(":","").lower(),0600)
|
||||
os.chown(wpath.networks + network["bssid"].replace(":","").lower(), 0, 0)
|
||||
os.chmod(wpath.networks + network["bssid"].replace(":", "").lower(), 0600)
|
||||
os.chown(wpath.networks + network["bssid"].replace(":", "").lower(), 0, 0)
|
||||
# We could do this above, but we'd like to permod (permission mod)
|
||||
# them before we write, so that it can't be read.
|
||||
fileness.write(z)
|
||||
fileness.close()
|
||||
|
||||
def LoadEncryptionMethods():
|
||||
''' Load encryption methods from configuration files
|
||||
''' Load encryption methods from configuration files
|
||||
|
||||
Loads all the encryption methods from the template files
|
||||
in /encryption/templates into a data structure. To be
|
||||
loaded, the template must be listed in the "active" file.
|
||||
Loads all the encryption methods from the template files
|
||||
in /encryption/templates into a data structure. To be
|
||||
loaded, the template must be listed in the "active" file.
|
||||
|
||||
'''
|
||||
encryptionTypes = {}
|
||||
types = open("encryption/templates/active","r")
|
||||
enctypes = types.readlines()
|
||||
for x in enctypes:
|
||||
'''
|
||||
encryptionTypes = {}
|
||||
types = open("encryption/templates/active","r")
|
||||
enctypes = types.readlines()
|
||||
for x in enctypes:
|
||||
# Skip some lines, we don't care who the author is/was, etc
|
||||
# we don't care about version either.
|
||||
x = x.strip("\n")
|
||||
current = open("encryption/templates/" + x,"r")
|
||||
line = current.readlines()
|
||||
x = x.strip("\n")
|
||||
current = open("encryption/templates/" + x,"r")
|
||||
line = current.readlines()
|
||||
# Get the length so we know where in the array to add data
|
||||
typeID = len(encryptionTypes)
|
||||
encryptionTypes[typeID] = {}
|
||||
@@ -165,19 +175,19 @@ def LoadEncryptionMethods():
|
||||
requiredFields = requiredFields.split(" ")
|
||||
index = -1
|
||||
for current in requiredFields:
|
||||
# The pretty names will start with an * so we can
|
||||
#seperate them with that
|
||||
# The pretty names will start with an * so we can
|
||||
# seperate them with that
|
||||
if current[0] == "*":
|
||||
# Make underscores spaces
|
||||
#and remove the *
|
||||
# and remove the *
|
||||
encryptionTypes[typeID][2][index][0] = current.replace("_",
|
||||
" ").lstrip("*")
|
||||
" ").lstrip("*")
|
||||
else:
|
||||
# Add to the list of things that are required
|
||||
# Add to the list of things that are required
|
||||
index = len(encryptionTypes[typeID][2])
|
||||
encryptionTypes[typeID][2][index] = {}
|
||||
encryptionTypes[typeID][2][index][1] = current
|
||||
return encryptionTypes
|
||||
return encryptionTypes
|
||||
|
||||
def noneToString(text):
|
||||
''' Convert None, "None", or "" to string type "None"
|
||||
@@ -189,3 +199,78 @@ def noneToString(text):
|
||||
return "None"
|
||||
else:
|
||||
return str(text)
|
||||
|
||||
def get_gettext():
|
||||
local_path = os.path.realpath(os.path.dirname(sys.argv[0])) + '/translations'
|
||||
langs = []
|
||||
lc, encoding = locale.getdefaultlocale()
|
||||
if (lc):
|
||||
langs = [lc]
|
||||
osLanguage = os.environ.get('LANGUAGE', None)
|
||||
if (osLanguage):
|
||||
langs += osLanguage.split(":")
|
||||
langs += ["en_US"]
|
||||
lang = gettext.translation('wicd', local_path, languages=langs,
|
||||
fallback = True)
|
||||
_ = lang.gettext
|
||||
return _
|
||||
|
||||
class LogWriter():
|
||||
""" A class to provide timestamped logging. """
|
||||
def __init__(self):
|
||||
self.file = open(wpath.log + 'wicd.log','a')
|
||||
self.eol = True
|
||||
self.logging_enabled = True
|
||||
|
||||
|
||||
def __del__(self):
|
||||
self.file.close()
|
||||
|
||||
|
||||
def write(self, data):
|
||||
""" Writes the data to the log with a timestamp.
|
||||
|
||||
This function handles writing of data to a log file. In order to
|
||||
handle output redirection, we need to be careful with how we
|
||||
handle the addition of timestamps. In any set of data that is
|
||||
written, we replace the newlines with a timestamp + new line,
|
||||
except for newlines that are the final character in data.
|
||||
|
||||
When a newline is the last character in data, we set a flag to
|
||||
indicate that the next write should have a timestamp prepended
|
||||
as well, which ensures that the timestamps match the time at
|
||||
which the data is written, rather than the previous write.
|
||||
|
||||
Keyword arguments:
|
||||
data -- The string to write to the log.
|
||||
|
||||
"""
|
||||
#global logging_enabled
|
||||
data = data.encode('utf-8')
|
||||
if len(data) <= 0: return
|
||||
if self.logging_enabled:
|
||||
if self.eol:
|
||||
self.file.write(self.get_time() + ' :: ')
|
||||
self.eol = False
|
||||
|
||||
if data[-1] == '\n':
|
||||
self.eol = True
|
||||
data = data[:-1]
|
||||
|
||||
self.file.write(
|
||||
data.replace('\n', '\n' + self.get_time() + ' :: '))
|
||||
if self.eol: self.file.write('\n')
|
||||
self.file.flush()
|
||||
|
||||
|
||||
def get_time(self):
|
||||
""" Return a string with the current time nicely formatted.
|
||||
|
||||
The format of the returned string is yyyy/mm/dd HH:MM:SS
|
||||
|
||||
"""
|
||||
x = time.localtime()
|
||||
return ''.join([
|
||||
str(x[0]).rjust(4,'0'), '/', str(x[1]).rjust(2,'0'), '/',
|
||||
str(x[2]).rjust(2,'0'), ' ', str(x[3]).rjust(2,'0'), ':',
|
||||
str(x[4]).rjust(2,'0'), ':', str(x[5]).rjust(2,'0')])
|
||||
Reference in New Issue
Block a user