Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. #-*- coding: utf-8 -*-
  2. from lxml import etree, html
  3. from pathlib import Path
  4. import os, shutil
  5. import logging
  6.  
  7. logger = logging.getLogger()
  8. logger.setLevel(logging.DEBUG)
  9. formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
  10. fh = logging.FileHandler('log.txt')
  11. fh.setLevel(logging.DEBUG)
  12. fh.setFormatter(formatter)
  13. logger.addHandler(fh)
  14. ch = logging.StreamHandler()
  15. ch.setLevel(logging.DEBUG)
  16. ch.setFormatter(formatter)
  17. logger.addHandler(ch)
  18. logger.info('-------------------')
  19. logger.info('Программа запущена.')
  20.  
  21. def parsing_xml(path_to_xml_file, name_xml_file):
  22.     try:
  23.         with open(path_to_xml_file + name_xml_file, 'r') as f:
  24.              xml = f.read().encode('utf-8')
  25.     except IOError as e:  
  26.         logger.error('В директории ' + path_to_xml_file + ' отсутствует файл ' + name_xml_file)
  27.     else:
  28.         list_from_xml = etree.XML(xml)    
  29.         index = 0
  30.         for i in list_from_xml:
  31.             if list_from_xml[index].tag == 'ProductID':
  32.                 p_id = list_from_xml[index].text.replace(' ','')
  33.             index += 1
  34.     return p_id
  35.  
  36. def get_part_for_comparison(string):
  37.     splitter = []
  38.     for i in string:
  39.         if i == '_':
  40.             break
  41.         splitter.append(i)
  42.     compare = ''.join(splitter)    
  43.     return compare
  44.  
  45. def create_destination_directory(prod_id, destination_dir):
  46.     if prod_id in os.listdir(destination_dir):
  47.         logger.info('Директория' + prod_id + ' уже есть.')
  48.     else:    
  49.         logger.info('Директории ' + prod_id + ' нет. Создаю.')
  50.         os.mkdir(prod_id, mode=0o777)
  51.         logger.info('Директория ' + prod_id + ' создана.')
  52.     pass
  53.  
  54. def main(source_dir, destination_dir, name_xml_file):
  55.     #получаем список всех директорий
  56.     for d, dirs, files in os.walk(source_dir):
  57.         for i in dirs:
  58.             path = source_dir + i + '/'
  59.             if get_part_for_comparison(i) == 'PhotoBook':                
  60.                 if os.listdir(path):
  61.                     if not os.path.exists(path + name_xml_file):
  62.                         continue
  63.                     prod_id = parsing_xml(path, name_xml_file)
  64.                     os.chdir(destination_dir)
  65.                     create_destination_directory(prod_id, destination_dir)
  66.                     shutil.move(source_dir + i, destination_dir + prod_id)                    
  67.                     logger.info('Заказ ' + i + ' перенесен!')
  68.                 else:
  69.                     logger.warning('Директория ' + path + ' пуста.')
  70.     pass
  71.  
  72. name_xml_file = 'PDF.xml'
  73. home_dir = str(Path.home())
  74. source_dir = home_dir + '/cur/test/'
  75. destination_dir = home_dir + '/cur/sorted/'
  76. main(source_dir, destination_dir, name_xml_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement