270 lines
7.3 KiB
Python
270 lines
7.3 KiB
Python
# -*- coding: UTF-8 -*-
|
||
#import wx
|
||
import color_info
|
||
import platform
|
||
import json
|
||
import os
|
||
import re
|
||
|
||
if platform.system() == "Windows":
|
||
LINE_END = "\r\n"
|
||
SPLIT_STR = "\\"
|
||
else:
|
||
SPLIT_STR = "/"
|
||
LINE_END = "\n"
|
||
|
||
|
||
def is_base(str):
|
||
if str == "int" or str == "string" or str == "float" or str == "atom" or str == "int32":
|
||
return True
|
||
return False
|
||
|
||
|
||
#判断颜色属于什么类型
|
||
# 必须 非必须 前后端都要 仅后端 仅前端 不导出
|
||
def get_color_type(color):
|
||
if color_info.required.count(color) == 1:
|
||
return True
|
||
elif color_info.optional.count(color) == 1:
|
||
return False
|
||
elif color_info.both.count(color) == 1:
|
||
return 1 #前后端都导出
|
||
elif color_info.server.count(color) == 1:
|
||
return 2 #仅后端导出
|
||
elif color_info.client.count(color) == 1:
|
||
return 3 #仅前端导出
|
||
elif color_info.none.count(color) == 1:
|
||
return False
|
||
else:
|
||
return False
|
||
#else:
|
||
# raise EOFError("无法识别的颜色类型")
|
||
|
||
#检测是否是excel文件
|
||
def is_ok(name):
|
||
if name[0] == "." or name[0] == "~":
|
||
return False
|
||
if name.endswith('xls') or name.endswith('xlsx'):
|
||
return True
|
||
return False
|
||
|
||
|
||
def forbid(name, config):
|
||
if config and config.get('forbid_xls', False) and name.endswith('xls'):
|
||
return "禁止XLS文件 %s" % name
|
||
return ""
|
||
|
||
|
||
#根据路径获取文件名
|
||
def get_file_name(path):
|
||
j = 0
|
||
k = -1
|
||
for i in range(len(path) - 1, 0, -1):
|
||
if path[i] == SPLIT_STR:
|
||
j = i
|
||
break
|
||
if path[i] == ".":
|
||
k = i
|
||
return path[j + 1:k]
|
||
|
||
|
||
#处理字符串,将breakGrow分割成break_grow形式
|
||
def to_lower(str):
|
||
list = []
|
||
flag = 0
|
||
num = 0
|
||
for i in range(len(str)):
|
||
if ord(str[i]) >= ord("A") and ord(str[i]) <= ord("Z"):
|
||
if i != 0 and flag == 0:
|
||
list.append("_")
|
||
flag = 0
|
||
num = 0
|
||
list.append(chr(ord(str[i]) + 32))
|
||
elif ord(str[i]) >= ord("0") and ord(str[i]) <= ord("9"):
|
||
if i != 0 and flag == 0 and num == 0:
|
||
list.append("_")
|
||
flag = 0
|
||
num = 1
|
||
list.append(str[i])
|
||
elif str[i] == "_":
|
||
flag = 1
|
||
num = 0
|
||
list.append(str[i])
|
||
else:
|
||
flag = 0
|
||
num = 0
|
||
list.append(str[i])
|
||
|
||
s = ''.join(list)
|
||
return s
|
||
|
||
#报错。直接终止程序
|
||
def err(file, x, y, info):
|
||
x = x + 1
|
||
y = y + 1
|
||
yy = ( 'A' if (y / 26 > 0) else '' ) + str(chr(ord('A') + (y - 1) % 26))
|
||
# wx.MessageBox(u"文件" + file + " " + str(x) + u"行: " + yy + u"列" + info, 'Info', wx.OK | wx.ICON_INFORMATION)
|
||
print(u"文件" + file + " " + str(x) + u"行: " + yy + u"列" + info + 'Info')
|
||
exit()
|
||
|
||
def deal_col(y):
|
||
return ( 'A' if (y / 26 > 0) else '' ) + str(chr(ord('A') + (y - 1) % 26))
|
||
|
||
def is_key(str):
|
||
return str[0] == "*"
|
||
|
||
def is_array(str):
|
||
return str[0] == "[" and str[-1] == "]"
|
||
|
||
def split_string(string):
|
||
stack_brackets = [] #括号匹配
|
||
stack_quota = [] #引号匹配
|
||
pointer_brackets = -1
|
||
pointer_quota = -1
|
||
tmp = []
|
||
res = []
|
||
for i in range(len(string)):
|
||
str = string[i]
|
||
if str == r'"' or str == r'“' or str == r'”': #检测引号,引号中间的括号逗号不处理
|
||
if pointer_quota < 0:
|
||
pointer_quota = pointer_quota + 1
|
||
stack_quota.append(str)
|
||
else:
|
||
pointer_quota -= 1
|
||
tmp.append(str)
|
||
elif (str == "{" or str == "[") and pointer_quota == -1: #检测括号,括号中间的逗号不处理
|
||
pointer_brackets += 1
|
||
stack_brackets.append(str)
|
||
tmp.append(str)
|
||
elif str == "}" and pointer_quota == -1: #检测右括号,栈里不是左括号抛异常
|
||
if pointer_brackets == -1:
|
||
raise EOFError("cannot parse string")
|
||
elif stack_brackets[pointer_brackets] != "{":
|
||
raise EOFError("cannot parse string")
|
||
else:
|
||
pointer_brackets -= 1
|
||
tmp.append(str)
|
||
elif str == "]" and pointer_quota == -1: #检测右中括号
|
||
if pointer_brackets == -1:
|
||
raise EOFError("cannot parse string")
|
||
elif stack_brackets[pointer_brackets] != "[":
|
||
raise EOFError("cannot parse string")
|
||
else:
|
||
pointer_brackets -= 1
|
||
tmp.append(str)
|
||
elif str == ' ' and pointer_quota == -1: #空格不处理
|
||
continue
|
||
elif str == "," and pointer_quota == -1 and pointer_brackets == -1:
|
||
res.append(tmp)
|
||
tmp = []
|
||
else:
|
||
tmp.append(str)
|
||
if len(tmp) > 0:
|
||
res.append(tmp)
|
||
strings = []
|
||
for i in range(len(res)):
|
||
strings.append(''.join(res[i]))
|
||
return strings
|
||
|
||
|
||
def deal_list(list): #数组多维降一维
|
||
while(True):
|
||
flag = 0
|
||
for j in range(len(list)):
|
||
if type(list[j]) == type([]):
|
||
list.extend(list[j])
|
||
list.pop(j)
|
||
flag = 1
|
||
if flag == 0:
|
||
break
|
||
return list
|
||
|
||
|
||
def trans_type(type):
|
||
if type == "int" or type == "int32":
|
||
return "integer()"
|
||
elif type == "float":
|
||
return "float()"
|
||
elif type == "string":
|
||
return "binary()"
|
||
elif type == "atom":
|
||
return "atom()"
|
||
else:
|
||
return "'" + type + "'()"
|
||
|
||
|
||
def read_json_file(path_name):
|
||
f = open(path_name, 'r')
|
||
data = f.read().replace('\xef\xbb\xbf', '')
|
||
f.close()
|
||
try:
|
||
json_data = json.loads(data)
|
||
except Exception as e:
|
||
print "config.json 格式错误"
|
||
raise e
|
||
return json_data
|
||
|
||
|
||
def read_default_dir(path_name):
|
||
f = open(path_name, 'r')
|
||
data = f.readlines()
|
||
f.close()
|
||
src = dst = ""
|
||
if len(data) >= 1:
|
||
src = data[0].replace("\xef\xbb\xbf", "").rstrip()
|
||
if len(data) >= 2:
|
||
dst = data[1].rstrip()
|
||
return src, dst
|
||
|
||
|
||
def store_default_dir(path_name, srcDir, dstDir):
|
||
f = open(path_name, "w")
|
||
f.writelines([srcDir, LINE_END, dstDir])
|
||
f.close()
|
||
|
||
|
||
def ignore(folder, root, name, config):
|
||
if not config or not config.get('ignore'):
|
||
return False
|
||
rules = config.get('ignore')
|
||
for i in range(len(rules)-1, -1, -1):
|
||
rule = rules[i]
|
||
ig_flag = True
|
||
if rule.startswith('!'):
|
||
ig_flag = False
|
||
rule = rule[1:]
|
||
if match(folder, root, name, rule):
|
||
return ig_flag
|
||
return False
|
||
|
||
|
||
def match(folder, root, name, rule):
|
||
path = os.path.relpath(root, folder)
|
||
file_path = os.path.join(path, name)
|
||
result = re.match(rule, file_path)
|
||
|
||
if result:
|
||
return True
|
||
else:
|
||
return False
|
||
|
||
# def full_match(p, s):
|
||
# m = re.match(^%s$'' % p, s)
|
||
# return m
|
||
|
||
def int_to_aa(x):
|
||
if x > 25:
|
||
return int_to_aa(x // 26 - 1) + chr(65 + x % 26)
|
||
return chr(65 + x)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
print match('xyz/abc', 'xyz/abc/qwe/pi', '1.txt', 'qwe/.i')
|
||
print match('test/dest', 'test/dest/loop', 't_arenaMarket.xls', "loop/.+xls")
|
||
print ignore('test/dest', 'test/dest/loop', 't_arenaMarket.xlsx', {
|
||
'ignore': [
|
||
"loop",
|
||
"!loop/.+xls"
|
||
]
|
||
})
|