http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11660

Revision: 11660
Author:   legoktm
Date:     2013-06-18 06:57:42 +0000 (Tue, 18 Jun 2013)
Log Message:
-----------
Move the datavalue formatting into a single function in the claim class

Modified Paths:
--------------
    branches/rewrite/pywikibot/page.py
    branches/rewrite/pywikibot/site.py

Modified: branches/rewrite/pywikibot/page.py
===================================================================
--- branches/rewrite/pywikibot/page.py  2013-06-18 06:28:08 UTC (rev 11659)
+++ branches/rewrite/pywikibot/page.py  2013-06-18 06:57:42 UTC (rev 11660)
@@ -2737,7 +2737,26 @@
         self.on_item.lastrevid = data['pageinfo']['lastrevid']
         self.sources.append(source)
 
+    def _formatDataValue(self):
+        """
+        Format the target into the proper JSON datavalue that Wikibase wants
+        """
+        if self.getType() == 'wikibase-item':
+            value = {'entity-type': 'item',
+                     'numeric-id': self.getTarget().getID(numeric=True)}
+        elif self.getType() == 'string':
+            value = self.getTarget()
+        elif self.getType() == 'commonsMedia':
+            value = self.getTarget().title(withNamespace=False)
+        elif self.getType() == 'globecoordinate':
+            value = self.getTarget().toWikibase()
+        else:
+            raise NotImplementedError('%s datatype is not supported yet.' % 
self.getType())
+        return value
 
+
+
+
 class Revision(object):
     """A structure holding information about a single revision of a Page."""
     def __init__(self, revid, timestamp, user, anon=False, comment=u"",

Modified: branches/rewrite/pywikibot/site.py
===================================================================
--- branches/rewrite/pywikibot/site.py  2013-06-18 06:28:08 UTC (rev 11659)
+++ branches/rewrite/pywikibot/site.py  2013-06-18 06:57:42 UTC (rev 11660)
@@ -3403,17 +3403,7 @@
         if bot:
             params['bot'] = 1
         if claim.getSnakType() == 'value':
-            if claim.getType() == 'wikibase-item':
-                params['value'] = json.dumps({'entity-type': 'item',
-                                              'numeric-id': 
claim.getTarget().getID(numeric=True)})
-            elif claim.getType() == 'string':
-                params['value'] = json.dumps(claim.getTarget())
-            elif claim.getType() == 'commonsMedia':
-                params['value'] = 
json.dumps(claim.getTarget().title(withNamespace=False))
-            elif claim.getType() == 'globecoordinate':
-                params['value'] = json.dumps(claim.getTarget().toWikibase())
-            else:
-                raise NotImplementedError('%s datatype is not supported yet.' 
% claim.getType())
+            params['value'] = json.dumps(claim._formatDataValue())
         params['token'] = self.token(item, 'edit')
         req = api.Request(site=self, **params)
         data = req.submit()
@@ -3444,18 +3434,7 @@
             params['bot'] = 1
         params['token'] = self.token(claim, 'edit')
         if snaktype == 'value':
-            #This code is repeated from above, maybe it should be it's own 
function?
-            if claim.getType() == 'wikibase-item':
-                params['value'] = json.dumps({'entity-type': 'item',
-                                              'numeric-id': 
claim.getTarget().getID(numeric=True)})
-            elif claim.getType() == 'string':
-                params['value'] = json.dumps(claim.getTarget())
-            elif claim.getType() == 'commonsMedia':
-                params['value'] = 
json.dumps(claim.getTarget().title(withNamespace=False))
-            elif claim.getType() == 'globecoordinate':
-                params['value'] = json.dumps(claim.getTarget().toWikibase())
-            else:
-                raise NotImplementedError('%s datatype is not supported yet.' 
% claim.getType())
+            params['value'] = json.dumps(claim._formatDataValue())
 
         for arg in kwargs:
             #TODO: Get the lastrevid from the item
@@ -3491,13 +3470,11 @@
         #build up the snak
         if source.getType() == 'wikibase-item':
             datavalue = {'type': 'wikibase-entityid',
-                         'value': {'entity-type': 'item',
-                                   'numeric-id': 
source.getTarget().getID(numeric=True),
-                                   },
+                         'value': source._formatDataValue(),
                          }
         elif source.getType() == 'string':
             datavalue = {'type': 'string',
-                         'value': source.getTarget(),
+                         'value': source._formatDataValue(),
                          }
         else:
             raise NotImplementedError('%s datatype is not supported yet.' % 
claim.getType())


_______________________________________________
Pywikipedia-svn mailing list
Pywikipedia-svn@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/pywikipedia-svn

Reply via email to