1
0
mirror of https://github.com/sashacmc/photo-importer.git synced 2024-11-24 08:02:14 +02:00
photo-importer/importer.py

125 lines
3.2 KiB
Python
Raw Normal View History

2018-05-11 22:10:03 +02:00
#!/usr/bin/python3
import os
import logging
import threading
import mover
2018-05-11 22:10:03 +02:00
import config
import rotator
import fileprop
2018-05-11 22:10:03 +02:00
class Importer(threading.Thread):
def __init__(self, config, input_path, output_path, dryrun):
2018-05-11 22:10:03 +02:00
threading.Thread.__init__(self)
self.__config = config
self.__input_path = input_path
self.__output_path = output_path
self.__dryrun = dryrun
self.__mov = None
2018-05-11 22:10:03 +02:00
self.__rot = None
self.__stat = {'stage': ''}
2018-05-11 22:10:03 +02:00
def run(self):
logging.info(
'Start: %s -> %s (dryrun: %s)' %
(self.__input_path, self.__output_path, self.__dryrun))
2018-05-11 22:10:03 +02:00
filenames, dirs = self.__scan_files(self.__input_path)
2018-05-11 22:10:03 +02:00
new_filenames = self.__image_filenames(self.__move_files(filenames))
2018-05-11 22:10:03 +02:00
if self.__config['main']['remove_empty_dirs']:
self.__remove_empty_dirs(dirs)
2018-05-11 22:10:03 +02:00
self.__rotate_files(new_filenames)
self.__stat['stage'] = 'done'
2018-05-11 22:10:03 +02:00
logging.info('Done')
def __scan_files(self, input_path):
self.__stat['stage'] = 'scan'
res_dir = []
2018-05-11 22:10:03 +02:00
res = []
2018-05-12 22:40:23 +02:00
for root, dirs, files in os.walk(
input_path, onerror=self.__on_walk_error):
2018-05-11 22:10:03 +02:00
for fname in files:
res.append(os.path.join(root, fname))
for dname in dirs:
res_dir.append(os.path.join(root, dname))
2018-05-11 22:10:03 +02:00
self.__stat['total'] = len(res)
res.sort()
res_dir.sort()
logging.info('Found %i files and %i dirs' % (len(res), len(res_dir)))
return res, res_dir
2018-05-11 22:10:03 +02:00
2018-05-12 22:40:23 +02:00
def __on_walk_error(self, err):
logging.error('Scan files error: %s' % err)
2018-05-11 22:10:03 +02:00
def __move_files(self, filenames):
logging.info('Moving')
self.__mov = mover.Mover(
self.__config,
self.__input_path,
self.__output_path,
filenames,
self.__dryrun)
self.__stat['stage'] = 'move'
2018-05-11 22:10:03 +02:00
res = self.__mov.run()
logging.info('Processed %s files' % len(res))
return res
2018-05-11 22:10:03 +02:00
def __image_filenames(self, move_result):
res = []
for old, new, prop in move_result:
if prop.type() == fileprop.IMAGE:
res.append(new)
return res
2018-05-11 22:10:03 +02:00
def __rotate_files(self, filenames):
logging.info('Rotating')
self.__rot = rotator.Rotator(
self.__config,
filenames,
self.__dryrun)
self.__stat['stage'] = 'rotate'
2018-05-11 22:10:03 +02:00
self.__rot.run()
def __remove_empty_dirs(self, dirs):
logging.info('Removing empty dirs')
len_dirs = reversed(sorted([(len(d), d) for d in dirs]))
for l, d in len_dirs:
try:
os.rmdir(d)
logging.info('Removed: %s', d)
except OSError:
logging.info('Skipped: %s', d)
2018-05-11 22:10:03 +02:00
def status(self):
if self.__mov:
self.__stat['move'] = self.__mov.status()
2018-05-11 22:10:03 +02:00
if self.__rot:
self.__stat['rotate'] = self.__rot.status()
2018-05-11 22:10:03 +02:00
return self.__stat
if __name__ == '__main__':
import sys
import log
log.initLogger()
imp = Importer(config.Config(), sys.argv[1], sys.argv[2])
2018-05-11 22:10:03 +02:00
imp.start()
imp.join()
print(imp.status())