示例#1
0
def newdir(path, opts=[]):
    '''新建文件夹'''
    def rec_newdir(path):
        if os.path.exists(path) or os.path.dirname(path) == path:
            return
        else:
            rec_newdir(os.path.dirname(path))
            os.mkdir(path)

    abs_path = parse_path(path)
    code, msg = 0, ''
    try:
        if 'p' in opts or 'parent' in opts:
            # rec_newdir(abs_path)
            os.makedirs(abs_path)
        else:
            os.mkdir(abs_path)
    except FileNotFoundError:
        code, msg = cfg.pack_error('ERROR_PATH_NOTEXIST',
                                   os.path.dirname(path))
    except PermissionError:
        code, msg = cfg.pack_error('Error_Permission', os.path.dirname(path))
    except FileExistsError:
        code, msg = cfg.pack_error('ERROR_ALREADY_EXIST', path)
    return {'code': code, 'msg': msg}
示例#2
0
def copy(srclist, dest, opts=[]):
    code, msg, ext = 0, '', {}
    abs_dest = parse_path(dest)
    if os.path.isfile(abs_dest):
        if len(srclist) > 1:
            code, msg = cfg.pack_error('Error_Not_Folder', abs_dest)
        elif os.path.isdir(parse_path(srclist[0])):
            code, msg = cfg.pack_error('CANNOT_OVERWRITE', srclist[0], dest)
    if code == 0:
        try:
            for src in srclist:
                abs_src = parse_path(src)
                if not os.path.exists(abs_src):
                    code, ext[src] = cfg.pack_warning('ERROR_PATH_NOTEXIST',
                                                      src)
                elif os.path.isdir(abs_src):
                    if 'r' not in opts:
                        code, ext[src] = cfg.pack_warning(
                            'Error_Lack_Option', '-r', f'复制文件夹{src}')
                    else:
                        shutil.copytree(abs_src, abs_dest)
                else:
                    shutil.copy(abs_src, abs_dest)
        except FileNotFoundError:
            code, msg = cfg.pack_error('ERROR_PATH_NOTEXIST', dest)
    result = {'code': code, 'msg': msg}
    if 'ext' in dir():
        result['ext'] = ext
    return result
示例#3
0
def change_tag(name):
    '''以文件夹方式打开标签'''
    code, msg, resource = 0, '', []
    if name == '#':
        resource = list_tagroots()
    elif not tags.get(name):
        code, msg = cfg.pack_error('Error_Tag_Not_Exist', name)
    else:
        # 子标签
        for sub in tags[name]['$subtags']:
            resource.append(get_tagmsg(sub))
        # 子路径
        for sub in tags[name]['$paths']:
            if check_range(sub) is False:
                continue
            if os.path.isdir(sub):
                resource.append(get_dirmsg(sub))
            elif os.path.isfile(sub):
                resource.append(list_file(sub))
    return {
        'code': code,
        'msg': msg,
        'resource': sort_resource(resource),
        'chain': get_chain(name)
    }
示例#4
0
def move(srclist, dest, opts=[]):
    code, msg, ext = 0, '', {}
    abs_dest = parse_path(dest)
    rename = not os.path.exists(abs_dest)
    if os.path.isfile(abs_dest):
        if len(srclist) > 1:
            code, msg = cfg.pack_error('Error_Not_Folder', dest)
        elif os.path.isdir(parse_path(srclist[0])):
            code, msg = cfg.pack_error('CANNOT_OVERWRITE', srclist[0], dest)
    if code == 0:
        try:
            for src in srclist:
                abs_src = parse_path(src)
                if not os.path.exists(abs_src):
                    code, ext[src] = cfg.pack_warning('ERROR_PATH_NOTEXIST',
                                                      src)
                elif os.path.isdir(abs_src) and '-r' not in opts:
                    code, ext[src] = cfg.pack_warning('Error_Lack_Option', src,
                                                      f'移动文件夹{src}')
                else:
                    shutil.move(abs_src, abs_dest)
                    # 标签信息也要相应修改
                    # 获得反斜杠的完全小写的路径(仅限Windows)
                    # norm_src = os.path.normpath(os.path.normcase(abs_src))
                    # norm_dest = os.path.normpath(os.path.normcase(abs_dest))
                    # 处理标签与norm_src的相互对应
                    # after_path应当是移动后的路径,若为文件夹,需进行转换
                    after_path = abs_dest if rename else os.path.join(
                        abs_dest, os.path.basename(abs_src)).replace(
                            '\\', '/')
                    twist(abs_src, after_path, delete=False)
                    # 处理标签与norm_src子路径的相互对应
                    for one_path in path_tags:
                        print(common_path(one_path, abs_src), abs_src)
                        if common_path(one_path, abs_src) == abs_src:
                            # one_path是abs_src的子路径
                            twist(one_path,
                                  re.sub(f'^{abs_src}', after_path, one_path),
                                  delete=False)
        except FileNotFoundError:
            code, msg = cfg.pack_error('ERROR_PATH_NOTEXIST', dest)
    result = {'code': code, 'msg': msg}
    if 'ext' in dir():
        result['ext'] = ext
    return result
示例#5
0
def new_tag(name, parent, color=None):
    parent = '#' if not parent else parent
    check = check_tagname(name)
    if check is not True:
        code, msg = cfg.pack_error('Error_Tag_Invaild_Name', check)
    elif parent != '#' and not tags.get(parent):
        code, msg = cfg.pack_error('Error_Tag_Not_Exist', parent)
    elif tags.get(name):
        code, msg = cfg.pack_error('ERROR_ALREADY_EXIST', name)
    else:
        tags[name] = {
            '$name': name,
            '$parent': parent,
            '$color': color,
            '$paths': [],
            '$subtags': [],
        }
        if parent != '#':
            tags[parent]['$subtags'].append(name)
        code, msg = 0, ''
        common_kit.tags_has_changed = True
    return {'code': code, 'msg': msg}
示例#6
0
def list_path(path, opts=[], cd=False):
    code, msg, resources = 0, '', []
    abspath = parse_path(path)
    if abspath == '':  # 意味着需要获取所有磁盘信息
        resources = list_disks()
    elif os.path.exists(abspath) is False:
        code, msg = cfg.pack_warning('ERROR_PATH_NOTEXIST', path)
    elif os.path.isdir(abspath):
        resources = list_folder(abspath)
        if '-r' in opts:
            pass
    elif cd:
        code, msg = cfg.pack_error('Error_Not_Folder', path)
    else:
        resources.append(list_file(abspath))
    return {'code': code, 'msg': msg, 'resource': sort_resource(resources)}
示例#7
0
def not_exist(path):
    code, msg = cfg.pack_error('ERROR_PATH_NOTEXIST', path)
    return {'code': code, 'msg': msg}