mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-02-21 18:06:46 +00:00
Nail down types.
This commit is contained in:
parent
dfffa8722a
commit
8fdbb6db6e
@ -74,6 +74,13 @@ ADD_FILE = ActionType(
|
||||
u"Add a new file as a child of a directory.",
|
||||
)
|
||||
|
||||
|
||||
class _OnlyFiles(object):
|
||||
"""Marker for replacement option of only replacing files."""
|
||||
|
||||
ONLY_FILES = _OnlyFiles()
|
||||
|
||||
|
||||
def update_metadata(metadata, new_metadata, now):
|
||||
"""Updates 'metadata' in-place with the information in 'new_metadata'.
|
||||
|
||||
@ -179,7 +186,7 @@ class Adder(object):
|
||||
if entries is None:
|
||||
entries = {}
|
||||
precondition(isinstance(entries, dict), entries)
|
||||
precondition(overwrite in (True, False, "only-files"), overwrite)
|
||||
precondition(overwrite in (True, False, ONLY_FILES), overwrite)
|
||||
# keys of 'entries' may not be normalized.
|
||||
self.entries = entries
|
||||
self.overwrite = overwrite
|
||||
@ -205,7 +212,7 @@ class Adder(object):
|
||||
if not self.overwrite:
|
||||
raise ExistingChildError("child %s already exists" % quote_output(name, encoding='utf-8'))
|
||||
|
||||
if self.overwrite == "only-files" and IDirectoryNode.providedBy(children[name][0]):
|
||||
if self.overwrite == ONLY_FILES and IDirectoryNode.providedBy(children[name][0]):
|
||||
raise ExistingChildError("child %s already exists as a directory" % quote_output(name, encoding='utf-8'))
|
||||
metadata = children[name][1].copy()
|
||||
|
||||
@ -701,7 +708,7 @@ class DirectoryNode(object):
|
||||
'new_child_namex' and 'current_child_namex' need not be normalized.
|
||||
|
||||
The overwrite parameter may be True (overwrite any existing child),
|
||||
False (error if the new child link already exists), or "only-files"
|
||||
False (error if the new child link already exists), or ONLY_FILES
|
||||
(error if the new child link exists and points to a directory).
|
||||
"""
|
||||
if self.is_readonly() or new_parent.is_readonly():
|
||||
|
@ -1978,12 +1978,12 @@ class Adder(GridTestMixin, unittest.TestCase, testutil.ShouldFailMixin):
|
||||
overwrite=False))
|
||||
d.addCallback(lambda res:
|
||||
root_node.set_node(u'file1', filenode,
|
||||
overwrite="only-files"))
|
||||
overwrite=dirnode.ONLY_FILES))
|
||||
d.addCallback(lambda res:
|
||||
self.shouldFail(ExistingChildError, "set_node",
|
||||
"child 'dir1' already exists",
|
||||
root_node.set_node, u'dir1', filenode,
|
||||
overwrite="only-files"))
|
||||
overwrite=dirnode.ONLY_FILES))
|
||||
return d
|
||||
|
||||
d.addCallback(_test_adder)
|
||||
|
@ -12,17 +12,18 @@ if PY2:
|
||||
|
||||
from twisted.trial import unittest
|
||||
from allmydata.web import status, common
|
||||
from allmydata.dirnode import ONLY_FILES
|
||||
from ..common import ShouldFailMixin
|
||||
from .. import common_util as testutil
|
||||
|
||||
class Util(ShouldFailMixin, testutil.ReallyEqualMixin, unittest.TestCase):
|
||||
|
||||
def test_parse_replace_arg(self):
|
||||
self.failUnlessReallyEqual(common.parse_replace_arg("true"), True)
|
||||
self.failUnlessReallyEqual(common.parse_replace_arg("false"), False)
|
||||
self.failUnlessReallyEqual(common.parse_replace_arg("only-files"),
|
||||
"only-files")
|
||||
self.failUnlessRaises(common.WebError, common.parse_replace_arg, "only_fles")
|
||||
self.failUnlessReallyEqual(common.parse_replace_arg(b"true"), True)
|
||||
self.failUnlessReallyEqual(common.parse_replace_arg(b"false"), False)
|
||||
self.failUnlessReallyEqual(common.parse_replace_arg(b"only-files"),
|
||||
ONLY_FILES)
|
||||
self.failUnlessRaises(common.WebError, common.parse_replace_arg, b"only_fles")
|
||||
|
||||
def test_abbreviate_time(self):
|
||||
self.failUnlessReallyEqual(common.abbreviate_time(None), "")
|
||||
|
@ -2,7 +2,7 @@ from past.builtins import unicode
|
||||
from six import ensure_text, ensure_str
|
||||
|
||||
try:
|
||||
from typing import Optional
|
||||
from typing import Optional, Union
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
@ -56,6 +56,7 @@ from twisted.web.resource import (
|
||||
IResource,
|
||||
)
|
||||
|
||||
from allmydata.dirnode import ONLY_FILES, _OnlyFiles
|
||||
from allmydata import blacklist
|
||||
from allmydata.interfaces import (
|
||||
EmptyPathnameComponentError,
|
||||
@ -105,17 +106,17 @@ def get_filenode_metadata(filenode):
|
||||
metadata['size'] = size
|
||||
return metadata
|
||||
|
||||
def boolean_of_arg(arg):
|
||||
# TODO: ""
|
||||
arg = ensure_text(arg)
|
||||
if arg.lower() not in ("true", "t", "1", "false", "f", "0", "on", "off"):
|
||||
def boolean_of_arg(arg): # type: (bytes) -> bool
|
||||
assert isinstance(arg, bytes)
|
||||
if arg.lower() not in (b"true", b"t", b"1", b"false", b"f", b"0", b"on", b"off"):
|
||||
raise WebError("invalid boolean argument: %r" % (arg,), http.BAD_REQUEST)
|
||||
return arg.lower() in ("true", "t", "1", "on")
|
||||
return arg.lower() in (b"true", b"t", b"1", b"on")
|
||||
|
||||
def parse_replace_arg(replace):
|
||||
replace = ensure_text(replace)
|
||||
if replace.lower() == "only-files":
|
||||
return replace
|
||||
|
||||
def parse_replace_arg(replace): # type: (bytes) -> Union[bool,_OnlyFiles]
|
||||
assert isinstance(replace, bytes)
|
||||
if replace.lower() == b"only-files":
|
||||
return ONLY_FILES
|
||||
try:
|
||||
return boolean_of_arg(replace)
|
||||
except WebError:
|
||||
|
Loading…
x
Reference in New Issue
Block a user