lists.arthurdejong.org
RSS feed

webcheck commit: r446 - in webcheck: . webcheck webcheck/plugins

[Date Prev][Date Next] [Thread Prev][Thread Next]

webcheck commit: r446 - in webcheck: . webcheck webcheck/plugins



Author: arthur
Date: Fri Oct  7 10:37:26 2011
New Revision: 446
URL: http://arthurdejong.org/viewvc/webcheck?revision=446&view=revision

Log:
move some file-handling functions to webcheck.util

Added:
   webcheck/webcheck/util.py
Modified:
   webcheck/cmd.py
   webcheck/webcheck/crawler.py
   webcheck/webcheck/plugins/__init__.py

Modified: webcheck/cmd.py
==============================================================================
--- webcheck/cmd.py     Fri Oct  7 10:35:54 2011        (r445)
+++ webcheck/cmd.py     Fri Oct  7 10:37:26 2011        (r446)
@@ -185,58 +185,6 @@
         sys.exit(1)
 
 
-def install_file(source, text=False):
-    """Install the given file in the output directory.
-    If the text flag is set to true it is assumed the file is text,
-    translating line endings."""
-    import shutil
-    import urlparse
-    # figure out mode to open the file with
-    mode = 'r'
-    if text:
-        mode += 'U'
-    # check with what kind of argument we are called
-    scheme = urlparse.urlsplit(source)[0]
-    if scheme == 'file':
-        # this is a file:/// url, translate to normal path and open
-        import urllib
-        source = urllib.url2pathname(urlparse.urlsplit(source)[2])
-    elif scheme == '' and os.path.isabs(source):
-        # this is an absolute path, just open it as is
-        pass
-    elif scheme == '':
-        # this is a relavite path, try to fetch it from the python path
-        for directory in sys.path:
-            tst = os.path.join(directory, source)
-            if os.path.isfile(tst):
-                source = tst
-                break
-    # TODO: support more schemes here
-    # figure out the destination name
-    target = os.path.join(config.OUTPUT_DIR, os.path.basename(source))
-    # test if source and target are the same
-    source = os.path.realpath(source)
-    if source == os.path.realpath(target):
-        debugio.warn('attempt to overwrite %(fname)s with itself' % {'fname': 
source})
-        return
-    # open the input file
-    sfp = None
-    try:
-        sfp = open(source, mode)
-    except IOError, (errno, strerror):
-        debugio.error('%(fname)s: %(strerror)s' %
-                      {'fname': source,
-                       'strerror': strerror})
-        sys.exit(1)
-    # create file in output directory (with overwrite question)
-    tfp = webcheck.plugins.open_file(os.path.basename(source))
-    # copy contents
-    shutil.copyfileobj(sfp, tfp)
-    # close files
-    tfp.close()
-    sfp.close()
-
-
 def main(site):
     """Main program."""
     # crawl through the website
@@ -254,9 +202,6 @@
     # for every plugin, generate a page
     site.generate()
     # put extra files in the output directory
-    install_file('webcheck.css', True)
-    install_file('fancytooltips/fancytooltips.js', True)
-    install_file('favicon.ico', False)
     debugio.info('done.')
 
 

Modified: webcheck/webcheck/crawler.py
==============================================================================
--- webcheck/webcheck/crawler.py        Fri Oct  7 10:35:54 2011        (r445)
+++ webcheck/webcheck/crawler.py        Fri Oct  7 10:37:26 2011        (r446)
@@ -41,6 +41,7 @@
 from webcheck.db import Session, Link, LinkProblem, PageProblem, children, \
                         embedded
 from webcheck import debugio
+from webcheck.util import install_file
 import webcheck.config
 import webcheck.parsers
 
@@ -420,3 +421,7 @@
             if hasattr(pluginmod, 'generate'):
                 debugio.info('  ' + plugin)
                 pluginmod.generate(self)
+        # install theme files
+        install_file('webcheck.css', True)
+        install_file('fancytooltips/fancytooltips.js', True)
+        install_file('favicon.ico', False)

Modified: webcheck/webcheck/plugins/__init__.py
==============================================================================
--- webcheck/webcheck/plugins/__init__.py       Fri Oct  7 10:35:54 2011        
(r445)
+++ webcheck/webcheck/plugins/__init__.py       Fri Oct  7 10:37:26 2011        
(r446)
@@ -52,6 +52,7 @@
 import webcheck
 from webcheck.db import Link
 from webcheck.parsers.html import htmlescape
+from webcheck.util import open_file
 import webcheck.config
 import webcheck.debugio
 
@@ -160,55 +161,6 @@
       indent + '</div>\n')
 
 
-def open_file(filename, istext=True, makebackup=False):
-    """This returns an open file object which can be used for writing. This
-    file is created in the output directory. The output directory (stored in
-    webcheck.config.OUTPUT_DIR is created if it does not yet exist. If the 
second
-    parameter is True (default) the file is opened as an UTF-8 text file."""
-    import os
-    # check if output directory exists and create it if needed
-    if not os.path.isdir(webcheck.config.OUTPUT_DIR):
-        try:
-            os.mkdir(webcheck.config.OUTPUT_DIR)
-        except OSError, (errno, strerror):
-            debugio.error('error creating directory %(dir)s: %(strerror)s' %
-                          {'dir': webcheck.config.OUTPUT_DIR,
-                           'strerror': strerror})
-            sys.exit(1)
-    # build the output file name
-    fname = os.path.join(webcheck.config.OUTPUT_DIR, filename)
-    # check if file exists
-    if os.path.exists(fname):
-        if makebackup:
-            # create backup of original (overwriting previous backup)
-            os.rename(fname, fname + '~')
-        elif not webcheck.config.OVERWRITE_FILES:
-            # ask to overwrite
-            try:
-                res = raw_input('webcheck: overwrite %s? [y]es, [a]ll, [q]uit: 
' % fname)
-            except EOFError:
-                # bail out in case raw_input() failed
-                debugio.error('error reading response')
-                res = 'q'
-            res = res.lower() + ' '
-            if res[0] == 'a':
-                webcheck.config.OVERWRITE_FILES = True
-            elif res[0] != 'y':
-                print 'Aborted.'
-                sys.exit(1)
-    # open the file for writing
-    try:
-        if istext:
-            return open(fname, 'w')
-        else:
-            return open(fname, 'wb')
-    except IOError, (errno, strerror):
-        debugio.error('error creating output file %(fname)s: %(strerror)s' %
-                      {'fname': fname,
-                       'strerror': strerror})
-        sys.exit(1)
-
-
 def _print_navbar(fp, selected):
     """Return an html fragement representing the navigation bar for a page."""
     fp.write('  <ul class="navbar">\n')

Added: webcheck/webcheck/util.py
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ webcheck/webcheck/util.py   Fri Oct  7 10:37:26 2011        (r446)
@@ -0,0 +1,108 @@
+
+# util.py - utility functions for webcheck
+#
+# Copyright (C) 1998, 1999 Albert Hopkins (marduk)
+# Copyright (C) 2002 Mike W. Meyer
+# Copyright (C) 2005, 2006, 2007, 2008, 2010, 2011 Arthur de Jong
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+#
+# The files produced as output from the software do not automatically fall
+# under the copyright of the software, unless explicitly stated otherwise.
+
+import os
+import shutil
+import sys
+import urllib
+import urlparse
+
+from webcheck import config, debugio
+
+
+def open_file(filename, istext=True, makebackup=False):
+    """This returns an open file object which can be used for writing. This
+    file is created in the output directory. The output directory (stored in
+    webcheck.config.OUTPUT_DIR is created if it does not yet exist. If the 
second
+    parameter is True (default) the file is opened as an UTF-8 text file."""
+    # check if output directory exists and create it if needed
+    if not os.path.isdir(config.OUTPUT_DIR):
+        os.mkdir(config.OUTPUT_DIR)
+    # build the output file name
+    fname = os.path.join(config.OUTPUT_DIR, filename)
+    # check if file exists
+    if os.path.exists(fname):
+        if makebackup:
+            # create backup of original (overwriting previous backup)
+            os.rename(fname, fname + '~')
+        elif not config.OVERWRITE_FILES:
+            # ask to overwrite
+            try:
+                res = raw_input('webcheck: overwrite %s? [y]es, [a]ll, [q]uit: 
' % fname)
+            except EOFError:
+                # bail out in case raw_input() failed
+                debugio.error('error reading response')
+                res = 'q'
+            res = res.lower() + ' '
+            if res[0] == 'a':
+                config.OVERWRITE_FILES = True
+            elif res[0] != 'y':
+                raise SystemExit('Aborted.')
+    # open the file for writing
+    if istext:
+        return open(fname, 'w')
+    else:
+        return open(fname, 'wb')
+
+
+def install_file(source, text=False):
+    """Install the given file in the output directory.
+    If the text flag is set to true it is assumed the file is text,
+    translating line endings."""
+    # figure out mode to open the file with
+    mode = 'r'
+    if text:
+        mode += 'U'
+    # check with what kind of argument we are called
+    scheme = urlparse.urlsplit(source)[0]
+    if scheme == 'file':
+        # this is a file:/// url, translate to normal path and open
+        source = urllib.url2pathname(urlparse.urlsplit(source)[2])
+    elif scheme == '' and os.path.isabs(source):
+        # this is an absolute path, just open it as is
+        pass
+    elif scheme == '':
+        # this is a relavite path, try to fetch it from the python path
+        for directory in sys.path:
+            tst = os.path.join(directory, source)
+            if os.path.isfile(tst):
+                source = tst
+                break
+    # TODO: support more schemes here
+    # figure out the destination name
+    target = os.path.join(config.OUTPUT_DIR, os.path.basename(source))
+    # test if source and target are the same
+    source = os.path.realpath(source)
+    if source == os.path.realpath(target):
+        debugio.warn('attempt to overwrite %(fname)s with itself' % {'fname': 
source})
+        return
+    # open the input file
+    sfp = open(source, mode)
+    # create file in output directory (with overwrite question)
+    tfp = open_file(os.path.basename(source))
+    # copy contents
+    shutil.copyfileobj(sfp, tfp)
+    # close files
+    tfp.close()
+    sfp.close()
-- 
To unsubscribe send an email to
webcheck-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/webcheck-commits/