-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Searches and creates a stub of a cstruct definitions | ||
from argparse import ArgumentParser | ||
from importlib import import_module | ||
from pathlib import Path | ||
|
||
|
||
def stubify_file(path: Path): | ||
... | ||
|
||
|
||
def main(): | ||
parser = ArgumentParser("stubify") | ||
parser.add_argument("path", type=Path, required=True) | ||
args = parser.parse_args() | ||
|
||
file_path: Path = args.path | ||
|
||
for file in file_path.glob("*.py"): | ||
if file.is_file() and ".py" in file.suffixes: | ||
stubify_file(file) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import textwrap | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from dissect.cstruct import cstruct | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"definition, name, expected_stub", | ||
[ | ||
( | ||
""" | ||
struct Test { | ||
int a; | ||
int b; | ||
} | ||
""", | ||
"Test", | ||
""" | ||
class Test: | ||
a: int | ||
b: int | ||
def __call__(self, a: int=..., b: int=...): ... | ||
""", | ||
), | ||
( | ||
""" | ||
struct Test { | ||
int a[]; | ||
} | ||
""", | ||
"Test", | ||
""" | ||
class Test: | ||
a: Array | ||
def __call__(self, a: Array=...): ... | ||
""", | ||
), | ||
( | ||
""" | ||
#define a 1 | ||
#define b b"data" | ||
#define c "test" | ||
""", | ||
None, | ||
""" | ||
a: int=... | ||
b: bytes=... | ||
c: str=... | ||
""", | ||
), | ||
( | ||
""" | ||
struct Test{ | ||
union { | ||
int a; | ||
int b; | ||
} | ||
} | ||
""", | ||
"Test", | ||
"""""", | ||
), | ||
], | ||
ids=["standard structure", "array", "definitions", "unions"], | ||
) | ||
def test_to_stub(definition: str, name: str, expected_stub: str): | ||
structure = cstruct() | ||
structure.load(definition) | ||
|
||
if name: | ||
generated_stub = getattr(structure, name).to_stub() | ||
else: | ||
generated_stub = structure.to_stub() | ||
expected_stub = textwrap.dedent(expected_stub).strip() | ||
|
||
assert expected_stub in generated_stub |