Skip to content

Commit 9fd9f82

Browse files
Added error handling for few cases.
1 parent f3b0b40 commit 9fd9f82

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

ip2location_csv_converter/commandline.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ def main():
5555
conversion_mode = 'range'
5656
write_mode = 'replace'
5757

58+
if (re.search(regex1, input_file) != None):
59+
print (f"Invalid input {input_file} detected for input filename. Please check again.")
60+
sys.exit(1)
61+
if (re.search(regex2, input_file) != None):
62+
print (f"Invalid input {input_file} detected for input filename. Please check again.")
63+
sys.exit(1)
64+
65+
if (re.search(regex1, output_file) != None):
66+
print (f"Invalid input {output_file} detected for input filename. Please check again.")
67+
sys.exit(1)
68+
if (re.search(regex2, output_file) != None):
69+
print (f"Invalid input {output_file} detected for input filename. Please check again.")
70+
sys.exit(1)
71+
5872
if ((os.path.isfile(input_file)) is False):
5973
print ("File doesn't exist! Please check again.")
6074
sys.exit(1)
@@ -67,6 +81,9 @@ def main():
6781
if param2 == '':
6882
print ("Please provide the database type of the CSV file.")
6983
sys.exit(1)
84+
elif (re.search(regex2, param2) != None):
85+
print ("Please provide the database type of the CSV file.")
86+
sys.exit(1)
7087
print(f'Converting {input_file} to {output_file} now...')
7188
csv_to_parquet(input_file, output_file, param2)
7289
print(f'Conversion done.')

ip2location_csv_converter/ip2location_csv_converter.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,13 @@ def csv_to_parquet(input_file, output_file, db_type):
305305
last_line = get_last_row(csv_file)
306306

307307
df_last = pd.read_csv(StringIO(last_line), header=None)
308+
309+
# Check the number of column against the header row
310+
if len(column_names) != df_last.shape[1]:
311+
print(f"Column numbers not matching, aborting now...")
312+
sys.exit(1)
313+
308314
ip_value = df_last.iloc[0, 0] # Replace with actual index
309-
# print("Is IPv6?", is_ipv6(ip_value))
310-
# print(f"{ip_value} is {detect_ip_version_from_number(ip_value)}")
311315
ip_ver = detect_ip_version_from_number(ip_value)
312316

313317
if column_names != '':

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setuptools.setup(
1212
name="ip2location-python-csv-converter",
13-
version="1.3.2",
13+
version="1.3.3",
1414
description="Python script to converts IP2Location CSV database into IP range or CIDR format.",
1515
long_description_content_type="text/markdown",
1616
long_description=long_description,

0 commit comments

Comments
 (0)