diff --git a/README.md b/README.md index 3d1a7aec..21f23ba6 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,7 @@ option. | ` --ping-s` | `ping` and `ping6` command streaming parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/ping_s) | | ` --pip-list` | `pip list` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/pip_list) | | ` --pip-show` | `pip show` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/pip_show) | +| ` --plist` | PLIST file parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/plist) | | ` --postconf` | `postconf -M` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/postconf) | | ` --ps` | `ps` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/ps) | | ` --route` | `route` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/route) | diff --git a/completions/jc_bash_completion.sh b/completions/jc_bash_completion.sh index cac8256d..0e31b000 100644 --- a/completions/jc_bash_completion.sh +++ b/completions/jc_bash_completion.sh @@ -4,7 +4,7 @@ _jc() jc_about_options jc_about_mod_options jc_help_options jc_special_options jc_commands=(acpi airport arp blkid chage cksum crontab date df dig dmidecode dpkg du env file finger free git gpg hciconfig id ifconfig iostat iptables iw jobs last lastb ls lsblk lsmod lsof lsusb md5 md5sum mount mpstat netstat nmcli ntpq pidstat ping ping6 pip pip3 postconf printenv ps route rpm rsync sfdisk sha1sum sha224sum sha256sum sha384sum sha512sum shasum ss stat sum sysctl systemctl systeminfo timedatectl top tracepath tracepath6 traceroute traceroute6 ufw uname update-alternatives upower uptime vdir vmstat w wc who xrandr zipinfo) - jc_parsers=(--acpi --airport --airport-s --arp --asciitable --asciitable-m --blkid --chage --cksum --crontab --crontab-u --csv --csv-s --date --df --dig --dir --dmidecode --dpkg-l --du --email-address --env --file --finger --free --fstab --git-log --git-log-s --gpg --group --gshadow --hash --hashsum --hciconfig --history --hosts --id --ifconfig --ini --iostat --iostat-s --ip-address --iptables --iso-datetime --iw-scan --jar-manifest --jobs --jwt --kv --last --ls --ls-s --lsblk --lsmod --lsof --lsusb --m3u --mount --mpstat --mpstat-s --netstat --nmcli --ntpq --passwd --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --postconf --ps --route --rpm-qi --rsync --rsync-s --sfdisk --shadow --ss --stat --stat-s --sysctl --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --top --top-s --tracepath --traceroute --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --vmstat --vmstat-s --w --wc --who --x509-cert --xml --xrandr --yaml --zipinfo) + jc_parsers=(--acpi --airport --airport-s --arp --asciitable --asciitable-m --blkid --chage --cksum --crontab --crontab-u --csv --csv-s --date --df --dig --dir --dmidecode --dpkg-l --du --email-address --env --file --finger --free --fstab --git-log --git-log-s --gpg --group --gshadow --hash --hashsum --hciconfig --history --hosts --id --ifconfig --ini --iostat --iostat-s --ip-address --iptables --iso-datetime --iw-scan --jar-manifest --jobs --jwt --kv --last --ls --ls-s --lsblk --lsmod --lsof --lsusb --m3u --mount --mpstat --mpstat-s --netstat --nmcli --ntpq --passwd --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --plist --postconf --ps --route --rpm-qi --rsync --rsync-s --sfdisk --shadow --ss --stat --stat-s --sysctl --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --top --top-s --tracepath --traceroute --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --vmstat --vmstat-s --w --wc --who --x509-cert --xml --xrandr --yaml --zipinfo) jc_options=(--force-color -C --debug -d --monochrome -m --pretty -p --quiet -q --raw -r --unbuffer -u --yaml-out -y) jc_about_options=(--about -a) jc_about_mod_options=(--pretty -p --yaml-out -y --monochrome -m --force-color -C) diff --git a/completions/jc_zsh_completion.sh b/completions/jc_zsh_completion.sh index f0aaae72..38614a4d 100644 --- a/completions/jc_zsh_completion.sh +++ b/completions/jc_zsh_completion.sh @@ -94,7 +94,7 @@ _jc() { 'xrandr:run "xrandr" command with magic syntax.' 'zipinfo:run "zipinfo" command with magic syntax.' ) - jc_parsers=(--acpi --airport --airport-s --arp --asciitable --asciitable-m --blkid --chage --cksum --crontab --crontab-u --csv --csv-s --date --df --dig --dir --dmidecode --dpkg-l --du --email-address --env --file --finger --free --fstab --git-log --git-log-s --gpg --group --gshadow --hash --hashsum --hciconfig --history --hosts --id --ifconfig --ini --iostat --iostat-s --ip-address --iptables --iso-datetime --iw-scan --jar-manifest --jobs --jwt --kv --last --ls --ls-s --lsblk --lsmod --lsof --lsusb --m3u --mount --mpstat --mpstat-s --netstat --nmcli --ntpq --passwd --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --postconf --ps --route --rpm-qi --rsync --rsync-s --sfdisk --shadow --ss --stat --stat-s --sysctl --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --top --top-s --tracepath --traceroute --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --vmstat --vmstat-s --w --wc --who --x509-cert --xml --xrandr --yaml --zipinfo) + jc_parsers=(--acpi --airport --airport-s --arp --asciitable --asciitable-m --blkid --chage --cksum --crontab --crontab-u --csv --csv-s --date --df --dig --dir --dmidecode --dpkg-l --du --email-address --env --file --finger --free --fstab --git-log --git-log-s --gpg --group --gshadow --hash --hashsum --hciconfig --history --hosts --id --ifconfig --ini --iostat --iostat-s --ip-address --iptables --iso-datetime --iw-scan --jar-manifest --jobs --jwt --kv --last --ls --ls-s --lsblk --lsmod --lsof --lsusb --m3u --mount --mpstat --mpstat-s --netstat --nmcli --ntpq --passwd --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --plist --postconf --ps --route --rpm-qi --rsync --rsync-s --sfdisk --shadow --ss --stat --stat-s --sysctl --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --top --top-s --tracepath --traceroute --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --vmstat --vmstat-s --w --wc --who --x509-cert --xml --xrandr --yaml --zipinfo) jc_parsers_describe=( '--acpi:`acpi` command parser' '--airport:`airport -I` command parser' @@ -166,6 +166,7 @@ _jc() { '--ping-s:`ping` and `ping6` command streaming parser' '--pip-list:`pip list` command parser' '--pip-show:`pip show` command parser' + '--plist:PLIST file parser' '--postconf:`postconf -M` command parser' '--ps:`ps` command parser' '--route:`route` command parser' diff --git a/docs/parsers/plist.md b/docs/parsers/plist.md new file mode 100644 index 00000000..0e1869d5 --- /dev/null +++ b/docs/parsers/plist.md @@ -0,0 +1,62 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.plist + +jc - JSON Convert PLIST file parser + +Converts binary and XML PLIST files. + +Usage (cli): + + $ cat file.plist | jc --plist + +Usage (module): + + import jc + result = jc.parse('plist', plist_command_output) + +Schema: + + [ + { + "plist": string, + "bar": boolean, + "baz": integer + } + ] + +Examples: + + $ plist | jc --plist -p + [] + + $ plist | jc --plist -p -r + [] + + + +### parse + +```python +def parse(data: Union[str, bytes], + raw: bool = False, + quiet: bool = False) -> Dict +``` + +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. Raw or processed structured data. + +### Parser Information +Compatibility: linux, darwin, cygwin, win32, aix, freebsd + +Version 1.0 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/jc/lib.py b/jc/lib.py index e554fbd9..4feff98f 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -79,6 +79,7 @@ parsers = [ 'ping-s', 'pip-list', 'pip-show', + 'plist', 'postconf', 'ps', 'route', diff --git a/jc/parsers/plist.py b/jc/parsers/plist.py new file mode 100644 index 00000000..3578ac59 --- /dev/null +++ b/jc/parsers/plist.py @@ -0,0 +1,93 @@ +"""jc - JSON Convert PLIST file parser + +Converts binary and XML PLIST files. + +Usage (cli): + + $ cat file.plist | jc --plist + +Usage (module): + + import jc + result = jc.parse('plist', plist_command_output) + +Schema: + + [ + { + "plist": string, + "bar": boolean, + "baz": integer + } + ] + +Examples: + + $ plist | jc --plist -p + [] + + $ plist | jc --plist -p -r + [] +""" +from typing import List, Dict, Union +import plistlib +import jc.utils + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = 'PLIST file parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] + + +__version__ = info.version + + +def _process(proc_data: Dict) -> 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. + """ + return proc_data + + +def parse( + data: Union[str, bytes], + raw: bool = False, + quiet: bool = False +) -> Dict: + """ + 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. Raw or processed structured data. + """ + jc.utils.compatibility(__name__, info.compatible, quiet) + + if isinstance(data, str): + data = bytes(data, 'utf-8') + + raw_output: Dict = {} + + if jc.utils.has_data(data): + + raw_output = plistlib.loads(data) + + return raw_output if raw else _process(raw_output) diff --git a/man/jc.1 b/man/jc.1 index 8657bd47..faaf14de 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -367,6 +367,11 @@ M3U and M3U8 file parser \fB--pip-show\fP `pip show` command parser +.TP +.B +\fB--plist\fP +PLIST file parser + .TP .B \fB--postconf\fP diff --git a/tests/fixtures/generic/plist-garageband-info.plist b/tests/fixtures/generic/plist-garageband-info.plist new file mode 100644 index 00000000..9b50a348 Binary files /dev/null and b/tests/fixtures/generic/plist-garageband-info.plist differ diff --git a/tests/fixtures/generic/plist-safari-info.plist b/tests/fixtures/generic/plist-safari-info.plist new file mode 100644 index 00000000..2847c569 --- /dev/null +++ b/tests/fixtures/generic/plist-safari-info.plist @@ -0,0 +1,799 @@ + + + + + ASWebAuthenticationSessionWebBrowserSupportCapabilities + + EphemeralBrowserSessionIsSupported + + IsSupported + + + Application-Group + + dot-mac + InternetAccounts + + BuildMachineOSBuild + 20A241133 + CFBundleDevelopmentRegion + English + CFBundleDisplayName + Safari + CFBundleDocumentTypes + + + CFBundleTypeExtensions + + css + + CFBundleTypeIconFile + css.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeMIMETypes + + text/css + + CFBundleTypeName + CSS style sheet + CFBundleTypeRole + Viewer + LSHandlerRank + Alternate + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + pdf + + CFBundleTypeIconFile + pdf.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeMIMETypes + + application/pdf + + CFBundleTypeName + PDF document + CFBundleTypeRole + Viewer + LSHandlerRank + Alternate + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + webarchive + + CFBundleTypeIconFile + webarchive.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeMIMETypes + + application/x-webarchive + + CFBundleTypeName + Web archive + CFBundleTypeRole + Viewer + ICExtension + ARCHIVE + LSHandlerRank + Default + LSIsAppleDefaultForType + + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + webbookmark + + CFBundleTypeIconFile + webbookmark.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeName + Safari bookmark + CFBundleTypeRole + Viewer + LSHandlerRank + Default + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + webhistory + + CFBundleTypeIconFile + webhistory.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeName + Safari history item + CFBundleTypeRole + Viewer + LSHandlerRank + Default + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + webloc + + CFBundleTypeIconFile + webloc.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeName + Web internet location + CFBundleTypeOSTypes + + ilht + + CFBundleTypeRole + Viewer + LSHandlerRank + Default + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + download + + CFBundleTypeIconFile + download10.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeName + Safari download + CFBundleTypeRole + Editor + LSHandlerRank + Default + LSTypeIsPackage + + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + safariextz + + CFBundleTypeIconFile + safariextz.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeMIMETypes + + application/x-safari-extension + + CFBundleTypeName + Safari extension + CFBundleTypeRole + Viewer + LSHandlerRank + Owner + LSTypeIsPackage + + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + gif + + CFBundleTypeIconFile + gif.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeMIMETypes + + image/gif + + CFBundleTypeName + GIF image + CFBundleTypeOSTypes + + GIFf + + CFBundleTypeRole + Viewer + LSHandlerRank + Alternate + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + html + htm + shtml + jhtml + + CFBundleTypeIconFile + html.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeMIMETypes + + text/html + + CFBundleTypeName + HTML document + CFBundleTypeOSTypes + + HTML + + CFBundleTypeRole + Viewer + ICExtension + HTML + LSHandlerRank + Default + LSIsAppleDefaultForType + + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + js + + CFBundleTypeIconFile + js.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeMIMETypes + + application/x-javascript + + CFBundleTypeName + JavaScript script + CFBundleTypeRole + Viewer + LSHandlerRank + Alternate + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + jpg + jpeg + + CFBundleTypeIconFile + jpeg.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeMIMETypes + + image/jpeg + + CFBundleTypeName + JPEG image + CFBundleTypeOSTypes + + JPEG + + CFBundleTypeRole + Viewer + ICExtension + JPEG + LSHandlerRank + Alternate + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + jp2 + + CFBundleTypeIconFile + jp2.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeMIMETypes + + image/jp2 + + CFBundleTypeName + JPEG 2000 image + CFBundleTypeOSTypes + + jp2 + + CFBundleTypeRole + Viewer + LSHandlerRank + Alternate + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + txt + text + + CFBundleTypeIconFile + txt.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeMIMETypes + + text/plain + + CFBundleTypeName + Plain text document + CFBundleTypeOSTypes + + TEXT + + CFBundleTypeRole + Viewer + ICExtension + TXT + LSHandlerRank + Alternate + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + png + + CFBundleTypeIconFile + png.icns + CFBundleTypeMIMETypes + + image/png + + CFBundleTypeName + PNG image + CFBundleTypeOSTypes + + PNGf + + CFBundleTypeRole + Viewer + LSHandlerRank + Alternate + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + tiff + tif + + CFBundleTypeIconFile + tiff.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeMIMETypes + + image/tiff + + CFBundleTypeName + TIFF image + CFBundleTypeOSTypes + + TIFF + + CFBundleTypeRole + Viewer + ICExtension + TIFF + LSHandlerRank + Alternate + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + url + + CFBundleTypeIconFile + url.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeName + Web site location + CFBundleTypeOSTypes + + LINK + + CFBundleTypeRole + Viewer + LSHandlerRank + Default + LSIsAppleDefaultForType + + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + ico + + CFBundleTypeIconFile + ico.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeMIMETypes + + image/x-icon + + CFBundleTypeName + Windows icon image + CFBundleTypeOSTypes + + ICO + + CFBundleTypeRole + Viewer + LSHandlerRank + Alternate + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + xhtml + xht + xhtm + xht + + CFBundleTypeIconFile + xhtml.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeMIMETypes + + application/xhtml+xml + + CFBundleTypeName + XHTML document + CFBundleTypeRole + Viewer + ICExtension + XHTML + LSHandlerRank + Default + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + xml + xbl + xsl + xslt + + CFBundleTypeIconFile + xml.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeMIMETypes + + application/xml + text/xml + + CFBundleTypeName + XML document + CFBundleTypeRole + Viewer + ICExtension + XML + LSHandlerRank + Default + NSDocumentClass + BrowserDocument + + + CFBundleTypeExtensions + + svg + + CFBundleTypeIconFile + svg.icns + CFBundleTypeIconSystemGenerated + YES + CFBundleTypeMIMETypes + + image/svg+xml + + CFBundleTypeName + SVG document + CFBundleTypeRole + Viewer + LSHandlerRank + Alternate + NSDocumentClass + BrowserDocument + + + CFBundleExecutable + Safari + CFBundleGetInfoString + 15.6, Copyright © 2003-2022 Apple Inc. + CFBundleHelpBookFolder + Safari.help + CFBundleHelpBookName + com.apple.Safari.help + CFBundleIconFile + AppIcon + CFBundleIconName + AppIcon + CFBundleIdentifier + com.apple.Safari + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + Safari + CFBundlePackageType + APPL + CFBundleShortVersionString + 15.6 + CFBundleSignature + sfri + CFBundleSupportedPlatforms + + MacOSX + + CFBundleURLTypes + + + CFBundleURLName + Web site URL + CFBundleURLSchemes + + http + https + + LSHandlerRank + Default + LSIsAppleDefaultForScheme + + + + CFBundleURLName + Local file URL + CFBundleURLSchemes + + file + + LSHandlerRank + Default + + + CFBundleURLSchemes + + prefs + + LSHandlerRank + None + + + CFBundleVersion + 17613.3.9.1.5 + DTCompiler + com.apple.compilers.llvm.clang.1_0 + DTPlatformBuild + 21G66 + DTPlatformName + macosx + DTPlatformVersion + 12.5 + DTSDKBuild + 21G66 + DTSDKName + macosx12.5.internal + DTXcode + 1330 + DTXcodeBuild + 13E6049a + HPDHelpProjectIdentifier + safari + LSApplicationCategoryType + public.app-category.productivity + LSFileQuarantineEnabled + + LSMinimumSystemVersion + 12.5.0 + NSAppleScriptEnabled + Yes + NSExtensionSDK + + com.apple.Safari.web-extension + + NSExtension + + NSExtensionAttributes + + NSExtensionPointName + com.apple.Safari.web-extension + NSExtensionPointVersion + 1.0 + + NSExtensionHostEntitlement + com.apple.private.can-load-any-content-blocker + NSExtensionProtocol + NSObject + PrincipalClass + NSObject + Subsystems + + NSSharingService_Subsystem + + + XPCService + + JoinExistingSession + + RunLoopType + NSRunLoop + ServiceType + Application + _AdditionalSubServices + + apple-extension-service + + + + + + NSLocationUsageDescription + Websites you visit may request your location. + NSMainNibFile + MainMenu + NSPrincipalClass + BrowserApplication + NSServices + + + NSKeyEquivalent + + default + L + + NSMenuItem + + default + Search With %WebSearchProvider@ + + NSMessage + searchWithWebSearchProvider + NSPortName + Safari + NSSendTypes + + public.utf8-plain-text + + + + NSMenuItem + + default + Add to Reading List + + NSMessage + addToReadingList + NSPortName + Safari + NSRequiredContext + + + NSTextContent + URL + + + NSLinkSchemes + + http + https + + + + NSSendTypes + + public.rtf + public.utf8-plain-text + + + + NSSpeechRecognitionUsageDescription + Websites you visit may access speech recognition. + NSSupportsAutomaticTermination + + NSSupportsSuddenTermination + + NSUserActivityTypes + + NSUserActivityTypeBrowsingWeb + + OSAScriptingDefinition + Safari.sdef + UTExportedTypeDeclarations + + + UTTypeConformsTo + + public.data + + UTTypeDescription + Safari bookmark + UTTypeIdentifier + com.apple.safari.bookmark + UTTypeTagSpecification + + public.filename-extension + + webbookmark + + + + + UTTypeConformsTo + + public.data + + UTTypeDescription + Safari extension + UTTypeIdentifier + com.apple.safari.extension + UTTypeTagSpecification + + public.filename-extension + + safariextz + + + + + UTTypeConformsTo + + public.data + + UTTypeDescription + Safari history item + UTTypeIdentifier + com.apple.safari.history + UTTypeTagSpecification + + public.filename-extension + + webhistory + + + + + +