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

Refactor source files to move towards latest coding standard: #67

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ recursive-include src/clp/components/core/src/clp *.h
recursive-include src/clp/components/core/src/clp *.hpp
recursive-include src/clp/components/core/src/clp *.inc
recursive-include src/clp_ffi_py *.hpp
recursive-include src/clp_ffi_py *.inc
recursive-include clp_ffi_py *.py
recursive-include clp_ffi_py *.pyi
recursive-include clp_ffi_py py.typed
Expand Down
35 changes: 16 additions & 19 deletions clp_ffi_py/ir/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ class QueryBuilderException(Exception):

class QueryBuilder:
"""
This class serves as an interface for conveniently constructing Query
objects utilized in CLP IR streaming search. It provides methods for
configuring and resetting search parameters.
This class serves as an interface for conveniently constructing Query objects utilized in CLP IR
streaming search. It provides methods for configuring and resetting search parameters.
For more details about the search query CLP IR stream supports, see
:class:`~clp_ffi_py.ir.native.Query` and
:class:`~clp_ffi_py.wildcard_query.WildcardQuery`.
:class:`~clp_ffi_py.ir.native.Query` and :class:`~clp_ffi_py.wildcard_query.WildcardQuery`.
"""

def __init__(self) -> None:
Expand Down Expand Up @@ -60,26 +58,25 @@ def wildcard_queries(self) -> List[WildcardQuery]:

def set_search_time_lower_bound(self, ts: int) -> QueryBuilder:
"""
:param ts: Start of the search time range (inclusive) as a UNIX epoch
timestamp in milliseconds.
:param ts: Start of the search time range (inclusive) as a UNIX epoch timestamp in
milliseconds.
:return: self.
"""
self._search_time_lower_bound = ts
return self

def set_search_time_upper_bound(self, ts: int) -> QueryBuilder:
"""
:param ts: End of the search time range (inclusive) as a UNIX epoch
timestamp in milliseconds.
:param ts: End of the search time range (inclusive) as a UNIX epoch timestamp in
milliseconds.
:return: self.
"""
self._search_time_upper_bound = ts
return self

def set_search_time_termination_margin(self, ts: int) -> QueryBuilder:
"""
:param ts: The search time termination margin as a UNIX epoch timestamp
in milliseconds.
:param ts: The search time termination margin as a UNIX epoch timestamp in milliseconds.
:return: self.
"""
self._search_time_termination_margin = ts
Expand All @@ -92,8 +89,8 @@ def set_search_time_termination_margin(self, ts: int) -> QueryBuilder:
)
def add_wildcard_query(self, wildcard_query: str, case_sensitive: bool = False) -> QueryBuilder:
"""
Constructs and adds a :class:`~clp_ffi_py.wildcard_query.WildcardQuery`
to the wildcard query list.
Constructs and adds a :class:`~clp_ffi_py.wildcard_query.WildcardQuery` to the wildcard
query list.
:param wildcard_query: The wildcard query string to add.
:param case_sensitive: Whether to perform case-sensitive matching.
Expand All @@ -106,8 +103,8 @@ def add_wildcard_query(self, wildcard_query: WildcardQuery) -> QueryBuilder:
"""
Adds the given wildcard query to the wildcard query list.
:param wildcard_query: The wildcard query to add. It can be any derived
class of :class:`~clp_ffi_py.wildcard_query.WildcardQuery`.
:param wildcard_query: The wildcard query to add. It can be any derived class of
:class:`~clp_ffi_py.wildcard_query.WildcardQuery`.
:return: self.
"""
...
Expand Down Expand Up @@ -189,10 +186,10 @@ def reset(self) -> QueryBuilder:

def build(self) -> Query:
"""
:raises QueryBuilderException: If the search time range lower bound
exceeds the search time range upper bound.
:return: A :class:`~clp_ffi_py.ir.native.Query` object initialized
with the parameters set by the builder.
:raises QueryBuilderException: If the search time range lower bound exceeds the search time
range upper bound.
:return: A :class:`~clp_ffi_py.ir.native.Query` object initialized with the parameters set
by the builder.
"""
if self._search_time_lower_bound > self._search_time_upper_bound:
raise QueryBuilderException(
Expand Down
33 changes: 15 additions & 18 deletions clp_ffi_py/ir/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@

class ClpIrStreamReader(Iterator[LogEvent]):
"""
This class represents a stream reader used to read/decode encoded log events
from a CLP IR stream. It also provides method(s) to instantiate a log event
generator with a customized search query.
This class represents a stream reader used to read/decode encoded log events from a CLP IR
stream. It also provides method(s) to instantiate a log event generator with a customized search
query.
:param istream: Input stream that contains encoded CLP IR.
:param decoder_buffer_size: Initial size of the decoder buffer.
:param enable_compression: A flag indicating whether the istream is
compressed using `zstd`.
:param allow_incomplete_stream: If set to `True`, an incomplete CLP IR
stream is not treated as an error. Instead, encountering such a stream
is seen as reaching its end without raising any exceptions.
:param enable_compression: A flag indicating whether the istream is compressed using `zstd`.
:param allow_incomplete_stream: If set to `True`, an incomplete CLP IR stream is not treated as
an error. Instead, encountering such a stream is seen as reaching its end without raising
any exceptions.
"""

DEFAULT_DECODER_BUFFER_SIZE: int = 65536
Expand Down Expand Up @@ -52,19 +51,17 @@ def read_next_log_event(self) -> Optional[LogEvent]:
- Next unread log event represented as an instance of LogEvent.
- None if the end of IR stream is reached.
:raise Exception:
If :meth:`~clp_ffi_py.ir.native.Decoder.decode_next_log_event`
fails.
If :meth:`~clp_ffi_py.ir.native.Decoder.decode_next_log_event` fails.
"""
return Decoder.decode_next_log_event(
self._decoder_buffer, allow_incomplete_stream=self._allow_incomplete_stream
)

def read_preamble(self) -> None:
"""
Try to decode the preamble and set `metadata`. If `metadata` has been
set already, it will instantly return. It is separated from `__init__`
so that the input stream does not need to be readable on a reader's
construction, but until the user starts to iterate logs.
Try to decode the preamble and set `metadata`. If `metadata` has been set already, it will
instantly return. It is separated from `__init__` so that the input stream does not need to
be readable on a reader's construction, but until the user starts to iterate logs.
:raise Exception:
If :meth:`~clp_ffi_py.ir.native.Decoder.decode_preamble` fails.
Expand All @@ -85,10 +82,10 @@ def search(self, query: Query) -> Generator[LogEvent, None, None]:
"""
Searches and yields log events that match a specific search query.
:param query: The input query object used to match log events. Check the
document of :class:`~clp_ffi_py.ir.Query` for more details.
:yield: The next unread encoded log event that matches the given search
query from the IR stream.
:param query: The input query object used to match log events. Check the document of
:class:`~clp_ffi_py.ir.Query` for more details.
:yield: The next unread encoded log event that matches the given search query from the IR
stream.
"""
if False is self.has_metadata():
self.read_preamble()
Expand Down
7 changes: 3 additions & 4 deletions clp_ffi_py/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

def get_formatted_timestamp(timestamp: int, timezone: Optional[tzinfo]) -> str:
"""
Gets the formatted timestamp string from the provided timestamp with the
provided timezone using isoformat.
Gets the formatted timestamp string from the provided timestamp with the provided timezone using
isoformat.
:param timestamp: Timestamp to format.
:param timezone: Timezone of timestamp parameter. If None is given, UTC is
used by default.
:param timezone: Timezone of timestamp parameter. If None is given, UTC is used by default.
:return: String of formatted timestamp.
"""
if timezone is None:
Expand Down
31 changes: 14 additions & 17 deletions clp_ffi_py/wildcard_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@

class WildcardQuery:
"""
An abstract class defining a wildcard query. Users should instantiate a
wildcard query through :class:`SubstringWildcardQuery` or
:class:`FullStringWildcardQuery`.
An abstract class defining a wildcard query. Users should instantiate a wildcard query through
:class:`SubstringWildcardQuery` or :class:`FullStringWildcardQuery`.
A wildcard string may contain the following types of wildcards:
1. '*': match 0 or more characters.
2. '?': match any single character.
Each wildcard can be escaped using a preceding '\\\\' (a single backslash).
Other characters that are escaped are treated as normal characters.
Each wildcard can be escaped using a preceding '\\\\' (a single backslash). Other characters
that are escaped are treated as normal characters.
"""

@deprecated(
Expand Down Expand Up @@ -61,12 +60,11 @@ def case_sensitive(self) -> bool:

class SubstringWildcardQuery(WildcardQuery):
"""
A wildcard query that can match a substring in a log event's message, in
contrast with :class:`FullStringWildcardQuery` where the query needs to
match the entire message.
A wildcard query that can match a substring in a log event's message, in contrast with
:class:`FullStringWildcardQuery` where the query needs to match the entire message.
This class is derived from :class:`WildcardQuery` by adding both a prefix
and a postfix wildcard ("*") to the input wildcard string.
This class is derived from :class:`WildcardQuery` by adding both a prefix and a postfix wildcard
("*") to the input wildcard string.
"""

def __init__(self, substring_wildcard_query: str, case_sensitive: bool = False):
Expand All @@ -84,15 +82,14 @@ def __init__(self, substring_wildcard_query: str, case_sensitive: bool = False):

class FullStringWildcardQuery(WildcardQuery):
"""
A wildcard query where the query must match the entirety of the log event's
message, in contrast with :class:`SubstringWildcardQuery` where the query
only needs to match a substring.
A wildcard query where the query must match the entirety of the log event's message, in contrast
with :class:`SubstringWildcardQuery` where the query only needs to match a substring.
This class is derived from :class:`WildcardQuery` as a more explicit
interface for full-string matches.
This class is derived from :class:`WildcardQuery` as a more explicit interface for full-string
matches.
Users can create a match that's anchored to only one end of the message by
adding a prefix OR postfix wildcard ("*").
Users can create a match that's anchored to only one end of the message by adding a prefix OR
postfix wildcard ("*").
"""

def __init__(self, full_string_wildcard_query: str, case_sensitive: bool = False):
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ archs = ["AMD64"]
make-summary-multi-line = true
pre-summary-newline = true
recursive = true
wrap-summaries = 80
wrap-descriptions = 80
wrap-summaries = 100
wrap-descriptions = 100

[tool.mypy]
explicit_package_bases = true
Expand Down
11 changes: 5 additions & 6 deletions src/clp_ffi_py/ExceptionFFI.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#ifndef CLP_FFI_PY_EXCEPTION_FFI
#define CLP_FFI_PY_EXCEPTION_FFI
#ifndef CLP_FFI_PY_IR_EXCEPTION_FFI
#define CLP_FFI_PY_IR_EXCEPTION_FFI

Copy link
Member

@davidlion davidlion Jul 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems incorrect no? (same with the endif)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm right, mb

#include <string>

#include <clp/TraceableException.hpp>

namespace clp_ffi_py {
/**
* A class that represents a traceable exception during the native code
* execution. Note: for exceptions of CPython execution, please use CPython
* interface to set the exception instead.
* A class that represents a traceable exception during the native code execution. Note: for
* exceptions of CPython execution, please use CPython interface to set the exception instead.
*/
class ExceptionFFI : public clp::TraceableException {
public:
Expand All @@ -29,4 +28,4 @@ class ExceptionFFI : public clp::TraceableException {
};
} // namespace clp_ffi_py

#endif // CLP_FFI_PY_EXCEPTION_FFI
#endif // CLP_FFI_PY_IR_EXCEPTION_FFI
50 changes: 24 additions & 26 deletions src/clp_ffi_py/PyObjectCast.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#ifndef CLP_FFI_PY_PY_OBJECT_CAST_HPP
#define CLP_FFI_PY_PY_OBJECT_CAST_HPP
#ifndef CLP_FFI_PY_IR_PY_OBJECT_CAST_HPP
#define CLP_FFI_PY_IR_PY_OBJECT_CAST_HPP

#include <clp_ffi_py/Python.hpp> // Must always be included before any other header files

#include <type_traits>

namespace clp_ffi_py {

/**
* Casts a given function pointer to a PyCFunction.
* The main purpose of this function is to silence clang-tidy checks on using
* `reinterpret_cast`. It does not perform any further function type checking.
* The main purpose of this function is to silence clang-tidy checks on using `reinterpret_cast`. It
* does not perform any further function type checking.
* @tparam Src The source function pointer type.
* @param src The source function pointer.
* @return PyCFunction using reinterpret_cast.
Expand All @@ -23,9 +22,9 @@ auto py_c_function_cast(Src src) noexcept -> PyCFunction {
}

/**
* Casts a given function pointer to a `getbufferproc` CPython function.
* The main purpose of this function is to silence clang-tidy checks on using
* `reinterpret_cast`. It does not perform any further function type checking.
* Casts a given function pointer to a `getbufferproc` CPython function. The main purpose of this
* function is to silence clang-tidy checks on using `reinterpret_cast`. It does not perform any
* further function type checking.
* @tparam Src The source function pointer type.
* @param src The source function pointer.
* @return `getbufferproc` using reinterpret_cast.
Expand All @@ -38,9 +37,9 @@ auto py_getbufferproc_cast(Src src) noexcept -> getbufferproc {
}

/**
* Casts a given function pointer to a `releasebufferproc` CPython function.
* The main purpose of this function is to silence clang-tidy checks on using
* `reinterpret_cast`. It does not perform any further function type checking.
* Casts a given function pointer to a `releasebufferproc` CPython function. The main purpose of
* this function is to silence clang-tidy checks on using `reinterpret_cast`. It does not perform
* any further function type checking.
* @tparam Src The source function pointer type.
* @param src The source function pointer.
* @return `releasebufferproc` using reinterpret_cast.
Expand All @@ -53,8 +52,8 @@ auto py_releasebufferproc_cast(Src src) noexcept -> releasebufferproc {
}

/**
* This template struct is used as a compile-time flag that indicates whether
* type T is a PyObject or not. By default, `cValue` is set to false.
* This template struct is used as a compile-time flag that indicates whether type T is a PyObject
* or not. By default, `cValue` is set to false.
* @tparam T
*/
template <typename T>
Expand All @@ -63,18 +62,16 @@ struct is_python_object {
};

/**
* This template const expression is a wrapper of underlying `cValue` stored in
* `is_python_object`, which is used to determine whether a type T is a valid
* Python object type.
* This template const expression is a wrapper of underlying `cValue` stored in `is_python_object`,
* which is used to determine whether a type T is a valid Python object type.
* @tparam T
*/
template <typename T> // NOLINTNEXTLINE(readability-identifier-naming)
constexpr bool is_python_object_v{is_python_object<T>::cValue};

/**
* The macro to create a specialization of is_python_object for a given type T.
* Only types that are marked with this macro will be considered as a valid
* Python object type.
* The macro to create a specialization of is_python_object for a given type T. Only types that are
* marked with this macro will be considered as a valid Python object type.
*/
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define CLP_FFI_PY_MARK_AS_PYOBJECT(T) \
Expand All @@ -85,12 +82,12 @@ constexpr bool is_python_object_v{is_python_object<T>::cValue};

/**
* Casts a given Src pointer to a Dst pointer. It should behave as followed:
* 1. If Src and Dst are the same type, return `src` if their type is a valid
* Python object type, or they are both PyObject.
* 2. If Dst is PyObject, the cast is valid if Src is a PyObject. A pointer,
* Python*, should be returned.
* 3. If Src is PyObject, the cast is valid if Dst is a valid Python object
* type. A pointer, Dst*, should be returned.
* 1. If Src and Dst are the same type, return `src` if their type is a valid Python object type, or
* they are both PyObject.
* 2. If Dst is PyObject, the cast is valid if Src is a PyObject. A pointer, Python*, should be
* returned.
* 3. If Src is PyObject, the cast is valid if Dst is a valid Python object type. A pointer, Dst*,
* should be returned.
* 4. Any other cases are considered as invalid cast.
* @tparam Dst The destination type. Must be given explicitly.
* @tparam Src The source type. Can be given implicitly.
Expand Down Expand Up @@ -129,4 +126,5 @@ CLP_FFI_PY_MARK_AS_PYOBJECT(ir::native::PyMetadata);
CLP_FFI_PY_MARK_AS_PYOBJECT(ir::native::PyQuery);
CLP_FFI_PY_MARK_AS_PYOBJECT(PyTypeObject);
} // namespace clp_ffi_py
#endif

#endif // CLP_FFI_PY_IR_PY_OBJECT_CAST_HPP
Loading
Loading