def parse_store(cls, storefile): """Parse the store.""" storeclass = cls.get_class() # Did we get file or filename? if not hasattr(storefile, "read"): storefile = open(storefile, "rb") # Read content for fixups content = storefile.read() storefile.close() # Parse file store = storeclass() store.parse(content, sample_length=None) # Did detection of headers work? if store.fieldnames != ["location", "source", "target"]: return store fileobj = csv.StringIO( store.detect_encoding(content, default_encodings=["utf-8", "utf-16"])[0]) # Try reading header reader = csv.reader(fileobj, store.dialect) header = next(reader) fileobj.close() # Check if the file is not two column only if len(header) != 2: return store return cls.parse_simple_csv(content, storefile)
def parse_store(cls, storefile): """Parse the store.""" storeclass = cls.get_class() # Did we get file or filename? if not hasattr(storefile, 'read'): storefile = open(storefile, 'rb') # Read content for fixups content = storefile.read() storefile.close() # Parse file store = storeclass() store.parse(content) # Did headers detection work? if store.fieldnames != ['location', 'source', 'target']: return store fileobj = csv.StringIO( store.detect_encoding(content, default_encodings=['utf-8', 'utf-16'])[0]) # Try reading header reader = csv.reader(fileobj, store.dialect) header = next(reader) fileobj.close() # Check if the file is not two column only if len(header) != 2: return store result = storeclass(fieldnames=['source', 'target']) result.parse(content) return result
def parse_store(cls, storefile): """Parse the store.""" storeclass = cls.get_class() # Did we get file or filename? if not hasattr(storefile, 'read'): storefile = open(storefile, 'rb') # Read content for fixups content = storefile.read() storefile.seek(0) # Parse file store = storeclass.parsefile(storefile) # Did headers detection work? if store.fieldnames != ['location', 'source', 'target']: return store # Do we have python 3 compatible csv module? new_csv = six.PY3 or hasattr(csv, 'PY3') if new_csv: content = content.decode('utf-8') fileobj = csv.StringIO(content) storefile.close() # Try reading header reader = csv.reader(fileobj, store.dialect) header = next(reader) fileobj.close() # We seem to have match if len(header) != 2: return store result = storeclass(fieldnames=['source', 'target']) if new_csv: result.parse(content.encode('utf-8')) else: result.parse(content) return result