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