Skip to content

Commit 15f78b8

Browse files
committed
added a function decorator to validate card objects in API helpers
1 parent 5c79881 commit 15f78b8

File tree

7 files changed

+49
-66
lines changed

7 files changed

+49
-66
lines changed

notecard/card.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
Notecard API commands.
55
"""
66
import notecard
7+
from .validators import validate_card_object
78

89

10+
@validate_card_object
911
def attn(card, mode=None, files=None, seconds=None):
1012
"""Configure interrupt detection between a host and Notecard.
1113
@@ -17,9 +19,6 @@ def attn(card, mode=None, files=None, seconds=None):
1719
Returns:
1820
string: The result of the Notecard request.
1921
"""
20-
if not isinstance(card, notecard.Notecard):
21-
raise Exception("Notecard object required")
22-
2322
req = {"req": "card.attn"}
2423
if mode:
2524
req["mode"] = mode
@@ -30,58 +29,51 @@ def attn(card, mode=None, files=None, seconds=None):
3029
return card.Transaction(req)
3130

3231

32+
@validate_card_object
3333
def time(card):
3434
"""Retrieve the current time and date from the Notecard.
3535
3636
Returns:
3737
string: The result of the Notecard request.
3838
"""
39-
if not isinstance(card, notecard.Notecard):
40-
raise Exception("Notecard object required")
41-
4239
req = {"req": "card.time"}
4340
return card.Transaction(req)
4441

4542

43+
@validate_card_object
4644
def status(card):
4745
"""Retrieve the status of the Notecard.
4846
4947
Returns:
5048
string: The result of the Notecard request.
5149
"""
52-
if not isinstance(card, notecard.Notecard):
53-
raise Exception("Notecard object required")
54-
5550
req = {"req": "card.status"}
5651
return card.Transaction(req)
5752

5853

54+
@validate_card_object
5955
def temp(card):
6056
"""Retrieve the current temperature from the Notecard.
6157
6258
Returns:
6359
string: The result of the Notecard request.
6460
"""
65-
if not isinstance(card, notecard.Notecard):
66-
raise Exception("Notecard object required")
67-
6861
req = {"req": "card.temp"}
6962
return card.Transaction(req)
7063

7164

65+
@validate_card_object
7266
def version(card):
7367
"""Retrieve firmware version] information from the Notecard.
7468
7569
Returns:
7670
string: The result of the Notecard request.
7771
"""
78-
if not isinstance(card, notecard.Notecard):
79-
raise Exception("Notecard object required")
80-
8172
req = {"req": "card.version"}
8273
return card.Transaction(req)
8374

8475

76+
@validate_card_object
8577
def voltage(card, hours=None, offset=None, vmax=None, vmin=None):
8678
"""Retrive current and historical voltage info from the Notecard.
8779
@@ -94,9 +86,6 @@ def voltage(card, hours=None, offset=None, vmax=None, vmin=None):
9486
Returns:
9587
string: The result of the Notecard request.
9688
"""
97-
if not isinstance(card, notecard.Notecard):
98-
raise Exception("Notecard object required")
99-
10089
req = {"req": "card.voltage"}
10190
if hours:
10291
req["hours"] = hours
@@ -109,6 +98,7 @@ def voltage(card, hours=None, offset=None, vmax=None, vmin=None):
10998
return card.Transaction(req)
11099

111100

101+
@validate_card_object
112102
def wireless(card, mode=None):
113103
"""Retrive wireless modem info or customize modem behavior.
114104
@@ -118,9 +108,6 @@ def wireless(card, mode=None):
118108
Returns:
119109
string: The result of the Notecard request.
120110
"""
121-
if not isinstance(card, notecard.Notecard):
122-
raise Exception("Notecard object required")
123-
124111
req = {"req": "card.wireless"}
125112
if mode:
126113
req["mode"] = mode

notecard/env.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
Notecard API commands.
55
"""
66
import notecard
7+
from .validators import validate_card_object
78

89

10+
@validate_card_object
911
def get(card, name=None):
1012
"""Perform an env.get request against a Notecard.
1113
@@ -15,9 +17,6 @@ def get(card, name=None):
1517
Returns:
1618
string: The result of the Notecard request.
1719
"""
18-
if not isinstance(card, notecard.Notecard):
19-
raise Exception("Notecard object required")
20-
2120
req = {"req": "env.get"}
2221
if name:
2322
req["name"] = name

notecard/file.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
Notecard API commands.
55
"""
66
import notecard
7+
from .validators import validate_card_object
78

89

10+
@validate_card_object
911
def changes(card, tracker=None, files=None):
1012
"""Perform individual or batch queries on Notefiles.
1113
@@ -16,9 +18,6 @@ def changes(card, tracker=None, files=None):
1618
Returns:
1719
string: The result of the Notecard request.
1820
"""
19-
if not isinstance(card, notecard.Notecard):
20-
raise Exception("Notecard object required")
21-
2221
req = {"req": "file.changes"}
2322
if tracker:
2423
req["tracker"] = tracker
@@ -27,6 +26,7 @@ def changes(card, tracker=None, files=None):
2726
return card.Transaction(req)
2827

2928

29+
@validate_card_object
3030
def delete(card, files=None):
3131
"""Delete individual notefiles and their contents.
3232
@@ -36,38 +36,31 @@ def delete(card, files=None):
3636
Returns:
3737
string: The result of the Notecard request.
3838
"""
39-
if not isinstance(card, notecard.Notecard):
40-
raise Exception("Notecard object required")
41-
4239
req = {"req": "file.delete"}
4340
if files:
4441
req["files"] = files
4542
return card.Transaction(req)
4643

4744

45+
@validate_card_object
4846
def stats(card):
4947
"""Obtain statistics about local notefiles.
5048
5149
Returns:
5250
string: The result of the Notecard request.
5351
"""
54-
if not isinstance(card, notecard.Notecard):
55-
raise Exception("Notecard object required")
56-
5752
req = {"req": "file.stats"}
5853

5954
return card.Transaction(req)
6055

6156

57+
@validate_card_object
6258
def pendingChanges(card):
6359
"""Retrive information about pending Notehub changes.
6460
6561
Returns:
6662
string: The result of the Notecard request.
6763
"""
68-
if not isinstance(card, notecard.Notecard):
69-
raise Exception("Notecard object required")
70-
7164
req = {"req": "file.changes.pending"}
7265

7366
return card.Transaction(req)

notecard/hub.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
Notecard API commands.
55
"""
66
import notecard
7+
from .validators import validate_card_object
78

89

10+
@validate_card_object
911
def set(card, product, sn=None,
1012
mode=None, minutes=None, hours=None, sync=False):
1113
"""Configure Notehub behavior on the Notecard.
@@ -22,9 +24,6 @@ def set(card, product, sn=None,
2224
Returns:
2325
string: The result of the Notecard request.
2426
"""
25-
if not isinstance(card, notecard.Notecard):
26-
raise Exception("Notecard object required")
27-
2827
req = {"req": "hub.set"}
2928
if product:
3029
req["product"] = product
@@ -42,45 +41,40 @@ def set(card, product, sn=None,
4241
return card.Transaction(req)
4342

4443

44+
@validate_card_object
4545
def sync(card):
4646
"""Initiate a sync of the Notecard to Notehub.
4747
4848
Returns:
4949
string: The result of the Notecard request.
5050
"""
51-
if not isinstance(card, notecard.Notecard):
52-
raise Exception("Notecard object required")
53-
5451
req = {"req": "hub.sync"}
5552
return card.Transaction(req)
5653

5754

55+
@validate_card_object
5856
def syncStatus(card):
5957
"""Retrive the status of a sync request.
6058
6159
Returns:
6260
string: The result of the Notecard request.
6361
"""
64-
if not isinstance(card, notecard.Notecard):
65-
raise Exception("Notecard object required")
66-
6762
req = {"req": "hub.sync.status"}
6863
return card.Transaction(req)
6964

7065

66+
@validate_card_object
7167
def status(card):
7268
"""Retrieve the status of the Notecard's connection.
7369
7470
Returns:
7571
string: The result of the Notecard request.
7672
"""
77-
if not isinstance(card, notecard.Notecard):
78-
raise Exception("Notecard object required")
79-
8073
req = {"req": "hub.status"}
8174
return card.Transaction(req)
8275

8376

77+
@validate_card_object
8478
def log(card, text, sync=False):
8579
"""Send a log request to the Notecard.
8680
@@ -91,23 +85,18 @@ def log(card, text, sync=False):
9185
Returns:
9286
string: The result of the Notecard request.
9387
"""
94-
if not isinstance(card, notecard.Notecard):
95-
raise Exception("Notecard object required")
96-
9788
req = {"req": "hub.log"}
9889
req["text"] = text
9990
req["sync"] = sync
10091
return card.Transaction(req)
10192

10293

94+
@validate_card_object
10395
def get(card):
10496
"""Retrive the current Notehub configuration parameters.
10597
10698
Returns:
10799
string: The result of the Notecard request.
108100
"""
109-
if not isinstance(card, notecard.Notecard):
110-
raise Exception("Notecard object required")
111-
112101
req = {"req": "hub.get"}
113102
return card.Transaction(req)

notecard/note.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
Notecard API commands.
55
"""
66
import notecard
7+
from .validators import validate_card_object
78

89

10+
@validate_card_object
911
def changes(card, file=None, tracker=None, max=None,
1012
start=None, stop=None, deleted=None, delete=None):
1113
"""Incrementally retrieve changes within a Notefile.
@@ -23,9 +25,6 @@ def changes(card, file=None, tracker=None, max=None,
2325
Returns:
2426
string: The result of the Notecard request.
2527
"""
26-
if not isinstance(card, notecard.Notecard):
27-
raise Exception("Notecard object required")
28-
2928
req = {"req": "note.changes"}
3029
if file:
3130
req["file"] = file
@@ -44,6 +43,7 @@ def changes(card, file=None, tracker=None, max=None,
4443
return card.Transaction(req)
4544

4645

46+
@validate_card_object
4747
def get(card, file="data.qi", note_id=None, delete=None, deleted=None):
4848
"""Retrive a note from an inbound or DB Notefile.
4949
@@ -58,9 +58,6 @@ def get(card, file="data.qi", note_id=None, delete=None, deleted=None):
5858
Returns:
5959
string: The result of the Notecard request.
6060
"""
61-
if not isinstance(card, notecard.Notecard):
62-
raise Exception("Notecard object required")
63-
6461
req = {"req": "note.get"}
6562
req["file"] = file
6663
if note_id:
@@ -72,6 +69,7 @@ def get(card, file="data.qi", note_id=None, delete=None, deleted=None):
7269
return card.Transaction(req)
7370

7471

72+
@validate_card_object
7573
def delete(card, file=None, note_id=None):
7674
"""Delete a DB note in a Notefile by its ID.
7775
@@ -82,9 +80,6 @@ def delete(card, file=None, note_id=None):
8280
Returns:
8381
string: The result of the Notecard request.
8482
"""
85-
if not isinstance(card, notecard.Notecard):
86-
raise Exception("Notecard object required")
87-
8883
req = {"req": "note.delete"}
8984
if file:
9085
req["file"] = file
@@ -93,6 +88,7 @@ def delete(card, file=None, note_id=None):
9388
return card.Transaction(req)
9489

9590

91+
@validate_card_object
9692
def update(card, file=None, note_id=None, body=None, payload=None):
9793
"""Update a note in a DB Notefile by ID.
9894
@@ -106,9 +102,6 @@ def update(card, file=None, note_id=None, body=None, payload=None):
106102
Returns:
107103
string: The result of the Notecard request.
108104
"""
109-
if not isinstance(card, notecard.Notecard):
110-
raise Exception("Notecard object required")
111-
112105
req = {"req": "note.get"}
113106
if file:
114107
req["file"] = file

notecard/validators.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Validation decorators for note-python."""
2+
import functools
3+
import notecard
4+
5+
6+
def validate_card_object(func):
7+
"""Ensure that the passed-in card is a Notecard."""
8+
@functools.wraps(func)
9+
def wrap_validator(*args, **kwargs):
10+
"""Check the instance of the passed-in card."""
11+
card = args[0]
12+
if not isinstance(card, notecard.Notecard):
13+
raise Exception("Notecard object required")
14+
15+
return func(*args, **kwargs)
16+
17+
return wrap_validator

0 commit comments

Comments
 (0)