Skip to content

Commit 7a56932

Browse files
committed
The path update, 1.0.5
1 parent 969aaef commit 7a56932

File tree

4 files changed

+55
-24
lines changed

4 files changed

+55
-24
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# diskspace
2-
Making it possible to use Linux df & du command on Windows
2+
Making it possible to use Linux df & du command on Windows/Linux.
33

44
# Requirements
55
Python 3.6 or up
@@ -29,3 +29,19 @@ optional arguments:
2929
-nf, --nofolders Ingore folders
3030
-ns, --nostats Don't display disk space at top
3131
```
32+
33+
# Usage in code example
34+
```py
35+
>>> import diskspace
36+
>>> show_dir = diskspace.ShowPath("./Desktop")
37+
>>> print(show_dir.all_files)
38+
# [<FileInfo name=Adios.png bytes=267391 human=261.1KiB folder=False created=1571303626.1382277>, <FileInfo name=Baby yoda flip pancakes.jpg bytes=47487 human=46.4KiB folder=False created=1574946741.9886782>, <FileInfo name=big sipp.jpg bytes=47076 human=46.0KiB folder=False created=1574028168.9891481>]
39+
```
40+
41+
### ShowPath()
42+
- path: str = Path to find files (Default: ".")
43+
- exclude: str = Exclude files (Default: None)
44+
- include_folder = Include folders in result (Default: True)
45+
- human: bool = Show disk space in human-readable value (Default: False)
46+
- include_stats: bool = Include stats of current disk in ShowPath.pretty_print (Default: True)
47+
- whitelist: str = Only include X files (Default: None)

diskspace/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = "1.0.4.1"
1+
__version__ = "1.0.5"
22

33
from .pathRender import ShowPath

diskspace/diskspace.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import shlex
22
import diskspace
33
import argparse
4+
import re
45

56
from . import argpar, utils, pathRender
67

@@ -10,39 +11,43 @@ def shell():
1011
parser = argpar.Arguments(description="Making it possible to use Linux df & du command on Windows", add_help=False)
1112
parser.add_argument('--help', action='help', default=argparse.SUPPRESS, help='Show this help message and exit.')
1213
parser.add_argument('-v', '--version', action='store_true', help='Show the version number and exit')
13-
parser.add_argument('-e', '--exclude', nargs='+', help='Exclude name or file extensions matching arguments', default=None)
14-
parser.add_argument('-o', '--only', nargs='+', help='Only include name or file extensions matching arguments', default=None)
14+
parser.add_argument('-bl', '--blacklist', nargs='+', help='Exclude name or file extensions matching arguments', default=None)
15+
parser.add_argument('-wl', '--whitelist', nargs='+', help='Only include name or file extensions matching arguments', default=None)
1516
parser.add_argument('-h', '--human', action='store_true', help='Convert bytes in to readable format')
1617
parser.add_argument('-nf', '--nofolders', action='store_true', help='Ingore folders')
18+
parser.add_argument('-p', '--path', nargs='+', help="Choose a different path to check diskspace")
1719
parser.add_argument('-ns', '--nostats', action='store_true', help="Don't display disk space at top")
1820

1921
try:
22+
arguments = arguments.replace("\\", "/")
2023
args = parser.parse_args(shlex.split(arguments))
2124
except Exception as e:
2225
utils.exitcode(e)
2326

2427
if args.version:
25-
utils.exitcode(diskspace.__version__)
28+
utils.exitcode(f"DiskSpace version: {diskspace.__version__}")
2629

27-
if args.nofolders:
28-
nofolders = False
29-
else:
30-
nofolders = True
30+
nofolders = False if args.nofolders else True
31+
nostats = False if args.nostats else True
3132

32-
if args.nostats:
33-
nostats = False
33+
if args.path:
34+
custom_path = " ".join(args.path)
35+
if not re.compile("^[A-Za-z]:\/|^\/").search(custom_path):
36+
# Custom path from current dir
37+
custom_path = "./" + " ".join(args.path)
3438
else:
35-
nostats = True
39+
custom_path = "."
3640

37-
if args.exclude and args.only:
38-
utils.exitcode("You can't define what to exclude and what to only include at the same time.")
41+
if args.blacklist and args.whitelist:
42+
utils.exitcode("You can't define blacklist/whitelist alone.")
3943

4044
print(pathRender.ShowPath(
45+
path=custom_path,
4146
include_folder=nofolders,
4247
include_stats=nostats,
43-
exclude=args.exclude,
44-
human=args.human,
45-
whitelist=args.only
48+
whitelist=args.whitelist,
49+
blacklist=args.blacklist,
50+
human=args.human
4651
).pretty_print)
4752

4853

diskspace/pathRender.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self, total: int, used: int, free: int,
1717
def __repr__(self):
1818
return '<OSInfo total={0.total} used={0.used} free={0.free}' \
1919
' total_human={0.total_human} used_human={0.used_human}' \
20-
' free_human={0.free_human}> used_percent={0.used_percent}'.format(self)
20+
' free_human={0.free_human}> used_percent={0.used_percent}>'.format(self)
2121

2222

2323
class FileInfo:
@@ -30,19 +30,20 @@ def __init__(self, name: str, bytes: int, human: str, folder: False, created: fl
3030

3131
def __repr__(self):
3232
return '<FileInfo name={0.name} bytes={0.bytes}' \
33-
' human={0.human} folder={0.folder} created={0.created}'.format(self)
33+
' human={0.human} folder={0.folder} created={0.created}>'.format(self)
3434

3535

3636
class ShowPath:
37-
def __init__(self, path='.', exclude=None, include_folder=True,
38-
human=False, include_stats=True, whitelist=False):
37+
def __init__(self, path: str = '.', blacklist: str = None, include_folder: bool = True,
38+
human: bool = False, include_stats: bool = True, whitelist: str = None):
3939
self.files = {}
4040
self.path = path
41-
self.exclude = exclude
41+
self.blacklist = blacklist
4242
self.human = human
4343
self.whitelist = whitelist
4444
self.include_folder = include_folder
4545
self.include_stats = include_stats
46+
self.path_size = 0
4647

4748
def readable(self, num, suffix='B'):
4849
""" Convert Bytes into human readable formats """
@@ -94,6 +95,8 @@ def getPathFiles(self):
9495
and converts them to class FileInfo """
9596
for i, file in enumerate(os.listdir(self.path)):
9697
is_folder = False
98+
filename = file
99+
file = f"{self.path}/{file}"
97100

98101
try:
99102
size = os.path.getsize(file)
@@ -113,7 +116,7 @@ def getPathFiles(self):
113116
continue
114117

115118
try:
116-
if any([x.lower() in file.lower() for x in self.exclude]):
119+
if any([x.lower() in file.lower() for x in self.blacklist]):
117120
continue
118121
except TypeError:
119122
pass
@@ -126,11 +129,18 @@ def getPathFiles(self):
126129
except TypeError:
127130
pass
128131

132+
self.path_size += size
129133
self.files[i] = FileInfo(
130-
file, size, self.readable(size),
134+
filename, size, self.readable(size),
131135
is_folder, self.creation_date(file)
132136
)
133137

138+
# Now add the current dir as a last touch
139+
self.files[len(self.files) + 1] = FileInfo(
140+
self.path, self.path_size, self.readable(self.path_size),
141+
True, self.creation_date(self.path)
142+
)
143+
134144
return self.files
135145

136146
@property

0 commit comments

Comments
 (0)