-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmkdird
More file actions
executable file
·112 lines (95 loc) · 3.68 KB
/
mkdird
File metadata and controls
executable file
·112 lines (95 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2024 Jared Daniel Carbonell Recomendable.
from datetime import date
from os import listdir, mkdir
from sys import argv
dirs = sorted(listdir())
def string_in_dir_names(dir_name: str):
"""Check if input dir_name is a subset of the names of one of the
directories in the current working directory.
Used to determine if directory yyyymmdd_xx is taken, where yyyymmdd
is the date and xx is the xx'th directory in that date.
"""
for directory in dirs:
if dir_name in directory:
dirs.pop(0)
return True
return False
def gen_date_string(no_of_days: int = 0):
"""Return a date string in the form of YYYYMMDD.
Input no_of_days defaults to zero, meaning return current date.
If no_of_days > 0, return date after no_of_days has passed from today.
If no_of_days < 0, return date before |no_of_days| has passed from today.
"""
date_int = date.today().toordinal() + no_of_days
date_tup = date.fromordinal(date_int).strftime("%Y%m%d")
return date_tup
def gen_dir_name(no_of_days: int = 0, dir_name_string: str = ""):
"""Return the string for the name of the directory to generate.
See gen_date_string() for info on input no_of_days.
Input dir_name_string is the name of the directory.
"""
dir_string = gen_date_string(no_of_days)
for i in range(1, 99):
append = str(i)
if len(append) < 2:
append = "0" + append
proposed_name = "{}_{}".format(dir_string, append)
if not string_in_dir_names(proposed_name):
if dir_name_string == "":
return proposed_name
return "{} '{}'".format(proposed_name, dir_name_string)
if dir_name_string == "":
return "{}_99".format(dir_string)
return "{}_99 '{}'".format(dir_string, dir_name_string)
def make_directory(no_of_days: int = 0, dir_name_string: str = ""):
"""Actually perform mkdir.
See gen_dir_name() for input parameter details.
"""
try:
dir_name = gen_dir_name(no_of_days, dir_name_string)
mkdir(dir_name)
show_dir_successfully_made(dir_name)
except FileExistsError:
show_dir_limit_exceeded()
exit(1)
def show_dir_limit_exceeded():
print("ERROR: Limit reached for number of directories created today.")
def show_dir_successfully_made(dir_name):
print("Directory {} successfully created.".format(dir_name))
def show_help():
"""Show the help documentation for the program."""
help_doc = """Usage: mkdird [options] [directory name]
Options:
-h, --help show this help message and exit
-p [value] date of directory is <value> days before current date
when left unspecified, <value> defaults to 1
-n [value] date of directory is <value> days after current date
when left unspecified, <value> defaults to 1"""
print(help_doc)
if __name__ == "__main__":
if len(argv) == 1:
make_directory()
else:
if argv[1] in ("-h", "--help"):
show_help()
exit(0)
no_of_days = 0
start_i = 1
if argv[1] in ("-p", "-n"):
start_i += 2
if len(argv) > 2 and argv[2].isnumeric():
no_of_days = int(argv[2])
else:
no_of_days = 1
start_i -= 1
if argv[1] == "-p":
no_of_days *= -1
dir_name = ""
for i in range(start_i, len(argv)):
if i == len(argv) - 1:
dir_name += str(argv[i])
break
dir_name += "{} ".format(argv[i])
make_directory(no_of_days, dir_name)