Skip to content

Commit f879d09

Browse files
authored
only fetch json schema and licenses once (#126)
* only fetch json schema and licenses once #125 * fix error
1 parent e16e12c commit f879d09

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "txt2detection"
7-
version = "1.0.3"
7+
version = "1.0.4"
88
authors = [
99
{ name = "dogesec" }
1010
]

tests/src/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_as_date_with_datetime_and_date():
2323
assert as_date(d_obj) == d_obj
2424

2525
def test_validate_token_count():
26-
provider = parse_model('openai')
26+
provider = parse_model('anthropic')
2727
with patch.object(type(provider), 'count_tokens') as mock_count_tokens:
2828
mock_count_tokens.return_value = 1025
2929
with pytest.raises(Exception):

txt2detection/models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import re
44
import typing
55
import uuid
6+
import requests
67
from slugify import slugify
78
from datetime import date as dt_date
8-
from typing import Any, List, Literal, Optional, Union
9+
from typing import Any, ClassVar, List, Literal, Optional, Union
910
from uuid import UUID
1011

1112
import jsonschema
@@ -194,6 +195,7 @@ class BaseDetection(BaseModel):
194195
level: Level
195196
_custom_id = None
196197
_extra_data: dict
198+
sigma_json_schema: ClassVar = requests.get("https://github.com/SigmaHQ/sigma-specification/raw/refs/heads/main/json-schema/sigma-detection-rule-schema.json").json()
197199

198200
def model_post_init(self, __context):
199201
self.tags = self.tags or []
@@ -249,9 +251,7 @@ def make_rule(self, bundler: "Bundler"):
249251
def validate_rule_with_json_schema(self, rule):
250252
jsonschema.validate(
251253
rule,
252-
{
253-
"$ref": "https://github.com/SigmaHQ/sigma-specification/raw/refs/heads/main/json-schema/sigma-detection-rule-schema.json"
254-
},
254+
self.sigma_json_schema,
255255
)
256256

257257
@property

txt2detection/utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ def validate_token_count(max_tokens, input, extractor: BaseAIExtractor):
5555
if token_count > max_tokens:
5656
raise Exception(f"{extractor.extractor_name}: input_file token count ({token_count}) exceeds INPUT_TOKEN_LIMIT ({max_tokens})")
5757

58+
59+
@lru_cache(maxsize=5)
60+
def get_licenses(date):
61+
resp = requests.get("https://github.com/spdx/license-list-data/raw/refs/heads/main/json/licenses.json")
62+
return {l['licenseId']: l['name'] for l in resp.json()['licenses']}
63+
5864
def valid_licenses():
59-
@lru_cache(maxsize=5)
60-
def get_licenses(date):
61-
resp = requests.get("https://github.com/spdx/license-list-data/raw/refs/heads/main/json/licenses.json")
62-
return {l['licenseId']: l['name'] for l in resp.json()['licenses']}
6365
return get_licenses(datetime.now().date().isoformat())
6466

6567

0 commit comments

Comments
 (0)