changeset 872:135198715129 storage-ng

implement item-get / item-put script commands
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 25 Sep 2011 18:53:52 +0200
parents 75de15de041b
children 09534d362ed6
files MoinMoin/script/__init__.py MoinMoin/script/maint/modify_item.py
diffstat 2 files changed, 78 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/script/__init__.py	Sun Sep 25 17:36:31 2011 +0200
+++ b/MoinMoin/script/__init__.py	Sun Sep 25 18:53:52 2011 +0200
@@ -45,6 +45,9 @@
     manager.add_command("maint_set_meta", Set_Meta())
     from MoinMoin.script.maint.create_item import Create_Item
     manager.add_command("maint_create_item", Create_Item())
+    from MoinMoin.script.maint import modify_item
+    manager.add_command("item-get", modify_item.GetItem())
+    manager.add_command("item-put", modify_item.PutItem())
     from MoinMoin.script.maint.modified_systemitems import Modified_SystemItems
     manager.add_command("maint_modified_systemitems", Modified_SystemItems())
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/script/maint/modify_item.py	Sun Sep 25 18:53:52 2011 +0200
@@ -0,0 +1,75 @@
+# Copyright: 2011 MoinMoin:ThomasWaldmann
+# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
+
+"""
+MoinMoin - get an item revision from the wiki, put it back into the wiki.
+"""
+
+import shutil
+
+try:
+    import json
+except ImportError:
+    import simplejson as json
+
+
+from flask import current_app as app
+from flask import g as flaskg
+from flaskext.script import Command, Option
+
+from MoinMoin.config import NAME, CURRENT, REVID, DATAID, SIZE, HASH_ALGORITHM
+
+
+class GetItem(Command):
+    description = 'Get an item revision from the wiki.'
+    option_list = (
+        Option('--name', '-n', dest='name', type=unicode, required=True,
+            help='Name of the item to get.'),
+        Option('--revid', '-r', dest='revid', type=unicode, required=False, default=CURRENT,
+            help='Revision ID of the revision to get (default: current rev).'),
+        Option('--meta', '-m', dest='meta_file', type=unicode, required=True,
+            help='Filename of file to create for the metadata.'),
+        Option('--data', '-d', dest='data_file', type=unicode, required=True,
+            help='Filename of file to create for the data.'),
+    )
+
+    def run(self, name, meta_file, data_file, revid):
+        item = app.storage[name]
+        rev = item[revid]
+        meta = json.dumps(dict(rev.meta), sort_keys=True, indent=2, ensure_ascii=False)
+        meta = meta.encode('utf-8')
+        with open(meta_file, 'wb') as mf:
+            mf.write(meta)
+        with open(data_file, 'wb') as df:
+            shutil.copyfileobj(rev.data, df)
+
+
+class PutItem(Command):
+    description = 'Put an item revision into the wiki.'
+    option_list = (
+        Option('--meta', '-m', dest='meta_file', type=unicode, required=True,
+            help='Filename of file to read as metadata.'),
+        Option('--data', '-d', dest='data_file', type=unicode, required=True,
+            help='Filename of file to read as data.'),
+        Option('--overwrite', '-o', action='store_true', dest='overwrite', default=False,
+            help='If given, overwrite existing revisions, if requested.'),
+    )
+
+    def run(self, meta_file, data_file, overwrite):
+        with open(meta_file, 'rb') as mf:
+            meta = mf.read()
+        meta = meta.decode('utf-8')
+        meta = json.loads(meta)
+        name = meta[NAME]
+        to_kill = [SIZE, HASH_ALGORITHM, # gets re-computed automatically
+                   DATAID,
+                  ]
+        for key in to_kill:
+            meta.pop(key, None)
+        if not overwrite:
+            # if we remove the REVID, it will create a new one and store under the new one
+            meta.pop(REVID, None)
+        item = app.storage[name]
+        with open(data_file, 'rb') as df:
+            item.store_revision(meta, df, overwrite=overwrite)
+