Coverage for .tox/coverage/lib/python3.11/site-packages/wuttjamaican/cmd/date_organize.py: 100%
22 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-22 21:42 -0600
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-22 21:42 -0600
1# -*- coding: utf-8; -*-
2################################################################################
3#
4# WuttJamaican -- Base package for Wutta Framework
5# Copyright © 2023 Lance Edgar
6#
7# This file is part of Wutta Framework.
8#
9# Wutta Framework is free software: you can redistribute it and/or modify it
10# under the terms of the GNU General Public License as published by the Free
11# Software Foundation, either version 3 of the License, or (at your option) any
12# later version.
13#
14# Wutta Framework is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17# more details.
18#
19# You should have received a copy of the GNU General Public License along with
20# Wutta Framework. If not, see <http://www.gnu.org/licenses/>.
21#
22################################################################################
23"""
24WuttJamaican - subcommand ``date-organize``
25"""
27import os
28import shutil
29import datetime
31from .base import Subcommand
34class DateOrganize(Subcommand):
35 """
36 Organize files into subfolders according to date
37 """
38 name = 'date-organize'
39 description = __doc__.strip()
41 def add_args(self):
42 """ """
43 self.parser.add_argument('folder', metavar='PATH',
44 help="Path to directory containing files which are "
45 "to be organized by date.")
47 def run(self, args):
48 """ """
49 today = datetime.date.today()
50 for filename in sorted(os.listdir(args.folder)):
51 path = os.path.join(args.folder, filename)
52 if os.path.isfile(path):
53 mtime = datetime.datetime.fromtimestamp(os.path.getmtime(path))
54 if mtime.date() < today:
55 datedir = mtime.strftime(os.sep.join(('%Y', '%m', '%d')))
56 datedir = os.path.join(args.folder, datedir)
57 if not os.path.exists(datedir):
58 os.makedirs(datedir)
59 shutil.move(path, datedir)