mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -04:00
add importing tag support, fix audio
This commit is contained in:
parent
835b4b5969
commit
ff4cc7b0af
4 changed files with 36 additions and 12 deletions
|
@ -8,8 +8,8 @@ Importing support
|
||||||
|
|
||||||
To import, a mapping is created of the form: [FieldModel, ...]. The mapping
|
To import, a mapping is created of the form: [FieldModel, ...]. The mapping
|
||||||
may be extended by calling code if a file has more fields. To ignore a
|
may be extended by calling code if a file has more fields. To ignore a
|
||||||
particular FieldModel, replace it with None. The same field model should not
|
particular FieldModel, replace it with None. A special number 0 donates a tags
|
||||||
occur more than once."""
|
field. The same field model should not occur more than once."""
|
||||||
|
|
||||||
__docformat__ = 'restructuredtext'
|
__docformat__ = 'restructuredtext'
|
||||||
|
|
||||||
|
@ -70,6 +70,7 @@ class Importer(object):
|
||||||
m = []
|
m = []
|
||||||
[m.append(f) for f in self.model.fieldModels if f.required]
|
[m.append(f) for f in self.model.fieldModels if f.required]
|
||||||
[m.append(f) for f in self.model.fieldModels if not f.required]
|
[m.append(f) for f in self.model.fieldModels if not f.required]
|
||||||
|
m.append(0)
|
||||||
rem = max(0, self.fields() - len(m))
|
rem = max(0, self.fields() - len(m))
|
||||||
m += [None] * rem
|
m += [None] * rem
|
||||||
del m[numFields:]
|
del m[numFields:]
|
||||||
|
@ -122,6 +123,13 @@ all but one card template."""))
|
||||||
|
|
||||||
def addCards(self, cards):
|
def addCards(self, cards):
|
||||||
"Add facts in bulk from foreign cards."
|
"Add facts in bulk from foreign cards."
|
||||||
|
# map tags field to attr
|
||||||
|
try:
|
||||||
|
idx = self.mapping.index(0)
|
||||||
|
for c in cards:
|
||||||
|
c.tags = c.fields[idx]
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
# add facts
|
# add facts
|
||||||
self.deck.updateProgress()
|
self.deck.updateProgress()
|
||||||
factIds = [genID() for n in range(len(cards))]
|
factIds = [genID() for n in range(len(cards))]
|
||||||
|
@ -238,7 +246,7 @@ from anki.importing.mnemosyne10 import Mnemosyne10Importer
|
||||||
from anki.importing.wcu import WCUImporter
|
from anki.importing.wcu import WCUImporter
|
||||||
|
|
||||||
Importers = (
|
Importers = (
|
||||||
(_("TAB/semicolon-separated file (*)"), TextImporter),
|
(_("Text file (tab/semicolon separated) (*)"), TextImporter),
|
||||||
(_("Anki 1.0 deck (*.anki)"), Anki10Importer),
|
(_("Anki 1.0 deck (*.anki)"), Anki10Importer),
|
||||||
(_("Mnemosyne 1.x deck (*.mem)"), Mnemosyne10Importer),
|
(_("Mnemosyne 1.x deck (*.mem)"), Mnemosyne10Importer),
|
||||||
(_("CueCard deck (*.wcu)"), WCUImporter),
|
(_("CueCard deck (*.wcu)"), WCUImporter),
|
||||||
|
|
|
@ -135,9 +135,15 @@ def clearQueueExternal():
|
||||||
try:
|
try:
|
||||||
import pyaudio
|
import pyaudio
|
||||||
import wave
|
import wave
|
||||||
|
|
||||||
|
PYAU_FORMAT = pyaudio.paInt16
|
||||||
|
PYAU_CHANNELS = 1
|
||||||
|
PYAU_RATE = 44100
|
||||||
|
PYAU_INPUT_INDEX = 0
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class _Recorder(object):
|
class _Recorder(object):
|
||||||
|
|
||||||
def postprocess(self):
|
def postprocess(self):
|
||||||
|
@ -161,16 +167,13 @@ class PyAudioThreadedRecorder(threading.Thread):
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
chunk = 1024
|
chunk = 1024
|
||||||
FORMAT = pyaudio.paInt16
|
|
||||||
CHANNELS = 1
|
|
||||||
RATE = 44100
|
|
||||||
p = pyaudio.PyAudio()
|
p = pyaudio.PyAudio()
|
||||||
stream = p.open(format = FORMAT,
|
stream = p.open(format=PYAU_FORMAT,
|
||||||
channels = CHANNELS,
|
channels=PYAU_CHANNELS,
|
||||||
rate = RATE,
|
rate=PYAU_RATE,
|
||||||
input = True,
|
input=True,
|
||||||
input_device_index = 0,
|
input_device_index=PYAU_INPUT_INDEX,
|
||||||
frames_per_buffer = chunk)
|
frames_per_buffer=chunk)
|
||||||
all = []
|
all = []
|
||||||
while not self.finish:
|
while not self.finish:
|
||||||
data = stream.read(chunk)
|
data = stream.read(chunk)
|
||||||
|
|
1
tests/importing/text-tags.txt
Normal file
1
tests/importing/text-tags.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
foo bar baz,qux
|
|
@ -7,6 +7,7 @@ from anki.errors import *
|
||||||
from anki import DeckStorage
|
from anki import DeckStorage
|
||||||
from anki.importing import anki10, csv, mnemosyne10
|
from anki.importing import anki10, csv, mnemosyne10
|
||||||
from anki.stdmodels import BasicModel
|
from anki.stdmodels import BasicModel
|
||||||
|
from anki.facts import Fact
|
||||||
|
|
||||||
from anki.db import *
|
from anki.db import *
|
||||||
|
|
||||||
|
@ -23,6 +24,17 @@ def test_csv():
|
||||||
assert i.total == 5
|
assert i.total == 5
|
||||||
deck.s.close()
|
deck.s.close()
|
||||||
|
|
||||||
|
def test_csv_tags():
|
||||||
|
deck = DeckStorage.Deck()
|
||||||
|
deck.addModel(BasicModel())
|
||||||
|
file = unicode(os.path.join(testDir, "importing/text-tags.txt"))
|
||||||
|
i = csv.TextImporter(deck, file)
|
||||||
|
i.doImport()
|
||||||
|
facts = deck.s.query(Fact).all()
|
||||||
|
assert len(facts) == 1
|
||||||
|
assert facts[0].tags == "baz, qux"
|
||||||
|
deck.s.close()
|
||||||
|
|
||||||
def test_mnemosyne10():
|
def test_mnemosyne10():
|
||||||
deck = DeckStorage.Deck()
|
deck = DeckStorage.Deck()
|
||||||
deck.addModel(BasicModel())
|
deck.addModel(BasicModel())
|
||||||
|
|
Loading…
Reference in a new issue