def fetch_args(self, args: argparse.Namespace) -> None: """Get arguments that were passed, and also perform basic validation on them. For example, make sure the center is an actual lat, lng , and make sure the radius is a positive number. Also, if radius is passed, then center must also be passed. Raises: ParameterError: Center was not a valid lat, lng coordinate, or radius was not positive. """ self._center = None if args.heatmap_center: latlng_str = args.heatmap_center.split(",") if len(latlng_str) != 2: raise ParameterError( f"Not a valid LAT,LNG pair: {args.heatmap_center}") try: lat = float(latlng_str[0].strip()) lng = float(latlng_str[1].strip()) except ValueError as e: raise ParameterError( f"Not a valid LAT,LNG pair: {args.heatmap_center}") from e if not -90 <= lat <= 90 or not -180 <= lng <= 180: raise ParameterError( f"Not a valid LAT,LNG pair: {args.heatmap_center}") self._center = s2sphere.LatLng.from_degrees(lat, lng) if args.heatmap_radius: if args.heatmap_radius <= 0: raise ParameterError( f"Not a valid radius: {args.heatmap_radius} (must be > 0)") if not args.heatmap_center: raise ParameterError("--heatmap-radius needs --heatmap-center") self._radius = args.heatmap_radius
def _list_gpx_files(base_dir: str) -> typing.Generator[str, None, None]: base_dir = os.path.abspath(base_dir) if not os.path.isdir(base_dir): raise ParameterError(f"Not a directory: {base_dir}") for name in os.listdir(base_dir): path_name = os.path.join(base_dir, name) if name.endswith(".gpx") and os.path.isfile(path_name): yield path_name
def main() -> None: """Handle command line arguments and call other modules as needed.""" p = poster.Poster() drawers = { "grid": grid_drawer.GridDrawer(p), "calendar": calendar_drawer.CalendarDrawer(p), "heatmap": heatmap_drawer.HeatmapDrawer(p), "circular": circular_drawer.CircularDrawer(p), "github": github_drawer.GithubDrawer(p), } args_parser = argparse.ArgumentParser(prog=__app_name__) args_parser.add_argument( "--gpx-dir", dest="gpx_dir", metavar="DIR", type=str, default=".", help="Directory containing GPX files (default: current directory).", ) args_parser.add_argument( "--output", metavar="FILE", type=str, default="poster.svg", help='Name of generated SVG image file (default: "poster.svg").', ) args_parser.add_argument( "--language", metavar="LANGUAGE", type=str, default="", help="Language (default: english).", ) args_parser.add_argument( "--localedir", metavar="DIR", type=str, help= "The directory where the translation files can be found (default: the system's locale directory).", ) args_parser.add_argument( "--year", metavar="YEAR", type=str, default="all", help= 'Filter tracks by year; "NUM", "NUM-NUM", "all" (default: all years)', ) args_parser.add_argument("--title", metavar="TITLE", type=str, help="Title to display.") args_parser.add_argument( "--athlete", metavar="NAME", type=str, default="John Doe", help='Athlete name to display (default: "John Doe").', ) args_parser.add_argument( "--special", metavar="FILE", action="append", default=[], help= "Mark track file from the GPX directory as special; use multiple times to mark " "multiple tracks.", ) types = '", "'.join(drawers.keys()) args_parser.add_argument( "--type", metavar="TYPE", default="grid", choices=drawers.keys(), help= f'Type of poster to create (default: "grid", available: "{types}").', ) args_parser.add_argument( "--background-color", dest="background_color", metavar="COLOR", type=str, default="#222222", help='Background color of poster (default: "#222222").', ) args_parser.add_argument( "--track-color", dest="track_color", metavar="COLOR", type=str, default="#4DD2FF", help='Color of tracks (default: "#4DD2FF").', ) args_parser.add_argument( "--track-color2", dest="track_color2", metavar="COLOR", type=str, help="Secondary color of tracks (default: none).", ) args_parser.add_argument( "--text-color", dest="text_color", metavar="COLOR", type=str, default="#FFFFFF", help='Color of text (default: "#FFFFFF").', ) args_parser.add_argument( "--special-color", dest="special_color", metavar="COLOR", default="#FFFF00", help='Special track color (default: "#FFFF00").', ) args_parser.add_argument( "--special-color2", dest="special_color2", metavar="COLOR", help="Secondary color of special tracks (default: none).", ) args_parser.add_argument( "--units", dest="units", metavar="UNITS", type=str, choices=["metric", "imperial"], default="metric", help='Distance units; "metric", "imperial" (default: "metric").', ) args_parser.add_argument( "--clear-cache", dest="clear_cache", action="store_true", help="Clear the track cache.", ) args_parser.add_argument( "--from-strava", dest="from_strava", metavar="FILE", type=str, help="strava config file", ) args_parser.add_argument("--verbose", dest="verbose", action="store_true", help="Verbose logging.") args_parser.add_argument("--logfile", dest="logfile", metavar="FILE", type=str) args_parser.add_argument( "--special-distance", dest="special_distance", metavar="DISTANCE", type=float, default=10.0, help="Special Distance1 by km and color with the special_color", ) args_parser.add_argument( "--special-distance2", dest="special_distance2", metavar="DISTANCE", type=float, default=20.0, help="Special Distance2 by km and corlor with the special_color2", ) args_parser.add_argument( "--min-distance", dest="min_distance", metavar="DISTANCE", type=float, default=1.0, help="min distance by km for track filter", ) for _, drawer in drawers.items(): drawer.create_args(args_parser) args = args_parser.parse_args() for _, drawer in drawers.items(): drawer.fetch_args(args) log = logging.getLogger("gpxtrackposter") log.setLevel(logging.INFO if args.verbose else logging.ERROR) if args.logfile: handler = logging.FileHandler(args.logfile) log.addHandler(handler) loader = track_loader.TrackLoader() loader.set_cache_dir( os.path.join(appdirs.user_cache_dir(__app_name__, __app_author__), "tracks")) if not loader.year_range.parse(args.year): raise ParameterError(f"Bad year range: {args.year}.") loader.special_file_names = args.special loader.set_min_length(args.min_distance * Units().km) if args.clear_cache: print("Clearing cache...") loader.clear_cache() if args.from_strava: tracks = loader.load_strava_tracks(args.from_strava) else: tracks = loader.load_tracks(args.gpx_dir) if not tracks: if not args.clear_cache: print("No tracks found.") return print( f"Creating poster of type {args.type} with {len(tracks)} tracks and storing it in file {args.output}..." ) p.set_language(args.language, args.localedir) p.set_athlete(args.athlete) p.set_title(args.title if args.title else p.translate("MY TRACKS")) p.special_distance = { "special_distance": args.special_distance * Units().km, "special_distance2": args.special_distance2 * Units().km, } p.colors = { "background": args.background_color, "track": args.track_color, "track2": args.track_color2 or args.track_color, "special": args.special_color, "special2": args.special_color2 or args.special_color, "text": args.text_color, } p.units = args.units p.set_tracks(tracks) if args.type == "github": p.height = 55 + p.years.count() * 43 p.draw(drawers[args.type], args.output)
def main(): """Handle command line arguments and call other modules as needed.""" p = poster.Poster() drawers = { "grid": grid_drawer.GridDrawer(p), "circular": circular_drawer.CircularDrawer(p), "github": github_drawer.GithubDrawer(p), } args_parser = argparse.ArgumentParser() args_parser.add_argument( "--gpx-dir", dest="gpx_dir", metavar="DIR", type=str, default=".", help="Directory containing GPX files (default: current directory).", ) args_parser.add_argument( "--output", metavar="FILE", type=str, default="poster.svg", help='Name of generated SVG image file (default: "poster.svg").', ) args_parser.add_argument( "--language", metavar="LANGUAGE", type=str, default="", help="Language (default: english).", ) args_parser.add_argument( "--year", metavar="YEAR", type=str, default="all", help= 'Filter tracks by year; "NUM", "NUM-NUM", "all" (default: all years)', ) args_parser.add_argument("--title", metavar="TITLE", type=str, help="Title to display.") args_parser.add_argument( "--athlete", metavar="NAME", type=str, default="John Doe", help='Athlete name to display (default: "John Doe").', ) args_parser.add_argument( "--special", metavar="FILE", action="append", default=[], help= "Mark track file from the GPX directory as special; use multiple times to mark " "multiple tracks.", ) types = '", "'.join(drawers.keys()) args_parser.add_argument( "--type", metavar="TYPE", default="grid", choices=drawers.keys(), help= f'Type of poster to create (default: "grid", available: "{types}").', ) args_parser.add_argument( "--background-color", dest="background_color", metavar="COLOR", type=str, default="#222222", help='Background color of poster (default: "#222222").', ) args_parser.add_argument( "--track-color", dest="track_color", metavar="COLOR", type=str, default="#4DD2FF", help='Color of tracks (default: "#4DD2FF").', ) args_parser.add_argument( "--track-color2", dest="track_color2", metavar="COLOR", type=str, help="Secondary color of tracks (default: none).", ) args_parser.add_argument( "--text-color", dest="text_color", metavar="COLOR", type=str, default="#FFFFFF", help='Color of text (default: "#FFFFFF").', ) args_parser.add_argument( "--special-color", dest="special_color", metavar="COLOR", default="#FFFF00", help='Special track color (default: "#FFFF00").', ) args_parser.add_argument( "--special-color2", dest="special_color2", metavar="COLOR", help="Secondary color of special tracks (default: none).", ) args_parser.add_argument( "--units", dest="units", metavar="UNITS", type=str, choices=["metric", "imperial"], default="metric", help='Distance units; "metric", "imperial" (default: "metric").', ) args_parser.add_argument( "--clear-cache", dest="clear_cache", action="store_true", help="Clear the track cache.", ) args_parser.add_argument("--verbose", dest="verbose", action="store_true", help="Verbose logging.") args_parser.add_argument("--logfile", dest="logfile", metavar="FILE", type=str) args_parser.add_argument( "--special-distance", dest="special_distance", metavar="DISTANCE", type=float, default=10.0, help="Special Distance1 by km and color with the special_color", ) args_parser.add_argument( "--special-distance2", dest="special_distance2", metavar="DISTANCE", type=float, default=20.0, help="Special Distance2 by km and corlor with the special_color2", ) args_parser.add_argument( "--min-distance", dest="min_distance", metavar="DISTANCE", type=float, default=3.0, help="min distance by km for track filter", ) args_parser.add_argument( "--use-localtime", dest="use_localtime", action="store_true", help="Use utc time or local time", ) args_parser.add_argument( "--from-db", dest="from_db", action="store_true", help="activities db file", ) for _, drawer in drawers.items(): drawer.create_args(args_parser) args = args_parser.parse_args() for _, drawer in drawers.items(): drawer.fetch_args(args) log = logging.getLogger("gpxtrackposter") log.setLevel(logging.INFO if args.verbose else logging.ERROR) if args.logfile: handler = logging.FileHandler(args.logfile) log.addHandler(handler) loader = track_loader.TrackLoader() if args.use_localtime: loader.use_local_time = True loader.cache_dir = os.path.join( appdirs.user_cache_dir(__app_name__, __app_author__), "tracks") if not loader.year_range.parse(args.year): raise ParameterError(f"Bad year range: {args.year}.") loader.special_file_names = args.special loader.min_length = args.min_distance * 1000 if args.clear_cache: print("Clearing cache...") loader.clear_cache() if args.from_db: # for svg from db here if you want gpx please do not use --from-db # args.type == "grid" means have polyline data or not tracks = loader.load_tracks_from_db(SQL_FILE, args.type == "grid") else: tracks = loader.load_tracks(args.gpx_dir) if not tracks: if not args.clear_cache: print("No tracks found.") return is_circular = args.type == "circular" if not is_circular: print( f"Creating poster of type {args.type} with {len(tracks)} tracks and storing it in file {args.output}..." ) p.set_language(args.language) p.athlete = args.athlete if args.title: p.title = args.title else: p.title = p.trans("MY TRACKS") p.special_distance = { "special_distance": args.special_distance, "special_distance2": args.special_distance2, } p.colors = { "background": args.background_color, "track": args.track_color, "track2": args.track_color2 or args.track_color, "special": args.special_color, "special2": args.special_color2 or args.special_color, "text": args.text_color, } p.units = args.units p.set_tracks(tracks) # circular not add footer and header p.drawer_type = "plain" if is_circular else "title" if args.type == "github": p.height = 55 + p.years.count() * 43 # for special circular if is_circular: years = p.years.all()[:] for y in years: p.years.from_year, p.years.to_year = y, y # may be refactor p.set_tracks(tracks) p.draw(drawers[args.type], os.path.join("assets", f"year_{str(y)}.svg")) else: p.draw(drawers[args.type], args.output)
def main(): """Handle command line arguments and call other modules as needed.""" p = poster.Poster() drawers = { "grid": grid_drawer.GridDrawer(p), "calendar": calendar_drawer.CalendarDrawer(p), "heatmap": heatmap_drawer.HeatmapDrawer(p), "circular": circular_drawer.CircularDrawer(p) } args_parser = argparse.ArgumentParser() args_parser.add_argument( '--gpx-dir', dest='gpx_dir', metavar='DIR', type=str, default='.', help='Directory containing GPX files (default: current directory).') args_parser.add_argument( '--output', metavar='FILE', type=str, default='poster.svg', help='Name of generated SVG image file (default: "poster.svg").') args_parser.add_argument( '--year', metavar='YEAR', type=str, default='all', help= 'Filter tracks by year; "NUM", "NUM-NUM", "all" (default: all years)') args_parser.add_argument('--title', metavar='TITLE', type=str, default="My Tracks", help='Title to display (default: "My Tracks").') args_parser.add_argument( '--athlete', metavar='NAME', type=str, default="John Doe", help='Athlete name to display (default: "John Doe").') args_parser.add_argument( '--special', metavar='FILE', action='append', default=[], help= 'Mark track file from the GPX directory as special; use multiple times to mark ' 'multiple tracks.') args_parser.add_argument( '--type', metavar='TYPE', default='grid', choices=drawers.keys(), help='Type of poster to create (default: "grid", available: "{}").'. format('", "'.join(drawers.keys()))) args_parser.add_argument( '--background-color', dest='background_color', metavar='COLOR', type=str, default='#222222', help='Background color of poster (default: "#222222").') args_parser.add_argument('--track-color', dest='track_color', metavar='COLOR', type=str, default='#4DD2FF', help='Color of tracks (default: "#4DD2FF").') args_parser.add_argument('--track-color2', dest='track_color2', metavar='COLOR', type=str, help='Secondary color of tracks (default: none).') args_parser.add_argument('--text-color', dest='text_color', metavar='COLOR', type=str, default='#FFFFFF', help='Color of text (default: "#FFFFFF").') args_parser.add_argument('--special-color', dest='special_color', metavar='COLOR', default='#FFFF00', help='Special track color (default: "#FFFF00").') args_parser.add_argument( '--special-color2', dest='special_color2', metavar='COLOR', help='Secondary color of special tracks (default: none).') args_parser.add_argument( '--units', dest='units', metavar='UNITS', type=str, choices=['metric', 'imperial'], default='metric', help='Distance units; "metric", "imperial" (default: "metric").') args_parser.add_argument('--clear-cache', dest='clear_cache', action='store_true', help='Clear the track cache.') args_parser.add_argument('--verbose', dest='verbose', action='store_true', help='Verbose logging.') args_parser.add_argument('--logfile', dest='logfile', metavar='FILE', type=str) for _, drawer in drawers.items(): drawer.create_args(args_parser) args = args_parser.parse_args() for _, drawer in drawers.items(): drawer.fetch_args(args) log = logging.getLogger('gpxtrackposter') log.setLevel(logging.INFO if args.verbose else logging.ERROR) if args.logfile: handler = logging.FileHandler(args.logfile) log.addHandler(handler) loader = track_loader.TrackLoader() loader.cache_dir = os.path.join( appdirs.user_cache_dir(__app_name__, __app_author__), "tracks") if not loader.year_range.parse(args.year): raise ParameterError('Bad year range: {}.'.format(args.year)) loader.special_file_names = args.special if args.clear_cache: print('Clearing cache...') loader.clear_cache() tracks = loader.load_tracks(args.gpx_dir) if not tracks: if not args.clear_cache: print('No tracks found.') return print( "Creating poster of type '{}' with {} tracks and storing it in file '{}'..." .format(args.type, len(tracks), args.output)) p.athlete = args.athlete p.title = args.title p.colors = { 'background': args.background_color, 'track': args.track_color, 'track2': args.track_color2 if args.track_color2 is not None else args.track_color, 'special': args.special_color, 'special2': args.special_color2 if args.special_color2 is not None else args.special_color, 'text': args.text_color } p.units = args.units p.set_tracks(tracks) p.draw(drawers[args.type], args.output)