Browse Source

CLI Argument, taking params from json & multiple value of a single filter header

Akash Singh 9 months ago
parent
commit
85bd2d927a
2 changed files with 44 additions and 1 deletions
  1. 35 1
      data_mover.py
  2. 9 0
      params.json

+ 35 - 1
data_mover.py

@@ -1,3 +1,5 @@
+import sys
+import json
 import xlrd3 as xlrd
 from openpyxl import load_workbook
 
@@ -23,6 +25,7 @@ class Writer:
     def save(self):
         # Call this method to save the file once every updates are written
         self.workbook.save(self.path)
+        self.workbook.close()
 
 
 class Mover:
@@ -46,6 +49,13 @@ class Mover:
         self.lines = lines
 
     def move(self, filters={}, add_missing_columns=False):
+        """
+
+        :param filters: dictionary of filters with header as key and data as value or list of data as value
+        :param add_missing_columns: True if we need all headers from source file to destination file which are currently
+        not in destination file.
+        :return:
+        """
         source = self.read_xlsx(self.source_file_path, self.source_sheet)
         destination = self.read_xlsx(self.destination_file_path, self.destination_sheet)
         destination_wb = Writer(self.destination_file_path, self.destination_sheet)  # Writer class object used to update existing file
@@ -59,8 +69,10 @@ class Mover:
         remove_data = []  # rows not matching filter are stored here which is used later to remove data from new_data
         for filter in filters:  # iterate through the dictionary of filter and
             ind = [x.value for x in source.row(0)].index(filter)  # getting index of filter header then use the same index to check data
+            if type(filters[filter]) is not list:
+                filters[filter] = [filters[filter]]
             for row in new_data:
-                if row[ind].value != filters[filter]:  # check if data is matching with filter
+                if row[ind].value not in filters[filter]:  # check if data is matching with filter
                     remove_data.append(row)
             for row in remove_data:  # removing unmatched data from new_data list
                 new_data.remove(row)
@@ -93,3 +105,25 @@ class Mover:
                 col += 1
                 destination_wb.write(1, col, headers)
         destination_wb.save()
+
+
+def parse_json(path):
+    """
+    Takes parameter from a json file.
+    :param path:
+    :return:
+    """
+    js = json.load(open(path))
+    lines = int(js["lines"]) or 0
+    mover = Mover(js["source_file"], js["source_sheet"], js["destination_file"], js["destination_sheet"], lines)
+    if js["add_missing_columns"].lower() == "true":
+        add_missing = True
+    else:
+        add_missing = False
+    mover.move(js["filters"], add_missing)
+
+
+if __name__ == "__main__":
+    js = sys.argv[-1]
+    if ".json" in js.lower():
+        parse_json(js)

+ 9 - 0
params.json

@@ -0,0 +1,9 @@
+{
+  "source_file" : "baangt_kfz_dummy.xlsx_20200731_130507.xlsx",
+  "source_sheet" : "Output",
+  "destination_file" : "testdata_wstv_fqa.xlsx",
+  "destination_sheet" : "TC_KFZ",
+  "lines" : 0,
+  "filters" : {"TestCaseStatus" : "OK"},
+  "add_missing_columns" : "False"
+}