forked from platypush/platypush
[chore] LINT fixes
This commit is contained in:
parent
3e3d47aa44
commit
dd862db29a
1 changed files with 35 additions and 16 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
import collections.abc
|
||||||
|
import datetime
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
@ -7,7 +9,11 @@ from platypush.config import Config
|
||||||
from platypush.backend.http.app import template_folder
|
from platypush.backend.http.app import template_folder
|
||||||
|
|
||||||
|
|
||||||
class HttpUtils(object):
|
class HttpUtils:
|
||||||
|
"""
|
||||||
|
Common utilities used by the HTTP backend and jinja templates.
|
||||||
|
"""
|
||||||
|
|
||||||
log = logging.getLogger('platypush:web')
|
log = logging.getLogger('platypush:web')
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -15,23 +21,30 @@ class HttpUtils(object):
|
||||||
if not isinstance(columns, int):
|
if not isinstance(columns, int):
|
||||||
try:
|
try:
|
||||||
columns = int(columns)
|
columns = int(columns)
|
||||||
except ValueError:
|
except ValueError as e:
|
||||||
raise RuntimeError('columns should be a number, got {} ({})'.format(type(columns), columns))
|
raise RuntimeError(
|
||||||
|
f'columns should be a number, got {type(columns)} ({columns})'
|
||||||
|
) from e
|
||||||
|
|
||||||
if 1 <= columns <= 12:
|
if 1 <= columns <= 12:
|
||||||
return 'col-{}'.format(columns)
|
return f'col-{columns}'
|
||||||
|
|
||||||
raise RuntimeError('Constraint violation: should be 1 <= columns <= 12, ' +
|
raise RuntimeError(
|
||||||
'got columns={}'.format(columns))
|
'Constraint violation: should be 1 <= columns <= 12, '
|
||||||
|
f'got columns={columns}'
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def search_directory(directory, *extensions, recursive=False):
|
def search_directory(directory, *extensions, recursive=False):
|
||||||
files = []
|
files = []
|
||||||
|
|
||||||
if recursive:
|
if recursive:
|
||||||
for root, subdirs, files in os.walk(directory):
|
for root, _, files in os.walk(directory):
|
||||||
for file in files:
|
for file in files:
|
||||||
if not extensions or os.path.splitext(file)[1].lower() in extensions:
|
if (
|
||||||
|
not extensions
|
||||||
|
or os.path.splitext(file)[1].lower() in extensions
|
||||||
|
):
|
||||||
files.append(os.path.join(root, file))
|
files.append(os.path.join(root, file))
|
||||||
else:
|
else:
|
||||||
for file in os.listdir(directory):
|
for file in os.listdir(directory):
|
||||||
|
@ -54,12 +67,15 @@ class HttpUtils(object):
|
||||||
break
|
break
|
||||||
|
|
||||||
if not uri:
|
if not uri:
|
||||||
raise RuntimeError(('Directory {} not found among the available ' +
|
raise RuntimeError(
|
||||||
'static resources on the webserver').format(
|
(
|
||||||
directory))
|
'Directory {} not found among the available '
|
||||||
|
+ 'static resources on the webserver'
|
||||||
|
).format(directory)
|
||||||
|
)
|
||||||
|
|
||||||
results = [
|
results = [
|
||||||
re.sub('^{}(.*)$'.format(resource_path), uri + '\\1', path)
|
re.sub(fr'^{resource_path}(.*)$', uri + '\\1', path)
|
||||||
for path in cls.search_directory(directory, *extensions)
|
for path in cls.search_directory(directory, *extensions)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -68,14 +84,14 @@ class HttpUtils(object):
|
||||||
@classmethod
|
@classmethod
|
||||||
def to_json(cls, data):
|
def to_json(cls, data):
|
||||||
def json_parse(x):
|
def json_parse(x):
|
||||||
if type(x) == __import__('datetime').timedelta:
|
if isinstance(x, datetime.timedelta):
|
||||||
return x.days * 24 * 60 * 60 + x.seconds + x.microseconds / 1e6
|
return x.days * 24 * 60 * 60 + x.seconds + x.microseconds / 1e6
|
||||||
|
|
||||||
# Ignore non-serializable attributes
|
# Ignore non-serializable attributes
|
||||||
cls.log.warning('Non-serializable attribute type "{}": {}'.format(type(x), x))
|
cls.log.warning('Non-serializable attribute type "%s": %s', type(x), x)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if isinstance(data, type({}.keys())):
|
if isinstance(data, collections.abc.KeysView):
|
||||||
# Convert dict_keys to list before serializing
|
# Convert dict_keys to list before serializing
|
||||||
data = list(data)
|
data = list(data)
|
||||||
return json.dumps(data, default=json_parse)
|
return json.dumps(data, default=json_parse)
|
||||||
|
@ -97,7 +113,9 @@ class HttpUtils(object):
|
||||||
# noinspection PyTypeChecker
|
# noinspection PyTypeChecker
|
||||||
return [
|
return [
|
||||||
os.path.join(directory, file)
|
os.path.join(directory, file)
|
||||||
for root, path, files in os.walk(os.path.abspath(os.path.join(template_folder, directory)))
|
for _, __, files in os.walk(
|
||||||
|
os.path.abspath(os.path.join(template_folder, directory))
|
||||||
|
)
|
||||||
for file in files
|
for file in files
|
||||||
if file.endswith('.html') or file.endswith('.htm')
|
if file.endswith('.html') or file.endswith('.htm')
|
||||||
]
|
]
|
||||||
|
@ -112,4 +130,5 @@ class HttpUtils(object):
|
||||||
path = path[0] if len(path) == 1 else os.path.join(*path)
|
path = path[0] if len(path) == 1 else os.path.join(*path)
|
||||||
return os.path.isfile(path)
|
return os.path.isfile(path)
|
||||||
|
|
||||||
|
|
||||||
# vim:sw=4:ts=4:et:
|
# vim:sw=4:ts=4:et:
|
||||||
|
|
Loading…
Reference in a new issue