mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-29 05:14:28 +02:00
Support nested imports, closes #351
This commit is contained in:
parent
a3df483c80
commit
7a43af232e
3 changed files with 28 additions and 1 deletions
|
|
@ -1023,6 +1023,12 @@ You can specify Python modules that should be imported and made available to you
|
||||||
'"\n".join(textwrap.wrap(value, 100))' \
|
'"\n".join(textwrap.wrap(value, 100))' \
|
||||||
--import=textwrap
|
--import=textwrap
|
||||||
|
|
||||||
|
This supports nested imports as well, for example to use `ElementTree <https://docs.python.org/3/library/xml.etree.elementtree.html>`__::
|
||||||
|
|
||||||
|
$ sqlite-utils convert content.db articles content \
|
||||||
|
'xml.etree.ElementTree.fromstring(value).attrib["title"]' \
|
||||||
|
--import=xml.etree.ElementTree
|
||||||
|
|
||||||
Use a CODE value of `-` to read from standard input:
|
Use a CODE value of `-` to read from standard input:
|
||||||
|
|
||||||
$ cat mycode.py | sqlite-utils convert content.db articles headline -
|
$ cat mycode.py | sqlite-utils convert content.db articles headline -
|
||||||
|
|
|
||||||
|
|
@ -2137,7 +2137,7 @@ def convert(
|
||||||
locals = {}
|
locals = {}
|
||||||
globals = {"r": recipes, "recipes": recipes}
|
globals = {"r": recipes, "recipes": recipes}
|
||||||
for import_ in imports:
|
for import_ in imports:
|
||||||
globals[import_] = __import__(import_)
|
globals[import_.split(".")[0]] = __import__(import_)
|
||||||
exec(code_o, globals, locals)
|
exec(code_o, globals, locals)
|
||||||
fn = locals["fn"]
|
fn = locals["fn"]
|
||||||
if dry_run:
|
if dry_run:
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,27 @@ def test_convert_import(test_db_and_path):
|
||||||
] == list(db["example"].rows)
|
] == list(db["example"].rows)
|
||||||
|
|
||||||
|
|
||||||
|
def test_convert_import_nested(fresh_db_and_path):
|
||||||
|
db, db_path = fresh_db_and_path
|
||||||
|
db["example"].insert({"xml": '<item name="Cleo" />'})
|
||||||
|
result = CliRunner().invoke(
|
||||||
|
cli.cli,
|
||||||
|
[
|
||||||
|
"convert",
|
||||||
|
db_path,
|
||||||
|
"example",
|
||||||
|
"xml",
|
||||||
|
'xml.etree.ElementTree.fromstring(value).attrib["name"]',
|
||||||
|
"--import",
|
||||||
|
"xml.etree.ElementTree",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
assert 0 == result.exit_code, result.output
|
||||||
|
assert [
|
||||||
|
{"xml": "Cleo"},
|
||||||
|
] == list(db["example"].rows)
|
||||||
|
|
||||||
|
|
||||||
def test_convert_dryrun(test_db_and_path):
|
def test_convert_dryrun(test_db_and_path):
|
||||||
db, db_path = test_db_and_path
|
db, db_path = test_db_and_path
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue