# Path: pages/parser.py
# def parse_page(dirpath):
#     """ Parse a page given a relative file path """
#     filename, msg = read_content_file(dirpath)
# 
#     content = msg.get_payload()
#     content_type = determine_page_content_type(content)
# 
#     data = {
#         'headers': dict(msg.items()),
#         'content': content,
#         'content_type': content_type,
#         'filename': filename,
#     }
# 
#     return data


import re
import shutil
import os
import traceback
from django.core.management.base import BaseCommand
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from bs4 import BeautifulSoup
from ...models import Page, Image
from ...parser import parse_page

            Image.objects.get_or_create(
                page=page,
                image=dst
            )
        wrapper = BeautifulSoup('<div>', 'lxml')
        [wrapper.div.append(el) for el in soup.body.contents]
        page.content = "%s" % wrapper.div
        page.content_markup_type = 'html'
        page.save()

    def handle(self, *args, **kwargs):
        self.SVN_REPO_PATH = getattr(settings, 'PYTHON_ORG_CONTENT_SVN_PATH', None)
        if self.SVN_REPO_PATH is None:
            raise ImproperlyConfigured("PYTHON_ORG_CONTENT_SVN_PATH not defined in settings")

        matches = []
        for root, dirnames, filenames in os.walk(self.SVN_REPO_PATH):
            for filename in filenames:
                if re.match(r'(content\.(ht|rst)|body\.html)$', filename):
                    matches.append(os.path.join(root, filename))

        for match in matches:
            path = self._build_path(match)

            # Skip homepage
            if path == '':
                continue

            try:

# Path: pages/parser.py
# def parse_page(dirpath):
#     """ Parse a page given a relative file path """
#     filename, msg = read_content_file(dirpath)
# 
#     content = msg.get_payload()
#     content_type = determine_page_content_type(content)
# 
#     data = {
#         'headers': dict(msg.items()),
#         'content': content,
#         'content_type': content_type,
#         'filename': filename,
#     }
# 
#     return data


# Path: pages/management/commands/import_pages_from_svn.py
import re
import shutil
import os
import traceback
from django.core.management.base import BaseCommand
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from bs4 import BeautifulSoup
from ...models import Page, Image
from ...parser import parse_page


            Image.objects.get_or_create(
                page=page,
                image=dst
            )
        wrapper = BeautifulSoup('<div>', 'lxml')
        [wrapper.div.append(el) for el in soup.body.contents]
        page.content = "%s" % wrapper.div
        page.content_markup_type = 'html'
        page.save()

    def handle(self, *args, **kwargs):
        self.SVN_REPO_PATH = getattr(settings, 'PYTHON_ORG_CONTENT_SVN_PATH', None)
        if self.SVN_REPO_PATH is None:
            raise ImproperlyConfigured("PYTHON_ORG_CONTENT_SVN_PATH not defined in settings")

        matches = []
        for root, dirnames, filenames in os.walk(self.SVN_REPO_PATH):
            for filename in filenames:
                if re.match(r'(content\.(ht|rst)|body\.html)$', filename):
                    matches.append(os.path.join(root, filename))

        for match in matches:
            path = self._build_path(match)

            # Skip homepage
            if path == '':
                continue

            try:

                data = parse_page(os.path.dirname(match))