Skip to content

Commit

Permalink
Fixed Pylint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cryolune committed Apr 5, 2021
1 parent 88f122b commit 43bcff6
Show file tree
Hide file tree
Showing 6 changed files with 352 additions and 303 deletions.
137 changes: 73 additions & 64 deletions app/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,93 @@
from sqlalchemy.sql import text
from setup import setup_environment

def insert_table(table,data):
#Make PostgreSQL Connection
engine = setup_environment.get_database()
data.to_sql(table, engine, if_exists = 'append', index=False)

def insert_sites_table(table,data):
#Make PostgreSQL Connection
csv.field_size_limit(sys.maxsize)
engine = setup_environment.get_database()
connection = None
connection = engine.raw_connection()
cur = connection.cursor()
with open(data, 'r') as f:
reader = csv.reader(f)
next(reader) # Skip the header row.
for row in reader:
cur.execute("INSERT INTO sites (sitename,city,state,country,notes,greenhouse,geometry,time_zone,soil,soilnotes) VALUES (%s, %s, %s, %s,%s, %s, ST_MakeValid(ST_Geomfromtext(%s,4326)), %s,%s, %s)",row)
print(row)
connection.commit()
connection.close()

def insert_table(table, data):
# Make PostgreSQL Connection
engine = setup_environment.get_database()
data.to_sql(table, engine, if_exists="append", index=False)


def insert_sites_table(table, data):
# Make PostgreSQL Connection
csv.field_size_limit(sys.maxsize)
engine = setup_environment.get_database()
connection = None
connection = engine.raw_connection()
cur = connection.cursor()
with open(data, "r") as f:
reader = csv.reader(f)
next(reader) # Skip the header row.
for row in reader:
cur.execute(
"INSERT INTO sites (sitename,city,state,country,notes,greenhouse,geometry,time_zone,soil,soilnotes) VALUES (%s, %s, %s, %s,%s, %s, ST_MakeValid(ST_Geomfromtext(%s,4326)), %s,%s, %s)",
row,
)
print(row)
connection.commit()
connection.close()


def fetch_specie_id(species):
# Make PostgreSQL Connection
engine = setup_environment.get_database()
connection = None
connection = engine.connect()
query = 'select id from species where scientificname = :name'
result_set = connection.execute(text(query), name = species).fetchone()
connection.close()
for r in result_set:
return r
engine = setup_environment.get_database()
connection = None
connection = engine.connect()
query = "select id from species where scientificname = :name"
result_set = connection.execute(text(query), name=species).fetchone()
connection.close()
for r in result_set:
return r


def fetch_sites_id(sitename):
# Make PostgreSQL Connection
engine = setup_environment.get_database()
connection = None
connection = engine.connect()
query = 'select id from sites where sitename = :name'
result_set = connection.execute(text(query), name = sitename).fetchone()
connection.close()
for r in result_set:
return r
engine = setup_environment.get_database()
connection = None
connection = engine.connect()
query = "select id from sites where sitename = :name"
result_set = connection.execute(text(query), name=sitename).fetchone()
connection.close()
for r in result_set:
return r


def fetch_cultivars_id(name,specie_id):
def fetch_cultivars_id(name, specie_id):
# Make PostgreSQL Connection
engine = setup_environment.get_database()
connection = None
connection = engine.connect()
query = 'select id from cultivars where name = :name and specie_id = :specie_id'
result_set = connection.execute(text(query), name = name,specie_id=specie_id).fetchone()
connection.close()
for r in result_set:
return r
engine = setup_environment.get_database()
connection = None
connection = engine.connect()
query = "select id from cultivars where name = :name and specie_id = :specie_id"
result_set = connection.execute(
text(query), name=name, specie_id=specie_id
).fetchone()
connection.close()
for r in result_set:
return r


def fetch_citations_id(author,year,title):
def fetch_citations_id(author, year, title):
# Make PostgreSQL Connection
engine = setup_environment.get_database()
connection = None
connection = engine.connect()
query = 'select id from citations where author = :author and year = :year and title = :title'
result_set = connection.execute(text(query),author=author,year=year, title = title).fetchone()
connection.close()
for r in result_set:
return r
engine = setup_environment.get_database()
connection = None
connection = engine.connect()
query = "select id from citations where author = :author and year = :year and title = :title"
result_set = connection.execute(
text(query), author=author, year=year, title=title
).fetchone()
connection.close()
for r in result_set:
return r


#Fetching ID from Users,experiments,cultivars and treatments tables
def fetch_id(value,table):
# Fetching ID from Users,experiments,cultivars and treatments tables
def fetch_id(value, table):
# Make PostgreSQL Connection
connection = None
engine = setup_environment.get_database()
connection = engine.connect()
query = 'select id from '+table+' where name = :name'
result_set = connection.execute(text(query), name = value).fetchone()
connection.close()
for r in result_set:
return r
connection = None
engine = setup_environment.get_database()
connection = engine.connect()
query = "select id from " + table + " where name = :name"
result_set = connection.execute(text(query), name=value).fetchone()
connection.close()
for r in result_set:
return r
14 changes: 8 additions & 6 deletions app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Main module of the Server file
"""

#Importing moudles
# Importing moudles
import connexion
import json
from time import sleep
Expand All @@ -14,7 +14,7 @@
sleep(5)

# Read the swagger.yml file to configure the endpoints
app.add_api("yaba.yaml",validate_responses=False)
app.add_api("yaba.yaml", validate_responses=False)


# create a URL route in our application for "/"
Expand All @@ -26,10 +26,12 @@ def home():
(on docker: localhost:5001/)
:return: the below json message
"""
return Response(json.dumps("Welcome to YABA API Index Route"), mimetype='application/json')
return Response(
json.dumps("Welcome to YABA API Index Route"), mimetype="application/json"
)


if __name__ == "__main__":
app.run(host="0.0.0.0",port=5000,debug=False)
#http_server = WSGIServer(('0.0.0.0', 5000), app)
#http_server.serve_forever()
app.run(host="0.0.0.0", port=5000, debug=False)
# http_server = WSGIServer(('0.0.0.0', 5000), app)
# http_server.serve_forever()
37 changes: 22 additions & 15 deletions app/setup/setup_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def get_database():
log.info("Successfully Connected to BETY Database!")
except IOError:
log.exception("Failed to get database connection!")
return None, 'fail'
return None, "fail"

return engine


def get_connection_from_config(config_file_name='credentials.yaml'):
def get_connection_from_config(config_file_name="credentials.yaml"):
"""
Sets up database connection from config file.
Input:
Expand All @@ -28,19 +28,25 @@ def get_connection_from_config(config_file_name='credentials.yaml'):
credentials for the PostgreSQL database
"""

with open(config_file_name, 'r') as f:
with open(config_file_name, "r") as f:
creds = yaml.safe_load(f)

if not ('PGHOST' in creds.keys() and
'PGUSER' in creds.keys() and
'PGPASSWORD' in creds.keys() and
'PGDATABASE' in creds.keys() and
'PGPORT' in creds.keys()):
raise Exception('Bad config file: ' + config_file_name)
if not (
"PGHOST" in creds.keys()
and "PGUSER" in creds.keys()
and "PGPASSWORD" in creds.keys()
and "PGDATABASE" in creds.keys()
and "PGPORT" in creds.keys()
):
raise Exception("Bad config file: " + config_file_name)

return get_engine(creds['PGDATABASE'], creds['PGUSER'],
creds['PGHOST'], creds['PGPORT'],
creds['PGPASSWORD'])
return get_engine(
creds["PGDATABASE"],
creds["PGUSER"],
creds["PGHOST"],
creds["PGPORT"],
creds["PGPASSWORD"],
)


def get_engine(db, user, host, port, passwd):
Expand All @@ -54,7 +60,8 @@ def get_engine(db, user, host, port, passwd):
passwd: Password for the database
"""

url = 'postgresql+psycopg2://{user}:{passwd}@{host}:{port}/{db}'.format(
user=user, passwd=passwd, host=host, port=port, db=db)
engine = create_engine(url,poolclass=QueuePool)
url = "postgresql+psycopg2://{user}:{passwd}@{host}:{port}/{db}".format(
user=user, passwd=passwd, host=host, port=port, db=db
)
engine = create_engine(url, poolclass=QueuePool)
return engine
Loading

0 comments on commit 43bcff6

Please sign in to comment.