3
3
from typing import Final
4
4
from pathlib import Path
5
5
from sys import argv
6
+ import sys
6
7
import os
7
8
import json
8
9
import psycopg2
11
12
12
13
# Database info
13
14
HOSTNAME : Final [str ] = 'localhost'
14
- DATABASE : Final [ str ] = 'bootcamp'
15
+ DATABASE : str = 'bootcamp'
15
16
USERNAME : str = os .environ ['BOOTCAMP_USER' ]
16
17
PWD : str = os .environ ['BOOTCAMP_CREDS' ]
17
- PORT_ID : Final [ str ] = '5433 '
18
+ PORT_ID : str = '5432 '
18
19
19
20
# Options
20
21
FORMAT : bool = True
@@ -561,6 +562,8 @@ def print_help():
561
562
562
563
print ('Usage: python ./script.py ([options])' )
563
564
print ('Example:' )
565
+ print ('\t python ./script.py -db=some_database' )
566
+ print ('\t python ./script.py -p=5433' )
564
567
print ('\t python ./script.py -c=false' )
565
568
print ('\t python ./script.py -f' )
566
569
print ('\t python ./script.py --cache=false' )
@@ -571,13 +574,19 @@ def print_help():
571
574
print ('\t python ./script.py --cache=false --verbose' )
572
575
print ('\n ' )
573
576
print ('Options:' )
574
- print ('-c, --cache\t \t Whether or not to cache and use the cached results. Default=true' )
577
+ print ('-db, --database\t \t Name of the database to connect to (DEFAULT="bootcamp")' )
578
+ print ('\t \t \t \t -db=some_db' )
579
+ print ('\t \t \t \t -database=some_other_db' )
580
+ print ('-p, --port\t \t The port to connect to (DEFAULT=5432)' )
581
+ print ('\t \t \t \t -p=5433' )
582
+ print ('\t \t \t \t --port=5434' )
583
+ print ('-c, --cache\t \t Whether or not to cache and use the cached results (Default=true)' )
575
584
print ('\t \t \t Disable caching and the use of any existing cache:' )
576
- print ('\t \t \t -c=false' )
577
- print ('\t \t \t --cache=false' )
585
+ print ('\t \t \t \t -c=false' )
586
+ print ('\t \t \t \t --cache=false' )
578
587
print ('-fmt, --format\t \t Format the fields to be consistent (DEFAULT=true)' )
579
- print ('\t \t \t -fmt=false' )
580
- print ('\t \t \t --format=false' )
588
+ print ('\t \t \t \t -fmt=false' )
589
+ print ('\t \t \t \t --format=false' )
581
590
print ('\t \t \t Disable consistency formatting:' )
582
591
print ("-f, --force\t \t Force a cache update" )
583
592
print ('-v, --verbose\t \t Prints out detailed information of the process' )
@@ -586,7 +595,7 @@ def print_help():
586
595
print ('-h, --help\t \t Print usage information' )
587
596
588
597
async def main ():
589
- global VERBOSE , FORCE_CACHE_UPDATE , CACHE , FORMAT
598
+ global VERBOSE , FORCE_CACHE_UPDATE , CACHE , FORMAT , DATABASE , PORT_ID
590
599
filename : str = 'swapi_data'
591
600
bin_location : str = './bin'
592
601
@@ -650,7 +659,30 @@ async def main():
650
659
elif count > 1 :
651
660
print ('Invalid syntax: Too many = in the flag %s' , la )
652
661
return
662
+ elif arg .startswith ('-db=' ) or arg .startswith ('--database=' ):
663
+ count : int = arg .count ('=' )
653
664
665
+ if count == 1 :
666
+ index : int = la .index ('=' ) + 1
667
+ value : str = la [index :]
668
+
669
+ DATABASE = value
670
+
671
+ elif count > 1 :
672
+ print ('Invalid syntax: Too many = in the flag %s' , la )
673
+ return
674
+ elif arg .startswith ('-p=' ) or arg .startswith ('--port=' ):
675
+ count : int = arg .count ('=' )
676
+
677
+ if count == 1 :
678
+ index : int = la .index ('=' ) + 1
679
+ value : str = la [index :]
680
+
681
+ PORT_ID = value
682
+
683
+ elif count > 1 :
684
+ print ('Invalid syntax: Too many = in the flag %s' , la )
685
+ return
654
686
655
687
if FORCE_CACHE_UPDATE and not CACHE :
656
688
print ('Cannot force a cache update and not have caching enabled.' )
@@ -660,6 +692,8 @@ async def main():
660
692
661
693
if VERBOSE :
662
694
print ('Running with Options:' )
695
+ print ('\t Database:' , DATABASE )
696
+ print ('\t Database:' , PORT_ID )
663
697
print ('\t Verbose Output:' , VERBOSE )
664
698
print ('\t Formatting:' , FORMAT )
665
699
print ('\t Cache:' , CACHE )
@@ -712,7 +746,7 @@ async def main():
712
746
print ('Constructing %s table..' , category )
713
747
714
748
if not create_table (cursor , category ):
715
- quit (- 1 )
749
+ sys . exit (- 1 )
716
750
717
751
print ('Populating tables..' )
718
752
@@ -721,15 +755,21 @@ async def main():
721
755
print ('Populating %s table..' , category )
722
756
723
757
if not populate_table (cursor , category ):
724
- quit (- 1 )
758
+ sys . exit (- 1 )
725
759
726
760
if VERBOSE :
727
761
print (len (DATA [category ]), ' records inserted' )
728
762
729
763
connection .commit ()
730
-
764
+
765
+ except psycopg2 .errors .OperationalError as e :
766
+ print ("Failed to connect.. \n {0}" .format (e ))
767
+ sys .exit (- 1 )
768
+
731
769
except Exception as err :
732
- print ('Encountered an error: ' , err .__class__ .__name__ )
770
+ # print('Encountered an error: ', err.__class__.__name__)
771
+ print ('Encountered an error: ' , err )
772
+ sys .exit (- 1 )
733
773
734
774
finally :
735
775
if cursor is not None :
0 commit comments