From c10415d6987a26aebb5fb6baf3f46dec08df98d0 Mon Sep 17 00:00:00 2001
From: Jeremy Fee <jmfee@usgs.gov>
Date: Tue, 17 May 2016 10:37:49 -0600
Subject: [PATCH] Update location code argument validation

---
 geomagio/Controller.py        |  6 ++++--
 geomagio/edge/LocationCode.py | 31 +++++++++++++++++++++++++++++++
 geomagio/edge/__init__.py     |  2 ++
 3 files changed, 37 insertions(+), 2 deletions(-)
 create mode 100644 geomagio/edge/LocationCode.py

diff --git a/geomagio/Controller.py b/geomagio/Controller.py
index 8dcb3eaea..f84006bb8 100644
--- a/geomagio/Controller.py
+++ b/geomagio/Controller.py
@@ -566,9 +566,11 @@ def parse_args(args):
             metavar=('FROM', 'TO'),
             nargs=2)
     parser.add_argument('--locationcode',
-            choices=['R0', 'R1', 'RM', 'Q0', 'D0', 'C0'])
+            help='EDGE location code, e.g. "R0", "R1"',
+            type=edge.LocationCode)
     parser.add_argument('--outlocationcode',
-            choices=['R0', 'R1', 'RM', 'Q0', 'D0', 'C0'])
+            help='EDGE output location code (when different from --locationcode)',
+            type=edge.LocationCode)
     parser.add_argument('--interval',
             default='minute',
             choices=['hourly', 'minute', 'second'])
diff --git a/geomagio/edge/LocationCode.py b/geomagio/edge/LocationCode.py
new file mode 100644
index 000000000..c4eefa949
--- /dev/null
+++ b/geomagio/edge/LocationCode.py
@@ -0,0 +1,31 @@
+"""EDGE Location Code argument validation."""
+
+import argparse
+import re
+
+
+def LocationCode(code):
+    """EDGE Location Code argument validator.
+
+    Location Code is the last component in a channel identifier;
+    SNCL => Station, Network, Channel, Location Code
+
+    Parameters
+    ----------
+    code : str
+        the code to validate
+
+    Returns
+    -------
+    str
+        validated lcoation code.
+
+    Raises
+    ------
+    argparse.ArgumentTypeError
+        if the location code doesn't match the regular expression.
+    """
+    try:
+        return re.match('^[A-Z0-9]{2}$', code).group(0)
+    except:
+        raise argparse.ArgumentTypeError('Invalid location code, expected /^[A-Z0-9]{2}$/')
diff --git a/geomagio/edge/__init__.py b/geomagio/edge/__init__.py
index 1555f9579..ba615d36c 100644
--- a/geomagio/edge/__init__.py
+++ b/geomagio/edge/__init__.py
@@ -2,9 +2,11 @@
 """
 
 from EdgeFactory import EdgeFactory
+from LocationCode import LocationCode
 from RawInputClient import RawInputClient
 
 __all__ = [
     'EdgeFactory',
+    'LocationCode',
     'RawInputClient'
 ]
-- 
GitLab