Skip to content

Commit 43bbeef

Browse files
update dbhelper
1 parent d516b0a commit 43bbeef

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

poetry.lock

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pycountry = "*"
2828
httpx = {extras = ["http2", "socks"], version = "*"}
2929
cleantext = "*"
3030
scrapy-playwright = "*"
31+
psycopg2 = "*"
3132

3233
[tool.poetry.group.dev.dependencies]
3334
ruff = "*"

tpdb/helpers/dbhelper.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
1+
from configparser import ConfigParser
2+
import psycopg2
3+
4+
5+
def db_config(filename='database_tpdb.ini', section='postgresql'):
6+
# create a parser
7+
parser = ConfigParser()
8+
# read config file
9+
parser.read(filename)
10+
11+
# get section, default to postgresql
12+
db = {}
13+
if parser.has_section(section):
14+
params = parser.items(section)
15+
for param in params:
16+
db[param[0]] = param[1]
17+
else:
18+
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
19+
20+
return db
21+
22+
123
def db_conn():
2-
pass
24+
db = db_config()
25+
conn = psycopg2.connect(**db)
26+
conn.autocommit = True
27+
cursor = conn.cursor()
28+
return conn, cursor

0 commit comments

Comments
 (0)