Skip to content

Commit

Permalink
feat: support glob patterns (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Snoopy1866 authored Jan 2, 2025
1 parent 5f46a96 commit 8d6f478
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
54 changes: 52 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ submit copydir "/source" "/dest" --merge "code.txt"
#### --exclude-dirs

`--exclude-dirs` 选项指定排除的目录列表,这些目录中的文件将会被跳过处理。
`--exclude-dirs` 选项指定排除的目录列表,这些目录中的文件将会被跳过处理。该选项支持 `glob` 模式,详见 [glob 模式介绍](#glob-模式介绍)

```bash
submit copydir "/source" "/dest" --exclude-dirs macro
Expand All @@ -263,14 +263,64 @@ submit copydir "/source" "/dest" --exclude-dirs macro qc initial

#### --exclude-files

`--exclude-files` 选项指定排除的文件列表,这些文件将会被跳过处理。
`--exclude-files` 选项指定排除的文件列表,这些文件将会被跳过处理。该选项支持 `glob` 模式,详见 [glob 模式介绍](#glob-模式介绍)

```bash
submit copydir "/source" "/dest" --exclude-dirs macro --exclude-files fcmp.sas format.sas
```

上述命令将在目录名称匹配 `macro` 时跳过处理其中的文件,并在文件名称匹配 `fcmp.sas``format.sas` (无论是否在 `macro` 目录中)时跳过处理。

### glob 模式介绍

`glob` 是一种使用通配符指定文件(目录)名称集合的模式,查看 [wiki](<https://en.wikipedia.org/wiki/Glob_(programming)>)

你可以在路径中使用以下特殊字符作为通配符:

- `*`: 匹配零个或多个字符,但不匹配路径分隔符 `/`。例如,`f*.sas` 匹配 `f1.sas``f2.sas``f3.sas` 等等。
- `**`: 匹配零个或多个任意字符。例如,`**/f*.sas` 匹配 `figure/f1.sas``figure/f2.sas``figure/draft/f1.sas` 等等。
- `?`: 匹配前面字符的零个或一个。例如,`t10?.sas` 匹配 `t1.sas``t10.sas`
- `+`: 匹配前面字符的一个或多个。例如,`t10+.sas` 匹配 `t10.sas``t100.sas``t1000.sas` 等等。
- `[]`: 匹配括号中列出的或包括在范围内的任一字母数字字符。范围只能包含 `a-z``A-Z``0-9`。例如,`t[1-3]0.sas` 匹配 `t10.sas``t20.sas``t30.sas`

假设有这样一个文件目录结构:

```
D:.
├─source
│ ├─f1.sas
│ ├─f2.sas
│ ├─f2-deprecated.sas
│ ├─f3.sas
│ ├─f3-deprecated.sas
│ ├─t1.sas
│ ├─t2.sas
│ ├─t2-deprecated.sas
│ ├─t2-deprecated-20241221.sas
│ ├─t3.sas
│ ├─t4.sas
│ ├─t5.sas
│ ├─t5-deprecated.sas
│ ├─t6.sas
│ ├─t7.sas
│ └─t7-deprecated.sas
└─dest
```

现在需要将 `source` 目录中的 `.sas` 文件转换为 `.txt` 文件,但忽略名称包含 `deprecated` 的文件。

如果不使用 `glob` 模式,命令应该是这样的:

```bash
submit copydir source dest --exclude-files "f2-deprecated.sas" "f3-deprecated.sas" "t2-deprecated.sas" "t2-deprecated-20241221.sas" "t5-deprecated.sas" "t7-deprecated.sas"
```

使用 `glob` 模式,命令得到简化:

```bash
submit copydir source dest --exclude-files "*-deprecated*.sas"
```

### 命令行选项参考

#### submit copyfile
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "submit"
version = "0.4.0"
version = "0.5.0"
description = "Extract code block which should be submitted to regulatory agency."
readme = "README.md"
requires-python = ">=3.10"
Expand Down
23 changes: 23 additions & 0 deletions submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations
import argparse
import glob
import os
import re
from enum import IntFlag, auto
Expand Down Expand Up @@ -57,6 +58,22 @@ def get_available_values(cls) -> list[ConvertMode]:
return modes


def expand_glob_patterns(patterns: list[str], root_dir: str) -> list[str]:
"""扩展 glob 模式。
Args:
patterns (list[str]): glob 模式列表。
root_dir (str): 根目录。
Returns:
list[str]: 文件路径列表。
"""
path = []
for pattern in patterns:
path.extend(glob.glob(pattern, root_dir=root_dir, recursive=True))
return path


def copy_file(
sas_file: str,
txt_file: str,
Expand Down Expand Up @@ -133,6 +150,12 @@ def copy_directory(
if not os.path.exists(txt_dir):
os.makedirs(txt_dir)

if exclude_dirs:
exclude_dirs = expand_glob_patterns(exclude_dirs, root_dir=sas_dir)

if exclude_files:
exclude_files = expand_glob_patterns(exclude_files, root_dir=sas_dir)

for dirpath, _, filenames in os.walk(sas_dir):
if exclude_dirs is not None and os.path.split(dirpath)[-1] in exclude_dirs:
continue
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8d6f478

Please sign in to comment.