1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

add remove_quotes() to utils and use in ini parsers

This commit is contained in:
Kelly Brazil
2024-02-01 21:02:15 -08:00
parent 264272a8b8
commit c05fa7e9cd
7 changed files with 54 additions and 31 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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.
<a id="jc.utils.remove_quotes"></a>
### 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
<a id="jc.utils.convert_to_int"></a>
### convert\_to\_int

View File

@ -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

View File

@ -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

View File

@ -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