0
|
1 |
#
|
|
2 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
#
|
|
16 |
|
|
17 |
import os
|
|
18 |
import sys
|
|
19 |
import logging
|
|
20 |
|
|
21 |
from cone.public import utils
|
|
22 |
|
|
23 |
class Output(object):
|
|
24 |
"""
|
|
25 |
Class representing an ExampleML output element.
|
|
26 |
"""
|
|
27 |
|
3
|
28 |
def __init__(self, file, encoding, text, lineno=None):
|
0
|
29 |
self.file = file
|
|
30 |
self.encoding = encoding
|
|
31 |
self.text = text
|
3
|
32 |
self.lineno = lineno
|
0
|
33 |
|
|
34 |
def get_refs(self):
|
|
35 |
"""
|
|
36 |
Return a list of all ConfML refs used in this output object.
|
|
37 |
"""
|
|
38 |
return utils.extract_delimited_tokens(self.text)
|
|
39 |
|
|
40 |
def get_output_file(self, output_dir, config):
|
|
41 |
"""
|
|
42 |
Return the path of the output file specified by this output object.
|
|
43 |
"""
|
|
44 |
# Expand ConfML references
|
|
45 |
file = utils.expand_refs_by_default_view(self.file, config.get_default_view())
|
|
46 |
return os.path.normpath(os.path.join(output_dir, file))
|
|
47 |
|
3
|
48 |
def write_to_file(self, output_dir, context):
|
0
|
49 |
"""
|
|
50 |
Write the text file specified by this output object to the
|
|
51 |
given output directory.
|
|
52 |
"""
|
|
53 |
# Get the actual output path and encoding
|
3
|
54 |
file_path = self.get_output_file(output_dir, context.configuration)
|
|
55 |
encoding = utils.expand_refs_by_default_view(self.encoding, context.configuration.get_default_view())
|
0
|
56 |
|
|
57 |
# Generate the binary data to write
|
3
|
58 |
text = utils.expand_refs_by_default_view(self.text, context.configuration.get_default_view())
|
0
|
59 |
data = text.encode(encoding)
|
|
60 |
|
|
61 |
|
|
62 |
# Write the file.
|
3
|
63 |
f = context.create_file(file_path, mode="wb")
|
0
|
64 |
try: f.write(data)
|
|
65 |
finally: f.close()
|
|
66 |
|
|
67 |
def __eq__(self, other):
|
|
68 |
if type(self) is type(other):
|
|
69 |
for varname in ('file', 'encoding', 'text'):
|
|
70 |
if getattr(self, varname) != getattr(other, varname):
|
|
71 |
return False
|
|
72 |
return True
|
|
73 |
else:
|
|
74 |
return False
|
|
75 |
|
|
76 |
def __ne__(self, other):
|
|
77 |
return not (self == other)
|
|
78 |
|
|
79 |
def __repr__(self):
|
|
80 |
return "Output(file=%r, encoding=%r, text=%r)" % (self.file, self.encoding, self.text)
|