Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from lxml import etree, html
- from pathlib import Path
- from threading import Thread
- from multiprocessing import Process
- from multiprocessing.dummy import Pool as ThreadPool
- import os, shutil, logging, random, time
- logger = logging.getLogger()
- logger.setLevel(logging.DEBUG)
- formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
- fh = logging.FileHandler('log.txt')
- fh.setLevel(logging.DEBUG)
- fh.setFormatter(formatter)
- logger.addHandler(fh)
- ch = logging.StreamHandler()
- ch.setLevel(logging.DEBUG)
- ch.setFormatter(formatter)
- logger.addHandler(ch)
- logger.info('----------------------------')
- logger.info('|--The program is started--|')
- logger.info('----------------------------')
- start_time = time.time()
- class PhotoBookSort(object):
- def __init__(self, source_folder, destination_dir, path_to_xml_file, name_xml_file):
- self.source_folder = source_folder
- self.destination_dir = destination_dir
- self.path_to_xml_file = path_to_xml_file
- self.name_xml_file = name_xml_file
- def parsing_xml(self, path_to_xml_file, name_xml_file):
- try:
- with open(os.path.join(path_to_xml_file, name_xml_file), 'r') as f:
- xml = f.read().encode('utf-8')
- except IOError as e:
- logger.error('Directory {s} do not have a {f} file.'.format(s=path_to_xml_file, f=name_xml_file))
- else:
- list_from_xml = etree.XML(xml)
- index = 0
- for i in list_from_xml:
- if list_from_xml[index].tag == 'ProductID':
- p_id = list_from_xml[index].text.replace(' ','')
- index += 1
- return p_id
- def get_part_for_comparison(self, string):
- splitter = []
- for i in string:
- if i == '_':
- break
- splitter.append(i)
- compare = ''.join(splitter)
- return compare
- def create_destination_directory(self, prod_id, destination_dir):
- if prod_id in os.listdir(destination_dir):
- logger.info('Directory {s} already exist.'.format(s=prod_id))
- else:
- logger.info('Directory {s} is missing. Create...'.format(s=prod_id))
- os.mkdir(prod_id, mode=0o777)
- logger.info('Directory {s} created.'.format(s=prod_id))
- def moving_orders(self, element, source_dir, destination_dir, name_xml_file):
- path = os.path.join(source_dir, element)
- if self.get_part_for_comparison(element) == 'PhotoBook':
- if os.listdir(path):
- if not os.path.exists(os.path.join(path, name_xml_file)):
- pass
- prod_id = self.parsing_xml(path, name_xml_file)
- os.chdir(destination_dir)
- self.create_destination_directory(prod_id, destination_dir)
- shutil.move(os.path.join(source_dir, element), os.path.join(destination_dir, prod_id))
- logger.info('Order {s} is moved'.format(s=element))
- else:
- logger.warning('Directory {s} is empty.'.format(s=path))
- def run(self, source_dir, destination_dir, name_xml_file):
- for d, dirs, files in os.walk(source_dir):
- for i in dirs:
- self.moving_orders(i, source_dir, destination_dir, name_xml_file)
- name_xml_file = 'PDF.xml'
- home_dir = str(Path.home())
- app_folder = 'cur'
- sorted_folder = 'sorted'
- source_folder = 'test'
- xmls_source = 'xmls_source'
- source_dir = os.path.join(home_dir, app_folder, source_folder)
- destination_dir = os.path.join(home_dir, app_folder,sorted_folder)
- xml_source = os.path.join(home_dir, app_folder, xmls_source)
- path_to_xml_file = os.path.join(home_dir, app_folder)
- if __name__ == '__main__':
- pbs = PhotoBookSort(source_folder, destination_dir, path_to_xml_file, name_xml_file)
- pbs.run(source_dir, destination_dir, name_xml_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement