jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/378800 )

Change subject: Add mechanism for storing wikipage locally instead of writing 
to wiki
......................................................................


Add mechanism for storing wikipage locally instead of writing to wiki

This allows e.g. local (docker) testing of page saving outputs and unit
testing of report writing functions.

The trigger for this is the HERITAGE_LOCAL_WRITE_PATH environement variable
which is automatically set by docker.

Support for this output method is added to all erfgoedbot scripts apart
from add_coord_to_articles.py (which relies on user interaction).

Also:
* Minor cleanup of add_object_location to make linter cry less

Bug: T174614
Change-Id: I2bf650f99a57a0e93dbb0c3d6f520049c3579957
---
M README.md
M docker-compose-bot.yml
M erfgoedbot/add_object_location_monuments.py
M erfgoedbot/categorize_images.py
M erfgoedbot/checkers.py
M erfgoedbot/common.py
M erfgoedbot/database_statistics.py
M erfgoedbot/images_of_monuments_without_id.py
M erfgoedbot/missing_commonscat_links.py
M erfgoedbot/populate_image_table.py
M erfgoedbot/unused_monument_images.py
M erfgoedbot/update_database.py
M requirements.txt
M tests/test_common.py
14 files changed, 246 insertions(+), 65 deletions(-)

Approvals:
  Jean-Frédéric: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/README.md b/README.md
index 50b5352..7f4267e 100644
--- a/README.md
+++ b/README.md
@@ -23,6 +23,9 @@
 # Build and start the Docker containers
 docker-compose -f docker-compose-bot.yml up --build -d
 
+# Ensure a directory for locally written pages
+mkdir -p docker_pages
+
 # Run the bot to harvest a country
 docker-compose -f docker-compose-bot.yml run --rm bot python 
erfgoedbot/update_database.py -countrycode:ge -langcode:ka -log
 
diff --git a/docker-compose-bot.yml b/docker-compose-bot.yml
index b4ed95b..b6cc878 100644
--- a/docker-compose-bot.yml
+++ b/docker-compose-bot.yml
@@ -4,6 +4,8 @@
     service: bot
   links:
     - db
+  environment:
+    - HERITAGE_LOCAL_WRITE_PATH=docker_pages
   volumes:
     - "./conf/database_config.dev.yml:/code/erfgoedbot/database_config.yml"
 
diff --git a/erfgoedbot/add_object_location_monuments.py 
b/erfgoedbot/add_object_location_monuments.py
index 1ffc23f..658a403 100644
--- a/erfgoedbot/add_object_location_monuments.py
+++ b/erfgoedbot/add_object_location_monuments.py
@@ -6,9 +6,9 @@
 
 '''
 import pywikibot
-from pywikibot import pagegenerators
 
 import monuments_config as mconfig
+import common as common
 from database_connection import (
     close_database_connection,
     connect_to_monuments_database,
@@ -16,7 +16,8 @@
 )
 
 
-def locateCountry(countrycode, lang, countryconfig, conn, cursor, conn2, 
cursor2):
+def locateCountry(countrycode, lang, countryconfig, conn, cursor, conn2,
+                  cursor2):
     '''
     Locate images in a single country.
     '''
@@ -91,7 +92,10 @@
     # not already a template on the page.
     templates = page.templates()
 
-    if u'Location' in page.templates() or u'Location dec' in page.templates() 
or u'Object location' in page.templates() or u'Object location dec' in 
page.templates():
+    if (u'Location' in templates or
+            u'Location dec' in templates or
+            u'Object location' in templates or
+            u'Object location dec' in templates):
         pywikibot.output(
             u'Location template already found at: %s' % page.title())
         return False
@@ -106,8 +110,6 @@
     '''
     Get coordinates from the erfgoed database
     '''
-    result = None
-
     query = u"""SELECT lat, lon, source FROM monuments_all
 WHERE id=%s
 AND country=%s
@@ -139,7 +141,7 @@
     newtext = putAfterTemplate(
         oldtext, u'Information', locationTemplate, loose=True)
     pywikibot.showDiff(oldtext, newtext)
-    page.put(newtext, comment)
+    common.save_to_wiki_or_local(page, comment, newtext)
 
 
 def putAfterTemplate(oldtext, template, toadd, loose=True):
@@ -191,7 +193,7 @@
             newtext = oldtext
             cats = pywikibot.getCategoryLinks(newtext)
             ll = pywikibot.getLanguageLinks(newtext)
-            nextext = pywikibot.removeLanguageLinks(newtext)
+            newtext = pywikibot.removeLanguageLinks(newtext)
             newtext = pywikibot.removeCategoryLinks(newtext)
             newtext += u'\n' + toadd
             newtext = pywikibot.replaceCategoryLinks(newtext, cats)
@@ -208,9 +210,6 @@
     (conn, cursor) = connect_to_monuments_database()
     (conn2, cursor2) = connect_to_commons_database()
 
-    generator = None
-    genFactory = pagegenerators.GeneratorFactory()
-
     for arg in pywikibot.handleArgs():
         option, sep, value = arg.partition(':')
         if option == '-countrycode':
@@ -219,8 +218,8 @@
             lang = value
         else:
             raise Exception(
-                u'Bad parameters. Expected "-countrycode", "-langcode" or '
-                u'pywikibot args. Found "{}"'.format(option))
+                u'Bad parameters. Expected "-countrycode", "-langcode" '
+                u'or pywikibot args. Found "{}"'.format(option))
 
     pywikibot.setSite(pywikibot.getSite(u'commons', u'commons'))
 
@@ -245,7 +244,8 @@
                 pywikibot.output(
                     u'Working on countrycode "%s" in language "%s"' % 
(countrycode, lang))
                 locateCountry(
-                    countrycode, lang, countryconfig, conn, cursor, conn2, 
cursor2)
+                    countrycode, lang, countryconfig, conn, cursor, conn2,
+                    cursor2)
 
     close_database_connection(conn, cursor)
 
diff --git a/erfgoedbot/categorize_images.py b/erfgoedbot/categorize_images.py
index 1705d56..b11447b 100644
--- a/erfgoedbot/categorize_images.py
+++ b/erfgoedbot/categorize_images.py
@@ -113,7 +113,8 @@
     if newcats:
         comment = u'Adding categories based on [[Template:%s]] with identifier 
%s (method %s)' % (
             commonsTemplateName, monumentId, categorisation_method)
-        replace_default_cat_with_new_categories_in_image(page, 
commonsCategoryBase, newcats, comment, verbose=True)
+        replace_default_cat_with_new_categories_in_image(
+            page, commonsCategoryBase, newcats, comment, verbose=True)
     else:
         pywikibot.log(u'Categories not found for %s' % page.title())
 
@@ -217,7 +218,7 @@
     if verbose:
         pywikibot.showDiff(old_text, final_text)
     try:
-        page.put(final_text, comment)
+        common.save_to_wiki_or_local(page, comment, final_text)
         return True
     except pywikibot.EditConflict:
         pywikibot.log(
@@ -402,7 +403,8 @@
         raise NoCommonsCatFromWikidataItemException(page)
 
 
-def processCountry(countrycode, lang, countryconfig, commonsCatTemplates, 
conn, cursor, overridecat=None):
+def processCountry(countrycode, lang, countryconfig, commonsCatTemplates, conn,
+                   cursor, overridecat=None):
     '''
     Work on a single country.
     '''
@@ -451,9 +453,7 @@
 
 
 def outputStatistics(statistics):
-    '''
-    Output the results of the bot as a nice wikitable
-    '''
+    """Output the results of the bot as a nice wikitable."""
     output = u'{| class="wikitable sortable"\n'
     output += \
         u'! country !! [[:en:List of ISO 639-1 codes|lang]] !! Base category 
!! Template !! data-sort-type="number"|Total images !! 
data-sort-type="number"|Categorized images !! data-sort-type="number"|Images 
left !! data-sort-type="number"|Current image count\n'
@@ -489,10 +489,11 @@
     site = pywikibot.Site('commons', 'commons')
     page = pywikibot.Page(
         site, u'Commons:Monuments database/Categorization/Statistics')
-
-    comment = u'Updating categorization statistics. Total: %s Categorized: %s 
Leftover: %s' % (
-        totalImages, categorizedImages, leftoverImages)
-    page.put(newtext=output, comment=comment)
+    summary = (
+        u'Updating categorization statistics. '
+        u'Total: {0} Categorized: {1} Leftover: {2}'.format(
+            totalImages, categorizedImages, leftoverImages))
+    common.save_to_wiki_or_local(page, summary, output)
 
 
 def getCommonscatTemplates(lang=None, project=None):
@@ -537,7 +538,8 @@
         else:
             raise Exception(
                 u'Bad parameters. Expected "-countrycode", "-langcode", '
-                u'"-overridecat" or pywikibot args. Found "{}"'.format(option))
+                u'"-overridecat" or pywikibot args. '
+                u'Found "{}"'.format(option))
 
     if countrycode and lang:
         if not mconfig.countries.get((countrycode, lang)):
@@ -569,7 +571,8 @@
             commonsCatTemplates = getCommonscatTemplates(
                 lang, countryconfig.get('project'))
             result = processCountry(
-                countrycode, lang, countryconfig, commonsCatTemplates, conn, 
cursor)
+                countrycode, lang, countryconfig, commonsCatTemplates, conn,
+                cursor)
             if result:
                 statistics.append(result)
 
diff --git a/erfgoedbot/checkers.py b/erfgoedbot/checkers.py
index 5cb73c3..cf98240 100644
--- a/erfgoedbot/checkers.py
+++ b/erfgoedbot/checkers.py
@@ -4,6 +4,7 @@
 """Checker methods"""
 
 import pywikibot
+import common as common
 
 _logger = "update_database"
 
@@ -20,8 +21,8 @@
     except (pywikibot.NoPage, pywikibot.IsRedirectPage):
         content = u''
     if exceptWord and exceptWord not in content:
-        content += "\n\n" + errorMsg + " --~~~~" + "\n\n"
-        talkPage.put(content, comment)
+        content += u'\n\n{0} --~~~~\n\n'.format(errorMsg)
+        common.save_to_wiki_or_local(talkPage, comment, content)
         return True
 
     return False
diff --git a/erfgoedbot/common.py b/erfgoedbot/common.py
index 333409c..148be99 100644
--- a/erfgoedbot/common.py
+++ b/erfgoedbot/common.py
@@ -1,8 +1,12 @@
 #!/usr/bin/python
 # -*- coding: utf-8  -*-
 """Support library of commonly shared functions."""
-
+from __future__ import unicode_literals
+from builtins import open
+import os
 import re
+import pywikibot
+from pywikibot.exceptions import OtherPageSaveError
 
 
 def get_source_link(source, harvest_type=None, label=None):
@@ -50,8 +54,8 @@
     commons is ('wikimedia', 'commons') rather than ('commons', 'commons').
 
     @param source: the source value from the SQL table
-    @harvest_type: the type of harvest from which the source was extracted.
-        e.g. "sparql"
+    @param harvest_type: the type of harvest from which the source was
+        extracted, e.g. "sparql".
     """
     site = None
     page_name = None
@@ -60,7 +64,7 @@
             return get_page_from_url(source)
         except AttributeError:
             raise ValueError(
-                u'Could not find source list ({0})'.format(source))
+                'Could not find source list ({0})'.format(source))
     else:
         supported_sites = ['wikipedia', 'wikivoyage', 'wikidata', 'wikimedia']
         pattern = '\/\/(.+?)\.({0})\.org\/w\/index\.php\?title=(.+?)&'.format(
@@ -71,6 +75,55 @@
             page_name = m.group(3)
         except AttributeError:
             raise ValueError(
-                u'Could not find source list ({0})'.format(source))
+                'Could not find source list ({0})'.format(source))
 
     return (page_name, site)
+
+
+def save_to_wiki_or_local(page, summary, content, minorEdit=True):
+    """
+    Save the content to the page on a given site or store it locally.
+
+    Whether the pages are outputted locally (and where to) is controlled by the
+    HERITAGE_LOCAL_WRITE_PATH environment variable.
+
+    @param page: the pywikibot.Page to which the content should be written
+    @param content: the content to store
+    @param summary: the edit summary to save the content with
+    @param minorEdit: if the edit should be marked as minor (defaults to True)
+    """
+    if not isinstance(page, pywikibot.Page):
+        pywikibot.warning(
+            'Could not save page {0} because it is not a Page '
+            'instance.'.format(page))
+
+    local_path = os.environ.get('HERITAGE_LOCAL_WRITE_PATH')
+
+    if not local_path:
+        try:
+            page.put(newtext=content, summary=summary, minorEdit=minorEdit)
+        except OtherPageSaveError:
+            pywikibot.warning(
+                'Could not save page {0} ({1})'.format(page, summary))
+    else:
+        filename = os.path.join(local_path, page_to_filename(page))
+        with open(filename, 'w', encoding='utf-8') as f:
+            f.write('#summary: {0}\n---------------\n'.format(summary))
+            f.write(unicode(content))
+
+
+def page_to_filename(page):
+    """
+    Create a standardised filename for a page.
+
+    The name takes the form [site][namespace]pagename.wiki where '/', ':' and
+    " " has been replaced by '_'. Namespace 0 is given as just '_'.
+
+    @param page: the pywikibot.Page for which to generate a filename.
+    """
+    site_str = str(page.site)
+    namespace_str = page.namespace().custom_prefix().rstrip(':') or '_'
+    pagename_str = page.title(as_filename=True, withNamespace=False)
+    filename = '[{site}][{ns}]{page}.wiki'.format(
+        site=site_str, ns=namespace_str, page=pagename_str)
+    return filename.replace(' ', '_').replace(':', '_')
diff --git a/erfgoedbot/database_statistics.py 
b/erfgoedbot/database_statistics.py
index b13b55c..ec7fe53 100755
--- a/erfgoedbot/database_statistics.py
+++ b/erfgoedbot/database_statistics.py
@@ -7,6 +7,7 @@
 '''
 import pywikibot
 
+import common as common
 from database_connection import (
     close_database_connection,
     connect_to_monuments_database
@@ -203,11 +204,11 @@
     output += u'|| %(source)s\n' % totals
 
     output += u'|}\n'
+
     site = pywikibot.Site('commons', 'commons')
     page = pywikibot.Page(site, u'Commons:Monuments database/Statistics')
-
     comment = u'Updating monument database statistics'
-    page.put(newtext=output, comment=comment)
+    common.save_to_wiki_or_local(page, comment, output)
 
 
 def getStatistics(country, language, conn, cursor):
@@ -307,14 +308,17 @@
 
 
 def main():
-    '''
-    The main loop
-    '''
-
+    """The main loop."""
     conn = None
     cursor = None
     (conn, cursor) = connect_to_monuments_database()
 
+    for arg in pywikibot.handleArgs():
+        option, sep, value = arg.partition(':')
+        raise Exception(
+            u'Bad parameters. Expected pywikibot args. '
+            u'Found "{}"'.format(option))
+
     statistics = {}
 
     for country in getCountries(conn, cursor):
diff --git a/erfgoedbot/images_of_monuments_without_id.py 
b/erfgoedbot/images_of_monuments_without_id.py
index 5413ea1..f4d8c59 100644
--- a/erfgoedbot/images_of_monuments_without_id.py
+++ b/erfgoedbot/images_of_monuments_without_id.py
@@ -16,6 +16,7 @@
 import pywikibot
 
 import monuments_config as mconfig
+import common as common
 from database_connection import (
     close_database_connection,
     connect_to_monuments_database,
@@ -23,7 +24,8 @@
 )
 
 
-def processCountry(countrycode, lang, countryconfig, conn, cursor, conn2, 
cursor2):
+def processCountry(countrycode, lang, countryconfig, conn, cursor, conn2,
+                   cursor2):
     '''
     Work on a single country.
     '''
@@ -92,7 +94,8 @@
         site = pywikibot.getSite(lang, project)
         page = pywikibot.Page(site, imagesWithoutIdPage)
         pywikibot.output(text)
-        page.put(text, comment, minorEdit=False)
+        common.save_to_wiki_or_local(
+            page, comment, text, minorEdit=False)
 
 
 def getMonumentsWithPhoto(countrycode, lang, countryconfig, conn, cursor):
@@ -184,7 +187,7 @@
     comment = u'Adding template %s based on usage in list' % (commonsTemplate,)
 
     pywikibot.showDiff(text, newtext)
-    page.put(newtext, comment)
+    common.save_to_wiki_or_local(page, comment, newtext)
     return True
 
 
@@ -205,8 +208,8 @@
             lang = value
         else:
             raise Exception(
-                u'Bad parameters. Expected "-countrycode", "-langcode" or '
-                u'pywikibot args. Found "{}"'.format(option))
+                u'Bad parameters. Expected "-countrycode", "-langcode" '
+                u'or pywikibot args. Found "{}"'.format(option))
 
     if countrycode and lang:
         if not mconfig.countries.get((countrycode, lang)):
diff --git a/erfgoedbot/missing_commonscat_links.py 
b/erfgoedbot/missing_commonscat_links.py
index 85974a1..012bccd 100644
--- a/erfgoedbot/missing_commonscat_links.py
+++ b/erfgoedbot/missing_commonscat_links.py
@@ -16,6 +16,7 @@
 import pywikibot
 
 import monuments_config as mconfig
+import common as common
 from database_connection import (
     close_database_connection,
     connect_to_monuments_database,
@@ -25,7 +26,8 @@
 _logger = "missing_commonscat"
 
 
-def processCountry(countrycode, lang, countryconfig, conn, cursor, conn2, 
cursor2):
+def processCountry(countrycode, lang, countryconfig, conn, cursor, conn2,
+                   cursor2):
     '''
     Work on a single country.
     '''
@@ -107,7 +109,7 @@
     site = pywikibot.Site(lang, u'wikipedia')
     page = pywikibot.Page(site, missingCommonscatPage)
     pywikibot.debug(text, _logger)
-    page.put(text, comment)
+    common.save_to_wiki_or_local(page, comment, text)
 
     return totalCategories
 
@@ -199,7 +201,7 @@
 
     comment = u'Updating missing commonscat links statistics. Total missing 
links: %s' % totalCategories
     pywikibot.debug(text, _logger)
-    page.put(newtext=text, comment=comment)
+    common.save_to_wiki_or_local(page, comment, text)
 
 
 def main():
@@ -219,8 +221,8 @@
             lang = value
         else:
             raise Exception(
-                u'Bad parameters. Expected "-countrycode", "-langcode" or '
-                u'pywikibot args. Found "{}"'.format(option))
+                u'Bad parameters. Expected "-countrycode", "-langcode" '
+                u'or pywikibot args. Found "{}"'.format(option))
 
     if countrycode and lang:
         if not mconfig.countries.get((countrycode, lang)):
diff --git a/erfgoedbot/populate_image_table.py 
b/erfgoedbot/populate_image_table.py
index 5ceac15..685e118 100644
--- a/erfgoedbot/populate_image_table.py
+++ b/erfgoedbot/populate_image_table.py
@@ -32,6 +32,7 @@
 import pywikibot
 
 import monuments_config as mconfig
+import common as common
 from database_connection import (
     close_database_connection,
     connect_to_monuments_database,
@@ -214,7 +215,7 @@
 
     comment = u'Updating indexed image statistics. Total indexed images: %s' % 
totalImages
     pywikibot.output(text)
-    page.put(newtext=text, comment=comment)
+    common.save_to_wiki_or_local(page, comment, text)
 
 
 def main():
@@ -224,6 +225,10 @@
         option, sep, value = arg.partition(':')
         if option == '-countrycode':
             countrycode = value
+        else:
+            raise Exception(
+                u'Bad parameters. Expected "-countrycode" or pywikibot args. '
+                u'Found "{}"'.format(option))
 
     if countrycode:
         pywikibot.output(u'Working on countrycode "%s"' % (countrycode,))
diff --git a/erfgoedbot/unused_monument_images.py 
b/erfgoedbot/unused_monument_images.py
index 16e6ebe..717d535 100644
--- a/erfgoedbot/unused_monument_images.py
+++ b/erfgoedbot/unused_monument_images.py
@@ -24,7 +24,8 @@
 _logger = "unused_images"
 
 
-def processCountry(countrycode, lang, countryconfig, conn, cursor, conn2, 
cursor2):
+def processCountry(countrycode, lang, countryconfig, conn, cursor, conn2,
+                   cursor2):
     '''
     Work on a single country.
     '''
@@ -107,7 +108,7 @@
     site = pywikibot.Site(lang, project)
     page = pywikibot.Page(site, unusedImagesPage)
     pywikibot.debug(text, _logger)
-    page.put(text, comment, minorEdit=False)
+    common.save_to_wiki_or_local(page, comment, text, minorEdit=False)
 
     return totalImages
 
@@ -190,7 +191,7 @@
 
     comment = u'Updating unused image statistics. Total unused images: %s' % 
totalImages
     pywikibot.debug(text, _logger)
-    page.put(newtext=text, comment=comment)
+    common.save_to_wiki_or_local(page, comment, text)
 
 
 def main():
diff --git a/erfgoedbot/update_database.py b/erfgoedbot/update_database.py
index b124386..d70deb2 100755
--- a/erfgoedbot/update_database.py
+++ b/erfgoedbot/update_database.py
@@ -22,7 +22,6 @@
 import pywikibot
 import pywikibot.data.sparql
 from pywikibot import pagegenerators
-from pywikibot.exceptions import OtherPageSaveError
 
 import monuments_config as mconfig
 import common as common
@@ -148,8 +147,9 @@
     """
     site = pywikibot.Site(u'commons', u'commons')
     page = pywikibot.Page(
-        site, u'Commons:Monuments database/Unknown fields/%s' % (
-            countryconfig.get('table'),))
+        site, u'Commons:Monuments database/Unknown fields/{0}'.format(
+            countryconfig.get('table')))
+    summary = u'Updating the list of unknown fields'
 
     text = u'{| class="wikitable sortable"\n'
     text += u'! Field !! Count\n'
@@ -159,11 +159,8 @@
 
     text += u'|}\n'
     text += u'[[Category:Commons:Monuments database/Unknown fields]]'
-    comment = u'Updating the list of unknown fields'
-    try:
-        page.put(text, comment)
-    except OtherPageSaveError:
-        pywikibot.warning("Could not save page %s (%s)" % (page, comment))
+
+    common.save_to_wiki_or_local(page, summary, text)
 
 
 def updateMonument(contents, source, countryconfig, conn, cursor, sourcePage):
@@ -528,7 +525,7 @@
         else:
             raise Exception(
                 u'Bad parameters. Expected "-countrycode", "-langcode", '
-                u'"-daysback", "-fullupdate" or pywikibot args. '
+                u'"-daysback", "-fullupdate", "-skip_wd" or pywikibot args. '
                 u'Found "{}"'.format(option))
 
     if countrycode and lang:
@@ -543,8 +540,7 @@
                 countrycode, lang))
         try:
             countryconfig = mconfig.countries.get((countrycode, lang))
-            processCountry(countryconfig, conn, cursor,
-                           fullUpdate, daysBack)
+            processCountry(countryconfig, conn, cursor, fullUpdate, daysBack)
         except Exception, e:
             pywikibot.error(
                 u"Unknown error occurred when processing country "
@@ -561,8 +557,8 @@
                 u'Working on countrycode "%s" in language "%s"' % (
                     countrycode, lang))
             try:
-                processCountry(countryconfig, conn, cursor,
-                               fullUpdate, daysBack)
+                processCountry(countryconfig, conn, cursor, fullUpdate,
+                               daysBack)
             except Exception, e:
                 pywikibot.error(
                     u"Unknown error occurred when processing country "
diff --git a/requirements.txt b/requirements.txt
index 6f0822c..b698dc0 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1,4 @@
+future
 pywikibot==3.0.20170403
 MySQL-python
 requests
diff --git a/tests/test_common.py b/tests/test_common.py
index 2f016c0..529d927 100644
--- a/tests/test_common.py
+++ b/tests/test_common.py
@@ -2,6 +2,9 @@
 
 import unittest
 import mock
+import tempfile
+import os
+import pywikibot
 from erfgoedbot import common
 
 
@@ -73,3 +76,107 @@
         self.mock_get_source.return_value = ('Q123', ('wikidata', 'www'))
         result = common.get_source_link('a link', 'sparql', 'bar')
         self.assertEquals(result, '[[:d:Q123|bar]]')
+
+
+class TestPageToFilename(unittest.TestCase):
+
+    def test_page_to_filename_commons(self):
+        site = pywikibot.Site('commons', 'commons')
+        page = pywikibot.Page(site, 'Foo')
+        self.assertEquals(
+            common.page_to_filename(page),
+            '[commons_commons][_]Foo.wiki'
+        )
+
+    def test_page_to_filename_wikipedia(self):
+        site = pywikibot.Site('en', 'wikipedia')
+        page = pywikibot.Page(site, 'Foo')
+        self.assertEquals(
+            common.page_to_filename(page),
+            '[wikipedia_en][_]Foo.wiki'
+        )
+
+    def test_page_to_filename_namespace(self):
+        site = pywikibot.Site('commons', 'commons')
+        page = pywikibot.Page(site, 'Template:Foo')
+        self.assertEquals(
+            common.page_to_filename(page),
+            '[commons_commons][Template]Foo.wiki'
+        )
+
+    def test_page_to_filename_subpage(self):
+        site = pywikibot.Site('commons', 'commons')
+        page = pywikibot.Page(site, 'Foo/Bar')
+        self.assertEquals(
+            common.page_to_filename(page),
+            '[commons_commons][_]Foo_Bar.wiki'
+        )
+
+    def test_page_to_filename_with_spaces(self):
+        site = pywikibot.Site('commons', 'commons')
+        page = pywikibot.Page(site, 'Foo bar')
+        self.assertEquals(
+            common.page_to_filename(page),
+            '[commons_commons][_]Foo_bar.wiki'
+        )
+
+
+class TestSaveToWikiOrLocal(unittest.TestCase):
+
+    def setUp(self):
+        site = pywikibot.Site('test', 'wikipedia')
+        self.page = pywikibot.Page(site, 'Foo')
+
+        patcher = mock.patch('erfgoedbot.common.page_to_filename')
+        self.mock_page_to_filename = patcher.start()
+        self.mock_page_to_filename.return_value = 'filename'
+        self.addCleanup(patcher.stop)
+
+        # Create a temporary file
+        self.test_outfile = tempfile.NamedTemporaryFile(delete=False)
+        patcher = mock.patch('erfgoedbot.common.os.path.join')
+        self.mock_join = patcher.start()
+        self.mock_join.return_value = self.test_outfile.name
+        self.addCleanup(patcher.stop)
+
+        # Ensure tests don't write
+        patcher = mock.patch('erfgoedbot.common.pywikibot.Page.put')
+        self.mock_page_put = patcher.start()
+        self.addCleanup(patcher.stop)
+
+        # Mock environment variable
+        patcher = mock.patch('erfgoedbot.common.os.environ.get')
+        self.mock_environ_get = patcher.start()
+        self.mock_environ_get.return_value = None
+        self.addCleanup(patcher.stop)
+
+    def tearDown(self):
+        # Closes and removes the file
+        os.remove(self.test_outfile.name)
+
+    def test_save_to_wiki_or_local_write_to_wiki(self):
+        summary = 'a summary'
+        content = 'The content'
+        common.save_to_wiki_or_local(self.page, summary, content)
+        self.mock_environ_get.assert_called_once_with(
+            'HERITAGE_LOCAL_WRITE_PATH')
+        self.mock_page_put.assert_called_once_with(
+            newtext=content, summary=summary, minorEdit=True)
+        self.mock_page_to_filename.assert_not_called()
+        self.mock_join.assert_not_called()
+        self.assertEquals(self.test_outfile.read(), '')
+
+    def test_save_to_wiki_or_local_write_locally(self):
+        summary = 'a summary'
+        content = u'The content'
+        self.mock_environ_get.return_value = 'something'
+        common.save_to_wiki_or_local(self.page, summary, content)
+        self.mock_environ_get.assert_called_once_with(
+            'HERITAGE_LOCAL_WRITE_PATH')
+        self.mock_page_put.assert_not_called()
+        self.mock_page_to_filename.assert_called_once_with(self.page)
+        self.mock_join.assert_called_once_with('something', 'filename')
+        self.assertEquals(
+            self.test_outfile.read(),
+            '#summary: a summary\n---------------\nThe content'
+        )

-- 
To view, visit https://gerrit.wikimedia.org/r/378800
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I2bf650f99a57a0e93dbb0c3d6f520049c3579957
Gerrit-PatchSet: 6
Gerrit-Project: labs/tools/heritage
Gerrit-Branch: master
Gerrit-Owner: Lokal Profil <[email protected]>
Gerrit-Reviewer: Jean-Frédéric <[email protected]>
Gerrit-Reviewer: Lokal Profil <[email protected]>
Gerrit-Reviewer: Multichill <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to