<filename>dopclean.py
#!/usr/bin/python
############################
# Python script
# Author : <NAME>
# Date : 2017/11/05
# version: 0.1
#
# Clean .dop files generated by DxO Optics Pro / PhotoLab
#
# Usage:
#	./dopclean <files_list>
#	./dopclean *.dop
#	./dopclean *.*
#
############################

import sys
if sys.version_info < (2,7):
  sys.stderr.write("This script requires Python 2.7 or newer.\n")
  sys.stderr.write("Current version: " + sys.version + "\n")
  sys.stderr.flush()
  sys.exit(1)
import os, shutil

# Local global constants
VERSION = '0.1'							# Script version
AUTOMATOR=1								# Automator flag, 0:disabled, Enabled otherwise
DEBUG=1									# debug mode, 0:disabled, Enabled otherwise
LOG_FILE_ENABLED=1						# 0: Disabled, Enabled otherwise
LOG_FILE_NAME="dopcleanlog.txt"			# Redirect debug print in a log
RAW_EXT=".ARW"							# RAW file extention (can be modified)
DOP_EXT=".dop"							# Extention of .dop file generated by DxO
SEPARATOR_STR="#################"

if(AUTOMATOR==1):
	DEBUG=0				# Keep output print disabled if the script is used in automator once there is no redirection in a log file
	LOG_FILE_ENABLED=1	# To be enabled for debugging purposes

def log(file, str, writeInLog_b):
	if(writeInLog_b==1):
		try:
			file.write(str+"\n")
		except:
			pass
	if DEBUG:
		print "%s\n" % str,

def main():
	# Log File
	if(LOG_FILE_ENABLED):
		if(AUTOMATOR):
			fulllogfile=os.getcwd()+"/"+LOG_FILE_NAME		# Current dir
			#fulllogfile="~/"+LOG_FILE_NAME					# DOESN'T WORK
			fulllogfile="/Users/subwarez/"+LOG_FILE_NAME
		else:
			fulllogfile=os.getcwd()+"/"+LOG_FILE_NAME		# Current dir
		logfile = open(fulllogfile, "w")
		if DEBUG:
			print "DEBUG: log %s created" % fulllogfile
			
	log(logfile, ".dop files cleanup v%s" % VERSION,1)
	# Debug, current location
	if DEBUG:
		log(logfile,"Script executed from: %s" % os.getcwd(),1)

	# Check inputs
	if len(sys.argv) == 1:
		log(logfile, "No input file(s)",1)
		try:
			logfile.close()
		except:
			pass
		sys.exit()
	else:
		log(logfile, "%d file(s) to be processed..." % (len(sys.argv)-1),1)


	# List input files
	if DEBUG!=0:
		# List all input files
		log(logfile, "Input files list:",0)
		for file_in in sys.argv[1:]:
			if(os.path.isdir(file_in) == 1):
				log(logfile, "DEBUG: %s is dir" % file_in,0)
				log(logfile, os.listdir(file_in),0)				# TEMP, need strings
			else:
				log(logfile, "DEBUG: file_in = %s" % file_in,0)
		log(logfile, SEPARATOR_STR,1)

	# For each file
	for file_in in sys.argv[1:]:
		if(os.path.isdir(file_in) == 1):
			log(logfile, "DEBUG: %s is dir : ignore" % file_in,0)
			# TODO
			continue
		folder, fullname = os.path.split(file_in)
		name, ext = os.path.splitext(fullname)
		
		if(ext != DOP_EXT):
			log(logfile, "Not a dop file => ignore",0)
			log(logfile, SEPARATOR_STR,0)
			continue
		else:
			log(logfile, "DEBUG: file_in = %s" % file_in,0)
			log(logfile, "DEBUG: folder = %s" % folder,0)
			log(logfile, "DEBUG: base = %s" % fullname,0)
			log(logfile, "DEBUG: name = %s" % name,0)
			log(logfile, "DEBUG: ext = %s" % ext,0)

			# Get the parent file name (RAW)
			subname, subext = os.path.splitext(name)
			log(logfile, "DEBUG: subname = %s" % subname,0)
			log(logfile, "DEBUG: subext = %s" % subext,0)
			
			if (os.path.isfile(folder+"/"+name) == 0):
				os.remove(folder+"/"+fullname)
				log(logfile, (folder+"/"+fullname+" removed"),1)
			else:
				log(logfile, "RAW file exist => ignore",0)
		log(logfile, SEPARATOR_STR,0)
	
	# Close log file
	if(LOG_FILE_NAME):
		logfile.close()
 
if __name__ == "__main__":
	main()