def import_csv(dataset, url, args): """ Import the csv data into the dataset """ csv_data_url, source_url = url source = Source(dataset, shell_account(), csv_data_url) # Analyse the csv data and add it to the source # If we don't analyse it we'll be left with a weird message source.analysis = analyze_csv(csv_data_url) # Check to see if the dataset already has this source for source_ in dataset.sources: if source_.url == csv_data_url: source = source_ break db.session.add(source) db.session.commit() dataset.generate() importer = CSVImporter(source) importer.run(**vars(args)) # Check if imported from the file system (source and data url differ) if csv_data_url != source_url: # If we did, then we must update the source url based on the # sources in the dataset model (so we need to fetch the source again # or else we'll add a new one) source = Source.by_id(source.id) source.url = source_url db.session.commit()
def analyze_source(source_id): from openspending.model import Source, meta as db from openspending.importer.analysis import analyze_csv source = Source.by_id(source_id) if not source: log.error("No such source: %s", source_id) log.info("Analyzing: %s", source.url) source.analysis = analyze_csv(source.url) if 'error' in source.analysis: log.error(source.analysis.get('error')) else: log.info("Columns: %r", source.analysis.get('columns')) db.session.commit()