From fa2c9ba2592c408949e6fa6ca00d5eebeee8aa45 Mon Sep 17 00:00:00 2001 From: Austin Marino Date: Sun, 13 Oct 2019 11:36:33 -0700 Subject: [PATCH] Aligned export related code with flake8 standards --- beetsplug/export.py | 33 ++++++++++++++++++++++----------- test/test_export.py | 42 +++++++++--------------------------------- 2 files changed, 31 insertions(+), 44 deletions(-) diff --git a/beetsplug/export.py b/beetsplug/export.py index 43417efea..2f6af072e 100644 --- a/beetsplug/export.py +++ b/beetsplug/export.py @@ -57,17 +57,23 @@ class ExportPlugin(BeetsPlugin): 'csv': { # CSV module formatting options. 'formatting': { - 'delimiter': ',', # The delimiter used to seperate columns. - 'dialect': 'excel' # The type of dialect to use when formating the file output. + # The delimiter used to seperate columns. + 'delimiter': ',', + # The dialect to use when formating the file output. + 'dialect': 'excel' } }, 'xml': { # XML module formatting options. 'formatting': { - 'encoding': 'unicode', # The output encoding. - 'xml_declaration':True, # Controls if an XML declaration should be added to the file. - 'method': 'xml', # Can be either "xml", "html" or "text" (default is "xml"). - 'short_empty_elements': True # Controls the formatting of elements that contain no content. + # The output encoding. + 'encoding': 'unicode', + # Controls if XML declaration should be added to the file. + 'xml_declaration': True, + # Can be either "xml", "html" or "text" (default is "xml"). + 'method': 'xml', + # Controls formatting of elements that contain no content. + 'short_empty_elements': True } } # TODO: Use something like the edit plugin @@ -105,11 +111,12 @@ class ExportPlugin(BeetsPlugin): def run(self, lib, opts, args): file_path = opts.output file_mode = 'a' if opts.append else 'w' - file_format = opts.format if opts.format else self.config['default_format'].get(str) + file_format = opts.format if opts.format else \ + self.config['default_format'].get(str) format_options = self.config[file_format]['formatting'].get(dict) export_format = ExportFormat.factory( - file_type=file_format, + file_type=file_format, **{ 'file_path': file_path, 'file_mode': file_mode @@ -144,8 +151,12 @@ class ExportFormat(object): self.path = file_path self.mode = file_mode self.encoding = encoding - # Assigned sys.stdout (terminal output) or the file stream for the path specified. - self.out_stream = codecs.open(self.path, self.mode, self.encoding) if self.path else sys.stdout + """ self.out_stream = + sys.stdout if path doesn't exit + codecs.open(..) else + """ + self.out_stream = codecs.open(self.path, self.mode, self.encoding) \ + if self.path else sys.stdout @classmethod def factory(cls, file_type, **kwargs): @@ -178,7 +189,7 @@ class CSVFormat(ExportFormat): def export(self, data, **kwargs): header = list(data[0].keys()) if data else [] - writer = csv.DictWriter(self.out_stream, fieldnames=self.header, **kwargs) + writer = csv.DictWriter(self.out_stream, fieldnames=header, **kwargs) writer.writeheader() writer.writerows(data) diff --git a/test/test_export.py b/test/test_export.py index ad00f83e7..9d7c4a457 100644 --- a/test/test_export.py +++ b/test/test_export.py @@ -19,10 +19,7 @@ from __future__ import division, absolute_import, print_function import unittest -from test import helper from test.helper import TestHelper -#from beetsplug.export import ExportPlugin, ExportFormat, JSONFormat, CSVFormat, XMLFormat -#from collections import namedtuple class ExportPluginTest(unittest.TestCase, TestHelper): @@ -41,11 +38,11 @@ class ExportPluginTest(unittest.TestCase, TestHelper): item1.track = "ttrack" item1.write() item1.store() - - out = self.run_with_output('export', '-f json -i "track,album" tartist') + options = '-f json -i "track,album" ' + item1.artist + out = self.run_with_output('export', options) self.assertIn('"track": "' + item1.track + '"', out) self.assertIn('"album": "' + item1.album + '"', out) - + def test_csv_output(self): item1, item2 = self.add_item_fixtures(count=2) item1.album = 'talbum' @@ -53,10 +50,10 @@ class ExportPluginTest(unittest.TestCase, TestHelper): item1.track = "ttrack" item1.write() item1.store() - - out = self.run_with_output('export', '-f json -i "track,album" tartist') + options = '-f csv -i "track,album" ' + item1.artist + out = self.run_with_output('export', options) self.assertIn(item1.track + ',' + item1.album, out) - + def test_xml_output(self): item1, item2 = self.add_item_fixtures(count=2) item1.album = 'talbum' @@ -64,35 +61,14 @@ class ExportPluginTest(unittest.TestCase, TestHelper): item1.track = "ttrack" item1.write() item1.store() - - out = self.run_with_output('export', '-f json -i "track,album" tartist') + options = '-f xml -i "track,album" ' + item1.artist + out = self.run_with_output('export', options) self.assertIn("" + item1.track + "", out) self.assertIn("" + item1.album + "", out) - """ - def setUp(self): - Opts = namedtuple('Opts', 'output append included_keys library format') - self.args = None - self._export = ExportPlugin() - included_keys = ['title,artist,album'] - self.opts = Opts(None, False, included_keys, True, "json") - self.export_format_classes = {"json": ExportFormat, "csv": CSVFormat, "xml": XMLFormat} - - def test_run(self, _format="json"): - self.opts.format = _format - self._export.run(lib=self.lib, opts=self.opts, args=self.args) - # 1.) Test that the ExportFormat Factory class method invoked the correct class - self.assertEqual(type(self._export.export_format), self.export_format_classes[_format]) - # 2.) Test that the cmd parser options specified were processed in correctly - self.assertEqual(self._export.export_format.path, self.opts.output) - mode = 'a' if self.opts.append else 'w' - self.assertEqual(self._export.export_format.mode, mode) - """ - - def suite(): return unittest.TestLoader().loadTestsFromName(__name__) if __name__ == '__main__': - unittest.main(defaultTest='suite') \ No newline at end of file + unittest.main(defaultTest='suite')