-
Notifications
You must be signed in to change notification settings - Fork 6
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 1da383b
Showing
9 changed files
with
1,002 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,34 @@ | ||
## 1.0 | ||
- Initial release. | ||
|
||
## 1.1 | ||
- Modified code structure. | ||
|
||
## 1.2 | ||
- Bug fixes. | ||
- Code refraction. | ||
|
||
## 1.3 | ||
- Renamed: | ||
- Classes: | ||
- `FileDate` ⟶ `File` | ||
- Functions | ||
- `Utils.release` ⟶ `Utils.drop` | ||
- Reduced code size. | ||
|
||
## 1.4 | ||
- Bug fixes. | ||
- `Utils.copy` | ||
|
||
## 1.5 | ||
- Bug fixes. | ||
|
||
## 1.6 | ||
- Bug fixes. | ||
- `Utils.swap` | ||
|
||
## 1.7 | ||
- Bug fixes. | ||
|
||
## 1.8 | ||
- `Utils.fromfile` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,80 @@ | ||
import re | ||
from filedate import File | ||
from dateutil.parser import parse | ||
|
||
class Keep: | ||
"""Utility for "holding" and "dropping" file dates.""" | ||
def __init__(self, Files: list): | ||
self.files = list(Files) | ||
|
||
def pick(self) -> dict: | ||
"""Pick the files dates.""" | ||
self.__dates = {} | ||
for Path in self.files: | ||
self.__dates.update({Path: File(Path).get()}) | ||
|
||
def drop(self): | ||
"""Drop the files dates.""" | ||
for Key, Value in self.__dates.items(): | ||
File(Key).set( | ||
created = Value["created"], | ||
modified = Value["modified"], | ||
accessed = Value["accessed"] | ||
) | ||
|
||
hold = pick | ||
release = drop | ||
|
||
#---# | ||
|
||
def copy(input: str, output: str): | ||
"""Copy one file date to another.""" | ||
input = File(input).get() | ||
|
||
File(output).set( | ||
created = input["created"], | ||
modified = input["modified"], | ||
accessed = input["accessed"] | ||
) | ||
|
||
def swap(input: str, output: str): | ||
"""Swaps file dates to another.""" | ||
output = File(output).get() | ||
|
||
File(input).set( | ||
created = output["created"], | ||
modified = output["modified"], | ||
accessed = output["accessed"] | ||
) | ||
|
||
def name(input: str, type: int = "created"): | ||
"""Sets date(s) of a file based on its name. | ||
Types (accordingly): | ||
1 / "created" | ||
2 / "modified" | ||
3 / "accessed" | ||
0 / "all\"""" | ||
input__ = ".".join(input.split(".")[:-1]) | ||
input__ = re.sub(r"\D", " ", input__).strip() | ||
try: | ||
date = parse(input__).timestamp() | ||
except ValueError: | ||
raise ValueError("Parsed string does not contain a date") | ||
|
||
#---# | ||
|
||
File_ = File(input) | ||
input_ = File_.get() | ||
type = str(type).lower() | ||
|
||
File_.set( | ||
created = date if type in ("0", "all", "1", "created") else input_["created"], | ||
modified = date if type in ("0", "all", "2", "modified") else input_["modified"], | ||
accessed = date if type in ("0", "all", "3", "accessed") else input_["accessed"] | ||
) | ||
|
||
#---# | ||
|
||
move = transfer = copy | ||
fromfile = fromname = fromfn = name |
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 @@ | ||
"""Simple, convenient and cross-platform file date changing library.""" | ||
|
||
from .__main__ import * | ||
from .Utils import * | ||
|
||
#---# | ||
|
||
__author__ = "kubinka0505" | ||
__credits__ = __author__ | ||
__version__ = "1.8" | ||
__date__ = "19.03.2022" |
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,85 @@ | ||
import os | ||
from datetime import datetime | ||
from dateutil.parser import parse | ||
|
||
#---# | ||
|
||
if os.sys.platform.startswith("win"): | ||
from ctypes import windll, wintypes, byref | ||
|
||
#-----# | ||
|
||
class File: | ||
def __init__(self, File: str): | ||
self.file = os.path.abspath(os.path.expanduser(os.path.expandvars(File))) | ||
|
||
def _modify(parameter: str): | ||
"""Convert `filedate.File.set` parameter to Epoch time.""" | ||
if isinstance(parameter, str): | ||
parameter = parse(parameter).timestamp() | ||
try: | ||
parameter = parameter // 1 | ||
except TypeError: | ||
parameter = parameter.timestamp() | ||
return parameter | ||
|
||
|
||
def get(self) -> dict: | ||
"""Returns a dictionary containing `datetime.fromtimestamp` | ||
objects - when file was created, modified and accessed.""" | ||
info = os.stat(self.file) | ||
dict = { | ||
"created": info.st_ctime, | ||
"modified": info.st_mtime, | ||
"accessed": info.st_atime | ||
} | ||
|
||
for Key, Value in dict.items(): | ||
dict[Key] = datetime.fromtimestamp(Value) | ||
|
||
return dict | ||
|
||
#-----# | ||
|
||
def set(self, modified: str, created: str = None, accessed: str = None): | ||
"""Sets new file dates. | ||
All parameters except `self.File` support: | ||
- String datetime representations parsable by `dateutil.parser.parse` | ||
- Epoch times | ||
`created` parameter is Windows only.""" | ||
|
||
Dates = File(self.file).get() | ||
os.chmod(self.file, 511) | ||
|
||
#---# | ||
|
||
created = (File._modify(created) if created else Dates["created"].timestamp()) // 1 | ||
modified = (File._modify(modified) if modified else Dates["modified"].timestamp()) // 1 | ||
accessed = (File._modify(accessed) if accessed else Dates["accessed"].timestamp()) // 1 | ||
|
||
#---# | ||
|
||
if created: | ||
if os.sys.platform.startswith("win"): | ||
timestamp = int((created * 1E7) + 116444736E9) | ||
ctime = wintypes.FILETIME(timestamp & 0xFFFFFFFF, timestamp >> 32) | ||
handle = windll.kernel32.CreateFileW(self.file, 256, 0, None, 3, 128, None) | ||
|
||
# Setting Creation Time | ||
windll.kernel32.SetFileTime(handle, byref(ctime), None, None) | ||
windll.kernel32.CloseHandle(handle) | ||
|
||
#---# | ||
|
||
# Setting Accessed & Modified Time | ||
os.utime(self.file, (accessed, modified)) | ||
|
||
#---# | ||
|
||
return None if File.SET_SILENT else File(self.file).get() | ||
|
||
#-----# | ||
|
||
SET_SILENT = False |
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,24 @@ | ||
from setuptools import * | ||
|
||
setup( | ||
name = "filedate", | ||
description = open("ReadMe.rst").readline().rstrip(), | ||
version = "1.8", | ||
author = "kubinka0505", | ||
license = "GPLv3", | ||
keywords = "filedate file date change changing changer", | ||
url = "https://github.com/kubinka0505/filedate", | ||
classifiers = [ | ||
"Development Status :: 6 - Mature", | ||
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)", | ||
"Programming Language :: Python :: 3 :: Only", | ||
"Operating System :: OS Independent", | ||
"Environment :: Console", | ||
"Intended Audience :: End Users/Desktop", | ||
"Topic :: Desktop Environment :: File Managers", | ||
"Natural Language :: English" | ||
], | ||
python_requires = ">=3.3", | ||
install_requires = "python-dateutil", | ||
packages = find_packages() | ||
) |
Oops, something went wrong.