遍历项目中用到的所有字符,过滤掉重复字符后存入指定文件

关键是中文字符,碰到gbk或gb2312格式字符,尽量用字库更大的gb18030格式来解码。

获得的文字先用 chardet.detect("my_str")["encoding"] 检测文字格式。然后用unicode(text, encoding_type)转换成unicode格式。最后写入文件时用 text.encode("gb18030")转为windows常用的格式写入文本。

define.py

#!/usr/bin/python
# encoding:utf-8
import os

cur=os.getcwd()
g_xcf_root=os.sep.join((cur,"..","dy_cike_xcf"))
g_xcf_assets=os.sep.join((g_xcf_root,"assets"))
g_xcf_config=os.sep.join((g_xcf_assets,"config"))
g_xcf_skeleton=os.sep.join((g_xcf_assets,"skeleton"))
g_xcf_font=os.sep.join((g_xcf_assets,"font"))
g_working_root=os.sep.join((cur,"android","assets"))
g_working_config=os.sep.join((g_working_root,"config"))
g_working_skeleton=os.sep.join((g_working_root,"skeleton"))
g_working_font=os.sep.join((g_working_root,"font"))

images_functions.py
#!/usr/bin/python
# encoding:utf-8
import os
import shutil

import sys
import define


class CFileInform(object):
    name = ""
    path = ""
    size = 0

    def __init__(self, name, path, size):
        self.name = name
        self.path = path
        self.size = size

    def __str__(self):
        return ("name=%s, path=%s, size=%d" % (self.name, self.path, self.size))


def print_succes(inform):
    print("%s %s  %s" % ('\033[1;32;40m :', inform, "\033[0m"))


def print_verb(inform):
    print("%s %s  %s" % ("\033[0m", inform, "\033[0m"))


def print_error(error_inform):
    print("%s %s  %s" % ('\033[1;31;40m :', error_inform, "\033[0m"))
    sys.exit(1)


def get_root():
    cur = os.getcwd()
    # root = os.sep.join((cur, "..", "dy_cike_xcf", "assets"))
    root = define.g_xcf_assets
    return root


def get_atlas_path():
    root = get_root()
    atlas_path = os.sep.join((root, "atlas"))
    return atlas_path


def get_image_names_by_csvfile(csv_path):
    images = []

    f = open(csv_path)
    try:
        for line in f.readlines():
            line = line.replace("\r\n", ",")
            line = line.replace("\n", ",")
            line = line.replace("\"", ",")
            sp = line.split(",")
            for sub in sp:
                if sub.endswith(".png") or sub.endswith(".jpg"):
                    images.append(sub)
    finally:
        f.close()
    return images


def get_xcf_images_by_dir(path_dir):
    root = get_root()
    parent_path = os.sep.join((root, path_dir))
    files = os.listdir(parent_path)
    image_names = []
    for f in files:
        if f.endswith(".csv"):
            if not f.endswith("_r2.csv"):
                filename = os.sep.join((parent_path, f))
                names = get_image_names_by_csvfile(filename)
                for n in names:
                    image_names.append(n)
    return image_names


def list_all_csvs(parent):
    files = []
    paths = os.listdir(parent)
    for p in paths:
        abs_path = os.sep.join((parent, p))
        if os.path.isdir(abs_path):
            for s in list_all_csvs(abs_path):
                files.append(s)
        elif os.path.isfile(abs_path):
            if abs_path.endswith(".csv"):
                files.append(abs_path)
    return files


def get_children_informs_by_suffix(parent, suffixs=()):
    """
    获得parent目录下指定类型的文件
    :param parent:
    :param suffixs:
    :return:
    """
    fileinforms = []
    for name in os.listdir(parent):
        path = os.sep.join((parent, name))
        if os.path.isdir(path):
            for element in get_children_informs_by_suffix(path, suffixs):
                fileinforms.append(element)
        elif os.path.isfile(path):
            for suffix in suffixs:
                if name.endswith(suffix):
                    size = os.path.getsize(path)
                    element = CFileInform(name, path, size)
                    fileinforms.append(element)
                    break
    return fileinforms


def is_valid_path(path):
    isvalid = True
    contain_filters = ("/test/", "/object/", "/temp/", ".del/", "2013", "2014", "2015", "2016", "2017")
    for symbol in contain_filters:
        os_symbol = symbol.replace("/", os.sep)
        if path.find(os_symbol) != -1:
            isvalid = False
            break

    if not isvalid:
        ends_filters = (".del", ".sel")
        for symbol in ends_filters:
            if path.endswith(symbol):
                isvalid = False
                break
    return isvalid


def get_image_folder_children_informs():
    root = get_root()
    image_folder = os.sep.join((root, "image"))
    valid_elements = []
    for element in get_children_informs_by_suffix(image_folder, (".jpg", ".png")):
        path = element.path
        isvalid = is_valid_path(path)
        if isvalid:
            valid_elements.append(element)
    return valid_elements


def get_atlas_children_informs():
    atlas_path = get_atlas_path()
    children = []
    for name in os.listdir(atlas_path):
        path = os.sep.join((atlas_path, name))
        if (os.path.isdir(path)):
            for element in get_children_informs_by_suffix(path, (".jpg", ".png")):
                children.append(element)
    return children


def get_used_image_names():
    used_names = []
    csv_names = []
    root = get_root()
    parents = ("cocostudio", "layout", "skeleton")

    for parent_dir in parents:
        path = os.sep.join((root, parent_dir))
        allcsvs = list_all_csvs(path)
        for csv in allcsvs:
            aimages = get_image_names_by_csvfile(csv)
            for image in aimages:
                if used_names.count(image) == 0:
                    used_names.append(image)
                    csv_names.append(csv)
    return used_names, csv_names


def get_notused_images():
    """
    Function:
        返回atlas中未使用的图片名称
    Return:
    """
    used_images = get_used_image_names()[0]
    atlas_map = {}
    for element in get_atlas_children_informs():
        atlas_map[element.name] = element.path

    for used in used_images:
        if atlas_map.has_key(used):
            del atlas_map[used]
    return atlas_map.values()


def trim_file_suffix(filename):
    sp = filename.split(".")
    return ".".join(sp[0:(len(sp) - 1)])


def get_extra_image_names():
    """
    Function:
        遍历无法从tmx、cocos.json中分析出来的图片名称,
        放到extra_resources_fight_config_r2.csv里面
    """
    images = get_xcf_images_by_dir("layout")
    return images


def check_invalid_image_names():
    """
    Function:
        list not used images.
    Returns:
        errorinform,  valide_images
    """
    error_inform = []
    invalide_names = []
    atlas_images = {}
    for element in get_atlas_children_informs():
        atlas_images[element.path] = element.name

    used_back = get_used_image_names()
    used_images = used_back[0]
    used_csvs = used_back[1]
    used_count = len(used_images)
    i = 0
    while i < used_count:
        used = used_images[i]
        used_preffix = trim_file_suffix(used)
        csv = used_csvs[i]
        used_png = used_preffix + ".png"
        used_jpg = used_preffix + ".jpg"

        if atlas_images.values().count(used_png) == 0 and atlas_images.values().count(used_jpg) == 0:
            if invalide_names.count(used) == 0:
                invalide_names.append(used)
                error_inform.append(used + ", " + csv)
        i = i + 1
    return error_inform


def get_a_path():
    atlas_path = get_atlas_path()
    apath = os.sep.join((atlas_path, "a"))
    if not (os.path.exists(apath) and os.path.isdir(apath)):
        os.makedirs(apath)
    return apath


def check_image_and_atlas():
    hint = "check_image_and_atlas"
    error_inform = []
    atlas_informs = get_atlas_children_informs()
    image_informs = get_image_folder_children_informs()
    atlas_names = []
    image_names = []

    for element in atlas_informs:
        atlas_names.append(element.name)

    for element in image_informs:
        image_names.append(element.name)

    # 判断atlas与image下每个文件名称是否唯一,并且两个文件夹下文件名称是否完全相同
    for name in atlas_names:
        if (atlas_names.count(name) > 1):
            for elem in atlas_informs:
                if 0 == cmp(elem.name, name):
                    error_inform.append("duplicate in atlas folder: %s" % (elem.path))
            return error_inform

    for name in image_names:
        if (image_names.count(name) > 1):
            for elem in image_informs:
                if 0 == cmp(elem.name, name):
                    error_inform.append("duplicate in image folder: %s" % (elem.path))
            return error_inform

    atlas_map = {}
    image_map = {}
    for elem in atlas_informs:
        atlas_map[elem.name] = elem

    for elem in image_informs:
        image_map[elem.name] = elem

    for key in image_map.keys():
        elem = image_map[key]
        if not atlas_map.has_key(elem.name):
            apath = get_a_path()
            dst = os.sep.join((apath, elem.name))
            shutil.copy(elem.path, dst)
            imagev = elem
            atlasv = CFileInform(imagev.name, apath, imagev.size)
            atlas_map[elem.name] = atlasv
            print_verb("not found file in atlas, copy image to atlas/a: %s" % (elem.path))

    for key in atlas_map:
        elem = atlas_map[key]
        if not image_map.has_key(elem.name):
            os.remove(elem.path)
            del atlas_map[elem.name]
            print_verb("not found file in image, delet atlas: %s" % (elem.path))

    if len(image_map) == len(atlas_map):
        for key in image_map.keys():
            image_elem = image_map[key]
            atlas_elem = atlas_map[key]
            if not atlas_elem.size == image_elem.size:
                shutil.copy(image_elem.path, atlas_elem.path)
                print_verb("size not equal . Cover image to atlas: %s  %s" % (image_elem.path, atlas_elem.path))
    else:
        error_inform.append("len is not equal :image_number=%d  atlas_number=%d" % (len(image_map), len(atlas_map)))
    return error_inform


if __name__ == "__main__":
    print("path = %s" % (get_root()))
    # images = get_notused_images()
    # for img in images:
    # print(img)
    # print(len(images))


font_filter.py
#!/usr/bin/python
# encoding:utf-8
import os
import site
import sys
import shutil
from pip._vendor.requests.packages import chardet
import define
import images_functions

__author__ = 'andrew'


def list_all_csv():
    conf_root = define.g_xcf_config
    print("list config path = %s" % (conf_root))
    all_files = os.listdir(conf_root)
    csv_files = []
    for f in all_files:
        path = os.sep.join((conf_root, f))
        if os.path.isfile(path) and path.endswith(".csv"):
            csv_files.append(path)
    return csv_files


def extract_characters(file_paths):
    hint = "extract_characters"
    default_encode = "gb18030"
    save_path = os.sep.join((define.g_xcf_font, "all_characters.config"))
    unicode_str = unicode(
        ''' !"#$%&'()*+,-/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^—、abcdefghijklmnopqrstuvwxyz{|}~''', "utf-8")

    for fpath in file_paths:
        csv = open(fpath, "r")
        encoding_type = ""
        try:
            text = csv.read()
            encoding_type = chardet.detect(text)["encoding"]
            if (encoding_type.lower() == "gbk") or (encoding_type.lower() == "gb2312"):
                substr=unicode(text,default_encode)
            else:
                substr=unicode(text,encoding_type)

            unicode_str = unicode_str + substr
        except UnicodeEncodeError, e:
            print("error UnicodeEncodeError %s   %s" % (hint, e))
            images_functions.print_error("fpath = %s,  encoding_type = %s" % ( fpath, encoding_type))
        except UnicodeDecodeError, e:
            print("error UnicodeDecodeError %s   %s" % (hint, e))
            images_functions.print_error("fpath = %s,  encoding_type = %s" % ( fpath, encoding_type))
        except Exception, e:
            print("error Exception %s   %s" % (hint, e))
            images_functions.print_error("fpath = %s,  encoding_type = %s" % ( fpath, encoding_type))
        finally:
            csv.close()

    uniq_buffer = unicode("")
    already = []

    for ch in unicode_str:  # .decode(default_encode).encode("utf-16"):
        if already.count(ch) == 0:
            uniq_buffer += ch
            already.append(ch)

    save_file = open(save_path, "w")
    try:
        result = uniq_buffer.encode(default_encode)
        save_file.write(result)
    except Exception, e:
        print("error %s   %s" % (hint, e))
        images_functions.print_error("save_path = " + save_path);
    finally:
        save_file.close()
    shutil.copy(save_path, define.g_working_font)
    images_functions.print_succes("hint = %s write file %s success--------------" % (hint, save_path))


if __name__ == "__main__":

    names = sys.argv
    csvs = []
    if len(names) > 1:
        print("len argv =%d" % (len(names)))
        config_path = define.g_xcf_config
        for name in names[1:]:
            path = os.sep.join((config_path, name))
            csvs.append(path)
    else:
        csvs = list_all_csv()
    extract_characters(csvs)






你可能感兴趣的:(遍历项目中用到的所有字符,过滤掉重复字符后存入指定文件)