configurationengine/source/plugins/example/ConeExamplePlugin/examplemlplugin/exampleml_model.py
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     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     
       
    28     def __init__(self, file, encoding, text):
       
    29         self.file = file
       
    30         self.encoding = encoding
       
    31         self.text = text
       
    32     
       
    33     def get_refs(self):
       
    34         """
       
    35         Return a list of all ConfML refs used in this output object.
       
    36         """
       
    37         return utils.extract_delimited_tokens(self.text)
       
    38     
       
    39     def get_output_file(self, output_dir, config):
       
    40         """
       
    41         Return the path of the output file specified by this output object.
       
    42         """
       
    43         # Expand ConfML references
       
    44         file = utils.expand_refs_by_default_view(self.file, config.get_default_view())
       
    45         return os.path.normpath(os.path.join(output_dir, file))
       
    46     
       
    47     def write_to_file(self, output_dir, config):
       
    48         """
       
    49         Write the text file specified by this output object to the
       
    50         given output directory.
       
    51         """
       
    52         # Get the actual output path and encoding
       
    53         file_path = self.get_output_file(output_dir, config)
       
    54         encoding = utils.expand_refs_by_default_view(self.encoding, config.get_default_view())
       
    55         
       
    56         # Generate the binary data to write
       
    57         text = utils.expand_refs_by_default_view(self.text, config.get_default_view())
       
    58         data = text.encode(encoding)
       
    59         
       
    60         # Make sure that the output directory exists
       
    61         dir = os.path.dirname(file_path)
       
    62         if dir != '' and not os.path.exists(dir):
       
    63             os.makedirs(dir)
       
    64         
       
    65         # Write the file.
       
    66         f = open(file_path, "wb")
       
    67         try:        f.write(data)
       
    68         finally:    f.close()
       
    69     
       
    70     def __eq__(self, other):
       
    71         if type(self) is type(other):
       
    72             for varname in ('file', 'encoding', 'text'):
       
    73                 if getattr(self, varname) != getattr(other, varname):
       
    74                     return False
       
    75             return True
       
    76         else:
       
    77             return False
       
    78     
       
    79     def __ne__(self, other):
       
    80         return not (self == other)
       
    81     
       
    82     def __repr__(self):
       
    83         return "Output(file=%r, encoding=%r, text=%r)" % (self.file, self.encoding, self.text)