| @@ 81-146 (lines=66) @@ | ||
| 78 | raise Invalid(_("Column '{}' is missing".format(col))) |
|
| 79 | ||
| 80 | ||
| 81 | @implementer(IDynamicAnalysisSpec, IDynamicAnalysisSpecSchema) |
|
| 82 | class DynamicAnalysisSpec(Container): |
|
| 83 | """Dynamic Analysis Specification |
|
| 84 | """ |
|
| 85 | _catalogs = [SETUP_CATALOG] |
|
| 86 | ||
| 87 | def get_workbook(self): |
|
| 88 | specs_file = self.specs_file |
|
| 89 | if not specs_file: |
|
| 90 | return None |
|
| 91 | data = StringIO(specs_file.data) |
|
| 92 | return load_workbook(data) |
|
| 93 | ||
| 94 | def get_worksheets(self): |
|
| 95 | wb = self.get_workbook() |
|
| 96 | if wb is None: |
|
| 97 | return [] |
|
| 98 | return wb.worksheets |
|
| 99 | ||
| 100 | def get_primary_sheet(self): |
|
| 101 | sheets = self.get_worksheets() |
|
| 102 | if len(sheets) == 0: |
|
| 103 | return None |
|
| 104 | return sheets[0] |
|
| 105 | ||
| 106 | def get_header(self): |
|
| 107 | header = [] |
|
| 108 | ps = self.get_primary_sheet() |
|
| 109 | if ps is None: |
|
| 110 | return header |
|
| 111 | for num, row in enumerate(ps.rows): |
|
| 112 | if num > 0: |
|
| 113 | break |
|
| 114 | header = [cell.value for cell in row] |
|
| 115 | return header |
|
| 116 | ||
| 117 | def get_specs(self): |
|
| 118 | ps = self.get_primary_sheet() |
|
| 119 | if ps is None: |
|
| 120 | return [] |
|
| 121 | keys = self.get_header() |
|
| 122 | specs = [] |
|
| 123 | ||
| 124 | def get_cell_string_value(cell): |
|
| 125 | value = cell.value |
|
| 126 | if api.is_string(value): |
|
| 127 | return value |
|
| 128 | elif value is None: |
|
| 129 | return None |
|
| 130 | return str(value) |
|
| 131 | ||
| 132 | for num, row in enumerate(ps.rows): |
|
| 133 | # skip the header |
|
| 134 | if num == 0: |
|
| 135 | continue |
|
| 136 | values = map(get_cell_string_value, row) |
|
| 137 | data = dict(zip(keys, values)) |
|
| 138 | specs.append(data) |
|
| 139 | return specs |
|
| 140 | ||
| 141 | def get_by_keyword(self): |
|
| 142 | specs = self.get_specs() |
|
| 143 | groups = defaultdict(list) |
|
| 144 | for spec in specs: |
|
| 145 | groups[spec.get("Keyword")].append(spec) |
|
| 146 | return groups |
|
| 147 | ||
| @@ 80-145 (lines=66) @@ | ||
| 77 | raise Invalid(_("Column '{}' is missing".format(col))) |
|
| 78 | ||
| 79 | ||
| 80 | @implementer(IDynamicAnalysisSpec) |
|
| 81 | class DynamicAnalysisSpec(Item): |
|
| 82 | """Dynamic Analysis Specification |
|
| 83 | """ |
|
| 84 | _catalogs = [SETUP_CATALOG] |
|
| 85 | ||
| 86 | def get_workbook(self): |
|
| 87 | specs_file = self.specs_file |
|
| 88 | if not specs_file: |
|
| 89 | return None |
|
| 90 | data = StringIO(specs_file.data) |
|
| 91 | return load_workbook(data) |
|
| 92 | ||
| 93 | def get_worksheets(self): |
|
| 94 | wb = self.get_workbook() |
|
| 95 | if wb is None: |
|
| 96 | return [] |
|
| 97 | return wb.worksheets |
|
| 98 | ||
| 99 | def get_primary_sheet(self): |
|
| 100 | sheets = self.get_worksheets() |
|
| 101 | if len(sheets) == 0: |
|
| 102 | return None |
|
| 103 | return sheets[0] |
|
| 104 | ||
| 105 | def get_header(self): |
|
| 106 | header = [] |
|
| 107 | ps = self.get_primary_sheet() |
|
| 108 | if ps is None: |
|
| 109 | return header |
|
| 110 | for num, row in enumerate(ps.rows): |
|
| 111 | if num > 0: |
|
| 112 | break |
|
| 113 | header = [cell.value for cell in row] |
|
| 114 | return header |
|
| 115 | ||
| 116 | def get_specs(self): |
|
| 117 | ps = self.get_primary_sheet() |
|
| 118 | if ps is None: |
|
| 119 | return [] |
|
| 120 | keys = self.get_header() |
|
| 121 | specs = [] |
|
| 122 | ||
| 123 | def get_cell_string_value(cell): |
|
| 124 | value = cell.value |
|
| 125 | if api.is_string(value): |
|
| 126 | return value |
|
| 127 | elif value is None: |
|
| 128 | return None |
|
| 129 | return str(value) |
|
| 130 | ||
| 131 | for num, row in enumerate(ps.rows): |
|
| 132 | # skip the header |
|
| 133 | if num == 0: |
|
| 134 | continue |
|
| 135 | values = map(get_cell_string_value, row) |
|
| 136 | data = dict(zip(keys, values)) |
|
| 137 | specs.append(data) |
|
| 138 | return specs |
|
| 139 | ||
| 140 | def get_by_keyword(self): |
|
| 141 | specs = self.get_specs() |
|
| 142 | groups = defaultdict(list) |
|
| 143 | for spec in specs: |
|
| 144 | groups[spec.get("Keyword")].append(spec) |
|
| 145 | return groups |
|
| 146 | ||