def load_color(self, value): # convert signed to unsigned 32-bit int value = struct.unpack('=I', struct.pack('=i', value))[0] # throw away leading ff, if any value &= 0x00ffffff return kurt.Color( (value & 0xff0000) >> 16, (value & 0x00ff00) >> 8, (value & 0x0000ff), )
def load_block(self, block_array): args = list(block_array) command = args.pop(0) assert isinstance(command, Symbol) command = command.value # special-case blocks with weird arguments if command == 'EventHatMorph': if args[0] == 'Scratch-StartClicked': return kurt.Block('whenGreenFlag') else: return kurt.Block('whenIreceive', args[0]) elif command == 'MouseClickEventHatMorph': return kurt.Block('whenClicked') elif command == 'changeVariable': command = args.pop(1).value else: command = command # recursively load args new_args = [] for arg in args: if isinstance(arg, list): if arg and isinstance(arg[0], Symbol): arg = self.load_block(arg) else: arg = map(self.load_block, arg) elif isinstance(arg, Color): arg = kurt.Color(arg.to_8bit()) elif isinstance(arg, Symbol): if arg.value == 'mouse': arg = 'mouse-pointer' elif arg.value == 'edge': arg = 'edge' elif arg.value in ('all', 'last', 'any'): arg = 'random' if arg.value == 'any' else arg.value else: raise ValueError(arg) elif getattr(arg, 'class_name', None) == 'ScratchStageMorph': arg = "Stage" elif getattr(arg, 'class_name', None) == 'ScratchSpriteMorph': arg = arg.name new_args.append(arg) return kurt.Block(command, *new_args)
def nud(self): self.value = kurt.Color(self.value) return self
import kurt from kurt.plugin import Kurt, KurtPlugin from kurt.scratch20.blocks import make_block_types, custom_block, make_spec SOUND_FORMATS = ['.wav'] WATCHER_MODES = [ None, 'normal', 'large', 'slider', ] CATEGORY_COLORS = { 'variables': kurt.Color('#ee7d16'), 'motion': kurt.Color('#4a6cd4'), 'looks': kurt.Color('#8a55d7'), 'sound': kurt.Color('#bb42c3'), 'sensing': kurt.Color('#2ca5e2'), } def get_blocks_by_id(this_block): if isinstance(this_block, kurt.Script): for block in this_block.blocks: for b in get_blocks_by_id(block): yield b elif isinstance(this_block, kurt.Block): yield this_block for arg in this_block.args:
assert p("say (length of [hello])")[0].args[0].command == 'stringLength:' assert p("say (length of (foo))")[0].args[0].command == 'stringLength:' #assert p("say (length of [positions v])")[0].args[0].command == 'lineCountOfList:' assert p(u'[x position v] of [Sprite1]')[0].command == 'getAttribute:of:' assert p(u'[x position v] of [Sprite1 v]')[0].command == 'getAttribute:of:' assert p(u'[sqrt v] of (6)')[0].command == 'computeFunction:of:' assert p(u'[sqrt v] of [6]')[0].command == 'computeFunction:of:' assert p(u'[x position v] of (foo)')[0].command == 'getAttribute:of:' assert p(u'[sqrt v] of (foo)')[0].command == 'computeFunction:of:' for mf in kurt.Insert('number', 'mathOp').options(): assert p(u'[%s v] of (foo)' % mf)[0].command == 'computeFunction:of:' assert p('set pen color to [#faa41a]')[0] == kurt.Block( 'penColor:', kurt.Color('#faa41a')) assert p('set pen color to (0)')[0] == kurt.Block('setPenHueTo:', 0) def check(x): b = p(x) assert p(b.stringify()) == b check(""" if <> say [hi] else play sound [pop v] end """)