configurationengine/source/cone/public/parsecontext.py
changeset 3 e7e0ae78773e
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
       
     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 threading
       
    18 import logging
       
    19 
       
    20 import cone.public.utils
       
    21 
       
    22 class ParseContext(object):
       
    23     """
       
    24     Parse context class for handling exceptions and problems encountered
       
    25     when parsing a file (ConfML, ImplML or anything included from those).
       
    26     
       
    27     The context contains the path to the file that is currently being parsed,
       
    28     but the file path can also be overridden when calling a handler method
       
    29     (e.g. an ImplML file includes some other file, which in contains a problem).
       
    30     
       
    31     Sub-classes should override _handle_exception() and _handle_problem(),
       
    32     NOT handle_exception() and handle_problem().
       
    33     """
       
    34     def __init__(self):
       
    35         #: The file that is currently being parsed.
       
    36         self.current_file = None
       
    37     
       
    38     def handle_exception(self, exception, file_path=None):
       
    39         """
       
    40         Handle an exception that occurred while parsing a file.
       
    41         
       
    42         @param exception: The exception that occurred.
       
    43         @param file_path: Override for the name of the file that was being parsed.
       
    44             If this is None, self.current_file is used.
       
    45         """
       
    46         if file_path is None:
       
    47             file_path = self.current_file
       
    48         
       
    49         self._handle_exception(exception, file_path)
       
    50     
       
    51     def handle_problem(self, problem, file_path=None):
       
    52         """
       
    53         Handle a Problem that occurred while parsing a file.
       
    54         
       
    55         @param problem: The problem object.
       
    56         @param file_path: Override for the name of the file that was being parsed.
       
    57             If this is None, self.current_file is used.
       
    58         """
       
    59         if file_path is None:
       
    60             file_path = self.current_file
       
    61         
       
    62         self._handle_problem(problem, file_path)
       
    63     
       
    64     def _handle_exception(self, exception, file_path):
       
    65         """
       
    66         Called to handle an exception that occurred while parsing a file.
       
    67         
       
    68         Note that this should always be called in an exception handler,
       
    69         so you can safely use e.g. traceback.format_exc().
       
    70         
       
    71         @param exception: The exception that occurred.
       
    72         @param file_path: The file that was being parsed.
       
    73         """
       
    74         cone.public.utils.log_exception(
       
    75             logging.getLogger('cone'),
       
    76             "Error parsing '%s': %s" % (file_path, str(exception)))
       
    77     
       
    78     def _handle_problem(self, problem, file_path):
       
    79         """
       
    80         Called when parsing a file yields a problem.
       
    81         @param problem: The problem object.
       
    82         @param file_path: Override for the name of the file that was being parsed.
       
    83             If this is None, self.current_file is used.
       
    84         """
       
    85         problem.file = file_path
       
    86         problem.log(logging.getLogger('cone'))
       
    87 
       
    88 # Thread-local storage for the contexts
       
    89 _contexts = threading.local()
       
    90 
       
    91 def _init_contexts():
       
    92     """
       
    93     Initialize thread-local contexts if they are not initialized yet.
       
    94     """
       
    95     if not hasattr(_contexts, 'implml'):
       
    96         _contexts.implml = None
       
    97         _contexts.confml = None
       
    98         _contexts.implml_default = ParseContext()
       
    99         _contexts.confml_default = ParseContext()
       
   100 
       
   101 def get_implml_context():
       
   102     """
       
   103     Get the current ImplML parse context.
       
   104     """
       
   105     _init_contexts()
       
   106     if _contexts.implml is not None:
       
   107         return _contexts.implml
       
   108     else:
       
   109         return _contexts.implml_default
       
   110 
       
   111 def set_implml_context(context):
       
   112     """
       
   113     Set the current ImplML parse context.
       
   114     @param context: The context to set. If None, the default context
       
   115         will be used.
       
   116     """
       
   117     _init_contexts()
       
   118     _contexts.implml = context
       
   119 
       
   120 def get_confml_context():
       
   121     """
       
   122     Get the current ConfML parse context.
       
   123     """
       
   124     _init_contexts()
       
   125     if _contexts.confml is not None:
       
   126         return _contexts.confml
       
   127     else:
       
   128         return _contexts.confml_default
       
   129 
       
   130 def set_confml_context(context):
       
   131     """
       
   132     Set the current ConfML parse context.
       
   133     @param context: The context to set. If None, the default context
       
   134         will be used.
       
   135     """
       
   136     _init_contexts()
       
   137     _contexts.confml = context