22import re , sys , datetime , csv , pkgutil
33from . import net , shared
44
5- try :
5+ try :
66 from io import StringIO
77except ImportError :
88 from cStringIO import StringIO
@@ -25,13 +25,13 @@ def read_dataset(filename, destination, abbrev_key, name_key, is_dict=False):
2525 destination [line [abbrev_key ]] = line [name_key ]
2626 except IOError as e :
2727 pass
28-
28+
2929airports = {}
3030countries = {}
3131states_au = {}
3232states_us = {}
3333states_ca = {}
34-
34+
3535try :
3636 reader = csv .reader (pkgdata ("airports.dat" ).splitlines ())
3737
@@ -50,7 +50,7 @@ def read_dataset(filename, destination, abbrev_key, name_key, is_dict=False):
5050
5151def precompile_regexes (source , flags = 0 ):
5252 return [re .compile (regex , flags ) for regex in source ]
53-
53+
5454grammar = {
5555 "_data" : {
5656 'id' : ['Domain ID:[ ]*(?P<val>.+)' ],
@@ -201,8 +201,8 @@ def precompile_regexes(source, flags=0):
201201}
202202
203203def preprocess_regex (regex ):
204- # Fix for #2; prevents a ridiculous amount of varying size permutations.
205- regex = re .sub (r"\\s\*\(\?P<([^>]+)>\.\+\)" , r"\s*(?P<\1>\S.*)" , regex )
204+ # Fix for #2; prevents a ridiculous amount of varying size permutations
205+ regex = re .sub (r"\\s\*\(\?P<([^>]+)>\.\+\)" , r"\\ s*(?P<\1>\ \S.*)" , regex )
206206 # Experimental fix for #18; removes unnecessary variable-size whitespace
207207 # matching, since we're stripping results anyway.
208208 regex = re .sub (r"\[ \]\*\(\?P<([^>]+)>\.\*\)" , r"(?P<\1>.*)" , regex )
@@ -553,7 +553,7 @@ def parse_raw_whois(raw_data, normalized=None, never_query_handles=True, handle_
553553 data ["nameservers" ].append (match .strip ())
554554 except KeyError as e :
555555 data ["nameservers" ] = [match .strip ()]
556-
556+
557557
558558 data ["contacts" ] = parse_registrants (raw_data , never_query_handles , handle_server )
559559
@@ -645,7 +645,7 @@ def normalize_data(data, normalized):
645645 for country , source in (("united states" , states_us ), ("australia" , states_au ), ("canada" , states_ca )):
646646 if country in contact ["country" ].lower () and contact ["state" ] in source :
647647 contact ["state" ] = source [contact ["state" ]]
648-
648+
649649 for key in ("email" ,):
650650 if key in contact and contact [key ] is not None and (normalized == True or key in normalized ):
651651 if is_string (contact [key ]):
@@ -660,7 +660,7 @@ def normalize_data(data, normalized):
660660 for key in ("city" , "organization" , "state" , "country" ):
661661 if key in contact and contact [key ] is not None and (normalized == True or key in normalized ):
662662 contact [key ] = normalize_name (contact [key ], abbreviation_threshold = 3 , length_threshold = 3 )
663-
663+
664664 if "name" in contact and "organization" not in contact :
665665 lines = [x .strip () for x in contact ["name" ].splitlines ()]
666666 new_lines = []
@@ -674,10 +674,10 @@ def normalize_data(data, normalized):
674674 contact ["name" ] = "\n " .join (lines )
675675 else :
676676 del contact ["name" ]
677-
677+
678678 if len (new_lines ) > 0 :
679679 contact ["organization" ] = "\n " .join (new_lines )
680-
680+
681681 if "street" in contact and "organization" not in contact :
682682 lines = [x .strip () for x in contact ["street" ].splitlines ()]
683683 if len (lines ) > 1 :
@@ -686,7 +686,7 @@ def normalize_data(data, normalized):
686686 contact ["organization" ] = lines [0 ]
687687 contact ["street" ] = "\n " .join (lines [1 :])
688688 break
689-
689+
690690 for key in list (contact .keys ()):
691691 try :
692692 contact [key ] = contact [key ].strip (", " )
@@ -831,10 +831,10 @@ def remove_suffixes(data):
831831 # Removes everything before and after the first non-whitespace continuous string.
832832 # Used to get rid of IP suffixes for nameservers.
833833 cleaned_list = []
834-
834+
835835 for entry in data :
836836 cleaned_list .append (re .search ("([^\s]+)\s*[\s]*" , entry ).group (1 ).lstrip ())
837-
837+
838838 return cleaned_list
839839
840840def parse_registrants (data , never_query_handles = True , handle_server = "" ):
@@ -911,7 +911,7 @@ def parse_registrants(data, never_query_handles=True, handle_server=""):
911911 elif category == "admin" :
912912 admin_contact = data_reference
913913 break
914-
914+
915915 # Post-processing
916916 for obj in (registrant , tech_contact , billing_contact , admin_contact ):
917917 if obj is not None :
@@ -986,18 +986,18 @@ def fetch_nic_contact(handle, lookup_server):
986986 response = net .get_whois_raw (handle , lookup_server )
987987 response = [segment .replace ("\r " , "" ) for segment in response ] # Carriage returns are the devil
988988 results = parse_nic_contact (response )
989-
989+
990990 if len (results ) > 0 :
991991 return results [0 ]
992992 else :
993993 raise shared .WhoisException ("No contact data found in the response." )
994-
994+
995995def parse_nic_contact (data ):
996996 handle_contacts = []
997997 for regex in nic_contact_regexes :
998998 for segment in data :
999999 matches = re .finditer (regex , segment )
10001000 for match in matches :
10011001 handle_contacts .append (match .groupdict ())
1002-
1002+
10031003 return handle_contacts
0 commit comments