terrasync.py: rename DirIndex attributes and remove accessors
In Python, common usage is not to define accessors, but to directly use class or instance attributes (especially when the associated data is constant after instance creation). If it later happens that a given attribute needs getter or setter logic, this can always be done via the @property decorator, and doesn't affect calling code at all. See for instance: https://docs.python.org/3/library/functions.html#property https://mail.python.org/pipermail/tutor/2012-December/thread.html#92990 Apply this to the DirIndex class and rename the following attributes for better readability: f -> files, d -> directories, t -> tarballs.
This commit is contained in:
parent
477d9f7a9a
commit
13f943b4a1
3 changed files with 19 additions and 60 deletions
|
@ -28,9 +28,9 @@ class DirIndex:
|
||||||
"""Parser for .dirindex files."""
|
"""Parser for .dirindex files."""
|
||||||
|
|
||||||
def __init__(self, dirIndexFile):
|
def __init__(self, dirIndexFile):
|
||||||
self.d = []
|
self.directories = []
|
||||||
self.f = []
|
self.files = []
|
||||||
self.t = []
|
self.tarballs = []
|
||||||
self.version = 0
|
self.version = 0
|
||||||
self.path = None # will be a VirtualPath instance when set
|
self.path = None # will be a VirtualPath instance when set
|
||||||
|
|
||||||
|
@ -54,24 +54,19 @@ class DirIndex:
|
||||||
tokens = line.split(':')
|
tokens = line.split(':')
|
||||||
if len(tokens) == 0:
|
if len(tokens) == 0:
|
||||||
continue
|
continue
|
||||||
|
elif tokens[0] == "version":
|
||||||
if tokens[0] == "version":
|
|
||||||
self.version = int(tokens[1])
|
self.version = int(tokens[1])
|
||||||
|
|
||||||
elif tokens[0] == "path":
|
elif tokens[0] == "path":
|
||||||
# This is relative to the repository root
|
# This is relative to the repository root
|
||||||
self.path = VirtualPath(tokens[1])
|
self.path = VirtualPath(tokens[1])
|
||||||
|
|
||||||
elif tokens[0] == "d":
|
elif tokens[0] == "d":
|
||||||
self.d.append({ 'name': tokens[1], 'hash': tokens[2] })
|
self.directories.append({'name': tokens[1], 'hash': tokens[2]})
|
||||||
|
|
||||||
elif tokens[0] == "f":
|
elif tokens[0] == "f":
|
||||||
self.f.append({ 'name': tokens[1],
|
self.files.append({'name': tokens[1],
|
||||||
'hash': tokens[2], 'size': int(tokens[3]) })
|
'hash': tokens[2], 'size': int(tokens[3])})
|
||||||
|
|
||||||
elif tokens[0] == "t":
|
elif tokens[0] == "t":
|
||||||
self.t.append({ 'name': tokens[1], 'hash': tokens[2],
|
self.tarballs.append({'name': tokens[1], 'hash': tokens[2],
|
||||||
'size': int(tokens[3]) })
|
'size': int(tokens[3])})
|
||||||
|
|
||||||
def _sanityCheck(self):
|
def _sanityCheck(self):
|
||||||
if self.path is None:
|
if self.path is None:
|
||||||
|
@ -81,19 +76,3 @@ class DirIndex:
|
||||||
raise InvalidDirIndexFile(
|
raise InvalidDirIndexFile(
|
||||||
"no 'path' field found; the first lines of this .dirindex file "
|
"no 'path' field found; the first lines of this .dirindex file "
|
||||||
"follow:\n\n" + '\n'.join(firstLines))
|
"follow:\n\n" + '\n'.join(firstLines))
|
||||||
|
|
||||||
def getVersion(self):
|
|
||||||
return self.version
|
|
||||||
|
|
||||||
def getPath(self):
|
|
||||||
return self.path
|
|
||||||
|
|
||||||
def getDirectories(self):
|
|
||||||
return self.d
|
|
||||||
|
|
||||||
def getTarballs(self):
|
|
||||||
return self.t
|
|
||||||
|
|
||||||
def getFiles(self):
|
|
||||||
return self.f
|
|
||||||
|
|
||||||
|
|
|
@ -556,26 +556,26 @@ class TerraSync:
|
||||||
|
|
||||||
def handleDirindexFile(self, dirindexFile):
|
def handleDirindexFile(self, dirindexFile):
|
||||||
dirIndex = dirindex.DirIndex(dirindexFile)
|
dirIndex = dirindex.DirIndex(dirindexFile)
|
||||||
virtualBase = dirIndex.getPath() # VirtualPath instance
|
virtualBase = dirIndex.path # VirtualPath instance
|
||||||
relativeBase = virtualBase.asRelative() # string, doesn't start with '/'
|
relativeBase = virtualBase.asRelative() # string, doesn't start with '/'
|
||||||
serverFiles = []
|
serverFiles = []
|
||||||
serverDirs = []
|
serverDirs = []
|
||||||
|
|
||||||
for file in dirIndex.getFiles():
|
for file in dirIndex.files:
|
||||||
f = file['name']
|
f = file['name']
|
||||||
self.processFileEntry(virtualBase / f,
|
self.processFileEntry(virtualBase / f,
|
||||||
join(relativeBase, f),
|
join(relativeBase, f),
|
||||||
file['hash'])
|
file['hash'])
|
||||||
serverFiles.append(f)
|
serverFiles.append(f)
|
||||||
|
|
||||||
for subdir in dirIndex.getDirectories():
|
for subdir in dirIndex.directories:
|
||||||
d = subdir['name']
|
d = subdir['name']
|
||||||
self.processDirectoryEntry(virtualBase / d,
|
self.processDirectoryEntry(virtualBase / d,
|
||||||
join(relativeBase, d),
|
join(relativeBase, d),
|
||||||
subdir['hash'])
|
subdir['hash'])
|
||||||
serverDirs.append(d)
|
serverDirs.append(d)
|
||||||
|
|
||||||
for tarball in dirIndex.getTarballs():
|
for tarball in dirIndex.tarballs:
|
||||||
# Tarballs are handled the same as normal files.
|
# Tarballs are handled the same as normal files.
|
||||||
f = tarball['name']
|
f = tarball['name']
|
||||||
self.processFileEntry(virtualBase / f,
|
self.processFileEntry(virtualBase / f,
|
||||||
|
|
|
@ -66,30 +66,10 @@ tarballs_in_sample_dirindex_1 = [
|
||||||
class TestDirIndex(unittest.TestCase):
|
class TestDirIndex(unittest.TestCase):
|
||||||
"""Unit tests for the DirIndex class."""
|
"""Unit tests for the DirIndex class."""
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.dirindex = DirIndex(testData("sample_dirindex_1"))
|
|
||||||
|
|
||||||
def test_readFrom(self):
|
def test_readFrom(self):
|
||||||
self.assertEqual(self.dirindex.version, 1)
|
d = DirIndex(testData("sample_dirindex_1"))
|
||||||
self.assertEqual(self.dirindex.path, VirtualPath("some/path"))
|
self.assertEqual(d.version, 1)
|
||||||
self.assertEqual(self.dirindex.d, directories_in_sample_dirindex_1)
|
self.assertEqual(d.path, VirtualPath("some/path"))
|
||||||
self.assertEqual(self.dirindex.f, files_in_sample_dirindex_1)
|
self.assertEqual(d.directories, directories_in_sample_dirindex_1)
|
||||||
self.assertEqual(self.dirindex.t, tarballs_in_sample_dirindex_1)
|
self.assertEqual(d.files, files_in_sample_dirindex_1)
|
||||||
|
self.assertEqual(d.tarballs, tarballs_in_sample_dirindex_1)
|
||||||
def test_getVersion(self):
|
|
||||||
self.assertEqual(self.dirindex.getVersion(), 1)
|
|
||||||
|
|
||||||
def test_getPath(self):
|
|
||||||
self.assertEqual(self.dirindex.getPath(), VirtualPath("some/path"))
|
|
||||||
|
|
||||||
def test_getDirectories(self):
|
|
||||||
self.assertEqual(self.dirindex.getDirectories(),
|
|
||||||
directories_in_sample_dirindex_1)
|
|
||||||
|
|
||||||
def test_getTarballs(self):
|
|
||||||
self.assertEqual(self.dirindex.getTarballs(),
|
|
||||||
tarballs_in_sample_dirindex_1)
|
|
||||||
|
|
||||||
def test_getFiles(self):
|
|
||||||
self.assertEqual(self.dirindex.getFiles(),
|
|
||||||
files_in_sample_dirindex_1)
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue