Commit b6e3d24e authored by Noé Gaumont's avatar Noé Gaumont 🐙
Browse files

refactor: run pyupgrade --p36-plus

parent 4515209286af
Pipeline #73748 passed with stages
in 2 minutes and 54 seconds
......@@ -68,9 +68,9 @@ def register_skos_rdf_output_mapping(reg):
LABELS_RDF_MAPPING = {
u'preferred_label': 'skos:prefLabel',
u'alternative_label': 'skos:altLabel',
u'hidden_label': 'skos:hiddenLabel',
'preferred_label': 'skos:prefLabel',
'alternative_label': 'skos:altLabel',
'hidden_label': 'skos:hiddenLabel',
}
......
......@@ -15,9 +15,7 @@
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""cubicweb-ctl plugin introducing skos-import command.
"""
from __future__ import print_function
import io
import logging
import sys
......@@ -121,7 +119,7 @@ class ImportSkosData(Command):
if self.get('format') == 'lcsv':
scheme_uri = self.get('scheme')
if not scheme_uri:
print(u'command failed: --scheme option is required for LCSV import')
print('command failed: --scheme option is required for LCSV import')
connection.repo.shutdown()
sys.exit(1)
try:
......@@ -131,21 +129,20 @@ class ImportSkosData(Command):
def extentities_generator():
"""ExtEntity generator function holding control on `fpath` file."""
with io.open(fpath, 'rb') as stream:
for extentity in lcsv_extentities(stream, scheme_uri,
language_code=u'fr'):
yield extentity
with open(fpath, 'rb') as stream:
yield from lcsv_extentities(stream, scheme_uri,
language_code='fr')
extentities = extentities_generator()
else:
graph = self.rdf_store_factories[self.get('rdf-store')]()
for filepath in args[1:]:
print(u'loading {} into RDF graph'.format(filepath))
print(f'loading {filepath} into RDF graph')
graph.load(filepath)
extentities = graph_extentities(graph)
import_log = LoggingImportLog()
print(u'\n%s' % underline_title('Importing Skos dataset'))
print('\n%s' % underline_title('Importing Skos dataset'))
with connection as cnx:
store = self.cw_store_factories[self.get('cw-store')](cnx, self.config)
try:
......@@ -154,11 +151,11 @@ class ImportSkosData(Command):
cnx.commit()
except BaseException as exc:
cnx.rollback()
print(u"Aborting due to: '%s'" % exc)
print("Aborting due to: '%s'" % exc)
else:
print(u'Created: %d\nUpdated: %d' % (len(created), len(updated)))
print('Created: %d\nUpdated: %d' % (len(created), len(updated)))
if conceptschemes:
print(u'Concept schemes:\n* %s' % '\n* '.join(conceptschemes))
print('Concept schemes:\n* %s' % '\n* '.join(conceptschemes))
connection.repo.shutdown()
......
......@@ -25,9 +25,9 @@ class SKOSRelationMapping(RelationMapping):
def __getitem__(self, rtype):
if rtype in ('exact_match', 'close_match'):
rql = 'Any S,O WHERE S {0} O'.format(rtype)
return set(tuple(x) for x in self.cnx.execute(rql))
return super(SKOSRelationMapping, self).__getitem__(rtype)
rql = f'Any S,O WHERE S {rtype} O'
return {tuple(x) for x in self.cnx.execute(rql)}
return super().__getitem__(rtype)
def ext_dump_relations(cnx, extid2eid, extentity):
......@@ -47,7 +47,7 @@ def ext_dump_relations(cnx, extid2eid, extentity):
extid2eid[obj] = obj
extentity.values.setdefault(rtype, set()).add(obj)
else:
raise Exception("can't dump inlined object relation {0}".format(rtype))
raise Exception(f"can't dump inlined object relation {rtype}")
else:
relations.append((subj, rtype, obj))
return relations
......@@ -168,10 +168,10 @@ class SKOSExtEntitiesImporter(ExtEntitiesImporter):
def __init__(self, *args, **kwargs):
self.restore_relations = kwargs.pop('restore_relations')
super(SKOSExtEntitiesImporter, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
def prepare_insert_entity(self, extentity):
eid = super(SKOSExtEntitiesImporter, self).prepare_insert_entity(extentity)
eid = super().prepare_insert_entity(extentity)
# (4.) restore relations formerly from/to an equivalent external uri
try:
relations = self.restore_relations.pop(extentity.extid)
......@@ -195,4 +195,4 @@ class SKOSExtEntitiesImporter(ExtEntitiesImporter):
values=dict(uri=str(object_uri),
cwuri=str(object_uri)))
self.prepare_insert_entity(extentity)
return super(SKOSExtEntitiesImporter, self).prepare_insert_deferred_relations(deferred)
return super().prepare_insert_deferred_relations(deferred)
......@@ -27,7 +27,7 @@ from cubicweb_skos import rdfio
from cubicweb_skos.rdfio import unicode_with_language as ul
def _add_concept(scheme, label, language_code, kind=u'preferred', **kwargs):
def _add_concept(scheme, label, language_code, kind='preferred', **kwargs):
cnx = scheme._cw
concept = cnx.create_entity('Concept', in_scheme=scheme, **kwargs)
cnx.create_entity('Label', label=label, language_code=language_code,
......@@ -41,7 +41,7 @@ class CSVParseError(Exception):
"""Error during CSV parsing, with a line information."""
def __init__(self, line):
super(CSVParseError, self).__init__()
super().__init__()
self.line = line
......@@ -72,7 +72,7 @@ class ConceptScheme(AnyEntity):
def top_concepts(self):
return list(self.top_concepts_rset.entities())
def add_concept(self, label, language_code=u'en', kind=u'preferred', **kwargs):
def add_concept(self, label, language_code='en', kind='preferred', **kwargs):
"""Add a top-concept to this scheme"""
return _add_concept(self, label, language_code, kind, **kwargs)
......@@ -162,7 +162,7 @@ class Concept(AnyEntity):
# Else pick a preferred label in any language.
return list(self.labels.values())[0]
def add_concept(self, label, language_code=u'en', kind=u'preferred'):
def add_concept(self, label, language_code='en', kind='preferred'):
"""Add a sub-concept to this concept"""
return _add_concept(self.scheme, label, language_code, kind, broader_concept=self)
......@@ -243,7 +243,7 @@ class ConceptRDFPrimaryAdapter(AbstractRDFAdapter):
register_rdf_mapping = staticmethod(skos.register_skos_concept_rdf_output_mapping)
def fill(self, graph):
super(ConceptRDFPrimaryAdapter, self).fill(graph)
super().fill(graph)
reg = self.registry
concept = self.entity
concept_uri = graph.uri(rdfio.RDFGraphGenerator.canonical_uri(concept))
......@@ -314,10 +314,10 @@ def _select_attributes(rql, var, attributes):
assert attributes
select, where = rql.split(' WHERE ')
for iattr, attr in enumerate(attributes):
attrvar = '%s%s' % (var, iattr)
where += ', %s %s %s' % (var, attr, attrvar)
attrvar = f'{var}{iattr}'
where += f', {var} {attr} {attrvar}'
select += ', %s' % attrvar
return '%s WHERE %s' % (select, where)
return f'{select} WHERE {where}'
def _split_rset(rset, entity_col):
......@@ -338,7 +338,7 @@ def cache_entities_relations(entities, rset, rtype, role, entity_col=1, target_c
* related entities are in `target_col` column of the rset
"""
no_relation = set(entities)
cache_key = '%s_%s' % (rtype, role)
cache_key = f'{rtype}_{role}'
for entity, subrset in _split_rset(rset, entity_col):
entity._cw_related_cache[cache_key] = (subrset, tuple(subrset.entities(target_col)))
try:
......
......@@ -93,8 +93,8 @@ class CreateCWSource(hook.Hook):
if 'through_cw_source' in self.entity.cw_edited:
return
source = self._cw.create_entity('CWSource', name=self.entity.name, url=self.entity.url,
type=u'datafeed', parser=u'rdf.skos',
config=u'synchronize=no\nuse-cwuri-as-url=no')
type='datafeed', parser='rdf.skos',
config='synchronize=no\nuse-cwuri-as-url=no')
self.entity.cw_edited['through_cw_source'] = source.eid
# remove once fix for https://www.cubicweb.org/ticket/5477315 is published
nocheck = self._cw.transaction_data.setdefault('skip-security', set())
......@@ -125,7 +125,7 @@ class CheckPreferredLabelOp(hook.DataOperationMixIn, hook.Operation):
containercls = list
def precommit_event(self):
concept_labels = dict((label.label_of[0].eid, label) for label in self.get_data())
concept_labels = {label.label_of[0].eid: label for label in self.get_data()}
concept_eids = ',' . join(str(concept_eid) for concept_eid in concept_labels)
rql = ('Any C WHERE EXISTS('
'C preferred_label L, L language_code LC, '
......
......@@ -28,11 +28,11 @@ class InvalidLCSVFile(RuntimeError):
"""LCSV input is malformed."""
def __init__(self, msg, column=None):
super(InvalidLCSVFile, self).__init__(msg)
super().__init__(msg)
self.column = column
class LCSV2RDF(object):
class LCSV2RDF:
"""Create RDF triples expressing the data contained in the LCSV stream
reference : W3C unofficial draft, http://jenit.github.io/linked-csv/
"""
......@@ -80,8 +80,7 @@ class LCSV2RDF(object):
else:
end_of_prolog = True
self.check_no_information_missing()
for triple in self.parse_data_line(line):
yield triple
yield from self.parse_data_line(line)
def check_no_information_missing(self):
""" raise an InvalidLCSVFile error if necessary information are missing"""
......
......@@ -34,7 +34,7 @@ class unicode_with_language(str): # pylint: disable=invalid-name
return self
def __repr__(self):
return '<%r(%s)>' % (super(unicode_with_language, self).__repr__(), self.lang)
return '<{!r}({})>'.format(super().__repr__(), self.lang)
def __eq__(self, other):
if isinstance(other, unicode_with_language):
......@@ -60,7 +60,7 @@ def normalize_uri(uri, prefixes, raise_on_error=False):
return prefixes[prefix] + value
except KeyError:
if raise_on_error:
raise RDFRegistryError('prefix {0} not found'.format(prefix))
raise RDFRegistryError('prefix {} not found'.format(prefix))
return uri
......@@ -105,7 +105,7 @@ class RDFRegistryError(Exception):
pass
class RDFRegistry(object):
class RDFRegistry:
"""Class inspired from yams.xy / cubes.dataio.xy and holding static information about how to
convert from a Yams model to RDF and the other way around.
"""
......@@ -197,7 +197,7 @@ class RDFRegistry(object):
yield rel, rdftype, reverse
class AbstractRDFGraph(object):
class AbstractRDFGraph:
"""Abstract base class for RDF graph implementation."""
_backend_format_map = {}
......@@ -279,7 +279,7 @@ class LibRDFRDFGraph(AbstractRDFGraph):
int: 'http://www.w3.org/2001/XMLSchema#integer',
}
_xsd_py_map = dict((v, k) for k, v in _py_xsd_map.items())
_xsd_py_map = {v: k for k, v in _py_xsd_map.items()}
def __init__(self, options_string="new='yes',hash-type='memory',dir='.'"):
import RDF
......@@ -296,7 +296,7 @@ class LibRDFRDFGraph(AbstractRDFGraph):
def _load(self, source, rdf_format):
"""Add RDF triplets from a file path or URL into the graph."""
assert isinstance(source, str), \
'{0}._load() expect a path or an URL as source'.format(self.__class__.__name__)
'{}._load() expect a path or an URL as source'.format(self.__class__.__name__)
if ':/' not in source:
source = 'file://' + abspath(source)
parser = self._parser(name=rdf_format)
......@@ -393,7 +393,7 @@ class LibRDFRDFGraph(AbstractRDFGraph):
class RDFLibRDFGraph(AbstractRDFGraph):
"""rdflib based RDF graph (http://rdflib.readthedocs.org)"""
_backend_format_map = dict((x, x) for x in ('xml', 'n3', 'nt', 'rdfa'))
_backend_format_map = {x: x for x in ('xml', 'n3', 'nt', 'rdfa')}
_backend_format_map['pretty-xml'] = 'xml'
def __init__(self):
......@@ -511,7 +511,7 @@ def entities_stack(entities):
entities.update(x for x in newones if x.eid not in processed)
class RDFGraphGenerator(object):
class RDFGraphGenerator:
"""RDF Graph generator using an RDF graph implementation.
Fill the graph by calling `add_entity` with each entity to add to the graph.
......
......@@ -40,7 +40,7 @@ class Label(EntityType):
label = String(required=True, fulltextindexed=True)
kind = String(required=True, internationalizable=True, indexed=True,
vocabulary=[_('preferred'), _('alternative'), _('hidden')],
default=u'preferred')
default='preferred')
language_code = String(maxsize=5, indexed=True,
description=_('language code, e.g. "en" or "fr-fr"'))
......
......@@ -54,7 +54,7 @@ class RDFSKOSImportService(Service):
__select__ = match_kwargs('stream') & match_user_groups('managers')
def call(self, stream, **kwargs):
import_log = HTMLImportLog(getattr(stream, 'filename', u''))
import_log = HTMLImportLog(getattr(stream, 'filename', ''))
(created, updated), scheme_uris = self._do_import(stream, import_log, **kwargs)
import_log.record_info('%s entities created' % len(created))
import_log.record_info('%s entities updated' % len(updated))
......@@ -139,7 +139,7 @@ def lcsv_extentities(stream, scheme_uri, delimiter=None, encoding='utf-8', langu
def relate_concepts(extentity):
"""Relate Concept ExtEntities to the ConceptScheme"""
if extentity.etype == 'Concept':
extentity.values.setdefault('in_scheme', set([])).add(scheme_uri)
extentity.values.setdefault('in_scheme', set()).add(scheme_uri)
return extentity
return map(relate_concepts, graph_extentities(graph))
......@@ -191,10 +191,10 @@ def graph_extentities(graph):
md5hash.update(label.encode('utf-8'))
labelid = str(extentity.extid) + '#' + rtype + md5hash.hexdigest()
labels.append(ExtEntity('Label', labelid,
{'label': set([label]),
{'label': {label},
'language_code': language_code,
'kind': set([kind]),
'label_of': set([extentity.extid])}))
'kind': {kind},
'label_of': {extentity.extid}}))
# yield extentity before labels since it must be handled first in case the ExternalUri
# to Concept transformation apply
yield extentity
......
......@@ -99,10 +99,10 @@ class AddConceptToConceptAction(action.LinkToEntityAction):
target_etype = 'Concept'
def url(self):
baseurl = super(AddConceptToConceptAction, self).url()
baseurl = super().url()
entity = self.cw_rset.get_entity(0, 0)
linkto = 'in_scheme:%s:subject' % (entity.scheme.eid)
return '%s&__linkto=%s' % (baseurl, self._cw.url_quote(linkto))
return f'{baseurl}&__linkto={self._cw.url_quote(linkto)}'
class AddNarrowerConceptAction(AddConceptToConceptAction):
......@@ -134,13 +134,13 @@ class PaginatedListView(baseviews.ListView):
return navcomp.ajax_page_url(**params)
def call(self, **kwargs):
self.w(u'<div id="{0}">'.format(self.domid))
self.w(f'<div id="{self.domid}">')
nav_html = UStringIO()
self.paginate(w=nav_html.write)
self.w(nav_html.getvalue())
super(PaginatedListView, self).call(**kwargs)
super().call(**kwargs)
self.w(nav_html.getvalue())
self.w(u'</div>')
self.w('</div>')
class ConceptSchemePrimaryView(tabs.TabbedPrimaryView):
......@@ -265,7 +265,7 @@ class ConceptSchemeAutoform(autoform.AutomaticEntityForm):
& score_entity(lambda x: not x.has_eid()))
def __init__(self, *args, **kwargs):
super(ConceptSchemeAutoform, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self.fieldsets_in_order = [None, _('inline csv import')]
for field in ImportSchemeConceptsForm._fields_:
field = copy(field)
......@@ -295,7 +295,7 @@ class ConceptSchemeEditController(editcontroller.EditController):
def edit_entity(self, formparams, multiple=False):
"""Use created entity and import concepts from specified file."""
eid = super(ConceptSchemeEditController, self).edit_entity(formparams, multiple=multiple)
eid = super().edit_entity(formparams, multiple=multiple)
form = self._cw.form
entity = self._cw.entity_from_eid(eid)
_, fobj = form['stream']
......@@ -309,7 +309,7 @@ class ConceptSchemeEditController(editcontroller.EditController):
# simple file import ###########################################################
class ImportSchemeConceptsMixIn(object):
class ImportSchemeConceptsMixIn:
__regid__ = 'skos.scheme.import'
__select__ = (is_instance('ConceptScheme')
& match_user_groups('managers')
......@@ -321,17 +321,17 @@ class ImportSchemeConceptsForm(ImportSchemeConceptsMixIn, forms.EntityFieldsForm
filefield = ff.FileField(name='stream', label=_('file'), required=True)
formatfield = ff.StringField(name='format', label=_('file format'),
required=True, internationalizable=True,
choices=[(_('simple'), u'simple'),
(_(u'LCSV'), u'lcsv')], sort=False)
choices=[(_('simple'), 'simple'),
(_('LCSV'), 'lcsv')], sort=False)
encoding = ff.StringField(name='encoding', label=_('file encoding'),
internationalizable=True,
choices=[(_('utf-8'), u'utf-8'),
(_('latin 1'), u'latin1')], sort=False)
choices=[(_('utf-8'), 'utf-8'),
(_('latin 1'), 'latin1')], sort=False)
language = ff.StringField(name='language_code',
label=_('language of concepts in the file'),
internationalizable=True,
choices=[(_('french'), u'fr'),
(_('english'), u'en')], sort=False)
choices=[(_('french'), 'fr'),
(_('english'), 'en')], sort=False)
delimiter = ff.StringField(name='delimiter', label=_('hierarchical delimiter'),
internationalizable=True,
choices=[(_('tabulation'), 'tab'),
......@@ -431,9 +431,9 @@ class SKOSImportForm(forms.FieldsForm):
formatfield = ff.StringField(name='format', label=_('RDF format'),
internationalizable=True,
choices=[(_('per extension'), ''),
(_('xml'), u'xml'),
(_('ntriples'), u'nt'),
(_('n3'), u'n3')],
(_('xml'), 'xml'),
(_('ntriples'), 'nt'),
(_('n3'), 'n3')],
sort=False)
form_buttons = [fw.SubmitButton(label=_('to_import'))]
......@@ -469,10 +469,10 @@ class SKOSImportResultView(View):
msg = self._cw._('SKOS import completed: %s') % self._cw.view('csv', schemes)
else:
msg = self._cw._('SKOS import completed but no scheme was found')
self.w(u'<div class="message">%s</div>' % msg)
self.w('<div class="message">%s</div>' % msg)
if logs:
self._cw.view('cw.log.table',
pyvalue=cwsources.log_to_table(self._cw, u''.join(logs)),
pyvalue=cwsources.log_to_table(self._cw, ''.join(logs)),
default_level='Info', w=self.w)
else:
......@@ -574,4 +574,4 @@ class SKOSSourceFormRenderer(formrenderers.FormRenderer):
w('<div class="help-block">%s</div>' % (self._cw._(
'Please specify <b>an arbitrary short name</b> for the data source and '
'<b>one or more URL from which RDF data may be retrieved</b>.')))
super(SKOSSourceFormRenderer, self).render(w, form, values)
super().render(w, form, values)
......@@ -52,9 +52,9 @@ if relationwidget_imported:
"""
baserql, args = self.constrained_rql()
# detect language first
languages = set(lang for lang, in self._cw.execute(
languages = {lang for lang, in self._cw.execute(
'DISTINCT Any OLC WHERE ' + baserql + ', O preferred_label OL, '
'OL language_code OLC', args))
'OL language_code OLC', args)}
for lang in self.preferred_languages():
if lang in languages:
break
......
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