Repository: weecology/retriever

Issue:
Implement citation lookup in Python interface
At the moment this appears to only be present in the command line interface. This change would involving moving the core code from this section of `__main__.py`: 

https://github.com/weecology/retriever/blob/0d9c16c4c874b366e6a0a64875bb4fc026187cc5/retriever/__main__.py#L64

Into a Python interface function and then calling that function from inside of `__main__.py`. The new function should return the string representation of the citation not (just) print it out.


Discussion:
@ethanwhite I will like to work on this issue. Kindly assign it to me
@ANURADHAJHA99  thank you for the interest to get this issue solved. I had started part of it.
The PR is https://github.com/weecology/retriever/pull/1422/files.
However the [CLI here](https://github.com/weecology/retriever/blob/0d9c16c4c874b366e6a0a64875bb4fc026187cc5/retriever/__main__.py#L64) should also use the interface. You can add that to the PR.

Also something that may be needed is creating a function(interface) to return the license just like we have done with the citation(in a separate PR).
 @henrykironde  so, basically i will import the script citation in CLI Module and call it according to args value and write the function to return the licence?
Import it https://github.com/weecology/retriever/blob/0d9c16c4c874b366e6a0a64875bb4fc026187cc5/retriever/__main__.py#L15


@henrykironde the function return for licence is already there, where am i following wrong, kindly guide me.

```
if args.command == 'license':
            if args.dataset is None:
                print(LICENSE)
            else:
                dataset_license = license(args.dataset)
                if dataset_license:
                    print(dataset_license)
                else:
                    print("There is no license information for {}".format(args.dataset))
            return
```
Ok first finish up the citation pr and we can move on to the license

Resolving patch:
diff --git a/retriever/__main__.py b/retriever/__main__.py
--- a/retriever/__main__.py
+++ b/retriever/__main__.py
@@ -7,12 +7,12 @@
 
 from retriever.engines import engine_list, choose_engine
 from retriever.lib.datasets import datasets, dataset_names, license
-from retriever.lib.defaults import sample_script, CITATION, SCRIPT_SEARCH_PATHS, LICENSE
+from retriever.lib.defaults import sample_script, CITATION, SCRIPT_SEARCH_PATHS, LICENSE, VERSION
 from retriever.lib.engine_tools import reset_retriever
 from retriever.lib.get_opts import parser
 from retriever.lib.install import _install
 from retriever.lib.repository import check_for_updates
-from retriever.lib.scripts import SCRIPT_LIST, reload_scripts, get_script, name_matches
+from retriever.lib.scripts import SCRIPT_LIST, reload_scripts, get_script, name_matches, get_script_citation
 from retriever.lib.create_scripts import create_package
 from retriever.lib.provenance import commit, commit_log
 
@@ -66,12 +66,9 @@ def main():
                 print("\nCitation for retriever:\n")
                 print(CITATION)
             else:
-                scripts = name_matches(script_list, args.dataset)
-                for dataset in scripts:
-                    print("\nDataset:  {}".format(dataset.name))
-                    print("Citation:   {}".format(dataset.citation))
-                    print("Description:   {}\n".format(dataset.description))
-
+                citations = get_script_citation(args.dataset)
+                for citation in citations:
+                    print("Citation:   {}".format(citation))
             return
 
         if args.command == 'license':
diff --git a/retriever/lib/__init__.py b/retriever/lib/__init__.py
--- a/retriever/lib/__init__.py
+++ b/retriever/lib/__init__.py
@@ -18,11 +18,12 @@
 from .scripts import reload_scripts
 from .scripts import get_script_upstream
 from .scripts import get_dataset_names_upstream
+from .scripts import get_script_citation
 
 __all__ = [
     'check_for_updates', 'commit', 'commit_log', 'create_package', 'datasets',
     'dataset_names', 'download', 'reload_scripts', 'reset_retriever', 'install_csv',
     'install_mysql', 'install_postgres', 'install_sqlite', 'install_msaccess',
     'install_json', 'install_xml', 'fetch', 'get_script_upstream',
-    'get_dataset_names_upstream'
+    'get_dataset_names_upstream', 'get_script_citation'
 ]
diff --git a/retriever/lib/scripts.py b/retriever/lib/scripts.py
--- a/retriever/lib/scripts.py
+++ b/retriever/lib/scripts.py
@@ -279,6 +279,19 @@ def get_script_version_upstream(dataset, repo=REPOSITORY):
     return version
 
 
+def get_script_citation(dataset=None):
+    """Get the citation list for a script"""
+    if dataset is None:
+        return [VERSION]
+    scripts = name_matches(reload_scripts(), dataset)
+    if scripts:
+        citations = []
+        for script in scripts:
+            citations.append(script.citation)
+        return citations
+    return None
+
+
 def get_dataset_names_upstream(keywords=None, licenses=None, repo=REPOSITORY):
     """Search all datasets upstream by keywords and licenses. If the keywords
     or licenses argument is passed, Github's search API is used for looking


Test patch:
diff --git a/test/test_retriever.py b/test/test_retriever.py
--- a/test/test_retriever.py
+++ b/test/test_retriever.py
@@ -491,6 +491,12 @@ def test_format_insert_value_string_complex():
     assert test_engine.format_insert_value(test_str, 'char') == test_str
 
 
+def test_get_script_citation():
+    """Test get citation of a script"""
+    cite = rt.get_script_citation("iris")
+    expected_cite = "R. A. Fisher. 1936."
+    assert expected_cite.lower() in cite[0].lower()
+
 def test_getmd5_lines():
     """Test md5 sum calculation given a line."""
     lines = ['a,b,c', '1,2,3', '4,5,6']