Skip to content

Commit b0a757f

Browse files
authored
Merge pull request #102 from UncoderIO/gis-7649
remove comments from incoming data
2 parents 67aadd5 + 7fcb6bb commit b0a757f

File tree

21 files changed

+58
-26
lines changed

21 files changed

+58
-26
lines changed

uncoder-core/app/translator/core/parser.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1616
-----------------------------------------------------------------
1717
"""
18-
18+
import re
1919
from abc import ABC, abstractmethod
2020
from typing import Union
2121

2222
from app.translator.core.exceptions.parser import TokenizerGeneralException
2323
from app.translator.core.functions import PlatformFunctions
2424
from app.translator.core.mapping import BasePlatformMappings, SourceMapping
25-
from app.translator.core.models.field import FieldValue, Field, FieldValue, Keyword
25+
from app.translator.core.models.field import Field, FieldValue, Keyword
2626
from app.translator.core.models.functions.base import ParsedFunctions
2727
from app.translator.core.models.identifier import Identifier
2828
from app.translator.core.models.platform_details import PlatformDetails
@@ -31,6 +31,11 @@
3131

3232

3333
class QueryParser(ABC):
34+
wrapped_with_comment_pattern: str = None
35+
36+
def remove_comments(self, text: str) -> str:
37+
return re.sub(self.wrapped_with_comment_pattern, "\n", text, flags=re.MULTILINE).strip()
38+
3439
def parse_raw_query(self, text: str, language: str) -> RawQueryContainer:
3540
return RawQueryContainer(query=text, language=language)
3641

uncoder-core/app/translator/core/render.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ def apply_field_value(self, field: str, operator: Identifier, value: DEFAULT_VAL
126126

127127
class QueryRender(ABC):
128128
comment_symbol: str = None
129-
is_multi_line_comment: bool = False
129+
is_single_line_comment: bool = False
130130
unsupported_functions_text = "Unsupported functions were excluded from the result query:"
131131

132132
platform_functions: PlatformFunctions = PlatformFunctions()
133133

134134
def render_not_supported_functions(self, not_supported_functions: list) -> str:
135-
line_template = f"{self.comment_symbol} " if self.comment_symbol and self.is_multi_line_comment else ""
135+
line_template = f"{self.comment_symbol} " if self.comment_symbol and self.is_single_line_comment else ""
136136
not_supported_functions_str = "\n".join(line_template + func.lstrip() for func in not_supported_functions)
137137
return "\n\n" + self.wrap_with_comment(f"{self.unsupported_functions_text}\n{not_supported_functions_str}")
138138

uncoder-core/app/translator/platforms/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
from app.translator.platforms.microsoft.renders.microsoft_sentinel_rule import MicrosoftSentinelRuleRender
4242
from app.translator.platforms.opensearch.parsers.opensearch import OpenSearchQueryParser
4343
from app.translator.platforms.opensearch.renders.opensearch import OpenSearchQueryRender
44-
from app.translator.platforms.palo_alto.renders.cortex_xsiam import CortexXQLQueryRender
4544
from app.translator.platforms.opensearch.renders.opensearch_cti import OpenSearchCTI
4645
from app.translator.platforms.opensearch.renders.opensearch_rule import OpenSearchRuleRender
46+
from app.translator.platforms.palo_alto.renders.cortex_xsiam import CortexXQLQueryRender
4747
from app.translator.platforms.qradar.parsers.qradar import QradarQueryParser
4848
from app.translator.platforms.qradar.renders.qradar import QradarQueryRender
4949
from app.translator.platforms.qradar.renders.qradar_cti import QRadarCTI

uncoder-core/app/translator/platforms/athena/parsers/athena.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class AthenaQueryParser(PlatformQueryParser):
3434
query_delimiter_pattern = r"\sFROM\s\S*\sWHERE\s"
3535
table_pattern = r"\sFROM\s(?P<table>[a-zA-Z\.\-\*]+)\sWHERE\s"
3636

37+
wrapped_with_comment_pattern = r"^\s*--.*(?:\n|$)"
38+
3739
def _parse_query(self, query: str) -> tuple[str, dict[str, Optional[str]]]:
3840
log_source = {"table": None}
3941
if re.search(self.query_delimiter_pattern, query, flags=re.IGNORECASE):

uncoder-core/app/translator/platforms/athena/renders/athena.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class AthenaQueryRender(PlatformQueryRender):
8787
field_value_map = AthenaFieldValue(or_token=or_token)
8888
query_pattern = "{prefix} WHERE {query} {functions}"
8989
comment_symbol = "--"
90-
is_multi_line_comment = True
90+
is_single_line_comment = True
9191

9292
def generate_prefix(self, log_source_signature: LogSourceSignature) -> str:
9393
table = str(log_source_signature) if str(log_source_signature) else "eventlog"

uncoder-core/app/translator/platforms/base/lucene/parsers/lucene.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class LuceneQueryParser(PlatformQueryParser):
2929
log_source_pattern = r"___source_type___\s*(?:[:=])\s*(?:\"?(?P<d_q_value>[%a-zA-Z_*:0-9\-/]+)\"|(?P<value>[%a-zA-Z_*:0-9\-/]+))(?:\s+(?:and|or)\s+|\s+)?" # noqa: E501
3030
log_source_key_types = ("index", "event\.category")
3131

32+
wrapped_with_comment_pattern = r"^\s*//.*(?:\n|$)"
33+
3234
def _parse_query(self, query: str) -> tuple[str, dict[str, list[str]]]:
3335
log_sources = {}
3436
for source_type in self.log_source_key_types:

uncoder-core/app/translator/platforms/base/lucene/renders/lucene.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class LuceneQueryRender(PlatformQueryRender):
133133
query_pattern = "{query} {functions}"
134134

135135
comment_symbol = "//"
136-
is_multi_line_comment = True
136+
is_single_line_comment = True
137137

138138
def generate_prefix(self, log_source_signature: LuceneLogSourceSignature) -> str: # noqa: ARG002
139139
return ""

uncoder-core/app/translator/platforms/base/spl/parsers/spl.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class SplQueryParser(PlatformQueryParser):
3434
platform_functions: SplFunctions = None
3535
tokenizer = SplTokenizer()
3636

37+
wrapped_with_comment_pattern = r"^\s*```(?:|\n|.)*```"
38+
3739
def _parse_log_sources(self, query: str) -> tuple[dict[str, list[str]], str]:
3840
log_sources = {}
3941
for source_type in self.log_source_key_types:

uncoder-core/app/translator/platforms/chronicle/parsers/chronicle.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class ChronicleQueryParser(PlatformQueryParser):
3030
tokenizer: ChronicleQueryTokenizer = ChronicleQueryTokenizer()
3131
details: PlatformDetails = chronicle_query_details
3232

33+
wrapped_with_comment_pattern = r"^\s*//.*(?:\n|$)"
34+
3335
def parse(self, raw_query_container: RawQueryContainer) -> TokenizedQueryContainer:
3436
tokens, source_mappings = self.get_tokens_and_source_mappings(raw_query_container.query, {})
3537
fields_tokens = self.get_fields_tokens(tokens=tokens)

uncoder-core/app/translator/platforms/chronicle/renders/chronicle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
limitations under the License.
1717
-----------------------------------------------------------------
1818
"""
19-
2019
from typing import Union
2120

2221
from app.translator.const import DEFAULT_VALUE_TYPE
@@ -109,4 +108,5 @@ class ChronicleQueryRender(PlatformQueryRender):
109108

110109
field_value_map = ChronicleFieldValue(or_token=or_token)
111110
query_pattern = "{query} {functions}"
112-
comment_symbol = r"//"
111+
comment_symbol = "//"
112+
is_single_line_comment = True

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy