| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | #======================================================================= | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | __version__ = '''0.0.01''' | 
					
						
							| 
									
										
										
										
											2013-05-28 15:47:25 +04:00
										 |  |  | __sub_version__ = '''20130528154208''' | 
					
						
							| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | __copyright__ = '''(c) Alex A. Naanou 2011''' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #----------------------------------------------------------------------- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import shutil | 
					
						
							|  |  |  | import json | 
					
						
							|  |  |  | import urllib2 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-28 15:39:59 +04:00
										 |  |  | from pli.logictypes import OR | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | from pprint import pprint | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-28 15:39:59 +04:00
										 |  |  | #----------------------------------------------------------------------- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | #----------------------------------------------------------------------- | 
					
						
							|  |  |  | #----------------------------------------------------------build_dirs--- | 
					
						
							| 
									
										
										
										
											2013-05-28 15:47:25 +04:00
										 |  |  | ##!!! make this figure out the path by itself... | 
					
						
							| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | def build_dirs(data, path, rewrite=None): | 
					
						
							|  |  |  | 	'''
 | 
					
						
							|  |  |  | 	rewrite can be: | 
					
						
							|  |  |  | 		'force'		- allways rewrite a destination file. | 
					
						
							|  |  |  | 		'older'		- rewrite only older files. | 
					
						
							|  |  |  | 		None		- never rewrite. | 
					
						
							|  |  |  | 	'''
 | 
					
						
							| 
									
										
										
										
											2013-05-20 03:06:48 +04:00
										 |  |  | 	version = data.get('version', 'gen1') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	print 'FORMAT:', version | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	# gen3 | 
					
						
							|  |  |  | 	if version == '2.0': | 
					
						
							| 
									
										
										
										
											2013-05-28 15:39:59 +04:00
										 |  |  | 		img = data['image_file'] | 
					
						
							|  |  |  | 		# absolute path... | 
					
						
							|  |  |  | 		if img.startswith('file:///'): | 
					
						
							|  |  |  | 			images = json.load(urllib2.urlopen(data['image_file'])) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		# relative... | 
					
						
							|  |  |  | 		else: | 
					
						
							|  |  |  | 			images = json.load(open(os.path.join(path, data['image_file']), 'r')) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-20 03:06:48 +04:00
										 |  |  | 		def get_image(gid, _): | 
					
						
							|  |  |  | 			return images[gid] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	# gen1 | 
					
						
							|  |  |  | 	else: | 
					
						
							|  |  |  | 		def get_image(gid, ribbon): | 
					
						
							|  |  |  | 			return ribbon[gid] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | 	ribbons = data['ribbons'] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-10 21:07:46 +04:00
										 |  |  | 	depth = len(ribbons)-1 | 
					
						
							| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | 	fav_path = os.path.join(path, *(['fav'] * depth)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if not os.path.exists(fav_path): | 
					
						
							|  |  |  | 		os.makedirs(fav_path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err_urls = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, ribbon in enumerate(ribbons): | 
					
						
							|  |  |  | 		level_path = os.path.join(path, *(['fav'] * (depth - i))) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-20 03:06:48 +04:00
										 |  |  | 		for guid in ribbon: | 
					
						
							|  |  |  | 			image = get_image(guid, ribbon) | 
					
						
							| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | 			try: | 
					
						
							| 
									
										
										
										
											2013-05-28 15:39:59 +04:00
										 |  |  | 				p = image['path'] | 
					
						
							|  |  |  | 				# absolute path... | 
					
						
							|  |  |  | 				if img.startswith('file:///'): | 
					
						
							|  |  |  | ##					# XXX for some magical reason this works and url2pathname does not... | 
					
						
							|  |  |  | ##					##!!! this will also break on utf paths... | 
					
						
							|  |  |  | ##					img = urllib2.urlopen(image['path']) | 
					
						
							|  |  |  | ##					p = img.fp.name | 
					
						
							|  |  |  | ##					img.close() | 
					
						
							|  |  |  | 					p = urllib2.decode(p) | 
					
						
							|  |  |  | 					p = p.split('file:///')[-1] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				# relative path... | 
					
						
							|  |  |  | 				else: | 
					
						
							|  |  |  | 					p = os.path.join(path, p) | 
					
						
							| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				# XXX do we need to indicate overwriting in the status? | 
					
						
							|  |  |  | 				if rewrite == 'force' or not os.path.exists(os.path.join(level_path, os.path.split(p)[-1])): | 
					
						
							|  |  |  | 					shutil.copy(p, level_path) | 
					
						
							|  |  |  | 					yield 'written', level_path, image | 
					
						
							|  |  |  | 				elif rewrite == 'older': | 
					
						
							|  |  |  | 					raise NotImplementedError | 
					
						
							|  |  |  | 					##!!! check dates... | 
					
						
							|  |  |  | 					##!!! | 
					
						
							|  |  |  | 					yield 'written', level_path, image | 
					
						
							|  |  |  | 				else: | 
					
						
							|  |  |  | 					yield 'skipped', level_path, image | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			except urllib2.URLError: | 
					
						
							|  |  |  | 				yield 'err', level_path, image | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #----------------------------------------------------------------------- | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  | 	from optparse import OptionParser | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-22 22:45:34 +04:00
										 |  |  | 	parser = OptionParser( | 
					
						
							|  |  |  | 			usage='%prog [options] DATAJSON TARGETDIR') | 
					
						
							| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	##!!! need to define the path so that it shoes up in -h | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	options, args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	# settings... | 
					
						
							|  |  |  | 	##!!! make this configurable... | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(args) != 2: | 
					
						
							|  |  |  | 		parser.print_usage() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	else: | 
					
						
							|  |  |  | 		data, path = args[0], args[1] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		path = path.replace('\\', '/') | 
					
						
							|  |  |  | 		data = json.load(file(data, 'r')) | 
					
						
							|  |  |  | 		err_urls = [] | 
					
						
							|  |  |  | 		cur_p = None | 
					
						
							| 
									
										
										
										
											2013-04-10 21:07:46 +04:00
										 |  |  | 		c = s = e = 0 | 
					
						
							| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for status, p, img in build_dirs(data, path): | 
					
						
							|  |  |  | 			if cur_p != p: | 
					
						
							|  |  |  | 				cur_p = p | 
					
						
							| 
									
										
										
										
											2013-04-10 21:07:46 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | 			if status == 'written': | 
					
						
							| 
									
										
										
										
											2013-04-10 21:07:46 +04:00
										 |  |  | 				c += 1 | 
					
						
							| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | 			elif status == 'skipped': | 
					
						
							| 
									
										
										
										
											2013-04-10 21:07:46 +04:00
										 |  |  | 				s += 1 | 
					
						
							| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | 			elif status == 'err': | 
					
						
							| 
									
										
										
										
											2013-04-10 21:07:46 +04:00
										 |  |  | 				e += 1 | 
					
						
							| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | 				err_urls += [img['path']] | 
					
						
							| 
									
										
										
										
											2013-04-10 21:07:46 +04:00
										 |  |  | 			print 'Copied: %s, Skipped: %s, Err: %s\r' % (c, s, e), | 
					
						
							| 
									
										
										
										
											2013-04-10 18:31:51 +04:00
										 |  |  | 		print | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if len(err_urls) != 0: | 
					
						
							|  |  |  | 			print 'Could not open %s files:' % len(err_urls) | 
					
						
							|  |  |  | 			pprint(err_urls) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #======================================================================= | 
					
						
							|  |  |  | #                                            vim:set ts=4 sw=4 nowrap : |