Coverage for modules/org/openteacher/logic/loaders/csv_/csv_ : 57%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
|
#! /usr/bin/env python3 # -*- coding: utf-8 -*-
# Copyright 2013, Marten de Vries # # This file is part of OpenTeacher. # # OpenTeacher is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # OpenTeacher is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with OpenTeacher. If not, see <http://www.gnu.org/licenses/>.
self._mm.mods(type="wordsStringParser"), ) self._mm.mods(type="translator"), )
def _parse(self): return self._modules.default("active", type="wordsStringParser").parse
return "words"
with open(path, "rbU") as f: data = f.read() encoding = chardet.detect(data)["encoding"] unicodeData = data.decode(encoding=encoding, errors="ignore")
sniffer = csv.Sniffer() dialect = sniffer.sniff(unicodeData) reader = csv.reader(unicodeData.split("\n"), dialect)
items = [] for i, line in enumerate(reader): if not line: continue try: questions = self._parse(line[0]) answers = self._parse(line[1]) except IndexError: continue
items.append({ "id": i, "questions": questions, "answers": answers, })
return { "resources": {}, "list": { "items": items, "tests": [], }, }
else: self._mm.resourcePath("translations") ) #TRANSLATORS: This is one of the file formats OpenTeacher can #TRANSLATORS: read.
global chardet except ImportError: #fallback. Strongly recommended to use the real one, #though ;) class MyChardet: def detect(self, *args, **kwargs): return {"encoding": "UTF-8"} chardet = MyChardet()
"csv": ["words"], "tsv": ["words"], } #no mimetype. Registering text/csv is too harsh.
else:
|