From 9b9bf749f73bbf9324431ffd18b2fe5a74c3d997 Mon Sep 17 00:00:00 2001
From: Carlos Vega <carlos.vega@uni.lu>
Date: Tue, 6 Nov 2018 11:01:20 +0100
Subject: [PATCH] added file input control

---
 smash/import_file.py | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/smash/import_file.py b/smash/import_file.py
index 3b71e032..d3a2e78c 100644
--- a/smash/import_file.py
+++ b/smash/import_file.py
@@ -1,3 +1,4 @@
+# coding=utf-8
 import os
 import django
 from django.conf import settings
@@ -37,7 +38,7 @@ def indexof(element, l):
 
 '''
 
-# Things that *could* already be on the database:
+# Things that *could* already be on the database:
 
 - Language
 - Country
@@ -310,7 +311,7 @@ def parse_row(index, row, visit_columns, appointmentTypes, voucher_types):
     country.save()
 
     # Location and Flying Team
-    # If no FT, then default location is CHL
+    # If no FT, then default location is CHL
     ft = None
     location = None
     if not row['FLYING TEAM (FT)']:
@@ -438,7 +439,7 @@ def parse_row(index, row, visit_columns, appointmentTypes, voucher_types):
     for visit_date in set(visit_dates):
         datetime_when = visit_date.strftime('%Y-%m-%d')
 
-        #  get the indices of each occurrence of the date and use them to get
+        #  get the indices of each occurrence of the date and use them to get
         # the appointment types
         appointment_types = itembetter(
             indexof(visit_date, visit_dates), appointmentTypes)
@@ -461,7 +462,13 @@ def parse_row(index, row, visit_columns, appointmentTypes, voucher_types):
 
 if __name__ == '__main__':
     logging.basicConfig(level=logging.DEBUG)
-    file = '/Users/carlos.vega/ownCloud/Documents/Projects/PDP/copy.xlsx'
+    if len(sys.argv) < 2:
+        logging.warn('Please, execute the program as: python {} file_path.xlsx'.format(sys.argv[0]))
+        sys.exit(1)
+    file = sys.argv[1]
+    if not os.path.isfile(file):
+        logging.warn('Please, execute the program with a valid file path.')
+        sys.exit(1)
     df = pd.read_excel(file, dtype=object)
     df = df.fillna('').astype(unicode)
     df.columns = [c.upper() for c in df.columns]
@@ -471,7 +478,7 @@ if __name__ == '__main__':
         df[column] = df[column].apply(function)
     # get visits columns
     regex = re.compile(r'\(V\d\)')
-    # 
+    # 
     study = Study.objects.filter(id=GLOBAL_STUDY_ID)[0]
     #enable vouchers
     study.columns.voucher_types = True
-- 
GitLab