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# Helper class for loading large fixture data
17from __future__ import with_statement
18
19import os
20import codecs
21
22from libcloud.utils.py3 import PY3
23from libcloud.utils.py3 import u
24
25FIXTURES_ROOT = {
26    'common': 'common/fixtures',
27    'compute': 'compute/fixtures',
28    'storage': 'storage/fixtures',
29    'loadbalancer': 'loadbalancer/fixtures',
30    'dns': 'dns/fixtures',
31    'drs': 'drs/fixtures',
32    'backup': 'backup/fixtures',
33    'openstack': 'compute/fixtures/openstack',
34    'container': 'container/fixtures'
35}
36
37
38class FileFixtures(object):
39    def __init__(self, fixtures_type, sub_dir=''):
40        script_dir = os.path.abspath(os.path.split(__file__)[0])
41        self.root = os.path.join(script_dir, FIXTURES_ROOT[fixtures_type],
42                                 sub_dir)
43
44    def load(self, file):
45        path = os.path.join(self.root, file)
46        if os.path.exists(path):
47            if PY3:
48                with open(path, 'r', encoding='utf-8') as fh:
49                    content = fh.read()
50                return u(content)
51            else:
52                with codecs.open(path, 'r', 'utf-8') as fh:
53                    content = fh.read()
54                return content
55        else:
56            raise IOError(path)
57
58
59class ComputeFileFixtures(FileFixtures):
60    def __init__(self, sub_dir=''):
61        super(ComputeFileFixtures, self).__init__(fixtures_type='compute',
62                                                  sub_dir=sub_dir)
63
64
65class StorageFileFixtures(FileFixtures):
66    def __init__(self, sub_dir=''):
67        super(StorageFileFixtures, self).__init__(fixtures_type='storage',
68                                                  sub_dir=sub_dir)
69
70
71class LoadBalancerFileFixtures(FileFixtures):
72    def __init__(self, sub_dir=''):
73        super(LoadBalancerFileFixtures, self).__init__(
74            fixtures_type='loadbalancer',
75            sub_dir=sub_dir)
76
77
78class DNSFileFixtures(FileFixtures):
79    def __init__(self, sub_dir=''):
80        super(DNSFileFixtures, self).__init__(fixtures_type='dns',
81                                              sub_dir=sub_dir)
82
83
84class DRSFileFixtures(FileFixtures):
85    def __init__(self, sub_dir=''):
86        super(DRSFileFixtures, self).__init__(fixtures_type='drs',
87                                              sub_dir=sub_dir)
88
89
90class OpenStackFixtures(FileFixtures):
91    def __init__(self, sub_dir=''):
92        super(OpenStackFixtures, self).__init__(fixtures_type='openstack',
93                                                sub_dir=sub_dir)
94
95
96class ContainerFileFixtures(FileFixtures):
97    def __init__(self, sub_dir=''):
98        super(ContainerFileFixtures, self).__init__(fixtures_type='container',
99                                                    sub_dir=sub_dir)
100
101
102class BackupFileFixtures(FileFixtures):
103    def __init__(self, sub_dir=''):
104        super(BackupFileFixtures, self).__init__(fixtures_type='backup',
105                                                 sub_dir=sub_dir)
106