yklibpy 0.1.49__tar.gz → 0.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- yklibpy-0.2.2/.python-version +1 -0
- yklibpy-0.2.2/CLAUDE.md +91 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/PKG-INFO +2 -2
- {yklibpy-0.1.49 → yklibpy-0.2.2}/mypy.ini +39 -39
- {yklibpy-0.1.49 → yklibpy-0.2.2}/pyproject.toml +2 -2
- yklibpy-0.2.2/src/yklibpy/cli/__init__.py +3 -0
- yklibpy-0.2.2/src/yklibpy/cli/cli.py +21 -0
- yklibpy-0.2.2/src/yklibpy/command/__init__.py +3 -0
- yklibpy-0.2.2/src/yklibpy/command/command.py +143 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/common/__init__.py +2 -1
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/common/safedict.py +3 -3
- yklibpy-0.2.2/src/yklibpy/common/timex.py +9 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/common/util_json.py +16 -16
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/common/util_yaml.py +1 -1
- yklibpy-0.2.2/src/yklibpy/config/__init__.py +3 -0
- yklibpy-0.2.2/src/yklibpy/config/appconfig.py +65 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/db/__init__.py +18 -7
- yklibpy-0.2.2/src/yklibpy/db/appstore.py +120 -0
- yklibpy-0.2.2/src/yklibpy/db/storex.py +56 -0
- yklibpy-0.2.2/src/yklibpy/htmlparser/__init__.py +27 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/htmlparser/app.py +2 -22
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/htmlparser/misc/__init__.py +6 -6
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/htmlparser/misc/anchortaginfo.py +28 -26
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/htmlparser/misc/anchortagx.py +26 -24
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/htmlparser/misc/priceinfo.py +17 -17
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/htmlparser/misc/tagx.py +30 -30
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/tomlop/fileitem.py +41 -41
- {yklibpy-0.1.49 → yklibpy-0.2.2}/uv.lock +46 -46
- yklibpy-0.1.49/.python-version +0 -1
- yklibpy-0.1.49/CLAUDE.md +0 -109
- yklibpy-0.1.49/src/yklibpy/htmlparser/__init__.py +0 -40
- yklibpy-0.1.49/src/yklibpy/htmlparser/amazonsavedcartscraper.py +0 -116
- yklibpy-0.1.49/src/yklibpy/htmlparser/fanzadoujinbasketscraper.py +0 -251
- yklibpy-0.1.49/src/yklibpy/htmlparser/fanzadoujinpurchasedscraper.py +0 -125
- yklibpy-0.1.49/src/yklibpy/htmlparser/kuscraper.py +0 -62
- yklibpy-0.1.49/src/yklibpy/htmlparser/udemyscraper.py +0 -190
- {yklibpy-0.1.49 → yklibpy-0.2.2}/.claude/settings.local.json +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/.editorconfig +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/.gitignore +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/.prettierrc +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/README.md +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/db.yaml +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/common/env.py +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/common/info.py +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/common/util.py +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/db/db_base.py +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/db/db_yaml.py +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/htmlparser/configprepare.py +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/htmlparser/htmlop.py +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/htmlparser/preparex.py +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/htmlparser/progress.py +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/htmlparser/scraper.py +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/py.typed +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/tomlop/__init__.py +0 -0
- {yklibpy-0.1.49 → yklibpy-0.2.2}/src/yklibpy/tomlop/tomlop.py +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.14.0
|
yklibpy-0.2.2/CLAUDE.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
+
|
|
5
|
+
## Project Overview
|
|
6
|
+
|
|
7
|
+
yklibpy is a Python utility library focused on HTML parsing/scraping, data format conversion (TOML/YAML), and application storage management. It targets Python 3.14+ and uses `uv` for dependency management with `hatchling` as the build backend.
|
|
8
|
+
|
|
9
|
+
## Development Commands
|
|
10
|
+
|
|
11
|
+
### Package Management
|
|
12
|
+
- Install dependencies: `uv sync`
|
|
13
|
+
- Add dependency: `uv add <package>`
|
|
14
|
+
- Add dev dependency: `uv add --dev <package>`
|
|
15
|
+
|
|
16
|
+
### Code Quality
|
|
17
|
+
- Format code: `black src/`
|
|
18
|
+
- Lint code: `ruff check src/`
|
|
19
|
+
- Type check: `mypy src/yklibpy`
|
|
20
|
+
- Configuration is split between `pyproject.toml` (strict mode, Python 3.14) and `mypy.ini` (module-specific settings with namespace packages enabled)
|
|
21
|
+
- Type checking uses strict mode by default
|
|
22
|
+
|
|
23
|
+
### Building and Testing
|
|
24
|
+
- Build package: `uv build` or `hatch build`
|
|
25
|
+
- Run tests: `pytest` (test directory: `tests/`, configured in pyproject.toml)
|
|
26
|
+
- Run a single test: `pytest tests/test_foo.py` or `pytest tests/test_foo.py::test_bar`
|
|
27
|
+
- Run with coverage: `pytest --cov`
|
|
28
|
+
|
|
29
|
+
### Publishing
|
|
30
|
+
- Build distribution: `python -m build`
|
|
31
|
+
- Upload to PyPI: `twine upload dist/*`
|
|
32
|
+
|
|
33
|
+
## Architecture
|
|
34
|
+
|
|
35
|
+
### Module Structure (`src/yklibpy/`)
|
|
36
|
+
|
|
37
|
+
The library is organized into six modules:
|
|
38
|
+
|
|
39
|
+
#### 1. `common/` - Core Utilities
|
|
40
|
+
- `Env`: Configuration and path management from YAML files, extracts scraper modes and base paths
|
|
41
|
+
- `Info`: Data container for parsed HTML information
|
|
42
|
+
- `Util`: General utilities including file operations and encoding detection
|
|
43
|
+
- `UtilYaml`/`UtilJson`: YAML/JSON operations with custom tag registration support
|
|
44
|
+
- `SafeDict`: Safe dictionary wrapper with type checking
|
|
45
|
+
- `Timex`: Timezone-aware timestamp utility (JST)
|
|
46
|
+
|
|
47
|
+
#### 2. `htmlparser/` - Web Scraping Framework
|
|
48
|
+
- **Base Class**: `Scraper` - core scraping logic with link extraction and deduplication
|
|
49
|
+
- **Concrete Scrapers**: `UdemyScraper`, `KUScraper`, `AmazonSavedCartScraper`, `FanzaDoujinPurchasedScraper`, `FanzaDoujinBasketScraper`
|
|
50
|
+
- **App**: Factory/orchestrator that creates the appropriate scraper based on a mode string from configuration. Processes HTML via BeautifulSoup (bs4 + html5lib parser, not lxml).
|
|
51
|
+
- **Supporting**: `HtmlOp`, `Preparex`, `Progress`, and `misc/` helpers (`AnchorTagInfo`, `AnchorTagx`, `PriceInfo`, `Tagx`)
|
|
52
|
+
|
|
53
|
+
#### 3. `tomlop/` - TOML/YAML Conversion
|
|
54
|
+
- `Tomlop`: Bidirectional TOML ↔ YAML conversion with deep dictionary comparison
|
|
55
|
+
- `FileItem`: File metadata and operations wrapper
|
|
56
|
+
|
|
57
|
+
#### 4. `db/` - Database and Storage Abstraction
|
|
58
|
+
- `DbBase`/`DbYaml`: YAML-backed key-value store with `chardet` encoding detection and custom YAML tag constructors
|
|
59
|
+
- `Storex`: File-backed storage supporting YAML, JSON, and plain text formats with auto-creation of parent directories
|
|
60
|
+
- `AppStore`: Cross-platform config/data file manager using OS conventions (APPDATA/LOCALAPPDATA on Windows, XDG on Unix)
|
|
61
|
+
|
|
62
|
+
#### 5. `cli/` - CLI Framework
|
|
63
|
+
- `Cli`: argparse wrapper for building command-line interfaces
|
|
64
|
+
|
|
65
|
+
#### 6. `command/` - Command Execution
|
|
66
|
+
- `Command`: Subprocess runner with timeout support and output capture, plus `array_to_dict` utility
|
|
67
|
+
|
|
68
|
+
#### 7. `config/` - Application Configuration
|
|
69
|
+
- `AppConfig`: File type constants, file association mappings, and default field definitions for config/db files
|
|
70
|
+
|
|
71
|
+
### Key Patterns
|
|
72
|
+
|
|
73
|
+
**Scraper Pattern**: All scrapers inherit from `Scraper` and implement site-specific parsing. `App` is the factory that selects the scraper via a mode string from YAML config.
|
|
74
|
+
|
|
75
|
+
**Storage Pattern**: `AppConfig` defines file associations (config vs db, YAML vs JSON) → `AppStore` resolves platform-specific paths → `Storex` handles file I/O. Config files go to APPDATA/%XDG_CONFIG_HOME, data files go to LOCALAPPDATA/XDG_DATA_HOME.
|
|
76
|
+
|
|
77
|
+
**Entry Points**: Multiple CLI entry points in pyproject.toml under `[project.scripts]` for scrapers, TOML tools, and DB utilities.
|
|
78
|
+
|
|
79
|
+
## Important Notes
|
|
80
|
+
|
|
81
|
+
### Type Annotations
|
|
82
|
+
- Strict mypy checking enabled
|
|
83
|
+
- Uses modern Python 3.14+ type syntax: `dict[str, Any]` not `Dict`, `X | None` not `Optional[X]`
|
|
84
|
+
|
|
85
|
+
### Encoding
|
|
86
|
+
- Uses `chardet` to detect file encodings with explicit encoding on all file operations
|
|
87
|
+
- Falls back to default encoding when detection fails
|
|
88
|
+
|
|
89
|
+
### Ruff Configuration
|
|
90
|
+
- Ignores E501 (line length) — no line length enforcement
|
|
91
|
+
- Checks: E (pycodestyle errors), F (pyflakes), I (isort), N (naming)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: yklibpy
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: Utilty Library by ykominami
|
|
5
5
|
Project-URL: Homepage, https://github.com/ykominami/yklibpy
|
|
6
6
|
Project-URL: Repository, https://github.com/ykominami/yklibpy
|
|
@@ -10,7 +10,7 @@ Classifier: Development Status :: 3 - Alpha
|
|
|
10
10
|
Classifier: Intended Audience :: Developers
|
|
11
11
|
Classifier: License :: OSI Approved :: MIT License
|
|
12
12
|
Classifier: Programming Language :: Python :: 3
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
14
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
15
15
|
Requires-Python: >=3.14
|
|
16
16
|
Requires-Dist: bs4>=0.0.2
|
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
[mypy]
|
|
2
|
-
mypy_path = src
|
|
3
|
-
namespace_packages = True
|
|
4
|
-
explicit_package_bases = True
|
|
5
|
-
check_untyped_defs = True
|
|
6
|
-
|
|
7
|
-
[mypy-yklibpy.htmlparser.scraper.*]
|
|
8
|
-
ignore_errors = False
|
|
9
|
-
[fanzadoujinbasketscraper.*]
|
|
10
|
-
ignore_errors = False
|
|
11
|
-
[util.*]
|
|
12
|
-
ignore_errors = False
|
|
13
|
-
[udemyscraper.*]
|
|
14
|
-
ignore_errors = False
|
|
15
|
-
[kuscraper.*]
|
|
16
|
-
ignore_errors = False
|
|
17
|
-
[amazon_saved_cart_scraper.*]
|
|
18
|
-
ignore_errors = False
|
|
19
|
-
[htmlop.*]
|
|
20
|
-
ignore_errors = False
|
|
21
|
-
[misc.*]
|
|
22
|
-
ignore_errors = False
|
|
23
|
-
[price_info.*]
|
|
24
|
-
ignore_errors = False
|
|
25
|
-
[tagx.*]
|
|
26
|
-
ignore_errors = False
|
|
27
|
-
[util_yaml.*]
|
|
28
|
-
ignore_errors = False
|
|
29
|
-
[work_info.*]
|
|
30
|
-
ignore_errors = False
|
|
31
|
-
[ymain.*]
|
|
32
|
-
ignore_errors = False
|
|
33
|
-
[zmain.*]
|
|
34
|
-
ignore_errors = False
|
|
35
|
-
[db_yaml.*]
|
|
36
|
-
ignore_errors = False
|
|
37
|
-
|
|
38
|
-
# [mypy-untyped_package.*]
|
|
39
|
-
# follow_untyped_imports = True
|
|
1
|
+
[mypy]
|
|
2
|
+
mypy_path = src
|
|
3
|
+
namespace_packages = True
|
|
4
|
+
explicit_package_bases = True
|
|
5
|
+
check_untyped_defs = True
|
|
6
|
+
|
|
7
|
+
[mypy-yklibpy.htmlparser.scraper.*]
|
|
8
|
+
ignore_errors = False
|
|
9
|
+
[fanzadoujinbasketscraper.*]
|
|
10
|
+
ignore_errors = False
|
|
11
|
+
[util.*]
|
|
12
|
+
ignore_errors = False
|
|
13
|
+
[udemyscraper.*]
|
|
14
|
+
ignore_errors = False
|
|
15
|
+
[kuscraper.*]
|
|
16
|
+
ignore_errors = False
|
|
17
|
+
[amazon_saved_cart_scraper.*]
|
|
18
|
+
ignore_errors = False
|
|
19
|
+
[htmlop.*]
|
|
20
|
+
ignore_errors = False
|
|
21
|
+
[misc.*]
|
|
22
|
+
ignore_errors = False
|
|
23
|
+
[price_info.*]
|
|
24
|
+
ignore_errors = False
|
|
25
|
+
[tagx.*]
|
|
26
|
+
ignore_errors = False
|
|
27
|
+
[util_yaml.*]
|
|
28
|
+
ignore_errors = False
|
|
29
|
+
[work_info.*]
|
|
30
|
+
ignore_errors = False
|
|
31
|
+
[ymain.*]
|
|
32
|
+
ignore_errors = False
|
|
33
|
+
[zmain.*]
|
|
34
|
+
ignore_errors = False
|
|
35
|
+
[db_yaml.*]
|
|
36
|
+
ignore_errors = False
|
|
37
|
+
|
|
38
|
+
# [mypy-untyped_package.*]
|
|
39
|
+
# follow_untyped_imports = True
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "yklibpy"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.2.2"
|
|
4
4
|
description = "Utilty Library by ykominami"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
authors = [
|
|
@@ -23,7 +23,7 @@ classifiers = [
|
|
|
23
23
|
"Intended Audience :: Developers",
|
|
24
24
|
"License :: OSI Approved :: MIT License",
|
|
25
25
|
"Programming Language :: Python :: 3",
|
|
26
|
-
"Programming Language :: Python :: 3.
|
|
26
|
+
"Programming Language :: Python :: 3.14",
|
|
27
27
|
"Programming Language :: Python :: Implementation :: PyPy",
|
|
28
28
|
]
|
|
29
29
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
from yklibpy.db.appstore import AppStore
|
|
5
|
+
|
|
6
|
+
class Cli:
|
|
7
|
+
def __init__(self, help_text: str) -> None:
|
|
8
|
+
self.parser = argparse.ArgumentParser(
|
|
9
|
+
description=help_text
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
def get_args(self) -> argparse.Namespace:
|
|
13
|
+
return self.args
|
|
14
|
+
|
|
15
|
+
def parse_args(self):
|
|
16
|
+
self.args = self.parser.parse_args()
|
|
17
|
+
return self.args
|
|
18
|
+
|
|
19
|
+
def get_subparsers(self, name: str) -> argparse.ArgumentParser:
|
|
20
|
+
self.subparsers = self.parser.add_subparsers(dest=name, required=True)
|
|
21
|
+
return self.subparsers
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import subprocess
|
|
3
|
+
from typing import Any, Optional
|
|
4
|
+
|
|
5
|
+
class Command:
|
|
6
|
+
def __init__():
|
|
7
|
+
pass
|
|
8
|
+
|
|
9
|
+
def run_command(
|
|
10
|
+
self,
|
|
11
|
+
command: str | list[str],
|
|
12
|
+
shell: bool = False,
|
|
13
|
+
encoding: str = "utf-8",
|
|
14
|
+
timeout: Optional[int] = None,
|
|
15
|
+
) -> tuple[str, int]:
|
|
16
|
+
"""
|
|
17
|
+
コマンドラインを実行して、標準出力への出力を文字列として受け取る。
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
command: 実行するコマンド(文字列またはリスト)
|
|
21
|
+
shell: shell経由で実行するかどうか(デフォルト: False)
|
|
22
|
+
encoding: 出力のエンコーディング(デフォルト: utf-8)
|
|
23
|
+
timeout: タイムアウト秒数(デフォルト: None)
|
|
24
|
+
|
|
25
|
+
Returns:
|
|
26
|
+
(標準出力の文字列, 終了コード) のタプル
|
|
27
|
+
|
|
28
|
+
Raises:
|
|
29
|
+
subprocess.TimeoutExpired: タイムアウトが発生した場合
|
|
30
|
+
subprocess.SubprocessError: その他のサブプロセスエラー
|
|
31
|
+
|
|
32
|
+
Example:
|
|
33
|
+
>>> ghprj = Ghprj()
|
|
34
|
+
>>> output, return_code = ghprj.run_command("echo hello")
|
|
35
|
+
>>> print(output) # "hello\\n"
|
|
36
|
+
>>> print(return_code) # 0
|
|
37
|
+
"""
|
|
38
|
+
try:
|
|
39
|
+
result = subprocess.run(
|
|
40
|
+
command,
|
|
41
|
+
shell=shell,
|
|
42
|
+
capture_output=True,
|
|
43
|
+
text=True,
|
|
44
|
+
encoding=encoding,
|
|
45
|
+
timeout=timeout,
|
|
46
|
+
)
|
|
47
|
+
return result.stdout, result.returncode
|
|
48
|
+
except subprocess.TimeoutExpired as e:
|
|
49
|
+
timeout_float = float(timeout) if timeout is not None else float(0)
|
|
50
|
+
raise subprocess.TimeoutExpired(
|
|
51
|
+
cmd=command,
|
|
52
|
+
timeout=timeout_float,
|
|
53
|
+
output=e.stdout.decode(encoding) if e.stdout else "",
|
|
54
|
+
stderr=e.stderr.decode(encoding) if e.stderr else "",
|
|
55
|
+
)
|
|
56
|
+
except subprocess.SubprocessError:
|
|
57
|
+
raise
|
|
58
|
+
|
|
59
|
+
def run_command_simple(self, command: str | list[str], shell: bool = False) -> str:
|
|
60
|
+
"""
|
|
61
|
+
コマンドラインを実行して、標準出力への出力を文字列として受け取る(シンプル版)。
|
|
62
|
+
エラー時は例外を発生させる。
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
command: 実行するコマンド(文字列またはリスト)
|
|
66
|
+
shell: shell経由で実行するかどうか(デフォルト: False)
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
標準出力の文字列
|
|
70
|
+
|
|
71
|
+
Raises:
|
|
72
|
+
subprocess.CalledProcessError: コマンドが非ゼロの終了コードで終了した場合
|
|
73
|
+
|
|
74
|
+
Example:
|
|
75
|
+
>>> ghprj = Ghprj()
|
|
76
|
+
>>> output = ghprj.run_command_simple("echo hello")
|
|
77
|
+
>>> print(output) # "hello\\n"
|
|
78
|
+
"""
|
|
79
|
+
try:
|
|
80
|
+
result = subprocess.run(
|
|
81
|
+
command,
|
|
82
|
+
shell=shell,
|
|
83
|
+
capture_output=True,
|
|
84
|
+
text=True,
|
|
85
|
+
check=True,
|
|
86
|
+
encoding="utf-8",
|
|
87
|
+
)
|
|
88
|
+
return result.stdout
|
|
89
|
+
except subprocess.CalledProcessError as e:
|
|
90
|
+
logging.exception(e)
|
|
91
|
+
'''
|
|
92
|
+
raise subprocess.CalledProcessError(
|
|
93
|
+
returncode=e.returncode,
|
|
94
|
+
cmd=command,
|
|
95
|
+
output=e.stdout.decode("utf-8") if e.stdout else "",
|
|
96
|
+
stderr=e.stderr.decode("utf-8") if e.stderr else "",
|
|
97
|
+
)
|
|
98
|
+
'''
|
|
99
|
+
|
|
100
|
+
def array_to_dict(
|
|
101
|
+
self, data: list[dict[str, Any]], key: str
|
|
102
|
+
) -> dict[str, dict[str, Any]]:
|
|
103
|
+
"""
|
|
104
|
+
JSON形式文字列の配列から、指定文字列をキーとする連想配列に変換する。
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
data: 辞書のリスト(JSON配列をパースしたもの)
|
|
108
|
+
key: 連想配列のキーとして使用するフィールド名
|
|
109
|
+
|
|
110
|
+
Returns:
|
|
111
|
+
指定したキーをキーとし、元の要素を値とする連想配列(辞書)
|
|
112
|
+
|
|
113
|
+
Raises:
|
|
114
|
+
KeyError: 指定したキーが要素に存在しない場合
|
|
115
|
+
TypeError: 要素が辞書でない場合
|
|
116
|
+
|
|
117
|
+
Example:
|
|
118
|
+
>>> ghprj = Ghprj()
|
|
119
|
+
>>> data = [
|
|
120
|
+
... {"name": "repo1", "url": "https://example.com/repo1"},
|
|
121
|
+
... {"name": "repo2", "url": "https://example.com/repo2"}
|
|
122
|
+
... ]
|
|
123
|
+
>>> result = ghprj.array_to_dict(data, "name")
|
|
124
|
+
>>> print(result["repo1"]) # {"name": "repo1", "url": "https://example.com/repo1"}
|
|
125
|
+
"""
|
|
126
|
+
result: dict[str, dict[str, Any]] = {}
|
|
127
|
+
for item in data:
|
|
128
|
+
if not isinstance(item, dict):
|
|
129
|
+
raise TypeError(
|
|
130
|
+
f"配列の要素は辞書である必要があります。現在の型: {type(item).__name__}"
|
|
131
|
+
)
|
|
132
|
+
if key not in item:
|
|
133
|
+
raise KeyError(
|
|
134
|
+
f"キー '{key}' が要素に存在しません。要素のキー: {list(item.keys())}"
|
|
135
|
+
)
|
|
136
|
+
key_value = item[key]
|
|
137
|
+
if not isinstance(key_value, (str, int, float, bool)) or key_value is None:
|
|
138
|
+
raise ValueError(
|
|
139
|
+
f"キー '{key}' の値は文字列、数値、真偽値である必要があります。現在の型: {type(key_value).__name__}"
|
|
140
|
+
)
|
|
141
|
+
result[str(key_value)] = item
|
|
142
|
+
|
|
143
|
+
return result
|
|
@@ -4,8 +4,9 @@ from yklibpy.common.util import Util
|
|
|
4
4
|
from yklibpy.common.util_yaml import UtilYaml
|
|
5
5
|
from yklibpy.common.util_json import UtilJson
|
|
6
6
|
from yklibpy.common.safedict import SafeDict
|
|
7
|
+
from yklibpy.common.timex import Timex
|
|
7
8
|
|
|
8
|
-
__all__ = ["Env", "Info", "Util", "UtilYaml", "UtilJson", "SafeDict"]
|
|
9
|
+
__all__ = ["Env", "Info", "Util", "UtilYaml", "UtilJson", "SafeDict", "Timex"]
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
def xmain() -> str:
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
class SafeDict(dict):
|
|
2
|
-
def __missing__(self, key):
|
|
3
|
-
return f"{{{key}}}"
|
|
1
|
+
class SafeDict(dict):
|
|
2
|
+
def __missing__(self, key):
|
|
3
|
+
return f"{{{key}}}"
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import json
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
class UtilJson:
|
|
5
|
-
@classmethod
|
|
6
|
-
def load_file(cls, file_name: str):
|
|
7
|
-
data = None
|
|
8
|
-
with open(file_name, "r", encoding="utf-8") as f:
|
|
9
|
-
data = json.load(f)
|
|
10
|
-
return data
|
|
11
|
-
|
|
12
|
-
@classmethod
|
|
13
|
-
def load_string(cls, string: str):
|
|
14
|
-
data = None
|
|
15
|
-
data = json.loads(string)
|
|
16
|
-
return data
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class UtilJson:
|
|
5
|
+
@classmethod
|
|
6
|
+
def load_file(cls, file_name: str):
|
|
7
|
+
data = None
|
|
8
|
+
with open(file_name, "r", encoding="utf-8") as f:
|
|
9
|
+
data = json.load(f)
|
|
10
|
+
return data
|
|
11
|
+
|
|
12
|
+
@classmethod
|
|
13
|
+
def load_string(cls, string: str):
|
|
14
|
+
data = None
|
|
15
|
+
data = json.loads(string)
|
|
16
|
+
return data
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
class AppConfig:
|
|
2
|
+
FILE_TYPE_YAML = "YAML"
|
|
3
|
+
FILE_TYPE_JSON = "JSON"
|
|
4
|
+
|
|
5
|
+
file_type_dict = {
|
|
6
|
+
FILE_TYPE_YAML: ".yml",
|
|
7
|
+
FILE_TYPE_JSON: ".json",
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
file_assoc = {
|
|
11
|
+
"config": {
|
|
12
|
+
"config": {
|
|
13
|
+
"file_type": FILE_TYPE_YAML,
|
|
14
|
+
"ext_name": "",
|
|
15
|
+
"file": "",
|
|
16
|
+
"value": "",
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"db": {
|
|
20
|
+
"db": {
|
|
21
|
+
"file_type": FILE_TYPE_YAML,
|
|
22
|
+
"ext_name": "",
|
|
23
|
+
"file": "",
|
|
24
|
+
"value": "",
|
|
25
|
+
},
|
|
26
|
+
"fetch": {
|
|
27
|
+
"file_type": FILE_TYPE_YAML,
|
|
28
|
+
"ext_name": "",
|
|
29
|
+
"file": "",
|
|
30
|
+
"value": "",
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
}
|
|
34
|
+
default_json_fields_in_db = [
|
|
35
|
+
"name",
|
|
36
|
+
"count",
|
|
37
|
+
"valid",
|
|
38
|
+
"url",
|
|
39
|
+
"owner",
|
|
40
|
+
"nameWithOwner",
|
|
41
|
+
"parent",
|
|
42
|
+
"pullRequests",
|
|
43
|
+
"createdAt",
|
|
44
|
+
"description",
|
|
45
|
+
"diskUsage",
|
|
46
|
+
"hasProjectsEnabled",
|
|
47
|
+
"homepageUrl",
|
|
48
|
+
]
|
|
49
|
+
default_json_fields = [
|
|
50
|
+
"name",
|
|
51
|
+
"url",
|
|
52
|
+
"owner",
|
|
53
|
+
"nameWithOwner",
|
|
54
|
+
"parent",
|
|
55
|
+
"pullRequests",
|
|
56
|
+
"createdAt",
|
|
57
|
+
"description",
|
|
58
|
+
"diskUsage",
|
|
59
|
+
"hasProjectsEnabled",
|
|
60
|
+
"homepageUrl",
|
|
61
|
+
]
|
|
62
|
+
fetch_item = {
|
|
63
|
+
"date": "",
|
|
64
|
+
}
|
|
65
|
+
key = "JSON_FIELDS"
|
|
@@ -1,20 +1,33 @@
|
|
|
1
1
|
from yklibpy.db.db_base import DbBase
|
|
2
2
|
from yklibpy.db.db_yaml import DbYaml
|
|
3
|
+
from yklibpy.db.storex import Storex
|
|
4
|
+
from yklibpy.db.appstore import AppStore
|
|
3
5
|
|
|
4
|
-
__all__ = [
|
|
6
|
+
__all__ = [
|
|
7
|
+
"DbBase",
|
|
8
|
+
"DbYaml",
|
|
9
|
+
"Storex",
|
|
10
|
+
"AppStore",
|
|
11
|
+
"get_or_create_db",
|
|
12
|
+
"db_yaml",
|
|
13
|
+
"db_yaml_x",
|
|
14
|
+
]
|
|
5
15
|
|
|
6
|
-
|
|
16
|
+
|
|
17
|
+
def get_or_create_db(kind: str, fname: str) -> DbYaml | None:
|
|
7
18
|
if kind.lower() == "yaml":
|
|
8
19
|
db = DbYaml(fname)
|
|
9
20
|
else:
|
|
10
21
|
db = None
|
|
11
22
|
return db
|
|
12
23
|
|
|
24
|
+
|
|
13
25
|
def db_yaml_x() -> DbYaml:
|
|
14
26
|
return db_yaml("db.yml")
|
|
15
27
|
|
|
16
|
-
|
|
17
|
-
|
|
28
|
+
|
|
29
|
+
def db_yaml(db_file: str) -> DbYaml:
|
|
30
|
+
db = get_or_create_db("yaml", db_file)
|
|
18
31
|
if db is None:
|
|
19
32
|
raise ValueError("Failed to create database")
|
|
20
33
|
db.load()
|
|
@@ -23,9 +36,7 @@ def db_yaml(db_file:str) -> DbYaml:
|
|
|
23
36
|
return db
|
|
24
37
|
|
|
25
38
|
|
|
26
|
-
__all__ = ["DbYaml", "get_or_create_db", "db_yaml"]
|
|
27
|
-
|
|
28
39
|
if __name__ == "__main__":
|
|
29
40
|
fname = "db.yaml"
|
|
30
|
-
db = get_or_create_db("
|
|
41
|
+
db = get_or_create_db("yaml", fname)
|
|
31
42
|
print(f"db={db}")
|