allura
修訂 | f52a58f08cd6111964729c8319cad2ba834c5536 (tree) |
---|---|
時間 | 2012-06-28 19:36:05 |
作者 | Igor Bondarenko <jetmind2@gmai...> |
Commiter | Igor Bondarenko |
[#4186] ticket:96 Added error handling
@@ -3,6 +3,7 @@ import os | ||
3 | 3 | import shutil |
4 | 4 | import json |
5 | 5 | import hashlib |
6 | +from MySQLdb import DatabaseError | |
6 | 7 | |
7 | 8 | from allura.command import base as allura_base |
8 | 9 |
@@ -41,7 +42,11 @@ class MySQLExtractor(MediawikiExtractor): | ||
41 | 42 | |
42 | 43 | def connection(self): |
43 | 44 | if not self._connection: |
44 | - self._connection = MySQLdb.connect(**self.db_options) | |
45 | + try: | |
46 | + self._connection = MySQLdb.connect(**self.db_options) | |
47 | + except DatabaseError, e: | |
48 | + allura_base.log.error("Can't connect to database: %s" % str(e)) | |
49 | + exit(2) | |
45 | 50 | return self._connection |
46 | 51 | |
47 | 52 | def _save(self, content, *paths): |
@@ -61,8 +61,16 @@ class MediawikiLoader(object): | ||
61 | 61 | pages.sort() # ensure that history in right order |
62 | 62 | for page in pages: |
63 | 63 | fn = os.path.join(page_dir, page) |
64 | - with open(fn, 'r') as pages_file: | |
65 | - page_data = json.load(pages_file) | |
64 | + try: | |
65 | + with open(fn, 'r') as pages_file: | |
66 | + page_data = json.load(pages_file) | |
67 | + except IOError, e: | |
68 | + allura_base.log.error("Can't open file: %s" % str(e)) | |
69 | + exit(2) | |
70 | + except ValueError, e: | |
71 | + allura_base.log.error("Can't load data from file %s: %s" | |
72 | + % (fn, str(e))) | |
73 | + exit(2) | |
66 | 74 | yield page_data |
67 | 75 | |
68 | 76 | def _talk(self, page_dir): |
@@ -70,8 +78,16 @@ class MediawikiLoader(object): | ||
70 | 78 | filename = os.path.join(page_dir, 'discussion.json') |
71 | 79 | if not os.path.isfile(filename): |
72 | 80 | return |
73 | - with open(filename, 'r') as talk_file: | |
74 | - talk_data = json.load(talk_file) | |
81 | + try: | |
82 | + with open(filename, 'r') as talk_file: | |
83 | + talk_data = json.load(talk_file) | |
84 | + except IOError, e: | |
85 | + allura_base.log.error("Can't open file: %s" % str(e)) | |
86 | + exit(2) | |
87 | + except ValueError, e: | |
88 | + allura_base.log.error("Can't load data from file %s: %s" | |
89 | + % (filename, str(e))) | |
90 | + exit(2) | |
75 | 91 | return talk_data |
76 | 92 | |
77 | 93 | def _attachments(self, page_dir): |
@@ -142,7 +158,11 @@ class MediawikiLoader(object): | ||
142 | 158 | page = WM.Page.query.get(app_config_id=self.wiki.config._id, |
143 | 159 | title=page_title) |
144 | 160 | for filename, path in self._attachments(page_dir): |
145 | - with open(path) as fp: | |
146 | - page.attach(filename, fp, | |
147 | - content_type=utils.guess_mime_type(filename)) | |
161 | + try: | |
162 | + with open(path) as fp: | |
163 | + page.attach(filename, fp, | |
164 | + content_type=utils.guess_mime_type(filename)) | |
165 | + except IOError, e: | |
166 | + allura_base.log.error("Can't open file: %s" % str(e)) | |
167 | + exit(2) | |
148 | 168 | allura_base.log.info('Loaded attachments for page %s.' % page_title) |
@@ -23,13 +23,14 @@ def test_mediawiki2markdown(): | ||
23 | 23 | mediawiki_text = """ |
24 | 24 | '''bold''' ''italics'' |
25 | 25 | == Getting started == |
26 | -* [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list] | |
26 | +* [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration] | |
27 | 27 | * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ] |
28 | 28 | """ |
29 | 29 | mediawiki_output = converters.mediawiki2markdown(mediawiki_text) |
30 | 30 | assert "**bold** _italics_" in mediawiki_output |
31 | 31 | assert "## Getting started" in mediawiki_output |
32 | - assert "* [MediaWiki FAQ](http://www.mediawiki.org/wiki/Manual:FAQ)" in mediawiki_output | |
32 | + assert ("* [MediaWiki FAQ](http://www.mediawiki.org/wiki/Manual:FAQ)" | |
33 | + in mediawiki_output) | |
33 | 34 | |
34 | 35 | |
35 | 36 | def test_mediawiki_internal_links2markdown(): |