diff --git a/CHANGELOG b/CHANGELOG index 864eb160..0ec74209 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ jc changelog -20240131 v1.24.1 +20240201 v1.24.1 - Add `--slurp` functionality to wrap output from multiple lines into a single array. Note, this only works with single-line input parsers. (e.g. `date`, `ip-address`, `url`, etc.) Streaming parsers are not supported. Use `jc -hhh` to find parsers compatible with the slurp option. @@ -16,6 +16,7 @@ jc changelog - Add `path-list` string parser to parse path list strings found in env variables - Add source link to online parser documentation - Add snap package build scripts +- Add `remove_quotes` function to `utils.py` - Add `line_slice` function to `utils.py` - Add `get_parser` function to `lib.py` - Enhance `nsd-control` parser to support more zone information diff --git a/docs/parsers/ini.md b/docs/parsers/ini.md index 3c0d2aa6..d5976a20 100644 --- a/docs/parsers/ini.md +++ b/docs/parsers/ini.md @@ -98,4 +98,4 @@ Compatibility: linux, darwin, cygwin, win32, aix, freebsd Source: [`jc/parsers/ini.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/ini.py) -Version 2.1 by Kelly Brazil (kellyjonbrazil@gmail.com) +Version 2.2 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/docs/parsers/ini_dup.md b/docs/parsers/ini_dup.md index a4a74d83..98a1f237 100644 --- a/docs/parsers/ini_dup.md +++ b/docs/parsers/ini_dup.md @@ -120,4 +120,4 @@ Compatibility: linux, darwin, cygwin, win32, aix, freebsd Source: [`jc/parsers/ini_dup.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/ini_dup.py) -Version 1.2 by Kelly Brazil (kellyjonbrazil@gmail.com) +Version 1.3 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/docs/utils.md b/docs/utils.md index 51ce2ee5..56173cea 100644 --- a/docs/utils.md +++ b/docs/utils.md @@ -6,6 +6,7 @@ * [is\_compatible](#jc.utils.is_compatible) * [compatibility](#jc.utils.compatibility) * [has\_data](#jc.utils.has_data) + * [remove\_quotes](#jc.utils.remove_quotes) * [convert\_to\_int](#jc.utils.convert_to_int) * [convert\_to\_float](#jc.utils.convert_to_float) * [convert\_to\_bool](#jc.utils.convert_to_bool) @@ -122,6 +123,25 @@ Returns: characters, otherwise False. For bytes data, returns True if there is any data, otherwise False. + + +### remove\_quotes + +```python +def remove_quotes(data: str) -> str +``` + +Remove single or double quotes surrounding a string. If no quotes are +found then the string is returned unmodified. + +Parameters: + + data: (string) Input value + +Returns: + + string + ### convert\_to\_int diff --git a/jc/parsers/ini.py b/jc/parsers/ini.py index 73d30b15..08a3e05e 100644 --- a/jc/parsers/ini.py +++ b/jc/parsers/ini.py @@ -75,7 +75,7 @@ import uuid class info(): """Provides parser metadata (version, author, etc.)""" - version = '2.1' + version = '2.2' description = 'INI file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -97,16 +97,6 @@ class MyDict(dict): super().__setitem__(key, value) -def _remove_quotes(value): - if value.startswith('"') and value.endswith('"'): - value = value[1:-1] - - elif value.startswith("'") and value.endswith("'"): - value = value[1:-1] - - return value - - def _process(proc_data): """ Final processing to conform to the schema. @@ -123,10 +113,10 @@ def _process(proc_data): for k, v in proc_data.items(): if isinstance(v, dict): for key, value in v.items(): - v[key] = _remove_quotes(value) + v[key] = jc.utils.remove_quotes(value) continue - proc_data[k] = _remove_quotes(v) + proc_data[k] = jc.utils.remove_quotes(v) return proc_data diff --git a/jc/parsers/ini_dup.py b/jc/parsers/ini_dup.py index 1d669db8..dff0ef39 100644 --- a/jc/parsers/ini_dup.py +++ b/jc/parsers/ini_dup.py @@ -97,7 +97,7 @@ import uuid class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.2' + version = '1.3' description = 'INI with duplicate key file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -127,16 +127,6 @@ class MultiDict(dict): super().__setitem__(key, value) -def _remove_quotes(value): - if value.startswith('"') and value.endswith('"'): - value = value[1:-1] - - elif value.startswith("'") and value.endswith("'"): - value = value[1:-1] - - return value - - def _process(proc_data): """ Final processing to conform to the schema. @@ -154,16 +144,16 @@ def _process(proc_data): if isinstance(v, dict): for key, value in v.items(): if isinstance(value, list): - v[key] = [_remove_quotes(x) for x in value] + v[key] = [jc.utils.remove_quotes(x) for x in value] else: - v[key] = _remove_quotes(value) + v[key] = jc.utils.remove_quotes(value) continue elif isinstance(v, list): - proc_data[k] = [_remove_quotes(x) for x in v] + proc_data[k] = [jc.utils.remove_quotes(x) for x in v] else: - proc_data[k] = _remove_quotes(v) + proc_data[k] = jc.utils.remove_quotes(v) return proc_data diff --git a/jc/utils.py b/jc/utils.py index 0d679ded..fe3736bd 100644 --- a/jc/utils.py +++ b/jc/utils.py @@ -183,6 +183,28 @@ def has_data(data: Union[str, bytes]) -> bool: return bool(data) +def remove_quotes(data: str) -> str: + """ + Remove single or double quotes surrounding a string. If no quotes are + found then the string is returned unmodified. + + Parameters: + + data: (string) Input value + + Returns: + + string + """ + if data.startswith('"') and data.endswith('"'): + data = data[1:-1] + + elif data.startswith("'") and data.endswith("'"): + data = data[1:-1] + + return data + + def convert_to_int(value: object) -> Optional[int]: """ Converts string and float input to int. Strips all non-numeric