Repository: coecms/ARCCSSive

Issue:
Make compare_ESGF questions case-insensitive
Currently compare_ESGF responses must be a capital `Y`. Make this case-insensitive, possibly also so that it works with 'yes'

Discussion:

Patch:
diff --git a/.travis.yml b/.travis.yml
index 9f1999b..18c4269 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,5 @@
 language: python
 python:
-    - '2.6'
     - '2.7'
     - '3.4'
 install:
diff --git a/ARCCSSive/cli/compare_ESGF.py b/ARCCSSive/cli/compare_ESGF.py
index 7d343f6..b74610c 100644
--- a/ARCCSSive/cli/compare_ESGF.py
+++ b/ARCCSSive/cli/compare_ESGF.py
@@ -25,6 +25,16 @@ from ARCCSSive.CMIP5.pyesgf_functions import ESGFSearch
 from ARCCSSive.CMIP5.other_functions import assign_mips, combine_constraints 
 from ARCCSSive.CMIP5.compare_helpers import *
 import sys
+from six.moves import input
+from distutils.util import strtobool
+
+def query_yes_no(prompt):
+    answer = input(prompt)
+    try:
+        return strtobool(answer)
+    except ValueError:
+        print("Please answer Y or N")
+        return query_yes_no(prompt)
 
 def parse_input():
     ''' Parse input arguments '''
@@ -148,11 +158,8 @@ def main():
                 print("Nothing currently available on ESGF nodes and no local version exists for constraints:\n",constraints,"and variables:",variables)
         else:
             print(esgf.ds_count(),"instances were found on ESGF and ",outputs.count()," on the local database")
-            if sys.version_info < ( 3, 0 ):
-                request=raw_input("Do you want to proceed with comparison (Y) or write current results (N) ? Y/N \n")
-            else:
-                request=input("Do you want to proceed with comparison (Y) or write current results (N) ? Y/N \n")
-            if request == "Y":
+            request = query_yes_no("Do you want to proceed with comparison (Y) or write current results (N) ? Y/N \n")
+            if request:
                 esgf_results, db_results=compare_instances(cmip5.session, esgf_results, db_results, orig_args.keys(), admin)
 
     # build table to summarise results
@@ -184,11 +191,8 @@ def main():
                         print(s.split("'")[0])
                         fout.writelines("'" +s + "'\n")
                     fout.close()
-                    if sys.version_info < ( 3, 0 ):
-                        request2=raw_input("submit a request to download these files? Y/N \n")
-                    else:
-                        request2=input("submit a request to download these files? Y/N \n")
-                    if request2 == "Y": os.system ("cp %s %s" % (outfile, outdir+outfile)) 
+                    request2 = query_yes_no("submit a request to download these files? Y/N \n")
+                    if request2: os.system ("cp %s %s" % (outfile, outdir+outfile)) 
         for var in variables:
             remote=[ds for ds in esgf_results if ds['variable']==var]
             local=[v for v in db_results if v.variable.__dict__['variable']==var]
diff --git a/conda/meta.yaml b/conda/meta.yaml
index 500d3f2..38bf6ef 100644
--- a/conda/meta.yaml
+++ b/conda/meta.yaml
@@ -29,9 +29,11 @@ requirements:
 test:
     source_files:
         - setup.cfg
+        - conftest.py
         - tests
     requires:
         - pytest
+        - mock # [py2k]
     commands:
         - py.test
         - search_replica -h


Test patch:
diff --git a/conftest.py b/conftest.py
index 57150ac..9495faf 100644
--- a/conftest.py
+++ b/conftest.py
@@ -11,3 +11,4 @@ if sys.version_info >= (3,0):
     collect_ignore.append('ARCCSSive/CMIP5/pyesgf_functions.py')
     collect_ignore.append('ARCCSSive/CMIP5/compare_helpers.py')
     collect_ignore.append('ARCCSSive/cli/compare_ESGF.py')
+    collect_ignore.append('tests/cli/test_compare_ESGF.py')
diff --git a/tests/cli/test_compare_ESGF.py b/tests/cli/test_compare_ESGF.py
new file mode 100644
index 0000000..fd046c7
--- /dev/null
+++ b/tests/cli/test_compare_ESGF.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+# Copyright 2018 ARC Centre of Excellence for Climate Systems Science
+# author: Scott Wales <scott.wales@unimelb.edu.au>
+# 
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+# 
+#     http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+from __future__ import print_function
+from ARCCSSive.cli.compare_ESGF import *
+import mock
+
+def sample_query_yes_no(answer, value):
+    with mock.patch('ARCCSSive.cli.compare_ESGF.input', return_value=answer) as inp:
+        assert query_yes_no("") == value
+
+def test_query_yes_no():
+    sample_query_yes_no("Y", True)
+    sample_query_yes_no("y", True)
+    sample_query_yes_no("yes", True)
+    sample_query_yes_no("yEs", True)
+
+    sample_query_yes_no("N", False)
+    sample_query_yes_no("n", False)
+    sample_query_yes_no("no", False)
+    sample_query_yes_no("nO", False)