Arthur Chalard 2 rokov pred
rodič
commit
a8d15c43a7
5 zmenil súbory, kde vykonal 76 pridanie a 4 odobranie
  1. 2 1
      .gitignore
  2. 6 0
      breakUncontinuous.py
  3. 9 0
      connectPaths.py
  4. 57 3
      connectsvg.py
  5. 2 0
      orderPaths.py

+ 2 - 1
.gitignore

@@ -1,2 +1,3 @@
 *.svg
-__pycache__
+__pycache__
+*.log

+ 6 - 0
breakUncontinuous.py

@@ -1,10 +1,14 @@
 # -*- coding:utf-8 -*-
+import logging
+log = logging.getLogger("connectsvg")
 
 def breakUncontinuous(paths, attributes) :
+	log.info("Starting with %d paths", len(paths))
 	i = 0
 	while i < len(paths) :
 		path = paths[i]
 		pathAttributes = attributes[i]
+		log.debug("Processing path "+str(i))
 
 		if not path.iscontinuous() :
 			replacePaths = path.continuous_subpaths()
@@ -17,6 +21,8 @@ def breakUncontinuous(paths, attributes) :
 				paths.insert(i, replacePath)
 				attributes.insert(i, pathAttributes)
 				i += 1
+			log.debug("Replaced by %d paths", len(replacePaths))
 		i += 1
 
+	log.info("Ending with %d paths", len(paths))
 	return paths, attributes

+ 9 - 0
connectPaths.py

@@ -1,25 +1,31 @@
 # -*- coding:utf-8 -*-
+import logging
+log = logging.getLogger("connectsvg")
 
 TOLERANCE = 0.0000000000005
 
 def connect(path, other) :
 	if abs(path.end - other.start) <= TOLERANCE :
+		log.debug("Appending path %s--%s", other.start, other.end)
 		other.start = path.end
 		path.extend(other)
 		return path, True
 
 	elif abs(path.end - other.end) <= TOLERANCE:
+		log.debug("Appending path %s--%s", other.start, other.end)
 		other.end = path.end;
 		path.extend(other.reversed())
 		return path, True
 
 	elif abs(path.start - other.start) <= TOLERANCE :
+		log.debug("Appending path %s--%s", other.start, other.end)
 		other.start = path.start
 		path = path.reversed()
 		path.extend(other)
 		return path, True
 
 	elif abs(path.start - other.end) <= TOLERANCE :
+		log.debug("Appending path %s--%s", other.start, other.end)
 		other.end = path.start
 		path = path.reversed()
 		path.extend(other.reversed())
@@ -28,12 +34,14 @@ def connect(path, other) :
 	return path, False
 
 def connectPaths(paths, attributes) :
+	log.info("Starting with %d paths", len(paths))
 	processedPaths = []
 	processedAttributes = []
 
 	while len(paths) > 0 :
 		path = paths.pop()
 		attr = attributes.pop()
+		log.debug("Processing path %s--%s", path.start, path.end)
 
 		i = 0
 		while i < len(paths) :
@@ -53,4 +61,5 @@ def connectPaths(paths, attributes) :
 			'id': 'path'+str(len(processedPaths))
 			})
 
+	log.info("Ending with %d paths", len(processedPaths))
 	return processedPaths, processedAttributes

+ 57 - 3
connectsvg.py

@@ -3,28 +3,82 @@
 
 from svgpathtools import svg2paths, wsvg
 import sys
+import logging
+import argparse
 
+##
+# Setup logging
+##
+FILE_MIN_LEVEL = logging.INFO
+STDOUT_MIN_LEVEL = logging.ERROR
+
+logging.addLevelName(logging.NOTSET, '?')
+logging.addLevelName(logging.DEBUG, 'D')
+logging.addLevelName(logging.INFO, 'I')
+logging.addLevelName(logging.WARNING, 'W')
+logging.addLevelName(logging.ERROR, 'E')
+logging.addLevelName(logging.CRITICAL, 'C')
+
+log = logging.getLogger('connectsvg')
+log.setLevel(min(FILE_MIN_LEVEL, STDOUT_MIN_LEVEL))
+
+fh = logging.FileHandler('connectSVG.log')
+fh.setLevel(FILE_MIN_LEVEL)
+log.addHandler(fh)
+
+fhFormat = logging.Formatter('%(levelname)s/%(asctime)s (%(module)s) : %(message)s', '%Y-%m-%d %H:%M:%S,%03d')
+fh.setFormatter(fhFormat)
+
+sh = logging.StreamHandler(sys.stdout)
+sh.setLevel(STDOUT_MIN_LEVEL)
+log.addHandler(sh)
+
+shFormat = logging.Formatter('%(levelname)s - %(message)s')
+sh.setFormatter(shFormat)
+
+
+##
+# Import program parts
+##
 from Exceptions import *
 from breakUncontinuous import breakUncontinuous
 from connectPaths import connectPaths
 from orderPaths import orderPaths
 
+##
+# Program
+##
+
+log.info("Starting connectSVG")
+
 try :
+	log.info("Reading arguments")
 	if len(sys.argv) not in (2,3) :
 		raise InvalidArgumentException()
 
 	file = sys.argv[1]
 	oFile = sys.argv[2] if len(sys.argv) == 3 else sys.argv[1]
 
+	log.info("Reading svg")
 	paths, attributes = svg2paths(file)
 
+	log.info("Breaking uncontinuous paths")
 	paths, attributes = breakUncontinuous(paths, attributes)
+	log.info("Connecting paths")
 	paths, attributes = connectPaths(paths, attributes)
+	log.info("Ordering paths")
 	paths, attributes = orderPaths(paths, attributes)
 
-	wsvg(paths, attributes=attributes, filename=oFile)
+	outfile = args.outfile if args.outfile else args.infile
+	log.info("Writing in %s", outfile)
+	wsvg(paths, attributes=attributes, filename=outfile)
 
 except InvalidArgumentException :
-	print('The command accepts exactly one argument with the file to read.')
+	log.critical("The command accepts one argument with the file to read and one optional with the file to write")
 except FileNotFoundError :
-	print('The given file is not found.')
+	log.critical('The given file to read is not found')
+except Exception as e :
+	log.critical("Un uncaught error has occured:", exc_info=e)
+	log.info("Terminating connectSVG without writing")
+else :
+	log.info("connectSVG has finished")

+ 2 - 0
orderPaths.py

@@ -1,4 +1,6 @@
 # -*- coding:utf-8 -*-
+import logging
+log = logging.getLogger("connectsvg")
 
 def orderPaths(paths, attributes) :
 	orderedPaths = []