From 1dec28bd6cf7f1d5e8c95e2be163bcee2ff71845 Mon Sep 17 00:00:00 2001 From: damien Date: Sun, 28 Feb 2016 14:26:31 +0100 Subject: [PATCH 01/10] fix check for empty command list in sequence frame a frame without any draw command returns an empty list (not None) --- tools/svg2pdc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/svg2pdc.py b/tools/svg2pdc.py index 6404094..2053c7b 100644 --- a/tools/svg2pdc.py +++ b/tools/svg2pdc.py @@ -559,7 +559,7 @@ def parse_svg_sequence(dir_name, precise=False, raise_error=False): translate, size = get_info(get_xml(file_list[0])) # get the viewbox from the first file for filename in file_list: cmd_list, error = get_commands(translate, get_xml(filename), precise, raise_error) - if cmd_list is not None: + if cmd_list: frames.append(cmd_list) if error: error_files.append(filename) From 3bb7f9a2ca3d0e1fab6087245164057ecc6d1359 Mon Sep 17 00:00:00 2001 From: damien Date: Sun, 28 Feb 2016 22:15:37 +0100 Subject: [PATCH 02/10] make svg2pdc executable --- tools/svg2pdc.py | 1 + 1 file changed, 1 insertion(+) mode change 100644 => 100755 tools/svg2pdc.py diff --git a/tools/svg2pdc.py b/tools/svg2pdc.py old mode 100644 new mode 100755 index 2053c7b..b1e361c --- a/tools/svg2pdc.py +++ b/tools/svg2pdc.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python # # Copyright (c) 2015 Pebble Technology # From f702045581e661dcd7ede9213e8a2289e6b3de35 Mon Sep 17 00:00:00 2001 From: damien Date: Mon, 29 Feb 2016 22:07:10 +0100 Subject: [PATCH 03/10] ignore .pyc files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b83805a..a1383e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build .lock-* +*.pyc From 575f37244e233c1718b6b6eb44353b8d4f5f3c32 Mon Sep 17 00:00:00 2001 From: damien Date: Mon, 29 Feb 2016 22:07:29 +0100 Subject: [PATCH 04/10] a frame now represents a "Draw Command Frame" it contains now both the command list and the duration --- tools/svg2pdc.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tools/svg2pdc.py b/tools/svg2pdc.py index b1e361c..f616232 100755 --- a/tools/svg2pdc.py +++ b/tools/svg2pdc.py @@ -505,22 +505,22 @@ def print_commands(commands): def print_frames(frames): for i in range(len(frames)): - print 'Frame {}:'.format(i + 1) - print_commands(frames[i]) + print 'Frame {}, duration {} ms:'.format(i + 1, frames[i]['duration']) + print_commands(frames[i]['command_list']) -def serialize_frame(frame, duration): - return pack('H', duration) + serialize(frame) # Frame duration +def serialize_frame(frame): + return pack('H', frame['duration']) + serialize(frame['command_list']) def pack_header(size): return pack(' Date: Tue, 1 Mar 2016 23:34:58 +0100 Subject: [PATCH 05/10] generate sequence from sequence xml file --- tools/svg2pdc.py | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/tools/svg2pdc.py b/tools/svg2pdc.py index f616232..ed68feb 100755 --- a/tools/svg2pdc.py +++ b/tools/svg2pdc.py @@ -551,7 +551,27 @@ def parse_svg_image(filename, precise=False, raise_error=False): return size, cmd_list, error -def parse_svg_sequence(dir_name, duration, precise=False, raise_error=False): +def parse_sequence_file(filename): + ''' + + + + + + + + + ''' + sequence_root = get_xml(filename) + frames_desc = [] + for frame_desc in sequence_root: + src = os.path.dirname(filename) + '/' + frame_desc.get('src') + duration = int(frame_desc.get('duration')) + frames_desc.append({'src': src, 'duration': duration}) + return frames_desc + + +def parse_svg_sequence_dir(dir_name, duration, precise=False, raise_error=False): frames = [] error_files = [] file_list = sorted(glob.glob(dir_name + "/*.svg")) @@ -567,6 +587,19 @@ def parse_svg_sequence(dir_name, duration, precise=False, raise_error=False): return size, frames, error_files +def parse_svg_sequence_file(filename, precise=False, raise_error=False): + frames = [] + error_files = [] + frames_desc = parse_sequence_file(filename) + for frame_desc in frames_desc: + size, cmd_list, error = parse_svg_image(frame_desc['src'], precise, raise_error) + if cmd_list: + frames.append({'command_list': cmd_list, 'duration': frame_desc['duration']}) + if error: + error_files.append(filename) + return size, frames, error_files + + def create_pdc_from_path(path, sequence, out_path, verbose, duration, play_count, precise=False, raise_error=False): dir_name = path output = '' @@ -579,8 +612,10 @@ def create_pdc_from_path(path, sequence, out_path, verbose, duration, play_count frames = [] commands = [] if sequence: - # get all .svg files in directory - result = parse_svg_sequence(dir_name, duration, precise, raise_error) + if os.path.isfile(path): + result = parse_svg_sequence_file(path, precise, raise_error) + else: + result = parse_svg_sequence_dir(dir_name, duration, precise, raise_error) if result: frames = result[1] size = result[0] @@ -629,7 +664,7 @@ def main(args): if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('path', type=str, - help="Path to svg file or directory (with multiple svg files)") + help="Path to svg file, or sequence file, or directory (with multiple svg files)") parser.add_argument('-s', '--sequence', action='store_true', help="Path is a directory and a sequence will be produced as output") parser.add_argument('-o', '--output', type=str, From 6dc85a8f41c5a9082d1456ea9f19f4fe873637df Mon Sep 17 00:00:00 2001 From: damien Date: Sun, 6 Mar 2016 14:09:42 +0100 Subject: [PATCH 06/10] handle errors in sequence file definition --- tools/svg2pdc.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tools/svg2pdc.py b/tools/svg2pdc.py index ed68feb..544368a 100755 --- a/tools/svg2pdc.py +++ b/tools/svg2pdc.py @@ -565,9 +565,15 @@ def parse_sequence_file(filename): sequence_root = get_xml(filename) frames_desc = [] for frame_desc in sequence_root: - src = os.path.dirname(filename) + '/' + frame_desc.get('src') - duration = int(frame_desc.get('duration')) - frames_desc.append({'src': src, 'duration': duration}) + try: + src = os.path.dirname(filename) + '/' + str(frame_desc.get('src')) + duration = int(frame_desc.get('duration')) + if os.path.isfile(src): + frames_desc.append({'src': src, 'duration': duration}) + else: + raise ValueError + except (TypeError, ValueError): + print 'Invalid frame definition: ' + src return frames_desc From c470fd7c8595af7b5293d8daba2ffe11307106c9 Mon Sep 17 00:00:00 2001 From: damien Date: Sun, 6 Mar 2016 14:27:10 +0100 Subject: [PATCH 07/10] use optional default duration in sequence file --- tools/svg2pdc.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/tools/svg2pdc.py b/tools/svg2pdc.py index 544368a..3c15df8 100755 --- a/tools/svg2pdc.py +++ b/tools/svg2pdc.py @@ -554,26 +554,30 @@ def parse_svg_image(filename, precise=False, raise_error=False): def parse_sequence_file(filename): ''' - - - + + + - - + + ''' sequence_root = get_xml(filename) frames_desc = [] - for frame_desc in sequence_root: - try: - src = os.path.dirname(filename) + '/' + str(frame_desc.get('src')) - duration = int(frame_desc.get('duration')) - if os.path.isfile(src): - frames_desc.append({'src': src, 'duration': duration}) - else: - raise ValueError - except (TypeError, ValueError): - print 'Invalid frame definition: ' + src + try: + default_duration = sequence_root.get('default_duration') + for frame_desc in sequence_root: + try: + src = os.path.dirname(filename) + '/' + str(frame_desc.get('src')) + duration = int(frame_desc.get('duration')) if frame_desc.get('duration') else int(default_duration) + if os.path.isfile(src): + frames_desc.append({'src': src, 'duration': duration}) + else: + raise ValueError + except (TypeError, ValueError): + print 'Invalid frame definition: ' + src + except (TypeError, ValueError): + print 'Invalid sequence file' return frames_desc From 24ae7f651cf75ad8d28cb2bf224ea859089b6c50 Mon Sep 17 00:00:00 2001 From: damien Date: Sun, 6 Mar 2016 15:01:08 +0100 Subject: [PATCH 08/10] use command line duration as fallback in sequence file --- tools/svg2pdc.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tools/svg2pdc.py b/tools/svg2pdc.py index 3c15df8..bda11d6 100755 --- a/tools/svg2pdc.py +++ b/tools/svg2pdc.py @@ -551,7 +551,7 @@ def parse_svg_image(filename, precise=False, raise_error=False): return size, cmd_list, error -def parse_sequence_file(filename): +def parse_sequence_file(filename, duration_fallback=False): ''' @@ -569,7 +569,12 @@ def parse_sequence_file(filename): for frame_desc in sequence_root: try: src = os.path.dirname(filename) + '/' + str(frame_desc.get('src')) - duration = int(frame_desc.get('duration')) if frame_desc.get('duration') else int(default_duration) + if frame_desc.get('duration'): + duration = int(frame_desc.get('duration')) + elif default_duration: + duration = int(default_duration) + else: + duration = int(duration_fallback) if os.path.isfile(src): frames_desc.append({'src': src, 'duration': duration}) else: @@ -597,10 +602,10 @@ def parse_svg_sequence_dir(dir_name, duration, precise=False, raise_error=False) return size, frames, error_files -def parse_svg_sequence_file(filename, precise=False, raise_error=False): +def parse_svg_sequence_file(filename, duration_fallback=False, precise=False, raise_error=False): frames = [] error_files = [] - frames_desc = parse_sequence_file(filename) + frames_desc = parse_sequence_file(filename, duration_fallback) for frame_desc in frames_desc: size, cmd_list, error = parse_svg_image(frame_desc['src'], precise, raise_error) if cmd_list: @@ -623,7 +628,7 @@ def create_pdc_from_path(path, sequence, out_path, verbose, duration, play_count commands = [] if sequence: if os.path.isfile(path): - result = parse_svg_sequence_file(path, precise, raise_error) + result = parse_svg_sequence_file(path, duration, precise, raise_error) else: result = parse_svg_sequence_dir(dir_name, duration, precise, raise_error) if result: From 5308a17483e4137b2de7728809c295b91d5e1952 Mon Sep 17 00:00:00 2001 From: damien Date: Sun, 6 Mar 2016 15:33:50 +0100 Subject: [PATCH 09/10] share code between sequence creation from directory and file --- tools/svg2pdc.py | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/tools/svg2pdc.py b/tools/svg2pdc.py index bda11d6..8b84ab3 100755 --- a/tools/svg2pdc.py +++ b/tools/svg2pdc.py @@ -586,33 +586,30 @@ def parse_sequence_file(filename, duration_fallback=False): return frames_desc -def parse_svg_sequence_dir(dir_name, duration, precise=False, raise_error=False): +def parse_svg_sequence(sequence_frames, precise=False, raise_error=False): + size = (0, 0) frames = [] error_files = [] - file_list = sorted(glob.glob(dir_name + "/*.svg")) - if not file_list: - return - translate, size = get_info(get_xml(file_list[0])) # get the viewbox from the first file - for filename in file_list: - cmd_list, error = get_commands(translate, get_xml(filename), precise, raise_error) + for sequence_frame in sequence_frames: + size, cmd_list, error = parse_svg_image(sequence_frame['src'], precise, raise_error) if cmd_list: - frames.append({'command_list': cmd_list, 'duration': duration}) + frames.append({'command_list': cmd_list, 'duration': sequence_frame['duration']}) if error: error_files.append(filename) return size, frames, error_files +def parse_svg_sequence_dir(dir_name, duration, precise=False, raise_error=False): + sequence_frames = [] + file_list = sorted(glob.glob(dir_name + "/*.svg")) + for filename in file_list: + sequence_frames.append({'src': filename, 'duration': duration}) + return parse_svg_sequence(sequence_frames, precise, raise_error) + + def parse_svg_sequence_file(filename, duration_fallback=False, precise=False, raise_error=False): - frames = [] - error_files = [] - frames_desc = parse_sequence_file(filename, duration_fallback) - for frame_desc in frames_desc: - size, cmd_list, error = parse_svg_image(frame_desc['src'], precise, raise_error) - if cmd_list: - frames.append({'command_list': cmd_list, 'duration': frame_desc['duration']}) - if error: - error_files.append(filename) - return size, frames, error_files + sequence_frames = parse_sequence_file(filename, duration_fallback) + return parse_svg_sequence(sequence_frames, precise, raise_error) def create_pdc_from_path(path, sequence, out_path, verbose, duration, play_count, precise=False, raise_error=False): From 850f787a2e5779b822a4ccb3198419e23aee285d Mon Sep 17 00:00:00 2001 From: damien Date: Sun, 6 Mar 2016 15:43:52 +0100 Subject: [PATCH 10/10] get the viewbox from the first sequence frame not to break the previous behaviour --- tools/svg2pdc.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/svg2pdc.py b/tools/svg2pdc.py index 8b84ab3..2db3306 100755 --- a/tools/svg2pdc.py +++ b/tools/svg2pdc.py @@ -591,7 +591,9 @@ def parse_svg_sequence(sequence_frames, precise=False, raise_error=False): frames = [] error_files = [] for sequence_frame in sequence_frames: - size, cmd_list, error = parse_svg_image(sequence_frame['src'], precise, raise_error) + image_size, cmd_list, error = parse_svg_image(sequence_frame['src'], precise, raise_error) + if size == (0, 0): # get the viewbox from the first frame + size = image_size if cmd_list: frames.append({'command_list': cmd_list, 'duration': sequence_frame['duration']}) if error: