metamath2sqlite 1.0.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ Copyright © 2026 Ramiro Santa Ana Anguiano
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the “Software”), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,120 @@
1
+ Metadata-Version: 2.4
2
+ Name: metamath2sqlite
3
+ Version: 1.0.4
4
+ Summary: Convert Metamath databases to SQLite
5
+ Keywords: metamath,sqlite,converter,database
6
+ Author: ram
7
+ Author-email: ram <hi@colima.press>
8
+ License-Expression: MIT
9
+ License-File: LICENSE.md
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Environment :: Console
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Topic :: Database
17
+ Classifier: Topic :: File Formats
18
+ Classifier: Topic :: Scientific/Engineering
19
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
20
+ Classifier: Topic :: Utilities
21
+ Requires-Dist: pypdl>=1.5.7
22
+ Requires-Dist: tree-sitter>=0.25.2
23
+ Requires-Dist: tree-sitter-metamath>=0.1.0
24
+ Maintainer: ram
25
+ Maintainer-email: ram <hi@colima.press>
26
+ Requires-Python: >=3.12
27
+ Project-URL: Repository, https://gitlab.com/perritotuerto/codigo/metamath/metamath2sqlite
28
+ Project-URL: Changelog, https://gitlab.com/perritotuerto/codigo/metamath/metamath2sqlite/-/blob/main/CHANGELOG.md
29
+ Project-URL: Releases, https://gitlab.com/perritotuerto/codigo/metamath/metamath2sqlite/-/releases
30
+ Description-Content-Type: text/markdown
31
+
32
+ # mm2sqlite
33
+
34
+ Converts [Metamath](https://us.metamath.org/mm.html)
35
+ databases to SQLite.
36
+
37
+ **NOTE**. It only converts statements.
38
+
39
+ ## Usage
40
+
41
+ Install CLI:
42
+
43
+ ```
44
+ uv tool install metamath2sqlite
45
+ ```
46
+
47
+ Convert wit CLI:
48
+
49
+ ```
50
+ mm2sqlite hol # or nf, iset, set...
51
+ ```
52
+
53
+ Print help:
54
+
55
+ ```
56
+ mm2sqlite -h
57
+ ```
58
+
59
+ Install as a library:
60
+
61
+ ```
62
+ uv add metamath2sqlite
63
+ ```
64
+
65
+ Import as a module:
66
+
67
+ ```
68
+ from mm2sqlite import MM
69
+
70
+ mm = MM("hol")
71
+ mm.convert()
72
+ ```
73
+
74
+ Fetch item:
75
+
76
+ ```
77
+ import sqlite3
78
+
79
+ connection = sqlite3.connect("hol.db")
80
+ cursor = connection.cursor()
81
+ sql_query = "SELECT * FROM statements WHERE label IN(?)"
82
+ statement = "ax-syl"
83
+ result = cursor.execute(sql_query, (statement,))
84
+ item = result.fetchone()
85
+ print(item)
86
+ ```
87
+
88
+ ## Development
89
+
90
+ Clone:
91
+
92
+ ```
93
+ git clone https://gitlab.com/perritotuerto/codigo/metamath/metamath2sqlite.git
94
+ ```
95
+
96
+ Format:
97
+
98
+ ```
99
+ uv run ruff format .
100
+ uv run isort .
101
+ ```
102
+
103
+ Check:
104
+
105
+ ```
106
+ uv run ruff check .
107
+ ```
108
+
109
+ Test:
110
+
111
+ ```
112
+ uv run python -m pytest -s --durations 0
113
+ ```
114
+
115
+ Tag version:
116
+
117
+ ```
118
+ git tag v1.0.0
119
+ git push origin tag v1.0.0
120
+ ```
@@ -0,0 +1,89 @@
1
+ # mm2sqlite
2
+
3
+ Converts [Metamath](https://us.metamath.org/mm.html)
4
+ databases to SQLite.
5
+
6
+ **NOTE**. It only converts statements.
7
+
8
+ ## Usage
9
+
10
+ Install CLI:
11
+
12
+ ```
13
+ uv tool install metamath2sqlite
14
+ ```
15
+
16
+ Convert wit CLI:
17
+
18
+ ```
19
+ mm2sqlite hol # or nf, iset, set...
20
+ ```
21
+
22
+ Print help:
23
+
24
+ ```
25
+ mm2sqlite -h
26
+ ```
27
+
28
+ Install as a library:
29
+
30
+ ```
31
+ uv add metamath2sqlite
32
+ ```
33
+
34
+ Import as a module:
35
+
36
+ ```
37
+ from mm2sqlite import MM
38
+
39
+ mm = MM("hol")
40
+ mm.convert()
41
+ ```
42
+
43
+ Fetch item:
44
+
45
+ ```
46
+ import sqlite3
47
+
48
+ connection = sqlite3.connect("hol.db")
49
+ cursor = connection.cursor()
50
+ sql_query = "SELECT * FROM statements WHERE label IN(?)"
51
+ statement = "ax-syl"
52
+ result = cursor.execute(sql_query, (statement,))
53
+ item = result.fetchone()
54
+ print(item)
55
+ ```
56
+
57
+ ## Development
58
+
59
+ Clone:
60
+
61
+ ```
62
+ git clone https://gitlab.com/perritotuerto/codigo/metamath/metamath2sqlite.git
63
+ ```
64
+
65
+ Format:
66
+
67
+ ```
68
+ uv run ruff format .
69
+ uv run isort .
70
+ ```
71
+
72
+ Check:
73
+
74
+ ```
75
+ uv run ruff check .
76
+ ```
77
+
78
+ Test:
79
+
80
+ ```
81
+ uv run python -m pytest -s --durations 0
82
+ ```
83
+
84
+ Tag version:
85
+
86
+ ```
87
+ git tag v1.0.0
88
+ git push origin tag v1.0.0
89
+ ```
@@ -0,0 +1,60 @@
1
+ [project]
2
+ name = "metamath2sqlite"
3
+ version = "1.0.4"
4
+ description = "Convert Metamath databases to SQLite"
5
+ readme = "README.md"
6
+ keywords = [
7
+ "metamath",
8
+ "sqlite",
9
+ "converter",
10
+ "database",
11
+ ]
12
+ classifiers = [
13
+ "Development Status :: 3 - Alpha",
14
+ "Environment :: Console",
15
+ "Intended Audience :: Developers",
16
+ "Intended Audience :: Science/Research",
17
+ "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3.12",
19
+ "Topic :: Database",
20
+ "Topic :: File Formats",
21
+ "Topic :: Scientific/Engineering",
22
+ "Topic :: Scientific/Engineering :: Mathematics",
23
+ "Topic :: Utilities",
24
+ ]
25
+ license = "MIT"
26
+ license-files = ["LICENSE.md"]
27
+ requires-python = ">=3.12"
28
+ dependencies = [
29
+ "pypdl>=1.5.7",
30
+ "tree-sitter>=0.25.2",
31
+ "tree-sitter-metamath>=0.1.0",
32
+ ]
33
+
34
+ [[project.authors]]
35
+ name = "ram"
36
+ email = "hi@colima.press"
37
+
38
+ [[project.maintainers]]
39
+ name = "ram"
40
+ email = "hi@colima.press"
41
+
42
+ [project.scripts]
43
+ mm2sqlite = "metamath2sqlite.cli:main"
44
+
45
+ [project.urls]
46
+ Repository = "https://gitlab.com/perritotuerto/codigo/metamath/metamath2sqlite"
47
+ Changelog = "https://gitlab.com/perritotuerto/codigo/metamath/metamath2sqlite/-/blob/main/CHANGELOG.md"
48
+ Releases = "https://gitlab.com/perritotuerto/codigo/metamath/metamath2sqlite/-/releases"
49
+
50
+ [dependency-groups]
51
+ dev = [
52
+ "isort>=8.0.1",
53
+ "pytest>=9.0.3",
54
+ "pytest-benchmark>=5.2.3",
55
+ "ruff>=0.15.11",
56
+ ]
57
+
58
+ [build-system]
59
+ requires = ["uv_build>=0.11.8,<0.13"]
60
+ build-backend = "uv_build"
@@ -0,0 +1,49 @@
1
+ [project]
2
+ name = "metamath2sqlite"
3
+ version = "1.0.4"
4
+ description = "Convert Metamath databases to SQLite"
5
+ readme = "README.md"
6
+ authors = [{ name = "ram", email = "hi@colima.press" }]
7
+ maintainers = [{ name = "ram", email = "hi@colima.press" }]
8
+ keywords = ["metamath", "sqlite", "converter", "database"]
9
+ classifiers = [
10
+ "Development Status :: 3 - Alpha",
11
+ "Environment :: Console",
12
+ "Intended Audience :: Developers",
13
+ "Intended Audience :: Science/Research",
14
+ "Programming Language :: Python :: 3",
15
+ "Programming Language :: Python :: 3.12",
16
+ "Topic :: Database",
17
+ "Topic :: File Formats",
18
+ "Topic :: Scientific/Engineering",
19
+ "Topic :: Scientific/Engineering :: Mathematics",
20
+ "Topic :: Utilities",
21
+ ]
22
+ license = "MIT"
23
+ license-files = ["LICENSE.md"]
24
+ requires-python = ">=3.12"
25
+ dependencies = [
26
+ "pypdl>=1.5.7",
27
+ "tree-sitter>=0.25.2",
28
+ "tree-sitter-metamath>=0.1.0",
29
+ ]
30
+
31
+ [dependency-groups]
32
+ dev = [
33
+ "isort>=8.0.1",
34
+ "pytest>=9.0.3",
35
+ "pytest-benchmark>=5.2.3",
36
+ "ruff>=0.15.11",
37
+ ]
38
+
39
+ [project.scripts]
40
+ mm2sqlite = "metamath2sqlite.cli:main"
41
+
42
+ [project.urls]
43
+ Repository = "https://gitlab.com/perritotuerto/codigo/metamath/metamath2sqlite"
44
+ Changelog = "https://gitlab.com/perritotuerto/codigo/metamath/metamath2sqlite/-/blob/main/CHANGELOG.md"
45
+ Releases = "https://gitlab.com/perritotuerto/codigo/metamath/metamath2sqlite/-/releases"
46
+
47
+ [build-system]
48
+ requires = ["uv_build>=0.11.8,<0.13"]
49
+ build-backend = "uv_build"
@@ -0,0 +1,2 @@
1
+ def hello() -> str:
2
+ return "Hello from metamath2sqlite!"
@@ -0,0 +1,26 @@
1
+ import argparse
2
+
3
+ from metamath2sqlite.core import MM
4
+
5
+
6
+ def main():
7
+ parser = argparse.ArgumentParser(
8
+ prog="mm2sqlite",
9
+ description="Convert metamath databases to sqlite",
10
+ epilog="Contact: hi@colima.press",
11
+ )
12
+ parser.add_argument(
13
+ "-o", "--output", help="output directory", metavar="path/to/dir/"
14
+ )
15
+ parser.add_argument("mmdbs", help="metamath DB files or names", nargs="+")
16
+ args = parser.parse_args()
17
+ for mmdb in args.mmdbs:
18
+ mm = MM(
19
+ mmpath=mmdb,
20
+ opath=args.output,
21
+ )
22
+ mm.convert()
23
+
24
+
25
+ if __name__ == "__main__":
26
+ main()
@@ -0,0 +1,112 @@
1
+ import re
2
+ import sqlite3
3
+ from pathlib import Path
4
+ from typing import ClassVar, Generator
5
+
6
+ import tree_sitter_metamath as tsmetamath
7
+ from pypdl import Pypdl
8
+ from tree_sitter import Language, Node, Parser, Tree
9
+
10
+
11
+ class MM:
12
+ MM_LANGUAGE: ClassVar[Language] = Language(tsmetamath.language())
13
+ MM_BASE_URL: ClassVar[str] = "https://us.metamath.org/metamath/"
14
+
15
+ def __init__(
16
+ self,
17
+ mmpath: str,
18
+ opath: str | None = None,
19
+ ):
20
+ mmpath = self._get_mm_path(mmpath)
21
+ self.name = mmpath.stem
22
+ self.opath = self._get_output_path(opath)
23
+ self.parser = Parser(self.MM_LANGUAGE)
24
+ self.bytes = mmpath.read_bytes()
25
+ self.connection, self.cursor = self._connect()
26
+
27
+ def convert(self) -> None:
28
+ flags = re.DOTALL | re.MULTILINE
29
+ comment_rgx = re.compile(rb"\$\(.+?\$\)", flags)
30
+ assert_stmt_rgx = re.compile(rb"[\w\.-]+\s+\$[ap]\s.+?\$\.", flags)
31
+ bytes = re.sub(
32
+ comment_rgx, b"", self.bytes
33
+ ) # Avoid satements in comments
34
+ for stmt in re.findall(assert_stmt_rgx, bytes):
35
+ tree = self.parser.parse(stmt)
36
+ label, math, proof = "", "", ""
37
+ for node in self.traverse(tree):
38
+ if node.type == "ERROR":
39
+ print(f"ERROR: Failed parsing: {stmt}")
40
+ break
41
+ elif (
42
+ not label and node.type == "label"
43
+ ): # First label == stmt label
44
+ label = node.text.decode()
45
+ elif (
46
+ node.type == "label"
47
+ ): # Other labels == labels used in proof
48
+ proof = f"{proof} {node.text.decode()}"
49
+ elif node.type == "math_symbol":
50
+ math = f"{math} {node.text.decode()}"
51
+ parameters = (
52
+ label.strip(),
53
+ math.strip(),
54
+ proof.strip(),
55
+ )
56
+ self.cursor.execute(
57
+ """INSERT OR IGNORE INTO statements
58
+ VALUES(?, ?, ?)""",
59
+ parameters,
60
+ )
61
+ self.connection.commit()
62
+
63
+ # Cfr. https://github.com/tree-sitter/py-tree-sitter/
64
+ # blob/master/examples/walk_tree.py
65
+ def traverse(self, tree: Tree) -> Generator[Node, None, None]:
66
+ cursor = tree.walk()
67
+ visited_children = False
68
+ while True:
69
+ if not visited_children:
70
+ yield cursor.node
71
+ if not cursor.goto_first_child():
72
+ visited_children = True
73
+ elif cursor.goto_next_sibling():
74
+ visited_children = False
75
+ elif not cursor.goto_parent():
76
+ break
77
+
78
+ def _get_output_path(self, rawpath: str | None = None) -> Path:
79
+ ext = ".db"
80
+ default = self.name + ext
81
+ path = Path(default if rawpath is None else rawpath)
82
+ if not path.suffix == ext:
83
+ raise ValueError(f"'{path}' extension is not '{ext}'")
84
+ return path
85
+
86
+ def _get_mm_path(self, rawpath: str) -> Path:
87
+ path = Path(f"{rawpath}.mm")
88
+ if not path.exists():
89
+ path = self._download(path)
90
+ if not path.is_file():
91
+ raise OSError(f"'{path}' is not a file")
92
+ return path
93
+
94
+ def _download(self, db_path: Path) -> Path:
95
+ basename = f"{db_path.stem}.mm"
96
+ url = f"{self.MM_BASE_URL}{basename}"
97
+ download = Pypdl().start(url, retries=3)
98
+ if not download:
99
+ raise FileNotFoundError(f"'{basename}' does not exist")
100
+ _, result = download[0]
101
+ return Path(result.path)
102
+
103
+ def _connect(self, db_path: Path | str | None = None) -> tuple:
104
+ path = db_path if db_path else self.opath
105
+ con = sqlite3.connect(path)
106
+ cur = con.cursor()
107
+ cur.execute("""CREATE TABLE IF NOT EXISTS statements(
108
+ label TEXT NOT NULL UNIQUE,
109
+ math TEXT NOT NULL,
110
+ proof TEXT
111
+ )""")
112
+ return (con, cur)
File without changes