Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pylint & astroid #3329

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
819 changes: 473 additions & 346 deletions .pylintrc

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ pbr >= 4.2.0
beautifulsoup4 >= 4.8.2
coverage >= 5.0.2
httmock >= 1.3.0
pylint==2.6.0
astroid==2.4.0
pylint==2.17.7
astroid==2.15.8
24 changes: 13 additions & 11 deletions timesketch/api/v1/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.
"""This module holds methods and classes to export events."""

from __future__ import unicode_literals

import io
import json
Expand All @@ -38,15 +37,15 @@ def export_aggregation(aggregation, sketch, zip_file):
zip_file (ZipFile): a zip file handle that can be used to write
content to.
"""
name = "{0:04d}_{1:s}".format(aggregation.id, aggregation.name)
name = f"{aggregation.id:04d}_{aggregation.name:s}"
parameters = json.loads(aggregation.parameters)
result_obj, meta = utils.run_aggregator(
sketch.id,
aggregator_name=aggregation.agg_type,
aggregator_parameters=parameters,
)

zip_file.writestr("aggregations/{0:s}.meta".format(name), data=json.dumps(meta))
zip_file.writestr(f"aggregations/{name:s}.meta", data=json.dumps(meta))

html = result_obj.to_chart(
chart_name=meta.get("chart_type"),
Expand All @@ -55,13 +54,13 @@ def export_aggregation(aggregation, sketch, zip_file):
interactive=True,
as_html=True,
)
zip_file.writestr("aggregations/{0:s}.html".format(name), data=html)
zip_file.writestr(f"aggregations/{name:s}.html", data=html)

string_io = io.StringIO()
data_frame = result_obj.to_pandas()
data_frame.to_csv(string_io, index=False)
string_io.seek(0)
zip_file.writestr("aggregations/{0:s}.csv".format(name), data=string_io.read())
zip_file.writestr(f"aggregations/{name:s}.csv", data=string_io.read())


def export_aggregation_group(group, sketch, zip_file):
Expand All @@ -74,11 +73,11 @@ def export_aggregation_group(group, sketch, zip_file):
zip_file (ZipFile): a zip file handle that can be used to write
content to.
"""
name = "{0:04d}_{1:s}".format(group.id, group.name)
name = f"{group.id:04d}_{group.name:s}"
chart, _, meta = utils.run_aggregator_group(group, sketch_id=sketch.id)

zip_file.writestr("aggregation_groups/{0:s}.meta".format(name), json.dumps(meta))
zip_file.writestr("aggregation_groups/{0:s}.html".format(name), chart.to_html())
zip_file.writestr(f"aggregation_groups/{name:s}.meta", json.dumps(meta))
zip_file.writestr(f"aggregation_groups/{name:s}.html", chart.to_html())


def export_story(story, sketch, story_exporter, zip_file):
Expand Down Expand Up @@ -107,7 +106,7 @@ def export_story(story, sketch, story_exporter, zip_file):
exporter.set_title(story.title)

zip_file.writestr(
"stories/{0:04d}_{1:s}.html".format(story.id, story.title),
f"stories/{story.id:04d}_{story.title:s}.html",
data=exporter.export_story(),
)

Expand Down Expand Up @@ -187,8 +186,11 @@ def query_to_filehandle(
else:
logger.warning(
"Data Frame returned from a search operation was "
"empty, count {0:d} out of {1:d} total. Query is: "
'"{2:s}"'.format(event_count, total_count, query_string or query_dsl)
"empty, count %d out of %d total. Query is: "
'"%s"',
event_count,
total_count,
query_string or query_dsl,
)

fh = io.StringIO()
Expand Down
12 changes: 8 additions & 4 deletions timesketch/api/v1/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

The timesketch API is a RESTful API that exposes the following resources:
"""
from __future__ import unicode_literals

import logging
from typing import Optional, Dict

from flask import current_app
from flask import jsonify
Expand All @@ -35,7 +35,7 @@
)


class ResourceMixin(object):
class ResourceMixin:
"""Mixin for API resources."""

# Schemas for database model resources
Expand Down Expand Up @@ -379,7 +379,11 @@ def datastore(self):
)

def to_json(
self, model, model_fields=None, meta=None, status_code=HTTP_STATUS_CODE_OK
self,
model: object,
model_fields: Optional[Dict] = None,
meta: Optional[Dict] = None,
status_code: int = HTTP_STATUS_CODE_OK,
):
"""Create json response from a database models.

Expand All @@ -393,7 +397,7 @@ def to_json(
Response in json format (instance of flask.wrappers.Response)
"""
if not meta:
meta = dict()
meta = {}

schema = {"meta": meta, "objects": []}

Expand Down
61 changes: 29 additions & 32 deletions timesketch/api/v1/resources/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class AggregationResource(resources.ResourceMixin, Resource):
"""Resource to query for aggregated results."""

@login_required
def get(self, sketch_id, aggregation_id): # pylint: disable=unused-argument
def get(self, sketch_id: int, aggregation_id: int):
"""Handles GET request to the resource.

Handler for /api/v1/sketches/:sketch_id/aggregation/:aggregation_id
Expand All @@ -74,16 +74,14 @@ def get(self, sketch_id, aggregation_id): # pylint: disable=unused-argument
if not aggregation:
abort(
HTTP_STATUS_CODE_NOT_FOUND,
"The aggregation ID ({0:d}) does not exist.".format(aggregation_id),
f"The aggregation ID ({aggregation_id:d}) does not exist.",
)
# Check that this aggregation belongs to the sketch
if aggregation.sketch_id != sketch.id:
abort(
HTTP_STATUS_CODE_NOT_FOUND,
"The sketch ID ({0:d}) does not match with the defined "
"sketch in the aggregation ({1:d})".format(
aggregation.sketch_id, sketch.id
),
f"The sketch ID ({aggregation.sketch_id:d}) does not match with "
f"the defined sketch in the aggregation ({sketch.id:d})",
)

# If this is a user state view, check that it
Expand All @@ -101,7 +99,7 @@ def get(self, sketch_id, aggregation_id): # pylint: disable=unused-argument

@login_required
# pylint: disable=unused-argument
def post(self, sketch_id, aggregation_id):
def post(self, sketch_id: int, aggregation_id: int):
"""Handles POST request to the resource.

Handler for /api/v1/sketches/:sketch_id/aggregation/:aggregation_id
Expand Down Expand Up @@ -168,12 +166,12 @@ def post(self, sketch_id, aggregation_id):
return self.to_json(aggregation, status_code=HTTP_STATUS_CODE_CREATED)

@login_required
def delete(self, sketch_id, aggregation_id):
def delete(self, sketch_id: int, aggregation_id: int):
"""Handles DELETE request to the resource.

Args:
sketch_id: Integer primary key for a sketch database model.
group_id: Integer primary key for an aggregation group database
aggregation_id: Integer primary key for an aggregation group database
model.
"""
sketch = Sketch.get_with_acl(sketch_id)
Expand All @@ -193,8 +191,8 @@ def delete(self, sketch_id, aggregation_id):
# Check that this aggregation belongs to the sketch
if aggregation.sketch_id != sketch.id:
msg = (
"The sketch ID ({0:d}) does not match with the aggregation "
"sketch ID ({1:d})".format(sketch.id, aggregation.sketch_id)
f"The sketch ID ({sketch.id:d}) does not match with the aggregation "
f"sketch ID ({aggregation.sketch_id:d})"
)
abort(HTTP_STATUS_CODE_FORBIDDEN, msg)

Expand Down Expand Up @@ -274,7 +272,7 @@ class AggregationGroupResource(resources.ResourceMixin, Resource):
"""Resource for aggregation group requests."""

@login_required
def get(self, sketch_id, group_id):
def get(self, sketch_id: int, group_id: int):
"""Handles GET request to the resource.

Args:
Expand All @@ -299,8 +297,8 @@ def get(self, sketch_id, group_id):
# Check that this group belongs to the sketch
if group.sketch_id != sketch.id:
msg = (
"The sketch ID ({0:d}) does not match with the aggregation "
"group sketch ID ({1:d})".format(sketch.id, group.sketch_id)
f"The sketch ID ({sketch.id:d}) does not match with the aggregation "
f"group sketch ID ({group.sketch_id:d})"
)
abort(HTTP_STATUS_CODE_FORBIDDEN, msg)

Expand All @@ -320,7 +318,7 @@ def get(self, sketch_id, group_id):
return jsonify(schema)

@login_required
def post(self, sketch_id, group_id):
def post(self, sketch_id: int, group_id: int):
"""Handles POST request to the resource.

Args:
Expand All @@ -339,8 +337,8 @@ def post(self, sketch_id, group_id):
# Check that this group belongs to the sketch
if group.sketch_id != sketch.id:
msg = (
"The sketch ID ({0:d}) does not match with the aggregation "
"group sketch ID ({1:d})".format(sketch.id, group.sketch_id)
f"The sketch ID ({sketch.id:d}) does not match with the aggregation "
f"group sketch ID ({group.sketch_id:d})"
)
abort(HTTP_STATUS_CODE_FORBIDDEN, msg)

Expand Down Expand Up @@ -373,7 +371,7 @@ def post(self, sketch_id, group_id):
if not aggregation:
abort(
HTTP_STATUS_CODE_BAD_REQUEST,
"No aggregation found for ID: {0:d}".format(agg_id),
f"No aggregation found for ID: {agg_id:d}",
)
aggregations.append(aggregation)

Expand All @@ -385,7 +383,7 @@ def post(self, sketch_id, group_id):
return self.to_json(group, status_code=HTTP_STATUS_CODE_CREATED)

@login_required
def delete(self, sketch_id, group_id):
def delete(self, sketch_id: int, group_id: int):
"""Handles DELETE request to the resource.

Args:
Expand All @@ -405,8 +403,8 @@ def delete(self, sketch_id, group_id):
# Check that this group belongs to the sketch
if group.sketch_id != sketch.id:
msg = (
"The sketch ID ({0:d}) does not match with the aggregation "
"group sketch ID ({1:d})".format(sketch.id, group.sketch_id)
f"The sketch ID ({sketch.id:d}) does not match with the aggregation "
f"group sketch ID ({group.sketch_id:d})"
)
abort(HTTP_STATUS_CODE_FORBIDDEN, msg)

Expand All @@ -431,7 +429,7 @@ class AggregationExploreResource(resources.ResourceMixin, Resource):
REMOVE_FIELDS = frozenset(["_shards", "hits", "timed_out", "took"])

@login_required
def post(self, sketch_id):
def post(self, sketch_id: int):
"""Handles POST request to the resource.

Handler for /api/v1/sketches/<int:sketch_id>/aggregation/explore/
Expand Down Expand Up @@ -509,17 +507,16 @@ def post(self, sketch_id):
try:
result_obj = aggregator.run(**aggregator_parameters)
except NotFoundError:
indices_msg = ", ".join(indices)
abort(
HTTP_STATUS_CODE_NOT_FOUND,
"Attempting to run an aggregation on a non-existing "
"index, index: {0:s} and parameters: {1!s}".format(
",".join(indices), aggregator_parameters
),
"Attempting to run an aggregation on a non-existing index, "
f"index: {indices_msg:s} and parameters: {aggregator_parameters!s}",
)
except ValueError as exc:
abort(
HTTP_STATUS_CODE_BAD_REQUEST,
"Unable to run the aggregation, with error: {0!s}".format(exc),
f"Unable to run the aggregation, with error: {exc!s}",
)
time_after = time.time()

Expand Down Expand Up @@ -588,7 +585,7 @@ class AggregationListResource(resources.ResourceMixin, Resource):
"""Resource to query for a list of stored aggregation queries."""

@login_required
def get(self, sketch_id):
def get(self, sketch_id: int):
"""Handles GET request to the resource.

Handler for /api/v1/sketches/<int:sketch_id>/aggregation/
Expand Down Expand Up @@ -616,7 +613,7 @@ def get(self, sketch_id):
return self.to_json(aggregations)

@staticmethod
def create_aggregation_from_form(sketch, form):
def create_aggregation_from_form(sketch: Sketch, form: forms.AggregationExploreForm):
"""Creates an aggregation from form data.

Args:
Expand Down Expand Up @@ -660,7 +657,7 @@ def create_aggregation_from_form(sketch, form):
return aggregation

@login_required
def post(self, sketch_id):
def post(self, sketch_id: int):
"""Handles POST request to the resource.

Args:
Expand Down Expand Up @@ -698,7 +695,7 @@ class AggregationGroupListResource(resources.ResourceMixin, Resource):
"""Resource to query for a list of stored aggregation queries."""

@login_required
def get(self, sketch_id):
def get(self, sketch_id: int):
"""Handles GET request to the resource.

Handler for /api/v1/sketches/<int:sketch_id>/aggregation/group/
Expand Down Expand Up @@ -742,7 +739,7 @@ def get(self, sketch_id):
return response

@login_required
def post(self, sketch_id):
def post(self, sketch_id: int):
"""Handles POST request to the resource.

Args:
Expand Down
2 changes: 1 addition & 1 deletion timesketch/api/v1/resources/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def post(self, sketch_id):
except KeyError as e:
logger.warning(
"Unable to build analyzer pipeline, analyzer does not "
"exists. Error message: {0!s}".format(e)
"exists. Error message: {!s}".format(e)
)
continue

Expand Down
Loading
Loading