mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -04:00
initMapping(); csv tweaks
This commit is contained in:
parent
44dea8211e
commit
a99101d95c
3 changed files with 18 additions and 20 deletions
|
@ -20,7 +20,7 @@ class TextImporter(NoteImporter):
|
|||
self.tagsToAdd = []
|
||||
|
||||
def foreignNotes(self):
|
||||
self.sniff()
|
||||
self.open()
|
||||
# process all lines
|
||||
log = []
|
||||
notes = []
|
||||
|
@ -31,12 +31,7 @@ class TextImporter(NoteImporter):
|
|||
else:
|
||||
reader = csv.reader(self.data, self.dialect, doublequote=True)
|
||||
for row in reader:
|
||||
try:
|
||||
row = [unicode(x, "utf-8") for x in row]
|
||||
except UnicodeDecodeError, e:
|
||||
raise ImportFormatError(
|
||||
type="encodingError",
|
||||
info=_("Please save in UTF-8 format. Click help for info."))
|
||||
row = [unicode(x, "utf-8") for x in row]
|
||||
if len(row) != self.numFields:
|
||||
log.append(_(
|
||||
"'%(row)s' had %(num1)d fields, "
|
||||
|
@ -54,7 +49,7 @@ class TextImporter(NoteImporter):
|
|||
self.fileobj.close()
|
||||
return notes
|
||||
|
||||
def sniff(self):
|
||||
def open(self):
|
||||
"Parse the top line and determine the pattern and number of fields."
|
||||
# load & look for the right pattern
|
||||
self.cacheFile()
|
||||
|
@ -79,15 +74,11 @@ class TextImporter(NoteImporter):
|
|||
del self.data[0]
|
||||
self.updateDelimiter()
|
||||
if not self.dialect and not self.delimiter:
|
||||
raise ImportFormatError(
|
||||
type="encodingError",
|
||||
info=_("Couldn't determine format of file."))
|
||||
raise Exception("unknownFormat")
|
||||
|
||||
def updateDelimiter(self):
|
||||
def err():
|
||||
raise ImportFormatError(
|
||||
type="encodingError",
|
||||
info=_("File is not encoded in UTF-8."))
|
||||
raise Exception("unknownFormat")
|
||||
self.dialect = None
|
||||
sniffer = csv.Sniffer()
|
||||
delims = [',', '\t', ';', ':']
|
||||
|
@ -123,7 +114,7 @@ class TextImporter(NoteImporter):
|
|||
|
||||
def fields(self):
|
||||
"Number of fields."
|
||||
self.sniff()
|
||||
self.open()
|
||||
return self.numFields
|
||||
|
||||
def noteFromFields(self, fields):
|
||||
|
|
|
@ -51,15 +51,22 @@ class NoteImporter(Importer):
|
|||
"The number of fields."
|
||||
return 0
|
||||
|
||||
def maybeChecksum(self, data, unique):
|
||||
if not unique:
|
||||
return ""
|
||||
return fieldChecksum(data)
|
||||
def initMapping(self):
|
||||
flds = [f['name'] for f in self.model['flds']]
|
||||
# truncate to provided count
|
||||
flds = flds[0:self.fields()]
|
||||
# if provided count is greater, pad
|
||||
flds = flds + [None] * (self.fields() - len(flds))
|
||||
self.mapping = flds
|
||||
|
||||
def foreignNotes(self):
|
||||
"Return a list of foreign notes for importing."
|
||||
assert 0
|
||||
|
||||
def open(self):
|
||||
"Open file and ensure it's in the right format."
|
||||
assert 0
|
||||
|
||||
def importNotes(self, notes):
|
||||
"Convert each card into a note, apply attributes and add to col."
|
||||
# gather checks for duplicate comparison
|
||||
|
|
|
@ -80,7 +80,7 @@ def test_csv():
|
|||
deck = getEmptyDeck()
|
||||
file = unicode(os.path.join(testDir, "support/text-2fields.txt"))
|
||||
i = TextImporter(deck, file)
|
||||
i.mapping = ['Front', 'Back']
|
||||
i.initMapping()
|
||||
i.run()
|
||||
# four problems - too many & too few fields, a missing front, and a
|
||||
# duplicate entry
|
||||
|
|
Loading…
Reference in a new issue