Advertisement
Guest User

Untitled

a guest
Jan 10th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. import os
  2.  
  3. from threading import Timer
  4.  
  5. from os.path import isfile, join, exists
  6.  
  7. import shutil
  8.  
  9. import MySQLdb
  10.  
  11. import time
  12.  
  13. import datetime
  14.  
  15. import logging
  16.  
  17. logging.basicConfig(filename='C:\Users\BetaBrawler\Downloads\Informe\Informe.txt', level=logging.DEBUG,
  18.                     format='%(asctime)s:%(levelname)s:%(message)s')
  19.  
  20.  
  21.  
  22. PATH_TO_WATCH = "C:\Users\BetaBrawler\Downloads"
  23.  
  24.  
  25.  
  26. class FileReader:
  27.  
  28.     def __init__(self, path = None):
  29.        
  30.         self.path = path
  31.        
  32.         self.running = False
  33.        
  34.         self.timer = None
  35.  
  36.     def printFilesData(self, files):
  37.        
  38.         if self.path:
  39.            
  40.             timestamp = time.strftime("%d-%m-%Y-%H-%M-%S")
  41.        
  42.             destination = join(self.path, timestamp)
  43.  
  44.             for index, file in enumerate(files):
  45.  
  46.              if isfile(join(self.path, file)):
  47.  
  48.               try:
  49.            
  50.                conn = MySQLdb.connect(host= "localhost", user="root", passwd="", db="watcherservice")
  51.                cur = conn.concusor()
  52.                cur.execute("INSERT INTO detalles (timestamp) VALUES (?)", (timestamp))
  53.                
  54.     def readAndMoveFile(self, fileData):
  55.        
  56.         logging.debug("Archivo:%s%s" % (fileData["name"], fileData["data"].read()))
  57.        
  58.         #El archivo debe ser cerrado para que se mueve
  59.         fileData["data"].close()
  60.        
  61.         if not exists(fileData["dest"]):
  62.            
  63.             os.makedirs(fileData["dest"])
  64.            
  65.         try:
  66.            
  67.             shutil.move(join(self.path, fileData["name"]), fileData["dest"])
  68.            
  69.             logging.debug("Archivo \"%s\" movido  \"%s\" ." % (fileData["name"], fileData["dest"]))
  70.            
  71.         except WindowsError as e:
  72.            
  73.             logging.warning(e)
  74.  
  75.     def listFiles(self):
  76.        
  77.         if self.path:
  78.            
  79.             return [file for file in os.listdir(self.path)]
  80.        
  81.         return None
  82.  
  83.     def stopWatching(self):
  84.        
  85.         logging.warning("Not watching.")
  86.        
  87.         self.timer.cancel()
  88.  
  89.     def complete(self):
  90.        
  91.         after = self.listFiles()
  92.        
  93.         added = [file for file in after if not file in self.lock]
  94.        
  95.         self.printFilesData(added)
  96.        
  97.         self.lock = after
  98.        
  99.         self.timer = Timer(5.0, self.complete)
  100.        
  101.         self.timer.start()
  102.  
  103.     def startWatching(self):
  104.        
  105.         if(self.path):
  106.            
  107.             self.lock = self.listFiles()
  108.            
  109.             logging.debug("Directorio que se esta observando %s..." % self.path)
  110.            
  111.             self.printFilesData(self.lock)
  112.            
  113.             self.timer = Timer(5.0, self.complete)
  114.            
  115.             self.timer.start()
  116.  
  117.         else: logging.warning("Ruta sin definir.")
  118.  
  119. class Main():
  120.  
  121.     def __init__(self):
  122.        
  123.         self.reader = FileReader("C:\Users\BetaBrawler\Downloads")
  124.        
  125.         self.reader.startWatching()
  126.  
  127.  
  128. if __name__ == '__main__':
  129.    
  130.     Main()
  131.    
  132.     #Debug: Seguimiento mas acusioso
  133.     #Info: Informacion
  134.     #Warning
  135.     #Critical
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement