def testCsvFromJSONAbleIncludeFields(self): ''' tests generation of csv from list of JSONAble object with including only specified fields (positive list) ''' lod = [{ "name": "Test", "label": 1 }, { "name": "Test 2", "label": 2 }, { "name": "Different", "location": "Munich" }] jsonAbleList = JSONAbleList(clazz=JSONAble) jsonAbleList.fromLoD(lod) actualCsvString = CSV.toCSV(jsonAbleList.getList(), includeFields=['name', 'location']) expectedCsvString = '"name","location"\r\n"Test",""\r\n"Test 2",""\r\n"Different","Munich"\r\n' self.assertEqual(actualCsvString, expectedCsvString)
def test_to_csv(self): '''tests if LoD is correctly converted to csv str''' expectedStr = self.csvStr actualStr = CSV.toCSV(self.csvLOD) self.assertEqual(expectedStr, actualStr)
def main(cls, args): ''' command line activation with parsed args Args: args(list): the command line arguments ''' debug = args.debug endpoints = EndpointManager.getEndpoints(args.endpointPath) qm = QueryManager(lang=args.language, debug=debug, queriesPath=args.queriesPath) query = None queryCode = args.query endpointConf = None formats = None # preload ValueFormatter ValueFormatter.getFormats(args.formatsPath) if args.list: for name, query in qm.queriesByName.items(): print(f"{name}:{query.title}") elif args.listEndpoints: # list endpoints for endpoint in endpoints.values(): if hasattr(endpoint, "lang") and endpoint.lang == args.language: print(endpoint) elif args.queryName is not None: if debug or args.showQuery: print(f"named query {args.queryName}:") if args.queryName not in qm.queriesByName: raise Exception(f"named query {args.queryName} not available") query = qm.queriesByName[args.queryName] formats = query.formats queryCode = query.query if debug or args.showQuery: if hasattr(query, "description") and query.description is not None: print(query.description) if query is None: name = "?" if queryCode is None and args.queryFile is not None: queryFilePath = Path(args.queryFile) queryCode = queryFilePath.read_text() name = queryFilePath.stem query = Query(name="?", query=queryCode, lang=args.language) if queryCode: if debug or args.showQuery: print(f"{args.language}:\n{queryCode}") if args.endpointName: endpointConf = endpoints.get(args.endpointName) if args.language == "sparql": method = 'POST' if args.endpointName: endPointUrl = endpointConf.endpoint method = endpointConf.method query.tryItUrl = endpointConf.website query.database = endpointConf.database else: endPointUrl = query.endpoint if args.method: method = method sparql = SPARQL(endPointUrl, method=method) if args.prefixes and endpointConf is not None: queryCode = f"{endpointConf.prefixes}\n{queryCode}" if args.raw: qres = cls.rawQuery(endPointUrl, query=query.query, resultFormat=args.format, mimeType=args.mimeType) print(qres) return if "wikidata" in args.endpointName and formats is None: formats = ["*:wikidata"] qlod = sparql.queryAsListOfDicts(queryCode) elif args.language == "sql": sqlDB = SQLDB(endpointConf.endpoint) qlod = sqlDB.query(queryCode) else: raise Exception( f"language {args.language} not known/supported") if args.format is Format.csv: csv = CSV.toCSV(qlod) print(csv) elif args.format in [ Format.latex, Format.github, Format.mediawiki ]: doc = query.documentQueryResult(qlod, tablefmt=str(args.format), floatfmt=".0f") docstr = doc.asText() print(docstr) elif args.format in [Format.json ] or args.format is None: # set as default # https://stackoverflow.com/a/36142844/1497139 print(json.dumps(qlod, indent=2, sort_keys=True, default=str)) elif args.format in [Format.xml]: lod2xml = Lod2Xml(qlod) xml = lod2xml.asXml() print(xml) else: raise Exception(f"format {args.format} not supported yet")