Skip to content

Commit b630bdf

Browse files
committed
Add multifile support
1 parent 0b7e710 commit b630bdf

8 files changed

Lines changed: 194 additions & 29 deletions

readsql/__main__.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
22
import re
33

4-
from readsql.parse_args import validate, parse_args
4+
from readsql.parse_args import parse_args
5+
from readsql.parse_args import validate
56

67
DIR = os.path.dirname(__file__)
78

@@ -35,7 +36,9 @@ def read_file(file_name, inplace=True):
3536

3637
def read_python_file(file_name, variables=None, inplace=True):
3738
variables = variables if variables else ['query']
38-
variables_regex = f"(?:{'|'.join(variables)})" if len(variables) > 1 else variables[0]
39+
variables_regex = (
40+
f"(?:{'|'.join(variables)})" if len(variables) > 1 else variables[0]
41+
)
3942

4043
with open(file_name, 'r') as inp:
4144
lines = inp.read()
@@ -44,7 +47,9 @@ def read_python_file(file_name, variables=None, inplace=True):
4447
regex = [
4548
m
4649
for m in re.finditer(
47-
r'(?:\s*' + variables_regex + r'\s*=\s*f?)(?:"{1,3}|\'{1,3})([^"]*)(:?"|\')',
50+
r'(?:\s*'
51+
+ variables_regex
52+
+ r'\s*=\s*f?)(?:"{1,3}|\'{1,3})([^"]*)(:?"|\')',
4853
lines,
4954
)
5055
]
@@ -96,16 +101,25 @@ def read_regexes():
96101

97102
def command_line_file(args):
98103
validate(args)
104+
aggregate = []
99105

100-
if args.path.endswith('.py'):
101-
lines = read_python_file(args.path, args.python_var, inplace=False)
102-
else:
103-
lines = read_file(args.path, inplace=False)
106+
for path in args.path:
107+
if path.endswith('.py'):
108+
lines = read_python_file(path, args.python_var, inplace=False)
109+
else:
110+
lines = read_file(path, inplace=False)
111+
112+
if args.nothing:
113+
print(f'{path} would be reformatted to:\n', lines)
114+
aggregate.append(lines)
115+
else:
116+
print(f'{path} has been reformatted to:\n', lines)
104117

105-
print(f'{args.path} has been reformatted to:\n', lines)
118+
with open(path, 'w') as out:
119+
out.write(lines)
106120

107-
with open(args.path, 'w') as out:
108-
out.write(lines)
121+
if args.nothing:
122+
return aggregate
109123

110124

111125
def command_line():

readsql/parse_args.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
import argparse
12
import os
23
import sys
34

4-
import argparse
5-
65

76
def parse_args():
87
parser = argparse.ArgumentParser(
98
prog='readsql', description='Convert SQL to most human readable format'
109
)
11-
parser.add_argument('path', type=str, help='Path to the file to be converted')
10+
parser.add_argument('path', nargs='+', help='Path to the file to be converted')
1211
parser.add_argument('-s', '--string', action='store_true', help='Read a string')
12+
parser.add_argument(
13+
'-n', '--nothing', action='store_true', help='Do nothing to the file'
14+
)
1315
parser.add_argument(
1416
'-py',
1517
'--python_var',
@@ -24,7 +26,8 @@ def parse_args():
2426

2527

2628
def validate(args):
27-
path = args.path
28-
if not os.path.isfile(path):
29-
print('The file path specified does not exist')
30-
sys.exit()
29+
paths = args.path
30+
for path in paths:
31+
if not os.path.isfile(path):
32+
print('The file path specified does not exist')
33+
sys.exit()

tests/speed.txt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
func:test_read_python_file__var:(((), {'variable': ['sql']}))__took: 0.001723 sec
1+
func:test_read_python_file__var:(((), {'variable': ['sql']}))__took: 0.001756 sec
22

3-
func:test_read_file__var:(((), {}))__took: 0.005423 sec
3+
func:test_read_file__var:(((), {}))__took: 0.004506 sec
44

5-
func:test_read_python_file__var:(((), {}))__took: 0.003344 sec
5+
func:test_read_python_file__var:(((), {}))__took: 0.001280 sec
66

7-
func:test_double_select__var:(((), {}))__took: 0.000642 sec
7+
func:test_double_select__var:(((), {}))__took: 0.000313 sec
88

9-
func:test_select_from_groub_by_where__var:(((), {}))__took: 0.000595 sec
9+
func:test_select_from_groub_by_where__var:(((), {}))__took: 0.000471 sec
1010

11-
func:test_is_not_null__var:(((), {}))__took: 0.000583 sec
11+
func:test_is_not_null__var:(((), {}))__took: 0.000264 sec
1212

13-
func:test_distinct__var:(((), {}))__took: 0.001059 sec
13+
func:test_distinct__var:(((), {}))__took: 0.000390 sec
1414

15-
func:test_create_table_if_not_exists__var:(((), {}))__took: 0.000418 sec
15+
func:test_create_table_if_not_exists__var:(((), {}))__took: 0.000404 sec
1616

17-
func:test_read_python_file__var:(((), {'variable': ['sql', 'query_template', 'query']}))__took: 0.001387 sec
17+
func:test_read_python_file__var:(((), {'variable': ['sql', 'query_template', 'query']}))__took: 0.001234 sec
18+
19+
func:test_read_python_multifile__var:(((), {}))__took: 0.002904 sec
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
query = """
2+
select * from languages;
3+
"""
4+
5+
query = """
6+
select *
7+
from games
8+
where test=0
9+
"""
10+
11+
12+
def get_query1():
13+
query = (
14+
f"SELEct max(weight) from world where ocean='Atlantic' and water in not null"
15+
)
16+
return query
17+
18+
19+
def get_query2():
20+
limit = 6
21+
query = f"SELEct speed from world where animal='dolphin' limit {limit}"
22+
return query
23+
24+
25+
def get_query3():
26+
query = 'select 5'
27+
return query
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
query = """
2+
SELECT * FROM languages;
3+
"""
4+
5+
query = """
6+
SELECT *
7+
FROM games
8+
WHERE test=0
9+
"""
10+
11+
12+
def get_query1():
13+
query = (
14+
f"SELECT MAX(weight) FROM world WHERE ocean='Atlantic' and water in NOT null"
15+
)
16+
return query
17+
18+
19+
def get_query2():
20+
limit = 6
21+
query = f"SELECT speed FROM world WHERE animal='dolphin' LIMIT {limit}"
22+
return query
23+
24+
25+
def get_query3():
26+
query = 'SELECT 5'
27+
return query
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
query = """
2+
select * from languages;
3+
"""
4+
5+
query = """
6+
select *
7+
from games
8+
where test=0
9+
"""
10+
11+
12+
def get_query1():
13+
query = (
14+
f"SELEct max(weight) from world where ocean='Atlantic' and water in not null"
15+
)
16+
return query
17+
18+
19+
def get_query2():
20+
limit = 6
21+
query = f"SELEct speed from world where animal='dolphin' limit {limit}"
22+
return query
23+
24+
25+
def get_query3():
26+
query = 'select 5'
27+
return query
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
query = """
2+
SELECT * FROM languages;
3+
"""
4+
5+
query = """
6+
SELECT *
7+
FROM games
8+
WHERE test=0
9+
"""
10+
11+
12+
def get_query1():
13+
query = (
14+
f"SELECT MAX(weight) FROM world WHERE ocean='Atlantic' and water in NOT null"
15+
)
16+
return query
17+
18+
19+
def get_query2():
20+
limit = 6
21+
query = f"SELECT speed FROM world WHERE animal='dolphin' LIMIT {limit}"
22+
return query
23+
24+
25+
def get_query3():
26+
query = 'SELECT 5'
27+
return query

tests/test_files.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import argparse
12
import os
23

34
import readsql.__main__ as rsql
@@ -19,7 +20,9 @@ def test_read_file():
1920
def test_read_python_file_wrap():
2021
@timing
2122
def test_read_python_file():
22-
return rsql.read_python_file(file_name=DIR + '/sql_in_python_example.py', inplace=False)
23+
return rsql.read_python_file(
24+
file_name=DIR + '/sql_in_python_example.py', inplace=False
25+
)
2326

2427
example = test_read_python_file()
2528
with open(DIR + '/sql_in_python_example_correct.py', 'r') as inp:
@@ -29,7 +32,11 @@ def test_read_python_file():
2932
def test_read_python_file_variable_wrap():
3033
@timing
3134
def test_read_python_file(variable):
32-
return rsql.read_python_file(file_name=DIR + '/sql_in_python_variable_example.py', variables=variable, inplace=False)
35+
return rsql.read_python_file(
36+
file_name=DIR + '/sql_in_python_variable_example.py',
37+
variables=variable,
38+
inplace=False,
39+
)
3340

3441
example = test_read_python_file(variable=['sql'])
3542
with open(DIR + '/sql_in_python_variable_example_correct.py', 'r') as inp:
@@ -39,8 +46,39 @@ def test_read_python_file(variable):
3946
def test_read_python_file_variables_wrap():
4047
@timing
4148
def test_read_python_file(variable):
42-
return rsql.read_python_file(file_name=DIR + '/sql_in_python_variables_example.py', variables=variable, inplace=False)
49+
return rsql.read_python_file(
50+
file_name=DIR + '/sql_in_python_variables_example.py',
51+
variables=variable,
52+
inplace=False,
53+
)
4354

4455
example = test_read_python_file(variable=['sql', 'query_template', 'query'])
4556
with open(DIR + '/sql_in_python_variables_example_correct.py', 'r') as inp:
4657
assert inp.read() == example
58+
59+
60+
def test_read_python_multifile_wrap():
61+
args = argparse.Namespace(
62+
nothing=True,
63+
path=[
64+
DIR + '/sql_in_python_multifile1_example.py',
65+
DIR + '/sql_in_python_multifile2_example.py',
66+
],
67+
python_var=['query'],
68+
string=False,
69+
)
70+
71+
@timing
72+
def test_read_python_multifile():
73+
return rsql.command_line_file(args)
74+
75+
example = test_read_python_multifile()
76+
corrects = [
77+
DIR + '/sql_in_python_multifile1_example_correct.py',
78+
DIR + '/sql_in_python_multifile2_example_correct.py',
79+
]
80+
aggregate = []
81+
for file in corrects:
82+
with open(file, 'r') as inp:
83+
aggregate.append(inp.read())
84+
assert str(aggregate) == str(example)

0 commit comments

Comments
 (0)