-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
353 lines (300 loc) · 11 KB
/
main.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from PIL import Image
import datetime as dt
import os
from math import ceil
from git import rmtree
import tkinter as tk
from tkinter.filedialog import askopenfilename, askdirectory
from random import choice
SIZE = (51, 7)
colors_amount = 4
activity_colors = ((255, 255, 255), (155, 233, 168), (64, 196, 99), (48, 161, 78), (33, 110, 57))
commits = list()
def modal_image_select():
input('To select an image, press enter')
root = tk.Tk()
root.attributes('-alpha', 0.0)
root.attributes('-topmost', True)
path_to_image = tk.filedialog.askopenfilename(parent=root, title='Select an image')
root.destroy()
if not path_to_image:
exit()
try:
Image.open(path_to_image)
except:
print('!!! Cannot convert this type of files to the activity !!!')
print()
return modal_image_select()
else:
return path_to_image
def modal_directory_select():
input('To select a directory, press enter ')
root = tk.Tk()
root.attributes('-alpha', 0.0)
root.attributes('-topmost', True)
directory = tk.filedialog.askdirectory(parent=root, title='Select a directory')
root.destroy()
if not directory:
exit()
return directory
def date_select(beginning=True):
if beginning:
try:
message = 'Please specify the beginning date in YYYY-MM-DD format \n'
year, month, day = input(message).split('-')
date = dt.datetime(day=int(day), month=int(month), year=int(year), hour=12)
except:
print('!!! Invalid date !!!')
print()
return date_select(beginning=True)
else:
return date
else:
try:
message = 'Please specify the ending date in YYYY-MM-DD format \n'
year, month, day = input(message).split('-')
date = dt.datetime(day=int(day), month=int(month), year=int(year), hour=12)
except:
print('!!! Invalid date !!!')
print()
return date_select(beginning=False)
else:
return date
def image_conversion(image_path):
global colors_amount, commits
im = Image.open(image_path)
width, height = im.size
if height != 7:
new_height = 7
new_width = new_height * width / height
im = im.resize((int(new_width), new_height), Image.LANCZOS)
width, height = im.size
im = im.convert('L')
pixels = im.load()
for i in range(width):
for j in range(height):
if pixels[i, j] == 255:
colors_amount = 5
break
else:
continue
break
result = im.quantize(colors_amount)
if len(result.getcolors()) == 1 and colors_amount == 5:
print('Cannot transform white image')
exit()
delta = 1 if colors_amount == 4 else 0
max_color = len(result.getcolors()) - 1
pixels = result.load()
for i in range(width):
line = list()
for j in range(height):
line.append(4 if pixels[i, j] == max_color else pixels[i, j] + delta)
commits.append(line)
def print_line():
print('-------------------------------------------------')
def description_of_exclude_days():
print()
print_line()
print('Please, specify the days you don`t want to have any activity')
print_line()
print('--To pass press enter--')
print('Sunday - 0')
print('Monday - 1')
print('Tuesday - 2')
print('Wednesday - 3')
print('Thursday - 4')
print('Friday - 5')
print('Saturday - 6')
print_line()
print('!!!Insert numbers of days of the week in a line separated by a whitespace!!!')
print_line()
print('Example: 0 6')
print_line()
print()
def exclude_days():
if input('To exclude days of the week from your activity, insert "Y"\n(press enter to skip)\n') != 'Y':
return set()
else:
try:
description_of_exclude_days()
excluding_days = set([int(day) for day in input('Exclude days: ').split()])
except:
print('!!! Input is incorrect !!!')
print()
return exclude_days()
else:
return excluding_days
def description_of_settings():
print()
print_line()
print('| For each day the program will randomly choose |\n'
'| a number of commits from the array, |\n'
'| generated by these settings |')
print_line()
print('number*frequency | this means that this "number" of commits\n'
' will appear with some "frequency"')
print('number1-number2 | this means numbers in range from "number1" to "number2"')
print('number | this means that this "number" of commits')
print_line()
print('!!!All settings must be inserted in a line separated by a whitespace!!!')
print_line()
print('Example: 1*2 2-4 7 -- this means 1 commit will appear with frequency 2\n'
' -- numbers from 2 to 4\n'
' -- number 7')
print()
print('This example generates an array [1, 1, 2, 3, 4, 7]')
print('Then for each day will be generated a random number of commits from this array')
print_line()
print()
def set_the_settings_of_random():
if input('To specify the random filling, insert "Y"\n(press enter to skip)\n') != 'Y':
try:
minimal = int(input('Specify the minimal amount of commits \n'))
maximal = int(input('Specify the maximal amount of commits \n'))
random_array = list(range(minimal, maximal + 1))
except:
print('!!! The settings are incorrect !!!')
print()
return set_the_settings_of_random()
else:
return random_array
else:
try:
description_of_settings()
line = input('Please insert the settings: ')
commands = line.split()
random_array = list()
for command in commands:
if command.isnumeric():
random_array.append(int(command))
if '-' in command:
start, end = command.split('-')
random_array += range(int(start), int(end) + 1)
if '*' in command:
number, frequency = command.split('*')
for i in range(int(frequency)):
random_array.append(int(number))
else:
print('!!! The settings are incorrect !!!')
print()
return set_the_settings_of_random()
except:
print('!!! The settings are incorrect !!!')
print()
return set_the_settings_of_random()
else:
return random_array
def random_conversion(beginning, ending, random_array, excluding_days=None):
empty_amount = (beginning.weekday() + 1) % 7
line = list()
for i in range((ending - beginning).days + 1 + empty_amount):
if len(line) == 7:
commits.append(line)
line = list()
if i in range(empty_amount) or ((i % 7) in excluding_days if excluding_days else False):
line.append(0)
else:
line.append(choice(random_array))
if line:
while len(line) < 7:
line.append(0)
commits.append(line)
def ask_git_configs():
if input('To specify commits configs(author, email), insert "Y"\n(press enter to skip)\n') != 'Y':
return {'author': None, 'e_mail': None}
else:
author = input('Specify the commits author (press enter to set by default): ')
e_mail = input('Specify email (press enter to set by default): ')
return {'author': author, 'e_mail': e_mail}
def set_git_config(configs):
if configs['author']:
os.system(f'git config --local user.name "{configs["author"]}"')
if configs['e_mail']:
os.system(f'git config --local user.email {configs["e_mail"]}')
def prepare_to_commits(directory_path, configs=None):
os.chdir(directory_path)
if os.path.isfile('commits_image.png'):
os.remove('commits_image.png')
if os.path.isdir('.git'):
rmtree('.git')
os.system('git init')
if configs:
set_git_config(configs)
result_image_setup()
def make_commits(beginning):
commit_date = beginning
for week in range(len(commits)):
for day in range((beginning.weekday() + 1) % 7 if week == 0 else 0, len(commits[0])):
for commit in range(commits[week][day]):
image = Image.open('commits_image.png')
pixels = image.load()
pixels[week, day] = activity_colors[get_activity_color(commits[week][day])]
image.save('commits_image.png')
os.system('git add commits_image.png')
command = f'git commit -m \"{commit_date.date()} {commit + 1}\" --no-edit --date=\"{commit_date}\" --allow-empty'
os.system(command)
commit_date += dt.timedelta(days=1)
print('Commits were generated. You can push them to your empty GitHub repository')
def calculate_ratio():
amounts = list()
for week in commits:
for day in week:
amounts.append(day)
return max(amounts) / 4.5
def get_activity_color(days_commits):
ratio = calculate_ratio()
if not ratio:
return 0
current_level = ceil(days_commits / ratio)
if round(ratio * 4.5) == 2:
return 4
if current_level >= 4:
return 4
if not current_level and days_commits > 0:
return 1
return current_level
def result_image_setup():
length = len(commits)
commits_image = Image.new("RGB", (length, 7), activity_colors[0])
commits_image.save('commits_image.png')
def menu():
while True:
print('\n' * 2)
print('Please select the action')
print('1. Convert an image to your GitHub heatmap activity')
print('2. Fill you GitHub heatmap activity randomly')
index = input('Your choice: ')
if index in ['1', '2']:
return int(index)
else:
print('Choice is incorrect')
def main():
index = menu()
if index == 1:
path_to_image = modal_image_select()
directory = modal_directory_select()
beginning = date_select()
image_conversion(path_to_image)
elif index == 2:
directory = modal_directory_select()
beginning = date_select()
ending = date_select(beginning=False)
if beginning == ending:
print('!!! Dates are the same !!!')
exit()
if ending < beginning:
print(' !!! Dates are incorrect !!!')
print('!!! The end date is before the start date !!!')
exit()
print()
excluding_days = exclude_days()
print()
random_array = set_the_settings_of_random()
random_conversion(beginning, ending, random_array, excluding_days=excluding_days)
configs = ask_git_configs()
print()
prepare_to_commits(directory, configs=configs)
make_commits(beginning)
if __name__ == '__main__':
main()