Browse Source

make type generate if not present, make FileObject subclass MDBase

main
John-Mark Gurney 5 years ago
parent
commit
96b90c059f
1 changed files with 29 additions and 49 deletions
  1. +29
    -49
      ui/cli.py

+ 29
- 49
ui/cli.py View File

@@ -27,10 +27,13 @@ class MDBase(object):
'uuid': uuid.uuid4,
'modified': datetime.datetime.utcnow
}
_common_properties = [ 'type', 'created_by_ref' ]
_common_properties = [ 'created_by_ref' ]

def __init__(self, obj):
obj = copy.deepcopy(obj)
if 'type' not in obj:
obj['type'] = self._type

for x in self._common_properties:
if x not in obj:
raise ValueError('common property %s not present' % `x`)
@@ -166,62 +169,35 @@ def _hashfile(fname):

return '%s:%s' % (_defaulthash, hash.hexdigest())

class FileObject(object):
def __init__(self, _dir, filename):
self._dir = os.path.realpath(_dir)
self._fname = filename
class FileObject(MDBase):
_type = 'file'

# XXX make sure this is correct
self._id = uuid.uuid5(_NAMESPACE_MEDASHARE_PATH,
'/'.join(os.path.split(self._dir) + ( self._fname, )))
@classmethod
def from_file(cls, _dir, filename, created_by_ref):
_dir = os.path.realpath(_dir)
fname = os.path.join(_dir, filename)
s = os.stat(fname)
self._mtime = datetime.datetime.utcfromtimestamp(s.st_mtime)
self._size = s.st_size
self._hashes = ( _hashfile(fname), )

@property
def hashes(self):
'''The hashes for this file.'''

# XXX - should return a frozen dict
return self._hashes

@property
def mtime(self):
'''The last modified date of the file.'''

return self._mtime

@property
def size(self):
'''The length of the file in bytes.'''

return self._size
@property
def filename(self):
'''The name of the file.'''

return self._fname

@property
def dir(self):
'''The directory of the file.'''

return self._dir
obj = {
'dir': _dir,
'created_by_ref': created_by_ref,
'filename': filename,
'id': uuid.uuid5(_NAMESPACE_MEDASHARE_PATH,
'/'.join(os.path.split(fname))),
'mtime': datetime.datetime.utcfromtimestamp(s.st_mtime),
'size': s.st_size,
'hashes': ( _hashfile(fname), ),
}

@property
def id(self):
'''The UUID of the path to this file.'''

return self._id
return cls(obj)

def enumeratedir(_dir='.'):
def enumeratedir(_dir, created_by_ref):
'''Enumerate all the files and directories (not recursive) in _dir.

Returned is a list of FileObjects.'''

return map(lambda x: FileObject(_dir, x), os.listdir(_dir))
return map(lambda x: FileObject.from_file(_dir, x, created_by_ref),
os.listdir(_dir))

class _TestCases(unittest.TestCase):
def setUp(self):
@@ -257,10 +233,13 @@ class _TestCases(unittest.TestCase):
self.assertEqual(ObjectStore.makehash('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', strict=False), 'sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')

def test_enumeratedir(self):
files = enumeratedir(self.tempdir)
files = enumeratedir(self.tempdir, '867c7563-79ae-435c-a265-9d8509cefac5')
ftest = files[0]
fname = 'test.txt'

# make sure that they are of type MDBase
self.assertIsInstance(ftest, MDBase)

oldid = ftest.id
self.assertEqual(ftest.filename, fname)
self.assertEqual(ftest.dir, self.tempdir)
@@ -273,7 +252,8 @@ class _TestCases(unittest.TestCase):
self.assertIn('sha512:7d5768d47b6bc27dc4fa7e9732cfa2de506ca262a2749cb108923e5dddffde842bbfee6cb8d692fb43aca0f12946c521cce2633887914ca1f96898478d10ad3f', ftest.hashes)

# XXX - make sure works w/ relative dirs
files = enumeratedir(os.path.relpath(self.tempdir))
files = enumeratedir(os.path.relpath(self.tempdir),
'867c7563-79ae-435c-a265-9d8509cefac5')
self.assertEqual(oldid, files[0].id)

def test_objectstore(self):


Loading…
Cancel
Save