123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #!/usr/bin/python
- # -*- coding:utf-8 -*-
- ##
- # This file is part of connectSVG.
- #
- # connectSVG is free software: you can redistribute it and/or modify it
- # under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License,
- # or (at your option) any later version.
- #
- # connectSVG is distributed in the hope that it will be useful, but WITHOUT ANY
- # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- # details.
- #
- # You should have received a copy of the GNU General Public License along
- # with connectSVG. If not, see <https://www.gnu.org/licenses/>.
- ##
- VERSION = [0,0]
- VERSION_STR = f"{VERSION[0]}.{VERSION[1]}"
- VERSION_REPR = f"connectSVG {VERSION_STR}"
- from svgpathtools import svg2paths, wsvg
- import sys
- import logging
- import argparse
- ##
- # Arguments parsing
- ##
- parser = argparse.ArgumentParser(
- prog="connectSVG",
- description="Connect and order the paths in a svg file and wite the new svg")
- parser.add_argument('infile',
- help="The svg to connect")
- parser.add_argument('outfile',
- nargs="?",
- default=None,
- help="""The name of the file to write in. If omited the program will
- write in the same file it reads""")
- parser.add_argument('-b', '--break-uncontinuous',
- action='store_true',
- help="Only break the uncontinuous paths")
- parser.add_argument('-t', '--tolerance',
- type=float,
- default=0.0000000000005,
- help="""Set the radius in which 2 points are taken as the same.
- This parameter is not considered if the connection is disabled.
- (default 0.0000000000005)""")
- connectGroup = parser.add_mutually_exclusive_group()
- connectGroup.add_argument('-c', '--connect',
- action='store_true',
- help="Only connect the paths")
- connectGroup.add_argument('--no-connect',
- action='store_true',
- help="Do everything except connecting paths")
- orderGroup = parser.add_mutually_exclusive_group()
- orderGroup.add_argument('-o', '--order',
- action='store_true',
- help="Only order the paths")
- orderGroup.add_argument('--no-order',
- action='store_true',
- help="Do everything except ordering the paths")
- parser.add_argument('-V', '--version',
- action='version',
- version=VERSION_REPR,
- help="Show program version")
- parser.add_argument('-v', '--verbose',
- action='store_const',
- const=logging.INFO,
- default=100,
- help="Print more log")
- parser.add_argument('-vv', '--vverbose',
- action='store_const',
- const=logging.DEBUG,
- default=100,
- help="Print the maximum of log")
- parser.add_argument('--verbose-file',
- action='store_const',
- const=logging.DEBUG,
- default=100,
- help="Print the maximum of log in the log file")
- args = parser.parse_args()
- ##
- # Setup logging
- ##
- FILE_MIN_LEVEL = min(logging.INFO, args.verbose_file)
- STDOUT_MIN_LEVEL = min(logging.ERROR, args.verbose, args.vverbose)
- 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)
- ##
- # Program
- ##
- log.info("Starting connectSVG")
- try :
- log.info("Reading file")
- paths, attributes = svg2paths(args.infile)
- log.info("Processing %s", args.infile)
- if not args.order :
- log.info("Breaking uncontinuous paths")
- from breakUncontinuous import breakUncontinuous
- paths, attributes = breakUncontinuous(paths, attributes)
- if not (args.no_connect or args.order or args.break_uncontinuous) :
- log.info("Connecting paths")
- import connectPaths
- connectPaths.TOLERANCE = args.tolerance
- paths, attributes = connectPaths.connectPaths(paths, attributes)
- if not (args.connect or args.no_order or args.break_uncontinuous) :
- log.info("Ordering paths")
- from orderPaths import orderPaths
- paths, attributes = orderPaths(paths, attributes)
- outfile = args.outfile if args.outfile else args.infile
- log.info("Writing in %s", outfile)
- wsvg(paths, attributes=attributes, filename=outfile)
- except FileNotFoundError :
- log.critical('The given file 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")
|