Last active
April 17, 2018 08:41
-
-
Save EnriqueSoria/aeb9283b9324e243605ae50e4e145881 to your computer and use it in GitHub Desktop.
Firmes (PC)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALBARAN_PATH = "C:\\Kronos\\Albaranes" | |
LOG_PATH = "logs" | |
PHONES = [ | |
{ | |
'name' : 'Motorola MotoG5', | |
'directory': 'motog5', | |
}, { | |
'name' : 'Lenovo Tab 7', | |
'directory' : 'lenovotab7', | |
}, { | |
'name': 'BQ X Enrique', | |
'directory': 'bqxenrique', | |
}, | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tkinter import Tk, Label, Button | |
from shutil import move as mv | |
from os import listdir as ls | |
from os import path | |
from sys import exit | |
from time import gmtime, strftime | |
from threading import Timer | |
import arrow | |
import psutil | |
import conf | |
join=path.join | |
def check_process_running(processname, ending='.exe'): | |
process_list = [psutil.Process(pid).name() for pid in psutil.pids()] | |
return '{name}.exe'.format(name=processname) in process_list | |
class PhoneSelector: | |
ALBARAN_PATH = conf.ALBARAN_PATH | |
LOG_PATH = conf.LOG_PATH | |
PHONES = conf.PHONES | |
def __init__(self, master): | |
self.master = master | |
master.title('Selector de mòbil') | |
self.center(master) | |
# label | |
self.label = Label( | |
master, | |
text="Tria des de quin mòbil has firmat" | |
) | |
self.label.grid(row=0, column=0, sticky='E') | |
# buttons | |
self.buttons = [] | |
self.labels = [] | |
for i, phone in enumerate(self.PHONES): | |
self.buttons.append( | |
Button( | |
self.master, | |
text=phone['name'], | |
command= lambda directory=phone['directory']: self.do_action(directory) | |
) | |
) | |
self.buttons[-1].grid(row=i+1, column=0, sticky='WE') | |
# Labels | |
self.labels.append( | |
Label( | |
text = '{x} firmes'.format(x=0) | |
) | |
) | |
self.labels[-1].grid(row=i+1, column=1, sticky='WE') | |
Button(self.master, text='refrescar', command=self.count_files).grid(row=i+1, column=2, sticky="WE") | |
self.count_files() | |
t = Timer(5.0, self.count_files) | |
t.start() | |
def count_files(self): | |
for phone, label in zip(self.PHONES, self.labels): | |
label['text'] = '{x} firmes'.format( | |
x = len([ | |
file.replace('.jpg','') | |
for file in ls(join(self.ALBARAN_PATH, phone['directory'])) | |
if file.endswith('.jpg') | |
]) | |
) | |
t = Timer(5.0, self.count_files) | |
t.start() | |
def do_action(self, directory): | |
self.move_files(directory) | |
self.show_files(directory) | |
def show_files(self, directory): | |
def write_log(logname, text): | |
now = arrow.now('local') | |
text = "\n\n ====== {d}/{m}/{y} {h}:{mm} ==== \n".format( | |
d=now.day, m=now.month, y=now.year, h=now.hour, mm=now.minute | |
) + text + "\n" | |
with open('{l}.log'.format(l=join(self.LOG_PATH,logname)), 'a') as log: | |
log.write(text) | |
# retrieve file list | |
filelist = [file.replace('.jpg','') for file in ls(self.ALBARAN_PATH) if file.endswith('.jpg')] | |
jpg_list = "\n -- Total: {n} albarans -- \n".format(n=len(filelist)) | |
jpg_list += "\n".join(filelist) | |
# write log | |
if len(filelist)>0: | |
write_log(directory, jpg_list) | |
# create label | |
self.label2 = Label( | |
self.master, | |
text= jpg_list | |
) | |
self.label2.grid(row=len(self.PHONES)+2, column=0) | |
# create quit button | |
self.button_quit = Button( | |
self.master, | |
text = "Continuar", | |
command = exit | |
) | |
self.button_quit.grid(row=len(self.PHONES)+3, column=0) | |
def move_files(self, directory): | |
basepath = join(self.ALBARAN_PATH,directory) | |
for filename in ls(basepath): | |
if filename.endswith('.jpg'): | |
mv( | |
join(basepath, filename), | |
join(self.ALBARAN_PATH, filename) | |
) | |
def center(self, toplevel): | |
toplevel.update_idletasks() | |
w = toplevel.winfo_screenwidth() | |
h = toplevel.winfo_screenheight() | |
size = tuple(int(_) for _ in toplevel.geometry().split('+')[0].split('x')) | |
x = w/2 - size[0]/2 | |
y = h/2 - size[1]/2 | |
toplevel.geometry("%dx%d+%d+%d" % (size + (x, y))) | |
if not check_process_running('syncthing'): | |
import os | |
os.startfile(r'C:\Program Files\SyncTrayzor\SyncTrayzor.exe') | |
root = Tk() | |
root.geometry("300x600") | |
my_gui = PhoneSelector(root) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment