add importing tag support, fix audio

This commit is contained in:
Damien Elmes 2009-01-17 22:32:40 +09:00
parent 835b4b5969
commit ff4cc7b0af
4 changed files with 36 additions and 12 deletions

View file

@ -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),

View file

@ -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)

View file

@ -0,0 +1 @@
foo bar baz,qux

View file

@ -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())