Add date-organize subcommand
				
					
				
			This commit is contained in:
		
							parent
							
								
									c3914738d5
								
							
						
					
					
						commit
						e2963403fe
					
				
					 5 changed files with 103 additions and 0 deletions
				
			
		
							
								
								
									
										8
									
								
								docs/api/wuttjamaican/cmd.date_organize.rst
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								docs/api/wuttjamaican/cmd.date_organize.rst
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,8 @@ | |||
| 
 | ||||
| ``wuttjamaican.cmd.date_organize`` | ||||
| ================================== | ||||
| 
 | ||||
| .. automodule:: wuttjamaican.cmd.date_organize | ||||
|    :members: | ||||
| 
 | ||||
| .. program-output:: wutta date-organize -h | ||||
|  | @ -10,6 +10,7 @@ | |||
|    app | ||||
|    cmd | ||||
|    cmd.base | ||||
|    cmd.date_organize | ||||
|    cmd.make_appdir | ||||
|    cmd.setup | ||||
|    conf | ||||
|  |  | |||
|  | @ -51,5 +51,6 @@ console_scripts = | |||
|         wutta = wuttjamaican.cmd.base:main | ||||
| 
 | ||||
| wutta.subcommands = | ||||
|         date-organize = wuttjamaican.cmd.date_organize:DateOrganize | ||||
|         make-appdir = wuttjamaican.cmd.make_appdir:MakeAppDir | ||||
|         setup = wuttjamaican.cmd.setup:Setup | ||||
|  |  | |||
							
								
								
									
										59
									
								
								src/wuttjamaican/cmd/date_organize.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								src/wuttjamaican/cmd/date_organize.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,59 @@ | |||
| # -*- coding: utf-8; -*- | ||||
| ################################################################################ | ||||
| # | ||||
| #  WuttJamaican -- Base package for Wutta Framework | ||||
| #  Copyright © 2023 Lance Edgar | ||||
| # | ||||
| #  This file is part of Wutta Framework. | ||||
| # | ||||
| #  Wutta Framework is free software: you can redistribute it and/or modify it | ||||
| #  under the terms of the GNU General Public License as published by the Free | ||||
| #  Software Foundation, either version 3 of the License, or (at your option) any | ||||
| #  later version. | ||||
| # | ||||
| #  Wutta Framework 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.  See the GNU General Public License for | ||||
| #  more details. | ||||
| # | ||||
| #  You should have received a copy of the GNU General Public License along with | ||||
| #  Wutta Framework.  If not, see <http://www.gnu.org/licenses/>. | ||||
| # | ||||
| ################################################################################ | ||||
| """ | ||||
| WuttJamaican - subcommand ``date-organize`` | ||||
| """ | ||||
| 
 | ||||
| import os | ||||
| import shutil | ||||
| import datetime | ||||
| 
 | ||||
| from .base import Subcommand | ||||
| 
 | ||||
| 
 | ||||
| class DateOrganize(Subcommand): | ||||
|     """ | ||||
|     Organize files in a given directory, according to date | ||||
|     """ | ||||
|     name = 'date-organize' | ||||
|     description = __doc__.strip() | ||||
| 
 | ||||
|     def add_args(self): | ||||
|         """ """ | ||||
|         self.parser.add_argument('folder', metavar='PATH', | ||||
|                                  help="Path to directory containing files which are " | ||||
|                                  "to be organized by date.") | ||||
| 
 | ||||
|     def run(self, args): | ||||
|         """ """ | ||||
|         today = datetime.date.today() | ||||
|         for filename in sorted(os.listdir(args.folder)): | ||||
|             path = os.path.join(args.folder, filename) | ||||
|             if os.path.isfile(path): | ||||
|                 mtime = datetime.datetime.fromtimestamp(os.path.getmtime(path)) | ||||
|                 if mtime.date() < today: | ||||
|                     datedir = mtime.strftime(os.sep.join(('%Y', '%m', '%d'))) | ||||
|                     datedir = os.path.join(args.folder, datedir) | ||||
|                     if not os.path.exists(datedir): | ||||
|                         os.makedirs(datedir) | ||||
|                     shutil.move(path, datedir) | ||||
							
								
								
									
										34
									
								
								tests/cmd/test_date_organize.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								tests/cmd/test_date_organize.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,34 @@ | |||
| # -*- coding: utf-8; -*- | ||||
| 
 | ||||
| import datetime | ||||
| import os | ||||
| 
 | ||||
| from wuttjamaican.conf import WuttaConfig | ||||
| from wuttjamaican.cmd import Command, date_organize | ||||
| from wuttjamaican.testing import FileConfigTestCase | ||||
| 
 | ||||
| 
 | ||||
| class TestDateOrganize(FileConfigTestCase): | ||||
| 
 | ||||
|     def test_run(self): | ||||
|         dates = [ | ||||
|             datetime.date(2023, 11, 21), | ||||
|             datetime.date(2023, 11, 20), | ||||
|             datetime.date(2023, 10, 15), | ||||
|             datetime.date(2023, 9, 10), | ||||
|         ] | ||||
| 
 | ||||
|         for date in dates: | ||||
|             dt = datetime.datetime.combine(date, datetime.time(0)) | ||||
|             filename = date.strftime('%Y%m%d.txt') | ||||
|             path = self.write_file(filename, '') | ||||
|             os.utime(path, (dt.timestamp(), dt.timestamp())) | ||||
| 
 | ||||
|         cmd = Command(subcommands={ | ||||
|             'date-organize': date_organize.DateOrganize, | ||||
|         }) | ||||
|         cmd.run('date-organize', self.tempdir) | ||||
| 
 | ||||
|         self.assertEqual(os.listdir(self.tempdir), ['2023']) | ||||
|         self.assertEqual(sorted(os.listdir(os.path.join(self.tempdir, '2023'))), | ||||
|                          ['09', '10', '11']) | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Lance Edgar
						Lance Edgar