-
Notifications
You must be signed in to change notification settings - Fork 1
/
sunset.py
executable file
·148 lines (129 loc) · 5.27 KB
/
sunset.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python
import logging
import os, sys
import argparse
import shutil
from datetime import datetime
from calc import Suncalc, EVENTS
from glob import glob
LOG_FORMAT="%(message)s"
logger = logging.getLogger(__name__)
def init():
"""
Create settings module if it does not exist.
"""
path = os.path.join(sys.path[0], 'settings.py')
if os.path.isfile(path) == False:
template = os.path.join(sys.path[0], 'sample_settings.py')
shutil.copyfile(template, path)
print("Created settings.py module from sample.")
def run_commands(args):
"""
Output or execute commands as specified in settings.
"""
init()
from settings import LATITUDE, LONGITUDE
from settings import commands, EVENT
s = Suncalc(LATITUDE, LONGITUDE, EVENT)
local_dt = datetime.now()
value = s.local_value(local_dt)
for c in commands(value):
if args.execute:
os.system(c)
else:
print(c)
def show_time(args):
"""
Output fime for selected event.
"""
init()
from settings import LATITUDE, LONGITUDE
s = Suncalc(LATITUDE, LONGITUDE, args.event)
local_dt = datetime.now()
value = s.local_value(local_dt)
print("Local {} is at {}".format(args.event, value))
def collect_images(args):
"""
Collect images with same index number to target directory.
Order collected files by date created and create symbolic link
for each file using numeric counter as filename.
"""
from settings import TARGET_DIRECTORY as source_dir # sic!
target_dir = args.target if os.path.isabs(args.target) else os.path.join(source_dir, args.target)
if os.path.isdir(source_dir):
if os.path.isdir(target_dir) == False:
os.makedirs(target_dir)
if args.purge:
# Remove old files in target directory
g = glob(os.path.join(target_dir, "*"))
count = len(g)
if count > 0:
if args.silent == False:
print("All files in target directory {} will be deleted.".format(
target_dir))
print("Do you want to continue? (y/n)")
if input().lower() != 'y':
print("Operation cancelled.")
return
for file in g:
os.remove(file)
pattern = os.path.join(source_dir, args.subdir, "*{:+d}.jpg".format(args.offset))
files = sorted(glob(pattern), key=os.path.getmtime)
count = len(files)
perform_operation = shutil.copyfile if args.copy else os.symlink
if count > 0:
for index, path in enumerate(files):
target_path = os.path.join(target_dir, "{:d}.jpg".format(index))
print(target_path)
perform_operation(path, target_path)
if args.copy:
print("Successfully copied {} files to {}.".format(count, target_dir))
else:
print("Successfully created {} symlinks to {}.".format(count, target_dir))
else:
print("No files found to be processed.")
else:
print("Source directory defined in settings does not exist.".format(source_dir))
def main():
parser = argparse.ArgumentParser("Sunset Calculator")
subparsers = parser.add_subparsers(help="Action to perfom.")
# Define parser for show-time action
subparser = subparsers.add_parser("show-time",
help="Show time for specific event.")
subparser.add_argument('--event', required=True, type=str, choices=EVENTS,
help="Defines event to be observed.")
subparser.set_defaults(func=show_time)
# Define parser for run-commands action
subparser = subparsers.add_parser("run-commands",
help="Run commands specified in settings.")
subparser.set_defaults(func=run_commands)
subparser.add_argument('--execute', action='store_true',
default=False, help="If set to True, execute commands using os.system.")
# Define parser for collect-images action
subparser = subparsers.add_parser("collect-images",
help="Collect images for creating time-lapse video.")
subparser.set_defaults(func=collect_images)
subparser.add_argument('--offset', type=int, required=True,
help="Index of images to be collected.")
subparser.add_argument('--target', type=str, required=True,
help="Target path for images.")
subparser.add_argument('--subdir', type=str, required=False, default='',
help="Sub-directory to look for images.")
subparser.add_argument('--purge', action='store_true',
help="If set to True, remove old files before proceeding.")
subparser.add_argument('--silent', default=False, action='store_true',
help="If set to True, do not ask for confirmation.")
subparser.add_argument('--copy', default=False, action='store_true',
help="If set to True, copy files instead of creating symlink.")
args = parser.parse_args()
if hasattr(args, 'func') == False:
parser.print_help()
return
try:
args.func(args)
except:
e = sys.exc_info()
logger.error("Operation failed: {0}".format(e[1]))
if __name__=='__main__':
logging.basicConfig(format=LOG_FORMAT, level=logging.DEBUG)
main()