55The collection can be saved to and loaded from disk, in JSON-LD format.
66"""
77
8+ from glob import glob
89import json
910import os
1011from .registry import lookup_type
@@ -69,7 +70,7 @@ def _sort_nodes_by_id(self):
6970 sorted_nodes = dict (sorted (self .nodes .items ()))
7071 self .nodes = sorted_nodes
7172
72- def save (self , path , individual_files = False , include_empty_properties = False ):
73+ def save (self , path , individual_files = False , include_empty_properties = False , group_by_schema = False ):
7374 """
7475 Save the node collection to disk in JSON-LD format.
7576
@@ -78,10 +79,18 @@ def save(self, path, individual_files=False, include_empty_properties=False):
7879
7980 path (str):
8081 either a file or a directory into which the metadata will be written.
82+ It is recommended to use the extension ".jsonld".
8183 individual_files (bool):
8284 if False (default), save the entire collection into a single file.
8385 if True, `path` must be a directory, and each node is saved into a
8486 separate file within that directory.
87+ include_empty_properties (bool):
88+ if False (default), do not include properties with value None.
89+ if True, include all properties.
90+ group_by_schema (bool):
91+ Only applies if `individual_files` is True.
92+ If False (default), save all files in a single directory.
93+ If True, save into subdirectories according to the schema name.
8594
8695 Returns
8796 -------
@@ -137,7 +146,12 @@ def save(self, path, individual_files=False, include_empty_properties=False):
137146 else :
138147 assert node .id .startswith ("_:" )
139148 file_identifier = node .id [2 :]
140- file_path = os .path .join (path , f"{ file_identifier } .jsonld" )
149+ if group_by_schema :
150+ dir_path = os .path .join (path , node .__class__ .__name__ )
151+ os .makedirs (dir_path , exist_ok = True )
152+ file_path = os .path .join (dir_path , f"{ file_identifier } .jsonld" )
153+ else :
154+ file_path = os .path .join (path , f"{ file_identifier } .jsonld" )
141155 with open (file_path , "w" ) as fp :
142156 data = node .to_jsonld (embed_linked_nodes = False , include_empty_properties = include_empty_properties )
143157 json .dump (data , fp , indent = 2 )
@@ -150,9 +164,9 @@ def load(self, *paths, version=DEFAULT_VERSION):
150164
151165 `*paths` may contain either:
152166
153- 1) a single directory, in which case
154- all JSON-LD files all the top level of this directory will be loaded
155- (but without descending into subdirectories)
167+ 1) a single directory, in which case all JSON-LD files in this directory
168+ and any non-hidden subdirectories will be loaded
169+ (where hidden subdirectories are those whose name starts with ".").
156170
157171 2) one or more JSON-LD files, which will all be loaded.
158172
@@ -161,11 +175,10 @@ def load(self, *paths, version=DEFAULT_VERSION):
161175 """
162176 if len (paths ) == 1 and os .path .isdir (paths [0 ]):
163177 data_dir = paths [0 ]
164- json_paths = [
165- os .path .join (data_dir , item )
166- for item in os .listdir (data_dir )
167- if os .path .splitext (item )[1 ] in (".json" , ".jsonld" )
168- ]
178+ json_paths = (
179+ glob (f"{ data_dir } /**/*.jsonld" , recursive = True )
180+ + glob (f"{ data_dir } /**/*.json" , recursive = True )
181+ )
169182 else :
170183 json_paths = paths
171184
0 commit comments