Skip to content

Commit ce154f4

Browse files
authored
Merge pull request #646 from PyThaiNLP/Fixed-#645
Add PYTHAINLP_READ_MODE
2 parents 61a0682 + 7d16224 commit ce154f4

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

docs/notes/installation.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ Runtime Configurations
6767

6868
Type `thainlp data path` at command line to see current PYTHAINLP_DATA_DIR.
6969

70+
.. envvar:: PYTHAINLP_READ_MODE
71+
72+
This environment variable specifies config PyThaiNLP to read-only mode. (0 = False, 1 = True)
7073

7174
FAQ
7275
===
@@ -76,3 +79,6 @@ A: For Python 3.10+, We have python-crfsuite problem in pythainlp, so you can re
7679

7780
Q: How to set the environment variables of each executor node in a distributed environment?
7881
A: You can read `PermissionError: [Errno 13] Permission denied: /home/pythainlp-data <https://github.com/PyThaiNLP/pythainlp/issues/475>`_.
82+
83+
Q: How to fixed PyThaiNLP to read-only mode?
84+
A: You can config PYTHAINLP_READ_MODE.

pythainlp/corpus/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
_CORPUS_DIRNAME = "corpus"
4040
_CORPUS_PATH = os.path.join(get_pythainlp_path(), _CORPUS_DIRNAME)
41+
_CHECK_MODE = os.getenv("PYTHAINLP_READ_MODE")
4142

4243
# remote corpus catalog URL
4344
_CORPUS_DB_URL = (
@@ -51,7 +52,7 @@
5152
_CORPUS_DB_PATH = get_full_data_path(_CORPUS_DB_FILENAME)
5253

5354
# create a local corpus database if it does not already exist
54-
if not os.path.exists(_CORPUS_DB_PATH):
55+
if not os.path.exists(_CORPUS_DB_PATH) and _CHECK_MODE != "1":
5556
TinyDB(_CORPUS_DB_PATH).close()
5657

5758

pythainlp/corpus/core.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
from pythainlp import __version__
1818

1919

20+
_CHECK_MODE = os.getenv("PYTHAINLP_READ_MODE")
21+
22+
2023
def get_corpus_db(url: str) -> requests.Response:
2124
"""
2225
Get corpus catalog from server.
@@ -42,7 +45,10 @@ def get_corpus_db_detail(name: str, version: str = None) -> dict:
4245
:return: details about a corpus
4346
:rtype: dict
4447
"""
45-
local_db = TinyDB(corpus_db_path())
48+
if _CHECK_MODE == "1":
49+
local_db = TinyDB(corpus_db_path(), access_mode='r')
50+
else:
51+
local_db = TinyDB(corpus_db_path(), access_mode='r')
4652
query = Query()
4753
if version is None:
4854
res = local_db.search(query.name == name)
@@ -354,6 +360,9 @@ def download(
354360
``$HOME/pythainlp-data/``
355361
(e.g. ``/Users/bact/pythainlp-data/wiki_lm_lstm.pth``).
356362
"""
363+
if _CHECK_MODE == "1":
364+
print("PyThaiNLP is read-only mode. It can't download.")
365+
return False
357366
if not url:
358367
url = corpus_db_url()
359368

@@ -453,6 +462,9 @@ def remove(name: str) -> bool:
453462
# FileNotFoundError: [Errno 2] No such file or directory:
454463
# '/usr/local/lib/python3.6/dist-packages/pythainlp/corpus/ttc'
455464
"""
465+
if _CHECK_MODE == "1":
466+
print("PyThaiNLP is read-only mode. It can't remove corpus.")
467+
return False
456468
db = TinyDB(corpus_db_path())
457469
query = Query()
458470
data = db.search(query.name == name)

0 commit comments

Comments
 (0)