Esempio n. 1
0
def css_precompiled(data, offset_path='.', argv=None):
    """
    预编译css
    :param data: 规则字典
    :param argv: 输入变量
    :return:
    """
    now_path = data['source_path'] + '/' + offset_path
    if os.path.isdir(now_path):
        fps = os.listdir(now_path)
        for fp in fps:
            css_precompiled(data, '/'.join([offset_path, fp]), argv)
    else:

        # 判断是不是修改了这个文件
        if os.path.realpath(now_path) != os.path.realpath(
                argv['change_file_path']):
            return

        with open(now_path, 'r') as fp:
            file_type = tools.get_file_type(now_path)
            if file_type not in ['styl', 'less', 'scss', 'css', 'sass']:
                return
            if file_type == 'scss' or file_type == 'sass':
                fp_data = css_precompiled_lib.handle_scss(fp.read())
            elif file_type == 'less':
                fp_data = css_precompiled_lib.handle_less(fp.read())
            else:
                fp_data = fp.read()
            tools.output_file(data['target_path'] + '/' + offset_path + '.css',
                              fp_data)
Esempio n. 2
0
def css_precompiled_get_str(data, offset_path='.'):
    """
    预编译css
    :param data: 规则字典
    :param argv: 输入变量
    :return:
    """
    css_str = ''
    now_path = data['source_path'] + '/' + offset_path
    if os.path.isdir(now_path):
        fps = os.listdir(now_path)
        for fp in fps:
            css_str += css_precompiled_get_str(data,
                                               '/'.join([offset_path, fp]))
    else:
        with open(now_path, 'r') as fp:
            file_type = tools.get_file_type(now_path)
            if file_type not in ['styl', 'less', 'scss', 'css']:
                return
            if file_type == 'scss':
                fp_data = css_precompiled_lib.handle_scss(fp.read())
            elif file_type == 'less':
                fp_data = css_precompiled_lib.handle_less(fp.read())
            else:
                fp_data = fp.read()
            css_str += fp_data
    return css_str + '\n'
Esempio n. 3
0
def minify_file_in_script(data, offset_path='.', argv=None):
    """
    压缩文件
    :param data: 规则字典
    :param path: 当前路径
    :return:
    """
    now_path = data['source_path'] + '/' + offset_path
    if os.path.isdir(now_path):
        fps = os.listdir(now_path)
        for fp in fps:
            minify_file_in_script(data, offset_path + '/' + fp, argv=argv)
    else:

        # 判断是不是修改了这个文件
        if os.path.realpath(now_path) != os.path.realpath(
                argv['change_file_path']):
            return

        fp_type = tools.get_file_type(now_path)
        if fp_type and fp_type in ['html', 'css', 'js']:
            read_type = 'r'
        else:
            read_type = 'rb'

        with open(now_path, read_type) as fp:
            fp_data = fp.read()
            if fp_type and fp_type in ['html', 'css', 'js']:
                fp_data = minify_file(fp_data, fp_type, data['encryption'],
                                      data['encryption'])
                write_type = 'w'
            else:
                write_type = 'wb'
            tools.output_file(data['target_path'] + '/' + offset_path, fp_data,
                              write_type)
Esempio n. 4
0
def css_precompiled(data, offset_path='.', argv=None):
    """
    预编译css
    :param data: 规则字典
    :param argv: 输入变量
    :return:
    """
    now_path = data['source_path'] + '/' + offset_path
    if os.path.isdir(now_path):
        fps = os.listdir(now_path)
        for fp in fps:
            css_precompiled(data, '/'.join([offset_path, fp]), argv)
    else:

        # 判断是不是修改了这个文件
        if os.path.realpath(now_path) != os.path.realpath(argv['change_file_path']):
            return

        with open(now_path, 'r') as fp:
            file_type = tools.get_file_type(now_path)
            if file_type not in ['styl', 'less', 'scss', 'css', 'sass']:
                return
            if file_type == 'scss' or file_type == 'sass':
                fp_data = css_precompiled_lib.handle_scss(fp.read())
            elif file_type == 'less':
                fp_data = css_precompiled_lib.handle_less(fp.read())
            else:
                fp_data = fp.read()
            tools.output_file(data['target_path'] + '/' + offset_path + '.css', fp_data)
Esempio n. 5
0
def css_precompiled_get_str(data, offset_path='.'):
    """
    预编译css
    :param data: 规则字典
    :param argv: 输入变量
    :return:
    """
    css_str = ''
    now_path = data['source_path'] + '/' + offset_path
    if os.path.isdir(now_path):
        fps = os.listdir(now_path)
        for fp in fps:
            css_str += css_precompiled_get_str(data, '/'.join([offset_path, fp]))
    else:
        with open(now_path, 'r') as fp:
            file_type = tools.get_file_type(now_path)
            if file_type not in ['styl', 'less', 'scss', 'css']:
                return
            if file_type == 'scss':
                fp_data = css_precompiled_lib.handle_scss(fp.read())
            elif file_type == 'less':
                fp_data = css_precompiled_lib.handle_less(fp.read())
            else:
                fp_data = fp.read()
            css_str += fp_data
    return css_str + '\n'
Esempio n. 6
0
def minify_file_in_script(data, offset_path='.', argv=None):
    """
    压缩文件
    :param data: 规则字典
    :param path: 当前路径
    :return:
    """
    now_path = data['source_path'] + '/' + offset_path
    if os.path.isdir(now_path):
        fps = os.listdir(now_path)
        for fp in fps:
            minify_file_in_script(data, offset_path + '/' + fp, argv=argv)
    else:

        # 判断是不是修改了这个文件
        if os.path.realpath(now_path) != os.path.realpath(argv['change_file_path']):
            return

        fp_type = tools.get_file_type(now_path)
        if fp_type and fp_type in ['html', 'css', 'js']:
            read_type = 'r'
        else:
            read_type = 'rb'

        with open(now_path, read_type) as fp:
            fp_data = fp.read()
            if fp_type and fp_type in ['html', 'css', 'js']:
                fp_data = minify_file(fp_data, fp_type, data['encryption'], data['encryption'])
                write_type = 'w'
            else:
                write_type = 'wb'
            tools.output_file(data['target_path'] + '/' + offset_path, fp_data, write_type)
Esempio n. 7
0
    def handle_all(self, event):

        # 忽略 .开头的隐藏文件
        if os.path.basename(event.src_path)[0:1] == '.':
            return
        # 忽略文件夹
        if not event.is_directory:
            log.info('监控到文件变化:%s' % event.src_path)
            argv = copy.deepcopy(self.argv)
            argv['change_file_path'] = event.src_path

            # 只处理已知的几种文件
            know_files = [
                'html', 'vue', 'txt', 'js', 'css',
                'jpg', 'png', 'less', 'scss', 'sass',
                'htm'
            ]
            if tools.get_file_type(event.src_path) not in know_files:
                return

            try:
                init(argv)
            except:
                traceback.print_exc()