-
Notifications
You must be signed in to change notification settings - Fork 4
/
edit_core.py
57 lines (42 loc) · 1.67 KB
/
edit_core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Core interface for editing an item."""
import datetime
import logging
import sqlalchemy.ext.associationproxy
from moe.library import LibItem
__all__ = ["EditError", "edit_item"]
log = logging.getLogger("moe.edit")
class EditError(Exception):
"""Error editing an item in the library."""
def edit_item(item: LibItem, field: str, value: str):
"""Sets a LibItem's ``field`` to ``value``.
Args:
item: Library item to edit.
field: Item field to edit.
value: Value to set the item's field to.
Raises:
EditError: ``field`` is not a valid attribute or is not editable.
"""
log.debug(f"Editing item. [{item=!r}, {field=!r}, {value=!r}]")
try:
attr = getattr(item, field)
except AttributeError as a_err:
raise EditError(f"Invalid field given. [{field=!r}]") from a_err
non_editable_fields = ["path"]
if field in non_editable_fields:
raise EditError(f"Non-editable field given. [{field=!r}]")
if isinstance(attr, str):
setattr(item, field, value)
elif isinstance(attr, int):
setattr(item, field, int(value))
elif isinstance(attr, set) or isinstance(
attr, sqlalchemy.ext.associationproxy._AssociationSet
):
setattr(item, field, {value.strip() for value in value.split(";")})
elif isinstance(attr, datetime.date):
try:
setattr(item, field, datetime.date.fromisoformat(value))
except ValueError as v_err:
raise EditError("Date must be in format YYYY-MM-DD") from v_err
else:
raise EditError(f"Editing field not supported. [{field=!r}]")
log.info(f"Item edited. [{item=!r}, {field=!r}, {value=!r}]")