From 4c1a3d1c9107e246fbffb3008e257f0d4fa6f990 Mon Sep 17 00:00:00 2001
From: Chris Maffeo <cmaffeo2@illinois.edu>
Date: Wed, 19 Sep 2018 08:11:24 -0500
Subject: [PATCH] Added configuration file, reporting function and version to
 mrdna package

---
 mrdna/__init__.py  |  6 ++++++
 mrdna/config.py    | 54 ++++++++++++++++++++++++++++++++++++++++++++++
 mrdna/reporting.py | 22 +++++++++++++++++++
 3 files changed, 82 insertions(+)
 create mode 100644 mrdna/config.py
 create mode 100644 mrdna/reporting.py

diff --git a/mrdna/__init__.py b/mrdna/__init__.py
index 493251d..42ddda9 100644
--- a/mrdna/__init__.py
+++ b/mrdna/__init__.py
@@ -1,8 +1,14 @@
 import os
 
+__version__ = "0.1.dev0"
+
 _RESOURCE_DIR = os.path.join(os.path.dirname(__file__), 'resources')
 def get_resource_path(relative_path):
     return os.path.join(_RESOURCE_DIR, relative_path)
 
 from .segmentmodel import SegmentModel, SingleStrandedSegment, DoubleStrandedSegment
 from .simulate import multiresolution_simulation
+
+from .reporting import report
+
+report("mrdna.__init__")
diff --git a/mrdna/config.py b/mrdna/config.py
new file mode 100644
index 0000000..8a7862b
--- /dev/null
+++ b/mrdna/config.py
@@ -0,0 +1,54 @@
+from pathlib import Path
+import appdirs
+import json
+
+_urlbase = "bionano.physics.illinois.edu"
+
+
+""" Get configuration file for mrdna """
+_USER_CONF_DIR = Path(appdirs.user_data_dir())
+_USER_CONF_DIR.mkdir(parents=True, exist_ok=True)
+_USER_CONF = _USER_CONF_DIR / "mrdna.conf"
+
+if _USER_CONF.exists():
+    with open(_USER_CONF) as ch:
+        config = json.load(ch)
+else:
+    config = {}
+    print("""Thanks for using the multi-resolution DNA modeling package mrdna!
+
+PRIVACY STATEMENT AND USER AGREEMENT:
+
+We respect your privacy. We will never collect or share your
+information without your explicit permission.
+
+At the same time, we would like to collect anonymous statistics about
+the use of the mrdna Python package for reporting to funding agencies
+so we can continue to develop and improve these utilities.
+
+For this, we ask for your permission to have the mrdna utility send
+your IP address along with information about what part of the mrdna
+utility you are using (e.g. cadnano reader, vHelix reader, mrdna
+script) to our webserver where it will be stored in a database, along
+with geolocation data and a timestamp. Except for your IP adress, no
+personally identifiable information will be stored. You are not
+required to opt-in to use the mrdna package!
+
+If you have concerns regarding your privacy, please contact the
+Aksimentiev group and we will get back to you as soon as we can.
+mailto:cmaffeo2@illinois.edu""")
+    while True:
+        print("""
+Do you agree to allow the mrdna package can send the information
+described above to the Aksimentiev group webserver? [y/N]""")
+        response = input()
+        if response in 'y Y yes Yes YES'.split():
+            print("Permission granted")
+            config['reporting_allowed'] = True
+            break
+        elif response in 'n N no No NO'.split() + [""]:
+            print("Permission denied")
+            config['reporting_allowed'] = False
+            break
+    with open(_USER_CONF,'w') as ch:
+        json.dump(config,ch)
diff --git a/mrdna/reporting.py b/mrdna/reporting.py
new file mode 100644
index 0000000..55e3eb4
--- /dev/null
+++ b/mrdna/reporting.py
@@ -0,0 +1,22 @@
+import mrdna
+import appdirs
+from .config import config
+
+def report(data):
+    if not config['reporting_allowed']:
+        return
+        
+    try:
+        import requests
+        import concurrent.futures
+        import urllib.parse
+        def send_url(data):
+            version = urllib.parse.quote_plus(mrdna.__version__)
+            data = urllib.parse.quote_plus(data)
+            requests.get(url = "http://bionano.physics.illinois.edu/mrdna-report/{}/{}".format(version,data))
+
+        with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
+            executor.submit(send_url,data)
+
+    except:
+        pass
-- 
GitLab