-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
75 lines (63 loc) · 3.36 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import argparse
from time import time
from msvcrt import getch
from sys import exit
import warnings
import traceback
from imageshow import show_image
from configtoargs import configtoargs
warnings.filterwarnings("ignore", ".*GUI is implemented.*") # matplotlib2.0.2会跳出警告
def get_args_parser():
parser = argparse.ArgumentParser(prog='Random Image Viewer ')
parser.add_argument('-m', '--mode', choices=['ontime', 'mongodb'], default='ontime', help='选择路径生成模式 默认文件树缓存在内存中')
parser.add_argument('-t', '--time', default=600, help='切换图片的时间间隔 单位是秒 默认600秒(10分钟)')
parser.add_argument('--seed', default=time(), help='随机数生成器的种子')
parser.add_argument('-b', '--background', default='k', help='设置背景颜色 默认为黑色')
parser.add_argument("-v", "--version", action='version', version='%(prog)sv0.1', help='输出版本号')
parser.add_argument('--screensize', default='fullscreen', help='指定窗口大小 默认为全屏 修改请用英文逗号分割宽高 宽在前 高在后')
parser.add_argument('--client', default='mongodb://localhost:27017/', help='MongoDB地址 默认为本地')
parser.add_argument('--database', default='RandomImage', help='MongoDB数据库名称 默认为RandomImage')
parser.add_argument('--collection', default='ImagePath', help='MongoDB数据库表名称 默认为ImagePath')
parser.add_argument('--targetpath', default='目录.txt', help='用于从指定目标文件中读取目录 默认为根目录下的 目录.txt')
parser.add_argument('--initialize', action='store_true', help='初始化数据库')
parser.add_argument('--update', action='store_true', help='更新数据库')
parser.add_argument('--clear', action='store_true', help='删除数据库表 \033[31m谨慎操作\033[0m')
parser.add_argument('--showimage', action='store_false', help='是否显示图片 默认显示')
parser.add_argument('-d', '--debug', action='store_true')
parser.add_argument('--configfile', action='store_false', help='是否使用配置文件 默认使用')
return parser
if __name__ == '__main__':
print('程序初始化中...')
parser = get_args_parser()
args = parser.parse_args()
if args.debug:
args = parser.parse_args(['-d', '--mode=mongodb', '--collection=人体h', '--time=600', '--configfile'])
print('本次运行随机种子为:{}'.format(args.seed))
if args.configfile:
args = configtoargs()
# print('使用配置文件')
if args.debug:
print('-'*10+'Debug模式已启用'+'-'*10)
try:
status = show_image(args) # 主程序
except Exception as e:
print('\n', traceback.format_exc())
print('\n错误!', e.args[0])
try:
if status == 0:
if args.debug:
print('正常退出')
else: # 正常模式正常退出
print('窗口关闭\n'+'-'*10+'按任意键退出程序'+'-'*10)
getch()
exit()
elif status == -1: # 不显示图片的模式
print('不显示照片 程序结束')
exit()
except NameError:
print('\n' + '-' * 10 + '按任意键退出程序' + '-' * 10)
if args.debug:
print('正常退出')
else:
getch()
exit()