From ff4cc7b0af1b9f73fd07dc8538577d5ee9afbb20 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sat, 17 Jan 2009 22:32:40 +0900 Subject: [PATCH] add importing tag support, fix audio --- anki/importing/__init__.py | 14 +++++++++++--- anki/sound.py | 21 ++++++++++++--------- tests/importing/text-tags.txt | 1 + tests/test_importing.py | 12 ++++++++++++ 4 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 tests/importing/text-tags.txt diff --git a/anki/importing/__init__.py b/anki/importing/__init__.py index 93dc18ad8..ed3567a6a 100644 --- a/anki/importing/__init__.py +++ b/anki/importing/__init__.py @@ -8,8 +8,8 @@ Importing support 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 -particular FieldModel, replace it with None. The same field model should not -occur more than once.""" +particular FieldModel, replace it with None. A special number 0 donates a tags +field. The same field model should not occur more than once.""" __docformat__ = 'restructuredtext' @@ -70,6 +70,7 @@ class Importer(object): m = [] [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(0) rem = max(0, self.fields() - len(m)) m += [None] * rem del m[numFields:] @@ -122,6 +123,13 @@ all but one card template.""")) def addCards(self, 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 self.deck.updateProgress() factIds = [genID() for n in range(len(cards))] @@ -238,7 +246,7 @@ from anki.importing.mnemosyne10 import Mnemosyne10Importer from anki.importing.wcu import WCUImporter Importers = ( - (_("TAB/semicolon-separated file (*)"), TextImporter), + (_("Text file (tab/semicolon separated) (*)"), TextImporter), (_("Anki 1.0 deck (*.anki)"), Anki10Importer), (_("Mnemosyne 1.x deck (*.mem)"), Mnemosyne10Importer), (_("CueCard deck (*.wcu)"), WCUImporter), diff --git a/anki/sound.py b/anki/sound.py index 64e310531..726a94a57 100644 --- a/anki/sound.py +++ b/anki/sound.py @@ -135,9 +135,15 @@ def clearQueueExternal(): try: import pyaudio import wave + + PYAU_FORMAT = pyaudio.paInt16 + PYAU_CHANNELS = 1 + PYAU_RATE = 44100 + PYAU_INPUT_INDEX = 0 except ImportError: pass + class _Recorder(object): def postprocess(self): @@ -161,16 +167,13 @@ class PyAudioThreadedRecorder(threading.Thread): def run(self): chunk = 1024 - FORMAT = pyaudio.paInt16 - CHANNELS = 1 - RATE = 44100 p = pyaudio.PyAudio() - stream = p.open(format = FORMAT, - channels = CHANNELS, - rate = RATE, - input = True, - input_device_index = 0, - frames_per_buffer = chunk) + stream = p.open(format=PYAU_FORMAT, + channels=PYAU_CHANNELS, + rate=PYAU_RATE, + input=True, + input_device_index=PYAU_INPUT_INDEX, + frames_per_buffer=chunk) all = [] while not self.finish: data = stream.read(chunk) diff --git a/tests/importing/text-tags.txt b/tests/importing/text-tags.txt new file mode 100644 index 000000000..c088e1500 --- /dev/null +++ b/tests/importing/text-tags.txt @@ -0,0 +1 @@ +foo bar baz,qux diff --git a/tests/test_importing.py b/tests/test_importing.py index 1ef3faa40..f7591c4e1 100644 --- a/tests/test_importing.py +++ b/tests/test_importing.py @@ -7,6 +7,7 @@ from anki.errors import * from anki import DeckStorage from anki.importing import anki10, csv, mnemosyne10 from anki.stdmodels import BasicModel +from anki.facts import Fact from anki.db import * @@ -23,6 +24,17 @@ def test_csv(): assert i.total == 5 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(): deck = DeckStorage.Deck() deck.addModel(BasicModel())