Aligned export related code with flake8 standards

This commit is contained in:
Austin Marino 2019-10-13 11:36:33 -07:00
parent ec705fae1e
commit fa2c9ba259
2 changed files with 31 additions and 44 deletions

View file

@ -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)

View file

@ -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("<title>" + item1.track + "</title>", out)
self.assertIn("<album>" + item1.album + "</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')
unittest.main(defaultTest='suite')