Skip to content

Commit 65b0589

Browse files
authored
Merge pull request #34 from ocsf/export-categories
Wiring category events into compilation CLI options and making event …
2 parents b1683eb + 385c2e7 commit 65b0589

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

src/ocsf/compile/__main__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
--set-observable Set the observable field on attributes to the corresponding Observable Type ID where applicable
3333
--no-set-observable Do not set the observable field on attributes to the corresponding Observable Type ID
3434
where applicable
35+
--category-classes Include classes in the category section of the schema output. This duplicates classes in each
36+
category without attributes in the same way as the OCSF Server's category export.
37+
--no-category-classes
38+
Do not include classes in the category section of the schema output. This is the default
39+
behavior.
3540
```
3641
3742
Examples:
@@ -125,6 +130,22 @@ def main():
125130
action="store_false",
126131
help="Do not set the observable field on attributes to the corresponding Observable Type ID where applicable",
127132
)
133+
parser.add_argument(
134+
"--category-classes",
135+
action="store_true",
136+
default=True,
137+
help=(
138+
"Include classes in the category section of the schema output. This "
139+
"duplicates classes in each category without attributes in the same "
140+
"way as the OCSF Server's category export."
141+
),
142+
)
143+
parser.add_argument(
144+
"--no-category-classes",
145+
dest="category_classes",
146+
action="store_false",
147+
help=("Do not include classes in the category section of the schema output. " "This is the default behavior."),
148+
)
128149

129150
args = parser.parse_args()
130151

@@ -142,6 +163,7 @@ def main():
142163
options.prefix_extensions = args.prefix_extensions
143164
options.set_object_types = args.set_object_types
144165
options.set_observable = args.set_observable
166+
options.map_events_to_categories = args.category_classes
145167

146168
repo = read_repo(args.path, preserve_raw_data=False)
147169

src/ocsf/compile/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def __init__(self, repo: Repository, options: CompilationOptions | None = None):
111111
# attribute of records easier. Only performed if
112112
# options.set_observable is True.
113113
MarkObservablesPlanner(self._proto, _options),
114-
# Map events to categories in the categories.json file.
114+
# Map events to the `classes` dictionary of each category.
115115
MapEventToCategoryPlanner(self._proto, _options),
116116
# Copy records that are ONLY defined in extensions to the core
117117
# schema so that they are included by ProtoSchema.schema().

src/ocsf/compile/planners/set_category.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ def apply(self, schema: ProtoSchema) -> MergeResult:
2121
return []
2222

2323
path = PurePath(target.path).parts
24+
if path[-1] == SpecialFiles.BASE_EVENT.value:
25+
return []
26+
2427
if RepoPaths.EVENTS.value not in path:
2528
raise ValueError(f"Cannot assign category to non-event: {target.path}")
2629
return []
2730

2831
category = path[path.index(RepoPaths.EVENTS.value) + 1]
32+
if category == "base_event.json":
33+
return []
2934

3035
categories = schema[SpecialFiles.CATEGORIES].data
3136
assert isinstance(categories, CategoriesDefn)

src/ocsf/repository/helpers.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,21 @@ class SpecialFiles(StrEnum):
4141
VERSION = "version.json"
4242
EXTENSION = "extension.json"
4343
OBSERVABLE = "objects/observable.json"
44+
BASE_EVENT = "base_event.json"
4445

4546
@staticmethod
4647
def contains(path: str) -> bool:
4748
return path in [e.value for e in SpecialFiles]
4849

4950

50-
SPECIAL_FILES = ("dictionary.json", "categories.json", "version.json", "extension.json", "objects/observable.json")
51+
SPECIAL_FILES = (
52+
"dictionary.json",
53+
"categories.json",
54+
"version.json",
55+
"extension.json",
56+
"objects/observable.json",
57+
"base_event.json",
58+
)
5159
"""Tuple containing strings of the values in the SpecialFiles enum."""
5260

5361

0 commit comments

Comments
 (0)