Skip to content
This repository was archived by the owner on Feb 3, 2024. It is now read-only.

Commit 90cd651

Browse files
authored
Merge pull request #330 from maarten-boot/master
sync with 1.20230829.1 whoisdomain; add makefile to test whl
2 parents f6c5188 + 83fc71f commit 90cd651

32 files changed

+7132
-2216
lines changed

makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
3+
all: reformat test build testwhl
4+
5+
reformat:
6+
./reformat-code.sh
7+
8+
test:
9+
./test2.py -d google.com
10+
11+
build:
12+
python -m build
13+
tar tvf dist/*.tar.gz
14+
15+
testwhl:
16+
./testwhl.sh 2>2 | tee 1

setup.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,20 @@
1010
download_url="https://github.com/DannyCork/python-whois/releases/latest",
1111
url="https://github.com/DannyCork/python-whois/",
1212
platforms=["any"],
13-
packages=["whois"],
13+
package_dir={
14+
"whois": "whois",
15+
"whois.tldDb": "whois/tldDb",
16+
"whois.context": "whois/context",
17+
"whois.cache": "whois/cache",
18+
"whois.strings": "whois/strings",
19+
},
20+
packages=[
21+
"whois",
22+
"whois.tldDb",
23+
"whois.context",
24+
"whois.cache",
25+
"whois.strings",
26+
],
1427
keywords=[
1528
"Python",
1629
"whois",

testwhl.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import whois
2+
3+
domain = "google.com"
4+
d = whois.query(domain, verbose=True)
5+
print(d.__dict__)

testwhl.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#! /usr/bin/env bash
2+
3+
ENV="tmp/t1"
4+
5+
mkdir -p tmp
6+
rm -rf tmp/t1
7+
python3 -m venv ${ENV}
8+
source ./${ENV}/bin/activate
9+
pip3 install dist/*.whl
10+
11+
python testwhl.py

whois/_0_init_tld.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import re
2-
import sys
32

43
from typing import (
54
Dict,
65
List,
76
Any,
87
)
98

10-
from .tld_regexpr import ZZ
119
from .exceptions import (
1210
UnknownTld,
1311
)
1412

15-
# from .parameterContext import ParameterContext # not needed yet here
13+
from .tldDb.tld_regexpr import ZZ
14+
1615

17-
Verbose: bool = False
1816
TLD_RE: Dict[str, Dict[str, Any]] = {}
1917
REG_COLLECTION_BY_KEY: Dict[str, Any] = {}
2018

@@ -44,12 +42,10 @@ def _get_tld_re(
4442
if "extend" in tmp:
4543
del tmp["extend"]
4644

47-
# we want now to exclude _server hints
45+
# we want now to exclude andy key starting with _ like _server,_test, ...
46+
# dont recompile each re by themselves, reuse existing compiled re
4847
tld_re = dict(
49-
# (k, re.compile(v, re.IGNORECASE) if (isinstance(v, str) and k[0] != "_") else v) for k, v in tmp.items()
50-
# dont recompile each re by themselves, reuse existing compiled re
51-
(k, REG_COLLECTION_BY_KEY[k][v] if (isinstance(v, str) and k[0] != "_") else v)
52-
for k, v in tmp.items()
48+
(k, REG_COLLECTION_BY_KEY[k][v] if (isinstance(v, str) and k[0] != "_") else v) for k, v in tmp.items()
5349
)
5450

5551
# meta domains start with _: examples _centralnic and _donuts
@@ -77,8 +73,6 @@ def _initOne(
7773

7874
# also automatically see if we can internationalize the domains to the official ascii string
7975
TLD_RE[tld2] = what
80-
if Verbose:
81-
print(f"{tld} -> {tld2}", file=sys.stderr)
8276

8377

8478
def _buildRegCollection(
@@ -141,20 +135,13 @@ def filterTldToSupportedPattern(
141135
# we have max 2 levels so first check if the last 2 are in our list
142136
tld = f"{d[-2]}.{d[-1]}"
143137
if tld in ZZ:
144-
if verbose:
145-
print(f"we have {tld}", file=sys.stderr)
146138
return tld
147139

148140
# if not check if the last item we have
149141
tld = f"{d[-1]}"
150142
if tld in ZZ:
151-
if verbose:
152-
print(f"we have {tld}", file=sys.stderr)
153143
return tld
154144

155-
if verbose:
156-
print(f"we DONT have {tld}", file=sys.stderr)
157-
158145
# if not fail
159146
a = f"The TLD {tld} is currently not supported by this package."
160147
b = "Use validTlds() to see what toplevel domains are supported."
@@ -169,7 +156,7 @@ def mergeExternalDictWithRegex(
169156
for tld in aDict.keys():
170157
ZZ[tld] = aDict[tld]
171158

172-
# reprocess te regexes we newly defined or overrode
159+
# reprocess the regexes we newly defined or overrode
173160
override = True
174161
for tld in aDict.keys():
175162
_initOne(tld, override)

whois/__init__.py

Lines changed: 158 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Callable,
1212
)
1313

14+
1415
from .version import (
1516
VERSION,
1617
)
@@ -32,27 +33,44 @@
3233
mergeExternalDictWithRegex,
3334
)
3435

35-
from .doQuery import query, q2
36-
from .lastWhois import get_last_raw_whois_data
36+
from .processWhoisDomainRequest import ProcessWhoisDomainRequest
37+
from .lastWhois import (
38+
get_last_raw_whois_data,
39+
initLastWhois,
40+
)
3741
from .doWhoisCommand import setMyCache
38-
from .noneStrings import NoneStrings, NoneStringsAdd
39-
from .quotaStrings import QuotaStrings, QuotaStringsAdd
42+
from .domain import Domain
4043

44+
from .strings.noneStrings import NoneStrings, NoneStringsAdd
45+
from .strings.quotaStrings import QuotaStrings, QuotaStringsAdd
46+
from .tldDb.tld_regexpr import ZZ
47+
from .context.dataContext import DataContext
48+
from .context.parameterContext import ParameterContext
49+
from .cache.simpleCacheBase import SimpleCacheBase
50+
from .cache.simpleCacheWithFile import SimpleCacheWithFile
51+
from .cache.dummyCache import DummyCache
52+
from .cache.dbmCache import DBMCache
4153

42-
from .doParse import (
43-
cleanupWhoisResponse,
44-
)
54+
HAS_REDIS = False
55+
try:
56+
import redis
4557

46-
from .tld_regexpr import ZZ
47-
from .domain import Domain
48-
from .parameterContext import ParameterContext
58+
HAS_REDIS = True
59+
except Exception as e:
60+
_ = e
61+
62+
if HAS_REDIS:
63+
from .cache.redisCache import RedisCache
4964

50-
from .simpleCacheBase import SimpleCacheBase
51-
from .simpleCacheWithFile import SimpleCacheWithFile
52-
from .dummyCache import DummyCache
53-
from .dbmCache import DBMCache
65+
WHOISDOMAIN: str = ""
66+
if os.getenv("WHOISDOMAIN"):
67+
WHOISDOMAIN = str(os.getenv("WHOISDOMAIN"))
5468

55-
# from .redisCache import RedisCache
69+
WD = WHOISDOMAIN.upper().split(":")
70+
71+
SIMPLISTIC = False
72+
if "SIMPLISTIC" in WD:
73+
SIMPLISTIC = True
5674

5775
TLD_LIB_PRESENT: bool = False
5876
try:
@@ -74,7 +92,6 @@
7492
# from init_tld
7593
"validTlds",
7694
"TLD_RE",
77-
"mergeExternalDictWithRegex",
7895
# from version
7996
"VERSION",
8097
# from parameterContext
@@ -112,6 +129,77 @@ def _inner(*args: str, **kw: Any) -> Dict[str, Any]:
112129
return _inner
113130

114131

132+
def q2(
133+
domain: str,
134+
pc: ParameterContext,
135+
) -> Optional[Domain]:
136+
137+
initLastWhois()
138+
139+
dc = DataContext(
140+
pc=pc,
141+
domain=domain,
142+
)
143+
pwdr = ProcessWhoisDomainRequest(
144+
domain=domain,
145+
pc=pc,
146+
dc=dc,
147+
)
148+
149+
return pwdr.processRequest()
150+
151+
152+
def query(
153+
domain: str,
154+
force: bool = False,
155+
cache_file: Optional[str] = None,
156+
cache_age: int = 60 * 60 * 48,
157+
slow_down: int = 0,
158+
ignore_returncode: bool = False,
159+
server: Optional[str] = None,
160+
verbose: bool = False,
161+
with_cleanup_results: bool = False,
162+
internationalized: bool = False,
163+
include_raw_whois_text: bool = False,
164+
return_raw_text_for_unsupported_tld: bool = False,
165+
timeout: Optional[float] = None,
166+
parse_partial_response: bool = False,
167+
cmd: str = "whois",
168+
simplistic: bool = False,
169+
withRedacted: bool = False,
170+
pc: Optional[ParameterContext] = None,
171+
# if you use pc as argument all above params (except domain are ignored)
172+
) -> Optional[Domain]:
173+
# see documentation about paramaters in parameterContext.py
174+
175+
assert isinstance(domain, str), Exception("`domain` - must be <str>")
176+
177+
if pc is None:
178+
pc = ParameterContext(
179+
force=force,
180+
cache_file=cache_file,
181+
cache_age=cache_age,
182+
slow_down=slow_down,
183+
ignore_returncode=ignore_returncode,
184+
server=server,
185+
verbose=verbose,
186+
with_cleanup_results=with_cleanup_results,
187+
internationalized=internationalized,
188+
include_raw_whois_text=include_raw_whois_text,
189+
return_raw_text_for_unsupported_tld=return_raw_text_for_unsupported_tld,
190+
timeout=timeout,
191+
parse_partial_response=parse_partial_response,
192+
cmd=cmd,
193+
simplistic=simplistic,
194+
withRedacted=withRedacted,
195+
)
196+
197+
if verbose:
198+
print(pc, file=sys.stderr)
199+
200+
return q2(domain=domain, pc=pc)
201+
202+
115203
# Add get function to support return result in dictionary form
116204
get = _result2dict(query)
117205

@@ -129,3 +217,57 @@ def getTestHint(tldString: str) -> Optional[str]:
129217
return None
130218

131219
return str(ZZ[tldString][k])
220+
221+
222+
def cleanupWhoisResponse(
223+
whoisStr: str,
224+
verbose: bool = False,
225+
with_cleanup_results: bool = False,
226+
withRedacted: bool = False,
227+
pc: Optional[ParameterContext] = None,
228+
) -> str:
229+
tmp2: List[str] = []
230+
231+
if pc is None:
232+
pc = ParameterContext(
233+
verbose=verbose,
234+
withRedacted=withRedacted,
235+
with_cleanup_results=with_cleanup_results,
236+
)
237+
238+
skipFromHere = False
239+
tmp: List[str] = whoisStr.split("\n")
240+
for line in tmp:
241+
if skipFromHere is True:
242+
continue
243+
244+
# some servers respond with: % Quota exceeded in the comment section (lines starting with %)
245+
if "quota exceeded" in line.lower():
246+
raise WhoisQuotaExceeded(whoisStr)
247+
248+
if pc.with_cleanup_results is True and line.startswith("%"): # only remove if requested
249+
continue
250+
251+
if pc.withRedacted is False:
252+
if "REDACTED FOR PRIVACY" in line: # these lines contibute nothing so ignore
253+
continue
254+
255+
if (
256+
"Please query the RDDS service of the Registrar of Record" in line
257+
): # these lines contibute nothing so ignore
258+
continue
259+
260+
# regular responses may at the end have meta info starting with a line >>> some texte <<<
261+
# similar trailing info exists with lines starting with -- but we wil handle them later
262+
# unfortunalery we have domains (google.st) that have this early at the top
263+
if 0:
264+
if line.startswith(">>>"):
265+
skipFromHere = True
266+
continue
267+
268+
if line.startswith("Terms of Use:"): # these lines contibute nothing so ignore
269+
continue
270+
271+
tmp2.append(line.strip("\r"))
272+
273+
return "\n".join(tmp2)

whois/cache/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)