def test_load_from_csv(self): oss = io.StringIO() table_ = holdings_reports.report_holdings(None, False, self.entries, self.options_map) table.table_to_csv(table_, file=oss) iss = io.StringIO(oss.getvalue()) holdings_list = list(holdings_reports.load_from_csv(iss)) self.assertEqual(2, len(holdings_list)) self.assertTrue(isinstance(holdings_list, list)) self.assertTrue(isinstance(holdings_list[0], holdings.Holding))
def main(): logging.basicConfig(level=logging.INFO, format='%(levelname)-8s: %(message)s') parser = gdrive.get_argparser(description=__doc__.strip()) parser.add_argument('filename', help="Beancount input file") parser.add_argument('docid', help="Document ID") parser.add_argument( '-o', '--output', action='store', default=datetime.date.today().strftime('beancount.%Y-%m-%d.pdf'), help="Where to write out the collated PDF file") args = parser.parse_args() # Load the file. entries, unused_errors, options_map = loader.load_file( args.filename, log_timings=logging.info, log_errors=sys.stderr) # Generate a report. holdings_list = holdings_reports.report_holdings( None, False, entries, options_map, aggregation_key=lambda holding: holding.currency) oss = io.StringIO() table.table_to_csv(holdings_list, file=oss) csv_contents = oss.getvalue() # Connect, with authentication. # Check https://developers.google.com/drive/scopes for all available scopes. scopes = [ 'https://www.googleapis.com/auth/drive', ] http = gdrive.get_authenticated_http(" ".join(scopes), args) # Access the drive API. drive = gdrive.discovery.build('drive', 'v2', http=http) # Get the document and replace it. metadata = drive.files().get(fileId=args.docid).execute() upload = MediaInMemoryUpload(csv_contents.encode('utf-8'), mimetype=metadata['mimeType']) metadata = drive.files().update(fileId=args.docid, media_body=upload).execute() pprint(metadata)
def test_table_to_csv(self): table_object = self.test_create_table() text = table.table_to_csv(table_object, lineterminator='\n') expected = textwrap.dedent("""\ Country,Capital,Currency,Amount Malawi,Lilongwe,Kwacha,0.111 Mali,Bamako,CFA franc,0.222 Mauritania,Nouakchott,Ouguiya,0.333 """) self.assertEqual(expected, text)