-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 60494af
Showing
6 changed files
with
727 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Set the default behavior, in case people don't have core.autocrlf set. | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Random collection of Python stuff | ||
|
||
#### Some of it might helpful to others. | ||
|
||
# Tools | ||
- [opus.py](https://github.com/DeadSix27/various_python_tools/tree/master/tools/opus_maker) | ||
Tool to encode and share opus audio files. | ||
|
||
# Libraries | ||
- [pathlibex.py](https://github.com/DeadSix27/various_python_tools/tree/master/libs/pathlibex) | ||
Basic extension to the pathlib (mostly used internally) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
# Copyright (C) 2019 <https://github.com/DeadSix27/> | ||
# | ||
# This source code is licensed under a | ||
# Creative Commons Attribution-NonCommercial 4.0 International License. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
# | ||
# You should have received a copy of the license along with this | ||
# work. If not, see <http://creativecommons.org/licenses/by-nc/4.0/>. | ||
|
||
from __future__ import annotations | ||
|
||
import os | ||
import pathlib | ||
import re | ||
import shutil | ||
from typing import AnyStr, List, Optional, Tuple, Union | ||
|
||
import magic | ||
|
||
|
||
class Path(pathlib.Path): | ||
''' | ||
Very crude and simple pathlib extension, | ||
mostly used internally for my needs, so might be buggy and code is unclean. | ||
''' | ||
_flavour = pathlib._windows_flavour if os.name == 'nt' else pathlib._posix_flavour | ||
|
||
def __new__(cls, *args): | ||
return super(Path, cls).__new__(cls, *args) | ||
|
||
def __init__(self, *args): | ||
super().__init__() | ||
self.ssuffix = self.suffix.lstrip(".") | ||
self._some_instance_ppath_value = self.exists() | ||
|
||
def listfiles(self, extensions=()) -> List[Path]: | ||
'''### listfiles | ||
##### listfiles | ||
### Args: | ||
`extensions` (tuple, optional): List of extensions to limit listing to, with dot prefix. Defaults to (). | ||
### Returns: | ||
List[Path]: List of Paths, matching the optionally specificed extension(s) | ||
''' | ||
lst = None | ||
if len(extensions) > 0: | ||
lst = [self.joinpath(x) for x in self._accessor.listdir(self) if self.joinpath(x).is_file() and x.lower().endswith(extensions)] | ||
else: | ||
lst = [self.joinpath(x) for x in self._accessor.listdir(self) if self.joinpath(x).is_file()] | ||
|
||
def convert(text): | ||
return int(text) if text.isdigit() else text | ||
|
||
def alphanum_key(key): | ||
return [convert(c) for c in re.split('([0-9]+)', str(key))] | ||
|
||
lst = sorted(lst, key=alphanum_key) | ||
return lst | ||
|
||
def listall(self, recursive=False) -> List[Path]: | ||
|
||
lst = [] | ||
if all: | ||
for r, dirs, files in os.walk(self): | ||
for f in files: | ||
lst.append(Path(os.path.join(r, f))) | ||
else: | ||
lst = [self.joinpath(x) for x in self._accessor.listdir(self)] | ||
|
||
def convert(text): | ||
return int(text) if text.isdigit() else text | ||
|
||
def alphanum_key(key): | ||
return [convert(c) for c in re.split('([0-9]+)', str(key))] | ||
|
||
lst = sorted(lst, key=alphanum_key) | ||
return lst | ||
|
||
def listdirs(self) -> List[Path]: | ||
'''### listdirs | ||
##### Same as listfiles, except for directories only. | ||
### Returns: | ||
List[Path]: List of Path's | ||
''' | ||
return [self.joinpath(x) for x in self._accessor.listdir(self) if self.joinpath(x).is_dir()] | ||
|
||
def copy(self, destination: Path) -> Path: | ||
'''### copy | ||
##### Copies the Path to the specificed destination. | ||
### Args: | ||
`destination` (Path): Destination to copy to. | ||
### Returns: | ||
Path: Path of the new copy. | ||
''' | ||
shutil.copy(self, destination) | ||
return destination | ||
|
||
@property | ||
def disk_usage(self) -> Path: | ||
return shutil.disk_usage(self) | ||
|
||
def change_suffix(self, newSuffix: str) -> Path: | ||
'''### change_name | ||
##### Changes the name, including suffix | ||
### Args: | ||
`newSuffix` (str): The new suffix | ||
### Returns: | ||
Path: Newly named Path. | ||
''' | ||
return Path(self.parent.joinpath(self.stem + newSuffix)) | ||
|
||
def change_name(self, name: str) -> Path: | ||
'''### change_name | ||
##### Changes the name, including suffix | ||
### Args: | ||
`name` (str): The new name | ||
### Returns: | ||
Path: Newly named Path. | ||
''' | ||
return self.parent.joinpath(name) | ||
|
||
def change_stem(self, new_stem: str) -> Path: | ||
'''### append_stem | ||
##### Changes the name, ignoring the suffix. | ||
### Args: | ||
`append_str` (str): String to append. | ||
### Returns: | ||
Path: Newly named Path. | ||
''' | ||
return self.parent.joinpath(new_stem + self.suffix) | ||
|
||
'''### append_stem | ||
##### Appends a string to the name, ignoring the suffix. | ||
### Args: | ||
`append_str` (str): String to append. | ||
### Returns: | ||
Path: Newly named Path. | ||
''' | ||
|
||
def append_stem(self, append_str: str) -> Path: | ||
'''[summary] | ||
Arguments: | ||
append_str {str} -- [description] | ||
Returns: | ||
Path -- [description] | ||
''' | ||
return self.parent.joinpath(self.stem + append_str + self.suffix) | ||
|
||
def append_name(self, append_str: str): | ||
'''### append_name | ||
##### Appends a string to the name, including the suffix. | ||
### Args: | ||
`append_str` (str): String to append. | ||
### Returns: | ||
Path: Newly named Path. | ||
''' | ||
return Path(self.parent.joinpath(self.name + append_str)) | ||
|
||
def rmtree(self) -> None: | ||
shutil.rmtree(self) | ||
|
||
def size(self) -> int: | ||
return self.stat().st_size | ||
|
||
def createDate(self): | ||
return self.stat().st_ctime | ||
|
||
def modifyDate(self): | ||
return self.stat().st_mtime | ||
|
||
def move(self, destination: Path) -> Path: | ||
'''### move | ||
##### Moves the Path to a newly specified Location. | ||
### Args: | ||
`destination` (Path): The destination to move the file to. | ||
### Returns: | ||
Path: The new location of the old Path | ||
''' | ||
shutil.move(self, destination) | ||
return destination | ||
|
||
@property | ||
def mime(self) -> str: | ||
ext = self.suffix.lstrip(".").lower() | ||
custom_types = { | ||
'ttf': 'font/ttf', | ||
'otf': 'font/otf', | ||
} | ||
if ext in custom_types: | ||
return custom_types[ext] | ||
|
||
mime = magic.Magic(mime=True) | ||
mime = mime.from_file(str(self)) | ||
return mime | ||
|
||
def fnmatch(self, match: str) -> bool: | ||
cPath = self.parent | ||
for p in cPath.listall(): | ||
if re.search(re.escape(match).replace("\\*", ".*"), p.name): | ||
return True | ||
|
||
return False | ||
|
||
def joinpath(self, *other): | ||
return Path(super().joinpath(*other)) | ||
|
||
@property | ||
def parent(self): | ||
"""The logical parent of the path.""" | ||
drv = self._drv | ||
root = self._root | ||
parts = self._parts | ||
if len(parts) == 1 and (drv or root): | ||
return self | ||
return self._from_parsed_parts(drv, root, parts[:-1]) |
Empty file.
Oops, something went wrong.