Skip to content

Commit

Permalink
Sample, py script to migrate sample ids
Browse files Browse the repository at this point in the history
  • Loading branch information
noties committed Jul 30, 2020
1 parent 0227615 commit 01143fd
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions app-sample/src/main/java/io/noties/markwon/app/samples/ids.py
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)

0 comments on commit 01143fd

Please sign in to comment.