Skip to content

Commit 213b506

Browse files
committed
feat(python): now generated python code passes flake8 --max-line-length 120 check
Fix #29
1 parent ea976ac commit 213b506

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

examples/python/routes.py

+26-19
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66

77
router = APIRouter()
88

9+
910
async def db_connection():
1011
return psycopg2.connect(
11-
database = os.getenv('DB_NAME'),
12-
user = os.getenv('DB_USER'),
13-
password = os.getenv('DB_PASSWORD'),
14-
host = os.getenv('DB_HOST', 'localhost'),
15-
port = 5432)
12+
database=os.getenv('DB_NAME'),
13+
user=os.getenv('DB_USER'),
14+
password=os.getenv('DB_PASSWORD'),
15+
host=os.getenv('DB_HOST', 'localhost'),
16+
port=5432)
1617

1718

1819
@router.get('/v1/categories/count')
19-
def get_v1_categories_count(conn = Depends(db_connection)):
20+
def get_v1_categories_count(conn=Depends(db_connection)):
2021
try:
2122
with conn:
22-
with conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor) as cur:
23+
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
2324
cur.execute("SELECT COUNT(*) AS counter FROM categories")
2425
result = cur.fetchone()
2526
if result is None:
@@ -28,11 +29,12 @@ def get_v1_categories_count(conn = Depends(db_connection)):
2829
finally:
2930
conn.close()
3031

32+
3133
@router.get('/v1/categories/stat')
32-
def get_v1_categories_stat(conn = Depends(db_connection)):
34+
def get_v1_categories_stat(conn=Depends(db_connection)):
3335
try:
3436
with conn:
35-
with conn.cursor(cursor_factory = psycopg2.extras.DictCursor) as cur:
37+
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
3638
result = {}
3739
cur.execute("SELECT COUNT(*) FROM categories")
3840
result['total'] = cur.fetchone()[0]
@@ -46,31 +48,33 @@ def get_v1_categories_stat(conn = Depends(db_connection)):
4648
finally:
4749
conn.close()
4850

51+
4952
@router.get('/v1/collections/{collectionId}/categories/count')
50-
def get_v1_collections_collection_id_categories_count(collectionId, conn = Depends(db_connection)):
53+
def get_v1_collections_collection_id_categories_count(collectionId, conn=Depends(db_connection)):
5154
try:
5255
with conn:
53-
with conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor) as cur:
56+
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
5457
cur.execute(
5558
"""
5659
SELECT COUNT(DISTINCT s.category_id) AS counter
5760
FROM collections_series cs
5861
JOIN series s
5962
ON s.id = cs.series_id
6063
WHERE cs.collection_id = %(collectionId)s
61-
""", { "collectionId": collectionId })
64+
""", {"collectionId": collectionId})
6265
result = cur.fetchone()
6366
if result is None:
6467
raise HTTPException(status_code=404)
6568
return result
6669
finally:
6770
conn.close()
6871

72+
6973
@router.get('/v1/categories')
70-
def get_list_v1_categories(limit, conn = Depends(db_connection)):
74+
def get_list_v1_categories(limit, conn=Depends(db_connection)):
7175
try:
7276
with conn:
73-
with conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor) as cur:
77+
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
7478
cur.execute(
7579
"""
7680
SELECT id
@@ -79,20 +83,22 @@ def get_list_v1_categories(limit, conn = Depends(db_connection)):
7983
, slug
8084
FROM categories
8185
LIMIT %(limit)s
82-
""", { "limit": limit })
86+
""", {"limit": limit})
8387
return cur.fetchall()
8488
finally:
8589
conn.close()
8690

91+
8792
@router.post('/v1/categories')
8893
def post_v1_categories():
8994
pass
9095

96+
9197
@router.get('/v1/categories/{categoryId}')
92-
def get_v1_categories_category_id(categoryId, conn = Depends(db_connection)):
98+
def get_v1_categories_category_id(categoryId, conn=Depends(db_connection)):
9399
try:
94100
with conn:
95-
with conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor) as cur:
101+
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
96102
cur.execute(
97103
"""
98104
SELECT id
@@ -101,19 +107,20 @@ def get_v1_categories_category_id(categoryId, conn = Depends(db_connection)):
101107
, slug
102108
FROM categories
103109
WHERE id = %(categoryId)s
104-
""", { "categoryId": categoryId })
110+
""", {"categoryId": categoryId})
105111
result = cur.fetchone()
106112
if result is None:
107113
raise HTTPException(status_code=404)
108114
return result
109115
finally:
110116
conn.close()
111117

118+
112119
@router.put('/v1/categories/{categoryId}')
113120
def put_v1_categories_category_id():
114121
pass
115122

123+
116124
@router.delete('/v1/categories/{categoryId}')
117125
def delete_v1_categories_category_id():
118126
pass
119-

src/templates/routes.py.ejs

+16-12
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ from fastapi import APIRouter, Depends, HTTPException
66

77
router = APIRouter()
88

9+
910
async def db_connection():
1011
return psycopg2.connect(
11-
database = os.getenv('DB_NAME'),
12-
user = os.getenv('DB_USER'),
13-
password = os.getenv('DB_PASSWORD'),
14-
host = os.getenv('DB_HOST', 'localhost'),
15-
port = 5432)
16-
12+
database=os.getenv('DB_NAME'),
13+
user=os.getenv('DB_USER'),
14+
password=os.getenv('DB_PASSWORD'),
15+
host=os.getenv('DB_HOST', 'localhost'),
16+
port=5432)
1717
<%
1818
// { "get", "/v1/categories/:categoryId" } => "get_v1_categories_category_id"
1919
function generate_method_name(method, path) {
@@ -57,7 +57,7 @@ endpoints.forEach(function(endpoint) {
5757
5858
// LATER: add support for aggregated_queries (#17)
5959
const argsFromQuery = method.query ? extractParamsFromQuery(method.query).map(stipOurPrefixes) : []
60-
const methodArgs = Array.from(new Set([...argsFromPath, ...argsFromQuery, 'conn = Depends(db_connection)']))
60+
const methodArgs = Array.from(new Set([...argsFromPath, ...argsFromQuery, 'conn=Depends(db_connection)']))
6161
6262
const queriesWithNames = []
6363
if (method.query) {
@@ -75,21 +75,22 @@ endpoints.forEach(function(endpoint) {
7575
const params = extractParamsFromQuery(query);
7676
const formattedParams = params.length > 0
7777
// [ "p.categoryId" ] => [ '"categoryId": categoryId' ]
78-
? ', { ' + params.map(param => param.substring(2)).map(param => `"${param}": ${param}`).join(', ') + ' }'
78+
? ', {' + params.map(param => param.substring(2)).map(param => `"${param}": ${param}`).join(', ') + '}'
7979
: ''
8080
queries.push({ [name]: { sql : sql, formattedParams: formattedParams }})
8181
}
8282
})
8383
8484
if (hasGetOne || hasGetMany) {
8585
%>
86+
8687
@router.get('<%- path %>')
8788
def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
8889
try:
8990
with conn:
9091
<% if (hasGetOne) {
9192
if (queries.length > 1) { /* we can omit cursor_factory but in this case we might get an unused import */-%>
92-
with conn.cursor(cursor_factory = psycopg2.extras.DictCursor) as cur:
93+
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
9394
result = {}
9495
<% queries.forEach(queryInfo => {
9596
for (const [name, query] of Object.entries(queryInfo)) {
@@ -104,7 +105,7 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
104105
} else {
105106
const query = queries[0].result
106107
-%>
107-
with conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor) as cur:
108+
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
108109
cur.execute(<%- query.sql %><%- query.formattedParams %>)
109110
result = cur.fetchone()
110111
if result is None:
@@ -114,7 +115,7 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
114115
} else {
115116
const query = queries[0].result
116117
-%>
117-
with conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor) as cur:
118+
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
118119
cur.execute(<%- query.sql %><%- query.formattedParams %>)
119120
return cur.fetchall()
120121
<%
@@ -126,6 +127,7 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
126127
}
127128
if (method.name === 'post') {
128129
%>
130+
129131
@router.post('<%- path %>')
130132
def <%- pythonMethodName %>():
131133
pass
@@ -134,6 +136,7 @@ def <%- pythonMethodName %>():
134136
}
135137
if (method.name === 'put') {
136138
%>
139+
137140
@router.put('<%- path %>')
138141
def <%- pythonMethodName %>():
139142
pass
@@ -142,6 +145,7 @@ def <%- pythonMethodName %>():
142145
}
143146
if (method.name === 'delete') {
144147
%>
148+
145149
@router.delete('<%- path %>')
146150
def <%- pythonMethodName %>():
147151
pass
@@ -150,4 +154,4 @@ def <%- pythonMethodName %>():
150154
}
151155
})
152156
})
153-
%>
157+
%>

0 commit comments

Comments
 (0)