GitBucket
4.21.2
Toggle navigation
Snippets
Sign in
Files
Branches
1
Releases
Issues
1
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
nigel.stanger
/
Handbook
Browse code
Refactored program name
master
1 parent
0691801
commit
13b7da47c42d9dc79068c371a224bb7a90a2fecd
Nigel Stanger
authored
on 26 Nov 2019
Patch
Showing
3 changed files
calendar/teachingdates/__init__.py
calendar/teachingdates/app.py
calendar/teachingdates/config/cli.py
Ignore Space
Show notes
View
calendar/teachingdates/__init__.py
PROG = "generate_dates"
Ignore Space
Show notes
View
calendar/teachingdates/app.py
import argparse import copy import pathlib import sys from teachingdates import PROG import teachingdates.calendars as calendars import teachingdates.config as config def run(): args = config.parse_command_line() if args.debug: print("{prog}: debug: args: {a}".format(prog=PROG, a=args)) try: period_config, paper_config = config.parse_config(args.config, args) if args.debug: print("{prog}: debug: period config: {c}".format( prog=PROG, c=period_config)) print("{prog}: debug: paper config: {c}".format( prog=PROG, c=paper_config)) # priority: year on command line (defaults to current year), # year in config if period_config: args.year = ( args.config["year"] if "year" in args.config.keys() else args.year) cal = calendars.TeachingCalendar( args.year, args.period, args.paper, period_config, paper_config) print("Period = {p}".format(p=args.period)) print(cal.calendar()) print(cal.lecture_dates()) except config.LectureStyleError as e: print("{prog}: error: 'lecture' style was requested but " "{c} contains no papers for {t}.".format( prog=PROG, c=args.config, t=args.period)) except config.PaperError as e: print("{prog}: error: no entry in {c} for paper {p} ({t}).".format( prog=PROG, c=args.config, p=args.paper, t=args.period)) except config.PeriodError as e: print("{prog}: error: no entry in {c} for teaching " "period {p}.".format( prog=PROG, c=args.config_file, p=args.period)) except KeyError as e: print("{prog}: error: teaching period {p} in {c} is missing " "required key {k}.".format( prog=PROG, p=args.period, c=args.config, k=e))
import argparse import copy import pathlib import sys import teachingdates.calendars as calendars import teachingdates.config as config def run(): args = config.parse_command_line() if args.debug: print("DEBUG: args: {0}".format(args)) try: period_config, paper_config = config.parse_config(args.config, args) if args.debug: print("DEBUG: period config: {0}".format(period_config)) print("DEBUG: paper config: {0}".format(paper_config)) # priority: year on command line (defaults to current year), # year in config if period_config: args.year = ( args.config["year"] if "year" in args.config.keys() else args.year) cal = calendars.TeachingCalendar( args.year, args.period, args.paper, period_config, paper_config) print("Period = {p}".format(p=args.period)) print(cal.calendar()) print(cal.lecture_dates()) except config.LectureStyleError as e: print("ERROR: 'lecture' style was requested but {c} contains no " "papers for {t}.".format(c=args.config, t=args.period)) except config.PaperError as e: print("ERROR: no entry in {c} for paper {p} ({t}).".format( c=args.config, p=args.paper, t=args.period)) except config.PeriodError as e: print("ERROR: no entry in {c} for teaching period " "{p}.".format(c=args.config_file, p=args.period)) except KeyError as e: print("ERROR: teaching period {p} in {c} is missing required " "key {k}.".format(p=args.period, c=args.config, k=e))
Ignore Space
Show notes
View
calendar/teachingdates/config/cli.py
import argparse import datetime import importlib.resources as pkg_resources import yaml from teachingdates import PROG def parse_command_line(): """Parse command-line arguments. Example command lines: generate_dates --style calendar --format latex --year 2020 --period S1 --paper INFO201 generate_dates --style lecture --format xml --period S2 --paper INFO202 """ format_map = { "t": "text", "text": "text", "l": "latex", "latex": "latex", "x": "xml", "xml": "xml", } style_map = { "c": "calendar", "calendar": "calendar", "l": "lecture", "lecture": "lecture", "i": "iso", "iso": "iso", } parser = argparse.ArgumentParser(prog=PROG, description="") parser.add_argument( "--config", "-c", default=None, dest="config_file", help="file name of the configuration file " "[default '~/.config/{prog}/config.yml']".format(prog=PROG)) parser.add_argument( "--debug", "-d", action='store_true', help="enable debugging output") parser.add_argument( "--format", "-f", default="latex", choices=["text", "t", "latex", "l", "xml", "x"], help="output format [default 'latex']") parser.add_argument( "--output", "-o", type=argparse.FileType("w", encoding="UTF-8"), help="file name to write the output to; existing " "files of the same name will be overwritten!") parser.add_argument( "--paper", "-p", default=None, help="paper code (e.g., INFO201) [required for 'lecture' style, " "ignored otherwise]") parser.add_argument( "--style", "-s", default="calendar", choices=["calendar", "c", "lecture", "l", "iso", "i"], help="output style: 'calendar' for teaching calendars in course " "outlines, 'lecture' for individual lecture dates, or 'iso'" "for dates across the entire ISO-8601 year [default 'calendar']") parser.add_argument( "--teaching-period", "-t", default=None, choices=["SS", "S1", "S2", "FY"], dest="period", help="teaching period [required]") parser.add_argument( "--year", "-y", type=int, default=datetime.date.today().year, help=("the year to generate dates for [default {y}]".format( y=datetime.date.today().year))) args = parser.parse_args() # normalise format and style args.format = format_map[args.format] args.style = style_map[args.style] # Load the default config if none is explicitly specified. if not args.config_file: args.config_file = "default.yml" args.config = yaml.safe_load( pkg_resources.read_text("teachingdates.config", args.config_file)) else: with open(args.config_file) as cfg: args.config = yaml.safe_load(cfg.read()) if args.style == "calendar": # --paper is irrelevant if args.paper: print("{prog}: warning: --paper/-p is ignored for " "'calendar' style\n".format(prog=PROG)) # --teaching-period is required if not args.period: parser.exit(2, "{prog}: error: --teaching-period/-t " "is required for 'calendar' style\n".format(prog=PROG)) elif args.style == "iso": # both --paper and --teaching-period are irrelevant if args.paper or args.period: print("{prog}: warning: --paper/-p and " "--teaching-period/-t are ignored for 'iso' " "style\n".format(prog=PROG)) elif args.style == "lecture": # both --paper and --teaching-period are required if not args.paper: parser.exit(2, message="{prog}: error: -paper/-p and " "--teaching-period/-t are required for 'lecture' " "style\n".format(prog=PROG)) # --teaching-period must be specified if --style is "calendar" or "lecture" else: pass return args
import argparse import datetime import importlib.resources as pkg_resources import yaml def parse_command_line(): """Parse command-line arguments. Example command lines: generate_dates --style calendar --format latex --year 2020 --period S1 --paper INFO201 generate_dates --style lecture --format xml --period S2 --paper INFO202 """ format_map = { "t": "text", "text": "text", "l": "latex", "latex": "latex", "x": "xml", "xml": "xml", } style_map = { "c": "calendar", "calendar": "calendar", "l": "lecture", "lecture": "lecture", "i": "iso", "iso": "iso", } parser = argparse.ArgumentParser( prog="generate_dates", description="") parser.add_argument( "--config", "-c", default=None, dest="config_file", help="file name of the configuration file " "[default 'default.yml' in teachingdates.config]") parser.add_argument( "--debug", "-d", action='store_true', help="enable debugging output") parser.add_argument( "--format", "-f", default="latex", choices=["text", "t", "latex", "l", "xml", "x"], help="output format [default 'latex']") parser.add_argument( "--output", "-o", type=argparse.FileType("w", encoding="UTF-8"), help="file name to write the output to; existing " "files of the same name will be overwritten!") parser.add_argument( "--paper", "-p", default=None, help="paper code (e.g., INFO201) [required for 'lecture' style, " "ignored otherwise]") parser.add_argument( "--style", "-s", default="calendar", choices=["calendar", "c", "lecture", "l", "iso", "i"], help="output style: 'calendar' for teaching calendars in course " "outlines, 'lecture' for individual lecture dates, or 'iso'" "for dates across the entire ISO-8601 year [default 'calendar']") parser.add_argument( "--teaching-period", "-t", default=None, choices=["SS", "S1", "S2", "FY"], dest="period", help="teaching period [required]") parser.add_argument( "--year", "-y", type=int, default=datetime.date.today().year, help=("the year to generate dates for [default {y}]".format( y=datetime.date.today().year))) args = parser.parse_args() # normalise format and style args.format = format_map[args.format] args.style = style_map[args.style] # Load the default config if none is explicitly specified. if not args.config_file: args.config_file = "default.yml" args.config = yaml.safe_load( pkg_resources.read_text("teachingdates.config", args.config_file)) else: with open(args.config_file) as cfg: args.config = yaml.safe_load(cfg.read()) if args.style == "calendar": # --paper is irrelevant if args.paper: print("generate_dates: warning: --paper/-p is ignored for " "'calendar' style") # --teaching-period is required if not args.period: parser.exit(2, "generate_dates: error: --teaching-period/-t " "is required for 'calendar' style\n") elif args.style == "iso": # both --paper and --teaching-period are irrelevant if args.paper or args.period: print("generate_dates: warning: --paper/-p and " "--teaching-period/-t are ignored for 'iso' style") elif args.style == "lecture": # both --paper and --teaching-period are required if not args.paper: parser.exit(2, message="generate_dates: error: -paper/-p and " "--teaching-period/-t are required for 'lecture' style\n") # --teaching-period must be specified if --style is "calendar" or "lecture" else: pass return args
Show line notes below