Skip to content

Confusing parse methods and constructors #3911

@nijel

Description

@nijel

Currently there are several approaches to construct storage instance:

  • parsefile class method takes file object or filename as string
  • parsestring class method takes bytes
  • __init__ generally creates empty object
  • on some storages __init__ accept inputfile parameter and behaves pretty much like parsefile duplicating it's logic
  • the parse method is used by parsestring and sometimes also to implement __init__ loading

This leads to lot of code duplication where each storage behaves slightly differently (some taking file name from name some also from filename, some closing the file, some seeking to start).

Some examples of parse implementations:

if not hasattr(self, 'filename'):
self.filename = getattr(xml, 'name', '')
if hasattr(xml, "read"):
xml.seek(0)
posrc = xml.read()
xml = posrc

if hasattr(input, 'name'):
self.filename = input.name
elif not getattr(self, 'filename', ''):
self.filename = ''
if hasattr(input, "read"):
tmsrc = input.read()
input.close()
input = tmsrc

if hasattr(input, 'name'):
self.filename = input.name
elif not getattr(self, 'filename', ''):
self.filename = ''
if not isinstance(input, bytes):
input = input.read()

if not hasattr(self, 'filename'):
self.filename = getattr(xml, 'name', '')
if hasattr(xml, "read"):
xml.seek(0)
posrc = xml.read()
xml = posrc

Changing this mess can break some usage, that's why I'm opening issue before working on the code.

My proposal to address this:

  • let TranslationStore.__init__ handle inputfile and inputbytes args
  • make parsestring and parsefile just wrapper to invoke __init__ with corresponding arg, possibly deprecating them (parsestring accepting bytes is confusing)
  • the individual storage parse methods will accept only bytes and will do only the actual parsing without any additional logic (this could be breaking change)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions