Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Add script to fix animations on windows - #383

Open
WillB97 wants to merge 2 commits into
mainfrom
fix-animations
Open

Add script to fix animations on windows#383
WillB97 wants to merge 2 commits into
mainfrom
fix-animations

Conversation

@WillB97

@WillB97 WillB97 commented Feb 18, 2023

Copy link
Copy Markdown
Contributor

Makes absolute paths relative, assumes ancillary files are siblings of the html.

Fixes #378

@WillB97
WillB97 requested a review from PeterJCLaw February 18, 2023 11:16

@PeterJCLaw PeterJCLaw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks for putting this together. Some small suggestions inline to make this a bit more robust.

Comment thread script/fix-animation
output_data.append(f' <link rel="stylesheet" href="{css_name}">\n')
elif '<webots-view' in line:
match = re.search(
r'data-thumbnail=(.*?) .*data-scene=(.*?) .*data-animation=(.*?)>', line)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please could we wrap this for consistency:

Suggested change
r'data-thumbnail=(.*?) .*data-scene=(.*?) .*data-animation=(.*?)>', line)
r'data-thumbnail=(.*?) .*data-scene=(.*?) .*data-animation=(.*?)>',
line,
)

Comment thread script/fix-animation
Comment on lines +17 to +18
css_name = match[1].replace('\\', '/').split('/')[-1]
output_data.append(f' <link rel="stylesheet" href="{css_name}">\n')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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).

Comment thread script/fix-animation
Comment on lines +25 to +27
jpg_name = match[1].replace('\\', '/').split('/')[-1]
x3d_name = match[2].replace('\\', '/').split('/')[-1]
json_name = match[3].replace('\\', '/').split('/')[-1]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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?
Perhaps like:

Suggested change
jpg_name = match[1].replace('\\', '/').split('/')[-1]
x3d_name = match[2].replace('\\', '/').split('/')[-1]
json_name = match[3].replace('\\', '/').split('/')[-1]
*jpg_dir, jpg_name = match[1].replace('\\', '/').split('/')
*x3d_dir, x3d_name = match[2].replace('\\', '/').split('/')
*json_dir, json_name = match[3].replace('\\', '/').split('/')
assert jpg_dir == x3d_dir == json_dir

Comment thread script/fix-animation
Comment on lines +42 to +43
parser.add_argument('animation')
parser.add_argument('output')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For learnings: you might be interested in argparse.FileType, a class which can be passed to the type kwarg here. It handles opening the files as well as natively supporting using - for STDIN and or STDOUT.

Comment thread script/fix-animation
data = f.readlines()

output_data = []
for line in data:

Copy link
Copy Markdown
Member

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Webots animations are broken (on Windows)

2 participants