-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5477616
commit 8e620b6
Showing
12 changed files
with
177 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.10.6" | ||
__version__ = "0.11.0" |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
""" | ||
Created on 2024-05-06 | ||
@author: wf | ||
""" | ||
import argparse | ||
import re | ||
from typing import Dict, Optional | ||
|
||
|
||
class Params: | ||
""" | ||
parameter handling | ||
""" | ||
|
||
def __init__(self, query: str): | ||
""" | ||
constructor | ||
Args: | ||
query(str): the query to analyze for parameters | ||
""" | ||
self.query = query | ||
self.pattern = re.compile(r"{{\s*(\w+)\s*}}") | ||
self.params = self.pattern.findall(query) | ||
self.params_dict = {param: "" for param in self.params} | ||
self.has_params = len(self.params) > 0 | ||
|
||
def set(self, params_dict: Dict): | ||
""" | ||
set my params | ||
""" | ||
self.params_dict = params_dict | ||
|
||
def apply_parameters(self) -> str: | ||
""" | ||
Replace Jinja templates in the query with corresponding parameter values. | ||
Returns: | ||
str: The query with Jinja templates replaced by parameter values. | ||
""" | ||
query = self.query | ||
for param, value in self.params_dict.items(): | ||
pattern = re.compile(r"{{\s*" + re.escape(param) + r"\s*\}\}") | ||
query = re.sub(pattern, value, query) | ||
return query | ||
|
||
|
||
class StoreDictKeyPair(argparse.Action): | ||
""" | ||
Custom argparse action to store key-value pairs as a dictionary. | ||
This class implements an argparse action to parse and store command-line | ||
arguments in the form of key-value pairs. The pairs should be separated by | ||
a comma and each key-value pair should be separated by an equals sign. | ||
Example: | ||
--option key1=value1,key2=value2,key3=value3 | ||
Reference: | ||
https://stackoverflow.com/a/42355279/1497139 | ||
""" | ||
|
||
def __call__( | ||
self, | ||
_parser: argparse.ArgumentParser, | ||
namespace: argparse.Namespace, | ||
values: str, | ||
_option_string: Optional[str] = None, | ||
) -> None: | ||
""" | ||
Parse key-value pairs and store them as a dictionary in the namespace. | ||
Args: | ||
parser (argparse.ArgumentParser): The argument parser object. | ||
namespace (argparse.Namespace): The namespace to store the parsed values. | ||
values (str): The string containing key-value pairs separated by commas. | ||
option_string (Optional[str]): The option string, if provided. | ||
""" | ||
my_dict = {} | ||
for kv in values.split(","): | ||
k, v = kv.split("=") | ||
my_dict[k] = v | ||
setattr(namespace, self.dest, my_dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
Created on 2024-05-06 | ||
@author: wf | ||
""" | ||
from lodstorage.params import Params | ||
from tests.basetest import Basetest | ||
|
||
|
||
class TestParams(Basetest): | ||
""" | ||
test the params handling | ||
""" | ||
|
||
def setUp(self, debug=False, profile=True): | ||
Basetest.setUp(self, debug=debug, profile=profile) | ||
|
||
def test_jinja_params(self): | ||
""" | ||
test jinia_params | ||
""" | ||
for sample, params_dict in [ | ||
("PREFIX target: <http://www.wikidata.org/entity/{{ q }}>", {"q": "Q80"}) | ||
]: | ||
params = Params(sample) | ||
if self.debug: | ||
print(params.params) | ||
self.assertEqual(["q"], params.params) | ||
self.assertTrue("q" in params.params_dict) | ||
params.params_dict = params_dict | ||
query = params.apply_parameters() | ||
self.assertEqual( | ||
"PREFIX target: <http://www.wikidata.org/entity/Q80>", query | ||
) |
Oops, something went wrong.