-
-
Notifications
You must be signed in to change notification settings - Fork 2
Add script to fix animations on windows #383
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,51 @@ | ||||||||||||||||
| #!/usr/bin/env python3 | ||||||||||||||||
| import re | ||||||||||||||||
| import argparse | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| def fix_animation(input: str, output: str) -> None: # noqa: A002 | ||||||||||||||||
| with open(input) as f: | ||||||||||||||||
| data = f.readlines() | ||||||||||||||||
|
|
||||||||||||||||
| output_data = [] | ||||||||||||||||
| for line in data: | ||||||||||||||||
| if 'stylesheet' in line: | ||||||||||||||||
| match = re.search(r'href="(.*?)"', line) | ||||||||||||||||
| if match is None: | ||||||||||||||||
| output_data.append(line) | ||||||||||||||||
| else: | ||||||||||||||||
| css_name = match[1].replace('\\', '/').split('/')[-1] | ||||||||||||||||
| output_data.append(f' <link rel="stylesheet" href="{css_name}">\n') | ||||||||||||||||
|
Comment on lines
+17
to
+18
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feels like it would be safer to do a find & replace on the adjusted text rather than fully rebuilding the line. That would avoid any issues with the underlying template changing (also below). |
||||||||||||||||
| elif '<webots-view' in line: | ||||||||||||||||
| match = re.search( | ||||||||||||||||
| r'data-thumbnail=(.*?) .*data-scene=(.*?) .*data-animation=(.*?)>', line) | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: please could we wrap this for consistency:
Suggested change
|
||||||||||||||||
| if match is None: | ||||||||||||||||
| output_data.append(line) | ||||||||||||||||
| else: | ||||||||||||||||
| jpg_name = match[1].replace('\\', '/').split('/')[-1] | ||||||||||||||||
| x3d_name = match[2].replace('\\', '/').split('/')[-1] | ||||||||||||||||
| json_name = match[3].replace('\\', '/').split('/')[-1] | ||||||||||||||||
|
Comment on lines
+25
to
+27
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe worth asserting that the removed portion of these is all the same?
Suggested change
|
||||||||||||||||
| output_data.append( | ||||||||||||||||
| f' <webots-view data-thumbnail={jpg_name} ' | ||||||||||||||||
| f'data-scene={x3d_name} data-animation={json_name}></webots-view>\n', | ||||||||||||||||
| ) | ||||||||||||||||
| else: | ||||||||||||||||
| output_data.append(line) | ||||||||||||||||
|
|
||||||||||||||||
| with open(output, 'w') as f: | ||||||||||||||||
| f.writelines(output_data) | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| def main() -> None: | ||||||||||||||||
| parser = argparse.ArgumentParser() | ||||||||||||||||
|
|
||||||||||||||||
| parser.add_argument('animation') | ||||||||||||||||
| parser.add_argument('output') | ||||||||||||||||
|
Comment on lines
+42
to
+43
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For learnings: you might be interested in |
||||||||||||||||
|
|
||||||||||||||||
| args = parser.parse_args() | ||||||||||||||||
|
|
||||||||||||||||
| fix_animation(args.animation, args.output) | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| if __name__ == '__main__': | ||||||||||||||||
| main() | ||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
General style point for learning: I would encourage thinking about separating the logic which processes the content of the files from the logic which handles the file I/O. Doing so tends to make the code clearer as well as making the processing logic more easily reusable.
For this case I'd probably go with a separate function which operates on a single line at a time (and have a comment explicitly noting the assumption that the HTML elements of interest are each on a single line), keeping the loop in the same function as the IO.