Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
my2to3
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Xavier Thompson
my2to3
Commits
c1ec610f
Commit
c1ec610f
authored
Jul 15, 2020
by
Bryton Lacquement
🚪
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor the sqlite3-related code
parent
186b2cb6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
41 deletions
+22
-41
my2to3/tests/__init__.py
my2to3/tests/__init__.py
+4
-11
my2to3/trace.py
my2to3/trace.py
+18
-30
No files found.
my2to3/tests/__init__.py
View file @
c1ec610f
from
lib2to3.tests.test_fixers
import
FixerTestCase
as
lib2to3FixerTestCase
import
sqlite3
from
my2to3.trace
import
database
,
get_data
,
tracing_functions
from
my2to3.trace
import
conn
,
get_data
,
tracing_functions
class
FixerTestCase
(
lib2to3FixerTestCase
):
...
...
@@ -10,16 +10,9 @@ class FixerTestCase(lib2to3FixerTestCase):
super
(
FixerTestCase
,
self
).
setUp
(
fix_list
,
fixer_pkg
,
options
)
# Clear the database
# XXX: a better way probably exists.
# TODO:
# - refactor
# - optimize
conn
=
sqlite3
.
connect
(
database
)
c
=
conn
.
cursor
()
for
table
in
c
.
execute
(
"SELECT name FROM sqlite_master WHERE type='table'"
).
fetchall
():
c
.
execute
(
"DELETE FROM %s"
%
table
)
conn
.
commit
()
conn
.
close
()
with
conn
:
for
table
in
conn
.
execute
(
"SELECT name FROM sqlite_master WHERE type='table'"
).
fetchall
():
conn
.
execute
(
"DELETE FROM %s"
%
table
)
def
assertDataEqual
(
self
,
table
,
data
):
self
.
assertEqual
(
get_data
(
table
),
data
)
...
...
my2to3/trace.py
View file @
c1ec610f
...
...
@@ -4,6 +4,7 @@ from lib2to3.pgen2 import tokenize
from
lib2to3.refactor
import
get_fixers_from_package
,
RefactoringTool
database
=
"traces.db"
conn
=
sqlite3
.
connect
(
database
)
tracing_functions
=
[]
...
...
@@ -13,28 +14,22 @@ def register_tracing_function(f):
def
create_table
(
table
,
*
columns
):
# TODO:
# - refactor
# - optimize
conn
=
sqlite3
.
connect
(
database
)
c
=
conn
.
cursor
()
v
=
', '
.
join
(
columns
)
c
.
execute
(
"CREATE TABLE IF NOT EXISTS %s (%s, UNIQUE (%s))"
%
(
table
,
v
,
v
))
conn
.
commit
()
conn
.
close
()
with
conn
:
v
=
', '
.
join
(
columns
)
conn
.
execute
(
"CREATE TABLE IF NOT EXISTS %s (%s, UNIQUE (%s))"
%
(
table
,
v
,
v
)
)
def
insert_unique
(
*
values
):
conn
=
sqlite3
.
connect
(
database
)
c
=
conn
.
cursor
()
try
:
c
.
execute
(
'INSERT INTO %s VALUES (%s)'
%
(
table
,
', '
.
join
([
'?'
]
*
len
(
values
))),
values
)
except
sqlite3
.
IntegrityError
as
e
:
if
not
str
(
e
).
startswith
(
'UNIQUE constraint failed:'
):
raise
else
:
conn
.
commit
()
conn
.
close
()
with
conn
:
try
:
conn
.
execute
(
'INSERT INTO %s VALUES (%s)'
%
(
table
,
', '
.
join
(
'?'
*
len
(
values
))),
values
)
except
sqlite3
.
IntegrityError
as
e
:
if
not
str
(
e
).
startswith
(
'UNIQUE constraint failed:'
):
raise
return
insert_unique
...
...
@@ -45,21 +40,14 @@ def get_fixers():
def
get_data
(
table
,
columns_to_select
=
'*'
,
conditions
=
{}):
# TODO:
# - refactor
# - optimize
conn
=
sqlite3
.
connect
(
database
)
c
=
conn
.
cursor
()
query
=
"SELECT %s FROM %s"
%
(
', '
.
join
(
columns_to_select
),
table
,
)
if
conditions
:
query
+=
" WHERE "
+
' AND '
.
join
(
k
+
" = "
+
v
for
k
,
v
in
conditions
.
items
())
try
:
return
c
.
execute
(
query
).
fetchall
()
finally
:
conn
.
close
()
query
+=
" WHERE "
+
' AND '
.
join
(
k
+
" = :"
+
k
for
k
in
conditions
.
keys
())
with
conn
:
return
conn
.
execute
(
query
,
conditions
).
fetchall
()
def
apply_fixers
(
string
,
name
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment