Skip to content

Commit 29234f0

Browse files
committed
Fixed file:(attribute:private<__m_is_lock> of class<File> is bool<True> when the file lock is off); Update file:Add property<status> to class<File>.
1 parent 716c231 commit 29234f0

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77

88
An easier-to-use Python package that object-oriented encapsulates Python traditional built-in file operations. It's also easier than `easygui`. o((>ω< ))o
99

10+
## 💿Obtainment
11+
12+
The source code is currently hosted on Github at: https://github.com/leoweyr/Python-Easierfile
13+
14+
Binary installers for the latest released version are available at the [Python Package Index (PyPI)](https://pypi.org/project/easierfile/).
15+
16+
```sh
17+
# PyPI
18+
pip install easierfile
19+
```
20+
21+
## 🔗Dependencies
22+
23+
- [Dynoptimdict - A dynamic data dict class that inherits and overrides the built-in dict class for special purposes. That provides real-time access to dynamic data, while still allowing the option to get only the specified data without calculating all. It adheres to the concept of program optimization which avoids loading if not used, saving both memory and time](https://github.com/leoweyr/Python-Dynoptimdict)
24+
1025
## ⚖️License
1126

1227
[MIT](https://github.com/leoweyr/Python-Easierfile/blob/main/LICENSE)

doc/API reference.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ File(file_path, is_auto_create, is_occupy)
2323
<td><code>File().info</code></td>
2424
<td>Return a dict of file attribute information.</br>● path: the absolute path of the file</br>● dir_path: the absolute path of the directory where the file is located</br>● full_name: the name of the file</br>● name: the name of the file without file extension</br>● ext: the extension of the file</td>
2525
</tr>
26+
<tr>
27+
<td><code>File().status</code></td>
28+
<td>Return a dict of file status information.</br>● file_lock: whether file lock is on</br>● exist: whether the file exist</td>
29+
</tr>
2630
</table>
2731

2832

easierfile/file.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def _unlock_file(file):
2121

2222

2323
import os
24-
24+
from dynoptimdict import DynamicDataDict as Dict
2525

2626
class File:
2727
def __init__(self, file_path, is_auto_create=True, is_occupy=True):
@@ -54,23 +54,23 @@ def __del__(self):
5454
pass
5555

5656
def __lock(self, is_lock):
57-
if os.path.isfile(self.__m_info["path"]):
57+
if self.status["exist"]:
5858
if is_lock:
5959
self.__m_file = open(self.__m_info["path"], "w")
6060
_lock_file(self.__m_file)
6161
self.__m_is_lock = is_lock
6262
else:
6363
_unlock_file(self.__m_file)
6464
self.__m_file.close()
65-
self.__m_is_lock = not is_lock
65+
self.__m_is_lock = is_lock
6666
else:
6767
if is_lock:
6868
raise FileNotFoundError("File was about to be occupied, but not found: " + self.__m_info["path"])
6969
else:
7070
raise FileNotFoundError("File was about to be unoccupied, but not found: " + self.__m_info["path"])
7171

7272
def create(self):
73-
if not os.path.isfile(self.__m_info["path"]):
73+
if not self.status["exist"]:
7474
if not os.path.exists(self.__m_info["dir_path"]): # Create the file directory if it doesn't exist, so that code<open()> doesn't throw the exception.
7575
os.mkdir(self.__m_info["dir_path"])
7676
try:
@@ -87,19 +87,19 @@ def create(self):
8787
def delete(self):
8888
if self.__m_is_lock:
8989
self.__lock(False)
90-
if os.path.isfile(self.__m_info["path"]):
90+
if self.status["exist"]:
9191
os.remove(self.__m_info["path"])
9292

9393
def rewrite(self,content):
94-
if os.path.isfile(self.__m_info["path"]):
94+
if self.status["exist"]:
9595
file_temp = open(self.__m_info["path"], "w")
9696
file_temp.write(content)
9797
file_temp.close()
9898
else:
9999
raise FileNotFoundError("File not found: " + self.__m_info["path"])
100100

101101
def append(self,content):
102-
if os.path.isfile(self.__m_info["path"]):
102+
if self.status["exist"]:
103103
file_temp = open(self.__m_info["path"], "a")
104104
file_temp.write(content)
105105
file_temp.close()
@@ -108,7 +108,7 @@ def append(self,content):
108108

109109
@property
110110
def content(self):
111-
if os.path.isfile(self.__m_info["path"]):
111+
if self.status["exist"]:
112112
file_temp = open(self.__m_info["path"], "r")
113113
content = file_temp.read()
114114
file_temp.close()
@@ -119,3 +119,17 @@ def content(self):
119119
@property
120120
def info(self):
121121
return self.__m_info
122+
123+
@property
124+
def status(self):
125+
def get_status_file_lock():
126+
return self.__m_is_lock
127+
128+
def get_status_exist():
129+
return os.path.isfile(self.__m_info["path"])
130+
131+
status = Dict()
132+
# Pass function pointers for obtaining dynamic data into object:Dict<status>.
133+
status["file_lock"] = get_status_file_lock
134+
status["exist"] = get_status_exist
135+
return status

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "easierfile"
3-
version = "2.0.0"
3+
version = "2.1.0"
44
description = "An easier-to-use Python package that object-oriented encapsulates Python traditional built-in file operations."
55
license = "MIT"
66
authors = ["leoweyr <[email protected]>"]
@@ -13,7 +13,8 @@ packages = [{include = "easierfile"}]
1313

1414

1515
[tool.poetry.dependencies]
16-
python = "^3.7"
16+
python = "^3.8"
17+
dynoptimdict = "^1.0.0"
1718

1819

1920
[build-system]

0 commit comments

Comments
 (0)