forked from noties/Markwon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sample, py script to migrate sample ids
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
app-sample/src/main/java/io/noties/markwon/app/samples/ids.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# script used to fix generated ids of samples (`DD` was used in pattern | ||
# instead of `dd`, so day in year instead of day in month) | ||
|
||
import os | ||
import re | ||
from datetime import datetime | ||
|
||
|
||
def samples(array, directory): | ||
for f in os.listdir(directory): | ||
name = os.path.join(directory, f) | ||
if os.path.isdir(name): | ||
samples(array, name) | ||
elif name.endswith("java") or name.endswith("kt"): | ||
array.append(name) | ||
|
||
|
||
def process(sample): | ||
with open(sample, "r+") as f: | ||
code = f.read() | ||
m = re.search( | ||
r"(?:\s\S)*@MarkwonSampleInfo\(\s*id\s*\=\s*?\"(\d+)\"(?:\s\S)*", code) | ||
if not m: | ||
print( | ||
"File {} does not contain `@MarkwonSampleInfo` annotation".format(sample)) | ||
else: | ||
group = m.group(1) | ||
# `%j` is day in year modifier | ||
# for day in year we take substring starting when month ends (year and month are fixed width) | ||
# and end excluding hour, minute and second (also fixed) | ||
day = datetime.strptime("{} {}".format( | ||
group[6:-6], group[0:4]), "%j %Y").day | ||
id = group[:6] + "{:02d}".format(day) + group[-6:] | ||
print("{} `{}` -> `{}`".format(sample, group, id)) | ||
updated_code = code.replace(group, id, 1) | ||
|
||
# https://stackoverflow.com/a/15976014 | ||
f.seek(0) | ||
f.write(updated_code) | ||
f.truncate() | ||
|
||
|
||
if __name__ == '__main__': | ||
array = [] | ||
samples(array, ".") | ||
for sample in array: | ||
process(sample) |