Skip to content

Commit

Permalink
global: add pre-commit with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalEgn committed Aug 7, 2024
1 parent efc31da commit ca95db4
Show file tree
Hide file tree
Showing 27 changed files with 2,065 additions and 1,685 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,3 @@ jobs:
with:
user: __token__
password: ${{ secrets.pypi_password }}

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ CHANGELOG
.idea

# vscode
.vscode
.vscode
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: fix-byte-order-marker
- id: mixed-line-ending
- id: name-tests-test
args: [ --pytest-test-first ]
exclude: '^(?!factories/)'
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.6
hooks:
- id: ruff
args: [ --fix ]
181 changes: 0 additions & 181 deletions examples/demo_parser.py

This file was deleted.

7 changes: 3 additions & 4 deletions inspire_query_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
# In applying this license, CERN does not waive the privileges and immunities
# granted to it by virtue of its status as an Intergovernmental Organization
# or submit itself to any jurisdiction.

"""A PEG-based query parser for INSPIRE"""
"""A PEG-based query parser for INSPIRE."""

from __future__ import absolute_import, print_function

from . import config # noqa: F401
from .parsing_driver import parse_query # noqa: F401
from inspire_query_parser import config # noqa: F401
from inspire_query_parser.parsing_driver import parse_query # noqa: F401
63 changes: 34 additions & 29 deletions inspire_query_parser/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@
# In applying this license, CERN does not waive the privileges and immunities
# granted to it by virtue of its status as an Intergovernmental Organization
# or submit itself to any jurisdiction.
"""AbstractSyntaxTree classes along with their concrete ones.
"""
AbstractSyntaxTree classes along with their concrete ones.
The module defines a generic AST element along with four AST node categories (which act as a basis for all the concrete
AST nodes) and finally, the concrete classes which represent the output of the parsing process.
The module defines a generic AST element along with four AST node
categories (which act as a basis for all the concrete AST nodes) and
finally, the concrete classes which represent the output of the parsing
process.
The generic AST node categories are:
- Leaf
- UnaryOp
- BinaryOp
- ListOp
The generic AST node categories are: - Leaf - UnaryOp -
BinaryOp - ListOp
The concrete AST nodes, represent higher level (domain specific) nodes.
"""
Expand All @@ -40,18 +37,19 @@

# #### Abstract Syntax Tree classes ####
class ASTElement(object):
"""Root AbstractSyntaxTree node that acts as a stub for calling the Visitor's `visit` dispatcher method."""
"""Root AbstractSyntaxTree node that acts as a stub for calling the
Visitor's `visit` dispatcher method."""

def accept(self, visitor, *args, **kwargs):
return visitor.visit(self, *args, **kwargs)


class Leaf(ASTElement):

def __init__(self, value=None):
self.value = value

def __eq__(self, other):
return type(self) == type(other) and self.value == other.value
return type(self) is type(other) and self.value == other.value

def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self.value)
Expand All @@ -61,12 +59,11 @@ def __hash__(self):


class UnaryOp(ASTElement):

def __init__(self, op):
self.op = op

def __eq__(self, other):
return type(self) == type(other) and self.op == other.op
return type(self) is type(other) and self.op == other.op

def __repr__(self):
return "%s(%r)" % (self.__class__.__name__, self.op)
Expand All @@ -76,30 +73,29 @@ def __hash__(self):


class BinaryOp(ASTElement):

def __init__(self, left, right):
self.left = left
self.right = right

def __eq__(self, other):
return (
type(self) == type(other)
) and (
self.left == other.left
) and (
self.right == other.right
(type(self) is type(other))
and (self.left == other.left)
and (self.right == other.right)
)

def __repr__(self):
return "%s(%s, %s)" % (self.__class__.__name__,
repr(self.left), repr(self.right))
return "%s(%s, %s)" % (
self.__class__.__name__,
repr(self.left),
repr(self.right),
)

def __hash__(self):
return hash((self.left, self.right))


class ListOp(ASTElement):

def __init__(self, children):
try:
iter(children)
Expand All @@ -109,7 +105,7 @@ def __init__(self, children):
self.children = children

def __eq__(self, other):
return type(self) == type(other) and self.children == other.children
return type(self) is type(other) and self.children == other.children

def __repr__(self):
return "%s(%r)" % (self.__class__.__name__, self.children)
Expand Down Expand Up @@ -144,15 +140,20 @@ class ValueOp(UnaryOp):


class QueryWithMalformedPart(BinaryOp):
"""A combination of recognized part of a query (with a parse tree) and some malformed input.
"""A combination of recognized part of a query (with a parse tree) and some
malformed input.
Its left child is the recognized parse tree, while its right child has the :class:`MalformedQuery`.
Its left child is the recognized parse tree, while its right child
has the :class:`MalformedQuery`.
"""

pass


class MalformedQuery(ListOp):
"""A :class:`ListOp` with children the unrecognized words of the parser's input."""
"""A :class:`ListOp` with children the unrecognized words of the parser's
input."""

pass


Expand Down Expand Up @@ -183,14 +184,18 @@ class Keyword(Leaf):

class GenericValue(Leaf):
"""Represents a generic value, which might contain a wildcard."""

WILDCARD_TOKEN = '*'

def __init__(self, value, contains_wildcard=False):
super(GenericValue, self).__init__(value)
self.contains_wildcard = contains_wildcard

def __eq__(self, other):
return super(GenericValue, self).__eq__(other) and self.contains_wildcard == other.contains_wildcard
return (
super(GenericValue, self).__eq__(other)
and self.contains_wildcard == other.contains_wildcard
)

def __hash__(self):
return hash((super(GenericValue, self).__hash__(), self.contains_wildcard))
Expand Down
Loading

0 comments on commit ca95db4

Please sign in to comment.