Skip to content

Commit fd17833

Browse files
committed
feat(py): implement the imports() method for the Rules object.
The `imports()` method returns a list of all the modules imported by a set of rules.
1 parent 9a8531f commit fd17833

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

py/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,11 @@ impl Rules {
940940

941941
Py::new(py, rules_iter)
942942
}
943+
944+
/// Returns a list of modules imported by the rules.
945+
fn imports(&self) -> PyResult<Vec<&str>> {
946+
Ok(self.inner.rules.imports().collect())
947+
}
943948
}
944949

945950
fn scan_results_to_py(

py/tests/test_api.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,15 @@ def test_rules_iterator():
375375
assert len(rules_list) == 2
376376
assert rules_list[0].identifier == 'foo'
377377
assert rules_list[1].identifier == 'bar'
378+
379+
380+
def test_rules_imports():
381+
rules = yara_x.compile('''
382+
import "pe"
383+
import "elf"
384+
rule test {
385+
condition:
386+
true
387+
}
388+
''')
389+
assert rules.imports() == ["pe", "elf"]

py/yara_x.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ class Rules:
383383
"""
384384
...
385385

386+
def imports(self) -> list[str]:
387+
r"""
388+
Returns a list of modules imported by the rules.
389+
"""
390+
...
391+
386392
def serialize_into(self, file: BinaryIO) -> None:
387393
r"""
388394
Serializes the rules into a file-like object.

0 commit comments

Comments
 (0)