def _serialize_csv_fields(self, fields, logger=DEFAULT_LOGGER): """ Serialize fields to CSV file record """ field_id = fields[constants.CSV_FIELD_ID] # List of conflicted document revisions field_revisions = "; ".join(fields[constants.CSV_FIELD_REVISIONS]) # Write CSV row try: self._csv_file_writer.writerow({ constants.CSV_FIELD_ID: field_id, constants.CSV_FIELD_NAME: fields[constants.CSV_FIELD_NAME], constants.CSV_FIELD_CONFLICTS: fields[constants.CSV_FIELD_CONFLICTS], constants.CSV_FIELD_DELETED: fields[constants.CSV_FIELD_DELETED], constants.CSV_FIELD_REVISIONS: field_revisions }) except ValueError as err: message = "Failed to write CSV row to file: %s. Document ID: %s." logger.error(message, self._csv_file, field_id) error_util.log_exception(logger, err)
def create_text_file(file, content, logger=DEFAULT_LOGGER): # pylint: disable=unused-variable """ Create the results summary text file """ logger.info("Creating text file: %s...", file) # Write summary file content try: with open(file, "w", newline="", encoding="utf-8", errors="strict") as file_handle: file_handle.write(content) except ValueError as err: logger.error("Failed to create text file: %s.", file) error_util.log_exception(logger, err) return False except OSError as err: logger.error("Failed to create text file: %s.", file) error_util.log_exception(logger, err) return False logger.info("Successfully created text file: %s.", file) return True
def init_client(self, logger=DEFAULT_LOGGER): """ Open the Cloudant account connection """ logger.info( "Establishing a connection with the Cloudant account: %s...", self._account) try: self._client = Cloudant(cloudant_user=self._api_key, auth_token=self._password, account=self._account, connect=True) except requests.exceptions.HTTPError as err: logger.error( "Failed to establish a connection with the Cloudant account: %s.", self._account) error_util.log_exception(logger, err) return False logger.info( "Successfully established a connection with the Cloudant account: %s.", self._account) return True
def delete_document(self, document_id, logger=DEFAULT_LOGGER): """ Delete the Cloudant document by ID """ logger.info("Deleting Cloudant document: %s...", document_id) if self._database is None: message = "Failed to delete Cloudant document: {0}. " \ "Database connection is closed: {1}.".format(document_id, self._database_name) logger.error(message) return None # Fetch document start_time = datetime.datetime.now() document = self.get_document(document_id, logger) if not document: message = "Failed to delete Cloudant document: {0}. " \ "The document is undefined.".format(document_id) logger.error(message) return None # Delete document try: document.delete() except CloudantDocumentException as err: message = "Failed to delete Cloudant document: {0}. " \ "The document revision ID is missing.".format(document_id) logger.error(message) error_util.log_exception(logger, err) return None except HTTPError as err: message = "Failed to delete Cloudant document: {0}.".format( document_id) logger.error(message) error_util.log_http_error(logger, err) return None end_time = datetime.datetime.now() elapsed_time = (end_time - start_time).total_seconds() * 1000 # ms logger.info("Successfully deleted Cloudant document: %s (%d ms).", document_id, elapsed_time) return document
def get_document(self, document_id, logger=DEFAULT_LOGGER): """ Retrieve the Cloudant document by ID (latest revision) """ logger.debug("Retrieving Cloudant document: %s...", document_id) if self._database is None: message = "Failed to retrieve Cloudant document: {0}. " \ "Database connection is closed: {1}.".format(document_id, self._database_name) logger.error(message) return None start_time = datetime.datetime.now() try: document = self._database[document_id] except KeyError as err: message = "Failed to retrieve Cloudant document: {0}.".format( document_id) logger.error(message) error_util.log_exception(logger, err) return None except HTTPError as err: message = "Failed to retrieve Cloudant document: {0}.".format( document_id) logger.error(message) error_util.log_http_error(logger, err) return None if not document: message = "Failed to retrieve Cloudant document: {0}. " \ "The document is undefined.".format(document_id) logger.error(message) return None end_time = datetime.datetime.now() elapsed_time = (end_time - start_time).total_seconds() * 1000 # ms logger.debug("Successfully retrieved Cloudant document: %s (%d ms).", document_id, elapsed_time) if logger_util.is_enabled_for_trace(logger): formatted_document = json.dumps( document, indent=constants.JSON_FORMAT_INDENT) logger_util.log_trace(logger, formatted_document) return document
def open_database(self, logger=DEFAULT_LOGGER): """ Open Cloudant database """ logger.info("Opening Cloudant database: %s...", self._database_name) try: self._database = self._client[self._database_name] except requests.exceptions.HTTPError as err: logger.error("Failed to open Cloudant database: %s.", self._database_name) error_util.log_exception(logger, err) return False logger.info("Successfully opened Cloudant database: %s.", self._database_name) return True
def _serialize_row(self, row, logger=DEFAULT_LOGGER): """ Serialize row to CSV file record """ field_id = row[constants.PROPERTY_ID] field_name = row[constants.PROPERTY_KEY] # Number of conflicted document revisions field_conflicts = self._get_conflicts_count(row) # Track total number of conflicted document revisions self._total_conflicted_revisions += field_conflicts # List of conflicted document revisions value = row[constants.PROPERTY_VALUE] field_revisions = "; ".join(value) # Write CSV row try: self._csv_file_writer.writerow({ constants.CSV_FIELD_ID: field_id, constants.CSV_FIELD_NAME: field_name, constants.CSV_FIELD_CONFLICTS: field_conflicts, constants.CSV_FIELD_REVISIONS: field_revisions }) except ValueError as err: message = "Failed to write CSV row to file: %s. Document ID: %s." logger.error(message, self._csv_file, field_id) error_util.log_exception(logger, err)
def list_directory(directory, logger=DEFAULT_LOGGER): # pylint: disable=unused-variable """ TODO """ path = directory if not isinstance(directory, Path): path = Path(directory) logger.info("Listing directory contents: %s ...", path) files = [] try: for file in path.iterdir(): files.append(file) file_status = file.stat() file_mode = stat.filemode(file_status.st_mode) logger.info("%s %s", file_mode, file) except FileNotFoundError as err: logger.error("Directory does not exist: %s.", path) logger.error("Failed to list directory contents: %s.", path) error_util.log_exception(logger, err) return files file_count = len(files) logger.info("Total: %s", file_count) logger.info("Successfully listed directory contents: %s.", path) return files
def create_directory(directory, parents=False, logger=DEFAULT_LOGGER): # pylint: disable=unused-variable """ Creates a directory to store the results """ path = directory if not isinstance(directory, Path): path = Path(directory) logger.info("Creating directory: %s...", path) try: path.mkdir(parents=parents) except FileExistsError as err: logger.warning("Directory already exists: %s.", path) except OSError as err: logger.error("Failed to create directory: %s.", path) error_util.log_exception(logger, err) return False logger.info("Successfully created directory: %s.", path) return True