From 0baa7444112ead61a468c0eac0affda0d937c5da Mon Sep 17 00:00:00 2001
From: zancas <zancas@leastauthority.com>
Date: Tue, 7 Oct 2014 13:11:31 -0600
Subject: [PATCH] refactor get_filenode_metadata(...) into common.py

---
 src/allmydata/web/common.py    | 14 ++++++++++++++
 src/allmydata/web/directory.py | 18 ++----------------
 src/allmydata/web/filenode.py  | 19 +++----------------
 3 files changed, 19 insertions(+), 32 deletions(-)

diff --git a/src/allmydata/web/common.py b/src/allmydata/web/common.py
index be6df366..2d15281c 100644
--- a/src/allmydata/web/common.py
+++ b/src/allmydata/web/common.py
@@ -18,6 +18,20 @@ from allmydata.util.encodingutil import to_str, quote_output
 
 TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
 
+def get_filenode_metadata(filenode):
+    metadata = {'size': filenode.get_size(),
+                'mutable': filenode.is_mutable()}
+    if metadata['mutable']:
+        mutable_type = filenode.get_version()
+        assert mutable_type in (SDMF_VERSION, MDMF_VERSION)
+        if mutable_type == MDMF_VERSION:
+            file_format = "MDMF"
+        else:
+            file_format = "SDMF"
+    else:
+        file_format = "CHK"
+    metadata['format'] = file_format
+    return metadata
 
 class IOpHandleTable(Interface):
     pass
diff --git a/src/allmydata/web/directory.py b/src/allmydata/web/directory.py
index 4dfe36fa..d82bf643 100644
--- a/src/allmydata/web/directory.py
+++ b/src/allmydata/web/directory.py
@@ -26,7 +26,7 @@ from allmydata.web.common import text_plain, WebError, \
      boolean_of_arg, get_arg, get_root, parse_replace_arg, \
      should_create_intermediate_directories, \
      getxmlfile, RenderMixin, humanize_failure, convert_children_json, \
-     get_format, get_mutable_type
+     get_format, get_mutable_type, get_filenode_metadata
 from allmydata.web.filenode import ReplaceMeMixin, \
      FileNodeHandler, PlaceHolderNodeHandler
 from allmydata.web.check_results import CheckResultsRenderer, \
@@ -869,7 +869,6 @@ class DirectoryAsHTML(rend.Page):
         req = IRequest(ctx)
         return get_arg(req, "results", "")
 
-
 def DirectoryJSONMetadata(ctx, dirnode):
     d = dirnode.list()
     def _got(children):
@@ -879,20 +878,7 @@ def DirectoryJSONMetadata(ctx, dirnode):
             rw_uri = childnode.get_write_uri()
             ro_uri = childnode.get_readonly_uri()
             if IFileNode.providedBy(childnode):
-                kiddata = ("filenode", {'size': childnode.get_size(),
-                                        'mutable': childnode.is_mutable(),
-                                        })
-                if childnode.is_mutable():
-                    mutable_type = childnode.get_version()
-                    assert mutable_type in (SDMF_VERSION, MDMF_VERSION)
-                    if mutable_type == MDMF_VERSION:
-                        file_format = "MDMF"
-                    else:
-                        file_format = "SDMF"
-                else:
-                    file_format = "CHK"
-                kiddata[1]['format'] = file_format
-
+                kiddata = ("filenode", get_filenode_metadata(childnode))
             elif IDirectoryNode.providedBy(childnode):
                 kiddata = ("dirnode", {'mutable': childnode.is_mutable()})
             else:
diff --git a/src/allmydata/web/filenode.py b/src/allmydata/web/filenode.py
index bce8e90c..ff65acc9 100644
--- a/src/allmydata/web/filenode.py
+++ b/src/allmydata/web/filenode.py
@@ -6,7 +6,7 @@ from twisted.internet import defer
 from nevow import url, rend
 from nevow.inevow import IRequest
 
-from allmydata.interfaces import ExistingChildError, SDMF_VERSION, MDMF_VERSION
+from allmydata.interfaces import ExistingChildError
 from allmydata.monitor import Monitor
 from allmydata.immutable.upload import FileHandle
 from allmydata.mutable.publish import MutableFileHandle
@@ -18,7 +18,7 @@ from allmydata.blacklist import FileProhibited, ProhibitedNode
 from allmydata.web.common import text_plain, WebError, RenderMixin, \
      boolean_of_arg, get_arg, should_create_intermediate_directories, \
      MyExceptionHandler, parse_replace_arg, parse_offset_arg, \
-     get_format, get_mutable_type
+     get_format, get_mutable_type, get_filenode_metadata
 from allmydata.web.check_results import CheckResultsRenderer, \
      CheckAndRepairResultsRenderer, LiteralCheckResultsRenderer
 from allmydata.web.info import MoreInfo
@@ -498,8 +498,7 @@ class FileDownloader(rend.Page):
 def FileJSONMetadata(ctx, filenode, edge_metadata):
     rw_uri = filenode.get_write_uri()
     ro_uri = filenode.get_readonly_uri()
-    data = ("filenode", {})
-    data[1]['size'] = filenode.get_size()
+    data = ("filenode", get_filenode_metadata(filenode))
     if ro_uri:
         data[1]['ro_uri'] = ro_uri
     if rw_uri:
@@ -507,21 +506,9 @@ def FileJSONMetadata(ctx, filenode, edge_metadata):
     verifycap = filenode.get_verify_cap()
     if verifycap:
         data[1]['verify_uri'] = verifycap.to_string()
-    data[1]['mutable'] = filenode.is_mutable()
     if edge_metadata is not None:
         data[1]['metadata'] = edge_metadata
 
-    if filenode.is_mutable():
-        mutable_type = filenode.get_version()
-        assert mutable_type in (SDMF_VERSION, MDMF_VERSION)
-        if mutable_type == MDMF_VERSION:
-            file_format = "MDMF"
-        else:
-            file_format = "SDMF"
-    else:
-        file_format = "CHK"
-    data[1]['format'] = file_format
-
     return text_plain(simplejson.dumps(data, indent=1) + "\n", ctx)
 
 def FileURI(ctx, filenode):
-- 
2.45.2