Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.95 KB | None | 0 0
  1. from lxml import etree, html
  2. from pathlib import Path
  3. from threading import Thread
  4. from multiprocessing import Process
  5. from multiprocessing.dummy import Pool as ThreadPool
  6. import os, shutil, logging, random, time
  7.  
  8.  
  9. logger = logging.getLogger()
  10. logger.setLevel(logging.DEBUG)
  11. formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
  12. fh = logging.FileHandler('log.txt')
  13. fh.setLevel(logging.DEBUG)
  14. fh.setFormatter(formatter)
  15. logger.addHandler(fh)
  16. ch = logging.StreamHandler()
  17. ch.setLevel(logging.DEBUG)
  18. ch.setFormatter(formatter)
  19. logger.addHandler(ch)
  20. logger.info('----------------------------')
  21. logger.info('|--The program is started--|')
  22. logger.info('----------------------------')
  23. start_time = time.time()
  24.  
  25.  
  26.  
  27. class PhotoBookSort(object):
  28.    
  29.     def __init__(self, source_folder, destination_dir, path_to_xml_file, name_xml_file):
  30.         self.source_folder = source_folder
  31.         self.destination_dir = destination_dir
  32.         self.path_to_xml_file = path_to_xml_file
  33.         self.name_xml_file = name_xml_file
  34.        
  35.     def parsing_xml(self, path_to_xml_file, name_xml_file):
  36.         try:
  37.             with open(os.path.join(path_to_xml_file, name_xml_file), 'r') as f:
  38.                 xml = f.read().encode('utf-8')
  39.         except IOError as e:  
  40.             logger.error('Directory {s} do not have a {f} file.'.format(s=path_to_xml_file, f=name_xml_file))
  41.         else:
  42.             list_from_xml = etree.XML(xml)    
  43.             index = 0
  44.             for i in list_from_xml:
  45.                 if list_from_xml[index].tag == 'ProductID':
  46.                     p_id = list_from_xml[index].text.replace(' ','')
  47.                 index += 1
  48.         return p_id
  49.  
  50.     def get_part_for_comparison(self, string):
  51.         splitter = []
  52.         for i in string:
  53.             if i == '_':
  54.                 break
  55.             splitter.append(i)
  56.         compare = ''.join(splitter)    
  57.         return compare
  58.  
  59.  
  60.     def create_destination_directory(self, prod_id, destination_dir):
  61.         if prod_id in os.listdir(destination_dir):
  62.             logger.info('Directory {s} already exist.'.format(s=prod_id))
  63.         else:    
  64.             logger.info('Directory {s} is missing. Create...'.format(s=prod_id))
  65.             os.mkdir(prod_id, mode=0o777)
  66.             logger.info('Directory {s} created.'.format(s=prod_id))
  67.  
  68.     def moving_orders(self, element, source_dir, destination_dir, name_xml_file):
  69.         path = os.path.join(source_dir, element)
  70.         if self.get_part_for_comparison(element) == 'PhotoBook':                
  71.             if os.listdir(path):
  72.                 if not os.path.exists(os.path.join(path, name_xml_file)):
  73.                     pass
  74.                 prod_id = self.parsing_xml(path, name_xml_file)
  75.                 os.chdir(destination_dir)
  76.                 self.create_destination_directory(prod_id, destination_dir)
  77.                 shutil.move(os.path.join(source_dir, element), os.path.join(destination_dir, prod_id))                    
  78.                 logger.info('Order {s} is moved'.format(s=element))
  79.             else:
  80.                 logger.warning('Directory {s} is empty.'.format(s=path))
  81.  
  82.  
  83.     def run(self, source_dir, destination_dir, name_xml_file):
  84.         for d, dirs, files in os.walk(source_dir):
  85.             for i in dirs:
  86.                 self.moving_orders(i, source_dir, destination_dir, name_xml_file)    
  87.            
  88.  
  89. name_xml_file = 'PDF.xml'
  90. home_dir = str(Path.home())
  91. app_folder = 'cur'
  92. sorted_folder = 'sorted'
  93. source_folder = 'test'
  94. xmls_source = 'xmls_source'
  95. source_dir = os.path.join(home_dir, app_folder, source_folder)
  96. destination_dir = os.path.join(home_dir, app_folder,sorted_folder)
  97. xml_source = os.path.join(home_dir, app_folder, xmls_source)
  98. path_to_xml_file = os.path.join(home_dir, app_folder)
  99.  
  100. if __name__ == '__main__':
  101.     pbs = PhotoBookSort(source_folder, destination_dir, path_to_xml_file, name_xml_file)
  102.     pbs.run(source_dir, destination_dir, name_xml_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement