various improvements
This commit is contained in:
parent
1dd3e9a720
commit
64bdbbd97b
25
fdict.py
25
fdict.py
@ -1,25 +0,0 @@
|
||||
from collections import UserDict
|
||||
from typing import Any
|
||||
|
||||
class _NotPresent:
|
||||
"""
|
||||
Type for representing non-existent dictionary keys.
|
||||
|
||||
See :class:`_FillableDict`
|
||||
"""
|
||||
|
||||
class _FillableDict(UserDict):
|
||||
"""Use :method:`fill` to add key if not present."""
|
||||
|
||||
def __init__(self, data: dict):
|
||||
self.data = data
|
||||
|
||||
def fill(self, key: str, value: Any) -> None:
|
||||
if self.data.get(key, _NotPresent) is _NotPresent:
|
||||
self.data[key] = value
|
||||
|
||||
d = _FillableDict({'a': None, 'b': 'BBBB'})
|
||||
d.fill('c', 'CCCCCCCCC')
|
||||
d.fill('a', 'CCCCCCCCC')
|
||||
d['a'].fill('gg', 'AAAAAAAA')
|
||||
print(d)
|
@ -1,10 +0,0 @@
|
||||
title: dev-1
|
||||
vcpus: 4
|
||||
memory: 4096
|
||||
volumes:
|
||||
- is_system: true
|
||||
type: file
|
||||
target: vda
|
||||
capacity:
|
||||
value: 5
|
||||
unit: GiB
|
34
pars.py
34
pars.py
@ -1,34 +0,0 @@
|
||||
import re
|
||||
|
||||
|
||||
def _split_unit(val: str) -> dict | None:
|
||||
match = re.match(r'([0-9]+)([a-z]+)', val, re.I)
|
||||
if match:
|
||||
return {
|
||||
'value': match.groups()[0],
|
||||
'unit': match.groups()[1],
|
||||
}
|
||||
return None
|
||||
|
||||
|
||||
def _parse_complex_arg(arg: str) -> dict:
|
||||
# key=value --> {'key': 'value'}
|
||||
if re.match(r'.+=.+', arg):
|
||||
key, val = arg.split('=')
|
||||
# system --> {'is_system': True}
|
||||
# ro --> {'is_readonly': True}
|
||||
elif re.match(r'^[a-z0-9_\.\-]+$', arg, re.I):
|
||||
key = 'is_' + arg.replace('ro', 'readonly')
|
||||
val = True
|
||||
else:
|
||||
raise ValueError('Invalid argument pattern')
|
||||
# key=15GiB --> {'key': {'value': 15, 'unit': 'GiB'}}
|
||||
if not isinstance(val, bool):
|
||||
val = _split_unit(val) or val
|
||||
return {key: val}
|
||||
|
||||
|
||||
print(_parse_complex_arg('source=/volumes/50c4410b-2ef0-4ffd-a2e5-04f0212772d4.qcow2'))
|
||||
print(_parse_complex_arg('capacity=15GiB'))
|
||||
print(_parse_complex_arg('system'))
|
||||
print(_parse_complex_arg('cpu.cores=8'))
|
Loading…
Reference in New Issue
Block a user