Skip to content

Commit

Permalink
URL redirect on plugin installation
Browse files Browse the repository at this point in the history
Use redirected filename when downloading scripts from the internet.
  • Loading branch information
pslacerda committed Aug 12, 2024
1 parent 03d7a7f commit f2c228c
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions modules/pymol/plugins/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,27 +309,30 @@ def fetchscript(title, dest=None, run=1, quiet=1):
filename = url = title
rawscript = 1

filename = os.path.join(dest, filename.rsplit('/')[-1])
if not quiet:
print('Downloading', url)

# get page content
try:
from . import pref_get
timeout = pref_get('network_timeout', 10.0)
handle = urllib2.urlopen(url, timeout=timeout)
content = handle.read().decode('utf-8')
filename = handle.url
except IOError as e:
raise CmdException(e, "Plugin-Error")

filename = os.path.join(dest, filename.rsplit('/')[-1])
if os.path.exists(filename):
if not quiet:
print('File "%s" exists, will not redownload')
else:
if not quiet:
print('Downloading', url)

# get page content
try:
content = urlreadstr(url, None if rawscript else 'utf-8')
except IOError as e:
raise CmdException(e, "Plugin-Error")

if not rawscript:
# redirect
redirect = re.match(r'\s*#REDIRECT\s*\[\[(.*?)\]\]', content)
if redirect is not None:
return fetchscript(redirect.group(1), dest, run, quiet)

# parse Infobox
pattern2 = re.compile(r'\{\{Infobox script-repo.*?\| *filename *= *([^|\s]+).*?\}\}', re.DOTALL)
chunks2 = pattern2.findall(content)
Expand All @@ -342,23 +345,22 @@ def fetchscript(title, dest=None, run=1, quiet=1):
# parse for <source ...>...</source>
pattern = re.compile(r'<(?:source|syntaxhighlight)\b[^>]*>(.*?)</(?:source|syntaxhighlight)>', re.DOTALL)
chunks = pattern.findall(content)

# check script-chunks for cmd.extend
chunks = [s for s in chunks if 'cmd.extend' in s]

if len(chunks) == 0:
raise CmdException('No <source> or <syntaxhighlight> block with cmd.extend found')
if len(chunks) > 1:
print('Warning: %d chunks found, only saving first' % (len(chunks)))

content = chunks[0]

with open(filename, 'w') as handle:
handle.write(content)
else:
with open(filename, 'wb') as handle:
with open(filename, 'w') as handle:
handle.write(content)

if int(run):
cmd.do("run " + filename, echo=not quiet)

Expand Down

0 comments on commit f2c228c

Please sign in to comment.