Skip to content

Commit 6e61371

Browse files
committed
replace parametercontext
1 parent ee592e2 commit 6e61371

4 files changed

Lines changed: 103 additions & 104 deletions

File tree

CLAUDE_TODO

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,21 @@
33
# TLD regex database compiled eagerly at import
44
whoisdomain/helpers.py · lines 99–100
55

6-
Importing whoisdomain forces processing all 3,000+ lines of ZZ immediately. Consumers who only need one TLD pay the cost of all of them. Make MY_TLD_INFO lazy — initialize on first call to get_TLD_RE() or filterTldToSupportedPattern(). Bonus: significantly faster python -c "import whoisdomain", which matters for CLI tools and lambdas.
7-
8-
---
9-
10-
# ParameterContext is a JSON-parsed config rather than a dataclass
11-
whoisdomain/context/parameterContext.py
12-
13-
A 150-line JSON literal is parsed at import to define ~25 parameters, then __getattr__ returns Any for every read. mypy can't help you, IDE autocomplete can't help your users, and the validation code (~80 lines) duplicates what @dataclass(slots=True) + __post_init__ would give you for free. Sketch:
14-
15-
@dataclass(slots=True)
16-
class ParameterContext:
17-
cmd: str = "whois"
18-
timeout: float = 30.0
19-
cache_age: int = 60 * 60 * 48
20-
verbose: bool = False
21-
rdap_only: bool = False
22-
# ... etc
23-
def to_json(self) -> str: return json.dumps(asdict(self))
24-
@classmethod
25-
def from_json(cls, s: str) -> "ParameterContext":
26-
return cls(**json.loads(s))
6+
Importing whoisdomain forces processing all 3,000+ lines of ZZ immediately.
7+
Consumers who only need one TLD pay the cost of all of them.
8+
Make MY_TLD_INFO lazy — initialize on first call to get_TLD_RE() or filterTldToSupportedPattern().
9+
Bonus: significantly faster python -c "import whoisdomain", which matters for CLI tools and lambdas.
2710

2811
---
2912

3013
# Module-level mutable globals everywhere
3114
helpers.py:99, doWhoisCommand.py:20, lastWhois.py:21, main.py:550
3215

33-
MY_TLD_INFO, CACHE_STUB, LastWhois, and a 14-name global declaration in main(). This makes the package effectively a singleton — concurrent users (e.g. an async webapp) will fight over state, and tests can't run in parallel. Move shared state onto a Whois client class that callers instantiate, and keep the module-level helpers as thin wrappers around a default instance for backwards compatibility.
16+
MY_TLD_INFO, CACHE_STUB, LastWhois, and a 14-name global declaration in main().
17+
This makes the package effectively a singleton — concurrent users (e.g. an async webapp) will fight over state,
18+
and tests can't run in parallel.
19+
Move shared state onto a Whois client class that callers instantiate,
20+
and keep the module-level helpers as thin wrappers around a default instance for backwards compatibility.
3421

3522
---
3623

whoisdomain/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def remoteQ2(
156156
print("remoteQ2:Receive:", request, file=sys.stderr)
157157

158158
pc: ParameterContext = ParameterContext()
159-
pc.fromJson(request["pc"])
159+
pc.from_json(request["pc"])
160160

161161
# call the func
162162
allOk = True

whoisdomain/context/parameterContext.py

Lines changed: 92 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,95 @@
66

77
log = logging.getLogger(__name__)
88

9+
10+
@dataclass(slots=True)
11+
class ParameterContext:
12+
# currently no params are mandatory
13+
14+
# if the whois command fails with code 1 still process the data returned as normal.
15+
ignore_returncode: bool = False
16+
17+
# Don't use cache.
18+
force: bool = False
19+
20+
# bla bla
21+
verbose: bool = False
22+
23+
# cleanup lines starting with % and REDACTED FOR PRIVACY
24+
with_cleanup_results: bool = False
25+
26+
# if true convert with internationalizedDomainNameToPunyCode()
27+
internationalized: bool = False
28+
29+
# if reqested the full response is also returned.
30+
include_raw_whois_text: bool = False
31+
32+
# as it says
33+
return_raw_text_for_unsupported_tld: bool = False
34+
35+
# try to parse partial response when cmd timed out
36+
# (stdbuf should be in PATH for best results)
37+
parse_partial_response: bool = False
38+
39+
# when simplistic is true,
40+
# we return null for most exceptions and dont pass info why we have no data.
41+
simplistic: bool = False
42+
43+
# show redacted output default no redacted data is shown
44+
withRedacted: bool = False
45+
46+
# specify the path to the cli whois you want to use.
47+
cmd: str = "whois"
48+
49+
# specify the path to the cli whois you want to use.
50+
cache_file: str | None = None
51+
52+
# use this whois server for making this query:
53+
# Linux/Mac: 'whois -h <server> <domain>'
54+
# Windows: 'whois.exe <domain> <server>'"
55+
server: str | None = None
56+
57+
# Cache expiration time for given domain in seconds 60*60*48 (48 hours).
58+
cache_age: int = 172800
59+
60+
# Time [s] it will wait after you query WHOIS database.
61+
slow_down: int = 0
62+
63+
# timeout in seconds for the whois command to return a result.
64+
timeout: float = 30.0
65+
66+
# allow auto install of sysinternals whois on windows if no whois found
67+
tryInstallMissingWhoisOnWindows: bool = False
68+
69+
# The number of lines we consider a short response.
70+
shortResponseLen: int = 5
71+
72+
# if lib 'tld' is installed add tld info based on get_tld(); fake the tld if needed
73+
withPublicSuffix: bool = False
74+
75+
# try to extract the whois servers from the whois output (uses --verbose)
76+
extractServers: bool = False
77+
78+
# strip https://icann.org/epp# from status response
79+
stripHttpStatus: bool = False
80+
81+
# if set to true we skip the strip www action
82+
noIgnoreWww: bool = False
83+
84+
# if set to true we only consult rdap
85+
rdapOnly: bool = False
86+
87+
# if set to true we only consult whois
88+
whoisOnly: bool = False
89+
90+
def to_json(self) -> str:
91+
return json.dumps(asdict(self))
92+
93+
@classmethod
94+
def from_json(cls, s: str) -> "ParameterContext":
95+
return cls(**json.loads(s))
96+
97+
998
ParamsStringJson: str = """
1099
{
11100
"ignore_returncode": {
@@ -156,84 +245,7 @@
156245
"""
157246

158247

159-
@dataclass(slots=True)
160248
class ParameterContext2:
161-
# if the whois command fails with code 1 still process the data returned as normal.
162-
ignore_returncode: bool = False
163-
# Don't use cache.
164-
force: bool = False
165-
verbose: bool = False
166-
# cleanup lines starting with % and REDACTED FOR PRIVACY
167-
with_cleanup_results: bool = False
168-
# if true convert with internationalizedDomainNameToPunyCode()
169-
internationalized: bool = False
170-
# if reqested the full response is also returned.
171-
include_raw_whois_text: bool = False
172-
return_raw_text_for_unsupported_tld: bool = False
173-
# try to parse partial response when cmd timed out
174-
# (stdbuf should be in PATH for best results)
175-
parse_partial_response: bool = False
176-
177-
# when simplistic is true,
178-
# we return null for most exceptions and dont pass info why we have no data.
179-
simplistic: bool = False
180-
181-
# show redacted output default no redacted data is shown
182-
withRedacted: bool = False
183-
184-
# specify the path to the cli whois you want to use.
185-
cmd: str = "whois"
186-
187-
# specify the path to the cli whois you want to use.
188-
cache_file: str | None = None
189-
190-
# use this whois server for making this query:
191-
# Linux/Mac: 'whois -h <server> <domain>'
192-
# Windows: 'whois.exe <domain> <server>'"
193-
server: str | None = None
194-
195-
# Cache expiration time for given domain in seconds 60*60*48 (48 hours).
196-
cache_age: int = 172800
197-
198-
# Time [s] it will wait after you query WHOIS database.
199-
slow_down: int = 0
200-
201-
# timeout in seconds for the whois command to return a result.
202-
timeout: float = 30.0
203-
204-
# allow auto install of sysinternals whois on windows if no whois found
205-
tryInstallMissingWhoisOnWindows: bool = False
206-
207-
# The number of lines we consider a short response.
208-
shortResponseLen: int = 5
209-
210-
# if lib 'tld' is installed add tld info based on get_tld(); fake the tld if needed
211-
withPublicSuffix: bool = False
212-
213-
# try to extract the whois servers from the whois output (uses --verbose)
214-
extractServers: bool = False
215-
216-
# strip https://icann.org/epp# from status response
217-
stripHttpStatus: bool = False
218-
219-
# if set to true we skip the strip www action
220-
noIgnoreWww: bool = False
221-
222-
# if set to true we only consult rdap
223-
rdapOnly: bool = False
224-
225-
# if set to true we only consult whois
226-
whoisOnly: bool = False
227-
228-
def to_json(self) -> str:
229-
return json.dumps(asdict(self))
230-
231-
@classmethod
232-
def from_json(cls, s: str) -> "ParameterContext2":
233-
return cls(**json.loads(s))
234-
235-
236-
class ParameterContext:
237249
kt: dict[str, Any]
238250
value: dict[str, Any]
239251
params: dict[str, Any]
@@ -355,13 +367,13 @@ def toJson(
355367

356368
def fromJson(
357369
self,
358-
jString: str,
370+
jstring: str,
359371
) -> None:
360-
zz = json.loads(jString)
372+
zz = json.loads(jstring)
361373
for k, v in zz.items():
362374
self.set(k, v)
363375

364376

365377
if __name__ == "__main__":
366378
pc = ParameterContext()
367-
print(pc.value)
379+
print(asdict(pc))

whoisdomain/procFunc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def oneItem(
4141
domain: str,
4242
pc: ParameterContext,
4343
) -> Any:
44-
jStr = pc.toJson()
44+
jStr = pc.to_json()
4545

4646
request: dict[str, Any] = {
4747
"domain": domain,

0 commit comments

Comments
 (0)