A chronological folder backup script for those who want flexibility.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.4 KiB

import os
import datetime
class Chronologics:
'''
@about:
Handles creation of chronological folders
@params:
destination: string (from class Chronos)
'''
def __init__(self, destination):
self.destination = destination
def _get_date(self):
# Returns tuple of numeric values of now()
time = datetime.datetime.now()
year = time.year
month = time.month
day = time.day
hour = time.hour
minute = time.minute
second = time.second
return (year, month, day, hour, minute, second)
def _create_paths(self, current_time):
# Returns a tuple of the concatenated paths in
# chronological order. It looks like:
# ('year', 'year/month', 'year/month/day')
return (
current_time[0],
f'{current_time[0]}/{current_time[1]}',
f'{current_time[0]}/{current_time[1]}/{current_time[2]}'
)
@property
def paths(self):
return self._create_paths(self._get_date())
def _create_folder(self, name):
try:
os.mkdir(os.path.join(self.destination, str(name)))
except FileExistsError:
pass
def _create_all_folders(self):
for time in self.paths:
self._create_folder(time)
def _create_chronological_folders(self):
current_time = self._get_date()
self._create_all_folders()
return f'{self.destination}/{self.paths[2]}' # returns the final destination
def init(self):
path = self._create_chronological_folders()
return path
def __call__(self):
return self.init()