From c332c4febf2bf757f662bc2cb22ec20fe0b4bcd0 Mon Sep 17 00:00:00 2001 From: Muescha <184316+muescha@users.noreply.github.com> Date: Wed, 31 Jan 2024 05:04:55 +0100 Subject: [PATCH] draft for path and path_list (#513) * draft for path_list * updaate doc * add input check * fix types * fix schema: add missing properties * add _process * fix _process docs * refactor: extract path.py parser * swap order of names alphabetically * documentation and comments * path parser: add early return for nodata * path and path-list parser: add test and fixtures * typo in file name * add early return for nodata * add test and fixtures * typo in file name * rename fixtures * rename fixtures * refactor to pathlib.Path * failing on windows - use PurePosixPath * changed the way to strip dot from suffix * add POSIX to path * test commit to see results on windows is failing * test commit to see results on windows is failing * add windows path detection * somehow Path not like the newline from input line * add test with more items * remove debug print * wrap test loops into into subTest * remove print statements * add path and path-list to CHANGELOG --------- Co-authored-by: Kelly Brazil --- CHANGELOG | 3 +- jc/lib.py | 2 + jc/parsers/path.py | 118 ++++++++ jc/parsers/path_list.py | 127 ++++++++ tests/fixtures/generic/path--long.json | 17 ++ tests/fixtures/generic/path--long.out | 1 + tests/fixtures/generic/path--one.json | 13 + tests/fixtures/generic/path--one.out | 1 + tests/fixtures/generic/path--windows.json | 14 + tests/fixtures/generic/path--windows.out | 1 + tests/fixtures/generic/path--with-spaces.json | 14 + tests/fixtures/generic/path--with-spaces.out | 1 + tests/fixtures/generic/path_list--long.json | 273 ++++++++++++++++++ tests/fixtures/generic/path_list--long.out | 1 + tests/fixtures/generic/path_list--one.json | 15 + tests/fixtures/generic/path_list--one.out | 1 + tests/fixtures/generic/path_list--two.json | 28 ++ tests/fixtures/generic/path_list--two.out | 1 + .../path_list--windows-environment.json | 26 ++ .../path_list--windows-environment.out | 1 + .../generic/path_list--windows-long.json | 202 +++++++++++++ .../generic/path_list--windows-long.out | 1 + .../fixtures/generic/path_list--windows.json | 27 ++ tests/fixtures/generic/path_list--windows.out | 1 + .../generic/path_list--with-spaces.json | 29 ++ .../generic/path_list--with-spaces.out | 1 + tests/test_path.py | 60 ++++ tests/test_path_list.py | 63 ++++ 28 files changed, 1041 insertions(+), 1 deletion(-) create mode 100644 jc/parsers/path.py create mode 100644 jc/parsers/path_list.py create mode 100644 tests/fixtures/generic/path--long.json create mode 100644 tests/fixtures/generic/path--long.out create mode 100644 tests/fixtures/generic/path--one.json create mode 100644 tests/fixtures/generic/path--one.out create mode 100644 tests/fixtures/generic/path--windows.json create mode 100644 tests/fixtures/generic/path--windows.out create mode 100644 tests/fixtures/generic/path--with-spaces.json create mode 100644 tests/fixtures/generic/path--with-spaces.out create mode 100644 tests/fixtures/generic/path_list--long.json create mode 100644 tests/fixtures/generic/path_list--long.out create mode 100644 tests/fixtures/generic/path_list--one.json create mode 100644 tests/fixtures/generic/path_list--one.out create mode 100644 tests/fixtures/generic/path_list--two.json create mode 100644 tests/fixtures/generic/path_list--two.out create mode 100644 tests/fixtures/generic/path_list--windows-environment.json create mode 100644 tests/fixtures/generic/path_list--windows-environment.out create mode 100644 tests/fixtures/generic/path_list--windows-long.json create mode 100644 tests/fixtures/generic/path_list--windows-long.out create mode 100644 tests/fixtures/generic/path_list--windows.json create mode 100644 tests/fixtures/generic/path_list--windows.out create mode 100644 tests/fixtures/generic/path_list--with-spaces.json create mode 100644 tests/fixtures/generic/path_list--with-spaces.out create mode 100644 tests/test_path.py create mode 100644 tests/test_path_list.py diff --git a/CHANGELOG b/CHANGELOG index b5149c9d..9ee7d51c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -12,7 +12,8 @@ jc changelog identifying which input matches to which output when using `--slurp` or when converting multiple files via `/proc` file magic syntax. - Add `kv-dup` parser for Key/Value files with duplicate keys -- TODO: Add `path-list` string parser to parse path list strings found in env variables +- Add `path` string parser to parse posix path +- Add `path-list` string parser to parse path list strings found in env variables - Enhance `nsd-control` parser to support more zone information - Enhance `ping` and `ping-s` parsers to support the `-I` command option - Enhance `proc-net-tcp` parser to add opposite endian support for architectures diff --git a/jc/lib.py b/jc/lib.py index 83d81c09..71b7c8a2 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -101,6 +101,8 @@ parsers: List[str] = [ 'os-prober', 'os-release', 'passwd', + 'path', + 'path-list', 'pci-ids', 'pgpass', 'pidstat', diff --git a/jc/parsers/path.py b/jc/parsers/path.py new file mode 100644 index 00000000..18327057 --- /dev/null +++ b/jc/parsers/path.py @@ -0,0 +1,118 @@ +"""jc - JSON Convert POSIX path string parser + +Parse a POSIX path. + +Usage (cli): + + $ echo "/Users/admin/.docker/bin" | jc --path + +Usage (module): + + import jc + result = jc.parse('path', path_string) + +Schema: + + { + "path": string or null, + "parent": string or null, + "filename": string or null, + "stem": string or null, + "extension": string or null, + "path_list": [ array or null + string + ], + } + +Examples: + + $ echo "/abc/def/gh.txt" | jc --path -p + + { + "path": "/abc/def/gh.txt", + "parent": "/abc/def", + "filename": "gh.txt", + "stem": "gh", + "extension": "txt", + "path_list": [ + "/", + "abc", + "def", + "gh.txt" + ] + } + +""" +from pathlib import PurePosixPath, PureWindowsPath +from typing import Dict + +import jc.utils + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = 'path string parser' + author = 'Michael Nietzold' + author_email = 'https://github.com/muescha' + compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] + tags = ['standard', 'string', 'slurpable'] + + +__version__ = info.version + + +def _process(proc_data: Dict) -> Dict: + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (Dictionary) raw structured data to process + + Returns: + + Dictionary. Structured to conform to the schema. + """ + + # no changes + return proc_data + + +def parse(data, raw=False, quiet=False): + """ + Main text parsing function + + Parameters: + + data: (string) text data to parse + raw: (boolean) unprocessed output if True + quiet: (boolean) suppress warning messages if True + + Returns: + + Dictionary representing a Key/Value pair document. + """ + jc.utils.compatibility(__name__, info.compatible, quiet) + jc.utils.input_type_check(data) + + if not jc.utils.has_data(data): + return {} + + data = data.rstrip("\n") + + if "\\" in data: + path = PureWindowsPath(data) + else: + path = PurePosixPath(data) + + raw_output = { + 'path': str(path), + 'parent': str(path.parent), + 'filename': path.name, + 'stem': path.stem, + 'extension': path.suffix[1:], + 'path_list': list(path.parts) + } + + return raw_output if raw else _process(raw_output) diff --git a/jc/parsers/path_list.py b/jc/parsers/path_list.py new file mode 100644 index 00000000..0f96b07e --- /dev/null +++ b/jc/parsers/path_list.py @@ -0,0 +1,127 @@ +"""jc - JSON Convert POSIX path list string parser + +Parse a colon-separated POSIX path list, commonly found in environment variables. + +Usage (cli): + + $ echo "/Users/admin/.docker/bin:/Users/admin/.asdf/shims" | jc --path-list + +Usage (module): + + import jc + result = jc.parse('path-list', path_string) + +Schema: + + [ + { + "path": string or null, + "parent": string or null, + "filename": string or null, + "stem": string or null, + "extension": string or null, + "path_list": [ array or null + string + ], + } + ] + +Examples: + + $ echo "/abc/def/gh.txt:/xyz/uvw/ab.app" | jc --path-list -p + + [ + { + "path": "/abc/def/gh.txt", + "parent": "/abc/def", + "filename": "gh.txt", + "stem": "gh", + "extension": "txt", + "path_list": [ + "/", + "abc", + "def", + "gh.txt" + ] + }, + { + "path": "/xyz/uvw/ab.app", + "parent": "/xyz/uvw", + "filename": "ab.app", + "stem": "ab", + "extension": "app", + "path_list": [ + "/", + "xyz", + "uvw", + "ab.app" + ] + } + ] + +""" + +import jc.parsers.path as path +from typing import Dict, List +import jc.utils + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = 'path list string parser' + author = 'Michael Nietzold' + author_email = 'https://github.com/muescha' + compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] + tags = ['standard', 'string', 'slurpable'] + + +__version__ = info.version + + +def _process(proc_data: List[Dict]) -> List[Dict]: + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (List of Dictionaries) raw structured data to process + + Returns: + + List of Dictionaries. Structured to conform to the schema. + """ + + # no changes + return proc_data + + +def parse(data, raw=False, quiet=False): + """ + Main text parsing function + + Parameters: + + data: (string) text data to parse + raw: (boolean) unprocessed output if True + quiet: (boolean) suppress warning messages if True + + Returns: + + List of Dictionaries representing a Key/Value pair document. + """ + jc.utils.compatibility(__name__, info.compatible, quiet) + jc.utils.input_type_check(data) + + # This parser is an alias of path.py + path.info = info # type: ignore + + delimiter = ":" if "\\" not in data else ";" + + raw_output = [ + path.parse(line, raw=raw, quiet=quiet) + for line in data.split(delimiter) + if jc.utils.has_data(data) + ] + + return raw_output if raw else _process(raw_output) diff --git a/tests/fixtures/generic/path--long.json b/tests/fixtures/generic/path--long.json new file mode 100644 index 00000000..6672059b --- /dev/null +++ b/tests/fixtures/generic/path--long.json @@ -0,0 +1,17 @@ +{ + "path": "/Library/Application Support/Script Editor/Templates/Cocoa-AppleScript Applet.app/Contents/Info.plist", + "parent": "/Library/Application Support/Script Editor/Templates/Cocoa-AppleScript Applet.app/Contents", + "filename": "Info.plist", + "stem": "Info", + "extension": "plist", + "path_list": [ + "/", + "Library", + "Application Support", + "Script Editor", + "Templates", + "Cocoa-AppleScript Applet.app", + "Contents", + "Info.plist" + ] +} \ No newline at end of file diff --git a/tests/fixtures/generic/path--long.out b/tests/fixtures/generic/path--long.out new file mode 100644 index 00000000..f7670ba9 --- /dev/null +++ b/tests/fixtures/generic/path--long.out @@ -0,0 +1 @@ +/Library/Application Support/Script Editor/Templates/Cocoa-AppleScript Applet.app/Contents/Info.plist \ No newline at end of file diff --git a/tests/fixtures/generic/path--one.json b/tests/fixtures/generic/path--one.json new file mode 100644 index 00000000..a2b37ab4 --- /dev/null +++ b/tests/fixtures/generic/path--one.json @@ -0,0 +1,13 @@ +{ + "path": "/abc/def/gh.txt", + "parent": "/abc/def", + "filename": "gh.txt", + "stem": "gh", + "extension": "txt", + "path_list": [ + "/", + "abc", + "def", + "gh.txt" + ] +} \ No newline at end of file diff --git a/tests/fixtures/generic/path--one.out b/tests/fixtures/generic/path--one.out new file mode 100644 index 00000000..3221ce4e --- /dev/null +++ b/tests/fixtures/generic/path--one.out @@ -0,0 +1 @@ +/abc/def/gh.txt \ No newline at end of file diff --git a/tests/fixtures/generic/path--windows.json b/tests/fixtures/generic/path--windows.json new file mode 100644 index 00000000..68851965 --- /dev/null +++ b/tests/fixtures/generic/path--windows.json @@ -0,0 +1,14 @@ +{ + "path": "C:\\Windows\\Program Files\\xfolder\\file.txt", + "parent": "C:\\Windows\\Program Files\\xfolder", + "filename": "file.txt", + "stem": "file", + "extension": "txt", + "path_list": [ + "C:\\", + "Windows", + "Program Files", + "xfolder", + "file.txt" + ] +} \ No newline at end of file diff --git a/tests/fixtures/generic/path--windows.out b/tests/fixtures/generic/path--windows.out new file mode 100644 index 00000000..70a034cd --- /dev/null +++ b/tests/fixtures/generic/path--windows.out @@ -0,0 +1 @@ +C:\Windows\Program Files\xfolder\file.txt \ No newline at end of file diff --git a/tests/fixtures/generic/path--with-spaces.json b/tests/fixtures/generic/path--with-spaces.json new file mode 100644 index 00000000..c1547f71 --- /dev/null +++ b/tests/fixtures/generic/path--with-spaces.json @@ -0,0 +1,14 @@ +{ + "path": "/User/admin/Application Support/App.app", + "parent": "/User/admin/Application Support", + "filename": "App.app", + "stem": "App", + "extension": "app", + "path_list": [ + "/", + "User", + "admin", + "Application Support", + "App.app" + ] +} \ No newline at end of file diff --git a/tests/fixtures/generic/path--with-spaces.out b/tests/fixtures/generic/path--with-spaces.out new file mode 100644 index 00000000..5c5233b3 --- /dev/null +++ b/tests/fixtures/generic/path--with-spaces.out @@ -0,0 +1 @@ +/User/admin/Application Support/App.app \ No newline at end of file diff --git a/tests/fixtures/generic/path_list--long.json b/tests/fixtures/generic/path_list--long.json new file mode 100644 index 00000000..e6a301ae --- /dev/null +++ b/tests/fixtures/generic/path_list--long.json @@ -0,0 +1,273 @@ +[ + { + "extension": "", + "filename": "bin", + "parent": "/Users/macuser/.docker", + "path": "/Users/macuser/.docker/bin", + "path_list": [ + "/", + "Users", + "macuser", + ".docker", + "bin" + ], + "stem": "bin" + }, + { + "extension": "", + "filename": "shims", + "parent": "/Users/macuser/.asdf", + "path": "/Users/macuser/.asdf/shims", + "path_list": [ + "/", + "Users", + "macuser", + ".asdf", + "shims" + ], + "stem": "shims" + }, + { + "extension": "", + "filename": "bin", + "parent": "/opt/homebrew/opt/asdf/libexec", + "path": "/opt/homebrew/opt/asdf/libexec/bin", + "path_list": [ + "/", + "opt", + "homebrew", + "opt", + "asdf", + "libexec", + "bin" + ], + "stem": "bin" + }, + { + "extension": "", + "filename": "bin", + "parent": "/opt/homebrew", + "path": "/opt/homebrew/bin", + "path_list": [ + "/", + "opt", + "homebrew", + "bin" + ], + "stem": "bin" + }, + { + "extension": "", + "filename": "sbin", + "parent": "/opt/homebrew", + "path": "/opt/homebrew/sbin", + "path_list": [ + "/", + "opt", + "homebrew", + "sbin" + ], + "stem": "sbin" + }, + { + "extension": "", + "filename": "bin", + "parent": "/usr/local", + "path": "/usr/local/bin", + "path_list": [ + "/", + "usr", + "local", + "bin" + ], + "stem": "bin" + }, + { + "extension": "", + "filename": "bin", + "parent": "/System/Cryptexes/App/usr", + "path": "/System/Cryptexes/App/usr/bin", + "path_list": [ + "/", + "System", + "Cryptexes", + "App", + "usr", + "bin" + ], + "stem": "bin" + }, + { + "extension": "", + "filename": "bin", + "parent": "/usr", + "path": "/usr/bin", + "path_list": [ + "/", + "usr", + "bin" + ], + "stem": "bin" + }, + { + "extension": "", + "filename": "bin", + "parent": "/", + "path": "/bin", + "path_list": [ + "/", + "bin" + ], + "stem": "bin" + }, + { + "extension": "", + "filename": "sbin", + "parent": "/usr", + "path": "/usr/sbin", + "path_list": [ + "/", + "usr", + "sbin" + ], + "stem": "sbin" + }, + { + "extension": "", + "filename": "sbin", + "parent": "/", + "path": "/sbin", + "path_list": [ + "/", + "sbin" + ], + "stem": "sbin" + }, + { + "extension": "", + "filename": "bin", + "parent": "/Library/Apple/usr", + "path": "/Library/Apple/usr/bin", + "path_list": [ + "/", + "Library", + "Apple", + "usr", + "bin" + ], + "stem": "bin" + }, + { + "extension": "", + "filename": "bin", + "parent": "/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local", + "path": "/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin", + "path_list": [ + "/", + "var", + "run", + "com.apple.security.cryptexd", + "codex.system", + "bootstrap", + "usr", + "local", + "bin" + ], + "stem": "bin" + }, + { + "extension": "", + "filename": "bin", + "parent": "/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr", + "path": "/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin", + "path_list": [ + "/", + "var", + "run", + "com.apple.security.cryptexd", + "codex.system", + "bootstrap", + "usr", + "bin" + ], + "stem": "bin" + }, + { + "extension": "", + "filename": "bin", + "parent": "/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal", + "path": "/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin", + "path_list": [ + "/", + "var", + "run", + "com.apple.security.cryptexd", + "codex.system", + "bootstrap", + "usr", + "appleinternal", + "bin" + ], + "stem": "bin" + }, + { + "extension": "", + "filename": "bin", + "parent": "/Users/macuser/.fig", + "path": "/Users/macuser/.fig/bin", + "path_list": [ + "/", + "Users", + "macuser", + ".fig", + "bin" + ], + "stem": "bin" + }, + { + "extension": "", + "filename": "bin", + "parent": "/Users/macuser/.local", + "path": "/Users/macuser/.local/bin", + "path_list": [ + "/", + "Users", + "macuser", + ".local", + "bin" + ], + "stem": "bin" + }, + { + "extension": "", + "filename": "scripts", + "parent": "/Users/macuser/Library/Application Support/JetBrains/Toolbox", + "path": "/Users/macuser/Library/Application Support/JetBrains/Toolbox/scripts", + "path_list": [ + "/", + "Users", + "macuser", + "Library", + "Application Support", + "JetBrains", + "Toolbox", + "scripts" + ], + "stem": "scripts" + }, + { + "extension": "", + "filename": "bin", + "parent": "/opt/homebrew/opt/fzf", + "path": "/opt/homebrew/opt/fzf/bin", + "path_list": [ + "/", + "opt", + "homebrew", + "opt", + "fzf", + "bin" + ], + "stem": "bin" + } +] \ No newline at end of file diff --git a/tests/fixtures/generic/path_list--long.out b/tests/fixtures/generic/path_list--long.out new file mode 100644 index 00000000..90e48025 --- /dev/null +++ b/tests/fixtures/generic/path_list--long.out @@ -0,0 +1 @@ +/Users/macuser/.docker/bin:/Users/macuser/.asdf/shims:/opt/homebrew/opt/asdf/libexec/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/macuser/.fig/bin:/Users/macuser/.local/bin:/Users/macuser/Library/Application Support/JetBrains/Toolbox/scripts:/opt/homebrew/opt/fzf/bin \ No newline at end of file diff --git a/tests/fixtures/generic/path_list--one.json b/tests/fixtures/generic/path_list--one.json new file mode 100644 index 00000000..0c4fd9e4 --- /dev/null +++ b/tests/fixtures/generic/path_list--one.json @@ -0,0 +1,15 @@ +[ + { + "path": "/abc/def/gh.txt", + "parent": "/abc/def", + "filename": "gh.txt", + "stem": "gh", + "extension": "txt", + "path_list": [ + "/", + "abc", + "def", + "gh.txt" + ] + } +] \ No newline at end of file diff --git a/tests/fixtures/generic/path_list--one.out b/tests/fixtures/generic/path_list--one.out new file mode 100644 index 00000000..3221ce4e --- /dev/null +++ b/tests/fixtures/generic/path_list--one.out @@ -0,0 +1 @@ +/abc/def/gh.txt \ No newline at end of file diff --git a/tests/fixtures/generic/path_list--two.json b/tests/fixtures/generic/path_list--two.json new file mode 100644 index 00000000..d47f3afb --- /dev/null +++ b/tests/fixtures/generic/path_list--two.json @@ -0,0 +1,28 @@ +[ + { + "path": "/abc/def/gh.txt", + "parent": "/abc/def", + "filename": "gh.txt", + "stem": "gh", + "extension": "txt", + "path_list": [ + "/", + "abc", + "def", + "gh.txt" + ] + }, + { + "path": "/xyz/uvw/ab.app", + "parent": "/xyz/uvw", + "filename": "ab.app", + "stem": "ab", + "extension": "app", + "path_list": [ + "/", + "xyz", + "uvw", + "ab.app" + ] + } +] \ No newline at end of file diff --git a/tests/fixtures/generic/path_list--two.out b/tests/fixtures/generic/path_list--two.out new file mode 100644 index 00000000..47ce6447 --- /dev/null +++ b/tests/fixtures/generic/path_list--two.out @@ -0,0 +1 @@ +/abc/def/gh.txt:/xyz/uvw/ab.app \ No newline at end of file diff --git a/tests/fixtures/generic/path_list--windows-environment.json b/tests/fixtures/generic/path_list--windows-environment.json new file mode 100644 index 00000000..1ae6020b --- /dev/null +++ b/tests/fixtures/generic/path_list--windows-environment.json @@ -0,0 +1,26 @@ +[ + { + "path": "%PROGRAMFILES%\\Git\\bin", + "parent": "%PROGRAMFILES%\\Git", + "filename": "bin", + "stem": "bin", + "extension": "", + "path_list": [ + "%PROGRAMFILES%", + "Git", + "bin" + ] + }, + { + "path": "c:\\my\\folder", + "parent": "c:\\my", + "filename": "folder", + "stem": "folder", + "extension": "", + "path_list": [ + "c:\\", + "my", + "folder" + ] + } +] diff --git a/tests/fixtures/generic/path_list--windows-environment.out b/tests/fixtures/generic/path_list--windows-environment.out new file mode 100644 index 00000000..4d507fe0 --- /dev/null +++ b/tests/fixtures/generic/path_list--windows-environment.out @@ -0,0 +1 @@ +%PROGRAMFILES%\Git\bin;c:\my\folder \ No newline at end of file diff --git a/tests/fixtures/generic/path_list--windows-long.json b/tests/fixtures/generic/path_list--windows-long.json new file mode 100644 index 00000000..cd595fdc --- /dev/null +++ b/tests/fixtures/generic/path_list--windows-long.json @@ -0,0 +1,202 @@ +[ + { + "path": "D:\\Program Files\\Autodesk\\Maya2008\\bin", + "parent": "D:\\Program Files\\Autodesk\\Maya2008", + "filename": "bin", + "stem": "bin", + "extension": "", + "path_list": [ + "D:\\", + "Program Files", + "Autodesk", + "Maya2008", + "bin" + ] + }, + { + "path": "C:\\Ruby192\\bin", + "parent": "C:\\Ruby192", + "filename": "bin", + "stem": "bin", + "extension": "", + "path_list": [ + "C:\\", + "Ruby192", + "bin" + ] + }, + { + "path": "C:\\WINDOWS\\system32", + "parent": "C:\\WINDOWS", + "filename": "system32", + "stem": "system32", + "extension": "", + "path_list": [ + "C:\\", + "WINDOWS", + "system32" + ] + }, + { + "path": "C:\\WINDOWS", + "parent": "C:\\", + "filename": "WINDOWS", + "stem": "WINDOWS", + "extension": "", + "path_list": [ + "C:\\", + "WINDOWS" + ] + }, + { + "path": "C:\\WINDOWS\\System32\\Wbem", + "parent": "C:\\WINDOWS\\System32", + "filename": "Wbem", + "stem": "Wbem", + "extension": "", + "path_list": [ + "C:\\", + "WINDOWS", + "System32", + "Wbem" + ] + }, + { + "path": "C:\\PROGRA~1\\DISKEE~2\\DISKEE~1", + "parent": "C:\\PROGRA~1\\DISKEE~2", + "filename": "DISKEE~1", + "stem": "DISKEE~1", + "extension": "", + "path_list": [ + "C:\\", + "PROGRA~1", + "DISKEE~2", + "DISKEE~1" + ] + }, + { + "path": "c:\\Program Files\\Microsoft SQL Server\\90\\Tools\\binn", + "parent": "c:\\Program Files\\Microsoft SQL Server\\90\\Tools", + "filename": "binn", + "stem": "binn", + "extension": "", + "path_list": [ + "c:\\", + "Program Files", + "Microsoft SQL Server", + "90", + "Tools", + "binn" + ] + }, + { + "path": "C:\\Program Files\\QuickTime\\QTSystem", + "parent": "C:\\Program Files\\QuickTime", + "filename": "QTSystem", + "stem": "QTSystem", + "extension": "", + "path_list": [ + "C:\\", + "Program Files", + "QuickTime", + "QTSystem" + ] + }, + { + "path": "D:\\Program Files\\TortoiseSVN\\bin", + "parent": "D:\\Program Files\\TortoiseSVN", + "filename": "bin", + "stem": "bin", + "extension": "", + "path_list": [ + "D:\\", + "Program Files", + "TortoiseSVN", + "bin" + ] + }, + { + "path": "D:\\Program Files\\Bazaar", + "parent": "D:\\Program Files", + "filename": "Bazaar", + "stem": "Bazaar", + "extension": "", + "path_list": [ + "D:\\", + "Program Files", + "Bazaar" + ] + }, + { + "path": "C:\\Program Files\\Android\\android-sdk\\tools", + "parent": "C:\\Program Files\\Android\\android-sdk", + "filename": "tools", + "stem": "tools", + "extension": "", + "path_list": [ + "C:\\", + "Program Files", + "Android", + "android-sdk", + "tools" + ] + }, + { + "path": "D:\\Program Files\\Microsoft Visual Studio\\Common\\Tools\\WinNT", + "parent": "D:\\Program Files\\Microsoft Visual Studio\\Common\\Tools", + "filename": "WinNT", + "stem": "WinNT", + "extension": "", + "path_list": [ + "D:\\", + "Program Files", + "Microsoft Visual Studio", + "Common", + "Tools", + "WinNT" + ] + }, + { + "path": "D:\\Program Files\\Microsoft Visual Studio\\Common\\MSDev98\\Bin", + "parent": "D:\\Program Files\\Microsoft Visual Studio\\Common\\MSDev98", + "filename": "Bin", + "stem": "Bin", + "extension": "", + "path_list": [ + "D:\\", + "Program Files", + "Microsoft Visual Studio", + "Common", + "MSDev98", + "Bin" + ] + }, + { + "path": "D:\\Program Files\\Microsoft Visual Studio\\Common\\Tools", + "parent": "D:\\Program Files\\Microsoft Visual Studio\\Common", + "filename": "Tools", + "stem": "Tools", + "extension": "", + "path_list": [ + "D:\\", + "Program Files", + "Microsoft Visual Studio", + "Common", + "Tools" + ] + }, + { + "path": "D:\\Program Files\\Microsoft Visual Studio\\VC98\\bin", + "parent": "D:\\Program Files\\Microsoft Visual Studio\\VC98", + "filename": "bin", + "stem": "bin", + "extension": "", + "path_list": [ + "D:\\", + "Program Files", + "Microsoft Visual Studio", + "VC98", + "bin" + ] + } +] diff --git a/tests/fixtures/generic/path_list--windows-long.out b/tests/fixtures/generic/path_list--windows-long.out new file mode 100644 index 00000000..34dc40ad --- /dev/null +++ b/tests/fixtures/generic/path_list--windows-long.out @@ -0,0 +1 @@ +D:\Program Files\Autodesk\Maya2008\bin;C:\Ruby192\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\PROGRA~1\DISKEE~2\DISKEE~1\;c:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\QuickTime\QTSystem\;D:\Program Files\TortoiseSVN\bin;D:\Program Files\Bazaar;C:\Program Files\Android\android-sdk\tools;D:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT;D:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin;D:\Program Files\Microsoft Visual Studio\Common\Tools;D:\Program Files\Microsoft Visual Studio\VC98\bin \ No newline at end of file diff --git a/tests/fixtures/generic/path_list--windows.json b/tests/fixtures/generic/path_list--windows.json new file mode 100644 index 00000000..c0c3d9fb --- /dev/null +++ b/tests/fixtures/generic/path_list--windows.json @@ -0,0 +1,27 @@ +[ + { + "path": "c:\\Program Files (x86)\\Git\\bin", + "parent": "c:\\Program Files (x86)\\Git", + "filename": "bin", + "stem": "bin", + "extension": "", + "path_list": [ + "c:\\", + "Program Files (x86)", + "Git", + "bin" + ] + }, + { + "path": "c:\\my\\folder", + "parent": "c:\\my", + "filename": "folder", + "stem": "folder", + "extension": "", + "path_list": [ + "c:\\", + "my", + "folder" + ] + } +] diff --git a/tests/fixtures/generic/path_list--windows.out b/tests/fixtures/generic/path_list--windows.out new file mode 100644 index 00000000..b4ba16b4 --- /dev/null +++ b/tests/fixtures/generic/path_list--windows.out @@ -0,0 +1 @@ +c:\Program Files (x86)\Git\bin;c:\my\folder \ No newline at end of file diff --git a/tests/fixtures/generic/path_list--with-spaces.json b/tests/fixtures/generic/path_list--with-spaces.json new file mode 100644 index 00000000..e65b8ab3 --- /dev/null +++ b/tests/fixtures/generic/path_list--with-spaces.json @@ -0,0 +1,29 @@ +[ + { + "path": "/User/admin/Application Support/App.app", + "parent": "/User/admin/Application Support", + "filename": "App.app", + "stem": "App", + "extension": "app", + "path_list": [ + "/", + "User", + "admin", + "Application Support", + "App.app" + ] + }, + { + "path": "/User/admin/path", + "parent": "/User/admin", + "filename": "path", + "stem": "path", + "extension": "", + "path_list": [ + "/", + "User", + "admin", + "path" + ] + } +] \ No newline at end of file diff --git a/tests/fixtures/generic/path_list--with-spaces.out b/tests/fixtures/generic/path_list--with-spaces.out new file mode 100644 index 00000000..25fc8ee0 --- /dev/null +++ b/tests/fixtures/generic/path_list--with-spaces.out @@ -0,0 +1 @@ +/User/admin/Application Support/App.app:/User/admin/path \ No newline at end of file diff --git a/tests/test_path.py b/tests/test_path.py new file mode 100644 index 00000000..28a15d5b --- /dev/null +++ b/tests/test_path.py @@ -0,0 +1,60 @@ +import os +import json +import unittest +from typing import Dict +from jc.parsers.path import parse + +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) + +FIXTURES_DIR = 'fixtures/generic/' + + +def open_file(name, ext): + return open(get_path(name, ext), 'r', encoding='utf-8') + + +def get_path(name, ext): + return os.path.join(THIS_DIR, name + '.' + ext) + + +class MyTests(unittest.TestCase): + f_in: Dict = {} + f_json: Dict = {} + + fixtures = { + 'path--one': 'fixtures/generic/path--one', + 'path--long': 'fixtures/generic/path--long', + 'path--windows': 'fixtures/generic/path--windows', + 'path--with-spaces': 'fixtures/generic/path--with-spaces', + } + + @classmethod + def setUpClass(cls): + + for file, filepath in cls.fixtures.items(): + with open_file(filepath, 'out') as in_file, \ + open_file(filepath, 'json') as json_file: + cls.f_in[file] = in_file.read() + cls.f_json[file] = json.loads(json_file.read()) + + def test_path_nodata(self): + """ + Test 'path' with no data + """ + self.assertEqual(parse('', quiet=True), {}) + + def test_path(self): + """ + Test 'path' with various logs + """ + for file in self.fixtures: + with self.subTest("fixture: " + file): + self.assertEqual( + parse(self.f_in[file], quiet=True), + self.f_json[file], + "Should be equal for test files: {0}.*".format(file) + ) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_path_list.py b/tests/test_path_list.py new file mode 100644 index 00000000..81c03e65 --- /dev/null +++ b/tests/test_path_list.py @@ -0,0 +1,63 @@ +import os +import json +import unittest +from typing import Dict +from jc.parsers.path_list import parse + +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) + +FIXTURES_DIR = 'fixtures/generic/' + + +def open_file(name, ext): + return open(get_path(name, ext), 'r', encoding='utf-8') + + +def get_path(name, ext): + return os.path.join(THIS_DIR, name + '.' + ext) + + +class MyTests(unittest.TestCase): + f_in: Dict = {} + f_json: Dict = {} + + fixtures = { + 'path_list--one': 'fixtures/generic/path_list--one', + 'path_list--two': 'fixtures/generic/path_list--two', + 'path_list--long': 'fixtures/generic/path_list--long', + 'path_list--windows': 'fixtures/generic/path_list--windows', + 'path_list--windows-long': 'fixtures/generic/path_list--windows-long', + 'path_list--windows-environment': 'fixtures/generic/path_list--windows-environment', + 'path_list--with-spaces': 'fixtures/generic/path_list--with-spaces', + } + + @classmethod + def setUpClass(cls): + + for file, filepath in cls.fixtures.items(): + with open_file(filepath, 'out') as in_file, \ + open_file(filepath, 'json') as json_file: + cls.f_in[file] = in_file.read() + cls.f_json[file] = json.loads(json_file.read()) + + def test_path_nodata(self): + """ + Test 'path' with no data + """ + self.assertEqual(parse('', quiet=True), []) + + def test_path(self): + """ + Test 'path' with various logs + """ + for file in self.fixtures: + with self.subTest("fixture: " + file): + self.assertEqual( + parse(self.f_in[file], quiet=True), + self.f_json[file], + "Should be equal for test files: {0}.*".format(file) + ) + + +if __name__ == '__main__': + unittest.main()