In a fairly recent article post, I called out for some testing with the new version of
Mythbuntu Log Grabber.
On a similar note, i would like to share a bit of python knowledge gained in the venture of building the new version.
As many will tell you, the Threading module in Python is usually not one to just go diving into for many reasons (none of which I have plans to go into here). However with a little know how (and allot of documentation) I was able to make things work quite well.
Lets have a look at the full code then break it down:
import threading
class LogGrabberPostThread(threading.Thread):
def __init__(self, data_input):
self.data_input = data_input
threading.Thread.__init__(self)
def run(self):
return post_data(self.data_input)
def post_data(data_in):
""" Post data to Mythbutnu Pastebin """
try:
params={}
params['code2'] = data_in
params['paste'] = "Send"
params['remember'] = "0" #Do you want a cookie ?
params = urllib.urlencode(params)
page = urllib.urlopen("http://mythbuntu.pastebin.com/", params)
paste_url = page.url
except:
paste_url = "Timeout/Error occured while uploading data. Local Data: /tmp/mythbuntu.diag"
return paste_url
So the code here is used to post text to the Mythbuntu Pastebin. Lets start with the Thread Class:
class LogGrabberPostThread(threading.Thread):
What I have done here is provide an override of the Thread class from the Threading module. This allows us to create a thread on demand and have it call the method of choice. In this case
ipost_data.
Next step is to create the actual override methods for the Thread class.
def __init__(self, data_input):
self.data_input = data_input
threading.Thread.__init__(self)
def run(self):
return post_data(self.data_input)
Since we are overriding the class defaults we have to create the required methods
"__init___" and
"run". Essentially init allow you you send data through to the method the tread will use and create the actual thread while run, surprise, starts up the thread.
With the threads wired up this way we are able to start a custom thread and wait for its return with results to interact with a UI such as used in Mythbuntu Log Grabber.
Finally, this is the example of how I used the thread inside the Mythbutnu Log Grabber plugin. for Mythbuntu Control Centre.
...
paste_data = file.read()
get_url = LogGrabberPostThread(paste_data)
paste_url = get_url.run()
...
Hope this helps, happy coding!