Skip to content

Commit 3eb2b06

Browse files
committed
Handle export.xml in other languages, closes #11
1 parent eb114d4 commit 3eb2b06

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

healthkit_to_sqlite/cli.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import sqlite_utils
55
from .utils import convert_xml_to_sqlite
66

7-
EXPORT_XML = "apple_health_export/export.xml"
8-
97

108
@click.command()
119
@click.argument(
@@ -33,14 +31,22 @@ def cli(export_zip, db_path, silent, xml):
3331
raise click.ClickException(
3432
"File is not a zip file. Use --xml if you are running against an XML file."
3533
)
36-
# Ensure export.xml is in there
34+
# Ensure something.xml with <!DOCTYPE HealthData or <HealthData is there
3735
filenames = {zi.filename for zi in zf.filelist}
38-
if EXPORT_XML not in filenames:
39-
raise click.ClickException(
40-
"Zip file does not contain {}".format(EXPORT_XML)
41-
)
42-
fp = zf.open(EXPORT_XML)
43-
file_length = zf.getinfo("apple_health_export/export.xml").file_size
36+
export_xml_path = None
37+
for filename in filenames:
38+
if filename.count("/") == 1 and filename.endswith(".xml"):
39+
firstbytes = zf.open(filename).read(1024)
40+
if (
41+
b"<!DOCTYPE HealthData" in firstbytes
42+
or b"<HealthData " in firstbytes
43+
):
44+
export_xml_path = filename
45+
break
46+
if export_xml_path is None:
47+
raise click.ClickException("Zip file does not contain valid export.xml")
48+
fp = zf.open(export_xml_path)
49+
file_length = zf.getinfo(export_xml_path).file_size
4450
db = sqlite_utils.Database(db_path)
4551
if silent:
4652
convert_xml_to_sqlite(fp, db, zipfile=zf)

tests/test_healthkit_to_sqlite.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@ def converted(xml_fp):
2626
return db
2727

2828

29-
@pytest.fixture
30-
def zip_file_with_gpx(tmpdir):
29+
@pytest.fixture(params=["export.xml", "exportar.xml"])
30+
def zip_file_with_gpx(request, tmpdir):
31+
export_xml_filename = request.param
3132
zip_contents_path = pathlib.Path(__file__).parent / "zip_contents"
3233
archive = str(tmpdir / "export.zip")
3334
buf = io.BytesIO()
3435
zf = zipfile.ZipFile(buf, "w")
3536
for filepath in zip_contents_path.glob("**/*"):
3637
if filepath.is_file():
37-
zf.write(filepath, str(filepath.relative_to(zip_contents_path)))
38+
arcname = filepath.relative_to(zip_contents_path)
39+
if arcname.name == "export.xml":
40+
arcname = arcname.parent / export_xml_filename
41+
zf.write(filepath, str(arcname))
3842
zf.close()
3943
with open(archive, "wb") as fp:
4044
fp.write(buf.getbuffer())

0 commit comments

Comments
 (0)