Commit ad321e48 authored by Laurent Peuch's avatar Laurent Peuch
Browse files

style: hg-formatsource with black

parent c6ea6004dfda
This diff is collapsed.
......@@ -31,23 +31,30 @@ from logilab.common.logging_ext import set_log_methods
from yams.constraints import BASE_CONVERTERS, BASE_CHECKERS
from yams.schema import role_name as rname
from cubicweb.__pkginfo__ import version as __version__ # noqa
from cubicweb.__pkginfo__ import version as __version__ # noqa
# make all exceptions accessible from the package
from logilab.common.registry import ObjectNotFound, NoSelectableObject, RegistryNotFound # noqa
from logilab.common.registry import (
ObjectNotFound,
NoSelectableObject,
RegistryNotFound,
) # noqa
from yams import ValidationError
from cubicweb._exceptions import * # noqa
from io import BytesIO
# ignore the pygments UserWarnings
warnings.filterwarnings('ignore', category=UserWarning,
message='.*was already imported',
module='.*pygments')
warnings.filterwarnings(
"ignore",
category=UserWarning,
message=".*was already imported",
module=".*pygments",
)
# pre python 2.7.2 safety
logging.basicConfig()
set_log_methods(sys.modules[__name__], logging.getLogger('cubicweb'))
set_log_methods(sys.modules[__name__], logging.getLogger("cubicweb"))
# this is necessary for i18n devtools test where chdir is done while __path__ is relative, which
# breaks later imports
......@@ -62,16 +69,19 @@ _ = str
class Binary(BytesIO):
"""class to hold binary data. Use BytesIO to prevent use of unicode data"""
_allowed_types = (bytes, bytearray, memoryview)
def __init__(self, buf=b''):
assert isinstance(buf, self._allowed_types), \
def __init__(self, buf=b""):
assert isinstance(buf, self._allowed_types), (
"Binary objects must use bytes/buffer objects, not %s" % buf.__class__
)
super().__init__(buf)
def write(self, data):
assert isinstance(data, self._allowed_types), \
assert isinstance(data, self._allowed_types), (
"Binary objects must use bytes/buffer objects, not %s" % data.__class__
)
# don't call super, BytesIO may be an old-style class (on python < 2.7.4)
BytesIO.write(self, data)
......@@ -83,7 +93,7 @@ class Binary(BytesIO):
"""
pos = self.tell()
self.seek(0)
if sys.platform == 'win32':
if sys.platform == "win32":
while True:
# the 16kB chunksize comes from the shutil module
# in stdlib
......@@ -103,8 +113,8 @@ class Binary(BytesIO):
Windows SMB shares
"""
binary = Binary()
with open(filename, 'rb') as fobj:
if sys.platform == 'win32':
with open(filename, "rb") as fobj:
if sys.platform == "win32":
while True:
# the 16kB chunksize comes from the shutil module
# in stdlib
......@@ -126,13 +136,13 @@ class Binary(BytesIO):
@classmethod
def zpickle(cls, obj):
""" return a Binary containing a gzipped pickle of obj """
"""return a Binary containing a gzipped pickle of obj"""
retval = cls()
retval.write(zlib.compress(pickle.dumps(obj, protocol=2)))
return retval
def unzpickle(self):
""" decompress and loads the stream before returning it """
"""decompress and loads the stream before returning it"""
return pickle.loads(zlib.decompress(self.getvalue()))
......@@ -140,7 +150,7 @@ def check_password(eschema, value):
return isinstance(value, (bytes, Binary))
BASE_CHECKERS['Password'] = check_password
BASE_CHECKERS["Password"] = check_password
def str_or_binary(value):
......@@ -149,7 +159,7 @@ def str_or_binary(value):
return bytes(value)
BASE_CONVERTERS['Password'] = str_or_binary
BASE_CONVERTERS["Password"] = str_or_binary
# use this dictionary to rename entity types while keeping bw compat
......@@ -161,9 +171,9 @@ CW_MIGRATION_MAP = {}
def neg_role(role):
if role == 'subject':
return 'object'
return 'subject'
if role == "subject":
return "object"
return "subject"
def role(obj):
......@@ -196,6 +206,7 @@ class CubicWebEventManager(object):
emit() accepts an additional context parameter that will be passed
to the callback if specified (and only in that case)
"""
def __init__(self):
self.callbacks = {}
......@@ -223,9 +234,11 @@ def onevent(event, *args, **kwargs):
...
>>>
"""
def _decorator(func):
CW_EVENT_MANAGER.bind(event, func, *args, **kwargs)
return func
return _decorator
......@@ -246,12 +259,14 @@ def validation_error(entity, errors, substitutions=None, i18nvalues=None):
for key in list(errors):
if isinstance(key, tuple):
errors[rname(*key)] = errors.pop(key)
return ValidationError(getattr(entity, 'eid', entity), errors,
substitutions, i18nvalues)
return ValidationError(
getattr(entity, "eid", entity), errors, substitutions, i18nvalues
)
# exceptions ##################################################################
class ProgrammingError(Exception):
"""Exception raised for errors that are related to the database's operation
and not necessarily under the control of the programmer, e.g. an unexpected
......
......@@ -23,25 +23,25 @@ software
modname = distname = "cubicweb"
numversion = (3, 31, 1)
version = '.'.join(str(num) for num in numversion)
version = ".".join(str(num) for num in numversion)
description = "a repository of entities / relations for knowledge management"
author = "Logilab"
author_email = "contact@logilab.fr"
web = 'https://www.cubicweb.org'
license = 'LGPL'
web = "https://www.cubicweb.org"
license = "LGPL"
classifiers = [
'Environment :: Web Environment',
'Framework :: CubicWeb',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: JavaScript',
"Environment :: Web Environment",
"Framework :: CubicWeb",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: JavaScript",
]
# data files that shall be copied into the main package directory
package_data = {
'cubicweb.web.views': ['*.pt'],
'cubicweb.pyramid': ['development.ini.tmpl'],
"cubicweb.web.views": ["*.pt"],
"cubicweb.pyramid": ["development.ini.tmpl"],
}
......@@ -28,6 +28,7 @@ from yams import ValidationError # noqa: F401
class CubicWebException(Exception):
"""base class for cubicweb server exception"""
msg = ""
def __str__(self):
......@@ -37,7 +38,7 @@ class CubicWebException(Exception):
else:
return self.msg
else:
return u' '.join(str(arg) for arg in self.args)
return " ".join(str(arg) for arg in self.args)
class ConfigurationError(CubicWebException):
......@@ -63,6 +64,7 @@ class SourceException(CubicWebException):
class CubicWebRuntimeError(CubicWebException):
"""base class for runtime exceptions"""
# repository exceptions #######################################################
......@@ -84,7 +86,8 @@ class BadConnectionId(ConnectionError):
class UnknownEid(RepositoryError):
"""the eid is not defined in the system tables"""
msg = 'No entity with eid %s in the repository'
msg = "No entity with eid %s in the repository"
class UniqueTogetherError(RepositoryError):
......@@ -92,17 +95,17 @@ class UniqueTogetherError(RepositoryError):
def __init__(self, session, **kwargs):
self.session = session
assert 'rtypes' in kwargs or 'cstrname' in kwargs
assert "rtypes" in kwargs or "cstrname" in kwargs
self.kwargs = kwargs
# fill cache while the session is open
self.rtypes
@cachedproperty
def rtypes(self):
if 'rtypes' in self.kwargs:
return self.kwargs['rtypes']
cstrname = str(self.kwargs['cstrname'])
cstr = self.session.find('CWUniqueTogetherConstraint', name=cstrname).one()
if "rtypes" in self.kwargs:
return self.kwargs["rtypes"]
cstrname = str(self.kwargs["cstrname"])
cstr = self.session.find("CWUniqueTogetherConstraint", name=cstrname).one()
return sorted(rtype.name for rtype in cstr.relations)
......@@ -119,12 +122,14 @@ class ViolatedConstraint(RepositoryError):
# security exceptions #########################################################
class Unauthorized(SecurityError):
"""raised when a user tries to perform an action without sufficient
credentials
"""
msg = u'You are not allowed to perform this operation'
msg1 = u'You are not allowed to perform %s operation on %s'
msg = "You are not allowed to perform this operation"
msg1 = "You are not allowed to perform %s operation on %s"
var = None
def __str__(self):
......@@ -132,15 +137,15 @@ class Unauthorized(SecurityError):
if self.args and len(self.args) == 2:
return self.msg1 % self.args
if self.args:
return u' '.join(self.args)
return " ".join(self.args)
return self.msg
except Exception as ex:
return str(ex)
class Forbidden(SecurityError):
"""raised when a user tries to perform a forbidden action
"""
"""raised when a user tries to perform a forbidden action"""
# source exceptions ###########################################################
......@@ -149,14 +154,17 @@ class EidNotInSource(SourceException):
"""trying to access an object with a particular eid from a particular
source has failed
"""
msg = 'No entity with eid %s in %s'
msg = "No entity with eid %s in %s"
# registry exceptions #########################################################
class UnknownProperty(RegistryException):
"""property found in database but unknown in registry"""
# query exception #############################################################
......@@ -177,8 +185,7 @@ class MultipleResultsError(CubicWebRuntimeError):
class NoResultError(CubicWebRuntimeError):
"""raised when no result is found but at least one is expected.
"""
"""raised when no result is found but at least one is expected."""
class UndoTransactionException(QueryError):
......@@ -203,13 +210,15 @@ class UndoTransactionException(QueryError):
:type errors: list
:param errors: List of errors occurred during undoing
"""
msg = u"The following error(s) occurred while undoing transaction #%d : %s"
msg = "The following error(s) occurred while undoing transaction #%d : %s"
def __init__(self, txuuid, errors):
super(UndoTransactionException, self).__init__(txuuid, errors)
self.txuuid = txuuid
self.errors = errors
# tools exceptions ############################################################
......
......@@ -18,6 +18,7 @@
import gc, types, weakref
from cubicweb.schema import CubicWebRelationSchema, CubicWebEntitySchema
try:
from cubicweb.web.request import _NeedAuthAccessMock
except ImportError:
......@@ -26,26 +27,42 @@ except ImportError:
listiterator = type(iter([]))
IGNORE_CLASSES = (
type, tuple, dict, list, set, frozenset, type(len),
type,
tuple,
dict,
list,
set,
frozenset,
type(len),
weakref.ref,
listiterator,
property, classmethod,
types.ModuleType, types.FunctionType, types.MethodType,
types.MemberDescriptorType, types.GetSetDescriptorType,
)
property,
classmethod,
types.ModuleType,
types.FunctionType,
types.MethodType,
types.MemberDescriptorType,
types.GetSetDescriptorType,
)
if _NeedAuthAccessMock is not None:
IGNORE_CLASSES = IGNORE_CLASSES + (_NeedAuthAccessMock,)
def _get_counted_class(obj, classes):
for cls in classes:
if isinstance(obj, cls):
return cls
raise AssertionError()
def gc_info(countclasses,
ignoreclasses=IGNORE_CLASSES,
viewreferrersclasses=(), showobjs=False, maxlevel=1):
def gc_info(
countclasses,
ignoreclasses=IGNORE_CLASSES,
viewreferrersclasses=(),
showobjs=False,
maxlevel=1,
):
gc.collect()
gc.collect()
counters = {}
......@@ -59,8 +76,7 @@ def gc_info(countclasses,
counters[cls.__name__] = 1
elif not isinstance(obj, ignoreclasses):
try:
key = '%s.%s' % (obj.__class__.__module__,
obj.__class__.__name__)
key = "%s.%s" % (obj.__class__.__module__, obj.__class__.__name__)
except AttributeError:
key = str(obj)
try:
......@@ -68,7 +84,7 @@ def gc_info(countclasses,
except KeyError:
ocounters[key] = 1
if isinstance(obj, viewreferrersclasses):
print(' ', obj, referrers(obj, showobjs, maxlevel))
print(" ", obj, referrers(obj, showobjs, maxlevel))
garbage = [repr(obj) for obj in gc.garbage]
return counters, ocounters, garbage
......@@ -76,8 +92,12 @@ def gc_info(countclasses,
def referrers(obj, showobj=False, maxlevel=1):
objreferrers = _referrers(obj, maxlevel)
try:
return sorted(set((type(x), showobj and x or getattr(x, '__name__', '%#x' % id(x)))
for x in objreferrers))
return sorted(
set(
(type(x), showobj and x or getattr(x, "__name__", "%#x" % id(x)))
for x in objreferrers
)
)
except TypeError:
s = set()
unhashable = []
......@@ -88,6 +108,7 @@ def referrers(obj, showobj=False, maxlevel=1):
unhashable.append(x)
return sorted(s) + unhashable
def _referrers(obj, maxlevel, _seen=None, _level=0):
interesting = []
if _seen is None:
......@@ -103,9 +124,9 @@ def _referrers(obj, maxlevel, _seen=None, _level=0):
if isinstance(x, (list, tuple, set, dict, listiterator)):
if _level >= maxlevel:
pass
#interesting.append(x)
# interesting.append(x)
else:
interesting += _referrers(x, maxlevel, _seen, _level+1)
interesting += _referrers(x, maxlevel, _seen, _level + 1)
else:
interesting.append(x)
return interesting
......@@ -38,6 +38,7 @@ from logilab.common.registry import RegistrableObject, yes
# the base class for all appobjects ############################################
class AppObject(RegistrableObject):
"""This is the base class for CubicWeb application objects which are
selected in a request context.
......@@ -74,6 +75,7 @@ class AppObject(RegistrableObject):
such as `AnyEntity`, `EntityView`, `AnyRsetView`, `Action`...
"""
__select__ = yes()
@classmethod
......@@ -84,11 +86,11 @@ class AppObject(RegistrableObject):
the right hook to create an instance for example). By default the
appobject is returned without any transformation.
"""
pdefs = getattr(cls, 'cw_property_defs', {})
pdefs = getattr(cls, "cw_property_defs", {})
for propid, pdef in pdefs.items():
pdef = pdef.copy() # may be shared
pdef['default'] = getattr(cls, propid, pdef['default'])
pdef['sitewide'] = getattr(cls, 'site_wide', pdef.get('sitewide'))
pdef = pdef.copy() # may be shared
pdef["default"] = getattr(cls, propid, pdef["default"])
pdef["sitewide"] = getattr(cls, "site_wide", pdef.get("sitewide"))
registry.vreg.register_property(cls._cwpropkey(propid), **pdef)
assert callable(cls.__select__), cls
return cls
......@@ -97,9 +99,9 @@ class AppObject(RegistrableObject):
super(AppObject, self).__init__()
self._cw = req
try:
self.cw_rset = extra.pop('rset')
self.cw_row = extra.pop('row', None)
self.cw_col = extra.pop('col', None)
self.cw_rset = extra.pop("rset")
self.cw_row = extra.pop("row", None)
self.cw_col = extra.pop("col", None)
except KeyError:
pass
self.cw_extra_kwargs = extra
......@@ -126,7 +128,7 @@ class AppObject(RegistrableObject):
"""return cw property key for the property of the given id for this
class
"""
return '%s.%s.%s' % (cls.__registry__, cls.__regid__, propid)
return "%s.%s.%s" % (cls.__registry__, cls.__regid__, propid)
def cw_propval(self, propid):
"""return cw property value associated to key
......@@ -137,6 +139,7 @@ class AppObject(RegistrableObject):
# these are overridden by set_log_methods below
# only defining here to prevent pylint from complaining
info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None
info = warning = error = critical = exception = debug = lambda msg, *a, **kw: None
set_log_methods(AppObject, getLogger('cubicweb.appobject'))
set_log_methods(AppObject, getLogger("cubicweb.appobject"))
......@@ -29,7 +29,7 @@ _CYPHERERS = {}
def _cypherer(seed):
if isinstance(seed, str):
seed = seed.encode('utf-8')
seed = seed.encode("utf-8")
try:
return _CYPHERERS[seed]
except KeyError:
......@@ -39,13 +39,13 @@ def _cypherer(seed):
def encrypt(data, seed):
string = pickle.dumps(data)
string = string + b'*' * (8 - len(string) % 8)
string = string + b"*" * (8 - len(string) % 8)
string = b64encode(_cypherer(seed).encrypt(string))
return string.decode('utf-8')
return string.decode("utf-8")
def decrypt(string, seed):
string = string.encode('utf-8')
string = string.encode("utf-8")
# pickle ignores trailing characters so we do not need to strip them off
string = _cypherer(seed).decrypt(b64decode(string))
return pickle.loads(string)
This diff is collapsed.
This diff is collapsed.
......@@ -112,10 +112,16 @@ class cwGNUTranslations(gettext.GNUTranslations):
return tmsg
def translation(domain, localedir=None, languages=None,
class_=None, fallback=False, codeset=None):
def translation(
domain, localedir=None, languages=None, class_=None, fallback=False, codeset=None
):
if class_ is None:
class_ = cwGNUTranslations
return gettext.translation(domain, localedir=localedir,
languages=languages, class_=class_,
fallback=fallback, codeset=codeset)
return gettext.translation(
domain,
localedir=localedir,
languages=languages,
class_=class_,
fallback=fallback,
codeset=codeset,
)
This diff is collapsed.
......@@ -28,6 +28,7 @@ def callfunc_every(func, number, iterable):
func()
func()
# import for backward compat
from cubicweb.dataimport.stores import *
from cubicweb.dataimport.pgstore import *
......
......@@ -35,11 +35,18 @@ def count_lines(stream_or_filename):
return i + 1
def ucsvreader_pb(stream_or_path, encoding='utf-8', delimiter=',', quotechar='"',
skipfirst=False, withpb=True, skip_empty=True):
def ucsvreader_pb(
stream_or_path,
encoding="utf-8",
delimiter=",",
quotechar='"',
skipfirst=False,
withpb=True,
skip_empty=True,
):
"""same as :func:`ucsvreader` but a progress bar is displayed as we iter on rows"""
if isinstance(stream_or_path, str):
stream = open(stream_or_path, 'rb')
stream = open(stream_or_path, "rb")
else:
stream = stream_or_path
rowcount = count_lines(stream)
......@@ -47,16 +54,29 @@ def ucsvreader_pb(stream_or_path, encoding='utf-8', delimiter=',', quotechar='"'
rowcount -= 1
if withpb:
pb = shellutils.ProgressBar(rowcount, 50)
for urow in ucsvreader(stream, encoding, delimiter, quotechar,
skipfirst=skipfirst, skip_empty=skip_empty):
for urow in ucsvreader(
stream,
encoding,
delimiter,
quotechar,
skipfirst=skipfirst,
skip_empty=skip_empty,
):
yield urow
if withpb:
pb.update()
print(' %s rows imported' % rowcount)
print(" %s rows imported" % rowcount)
def ucsvreader(stream, encoding='utf-8', delimiter=',', quotechar='"',
skipfirst=False, ignore_errors=False, skip_empty=True):
def ucsvreader(
stream,
encoding="utf-8",
delimiter=",",
quotechar='"',
skipfirst=False,
ignore_errors=False,