Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / utils / path.py
1 """
2 Copyright (C) 2018-2019 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 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
17 import os
18 import ntpath
19
20
21 class Path:
22     @staticmethod
23     def get_model(model_file_path:str, addition:str = None, directory:str = None) -> str:
24         if model_file_path is None:
25             raise ValueError("model_file_path is None")
26
27         file_name = ntpath.basename(model_file_path)
28         model = os.path.splitext(file_name)
29         if len(model) < 2:
30             raise ValueError("model file name '{}' is not correct".format(file_name))
31         if directory:
32             return os.path.join(
33                 directory, 
34                 model[len(model) - 2] + (addition if addition else "") + ".xml")
35         else:
36             return os.path.join(
37                 os.path.dirname(model_file_path), 
38                 model[len(model) - 2] + (addition if addition else "") + ".xml")
39
40     @staticmethod
41     def get_weights(model_file_path:str, addition:str = None, directory:str = None) -> str:
42         if model_file_path is None:
43             raise ValueError("model_file_path is None")
44
45         file_name = ntpath.basename(model_file_path)
46         model = os.path.splitext(file_name)
47         if len(model) < 2:
48             raise ValueError("model file name '{}' is not correct".format(file_name))
49         if directory:
50             return os.path.join(
51                 directory, 
52                 model[len(model) - 2] + (addition if addition else "") + ".bin")
53         else:
54             return os.path.join(
55                 os.path.dirname(model_file_path), 
56                 model[len(model) - 2] + (addition if addition else "") + ".bin")
57
58     @staticmethod
59     def update_name(file_path: str, addition: str) -> str:
60         file_name = ntpath.basename(file_path)
61         parts = os.path.splitext(file_name)
62
63         name = parts[0]
64         extension = parts[-1] if len(parts) >= 2 else ""
65
66         dir = os.path.dirname(file_path)
67         return os.path.join(dir, name + addition + extension)