Skip to content

Commit

Permalink
Release 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
kubinka0505 committed Dec 5, 2021
0 parents commit 2c998ed
Show file tree
Hide file tree
Showing 7 changed files with 896 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Documents/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 1.0
Initial release.

## 1.1
Modified code structure.
19 changes: 19 additions & 0 deletions Files/filedate/Utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from filedate import FileDate

class Keep:
"""Utility for "holding" and "releasing" file dates."""
def __init__(self, Files: list):
self.files = Files

def hold(self) -> dict:
"""Hold the file date."""
self.__dates = {File: FileDate(File).get() for File in self.files}

def release(self):
"""Release the file date."""
for Key, Value in self.__dates.items():
FileDate(Key).set(
created = Value["created"],
modified = Value["modified"],
accessed = Value["accessed"]
)
12 changes: 12 additions & 0 deletions Files/filedate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Simple, convenient and cross-platform file date changing library"""

import os
from .__main__ import *
from .Utils import *

#---#

__author__ = "kubinka0505"
__credits__ = __author__
__version__ = "1.1"
__date__ = "05.12.2020"
93 changes: 93 additions & 0 deletions Files/filedate/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import os
from platform import system
from datetime import datetime
from dateutil.parser import parse

#---#

class FileDate:
def __init__(self, File: str):
self.file = os.path.abspath(os.path.expanduser(os.path.expandvars(File)))

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 `File` support:
- String datetime representations parsable by `dateutil.parser.parse`
- Epoch times
"created" is Windows only."""
Dates = FileDate(self.file).get()
os.chmod(self.file, 511)

#---#

if modified:
if isinstance(modified, str):
modified = parse(modified).timestamp()
try:
modified = modified // 1
except:
modified = modified.timestamp()
modified = modified // 1

if accessed:
if isinstance(accessed, str):
accessed = parse(accessed).timestamp()
try:
accessed = accessed // 1
except:
accessed = accessed.timestamp()
accessed = accessed // 1

#---#

if system() == "Windows":
if created:
from ctypes import windll, wintypes, byref

#---#

if isinstance(created, str):
created = parse(created).timestamp()
try:
created = created // 1
except:
created = created.timestamp()
created = created // 1

#---#

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 FileDate.SET_SILENT else FileDate(self.file).get()

#-----#

SET_SILENT = False
23 changes: 23 additions & 0 deletions Files/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from setuptools import *

setup(
name = "filedate",
description = "Simple, convenient and cross-platform file date changing library.",
version = "1.1",
author = "kubinka0505",
license = "GPL v3",
keywords = "filedate file date change changing changer",
url = "https://github.com/kubinka0505/filedate",
packages = find_packages(),
install_requires = ["python-dateutil"],
classifiers = [
"Development Status :: 5 - Production/Stable",
"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"
],
)
Loading

0 comments on commit 2c998ed

Please sign in to comment.