Commit 96bc7abc authored by Recteur LP's avatar Recteur LP

PEP8 Formatting

parent 1fac8ff9
Pipeline #1533 failed with stages
in 2 minutes and 6 seconds
......@@ -13,7 +13,7 @@ class cache():
self.r = redis.StrictRedis(host=server, port=port, db=db)
try:
self.r.ping()
except:
except Exception:
log.error("Cannot connect to redis server {}:{} db:{}".format(server, port, db))
raise Exception("Cannot connect to {}:{} db:{}".format(server, port, db))
......@@ -43,5 +43,5 @@ class cache():
if self.r.get("pyrmin_{}_{}".format(self.namespace, name)) is not None:
return json.loads(self.r.get("pyrmin_{}_{}".format(self.namespace, name)))
else:
log.warning("Cache not found with error: " + str(e))
raise Exception("Cache not found with error: " + str(e))
log.warning("Cache not found")
raise Exception("Cache not found")
......@@ -147,10 +147,10 @@ class Core():
log.error("Error:", exc_info=True)
for check in submenu:
if (
not user.admin and
check['href'] in self.config and
"groups" in self.config[check['href']] and not
user().ingroups(self.config[check['href']]['groups'])
not user.admin
and check['href'] in self.config
and "groups" in self.config[check['href']]
and not user().ingroups(self.config[check['href']]['groups'])
):
submenu.remove(check)
menu.update({"childs": submenu})
......@@ -175,10 +175,10 @@ class Core():
pconfig = self.get_plugin_config(plugin.__namespace__)
if (
(
user.auth and
(
user.admin or
("groups" not in pconfig or user.ingroups(pconfig['groups']))
user.auth
and (
user.admin
or ("groups" not in pconfig or user.ingroups(pconfig['groups']))
)
) or (hasattr(plugin, '__public__') and plugin.__public__)
):
......@@ -264,9 +264,9 @@ class Web():
path = os.path.join(os.path.dirname(p.__file__), static)
if os.path.exists(path):
log.debug(
'Appending path: ' +
path + ' to ' +
'/'.join(['/plugins', p.__namespace__, static])
'Appending path: '
+ path + ' to '
+ '/'.join(['/plugins', p.__namespace__, static])
)
self.core_conf.update({
'/'.join(['/plugins', p.__namespace__, static]): {
......@@ -277,8 +277,8 @@ class Web():
else:
self.config['plugins'].remove(plugin)
log.error(
"Plugin " + plugin + " not installed correctly !" +
" Re-install it or Remove it from your plugins"
"Plugin " + plugin + " not installed correctly !"
+ " Re-install it or Remove it from your plugins"
)
sys.exit(1)
else:
......@@ -289,8 +289,8 @@ class Web():
from .handlers import render_plugin
if hasattr(module, '__static__') and module.__static__:
log.debug(
"Loading Static App: " + module.__name__ + " on /" + module.__namespace__ +
" with static: " + os.path.join(self.config['plugins_path'], module.__namespace__, module.__static__)
"Loading Static App: " + module.__name__ + " on /" + module.__namespace__
+ " with static: " + os.path.join(self.config['plugins_path'], module.__namespace__, module.__static__)
)
self.core_conf.update({
"/" + module.__namespace__: {
......
......@@ -19,19 +19,19 @@ __version__ = "0.4.0"
def error(self, msg='', context='', severity=logging.INFO, traceback=False):
"""Write the given ``msg`` to the error log.
"""Write the given ``msg`` to the error log.
This is not just for errors! Applications may call this at any time
to log application-specific information.
This is not just for errors! Applications may call this at any time
to log application-specific information.
If ``traceback`` is True, the traceback of the current exception
(if any) will be appended to ``msg``.
"""
exc_info = None
if traceback:
exc_info = cherrypy._cperror._exc_info()
If ``traceback`` is True, the traceback of the current exception
(if any) will be appended to ``msg``.
"""
exc_info = None
if traceback:
exc_info = cherrypy._cperror._exc_info()
self.error_log.log(severity, ' '.join((context, msg)), exc_info=exc_info)
self.error_log.log(severity, ' '.join((context, msg)), exc_info=exc_info)
def init_colorlog(level='INFO', log=logging.getLogger('pyrmin')):
......@@ -49,6 +49,7 @@ def init_colorlog(level='INFO', log=logging.getLogger('pyrmin')):
class logwrap(logging.getLoggerClass()):
""" Pretty Print Python Object as JSON in Debug Mode
"""
def __init__(self, name):
logging.Logger.__init__(self, name)
......
......@@ -10,6 +10,7 @@ import hashlib
import base64
import six
from six.moves.urllib.parse import urlencode
from cherrypy._cpcompat import parse_http_list, parse_keqv_list
from ._log import log
from ._core import Core
......@@ -124,9 +125,9 @@ class LogIn(object):
def login(self, username, password):
log.debug('Login user: {}'.format(username))
if (
'pyrmin' in self.config and
'auth_module' in self.config['pyrmin'] and
self.config['pyrmin']['auth_module'] in Core(self.config).get_conf(self.config['pyrmin']['config_path'])
'pyrmin' in self.config
and 'auth_module' in self.config['pyrmin']
and self.config['pyrmin']['auth_module'] in Core(self.config).get_conf(self.config['pyrmin']['config_path'])
):
try:
log.debug("Trying auth with auth_module: {}".format(self.config['pyrmin']['auth_module']))
......@@ -145,21 +146,21 @@ class LogIn(object):
log.debug("Trying local users ...")
password = hashlib.sha256(password).hexdigest()
if (
username == "admin" and
'password' in self.config['pyrmin'] and
password == self.config['pyrmin']['password']
username == "admin"
and 'password' in self.config['pyrmin']
and password == self.config['pyrmin']['password']
):
cherrypy.session['user'] = {
"username": username,
"auth": True,
"admin": True,
"displayname": 'Administrator',
"img": self.gravatar_url(username)
"img": self.get_image_url(username)
}
return True
elif (
'users' in self.config['pyrmin'] and
len(self.config['pyrmin']['users']) > 0
'users' in self.config['pyrmin']
and len(self.config['pyrmin']['users']) > 0
):
for u in self.config['pyrmin']['users']:
if u['username'] == username and u['password'] == password:
......@@ -168,12 +169,12 @@ class LogIn(object):
"auth": True,
"admin": False,
"displayname": username,
"img": self.gravatar_url(username)
"img": self.get_image_url(username)
}
return True
return False
def gravatar_url(self, mail):
def get_image_url(self, mail):
""" Return Gravatar url for mail
"""
if 'pyrmin' in self.config and self.config['pyrmin']['gravatar']:
......
......@@ -130,9 +130,9 @@ class render_plugin(render_class):
if not Core(self.config).check_plugin_perms(self.plugin, self.user):
raise cherrypy.HTTPError(401, 'Log in first.')
if (
not self.user.admin and
"groups" in self.config and
not self.user.ingroups(self.config['groups'], self.user.username)
not self.user.admin
and "groups" in self.config
and not self.user.ingroups(self.config['groups'], self.user.username)
):
raise cherrypy.HTTPError(401, 'Not in groups: ' + ', '.join(self.config['groups']))
......
......@@ -8,7 +8,6 @@ import os
# import sys
import cherrypy
import inspect
import traceback
from collections import namedtuple
from jinja2 import Environment, FileSystemLoader
......@@ -192,7 +191,7 @@ class tools():
ppath = os.path.basename(os.path.normpath(os.path.dirname(caller[1])))
try:
return cherrypy.session[ppath][key]
except:
except Exception:
return False
def set_session(self, key, value):
......@@ -239,7 +238,7 @@ def to_json(func):
try:
response.headers["Content-Type"] = "application/json"
return json.dumps(value)
except:
except Exception:
return value
return wrapper
......
......@@ -5,8 +5,6 @@
# Author: PyRmin.io
import cherrypy
import hashlib
from six.moves.urllib.parse import urlencode
from ._log import log
from .plugins import Plugins
from ._core import Core
......@@ -15,9 +13,6 @@ import getpass
import socket
__gravatar_url__ = 'https://www.gravatar.com/avatar/'
class User():
"""
pyrmin User ObjectClass
......@@ -55,14 +50,14 @@ class User():
def __set_auth_module_user(self):
if (
not self.header_auth() and
hasattr(cherrypy, 'session') and
'user' not in cherrypy.session and
'pyrmin' in self.config and
'auth_module' in self.config['pyrmin'] and
self.config['pyrmin']['auth_module'] in Core(self.config).get_conf(self.config['pyrmin']['config_path']) and
'Remote-User' in cherrypy.request.headers and
cherrypy.request.headers['Remote-User'] != "(null)"
not self.header_auth()
and hasattr(cherrypy, 'session')
and 'user' not in cherrypy.session
and 'pyrmin' in self.config
and 'auth_module' in self.config['pyrmin']
and self.config['pyrmin']['auth_module'] in Core(self.config).get_conf(self.config['pyrmin']['config_path'])
and 'Remote-User' in cherrypy.request.headers
and cherrypy.request.headers['Remote-User'] != "(null)"
):
name = cherrypy.request.headers['Remote-User']
log.debug("Remote-User: " + name)
......@@ -77,7 +72,7 @@ class User():
try:
# Get user from cache/session
return cherrypy.session['groups']
except:
except Exception:
return False
def ingroups(self, groups, username=None):
......@@ -106,9 +101,10 @@ class User():
}
else:
if (
'pyrmin' in self.config and
'auth_module' in self.config['pyrmin'] and
self.config['pyrmin']['auth_module'] in Core(self.config).get_conf(self.config['pyrmin']['config_path'])
'pyrmin' in self.config
and 'auth_module' in self.config['pyrmin']
and self.config['pyrmin']['auth_module']
in Core(self.config).get_conf(self.config['pyrmin']['config_path'])
):
return Plugins(self.config).get(self.config['pyrmin']['auth_module']).auth(
Core(self.config).get_conf(self.config['pyrmin']['config_path'])[
......
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