|
| 1 | +# -*- coding:utf-8 -*- |
| 2 | + |
| 3 | +import logging |
| 4 | +import os |
| 5 | + |
| 6 | +from com.dvsnier.directory.idirectory import IDirectory |
| 7 | + |
| 8 | + |
| 9 | +class AbstractDirectory(IDirectory, object): |
| 10 | + '''the Abstract Directory class''' |
| 11 | + # True: the strict mode, otherwise the base mode |
| 12 | + strategy_mode = False |
| 13 | + |
| 14 | + def __init__(self, strategy_mode=False): |
| 15 | + super(AbstractDirectory, self).__init__() |
| 16 | + self.strategy_mode = strategy_mode |
| 17 | + if self.strategy_mode: |
| 18 | + logging.warning( |
| 19 | + 'At present, the strict review mode is adopted, and the illegal path is thrown directly. If the current directory is used by default to meet the current situation, please use the general review mode' |
| 20 | + ) |
| 21 | + else: |
| 22 | + logging.debug( |
| 23 | + 'Currently, the general review mode, workspace region space, current region space, executed region space and the same naming mode are used for the implementation of regional space' |
| 24 | + ) |
| 25 | + |
| 26 | + def mk_dir(self, output_dir_name): |
| 27 | + 'the initialize global output dir that two modes are provided, one is `strict mode`, the other is `general mode`, and the default is `general mode` (i.e. current work execution environment)' |
| 28 | + project_dir = None |
| 29 | + if self.strategy_mode: |
| 30 | + if self.get_work_region_space(): |
| 31 | + project_dir = self.get_work_region_space() |
| 32 | + else: |
| 33 | + raise ValueError( |
| 34 | + 'In strict mode, illegal WORK_REGIOIN_SPACE value, please check WORK_REGIOIN_SPACE parameter.') |
| 35 | + else: |
| 36 | + project_dir = os.path.abspath('.') |
| 37 | + output_dir = os.path.join(project_dir, output_dir_name) |
| 38 | + if not os.path.exists(output_dir): |
| 39 | + os.makedirs(output_dir) |
| 40 | + return output_dir |
| 41 | + |
| 42 | + def mk_children_dir(self, output_dir_name, sub_dir_name): |
| 43 | + 'the initialize children dir that default, the specified directory is created from the current execution environment' |
| 44 | + if not os.path.exists(output_dir_name): |
| 45 | + os.makedirs(output_dir_name) |
| 46 | + children_dir = os.path.join(output_dir_name, sub_dir_name) |
| 47 | + if not os.path.exists(children_dir): |
| 48 | + os.makedirs(children_dir) |
| 49 | + # logging.debug('the current children_dir is %s' % children_dir) |
| 50 | + return children_dir |
| 51 | + |
| 52 | + def mk_output_dir(self, output_dir_name, output_default_super_dir_name='out'): |
| 53 | + 'the initialize output dir that two modes are provided, one is `strict mode`, the other is `general mode`, and the default is `general mode` (i.e. current work execution environment)' |
| 54 | + project_dir = None |
| 55 | + if self.strategy_mode: |
| 56 | + if self.get_work_region_space(): |
| 57 | + project_dir = self.get_work_region_space() |
| 58 | + else: |
| 59 | + raise ValueError( |
| 60 | + 'In strict mode, illegal WORK_REGIOIN_SPACE value, please check WORK_REGIOIN_SPACE parameter.') |
| 61 | + else: |
| 62 | + project_dir = os.path.abspath('.') |
| 63 | + out_dir = os.path.join(project_dir, output_default_super_dir_name) |
| 64 | + output_dir = os.path.join(out_dir, output_dir_name) |
| 65 | + if not os.path.exists(output_dir): |
| 66 | + os.makedirs(output_dir) |
| 67 | + return output_dir |
0 commit comments