From b5c3a03d56e9711105b715cdd99fa2655dc4888c Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Thu, 25 Jan 2024 18:09:43 -0800 Subject: [PATCH] implement slurp by wrapping in a list, adding "_file" for /proc, and further wrapping in {"result": data} when --meta-out is used. --- jc/cli.py | 100 +++++++++++++++++++++++++----------- tests/test_jc_cli.py | 117 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 187 insertions(+), 30 deletions(-) diff --git a/jc/cli.py b/jc/cli.py index 8e0b2f73..46238db1 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -66,15 +66,17 @@ if PYGMENTS_INSTALLED: class JcCli(): - __slots__ = ( - 'data_in', 'data_out', 'options', 'args', 'parser_module', 'parser_name', 'indent', 'pad', - 'custom_colors', 'show_hidden', 'show_categories', 'ascii_only', 'json_separators', - 'json_indent', 'run_timestamp', 'about', 'debug', 'verbose_debug', 'force_color', 'mono', - 'help_me', 'pretty', 'quiet', 'ignore_exceptions', 'raw', 'slurp', 'meta_out', 'unbuffer', - 'version_info', 'yaml_output', 'bash_comp', 'zsh_comp', 'magic_found_parser', - 'magic_options', 'magic_run_command', 'magic_run_command_str', 'magic_stdout', - 'magic_stderr', 'magic_returncode', 'slice_str', 'slice_start', 'slice_end' - ) + __slots__ = ('data_in', 'data_out', 'options', 'args', 'parser_module', + 'parser_name', 'indent', 'pad', 'custom_colors', + 'show_hidden', 'show_categories', 'ascii_only', + 'json_separators', 'json_indent', 'run_timestamp', + 'inputlist', 'about', 'debug', 'verbose_debug', + 'force_color', 'mono', 'help_me', 'pretty', 'quiet', + 'ignore_exceptions', 'raw', 'slurp', 'meta_out', 'unbuffer', + 'version_info', 'yaml_output', 'bash_comp', 'zsh_comp', + 'magic_found_parser', 'magic_options', 'magic_run_command', + 'magic_run_command_str', 'magic_stdout', 'magic_stderr', + 'magic_returncode', 'slice_str', 'slice_start', 'slice_end') def __init__(self) -> None: self.data_in: Optional[Union[str, bytes, TextIO, Iterable[str]]] = None @@ -92,6 +94,7 @@ class JcCli(): self.json_separators: Optional[tuple[str, str]] = (',', ':') self.json_indent: Optional[int] = None self.run_timestamp: Optional[datetime] = None + self.inputlist: Optional[List[str]] = None # slicer self.slice_str: str = '' @@ -531,8 +534,9 @@ class JcCli(): output to magic_stdout. If multiple /proc files are detected, then a list of string output - is sento to magic_stdout. Since the proc parser is slurpable, it will - take the list of strings and parse the data into an array. + is sent to self.magic_stdout and a corresponding list of proc filenames + is sent to self.inputlist. These will be turned into a dict when slurped + downstream. """ if self.magic_run_command_str.startswith('/proc'): try: @@ -542,10 +546,12 @@ class JcCli(): if ' ' in self.magic_run_command_str: self.slurp = True multi_out: List[str] = [] - proc_files = self.magic_run_command_str.split() + filelist = self.magic_run_command_str.split() + filelist = [x.strip() for x in filelist] + self.inputlist = filelist - for file in proc_files: - # multi_out.append(self.open_text_file('/Users/kelly/temp/' + file)) + for file in self.inputlist: + # multi_out.append(self.open_text_file('/Users/kelly/temp' + file)) multi_out.append(self.open_text_file(file)) self.magic_stdout = multi_out @@ -553,7 +559,7 @@ class JcCli(): # single proc file else: file = self.magic_run_command_str - # self.magic_stdout = self.open_text_file('/Users/kelly/temp/' + file) + # self.magic_stdout = self.open_text_file('/Users/kelly/temp' + file) self.magic_stdout = self.open_text_file(file) except OSError as e: @@ -629,7 +635,7 @@ class JcCli(): def add_metadata_to_output(self) -> None: """ - This function mutates data_out in place. If the _jc_meta field + This function mutates self.data_out in place. If the _jc_meta field does not already exist, it will be created with the metadata fields. If the _jc_meta field already exists, the metadata fields will be added to the existing object. @@ -650,6 +656,9 @@ class JcCli(): meta_obj['magic_command'] = self.magic_run_command meta_obj['magic_command_exit'] = self.magic_returncode + if self.inputlist: + meta_obj['input_list'] = self.inputlist + if isinstance(self.data_out, dict): if '_jc_meta' not in self.data_out: self.data_out['_jc_meta'] = {} @@ -684,33 +693,66 @@ class JcCli(): def create_slurp_output(self) -> None: """ - Slurp output into an array. Only works for single-line strings or - multiple files coming from the /proc magic syntax. + Slurp input into a list. If input is coming from multiple /proc files + using magic syntax, then also add a `_file` key to the output. + + If --meta-out is used then further wrap the data in a dict like so: + {"result": data} + + self.input_list will already exist if the data is coming from the + /proc magic sytnax. Otherwise this funcion will build it for normal + slurp items. + + This will allow --meta-out to add its information in a clean way. + + This method updates self.data_out """ if self.parser_module and isinstance(self.data_in, (str, Iterable)): - items = [] + self.data_out = [] + # single-line string parsers if isinstance(self.data_in, str): items = self.data_in.splitlines() - elif isinstance(self.data_in, List): + self.inputlist = [] + + for line in items: + line = line.strip() + self.inputlist.append(line) + parsed_line = self.parser_module.parse( + line, + raw=self.raw, + quiet=self.quiet + ) + + self.data_out.append(parsed_line) + + # multiple files from /proc magic syntax + elif isinstance(self.data_in, List) and self.inputlist: items = self.data_in - self.data_out = [] - for line in items: - parsed_line = self.parser_module.parse( - line, - raw=self.raw, - quiet=self.quiet - ) + for mline in zip(self.inputlist, items): + parsed_line = self.parser_module.parse( + mline[1], + raw=self.raw, + quiet=self.quiet + ) - self.data_out.append(parsed_line) + if isinstance(parsed_line, dict): + parsed_line.update({'_file': mline[0]}) + + elif isinstance(parsed_line, List): + for obj in parsed_line: + obj.update({'_file': mline[0]}) + + self.data_out.append(parsed_line) if self.meta_out: + self.data_out = {"result": self.data_out} self.run_timestamp = datetime.now(timezone.utc) self.add_metadata_to_output() def create_normal_output(self) -> None: - """standard output""" + """standard output - updates self.data_out""" if self.parser_module: self.data_out = self.parser_module.parse( self.data_in, diff --git a/tests/test_jc_cli.py b/tests/test_jc_cli.py index 866ff397..3a3c5d35 100644 --- a/tests/test_jc_cli.py +++ b/tests/test_jc_cli.py @@ -9,6 +9,7 @@ except ModuleNotFoundError: PYGMENTS_INSTALLED=False from jc.cli import JcCli import jc.parsers.url as url_parser +import jc.parsers.proc as proc_parser class MyTests(unittest.TestCase): @@ -462,7 +463,7 @@ class MyTests(unittest.TestCase): cli.slicer() self.assertEqual(list(cli.data_in), expected) - def test_slurp(self): + def test_slurp_standard(self): cli = JcCli() cli.parser_module = url_parser cli.data_in = '''http://www.google.com @@ -472,5 +473,119 @@ class MyTests(unittest.TestCase): cli.create_slurp_output() self.assertEqual(cli.data_out, expected) + def test_slurp_standard_with_meta_out(self): + cli = JcCli() + cli.meta_out = True + cli.parser_module = url_parser + cli.data_in = '''http://www.google.com + https://www.kelly.com/testing + https://mail.apple.com''' + expected = {"result":[{"url":"http://www.google.com","scheme":"http","netloc":"www.google.com","path":None,"parent":None,"filename":None,"stem":None,"extension":None,"path_list":None,"query":None,"query_obj":None,"fragment":None,"username":None,"password":None,"hostname":"www.google.com","port":None,"encoded":{"url":"http://www.google.com","scheme":"http","netloc":"www.google.com","path":None,"parent":None,"filename":None,"stem":None,"extension":None,"path_list":None,"query":None,"fragment":None,"username":None,"password":None,"hostname":"www.google.com","port":None},"decoded":{"url":"http://www.google.com","scheme":"http","netloc":"www.google.com","path":None,"parent":None,"filename":None,"stem":None,"extension":None,"path_list":None,"query":None,"fragment":None,"username":None,"password":None,"hostname":"www.google.com","port":None}},{"url":"https://www.kelly.com/testing","scheme":"https","netloc":"www.kelly.com","path":"/testing","parent":"/","filename":"testing","stem":"testing","extension":None,"path_list":["testing"],"query":None,"query_obj":None,"fragment":None,"username":None,"password":None,"hostname":"www.kelly.com","port":None,"encoded":{"url":"https://www.kelly.com/testing","scheme":"https","netloc":"www.kelly.com","path":"/testing","parent":"/","filename":"testing","stem":"testing","extension":None,"path_list":["testing"],"query":None,"fragment":None,"username":None,"password":None,"hostname":"www.kelly.com","port":None},"decoded":{"url":"https://www.kelly.com/testing","scheme":"https","netloc":"www.kelly.com","path":"/testing","parent":"/","filename":"testing","stem":"testing","extension":None,"path_list":["testing"],"query":None,"fragment":None,"username":None,"password":None,"hostname":"www.kelly.com","port":None}},{"url":"https://mail.apple.com","scheme":"https","netloc":"mail.apple.com","path":None,"parent":None,"filename":None,"stem":None,"extension":None,"path_list":None,"query":None,"query_obj":None,"fragment":None,"username":None,"password":None,"hostname":"mail.apple.com","port":None,"encoded":{"url":"https://mail.apple.com","scheme":"https","netloc":"mail.apple.com","path":None,"parent":None,"filename":None,"stem":None,"extension":None,"path_list":None,"query":None,"fragment":None,"username":None,"password":None,"hostname":"mail.apple.com","port":None},"decoded":{"url":"https://mail.apple.com","scheme":"https","netloc":"mail.apple.com","path":None,"parent":None,"filename":None,"stem":None,"extension":None,"path_list":None,"query":None,"fragment":None,"username":None,"password":None,"hostname":"mail.apple.com","port":None}}],"_jc_meta":{"parser":"url","timestamp":1659659829.273349,"slice_start":None,"slice_end":None,"input_list":["http://www.google.com","https://www.kelly.com/testing","https://mail.apple.com"]}} + cli.create_slurp_output() + cli.run_timestamp = datetime(2022, 8, 5, 0, 37, 9, 273349, tzinfo=timezone.utc) + cli.parser_name = 'url' + cli.add_metadata_to_output() + self.assertEqual(cli.data_out, expected) + + def test_slurp_proc(self): + cli = JcCli() + cli.parser_module = proc_parser + cli.inputlist = ['/proc/stat', '/proc/cpuinfo'] + cli.data_in = [ +'''cpu 6002 152 8398 3444436 448 0 1174 0 0 0 +cpu0 2784 137 4367 1732802 225 0 221 0 0 0 +cpu1 3218 15 4031 1711634 223 0 953 0 0 0 +intr 2496709 18 73 0 0 0 0 0 0 1 0 0 0 18 0 0 0 4219 37341 423366 128490 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9063 2363 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +ctxt 4622716 +btime 1662154781 +processes 9831 +procs_running 1 +procs_blocked 0 +softirq 3478985 35230 1252057 3467 128583 51014 0 171199 1241297 0 596138 +''', +'''processor : 0 +vendor_id : GenuineIntel +cpu family : 6 +model : 142 +model name : Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz +stepping : 9 +cpu MHz : 2303.998 +cache size : 4096 KB +physical id : 0 +siblings : 1 +core id : 0 +cpu cores : 1 +apicid : 0 +initial apicid : 0 +fpu : yes +fpu_exception : yes +cpuid level : 22 +wp : yes +flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc eagerfpu pni pclmulqdq monitor ssse3 cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm 3dnowprefetch fsgsbase avx2 invpcid rdseed clflushopt md_clear flush_l1d +bogomips : 4607.99 +clflush size : 64 +cache_alignment : 64 +address sizes : 39 bits physical, 48 bits virtual +power management: +''' + ] + expected = [{"cpu":{"user":6002,"nice":152,"system":8398,"idle":3444436,"iowait":448,"irq":0,"softirq":1174,"steal":0,"guest":0,"guest_nice":0},"cpu0":{"user":2784,"nice":137,"system":4367,"idle":1732802,"iowait":225,"irq":0,"softirq":221,"steal":0,"guest":0,"guest_nice":0},"cpu1":{"user":3218,"nice":15,"system":4031,"idle":1711634,"iowait":223,"irq":0,"softirq":953,"steal":0,"guest":0,"guest_nice":0},"interrupts":[2496709,18,73,0,0,0,0,0,0,1,0,0,0,18,0,0,0,4219,37341,423366,128490,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9063,2363,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"context_switches":4622716,"boot_time":1662154781,"processes":9831,"processes_running":1,"processes_blocked":0,"softirq":[3478985,35230,1252057,3467,128583,51014,0,171199,1241297,0,596138],"_file":"/proc/stat"},[{"processor":0,"vendor_id":"GenuineIntel","cpu family":6,"model":142,"model name":"Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz","stepping":9,"cpu MHz":2303.998,"cache size":"4096 KB","physical id":0,"siblings":1,"core id":0,"cpu cores":1,"apicid":0,"initial apicid":0,"fpu":True,"fpu_exception":True,"cpuid level":22,"wp":True,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clflush","mmx","fxsr","sse","sse2","ht","syscall","nx","rdtscp","lm","constant_tsc","rep_good","nopl","xtopology","nonstop_tsc","eagerfpu","pni","pclmulqdq","monitor","ssse3","cx16","pcid","sse4_1","sse4_2","x2apic","movbe","popcnt","aes","xsave","avx","rdrand","hypervisor","lahf_lm","abm","3dnowprefetch","fsgsbase","avx2","invpcid","rdseed","clflushopt","md_clear","flush_l1d"],"bogomips":4607.99,"clflush size":64,"cache_alignment":64,"address sizes":"39 bits physical, 48 bits virtual","power management":None,"address_size_physical":39,"address_size_virtual":48,"cache_size_num":4096,"cache_size_unit":"KB","_file":"/proc/cpuinfo"}]] + cli.quiet = True + cli.create_slurp_output() + self.assertEqual(cli.data_out, expected) + + def test_slurp_proc_with_meta_out(self): + cli = JcCli() + cli.meta_out = True + cli.parser_module = proc_parser + cli.magic_run_command = ['/proc/stat', '/proc/cpuinfo'] + cli.magic_returncode = 0 + cli.inputlist = ['/proc/stat', '/proc/cpuinfo'] + cli.data_in = [ +'''cpu 6002 152 8398 3444436 448 0 1174 0 0 0 +cpu0 2784 137 4367 1732802 225 0 221 0 0 0 +cpu1 3218 15 4031 1711634 223 0 953 0 0 0 +intr 2496709 18 73 0 0 0 0 0 0 1 0 0 0 18 0 0 0 4219 37341 423366 128490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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +ctxt 4622716 +btime 1662154781 +processes 9831 +procs_running 1 +procs_blocked 0 +softirq 3478985 35230 1252057 3467 128583 51014 0 171199 1241297 0 596138 +''', +'''processor : 0 +vendor_id : GenuineIntel +cpu family : 6 +model : 142 +model name : Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz +stepping : 9 +cpu MHz : 2303.998 +cache size : 4096 KB +physical id : 0 +siblings : 1 +core id : 0 +cpu cores : 1 +apicid : 0 +initial apicid : 0 +fpu : yes +fpu_exception : yes +cpuid level : 22 +wp : yes +flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc eagerfpu pni pclmulqdq monitor ssse3 cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm 3dnowprefetch fsgsbase avx2 invpcid rdseed clflushopt md_clear flush_l1d +bogomips : 4607.99 +clflush size : 64 +cache_alignment : 64 +address sizes : 39 bits physical, 48 bits virtual +power management: +''' + ] + expected = {"result":[{"cpu":{"user":6002,"nice":152,"system":8398,"idle":3444436,"iowait":448,"irq":0,"softirq":1174,"steal":0,"guest":0,"guest_nice":0},"cpu0":{"user":2784,"nice":137,"system":4367,"idle":1732802,"iowait":225,"irq":0,"softirq":221,"steal":0,"guest":0,"guest_nice":0},"cpu1":{"user":3218,"nice":15,"system":4031,"idle":1711634,"iowait":223,"irq":0,"softirq":953,"steal":0,"guest":0,"guest_nice":0},"interrupts":[2496709,18,73,0,0,0,0,0,0,1,0,0,0,18,0,0,0,4219,37341,423366,128490,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9063,2363,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"context_switches":4622716,"boot_time":1662154781,"processes":9831,"processes_running":1,"processes_blocked":0,"softirq":[3478985,35230,1252057,3467,128583,51014,0,171199,1241297,0,596138],"_file":"/proc/stat"},[{"processor":0,"vendor_id":"GenuineIntel","cpu family":6,"model":142,"model name":"Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz","stepping":9,"cpu MHz":2303.998,"cache size":"4096 KB","physical id":0,"siblings":1,"core id":0,"cpu cores":1,"apicid":0,"initial apicid":0,"fpu":True,"fpu_exception":True,"cpuid level":22,"wp":True,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clflush","mmx","fxsr","sse","sse2","ht","syscall","nx","rdtscp","lm","constant_tsc","rep_good","nopl","xtopology","nonstop_tsc","eagerfpu","pni","pclmulqdq","monitor","ssse3","cx16","pcid","sse4_1","sse4_2","x2apic","movbe","popcnt","aes","xsave","avx","rdrand","hypervisor","lahf_lm","abm","3dnowprefetch","fsgsbase","avx2","invpcid","rdseed","clflushopt","md_clear","flush_l1d"],"bogomips":4607.99,"clflush size":64,"cache_alignment":64,"address sizes":"39 bits physical, 48 bits virtual","power management":None,"address_size_physical":39,"address_size_virtual":48,"cache_size_num":4096,"cache_size_unit":"KB","_file":"/proc/cpuinfo"}]],"_jc_meta":{"parser":"proc","timestamp":1659659829.273349,"slice_start":None,"slice_end":None,"magic_command":["/proc/stat","/proc/cpuinfo"],"magic_command_exit":0,"input_list":["/proc/stat","/proc/cpuinfo"]}} + cli.quiet = True + cli.create_slurp_output() + cli.run_timestamp = datetime(2022, 8, 5, 0, 37, 9, 273349, tzinfo=timezone.utc) + cli.parser_name = 'proc' + cli.add_metadata_to_output() + self.assertEqual(cli.data_out, expected) + if __name__ == '__main__': unittest.main() \ No newline at end of file