Skip to content

Commit

Permalink
feat: add --merge option (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Snoopy1866 authored Jan 2, 2025
1 parent ed8a4d6 commit 5f46a96
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,27 @@ submit copydir "/source" "/dest"

[`--encoding`](#--encoding)

#### --merge

`--merge` 选项指定将所有 `.sas` 文件进行转换后合并到单个 `.txt` 文件。

例如:

```bash
submit copydir "/source" "/dest" --merge "code.txt"
```

上述代码会将 `/source` 目录中的所有 `.sas` 文件转换成 `.txt` 文件,并将转换后的 `.txt` 文件合并到 `/dest/code.txt` 中。

> [!NOTE]
>
> 合并后的 `.txt` 文件包含源目录中所有需要递交的 sas 代码,使用注释 `/*======`_`filename`_`.txt======*/` 分隔来自不同 `.sas` 文件的代码。
> 其中 _`filename`_ 是源目录中 `.sas` 文件名称(不含扩展名)。
> [!IMPORTANT]
>
> 某些地方医疗器械监督管理局不接收压缩包作为递交文件,且递交文件数量存在限制,因此必须将所有 `.sas` 文件合并成一个单独的 `.txt` 文件。
#### --exclude-dirs

`--exclude-dirs` 选项指定排除的目录列表,这些目录中的文件将会被跳过处理。
Expand Down Expand Up @@ -273,7 +294,7 @@ options:
#### submit copydir

```bash
usage: submit [options] copydir [-h] [-c {positive,negative,both}] [--macro-subs MACRO_SUBS] [--encoding ENCODING] [-exf [EXCLUDE_FILES ...]] [-exd [EXCLUDE_DIRS ...]] sas_dir txt_dir
usage: submit [options] copydir [-h] [-c {positive,negative,both}] [--macro-subs MACRO_SUBS] [--encoding ENCODING] [-mrg MERGE] [-exf [EXCLUDE_FILES ...]] [-exd [EXCLUDE_DIRS ...]] sas_dir txt_dir

positional arguments:
sas_dir SAS 文件目录
Expand All @@ -286,6 +307,7 @@ options:
--macro-subs MACRO_SUBS
宏变量替换,格式为 {key=value,...}(默认无)
--encoding ENCODING 编码格式(默认自动检测)
-mrg, --merge MERGE 合并到一个文件(默认无)
-exf, --exclude-files [EXCLUDE_FILES ...]
排除文件列表(默认无)
-exd, --exclude-dirs [EXCLUDE_DIRS ...]
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ build-backend = "setuptools.build_meta"

[project]
name = "submit"
version = "0.3.0"
version = "0.4.0"
description = "Extract code block which should be submitted to regulatory agency."
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"chardet>=5.2.0",
"natsort>=8.4.0",
]
authors = [{ name = "Snoopy1866", email = "[email protected]"}]
license = {file = "LICENSE"}
Expand Down
25 changes: 25 additions & 0 deletions submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from enum import IntFlag, auto

from chardet import detect
from natsort import natsorted

SYLBOMS = r"[\s\*\-\=]"

Expand Down Expand Up @@ -106,6 +107,7 @@ def copy_file(
def copy_directory(
sas_dir: str,
txt_dir: str,
merge: str | None = None,
convert_mode: ConvertMode = ConvertMode.BOTH,
macro_subs: dict[str, str] | None = None,
exclude_files: list[str] = None,
Expand All @@ -117,6 +119,7 @@ def copy_directory(
Args:
sas_dir (str): SAS 文件夹路径。
txt_dir (str): TXT 文件夹路径。
merge (str | None, optional): 合并到一个文件,默认值为 None。
convert_mode (ConvertMode, optional): 转换模式,默认值为 ConvertMode.BOTH。
macro_subs (dict[str, str] | None, optional): 一个字典,其键为 SAS 代码中的宏变量名称,值为替代的字符串,默认值为 None。
exclude_files (list[str], optional): 排除文件列表,默认值为 None。
Expand All @@ -142,6 +145,26 @@ def copy_directory(
txt_file = os.path.join(txt_dir, ref_path, file.replace(".sas", ".txt"))
copy_file(sas_file, txt_file, convert_mode=convert_mode, macro_subs=macro_subs, encoding=encoding)

if merge is not None:
merge_file = os.path.join(txt_dir, merge)
with open(merge_file, "w", encoding=encoding) as f:
for dirpath, _, filenames in os.walk(txt_dir):
filenames = natsorted(filenames)
for file in filenames:
if file.endswith(".txt") and file != merge:
txt_file = os.path.join(dirpath, file)
with open(txt_file, "r", encoding=encoding) as txt:
f.write(f"/*======{file}======*/\n")
f.write(txt.read())
f.write("\n")

for dirpath, _, filenames in os.walk(txt_dir):
filenames = natsorted(filenames)
for file in filenames:
if file.endswith(".txt") and file != merge:
txt_file = os.path.join(dirpath, file)
os.remove(txt_file)


def parse_dict(arg: str) -> dict[str, str]:
"""解析字典字符串。
Expand Down Expand Up @@ -195,6 +218,7 @@ def main() -> None:
parser_dir = subparsers.add_parser("copydir", aliases=["cpd"], parents=[parent_parser], help="多个 SAS 文件转换")
parser_dir.add_argument("sas_dir", help="SAS 文件目录")
parser_dir.add_argument("txt_dir", help="TXT 文件目录")
parser_dir.add_argument("-mrg", "--merge", default=None, help="合并到一个文件(默认无)")
parser_dir.add_argument("-exf", "--exclude-files", nargs="*", default=None, help="排除文件列表(默认无)")
parser_dir.add_argument("-exd", "--exclude-dirs", nargs="*", default=None, help="排除目录列表(默认无)")

Expand All @@ -212,6 +236,7 @@ def main() -> None:
copy_directory(
sas_dir=args.sas_dir,
txt_dir=args.txt_dir,
merge=args.merge,
convert_mode=args.convert_mode,
macro_subs=args.macro_subs,
exclude_files=args.exclude_files,
Expand Down
17 changes: 15 additions & 2 deletions uv.lock

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

0 comments on commit 5f46a96

Please sign in to comment.