Handle reading version info from jgoguens updated plugins

The version info is now specified as an attribute of a module, import.
This commit is contained in:
Kovid Goyal 2020-06-17 08:55:46 +05:30
parent a9f9f8088e
commit 32438d1565
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -183,27 +183,41 @@ def convert_node(fields, x, names={}, import_data=None):
elif name == 'BinOp': elif name == 'BinOp':
if x.right.__class__.__name__ == 'Str': if x.right.__class__.__name__ == 'Str':
return x.right.s.decode('utf-8') if isinstance(x.right.s, bytes) else x.right.s return x.right.s.decode('utf-8') if isinstance(x.right.s, bytes) else x.right.s
elif name == 'Attribute':
return conv(getattr(conv(x.value), x.attr))
raise TypeError('Unknown datatype %s for fields: %s' % (x, fields)) raise TypeError('Unknown datatype %s for fields: %s' % (x, fields))
Alias = namedtuple('Alias', 'name asname') Alias = namedtuple('Alias', 'name asname')
class Module(object):
pass
def get_import_data(name, mod, zf, names): def get_import_data(name, mod, zf, names):
mod = mod.split('.') mod = mod.split('.')
if mod[0] == 'calibre_plugins': if mod[0] == 'calibre_plugins':
mod = mod[2:] mod = mod[2:]
is_module_import = not mod
if is_module_import:
mod = [name]
mod = '/'.join(mod) + '.py' mod = '/'.join(mod) + '.py'
if mod in names: if mod in names:
raw = zf.open(names[mod]).read() raw = zf.open(names[mod]).read()
module = ast.parse(raw, filename='__init__.py') module = ast.parse(raw, filename='__init__.py')
top_level_assigments = [x for x in ast.iter_child_nodes(module) if x.__class__.__name__ == 'Assign'] top_level_assigments = [x for x in ast.iter_child_nodes(module) if x.__class__.__name__ == 'Assign']
module = Module()
for node in top_level_assigments: for node in top_level_assigments:
targets = {getattr(t, 'id', None) for t in node.targets} targets = {getattr(t, 'id', None) for t in node.targets}
targets.discard(None) targets.discard(None)
for x in targets: for x in targets:
if x == name: if is_module_import:
setattr(module, x, node.value)
elif x == name:
return convert_node({x}, node.value) return convert_node({x}, node.value)
if is_module_import:
return module
raise ValueError('Failed to find name: %r in module: %r' % (name, mod)) raise ValueError('Failed to find name: %r in module: %r' % (name, mod))
else: else:
raise ValueError('Failed to find module: %r' % mod) raise ValueError('Failed to find module: %r' % mod)