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)
Currently there are several approaches to construct storage instance:
parsefileclass method takes file object or filename as stringparsestringclass method takes bytes__init__generally creates empty object__init__acceptinputfileparameter and behaves pretty much likeparsefileduplicating it's logicparsemethod is used byparsestringand sometimes also to implement__init__loadingThis leads to lot of code duplication where each storage behaves slightly differently (some taking file name from
namesome also fromfilename, some closing the file, some seeking to start).Some examples of
parseimplementations:translate/translate/storage/lisa.py
Lines 328 to 333 in f165a81
translate/translate/storage/trados.py
Lines 204 to 211 in f165a81
translate/translate/storage/pypo.py
Lines 816 to 821 in f165a81
translate/translate/storage/aresource.py
Lines 463 to 468 in f165a81
Changing this mess can break some usage, that's why I'm opening issue before working on the code.
My proposal to address this:
TranslationStore.__init__handleinputfileandinputbytesargsparsestringandparsefilejust wrapper to invoke__init__with corresponding arg, possibly deprecating them (parsestringaccepting bytes is confusing)parsemethods will accept only bytes and will do only the actual parsing without any additional logic (this could be breaking change)