Skip to content

Commit

Permalink
Add multifile support
Browse files Browse the repository at this point in the history
  • Loading branch information
AzisK committed Dec 6, 2020
1 parent 0b7e710 commit b630bdf
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 29 deletions.
34 changes: 24 additions & 10 deletions readsql/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import re

from readsql.parse_args import validate, parse_args
from readsql.parse_args import parse_args
from readsql.parse_args import validate

DIR = os.path.dirname(__file__)

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

def read_python_file(file_name, variables=None, inplace=True):
variables = variables if variables else ['query']
variables_regex = f"(?:{'|'.join(variables)})" if len(variables) > 1 else variables[0]
variables_regex = (
f"(?:{'|'.join(variables)})" if len(variables) > 1 else variables[0]
)

with open(file_name, 'r') as inp:
lines = inp.read()
Expand All @@ -44,7 +47,9 @@ def read_python_file(file_name, variables=None, inplace=True):
regex = [
m
for m in re.finditer(
r'(?:\s*' + variables_regex + r'\s*=\s*f?)(?:"{1,3}|\'{1,3})([^"]*)(:?"|\')',
r'(?:\s*'
+ variables_regex
+ r'\s*=\s*f?)(?:"{1,3}|\'{1,3})([^"]*)(:?"|\')',
lines,
)
]
Expand Down Expand Up @@ -96,16 +101,25 @@ def read_regexes():

def command_line_file(args):
validate(args)
aggregate = []

if args.path.endswith('.py'):
lines = read_python_file(args.path, args.python_var, inplace=False)
else:
lines = read_file(args.path, inplace=False)
for path in args.path:
if path.endswith('.py'):
lines = read_python_file(path, args.python_var, inplace=False)
else:
lines = read_file(path, inplace=False)

if args.nothing:
print(f'{path} would be reformatted to:\n', lines)
aggregate.append(lines)
else:
print(f'{path} has been reformatted to:\n', lines)

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

with open(args.path, 'w') as out:
out.write(lines)
if args.nothing:
return aggregate


def command_line():
Expand Down
17 changes: 10 additions & 7 deletions readsql/parse_args.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import argparse
import os
import sys

import argparse


def parse_args():
parser = argparse.ArgumentParser(
prog='readsql', description='Convert SQL to most human readable format'
)
parser.add_argument('path', type=str, help='Path to the file to be converted')
parser.add_argument('path', nargs='+', help='Path to the file to be converted')
parser.add_argument('-s', '--string', action='store_true', help='Read a string')
parser.add_argument(
'-n', '--nothing', action='store_true', help='Do nothing to the file'
)
parser.add_argument(
'-py',
'--python_var',
Expand All @@ -24,7 +26,8 @@ def parse_args():


def validate(args):
path = args.path
if not os.path.isfile(path):
print('The file path specified does not exist')
sys.exit()
paths = args.path
for path in paths:
if not os.path.isfile(path):
print('The file path specified does not exist')
sys.exit()
20 changes: 11 additions & 9 deletions tests/speed.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
func:test_read_python_file__var:(((), {'variable': ['sql']}))__took: 0.001723 sec
func:test_read_python_file__var:(((), {'variable': ['sql']}))__took: 0.001756 sec

func:test_read_file__var:(((), {}))__took: 0.005423 sec
func:test_read_file__var:(((), {}))__took: 0.004506 sec

func:test_read_python_file__var:(((), {}))__took: 0.003344 sec
func:test_read_python_file__var:(((), {}))__took: 0.001280 sec

func:test_double_select__var:(((), {}))__took: 0.000642 sec
func:test_double_select__var:(((), {}))__took: 0.000313 sec

func:test_select_from_groub_by_where__var:(((), {}))__took: 0.000595 sec
func:test_select_from_groub_by_where__var:(((), {}))__took: 0.000471 sec

func:test_is_not_null__var:(((), {}))__took: 0.000583 sec
func:test_is_not_null__var:(((), {}))__took: 0.000264 sec

func:test_distinct__var:(((), {}))__took: 0.001059 sec
func:test_distinct__var:(((), {}))__took: 0.000390 sec

func:test_create_table_if_not_exists__var:(((), {}))__took: 0.000418 sec
func:test_create_table_if_not_exists__var:(((), {}))__took: 0.000404 sec

func:test_read_python_file__var:(((), {'variable': ['sql', 'query_template', 'query']}))__took: 0.001387 sec
func:test_read_python_file__var:(((), {'variable': ['sql', 'query_template', 'query']}))__took: 0.001234 sec

func:test_read_python_multifile__var:(((), {}))__took: 0.002904 sec
27 changes: 27 additions & 0 deletions tests/sql_in_python_multifile1_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
query = """
select * from languages;
"""

query = """
select *
from games
where test=0
"""


def get_query1():
query = (
f"SELEct max(weight) from world where ocean='Atlantic' and water in not null"
)
return query


def get_query2():
limit = 6
query = f"SELEct speed from world where animal='dolphin' limit {limit}"
return query


def get_query3():
query = 'select 5'
return query
27 changes: 27 additions & 0 deletions tests/sql_in_python_multifile1_example_correct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
query = """
SELECT * FROM languages;
"""

query = """
SELECT *
FROM games
WHERE test=0
"""


def get_query1():
query = (
f"SELECT MAX(weight) FROM world WHERE ocean='Atlantic' and water in NOT null"
)
return query


def get_query2():
limit = 6
query = f"SELECT speed FROM world WHERE animal='dolphin' LIMIT {limit}"
return query


def get_query3():
query = 'SELECT 5'
return query
27 changes: 27 additions & 0 deletions tests/sql_in_python_multifile2_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
query = """
select * from languages;
"""

query = """
select *
from games
where test=0
"""


def get_query1():
query = (
f"SELEct max(weight) from world where ocean='Atlantic' and water in not null"
)
return query


def get_query2():
limit = 6
query = f"SELEct speed from world where animal='dolphin' limit {limit}"
return query


def get_query3():
query = 'select 5'
return query
27 changes: 27 additions & 0 deletions tests/sql_in_python_multifile2_example_correct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
query = """
SELECT * FROM languages;
"""

query = """
SELECT *
FROM games
WHERE test=0
"""


def get_query1():
query = (
f"SELECT MAX(weight) FROM world WHERE ocean='Atlantic' and water in NOT null"
)
return query


def get_query2():
limit = 6
query = f"SELECT speed FROM world WHERE animal='dolphin' LIMIT {limit}"
return query


def get_query3():
query = 'SELECT 5'
return query
44 changes: 41 additions & 3 deletions tests/test_files.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import argparse
import os

import readsql.__main__ as rsql
Expand All @@ -19,7 +20,9 @@ def test_read_file():
def test_read_python_file_wrap():
@timing
def test_read_python_file():
return rsql.read_python_file(file_name=DIR + '/sql_in_python_example.py', inplace=False)
return rsql.read_python_file(
file_name=DIR + '/sql_in_python_example.py', inplace=False
)

example = test_read_python_file()
with open(DIR + '/sql_in_python_example_correct.py', 'r') as inp:
Expand All @@ -29,7 +32,11 @@ def test_read_python_file():
def test_read_python_file_variable_wrap():
@timing
def test_read_python_file(variable):
return rsql.read_python_file(file_name=DIR + '/sql_in_python_variable_example.py', variables=variable, inplace=False)
return rsql.read_python_file(
file_name=DIR + '/sql_in_python_variable_example.py',
variables=variable,
inplace=False,
)

example = test_read_python_file(variable=['sql'])
with open(DIR + '/sql_in_python_variable_example_correct.py', 'r') as inp:
Expand All @@ -39,8 +46,39 @@ def test_read_python_file(variable):
def test_read_python_file_variables_wrap():
@timing
def test_read_python_file(variable):
return rsql.read_python_file(file_name=DIR + '/sql_in_python_variables_example.py', variables=variable, inplace=False)
return rsql.read_python_file(
file_name=DIR + '/sql_in_python_variables_example.py',
variables=variable,
inplace=False,
)

example = test_read_python_file(variable=['sql', 'query_template', 'query'])
with open(DIR + '/sql_in_python_variables_example_correct.py', 'r') as inp:
assert inp.read() == example


def test_read_python_multifile_wrap():
args = argparse.Namespace(
nothing=True,
path=[
DIR + '/sql_in_python_multifile1_example.py',
DIR + '/sql_in_python_multifile2_example.py',
],
python_var=['query'],
string=False,
)

@timing
def test_read_python_multifile():
return rsql.command_line_file(args)

example = test_read_python_multifile()
corrects = [
DIR + '/sql_in_python_multifile1_example_correct.py',
DIR + '/sql_in_python_multifile2_example_correct.py',
]
aggregate = []
for file in corrects:
with open(file, 'r') as inp:
aggregate.append(inp.read())
assert str(aggregate) == str(example)

0 comments on commit b630bdf

Please sign in to comment.