archivos.py 3.76 KB
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5 import QtWidgets
import os
import sys
import tempfile
import subprocess

class AppThread(QThread):
    signal = pyqtSignal('PyQt_PyObject')

    def __init__(self):
        QThread.__init__(self)
        self.aplicativo = ""

    # run method gets called when we start the thread
    def run(self):
        #tmpdir = tempfile.mkdtemp()
        cmd = "python3 {0}".format(self.aplicativo)
        subprocess.check_output(cmd.split())
        # git clone done, now inform the main thread with the output
        #self.signal.emit(tmpdir)

class LanzarThread(QThread):
    signal = pyqtSignal('PyQt_PyObject')

    def __init__(self):
        QThread.__init__(self)
        self.comando = ""


    def run(self):
        #print('ejecutar ' )
        cmd = "{0}".format(self.comando)
        print('ejecutar ', cmd )
        subprocess.check_output(cmd.split())

class LanzarArchivos(QThread):
    signal = pyqtSignal('PyQt_PyObject')

    def __init__(self):
        QThread.__init__(self)
        self.comando = ""


    def run(self):
        #print('ejecutar ' )
        cmd = "{0}".format(self.comando)
        print(['xdg-open', cmd])
        subprocess.check_output(['xdg-open', cmd])


def escrivir_txt(path, texto):
    try:
        f = open(path, 'w+')
        for i in texto:
            f.write(i)
        f.close()
        return(True)
    except:
        return(False)

def leer_archivo(path):
    texto = []
    try:
        f = open(path, 'r')
        f1 = f.readlines()
        for x in f1:
            texto.append(x)
        return(texto)
    except:
        return(False)

def path():
    DEFAULT_PATH = os.path.abspath('./')
    return(DEFAULT_PATH)

def buscar_aplicativos(path):
    #print(os.listdir(path + '/'))
    folder = []
    for file in os.listdir(path + '/'):
        if file.endswith("_main.py"):
            #print(os.path.join(path, file))
            folder.append([os.path.join(path, file), file, file[:-3]])
            print([os.path.join(path, file), file, file[:-3]])
    return(folder)

#print(path('desktop/dier.py'))
def carga_dinamica():
    DEFAULT_PATH = path()
    modulos = buscar_aplicativos(DEFAULT_PATH)
    print(modulos)
    devolucion = []
    for modulo in modulos:
        modulo = modulo[2]
        #print(modulo)
        retorno = __import__(modulo)
        retorno = retorno.nombre()
        print(retorno)
        devolucion.append(retorno)
    return(devolucion)


def lanzar_aplicativo(self, app, item):
    DEFAULT_PATH = path()
    modulos = buscar_aplicativos(DEFAULT_PATH)
    for modulo in modulos:
        PY_modulo = modulo[2]
        retorno = __import__(PY_modulo)
        retorno = retorno.nombre()
        #print(retorno)
        if retorno[0] == item:
            print('lanzar ' + str(item))
            self.app_thread = AppThread()
            self.app_thread.aplicativo = modulo[0]
            self.app_thread.start()
            return(True)
    print('no se encontro aplicativo')
    return(False)

def ejecutar(self, comando):
    try:
        self.app_thread = LanzarThread()
        self.app_thread.comando = comando
        self.app_thread.start()
    except:
        return(False)

def escrivir_tuple(path, TUPLE):
    try:
        f = open(path, 'w+')
        for i in TUPLE:
            f.write(i)
            f.write('\n')

        f.close()
        return(True)
    except:
        return(False)

def leer_tuple(path):
    texto = []
    try:
        f = open(path, 'r')
        f1 = f.readlines()
        for x in f1:
            y = x[:-1]
            texto.append(y)
        return(texto)
    except:
        return(False)

def abrir_arch(self, arch):
    try:
        self.app_thread = LanzarArchivos()
        self.app_thread.comando = arch
        self.app_thread.start()
    except:
        return(False)