From 5eef7bd769f8ffb03ba396833d1b33484041a99f Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 19 May 2021 16:14:26 -0700 Subject: [PATCH] use LibraryNotInstalled exception instead of exiting via sys.exit --- jc/parsers/xml.py | 16 ++++++++-------- jc/parsers/yaml.py | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/jc/parsers/xml.py b/jc/parsers/xml.py index e662f7d4..6db0b590 100644 --- a/jc/parsers/xml.py +++ b/jc/parsers/xml.py @@ -65,19 +65,13 @@ Examples: ... } """ -import sys import jc.utils -# check if xml library is installed and fail gracefully if it is not -try: - import xmltodict -except Exception: - jc.utils.error_message('The xmltodict library is not installed.') - sys.exit(1) +from jc.exceptions import LibraryNotInstalled class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.4' + version = '1.5' description = 'XML file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -121,6 +115,12 @@ def parse(data, raw=False, quiet=False): Dictionary. Raw or processed structured data. """ + # check if xml library is installed and fail gracefully if it is not + try: + import xmltodict + except Exception: + raise LibraryNotInstalled('The xmltodict library is not installed.') + if not quiet: jc.utils.compatibility(__name__, info.compatible) diff --git a/jc/parsers/yaml.py b/jc/parsers/yaml.py index f25294e0..bfcbe85a 100644 --- a/jc/parsers/yaml.py +++ b/jc/parsers/yaml.py @@ -79,19 +79,13 @@ Examples: } ] """ -import sys import jc.utils -# check if yaml library is installed and fail gracefully if it is not -try: - from ruamel.yaml import YAML -except Exception: - jc.utils.error_message('The ruamel.yaml library is not installed.') - sys.exit(1) +from jc.exceptions import LibraryNotInstalled class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.4' + version = '1.5' description = 'YAML file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -135,6 +129,12 @@ def parse(data, raw=False, quiet=False): List of Dictionaries representing the YAML documents. """ + # check if yaml library is installed and fail gracefully if it is not + try: + from ruamel.yaml import YAML + except Exception: + raise LibraryNotInstalled('The ruamel.yaml library is not installed.') + if not quiet: jc.utils.compatibility(__name__, info.compatible)