Skip to content

Commit 2922f24

Browse files
author
zhenwei-li
committed
添加新版本 CommonDirectory
1 parent 657eee8 commit 2922f24

2 files changed

Lines changed: 186 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# -*- coding:utf-8 -*-
2+
3+
import os
4+
import time
5+
import datetime
6+
7+
8+
class CommonDirectory(object):
9+
'''the Common Directory class'''
10+
def __init__(self):
11+
super(CommonDirectory, self).__init__()
12+
13+
def generate_complex_file_name(self, output_dir_name, file_name):
14+
'the generate complex file name'
15+
output_dir = self.mk_output_dir(output_dir_name)
16+
name = str("%s_%s.log" % (file_name, int(time.time())))
17+
return os.path.join(output_dir, name)
18+
19+
def generate_fmt_file_name(self, output_dir_name, file_name, fmt='%Y%m%d_%H%M%S'):
20+
'the generate out with fmt file name'
21+
output_dir = self.mk_output_dir(output_dir_name)
22+
name = str("%s_%s.txt" % (file_name, datetime.datetime.now().strftime(fmt)))
23+
return os.path.join(output_dir, name)
24+
25+
def generate_file_name(self, output_dir_name, file_name):
26+
'the generate out file name'
27+
output_dir = self.mk_output_dir(output_dir_name)
28+
return os.path.join(output_dir, file_name)
29+
30+
def generate_file_name_only(self, output_dir_name, file_name):
31+
'the generate file name only'
32+
output_dir = self.mk_dir(output_dir_name)
33+
return os.path.join(output_dir, file_name)
34+
35+
def generate_complex_or_fmt_file_name(self, output_dir_name, file_name, fmt='%Y%m%d_%H%M%S'):
36+
'the generate out complex or fmt file name'
37+
output_dir = self.mk_output_dir(output_dir_name)
38+
if file_name is None or len(file_name.strip()) == 0:
39+
raise ValueError('the file name is invaild.')
40+
file_name = file_name.strip()
41+
if '.' in file_name:
42+
rdot_index = file_name.rfind('.')
43+
if rdot_index > 0:
44+
file_name = str(
45+
"%s_%s%s" %
46+
(file_name[0:rdot_index], datetime.datetime.now().strftime(fmt), file_name[rdot_index:]))
47+
else:
48+
file_name = str("%s_%s" % (file_name, datetime.datetime.now().strftime(fmt)))
49+
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: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# -*- coding:utf-8 -*-
2+
3+
import unittest
4+
5+
from com.dvsnier.directory.common_directory import CommonDirectory
6+
7+
8+
class Test_Common_Directory(unittest.TestCase):
9+
''' the test common dir '''
10+
@classmethod
11+
def setUpClass(cls):
12+
print("...the set up...")
13+
print('')
14+
cls.directory = CommonDirectory()
15+
16+
def setUp(self):
17+
super(Test_Common_Directory, self).setUp()
18+
19+
def test_generate_complex_file_name(self):
20+
'the test generate complex file name'
21+
dir_name = 'https'
22+
file_name = 'test_name'
23+
output = self.directory.generate_complex_file_name(dir_name, file_name)
24+
print("\nthe test generate_complex_file_name(%s) is succeed." % output)
25+
self.assertIsNotNone(output, 'test_generate_complex_file_name is error.')
26+
27+
def test_generate_fmt_file_name(self):
28+
'the test generate fmt file name'
29+
dir_name = 'https'
30+
file_name = 'test_name'
31+
# output = generate_fmt_file_name(dir_name, file_name)
32+
output = self.directory.generate_fmt_file_name(dir_name, file_name, fmt='%Y%m%d_%H%M%S.%f')
33+
print("\nthe test generate_fmt_file_name(%s) is succeed." % output)
34+
self.assertIsNotNone(output, 'generate_fmt_file_name is error.')
35+
36+
def test_generate_file_name(self):
37+
'the test generate file name'
38+
dir_name = 'https'
39+
file_name = 'test_name'
40+
output = self.directory.generate_file_name(dir_name, file_name)
41+
print("\nthe test test_generate_file_name(%s) is succeed." % output)
42+
self.assertIsNotNone(output, 'test_generate_file_name is error.')
43+
44+
def test_generate_file_name_only(self):
45+
'the test generate file name only'
46+
dir_name = 'out/https_2'
47+
file_name = 'test_name'
48+
output = self.directory.generate_file_name_only(dir_name, file_name)
49+
print("\nthe test test_generate_file_name_only(%s) is succeed." % output)
50+
self.assertIsNotNone(output, 'test_generate_file_name_only is error.')
51+
52+
def test_generate_complex_or_fmt_file_name(self):
53+
'the test generate complex or fmt file name'
54+
dir_name = 'https'
55+
# file_name = ''
56+
# file_name = ' '
57+
# file_name = 'test_name'
58+
# file_name = ' test_name'
59+
# file_name = 'test_name '
60+
# file_name = ' test_name '
61+
file_name = 'test_name.txt'
62+
# file_name = 'test_name.txt.log'
63+
# file_name = '.test_name.txt.log'
64+
# file_name = '.test_name.txt.log.'
65+
# file_name = 'test.name.txt.log'
66+
output = self.directory.generate_complex_or_fmt_file_name(dir_name, file_name)
67+
print("\nthe test test_generate_complex_or_fmt_file_name(%s) is succeed." % output)
68+
self.assertIsNotNone(output, 'test_generate_complex_or_fmt_file_name is error.')
69+
70+
def test_mk_dir(self):
71+
'the test mk dir'
72+
dir_name = 'config'
73+
output = self.directory.mk_dir(dir_name)
74+
print("\nthe test test_mk_dir(%s) is succeed." % output)
75+
self.assertIsNotNone(output, 'test_mk_dir is error.')
76+
77+
def test_mk_output_dir(self):
78+
'the test mk output dir'
79+
dir_name = 'https'
80+
output = self.directory.mk_output_dir(dir_name)
81+
print("\nthe test mk_output_dir(%s) is succeed." % output)
82+
self.assertIsNotNone(output, 'test_mk_output_dir is error.')
83+
84+
def test_mk_children_dir(self):
85+
'the test mk childer dir'
86+
sub_name = 'children'
87+
output = self.directory.mk_children_dir('out/http', sub_dir_name=sub_name)
88+
print("\nthe test mk_children_dir(%s) is succeed." % output)
89+
self.assertIsNotNone(output, 'test_mk_children_dir is error.')
90+
91+
def tearDown(self):
92+
super(Test_Common_Directory, self).tearDown()
93+
94+
@classmethod
95+
def tearDownClass(cls):
96+
print('')
97+
print("...the tear down...")
98+
99+
100+
if __name__ == '__main__':
101+
suite = unittest.TestLoader().loadTestsFromTestCase(Test_Common_Directory)
102+
unittest.TextTestRunner(verbosity=2).run(suite)
103+
# unittest.main()

0 commit comments

Comments
 (0)