ExecutionThread.py :  » Business-Application » ECLiPt-Roaster » eroaster-2.2.0-0.9a » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Business Application » ECLiPt Roaster 
ECLiPt Roaster » eroaster 2.2.0 0.9a » ExecutionThread.py
"""

The external threads, which executes commands and sends the output back to the main
application.

"""

# Initialize i18n
from constants import localedir,unixname,audioinfostring,data_file_list,execution_thread_done
from tools import gettext,get_tempdir

gettext = gettext(unixname, localedir)
gettext.textdomain(unixname)
_ = gettext.gettext

import sys
import os
import log4py
import popen2

from re import sub
from string import zfill,split,find
from threading import Thread
from time import sleep
from tools import mkdirtree,get_tempdir

# this is for installations with both python-gtk versions
try:
    import pygtk
    pygtk.require('2.0')
except:
    pass

try:
    import gtk
except (RuntimeError, TypeError, NameError), detail:
    logger = log4py.Logger().get_instance()
    logger.error(_("An error occured: %s") % detail)
    sys.exit(1)

class ExecutionThread (Thread):

    def __init__(self, command, filelist, cddb_info, waitreload_lockfile, callback_function, loglevel = log4py.LOGLEVEL_NORMAL):
        Thread.__init__(self)
        self.__ExecutionThread_command = command
        self.__ExecutionThread_filelist = filelist
        self.__ExecutionThread_cddb_info = cddb_info
        self.__ExecutionThread_waitreload_lockfile = waitreload_lockfile
        self.__ExecutionThread_callback_function = callback_function
        self.__ExecutionThread_logger = log4py.Logger().get_instance(self)
        self.__ExecutionThread_logger.set_loglevel(loglevel)

    def __ExecutionThread_platform_command(self, command):
        """ Creates a cmd file on win32 platforms. Otherwise win32 won't execute some commands. """
        if (sys.platform != "win32"):
            return command
        else:
            tmp_command = "%s%stmp_command.cmd" % (get_tempdir(), os.sep)
            file = open(tmp_command, "w")
            file.write("%s\n" % command)
            file.close()
            return tmp_command

    def run(self):
        sys.stderr = sys.stdout
        for i in range(len(self.__ExecutionThread_filelist)):
            command = self.__ExecutionThread_command
            file = self.__ExecutionThread_filelist[i]
            if (file != data_file_list):
                # this is executed for audio files only (in that case the only entry of filelist is "data_file_list"
                track_number = zfill(file, 2)
                if (self.__ExecutionThread_cddb_info.has_key(file)):
                    cdtitle = sub("\/", r"\\", self.__ExecutionThread_cddb_info.get_text())
                    if (cdtitle == ""):
                        cdtitle = _("Unknown")
                    trackname = self.__ExecutionThread_cddb_info[file]
                    trackname = sub("\/", r"\\", trackname)
                    trackname = sub("\"", "'", trackname)
                else:
                    cdtitle = _("Unknown")
                    trackname = _("Unknown")
                command = sub("%n", track_number, command)
                command = sub("%c", cdtitle, command)
                command = sub("%t", trackname, command)
                filename = split(command, "\"")[1]
                directory = os.path.dirname(filename)
                if (not os.path.exists(directory)):
                    self.__ExecutionThread_logger.debug(_("Creating directory %s") % directory)
                    mkdirtree(directory)
                fileString = "%s %d of %d\n" % (audioinfostring, i + 1, len(self.__ExecutionThread_filelist))
                gtk.threads_enter()
                self.__ExecutionThread_callback_function(fileString)
                gtk.threads_leave()

            command = self.__ExecutionThread_platform_command(command)
            self.__ExecutionThread_logger.debug(_("Executing %s") % command)
            pipe = popen2.popen2(command)
            line = None
            sendreturn = gtk.FALSE
            while (line != ""):
                line = pipe[0].readline()
                if (sendreturn == gtk.TRUE):          # ignore the empty line resulting from the <CR>
                    line = None
                sendreturn = gtk.FALSE
                if (line != "") and (line != None):
                    if (find(line, "Re-load disk and hit <CR>") != -1):
                        sendreturn = gtk.TRUE
                    gtk.threads_enter()
                    self.__ExecutionThread_callback_function(line)
                    gtk.threads_leave()
                    if (sendreturn == gtk.TRUE):
                        sleep(0.5)
                        while (os.path.exists(self.__ExecutionThread_waitreload_lockfile)):
                            sleep(0.25)
                        pipe[1].write("\n")
                        pipe[1].flush()
            pipe[0].close()
            pipe[1].close()
            pipe = None
            if (sys.platform == "win32"):
                # Remove temporary file created on win32 platform
                os.remove(command)

        gtk.threads_enter()
        self.__ExecutionThread_callback_function(execution_thread_done)
        gtk.threads_leave()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.