Skip to content

Commit 99cb504

Browse files
committed
fixes #67
1 parent ede1e31 commit 99cb504

25 files changed

+23907
-216
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ only-include = ["snapquery"]
9595

9696
[project.scripts]
9797
snapquery = "snapquery.snapquery_cmd:main"
98+
query-set = "snapquery.query_set_cmd:main"
9899

99100
[tool.black]
100101
line-length = 120

snapquery.puml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ note right of NamedQuery
5454
end note
5555

5656
note right of QueryStats
57+
see [https://github.com/WolfgangFahl/snapquery/blob/ede1e310151f6205163efe40aa07879779043f89/snapquery/snapquery_core.py#L36 snapquery_core]
5758
statistics about the execution of a query
5859
end note
5960

@@ -66,10 +67,11 @@ note right of QItem
6667
end note
6768

6869
note right of QueryDetails
70+
see [[https://github.com/WolfgangFahl/snapquery/blob/ede1e310151f6205163efe40aa07879779043f89/snapquery/snapquery_core.py#L384 snapquery_core]]
6971
Stores details for a named query
7072
end note
7173

7274
note right of ReferencedRessources
7375
Connects QueryDetails to Property and QItem
7476
end note
75-
@enduml
77+
@enduml

snapquery/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.2.2"
1+
__version__ = "0.2.3"

snapquery/qimport.py

Lines changed: 0 additions & 117 deletions
This file was deleted.

snapquery/query_set_cmd.py

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
"""
2+
Created on 2025-12-04
3+
4+
@author: wf
5+
"""
6+
from argparse import ArgumentParser, Namespace
7+
import sys
8+
9+
from basemkit.base_cmd import BaseCmd
10+
from snapquery.query_set_tool import QuerySetTool
11+
from snapquery.version import Version
12+
13+
14+
class QuerySetCmdVersion(Version):
15+
"""Version information"""
16+
name = "query-set"
17+
description = "QuerySet tool"
18+
19+
class QuerySetCmd(BaseCmd):
20+
"""
21+
Command Line Interface for Query Set handling
22+
"""
23+
24+
def add_arguments(self, parser: ArgumentParser):
25+
"""
26+
Add command-specific arguments.
27+
28+
Args:
29+
parser: ArgumentParser to add arguments to
30+
"""
31+
super().add_arguments(parser)
32+
# Conversion options
33+
parser.add_argument(
34+
"--convert",
35+
action="store_true",
36+
help="Convert a NamedQuerySet",
37+
)
38+
parser.add_argument(
39+
"-i",
40+
"--input",
41+
dest="input",
42+
help="Input file path or URL for a NamedQuerySet (JSON or YAML).",
43+
)
44+
parser.add_argument(
45+
"-o",
46+
"--output",
47+
dest="output",
48+
help="Output file path. If omitted, prints to stdout.",
49+
)
50+
parser.add_argument(
51+
"--in-format",
52+
choices=["auto", "json", "yaml"],
53+
default="auto",
54+
help="Input format. If 'auto', inferred from extension or tried JSON then YAML.",
55+
)
56+
parser.add_argument(
57+
"--out-format",
58+
choices=["yaml", "json"],
59+
default="yaml",
60+
help="Output format to write.",
61+
)
62+
parser.add_argument(
63+
"--indent",
64+
type=int,
65+
default=2,
66+
help="JSON pretty-print indentation for --out-format json.",
67+
)
68+
69+
# Short URL path: create a NamedQuerySet from one or more w.wiki URLs
70+
parser.add_argument(
71+
"--shorturl",
72+
nargs="+",
73+
help="One or more Wikidata short URLs (e.g., https://w.wiki/XXXX) to build a NamedQuerySet.",
74+
)
75+
parser.add_argument(
76+
"--domain",
77+
help="Domain for NamedQuery(s) (required with --shorturl).",
78+
)
79+
parser.add_argument(
80+
"--namespace",
81+
help="Namespace for NamedQuery(s) (required with --shorturl).",
82+
)
83+
parser.add_argument(
84+
"--llm",
85+
action="store_true",
86+
help="Enable LLM enrichment (placeholder; not used in this snippet).",
87+
)
88+
89+
def handle_args(self, args: Namespace) -> bool:
90+
"""
91+
Handle parsed arguments and execute scraping.
92+
93+
Args:
94+
args: Parsed command-line arguments
95+
96+
Returns:
97+
True if handled (no further processing needed)
98+
"""
99+
handled = super().handle_args(args)
100+
if handled:
101+
return True
102+
103+
if args.convert:
104+
self._handle_convert(args)
105+
return True
106+
107+
# Short URL → NamedQuerySet
108+
if args.shorturl:
109+
self._handle_shorturl(args)
110+
return True
111+
112+
return False
113+
114+
def _handle_convert(self, args: Namespace) -> None:
115+
"""
116+
Handle the conversion workflow delegating to QuerySetTool.
117+
"""
118+
if not args.input:
119+
raise ValueError("Missing --input for --convert")
120+
121+
tool = QuerySetTool()
122+
nq_set = tool.load_query_set(input_src=args.input, input_format=args.in_format)
123+
124+
self._output_dataset(nq_set, args)
125+
126+
def _handle_shorturl(self, args: Namespace) -> None:
127+
"""
128+
Handle the short URL workflow delegating to QuerySetTool.
129+
"""
130+
if not args.domain or not args.namespace:
131+
raise ValueError("--domain and --namespace are required with --shorturl")
132+
133+
tool = QuerySetTool()
134+
nq_set = tool.get_query_set_from_short_urls(
135+
short_urls=args.shorturl,
136+
domain=args.domain,
137+
namespace=args.namespace
138+
)
139+
140+
self._output_dataset(nq_set, args)
141+
142+
def _output_dataset(self, nq_set, args: Namespace) -> None:
143+
"""
144+
Helper to output a NamedQuerySet to file or stdout.
145+
"""
146+
out_fmt = args.out_format
147+
if args.output:
148+
# Save to file
149+
if out_fmt == "yaml":
150+
nq_set.save_to_yaml_file(args.output)
151+
else:
152+
nq_set.save_to_json_file(args.output, indent=args.indent)
153+
else:
154+
# Print to stdout
155+
if out_fmt == "yaml":
156+
sys.stdout.write(nq_set.to_yaml(sort_keys=False))
157+
else:
158+
sys.stdout.write(nq_set.to_json(indent=args.indent))
159+
160+
161+
def main(argv=None):
162+
"""Main entry point."""
163+
exit_code = QuerySetCmd.main(QuerySetCmdVersion(), argv)
164+
return exit_code
165+
166+
167+
if __name__ == "__main__":
168+
main()

0 commit comments

Comments
 (0)