#!/usr/bin/env python
# guidexml -- guidexml class for python
# Copyright 2009-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
import datetime
import os
import xml.etree.cElementTree
MAXLENGTH = 80
LANGUAGES = ['en', 'de']
LICENSE = ''.join(['\n',
'\n
class Document(object): title = str() abstract = str() docversion = str() encoding = str() doctype = str() date = str() version = str() license = str() link = str() lang = str() chapters = list() authors = list() def __init__(self, title, version, abstract, lang, link, docversion='1.0', encoding='UTF-8', doctype='/dtd/guide.dtd', date=None, license=None): if type(title) is not str: raise TypeError if type(abstract) is not str: raise TypeError if type(docversion) is not str: raise TypeError if type(encoding) is not str: raise TypeError if type(doctype) is not str: raise TypeError if type(version) is not str: raise TypeError if type(date) is not str and date is not None: raise TypeError if type(license) is not str and license is not None: raise TypeError if type(link) is not str: raise TypeError if type(lang) is not str: raise TypeError if lang not in LANGUAGES: raise ValueError self.title = title self.abstract = abstract self.docversion = docversion self.encoding = encoding self.doctype = doctype self.version = version self.link = link self.lang = lang if date is None: self.date = datetime.datetime.now().isoformat()[:10] else: self.date = date #TODO check YYYY-MM-DD if license is None: self.license = LICENSE else: self.license = license def addAuthor(self, title, name, email=None): self.authors.append(Author(title, name, email)) def append(self, chapter): if type(chapter) is not Chapter: raise TypeError else: self.chapters.append(chapter) def create(self, path, filename): xmlfile = os.path.join(path, filename) #TODO output = list() if len(self.authors) == 0: raise ValueError output.append('\n' % (self.docversion, self.encoding)) output.append('\n\n\n' % (self.doctype)) output.append('\n ') print ''.join(output) #TODO def linebreak(text): if len(text) <= MAXLENGTH: return text linebreak = str() i = 0; while i < len(text): if i + MAXLENGTH < len(text): linebreak += ''.join([text[i:i + MAXLENGTH], '\n']) else: linebreak += text[i:i + MAXLENGTH] i += MAXLENGTH return linebreak class Author(object): title = str() name = str() email = str() def __init__(self, title, name, email=None): if type(title) is not str: raise TypeError if type(name) is not str: raise TypeError if type(email) is not str and email is not None: raise TypeError self.title = title self.name = name self.email = email class Chapter(object): title = None sections = list() def __init__(self, title): if type(title) is not str: raise TypeError self.title = title def append(self, section): if type(section) is not Section: raise TypeError else: self.sections.append(section) class Section(object): title = None bodys = list() def __init__(self, title): if type(title) is not str: raise TypeError self.title = title tags = list() def append(self, tag): if type(tag) is Tag: self.tags.append(tag) elif type(tag) is list: for item in tag: self.append(item) elif type(tag) is str: self.tags.append(Tag('text', tag)) else: raise TypeError class Tag(object): tagtype = str() text = str() def __init__(self, tagtype, text): if tagtype not in TAGTYPES: raise TypeError if type(text) is not str: raise TypeError self.tagtype = tagtype self.text = text def __repr__(self): if (len(self.tagtype) * 2 + 5 + len(self.text)) > MAXLENGTH: return '<%s>\n%s\n%s>' % (self.tagtype, self.text, self.tagtype) else: return '<%s>%s%s>' % (self.tagtype, self.text, self.tagtype) def preserve(text): return Tag('pre', text) def paragraph(text): return Tag('p', text) def warning(text): return Tag('warn', text) def important(text): return Tag('impo', text) def note(text): return Tag('note', text) def comment(text): return Tag('comment', text) def path(text): return Tag('path', text) def command(text): return Tag('c', text) def userinput(text): return Tag('i', text) def keyword(text): return Tag('keyword', text) def identifier(text): return Tag('ident', text) def constant(text): return Tag('const', text) def statement(text): return Tag('stmt', text) def variable(text): return Tag('var', text) def bold(text): return Tag('b', text) def emphasize(text): return Tag('e', text) def subscript(text): return Tag('sub', text) def superscript(text): return Tag('sup', text) def uri(text): return Tag('uri', text)%s \n\n' % (self.title)) for item in self.authors: output.append('\n' % (item.title)) if item.email is not None: output.append(' ' % (item.email, item.name)) else: output.append(' %s\n' % (item.name)) output.append('\n\n%s \n\n%s\n ' % (linebreak(self.abstract))) output.append('\n\n%s\n\n%s \n' % (self.license, self.version)) output.append('%s \n\n' % (self.date)) if len(self.chapters) > 0: for chapter in self.chapters: output.append('\n') if len(chapter.sections) > 0: for section in chapter.sections: output.append(' \n') output.append('\n') output.append('\n') for tag in section.tags: if tag.tagtype in ['pre', 'p']: output.append('\n%s\n' % repr(tag)) else: output.append(repr(tag)) output.append('\n\n') output.append(' \n') output.append('