Python LMF library
 All Classes Namespaces Files Functions Variables
odt.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 """! @package output
5 """
6 
7 from config.odt import lmf_to_odt
8 from utils.error_handling import OutputError
9 from utils.io import open_read
10 
11 from odf.opendocument import OpenDocumentText
12 from odf.style import Style, TextProperties
13 from odf.text import H, P, Span
14 
15 def file_read(filename):
16  """! @brief Read file contents.
17  @param filename The name of the file with full path containing information to read, for instance the text introduction of the document: 'user/config/introduction.txt'.
18  @return A Python string containing read information.
19  """
20  contents = ""
21  if filename is not None:
22  file = open_read(filename)
23  contents = file.read()
24  file.close()
25  return contents
26 
27 def odt_write(object, filename, introduction=None, lmf2odt=lmf_to_odt, items=lambda lexical_entry: lexical_entry.get_lexeme(), sort_order=None, paradigms=False, reverse=False):
28  """! @brief Write a document file.
29  @param object The LMF instance to convert into document output format.
30  @param filename The name of the document file to write with full path, for instance 'user/output.odt'.
31  @param introduction The name of the text file with full path containing the introduction of the document, for instance 'user/config/introduction.txt'. Default value is None.
32  @param lmf2odt A function giving the mapping from LMF representation information that must be written to ODT commands, in a defined order. Default value is 'lmf_to_odt' function defined in 'pylmflib/config/odt.py'. Please refer to it as an example.
33  @param items Lambda function giving the item to sort. Default value is 'lambda lexical_entry: lexical_entry.get_lexeme()', which means that the items to sort are lexemes.
34  @param sort_order Python list. Default value is 'None', which means that the document output is alphabetically ordered.
35  @param paradigms A boolean value to introduce paradigms in document or not.
36  @param reverse A boolean value to set if a reverse dictionary is wanted.
37  """
38  import string
39  if sort_order is None:
40  # Lowercase and uppercase letters must have the same rank
41  sort_order = dict([(c, ord(c)) for c in string.lowercase])
42  up = dict([(c, ord(c) + 32) for c in string.uppercase])
43  sort_order.update(up)
44  sort_order.update({'':0, ' ':0})
45  textdoc = OpenDocumentText()
46  # Styles
47  s = textdoc.styles
48  h1style = Style(name="Heading 1", family="paragraph")
49  h1style.addElement(TextProperties(attributes={'fontsize':"24pt", 'fontweight':"bold" }))
50  s.addElement(h1style)
51  # An automatic style
52  boldstyle = Style(name="Bold", family="text")
53  boldprop = TextProperties(fontweight="bold", fontname="Arial", fontsize="8pt")
54  boldstyle.addElement(boldprop)
55  textdoc.automaticstyles.addElement(boldstyle)
56  # Parse LMF values
57  if object.__class__.__name__ == "LexicalResource":
58  for lexicon in object.get_lexicons():
59  # Document title
60  h = H(outlinelevel=1, stylename=h1style, text=lexicon.get_id())
61  textdoc.text.addElement(h)
62  # Plain paragraph
63  p = P(text=lexicon.get_label())
64  # Text
65  boldpart = Span(stylename=boldstyle, text="Test. ")
66  p.addElement(boldpart)
67  # Introduction
68  if introduction is not None:
69  p.addText(file_read(introduction))
70  textdoc.text.addElement(p)
71  # Page break
72  #
73  # Text body
74  lmf2odt(lexicon, textdoc, items, sort_order, paradigms, reverse)
75  else:
76  raise OutputError(object, "Object to write must be a Lexical Resource.")
77  textdoc.save(filename)
def file_read
Read file contents.
Definition: odt.py:15
def open_read
Open file in read mode (automatically decode file in unicode).
Definition: io.py:36
def odt_write
Write a document file.
Definition: odt.py:27