Advertisement
Brovashift

args

Apr 9th, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.68 KB | None | 0 0
  1. import os
  2. import sys
  3.  
  4. from argparse import ArgumentParser
  5.  
  6. from utils.course import *
  7. from utils.date import *
  8. from utils.region import *
  9.  
  10.  
  11. help = (
  12.     f'Run:\n'
  13.     f'\t./rpscrape.py\n'
  14.     f'\t[rpscrape]> [region|course] [year|range] [flat|jumps]\n\n'
  15.     f'\tRegions have alphabetic codes\n'
  16.     f'\tCourses have numeric codes\n\n'
  17.     f'Examples:\n'
  18.     f'\t[rpscrape]> ire 1999 flat\n'
  19.     f'\t[rpscrape]> gb 2015-2018 jumps\n'
  20.     f'\t[rpscrape]> 533 1998-2018 flat\n'
  21. )
  22.  
  23.  
  24. options = (
  25.     f'\t{"regions": <20} List all available region codes\n'
  26.     f'\t{"regions [search]": <20} Search for specific region code\n\n'
  27.     f'\t{"courses": <20} List all courses\n'
  28.     f'\t{"courses [search]": <20} Search for specific course\n'
  29.     f'\t{"courses [region]": <20} List courses in region - e.g courses ire\n\n'
  30.     f'\t{"-d, date": <20} Scrape race by date - e.g -d 2019/12/17 gb\n\n'
  31.     f'\t{"help": <20} Show help\n'
  32.     f'\t{"options": <20} Show options\n'
  33.     f'\t{"cls, clear": <20} Clear screen\n'
  34.     f'\t{"q, quit, exit": <20} Quit\n'
  35. )
  36.  
  37.  
  38. INFO = {
  39.     'date': 'Date or date range. Format YYYY/MM/DD - e.g 2008/01/05 or 2020/01/19-2020/05/01',
  40.     'course': 'Course code. 1 to 4 digit code - e.g 20',
  41.     'region': 'Region code. 2 or 3 letter e.g ire',
  42.     'year': 'Year or range of years. Format YYYY - e.g 2018 or 2019-2020',
  43.     'type': 'Race type flat|jumps',
  44. }
  45.  
  46.  
  47. ERROR = {
  48.     'arg_len': 'Error: Too many arguments.\n\tUsage:\n\t\t[rpscrape]> [region|course] [year|range] [flat|jumps]',
  49.     'incompatible': 'Arguments incompatible.\n',
  50.     'incompatible_course': 'Choose course or region, not both.',
  51.     'invalid_c_or_r': 'Invalid course or region',
  52.     'invalid_course': 'Invalid Course code.\n\nExamples:\n\t\t-c 20\n\t\t-c 1083',
  53.     'invalid_date': 'Invalid date. Format:\n\t\t-d YYYY/MM/DD\n\nExamples:\n\t\t-d 2020/01/19\n\t\t2020/01/19-2020/01/29',
  54.     'invalid_region': 'Invalid Region code. \n\nExamples:\n\t\t-r gb\n\t\t-r ire',
  55.     'invalid_region_int': 'Invalid Region code. \n\nExamples:\n\t\t2020/01/19 gb\n\t\t2021/07/11 ire',
  56.     'invalid_type': 'Invalid type.\n\nMust be either flat or jumps.\n\nExamples:\n\t\t-t flat\n\t\t-t jumps',
  57.     'invalid_year': 'Invalid year.\n\nFormat:\n\t\tYYYY\n\nExamples:\n\t\t-y 2015\n\t\t-y 2012-2017',
  58.     'invalid_year_int': 'Invalid year. Must be in range 1988-'
  59. }
  60.  
  61.  
  62. class ArgParser:
  63.  
  64.     def __init__(self):
  65.         self.dates = []
  66.         self.tracks = []
  67.         self.years = []
  68.         self.parser = ArgumentParser()
  69.         self.add_arguments()
  70.  
  71.     def add_arguments(self):
  72.         self.parser.add_argument('-d', '--date',    metavar='', type=str, help=INFO['date'])
  73.         self.parser.add_argument('-c', '--course',  metavar='', type=str, help=INFO['course'])
  74.         self.parser.add_argument('-r', '--region',  metavar='', type=str, help=INFO['region'])
  75.         self.parser.add_argument('-y', '--year',    metavar='', type=str, help=INFO['year'])
  76.         self.parser.add_argument('-t', '--type',    metavar='', type=str, help=INFO['type'])
  77.  
  78.     def parse_args(self, arg_list):
  79.         args = self.parser.parse_args(args=arg_list)
  80.  
  81.         if args.date:
  82.             if any([args.course, args.year]):
  83.                 self.parser.error(ERROR['incompatible'])
  84.  
  85.             if not check_date(args.date):
  86.                 self.parser.error(ERROR['invalid_date'])
  87.  
  88.             self.dates = get_dates(args.date)
  89.  
  90.         if args.course and args.region:
  91.             self.parser.error(ERROR['incompatible'] + ERROR['incompatible_course'])
  92.  
  93.         if args.region:
  94.             if not valid_region(args.region):
  95.                 self.parser.error(ERROR['invalid_region'])
  96.  
  97.             self.tracks = [course for course in courses(args.region)]
  98.         else:
  99.             args.region = 'all'
  100.  
  101.         if args.course:
  102.             if not valid_course(args.course):
  103.                 self.parser.error(ERROR['invalid_course'])
  104.  
  105.             self.tracks = [(args.course, course_name(args.course))]
  106.  
  107.         if args.year:
  108.             self.years = parse_years(args.year)
  109.  
  110.             if not self.years or not valid_years(self.years):
  111.                 self.parser.error(ERROR['invalid_year'])
  112.  
  113.         if args.type and args.type not in {'flat', 'jumps'}:
  114.             self.parser.error(ERROR['invalid_type'])
  115.  
  116.         if not args.type:
  117.             args.type = ''
  118.  
  119.         return args
  120.  
  121.     def parse_args_interactive(self, args):
  122.         if len(args) == 1:
  123.             self.opts(args[0])
  124.             return
  125.  
  126.         if args[0] in {'courses', 'regions'}:
  127.             self.search(args[0], ' '.join(args[1:]), args[1])
  128.             return
  129.  
  130.         parsed = {}
  131.  
  132.         if args[0] in {'-d', 'date', 'dates'}:
  133.             parsed = self.parse_date_request(args)
  134.         else:
  135.             if len(args) > 3:
  136.                 print(ERROR['arg_len'])
  137.  
  138.             if args[0] not in {'courses', 'regions'} and len(args) != 3:
  139.                 return
  140.  
  141.             year = args[1]
  142.             parsed['years'] = self.parse_year(year)
  143.             parsed['type'] = self.get_racing_type(args[2])
  144.  
  145.             if not parsed['type']:
  146.                 print(ERROR['invalid_type'])
  147.  
  148.             elif valid_region(args[0]):
  149.                 region = args[0]
  150.                 parsed['folder_name'] = f'regions/{region}'
  151.                 parsed['file_name'] = year
  152.                 parsed['tracks'] = [course for course in courses(region)]
  153.  
  154.             elif valid_course(args[0]):
  155.                 course_id = args[0]
  156.                 course = course_name(course_id)
  157.                 parsed['folder_name'] = f'courses/{course}'
  158.                 parsed['file_name'] = year
  159.                 parsed['tracks'] = [(course_id, course)]
  160.  
  161.             else:
  162.                 print(ERROR['invalid_c_or_r'])
  163.  
  164.         return parsed
  165.  
  166.     def get_racing_type(self, code):
  167.         if code in {'j', '-j', 'jump', 'jumps'}:
  168.             return 'jumps'
  169.         if code in {'f', '-f', 'flat'}:
  170.             return 'flat'
  171.         return ''
  172.  
  173.     def opts(self, option):
  174.         if option == 'help':
  175.             print(help)
  176.         elif option in {'options', 'opt', '?'}:
  177.             print(options)
  178.         elif option in {'clear', 'cls', 'clr'}:
  179.             os.system('cls' if os.name == 'nt' else 'clear')
  180.         elif option in {'q', 'quit', 'exit'}:
  181.             sys.exit()
  182.         elif option == 'regions':
  183.             print_regions()
  184.         elif option == 'courses':
  185.             print_courses()
  186.  
  187.     def parse_date_request(self, args):
  188.         parsed = {}
  189.  
  190.         if check_date(args[1]):
  191.             parsed['dates'] = get_dates(args[1])
  192.             parsed['folder_name'] = 'dates/'
  193.             parsed['file_name'] = args[1].replace('/', '_')
  194.             parsed['type'] = ''
  195.  
  196.             if len(args) > 2:
  197.                 if valid_region(args[2]):
  198.                     parsed['region'] = args[2]
  199.                     parsed['folder_name'] += args[2]
  200.                 else:
  201.                     print(ERROR['invalid_region_int'])
  202.                     return {}
  203.  
  204.             if len(args) > 3:
  205.                 parsed['type'] = self.get_racing_type(args[3])
  206.         else:
  207.             print(ERROR['invalid_date'])
  208.  
  209.         return parsed
  210.  
  211.     def parse_year(self, year):
  212.         years = parse_years(year)
  213.         current_year = str(datetime.today().year)
  214.  
  215.         if not valid_years(years):
  216.             print(ERROR['invalid_year_int'] + current_year)
  217.  
  218.         return years
  219.  
  220.     def search(self, search_type, search_term, region):
  221.         if search_type == 'regions':
  222.             region_search(search_term)
  223.         else:
  224.             if valid_region(region):
  225.                 print_courses(region)
  226.             else:
  227.                 course_search(search_term)
  228.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement