Skip to content

Commit 9c317bd

Browse files
author
zhenwei-li
committed
添加 AbstrctDirectory 测试用例
1 parent 348d4b4 commit 9c317bd

3 files changed

Lines changed: 127 additions & 36 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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
Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# -*- coding:utf-8 -*-
22

3+
import datetime
34
import os
45
import time
5-
import datetime
6+
7+
from com.dvsnier.directory.abstract_directory import AbstractDirectory
68

79

8-
class CommonDirectory(object):
10+
class CommonDirectory(AbstractDirectory, object):
911
'''the Common Directory class'''
1012
def __init__(self):
1113
super(CommonDirectory, self).__init__()
@@ -47,37 +49,3 @@ def generate_complex_or_fmt_file_name(self, output_dir_name, file_name, fmt='%Y%
4749
else:
4850
file_name = str("%s_%s" % (file_name, datetime.datetime.now().strftime(fmt)))
4951
return os.path.join(output_dir, file_name)
50-
51-
def mk_dir(self, output_dir_name):
52-
'the initialize global output dir'
53-
project_dir = os.path.abspath('.')
54-
output_dir = os.path.join(project_dir, output_dir_name)
55-
if not os.path.exists(output_dir):
56-
os.makedirs(output_dir)
57-
return output_dir
58-
59-
def mk_output_dir(self, output_dir_name):
60-
'the initialize output dir'
61-
# root_dir = os.path.dirname(os.path.abspath('.'))
62-
# logging.debug('the current root_dir is %s' % root_dir)
63-
project_dir = os.path.abspath('.')
64-
# logging.debug('the current project_dir is %s' % project_dir)
65-
# src_dir = os.path.join(project_dir, 'src')
66-
# logging.debug('the current src_dir is %s' % src_dir)
67-
out_dir = os.path.join(project_dir, 'out')
68-
# logging.debug('the current out_dir is %s' % out_dir)
69-
output_dir = os.path.join(out_dir, output_dir_name)
70-
if not os.path.exists(output_dir):
71-
os.makedirs(output_dir)
72-
# logging.debug('the current output_dir is %s' % output_dir)
73-
return output_dir
74-
75-
def mk_children_dir(self, output_dir_name, sub_dir_name):
76-
'the initialize children dir'
77-
if not os.path.exists(output_dir_name):
78-
os.makedirs(output_dir_name)
79-
children_dir = os.path.join(output_dir_name, sub_dir_name)
80-
if not os.path.exists(children_dir):
81-
os.makedirs(children_dir)
82-
# logging.debug('the current children_dir is %s' % children_dir)
83-
return children_dir
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding:utf-8 -*-
2+
3+
import os
4+
import unittest
5+
6+
from com.dvsnier.directory.abstract_directory import AbstractDirectory
7+
8+
9+
class Test_AbstrctDirectory(unittest.TestCase):
10+
''' the test xxx '''
11+
@classmethod
12+
def setUpClass(cls):
13+
print("...the set up...")
14+
print('')
15+
cls.directory = AbstractDirectory(strategy_mode=True)
16+
# cls.directory = AbstractDirectory()
17+
18+
def setUp(self):
19+
super(Test_AbstrctDirectory, self).setUp()
20+
21+
def test_0_mk_dir(self):
22+
self.directory.set_work_region_space(os.path.join(os.getcwd(), 'out'))
23+
self.directory.mk_dir('output/asc_0')
24+
25+
def test_1_mk_children_dir(self):
26+
# the current executed environment directory
27+
self.directory.mk_children_dir(output_dir_name='out', sub_dir_name='sub_1/asc_1')
28+
# the settings work region space environment directory
29+
self.directory.mk_children_dir(output_dir_name=self.directory.get_work_region_space(),
30+
sub_dir_name='sub_2/asc_12')
31+
# the settings executed region space environment directory
32+
self.directory.mk_children_dir(output_dir_name=self.directory.get_executed_region_space(),
33+
sub_dir_name='sub_3/asc_123')
34+
# the settings current region space environment directory
35+
self.directory.mk_children_dir(output_dir_name=self.directory.get_current_region_space(),
36+
sub_dir_name='sub_4/asc_1234')
37+
38+
def test_2_mk_output_dir(self):
39+
self.directory.mk_output_dir(output_dir_name=self.directory.get_work_region_space())
40+
self.directory.mk_output_dir(output_dir_name=self.directory.get_work_region_space(),
41+
output_default_super_dir_name='dist')
42+
43+
def tearDown(self):
44+
super(Test_AbstrctDirectory, self).tearDown()
45+
46+
@classmethod
47+
def tearDownClass(cls):
48+
print('')
49+
print("...the tear down...")
50+
51+
52+
if __name__ == '__main__':
53+
''' the unittest suite '''
54+
suite = unittest.TestLoader().loadTestsFromTestCase(Test_AbstrctDirectory)
55+
unittest.TextTestRunner(verbosity=2).run(suite)
56+
# unittest.main()

0 commit comments

Comments
 (0)