1919import json
2020import pathlib
2121
22+ from dfindexeddb import utils
2223from dfindexeddb import version
2324from dfindexeddb .leveldb import descriptor
2425from dfindexeddb .leveldb import ldb
2526from dfindexeddb .leveldb import log
2627from dfindexeddb .leveldb import record
28+ from dfindexeddb .leveldb .plugins import manager
2729
2830
2931_VALID_PRINTABLE_CHARACTERS = (
@@ -37,7 +39,7 @@ class Encoder(json.JSONEncoder):
3739 def default (self , o ):
3840 """Returns a serializable object for o."""
3941 if dataclasses .is_dataclass (o ):
40- o_dict = dataclasses .asdict (o )
42+ o_dict = utils .asdict (o )
4143 return o_dict
4244 if isinstance (o , bytes ):
4345 out = []
@@ -66,15 +68,39 @@ def _Output(structure, output):
6668
6769def DbCommand (args ):
6870 """The CLI for processing leveldb folders."""
71+ if args .plugin and args .plugin == 'list' :
72+ for plugin , _ in manager .LeveldbPluginManager .GetPlugins ():
73+ print (plugin )
74+ return
75+
76+ if args .plugin :
77+ plugin_class = manager .LeveldbPluginManager .GetPlugin (args .plugin )
78+ else :
79+ plugin_class = None
80+
6981 for leveldb_record in record .FolderReader (
7082 args .source ).GetRecords (
7183 use_manifest = args .use_manifest ,
7284 use_sequence_number = args .use_sequence_number ):
73- _Output (leveldb_record , output = args .output )
85+ if plugin_class :
86+ plugin_record = plugin_class .FromLevelDBRecord (leveldb_record )
87+ _Output (plugin_record , output = args .output )
88+ else :
89+ _Output (leveldb_record , output = args .output )
7490
7591
7692def LdbCommand (args ):
7793 """The CLI for processing ldb files."""
94+ if args .plugin and args .plugin == 'list' :
95+ for plugin , _ in manager .LeveldbPluginManager .GetPlugins ():
96+ print (plugin )
97+ return
98+
99+ if args .plugin :
100+ plugin_class = manager .LeveldbPluginManager .GetPlugin (args .plugin )
101+ else :
102+ plugin_class = None
103+
78104 ldb_file = ldb .FileReader (args .source )
79105
80106 if args .structure_type == 'blocks' :
@@ -85,14 +111,28 @@ def LdbCommand(args):
85111 elif args .structure_type == 'records' or not args .structure_type :
86112 # Prints key value record information.
87113 for key_value_record in ldb_file .GetKeyValueRecords ():
88- _Output (key_value_record , output = args .output )
114+ if plugin_class :
115+ plugin_record = plugin_class .FromKeyValueRecord (key_value_record )
116+ _Output (plugin_record , output = args .output )
117+ else :
118+ _Output (key_value_record , output = args .output )
89119
90120 else :
91121 print (f'{ args .structure_type } is not supported for ldb files.' )
92122
93123
94124def LogCommand (args ):
95125 """The CLI for processing log files."""
126+ if args .plugin and args .plugin == 'list' :
127+ for plugin , _ in manager .LeveldbPluginManager .GetPlugins ():
128+ print (plugin )
129+ return
130+
131+ if args .plugin :
132+ plugin_class = manager .LeveldbPluginManager .GetPlugin (args .plugin )
133+ else :
134+ plugin_class = None
135+
96136 log_file = log .FileReader (args .source )
97137
98138 if args .structure_type == 'blocks' :
@@ -114,7 +154,11 @@ def LogCommand(args):
114154 or not args .structure_type ):
115155 # Prints key value record information.
116156 for internal_key_record in log_file .GetParsedInternalKeys ():
117- _Output (internal_key_record , output = args .output )
157+ if plugin_class :
158+ plugin_record = plugin_class .FromKeyValueRecord (internal_key_record )
159+ _Output (plugin_record , output = args .output )
160+ else :
161+ _Output (internal_key_record , output = args .output )
118162
119163 else :
120164 print (f'{ args .structure_type } is not supported for log files.' )
@@ -146,6 +190,7 @@ def DescriptorCommand(args):
146190 else :
147191 print (f'{ args .structure_type } is not supported for descriptor files.' )
148192
193+
149194def App ():
150195 """The CLI app entrypoint for parsing leveldb files."""
151196 parser = argparse .ArgumentParser (
@@ -182,6 +227,9 @@ def App():
182227 'repr' ],
183228 default = 'json' ,
184229 help = 'Output format. Default is json' )
230+ parser_db .add_argument (
231+ '--plugin' ,
232+ help = 'Use plugin to parse records.' )
185233 parser_db .set_defaults (func = DbCommand )
186234
187235 parser_log = subparsers .add_parser (
@@ -200,6 +248,9 @@ def App():
200248 'repr' ],
201249 default = 'json' ,
202250 help = 'Output format. Default is json' )
251+ parser_log .add_argument (
252+ '--plugin' ,
253+ help = 'Use plugin to parse records.' )
203254 parser_log .add_argument (
204255 '-t' ,
205256 '--structure_type' ,
@@ -227,6 +278,9 @@ def App():
227278 'repr' ],
228279 default = 'json' ,
229280 help = 'Output format. Default is json' )
281+ parser_ldb .add_argument (
282+ '--plugin' ,
283+ help = 'Use plugin to parse records.' )
230284 parser_ldb .add_argument (
231285 '-t' ,
232286 '--structure_type' ,
0 commit comments