if __name__ == '__main__': args = sys.argv[1:] if len(args) >= 1 and len(args) <= 3: command = args[0] if command == '-h': print( '\n-f [path to template] [path to file] - enter path to template to be used in formatting(e.g. resources/templates-settings.json") and path to file to be formatted' ) print('-v [path to file] - enter path to file to be analyzed') elif command == '-f': templateName = args[1] path = args[2] if FileReader.isFile(path): sourceCode = FileReader.readFile(path) lexer = Lexer(sourceCode) lexer.execute() formatter = Formatter("Formatting", lexer.getTokens(), path, templateName) result = formatter.execute() resultFile = FileReader.writeToFile(path, result) print('formatted file ' + resultFile) else: jsFiles = FileReader.getAllJsFiles(path) for file in jsFiles: sourceCode = FileReader.readFile(file) lexer = Lexer(sourceCode) lexer.execute() formatter = Formatter("Formatting", lexer.getTokens(), file, templateName)
class Join: def __init__(self): self.utils = Utils() self.filereader = FileReader() def join(self, dictionary, tableNames, const_conditions, join_conditions): database = {} visited = {} for t in tableNames: visited[t] = False database[t] = [] self.filereader.readFile(t + ".csv", database[t]) # print database Jc = self.get_join_conditions(dictionary, join_conditions) remove_attribs = [] i = 1 for t in tableNames: self.utils.spaces_rem(t) if i == 1: resultant_data = database[t] visited[t] = True schema = dictionary[t] i = 0 else: for key, value in visited.items(): if visited[key]: try: join_attribs = Jc[(t, key)] except: join_attribs = None if join_attribs: remove_attribs.append(t + '.' + join_attribs[0]) resultant_data, schema = self.join_tables( resultant_data, database[t], key, t, schema, join_attribs[1], join_attribs[0], dictionary) else: resultant_data, schema = self.join_tables( resultant_data, database[t], key, t, schema, None, None, dictionary) if const_conditions: if "=" in const_conditions: if len(const_conditions): resultant_data, schema = self.utils.rem_via_constants( resultant_data, const_conditions, schema, dictionary, tableNames) for r in remove_attribs: try: schema.remove(r) except: print("No such attribute present") return resultant_data, schema def get_join_conditions(self, dictionary, join_conditions): Jc = {} if join_conditions: for j in join_conditions: j = self.utils.spaces_rem(j) c = self.utils.parse_condition(j) if c: Jc[(c[0], c[1])] = (c[2], c[3]) Jc[(c[1], c[0])] = (c[3], c[2]) return Jc def join_tables(self, resultant_data, table_data, table1, table2, schema, r_att, t_att, dictionary): if schema: if r_att and t_att: h = {} new = [] old = [] i = schema.index(table1 + "." + r_att) for idx, row in enumerate(resultant_data): h[row[i]] = idx if t_att: i = dictionary[table2].index(table2 + "." + t_att) if resultant_data: for row in table_data: if h.has_key(row[i]): new.append(resultant_data[h[row[i]]] + row) resultant_data = new else: new = [] if resultant_data: for r in resultant_data: for t in table_data: new.append(r + t) resultant_data = new schema += dictionary[table2] return (resultant_data, schema)
class Execute: def __init__(self): self.utils = Utils() self.filereader = FileReader() self.join = Join() def run(self, dictionary, tableNames, attributes, conditions, d, aggr_func): conditions = re.sub(' ?= ?', '=', conditions) self.utils.spaces_rem(conditions) conditions = [self.utils.spaces_rem(c) for c in conditions.split(" ")] if len(conditions[0]): resultant_data = [] join_conditions = [] const_conditions = "" for c in conditions: self.utils.spaces_rem(c) RHS = "" x = c.split("=") try: RHS = x[1] RHS = self.utils.spaces_rem(RHS) except: RHS = "" if '.' in RHS: join_conditions.append(c) if '.' not in RHS: const_conditions += " " + c + " " if const_conditions: const_conditions = self.utils.spaces_rem(const_conditions) resultant_data, schema = self.join.join(dictionary, tableNames, const_conditions, join_conditions) const_conditions = self.utils.spaces_rem(const_conditions) if resultant_data: self.print_result(resultant_data, attributes, schema, d, aggr_func) elif len(tableNames) > 1: resultant_data, schema = self.join.join(dictionary, tableNames, None, None) if resultant_data: self.print_result(resultant_data, attributes, schema, d, aggr_func) else: resultant_data = [] schema = dictionary[tableNames[0]] self.filereader.readFile(tableNames[0] + ".csv", resultant_data) if resultant_data: self.print_result(resultant_data, attributes, schema, d, aggr_func) def print_result(self, resultant_data, attributes, schema, d, aggr_func): if len(d): i = 1 if attributes: for a in attributes: if i == 1: print(a, end="") i = 0 else: print("," + a, end="") print("\n") h = {} if resultant_data: for idx, row in enumerate(resultant_data): try: if h[row[schema.index(d[0])]]: continue except: h[row[schema.index(d[0])]] = idx if h: for key, value in h.items(): i = 1 for col in attributes: data = resultant_data[value] if i == 1: i = 0 print(data[schema.index(col)], end="") else: print("," + data[schema.index(col)], end="") print("\n") elif aggr_func: param = self.utils.spaces_rem(aggr_func).split('(')[0] type_of_func = self.utils.spaces_rem(param).lower() param2 = self.utils.spaces_rem(aggr_func).split('(')[1][:-1] self.utils.print_aggr(type_of_func, resultant_data, schema, self.utils.spaces_rem(param2)) else: i = 1 if attributes: for a in attributes: if i == 1: print(a, end="") i = 0 else: print("," + a, end="") print("\n") if resultant_data: for data in resultant_data: i = 1 for col in attributes: if i == 1: print(data[schema.index(col)], end="") i = 0 else: print("," + data[schema.index(col)], end="") print("\n")