summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjoern Tropf <asymmail@googlemail.com>2009-06-25 14:44:31 +0200
committerBjoern Tropf <asymmail@googlemail.com>2009-06-25 14:44:31 +0200
commitc3c48a49b6a2c7b79cdafa40fc5709fd346fd2b0 (patch)
tree28cb309a3d846014c997387f9bde95ed82834e45
parentAdd status to vulnerability (diff)
downloadkernel-check-c3c48a49b6a2c7b79cdafa40fc5709fd346fd2b0.tar.gz
kernel-check-c3c48a49b6a2c7b79cdafa40fc5709fd346fd2b0.tar.bz2
kernel-check-c3c48a49b6a2c7b79cdafa40fc5709fd346fd2b0.zip
Add guidexml class
-rw-r--r--TODO1
-rw-r--r--guidexml.py311
-rw-r--r--testguidexml.py22
3 files changed, 334 insertions, 0 deletions
diff --git a/TODO b/TODO
index a3e89e6..b4e0349 100644
--- a/TODO
+++ b/TODO
@@ -5,6 +5,7 @@ Todo
- Rename functions
- Rework interval class and all interval functions (expand?)
- Rework cves.refs
+- class(object) (new class)
Clean code
==========
diff --git a/guidexml.py b/guidexml.py
new file mode 100644
index 0000000..fed852d
--- /dev/null
+++ b/guidexml.py
@@ -0,0 +1,311 @@
+#! /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(['<!-- The content of this document is licensed under the CC-BY-SA license -->\n',
+'<!-- See http://creativecommons.org/licenses/by-sa/2.5 -->\n<license/>'])
+
+TAGTYPES = ['p', 'c', 'b', 'e', 'i', 'pre', 'path', 'uri', 'note', 'warn', 'impo',
+ 'comment', 'sub', 'sup', 'keyword', 'ident', 'const', 'stmt', 'var']
+
+#pre only in first body
+#<pre caption="Code Sample">
+
+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('<?xml version="%s" encoding="%s"?>\n' % (self.docversion, self.encoding))
+ output.append('<!DOCTYPE guide SYSTEM "%s">\n<!-- $Header$ -->\n\n' % (self.doctype))
+ output.append('<guide>\n<title>%s</title>\n\n' % (self.title))
+
+ for item in self.authors:
+ output.append('<author title="%s">\n' % (item.title))
+ if item.email is not None:
+ output.append(' <mail link="%s">%s</mail>\n</author>' % (item.email, item.name))
+ else:
+ output.append(' %s\n</author>' % (item.name))
+
+ output.append('\n\n<abstract>\n%s\n</abstract>' % (linebreak(self.abstract)))
+ output.append('\n\n%s\n\n<version>%s</version>\n' % (self.license, self.version))
+ output.append('<date>%s</date>\n\n' % (self.date))
+
+ if len(self.chapters) > 0:
+ for chapter in self.chapters:
+ output.append('<chapter>\n')
+ if len(chapter.sections) > 0:
+ for section in chapter.sections:
+ output.append('<section>\n')
+ output.append('<body>\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</body>\n')
+ output.append('</section>\n')
+ output.append('</chapter>\n')
+
+ output.append('</guide>')
+
+ 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)
+
diff --git a/testguidexml.py b/testguidexml.py
new file mode 100644
index 0000000..a84ed36
--- /dev/null
+++ b/testguidexml.py
@@ -0,0 +1,22 @@
+#! /usr/bin/env python
+
+import guidexml
+
+doc = guidexml.Document('mydocument', '2.0', 'Sehr nuetzliches infos', 'en', '/doc/en/bla')
+
+doc.addAuthor('creator', 'asyme', 'asymmail@googlemail.de')
+doc.addAuthor('creator', 'rbu')
+
+chap = guidexml.Chapter('1 chapter')
+sect = guidexml.Section('1 section')
+sect = guidexml.Section('1 section')
+
+taguri = guidexml.uri("http://google.de")
+tagpara = guidexml.paragraph("hello world, Visit: %s. das ist ein sehr langer text mit mehr als MAXLENGTH Zeichen." % repr(taguri))
+
+doc.append(chap)
+chap.append(sect)
+sect.append(tagpara)
+
+doc.create('/', 'file')
+