示例#1
0
def get_project_files():
	"""
	This function fetches the last session of the last opened files in a project when that project is reopened later.
	:return:
	"""
	response = {
				"tableData": None,
				"yamlData": None,
				"wikifierData": None,
				"settings": {"endpoint": None}
			}
	if 'uid' in session:
		user_id = session["uid"]
		project_id = request.form['pid']
		project_config_path = get_project_config_path(user_id, project_id)
		project = Project(project_config_path)
		data_file_id, sheet_name = project.get_current_file_and_sheet()
		if data_file_id:
			file_extension = get_file_extension(data_file_id)
			response["tableData"] = dict()
			response["tableData"]["isCSV"] = True if file_extension.lower() == "csv" else False
			response["tableData"]["filename"] = project.get_file_name_by_id(data_file_id)
			data_file_path = str(Path(app.config['UPLOAD_FOLDER']) / user_id / project_id / "df" / data_file_id)
			response["tableData"].update(excel_to_json(data_file_path, sheet_name, True))
			if response["tableData"]["isCSV"]:
				response["tableData"]["currSheetName"] = None
				response["tableData"]["sheetNames"] = None
		else:
			response["tableData"] = None
		wikifier_config_file_name = project.get_wikifier_region_filename()
		if wikifier_config_file_name:
			wikifier_config = deserialize_wikifier_config(user_id, project_id, wikifier_config_file_name)
			item_table = ItemTable(wikifier_config)
			region_qnodes = item_table.get_region_qnodes()
			response["wikifierData"] = region_qnodes
		else:
			response["wikifierData"] = None
			item_table = ItemTable()

		yaml_file_id = project.get_yaml_file_id(data_file_id, sheet_name)
		if yaml_file_id:
			response["yamlData"] = dict()
			yaml_file_name = yaml_file_id + ".yaml"
			yaml_file_path = str(Path.cwd() / "config" / "uploads" / user_id / project_id / "yf" / yaml_file_name)
			response["yamlData"]["yamlFileContent"] = read_file(yaml_file_path)
			if data_file_id:
				yaml_config_file_name = yaml_file_id + ".pickle"
				yaml_config_file_path = str(Path.cwd() / "config" / "uploads" / user_id / project_id / "yf" / yaml_config_file_name)
				data_file_path = str(Path(app.config['UPLOAD_FOLDER']) / user_id / project_id / "df" / data_file_id)

				yaml_config = load_yaml_config(yaml_config_file_path)
				template = yaml_config.get_template()
				region = yaml_config.get_region()
				response["yamlData"]['yamlRegions'] = highlight_region(item_table, data_file_path, sheet_name, region, template)
		else:
			response["yamlData"] = None
		response["settings"]["endpoint"] = project.get_sparql_endpoint()
	response_json = json.dumps(response)
	return response_json
示例#2
0
def change_sheet():
	"""
	This route is used when a user switches a sheet in an excel data file.
	:return:
	"""
	if 'uid' in session:
		response = {
					"tableData": dict(),
					"wikifierData": dict(),
					"yamlData": dict(),
					"error": None
				}
		project_meta = dict()
		user_id = session['uid']
		new_sheet_name = request.form['sheet_name']
		project_id = request.form['pid']
		project_config_path = get_project_config_path(user_id, project_id)
		project = Project(project_config_path)
		data_file_id, current_sheet_name = project.get_current_file_and_sheet()
		data_file_path = str(Path.cwd() / "config" / "uploads" / user_id / project_id / "df" / data_file_id)
		data = excel_to_json(data_file_path, new_sheet_name)
		table_data = response["tableData"]
		table_data["filename"] = project.get_file_name_by_id(data_file_id)
		table_data["isCSV"] = False # because CSVs don't have sheets
		table_data["sheetNames"] = data["sheetNames"]
		table_data["currSheetName"] = data["currSheetName"]
		table_data["sheetData"] = data["sheetData"]
		project_meta["currentSheetName"] = data["currSheetName"]

		add_excel_file_to_bindings(data_file_path, new_sheet_name)

		region_map, region_file_name = get_region_mapping(user_id, project_id, project, data_file_id, new_sheet_name)
		item_table = ItemTable(region_map)
		wikifier_output_filepath = str(Path.cwd() / "config" / "uploads" / user_id / project_id / "wf" / "other.csv")
		if Path(wikifier_output_filepath).exists():
			build_item_table(item_table, wikifier_output_filepath, data_file_path, new_sheet_name)
		region_qnodes = item_table.get_region_qnodes()
		response["wikifierData"] = region_qnodes
		project_meta["wikifierRegionMapping"] = dict()
		project_meta["wikifierRegionMapping"][data_file_id] = dict()
		project_meta["wikifierRegionMapping"][data_file_id][new_sheet_name] = region_file_name
		update_wikifier_region_file(user_id, project_id, region_file_name, region_qnodes)

		yaml_file_id = project.get_yaml_file_id(data_file_id, new_sheet_name)
		if yaml_file_id:
			response["yamlData"] = dict()
			yaml_file_name = yaml_file_id + ".yaml"
			yaml_file_path = str(Path.cwd() / "config" / "uploads" / user_id / project_id / "yf" / yaml_file_name)
			response["yamlData"]["yamlFileContent"] = read_file(yaml_file_path)
			if data_file_id:
				yaml_config_file_name = yaml_file_id + ".pickle"
				yaml_config_file_path = str(
					Path.cwd() / "config" / "uploads" / user_id / project_id / "yf" / yaml_config_file_name)
				data_file_path = str(Path(app.config['UPLOAD_FOLDER']) / user_id / project_id / "df" / data_file_id)

				yaml_config = load_yaml_config(yaml_config_file_path)
				template = yaml_config.get_template()
				region = yaml_config.get_region()
				response["yamlData"]['yamlRegions'] = highlight_region(item_table, data_file_path, new_sheet_name, region, template)
				project_meta["yamlMapping"] = dict()
				project_meta["yamlMapping"][data_file_id] = dict()
				project_meta["yamlMapping"][data_file_id][data["currSheetName"]] = yaml_file_id
		else:
			response["yamlData"] = None

		project.update_project_config(project_meta)
		return json.dumps(response, indent=3)