def main() : """ Get the command line arguments """ p = optparse.OptionParser() p.add_option("-d", action="store_true", dest="debug") p.add_option("--debug", action="store_true", dest="debug") p.add_option("--config_file", action="store", dest="config_file") p.add_option("--test_parse", action="store_true", dest="test_parse") p.set_defaults(debug = False) opts, source_file_args = p.parse_args() try : # Config File is mandatory if not opts.config_file : raise ParseError("No Config file") # # Upload the configs # Config(opts.config_file) pattern_spec = DTPOParseSpec(Config.config.get_pattern_file()) except DTPOFileError as file_error: dtpo_alert(log_type = 'fatal', reason = file_error.message) raise SystemExit("FATAL ERROR - Failed to parse config file") except ParseError as parse_error : dtpo_alert('fatal', reason = parse_error.message) raise SystemExit("FATAL ERROR - Failed to parse pattern file") # # Now iterate through the files # for source_file in source_file_args: dtpo_log('info', "Started processing -> %s", source_file) try : # TODO - we're assuming PDF files here # Check that the file name actually ends in # pdf if not rename it as it will save trouble with DTPO later suffix = source_file[-3:] if suffix.lower() != 'pdf' : dtpo_log('debug', "Adding pdf suffix on to '%s'", source_file) source_dir = Config.config.get_source_directory() + '/' os.rename(source_dir + source_file, source_dir + source_file + '.pdf') source_file += '.pdf' # # Convert the file to text if we can and then parse it # import_details = get_import_parameters(source_file, pattern_spec, opts.test_parse) if opts.test_parse : import_details.print_import_details(source_file) else : execute_import(import_details) trash_file(source_file, import_details.get_document_name()) dtpo_alert('info', file_name = import_details.get_document_name(), group_name = import_details.group) except DTPOFileError as file_error : # We failed ... Leave the file be as there is a problem with it dtpo_log('error', "Import failed for '%s' - file not touched\n%s", basename(source_file), file_error.message) dtpo_alert('fatal', reason = file_error.message, file_name = source_file) except ParseError as parse_error : # We failed ... Move the file to the Orphan directory dtpo_log('error', "Import failed for '%s' - orphaning file\n%s", basename(source_file), parse_error.message) dtpo_alert('error', reason = parse_error.message, file_name = source_file) orphan_file(source_file) except Exception as exception : # Something horrible has happend dtpo_log('fatal', "System error for '%s'\n%s", basename(source_file), str(exception)) dtpo_alert('fatal', reason = str(exception), file_name = source_file) dtpo_log('debug', 'Completed Successfully')
def execute_import(import_parameters) : """ Now run the actual import into DTPO """ assert import_parameters.source_file assert import_parameters.file_type assert import_parameters.mime_type assert import_parameters.group assert import_parameters.tags source_file = import_parameters.source_file database = Config.config.get_database_directory() + '/' + \ import_parameters.database document_name = import_parameters.get_document_name() dtpo_log('info', "execute_import source file -> %s", source_file) dtpo_log('info', "execute_import database -> %s", database) dtpo_log('info', "execute_import group -> %s", import_parameters.group) dtpo_log('info', "execute_import tags -> %s", import_parameters.tags) dtpo_log('info', "execute_import document name -> %s", document_name) try : try : # First see if the relevant database is open already dtpo_db_id = None dt = app(u'DEVONthink Pro') for dtpo_db in dt.databases.get() : if dtpo_db.path() == database : dtpo_db_id = dtpo_db.id() break if dtpo_db_id is None : dtpo_db = app(u'DEVONthink Pro').open_database(database) dtpo_db_id = dtpo_db.id() except AttributeError as attribute_error : message = "Failed to open database {0} -> {1}".format( import_parameters.database, str(attribute_error)) raise ParseError(message) try : dtpo_group = app(u'DEVONthink Pro').create_location( import_parameters.group, in_=app.databases.ID(dtpo_db_id)) # get the group to check that it's there dtpo_group_id = dtpo_group.id() #pylint: disable-msg=W0612 except AttributeError as attribute_error : message = "Failed access group {0} -> {1}".format( import_parameters.group, str(attribute_error)) raise ParseError(message) try : doc = app(u'DEVONthink Pro').import_( import_parameters.source_file, name = document_name, to = dtpo_group) docid = doc.id() except AttributeError as attribute_error : message = "Failed import document {0} -> {1}".format( document_name, str(attribute_error)) raise ParseError(message) try : app(u'DEVONthink Pro').databases.ID( dtpo_db_id).contents.ID(docid).unread.set(True) app(u'DEVONthink Pro').databases.ID( dtpo_db_id).contents.ID(docid).tags.set(import_parameters.tags) app(u'DEVONthink Pro').databases.ID( dtpo_db_id).contents.ID(docid).URL.set('') duplicate = app(u'DEVONthink Pro').databases.ID( dtpo_db_id).contents.ID(docid).number_of_duplicates.get() if int(duplicate) > 0 : dtpo_alert('warn', reason = '{0} duplicates of '\ .format(duplicate), file_name = document_name) except AttributeError as attribute_error : message = "Failed set attributes {0} -> {1}".format( import_parameters.get_document_name(), str(attribute_error)) raise ParseError(message) except ParseError as parse_error: raise parse_error except Exception as exception : ex_type = type(exception) message = "Unexpected exception {0} -> {1}".format( ex_type, str(exception)) raise Exception(message) return True