Commit c311352f authored by Denis Laxalde's avatar Denis Laxalde
Browse files

[entities,views] Use delimiter instead of separator variable name

Also make the `delimiter` parameter mandatory in `add_concepts_from_file`
method.
parent 37669a49c842
......@@ -70,14 +70,14 @@ class ConceptScheme(AnyEntity):
"""Add a top-concept to this scheme"""
return _add_concept(self, label, language_code, **kwargs)
def add_concepts_from_file(self, stream, encoding, language_code, separator='\t'):
def add_concepts_from_file(self, stream, encoding, language_code, delimiter):
"""Read a stream and create the listed concepts inside the ConceptScheme
'separators' are considered as hierarchical information. There must be a concept per line,
each line starting by N separators (indicating the hierarchical level) or nothing if the
'delimiters' are considered as hierarchical information. There must be a concept per line,
each line starting by N delimiters (indicating the hierarchical level) or nothing if the
concept has no parent.
Example (sep = u';')
Example (delimiter = u';')
titi
;toto
;;tata
......@@ -100,13 +100,13 @@ class ConceptScheme(AnyEntity):
raise CSVDecodeError(nline)
if not line:
continue
# Find the label by removing leading separators (and spaces), then
# remove trailing separators and spaces.
label = line.lstrip(separator + ' ').rstrip(separator + ' ')
# Find the label by removing leading delimiters (and spaces), then
# remove trailing delimiters and spaces.
label = line.lstrip(delimiter + ' ').rstrip(delimiter + ' ')
if not label:
# The line is full of separator.
# The line is full of delimiter.
continue
next_level = line[:line.index(label)].count(separator)
next_level = line[:line.index(label)].count(delimiter)
if next_level - level > 1:
# Concept must be at much one level below its parent.
raise CSVIndentationError(nline)
......
......@@ -382,7 +382,7 @@ msgctxt "Concept"
msgid "hidden_label_of_object"
msgstr ""
msgid "hierarchical separator"
msgid "hierarchical delimiter"
msgstr ""
msgid "import concept scheme from SKOS"
......
......@@ -383,7 +383,7 @@ msgctxt "Concept"
msgid "hidden_label_of_object"
msgstr ""
msgid "hierarchical separator"
msgid "hierarchical delimiter"
msgstr "séparateur hiérarchique"
msgid "import concept scheme from SKOS"
......@@ -545,3 +545,6 @@ msgstr ""
msgid "xml"
msgstr ""
#~ msgid "hierarchical separator"
#~ msgstr "séparateur hiérarchique"
......@@ -36,7 +36,7 @@ class ViewsTC(CubicWebTC):
'encoding': u'utf-8',
'language': u'fr',
'format': u'simple',
'separator': u'tab',
'delimiter': u'tab',
}, [(scheme, {})])
# now actually tests the import, using scheme.view and not self.view which doesn't like
# exception, even Redirect
......@@ -59,7 +59,7 @@ class ViewsTC(CubicWebTC):
posted = {'file': (fname, open(self.datapath(fname))),
'encoding': u'utf-8',
'language': u'fr',
'separator': u'tab',
'delimiter': u'tab',
'format': u'lcsv'}
req.form = self.fake_form('skos.scheme.import', posted, [(scheme, {})])
# now actually tests the import, using scheme.view and not self.view which doesn't like
......
......@@ -239,7 +239,7 @@ class ImportSchemeConceptsForm(ImportSchemeConceptsMixIn, forms.EntityFieldsForm
internationalizable=True,
choices=[(_('french'), u'fr'),
(_('english'), u'en')], sort=False)
separator = ff.StringField(name='separator', label=_('hierarchical separator'),
delimiter = ff.StringField(name='delimiter', label=_('hierarchical delimiter'),
internationalizable=True,
choices=[(_('tabulation'), 'tab'),
(_(';'), ';'),
......@@ -254,7 +254,7 @@ class ImportSchemeConceptsForm(ImportSchemeConceptsMixIn, forms.EntityFieldsForm
class ImportSchemeConceptsView(ImportSchemeConceptsMixIn, EntityView):
separators = {'tab': '\t', 'space': ' ', ',': ',', ';': ';'}
delimiters = {'tab': '\t', 'space': ' ', ',': ',', ';': ';'}
def entity_call(self, entity):
self.w('<h1>%s</h1>' % self._cw._('Import concepts from file'))
......@@ -272,7 +272,7 @@ class ImportSchemeConceptsView(ImportSchemeConceptsMixIn, EntityView):
form.render(w=self.w)
def _simple_import(self, entity, posted):
delimiter = self.separators[posted['separator']]
delimiter = self.delimiters[posted['delimiter']]
try:
entity.add_concepts_from_file(posted['file'], posted['encoding'],
posted['language'], delimiter)
......@@ -284,7 +284,7 @@ class ImportSchemeConceptsView(ImportSchemeConceptsMixIn, EntityView):
{None: self._cw._("Encoding error line %s") % exc.line})
def _lcsv_import(self, entity, posted):
delimiter = self.separators[posted['separator']]
delimiter = self.delimiters[posted['delimiter']]
try:
self._cw.cnx.call_service('lcsv.skos.import',
scheme_uri=entity.cwuri,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment