From 9ec3e015b27907873f1c4803f1b8c1babf7e6dc5 Mon Sep 17 00:00:00 2001
From: Edward d'Auvergne <edward@nmr-relax.com>
Date: Wed, 13 Nov 2019 14:01:19 +0100
Subject: [PATCH] Catalogs: Modification of the configuration file parsing.

The code is now a function in the 'catalog' module and checks for the existence
of the file are now performed.
---
 catalog/catalog.py        | 15 ++++++++++++++-
 catalog/update-catalog.py |  7 ++-----
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/catalog/catalog.py b/catalog/catalog.py
index 2fea611..003bb03 100644
--- a/catalog/catalog.py
+++ b/catalog/catalog.py
@@ -6,7 +6,7 @@ from fnmatch import fnmatch, translate
 import lxml.etree as ET
 import os
 from os.path import exists, join, relpath
-from os import walk
+from os import F_OK, access, walk
 import re
 import sgprops
 import sys
@@ -393,3 +393,16 @@ def fetch_zip_exclude_list(name, path, exclude_path):
 
     # Return the list.
     return blacklist
+
+
+def parse_config_file(parser=None, file_name=None):
+    """Test and parse the catalog configuration file."""
+
+    # Check for the file.
+    if not access(file_name, F_OK):
+        print("CatalogError: The catalog configuration file '%s' cannot be found." % file_name)
+        sys.exit(1)
+
+    # Parse the XML and return the root node.
+    config = ET.parse(file_name, parser)
+    return config.getroot()
diff --git a/catalog/update-catalog.py b/catalog/update-catalog.py
index a263b70..fdce1c0 100755
--- a/catalog/update-catalog.py
+++ b/catalog/update-catalog.py
@@ -13,7 +13,7 @@ import sgprops
 import sys
 import catalogTags
 import catalog
-from catalog import make_aircraft_node, make_aircraft_zip
+from catalog import make_aircraft_node, make_aircraft_zip, parse_config_file
 
 
 CATALOG_VERSION = 4
@@ -214,10 +214,7 @@ if not os.path.isdir(args.dir):
     exit(0)
 
 parser = ET.XMLParser(remove_blank_text=True)
-
-config_file = os.path.join(args.dir, 'catalog.config.xml')
-config = ET.parse(config_file, parser)
-config_node = config.getroot()
+config_node = parse_config_file(parser=parser, file_name=os.path.join(args.dir, 'catalog.config.xml'))
 
 template_file = os.path.join(args.dir, 'template.xml')
 template = ET.parse(template_file, parser)