vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / parser / loading / file.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import codecs
17
18 from .loader import Loader
19 from .exceptions import LoaderException, DocumentNotFoundException
20
21
22 class FileTextLoader(Loader):
23     """
24     ARIA file text loader.
25
26     Extracts a text document from a file. The default encoding is UTF-8, but other supported
27     encoding can be specified instead.
28     """
29
30     def __init__(self, context, path, encoding='utf-8'):
31         self.context = context
32         self.path = path
33         self.encoding = encoding
34         self._file = None
35
36     def open(self):
37         try:
38             self._file = codecs.open(self.path, mode='r', encoding=self.encoding, buffering=1)
39         except IOError as e:
40             if e.errno == 2:
41                 raise DocumentNotFoundException('file not found: "%s"' % self.path, cause=e)
42             else:
43                 raise LoaderException('file I/O error: "%s"' % self.path, cause=e)
44         except Exception as e:
45             raise LoaderException('file error: "%s"' % self.path, cause=e)
46
47     def close(self):
48         if self._file is not None:
49             try:
50                 self._file.close()
51             except IOError as e:
52                 raise LoaderException('file I/O error: "%s"' % self.path, cause=e)
53             except Exception as e:
54                 raise LoaderException('file error: "%s"' % self.path, cause=e)
55
56     def load(self):
57         if self._file is not None:
58             try:
59                 return self._file.read()
60             except IOError as e:
61                 raise LoaderException('file I/O error: "%s"' % self.path, cause=e)
62             except Exception as e:
63                 raise LoaderException('file error %s' % self.path, cause=e)
64         return None