Skip to content

Commit

Permalink
Add .state property to file tuples returned from JFSFileDirList(), an…
Browse files Browse the repository at this point in the history
…d revamp logic to be more resilient to unexpected file objects. See #88
  • Loading branch information
havardgulldahl committed Jun 25, 2016
1 parent 84f6322 commit 6c844dd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
41 changes: 32 additions & 9 deletions src/jottalib/JFS.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,24 +148,35 @@ class JFSServerError(JFSError): # HTTP 500


class JFSFileDirList(object):
'Wrapping <filedirlist>, a simple tree of folders and their files'
"""get a <filedirlist> for any jottafolder by appending ?mode=list to your query
'''Wrapping <filedirlist>, a simple tree of folders and their files
Get a <filedirlist> for any jottafolder by appending ?mode=list to your query
Then you will get this object, with a .tree property, which is a list of all
files and folders.
Files will be a namedtuple() with five properties:
.name - file name
.state - file state (str): one of JFS.ProtoFile.STATE_*, e.g 'COMPLETED' or 'INCOMPLETE'
.size - file size (int or None): full size , partially uploaded size or no size, depentant on file state
.md5 - jottacloud file hash (str or None): corrupt files have no md5 hash
.uuid - jottacloud assigned uuid
<filedirlist time="2015-05-28-T18:57:06Z" host="dn-093.site-000.jotta.no">
<folders>
<folder name="Sync">
<path xml:space="preserve">/havardgulldahl/Jotta</path>
<abspath xml:space="preserve">/havardgulldahl/Jotta</abspath>
<files>
<file>..."""
<file>...'''


def __init__(self, filedirlistobject, jfs, parentpath): # filedirlistobject from lxml.objectify
self.filedirlist = filedirlistobject
self.parentPath = parentpath
self.jfs = jfs

treefile = namedtuple('TreeFile', 'name size md5 uuid')
treefile = namedtuple('TreeFile', 'name size md5 uuid state')

self.tree = {}
for folder in self.filedirlist.folders.iterchildren():
Expand All @@ -178,15 +189,27 @@ def __init__(self, filedirlistobject, jfs, parentpath): # filedirlistobject from
t.append(treefile(unicode(file_.attrib['name']),
int(file_.currentRevision.size),
unicode(file_.currentRevision.md5),
unicode(file_.attrib['uuid'])
unicode(file_.attrib['uuid']),
unicode(file_.currentRevision.state)
)
)
else:
# an incomplete file
# This is an incomplete or, possibly, corrupt file
#
# Incomplete files have no `size` in a filedirlist, you
# need to fetch the JFSFile explicitly to see that property
try:
# incomplete files carry a md5 hash,
_md5 = unicode(file_.latestRevision.md5)
except AttributeError:
# while other may not
# see discussion in #88
_md5 = None
t.append(treefile(unicode(file_.attrib['name']),
-1, # incomplete files have no size
unicode(file_.latestRevision.md5),
unicode(file_.attrib['uuid'])
None, # return size as None
_md5,
unicode(file_.attrib['uuid']),
unicode(file_.latestRevision.state)
)
)
self.tree[posixpath.join(path, foldername)] = t
Expand Down
10 changes: 9 additions & 1 deletion tests/test_JFS.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
__author__ = '[email protected]'

# import standardlib
import os, logging, datetime
import os, logging, datetime, types
import tempfile, posixpath, urllib
import six
from six.moves import cStringIO as StringIO
Expand Down Expand Up @@ -462,6 +462,14 @@ def test_api(self):
fdl = jfs.getObject('/Jotta/Sync/?mode=list')
assert isinstance(fdl, JFS.JFSFileDirList)
assert len(fdl.tree) > 0
i = 0
for folders in fdl.tree.values(): # iterate over all files in tree list
for f in folders:
assert isinstance(f.name, unicode)
assert isinstance(f.md5, (basestring, types.NoneType))
assert isinstance(f.state, basestring)
assert isinstance(f.uuid, basestring)
assert isinstance(f.size, (int, types.NoneType))

class TestJFSError:
'Test different JFSErrors'
Expand Down

0 comments on commit 6c844dd

Please sign in to comment.