Skip to content

Commit

Permalink
Update docstrings, make axon.bytes() throw an exception on missing fi…
Browse files Browse the repository at this point in the history
…les.
  • Loading branch information
vEpiphyte committed Oct 11, 2017
1 parent a2ce090 commit 598cfa7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
46 changes: 38 additions & 8 deletions synapse/axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,18 +820,42 @@ def bytes(self, htype, hvalu):
hvalu (str): Hash value.
Examples:
Write bytes for a md5 to disk::
Get the bytes for a given guid and do stuff with them::
for byts in axon.bytes('md5',md5sum):
for byts in axon.bytes('guid', axonblobguid):
dostuff(byts)
Iteratively write bytes to a file for a given md5sum::
for byts in axon.bytes('md5', md5sum):
fd.write(byts)
Returns:
bytes: A chunk of bytes
Form a contiguous bytes object for a given sha512sum. This is not recommended for large files.::
byts = b''.join((_byts for _byts in axon.bytes('sha512', sha512sum)))
Notes:
This API will raise an exception to the caller if the requested
hash is not present in the axon. This is contrasted against the
Axon.iterblob() API, which first requires the caller to first
obtain an axon:blob tufo in order to start retrieving bytes from
the axon.
Yields:
bytes: A chunk of bytes for a given hash.
Raises:
NoSuchFile: If the requested hash is not present in the axon. This
is raised when the generator is first consumed.
'''
blob = self.has(htype, hvalu)
if blob:
for byts in self.iterblob(blob):
yield byts
else:
raise s_common.NoSuchFile(mesg='The requested blob was not found.',
htype=htype, hvalu=hvalu)

def iterblob(self, blob):
'''
Expand All @@ -841,14 +865,20 @@ def iterblob(self, blob):
blob ((str, dict)): axon:blob tufo to yield bytes from.
Examples:
Get the bytes from a blob::
Get the bytes from a blob and do stuff with them::
for byts in axon.iterAxonBlob(blob):
for byts in axon.iterblob(blob):
dostuff(byts)
Form a contiguous bytes object for a given blob::
Iteratively write bytes to a file for a given blob::
fd = file('foo.bin','wb')
for byts in axon.iterblob(blob):
fd.write(byts)
Form a contiguous bytes object for a given blob. This is not recommended for large files.::
byts = b''.join((_byts for _bytes in eaxon.iterAxonBlob(blob)))
byts = b''.join((_byts for _byts in axon.iterblob(blob)))
Yields:
bytes: A chunk of bytes
Expand Down
2 changes: 1 addition & 1 deletion synapse/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class IsRuntProp(SynErr): pass
class NoSuch(Exception): pass
class NoSuchJob(Exception): pass
class NoSuchObj(SynErr): pass
class NoSuchFile(Exception): pass
class NoSuchFile(SynErr): pass
class NoSuchIden(Exception): pass
class NoSuchMeth(SynErr): pass
class NoSuchFunc(SynErr): pass
Expand Down
3 changes: 1 addition & 2 deletions synapse/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ def skipIfNoInternet(self):
raise unittest.SkipTest('SYN_TEST_SKIP_INTERNET envar set')

def skipLongTest(self):
valu = os.getenv('SYN_TEST_SKIP_LONG', 0)
if bool(int(valu)):
if bool(int(os.getenv('SYN_TEST_SKIP_LONG', 0))):
raise unittest.SkipTest('SYN_TEST_SKIP_LONG envar set')

def getPgConn(self):
Expand Down
3 changes: 3 additions & 0 deletions synapse/tests/test_axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def test_axon_basics(self):
self.none(axon.wants('md5', asdfhash, 8))
self.nn(axon.wants('md5', craphash, 8))

with self.assertRaises(NoSuchFile):
_ = [byts for byts in axon.bytes('md5', craphash)]

def test_axon_restrictions(self):
with self.getTestDir() as axondir:
with s_axon.Axon(axondir) as axon:
Expand Down

0 comments on commit 598cfa7

Please sign in to comment.