Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 896474: Generate forums from data file in SVN.
- Loading branch information
Showing
11 changed files
with
195 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import re | ||
|
||
from django.utils.functional import cached_property | ||
|
||
from ordereddict import OrderedDict | ||
|
||
from bedrock.externalfiles import ExternalFile | ||
|
||
|
||
class ForumsFile(ExternalFile): | ||
forum_line_re = re.compile(r'^((?:mozilla\.)?[a-z0-9\-\.]+)\s+(.*)$') | ||
title_line_re = re.compile(r'^:(.*)$') | ||
|
||
def validate_content(self, content): | ||
lines = content.split('\n') | ||
try: | ||
forums = self._parse(lines) | ||
except Exception: | ||
raise ValueError('Error parsing forums file.') | ||
|
||
# currently 15 categories | ||
if not len(forums.keys()) > 10: | ||
raise ValueError('Forums file truncated or corrupted.') | ||
|
||
return content | ||
|
||
@cached_property | ||
def ordered(self): | ||
return self._parse(self.readlines()) | ||
|
||
def _parse(self, lines): | ||
forums = OrderedDict() | ||
current_group = None | ||
for line in lines: | ||
line = line.strip() | ||
if not line: | ||
continue | ||
|
||
title_m = self.title_line_re.match(line) | ||
if title_m: | ||
current_group = forums[title_m.group(1)] = [] | ||
|
||
elif current_group is not None: | ||
forum_m = self.forum_line_re.match(line) | ||
if forum_m: | ||
forum_id, forum_desc = forum_m.groups() | ||
forum_id_dashed = re.sub(r'^mozilla\.', '', forum_id) | ||
forum_id_dashed = forum_id_dashed.replace('.', '-') | ||
current_group.append({ | ||
'id': forum_id, | ||
'dashed': forum_id_dashed, | ||
'description': forum_desc, | ||
}) | ||
|
||
return forums |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# -*- coding: utf-8 -*- | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
from ordereddict import OrderedDict | ||
|
||
from bedrock.mozorg import forums | ||
from bedrock.mozorg.tests import TestCase | ||
|
||
|
||
FORUMS_GOOD_CONTENT = [ | ||
':Particularly Important Forums\n', | ||
'\n', | ||
'mozilla.announce New releases will be announced here.\n', | ||
'mozilla.announce.compatibility Add-on compatibility announcements. (Moderated)\n', | ||
'\n', | ||
':Applications and Projects\n', | ||
'\n', | ||
'firefox-dev For development of Firefox.\n', | ||
] | ||
|
||
FORUMS_GOOD_DICT = OrderedDict() | ||
FORUMS_GOOD_DICT['Particularly Important Forums'] = [ | ||
{'id': 'mozilla.announce', 'dashed': 'announce', | ||
'description': 'New releases will be announced here.'}, | ||
{'id': 'mozilla.announce.compatibility', 'dashed': 'announce-compatibility', | ||
'description': 'Add-on compatibility announcements. (Moderated)'}, | ||
] | ||
FORUMS_GOOD_DICT['Applications and Projects'] = [ | ||
{'id': 'firefox-dev', 'dashed': 'firefox-dev', 'description': 'For development of Firefox.'}, | ||
] | ||
|
||
|
||
class TestForums(TestCase): | ||
def setUp(self): | ||
self.forums_file = forums.ForumsFile('forums') | ||
|
||
def test_forums_ordered(self): | ||
"""Should give an ordered dict of forums from file.""" | ||
for title, forums_list in self.forums_file._parse(FORUMS_GOOD_CONTENT).items(): | ||
for i, forum in enumerate(forums_list): | ||
self.assertDictEqual(forum, FORUMS_GOOD_DICT[title][i]) | ||
|
||
def test_forums_validation(self): | ||
with self.assertRaises(ValueError): | ||
# shouldn't be enough data | ||
self.forums_file.validate_content(''.join(FORUMS_GOOD_CONTENT)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters