Coverage for jbank/management/commands/make_parse_format.py: 0%

1 import csv

2 import logging

3 from pprint import pprint

4 from typing import List

5 from django.core.management.base import CommandParser

6 from jutil.command import SafeCommand

7 from jutil.validators import variable_name_sanitizer

8

9 logger = logging.getLogger(__name__)

10

11

12 class Command(SafeCommand):

13 help = """

14 Reads CSV files and generates matching parse structures

15 for parsing line-based files.

16

17 Default format:

18

19 TITLE1,xxx

20 FIELD_NAME,CHAR_COUNT

21 FIELD_NAME,CHAR_COUNT

22 FIELD_NAME,CHAR_COUNT

23 [empty row]

24 TITLE2,xxx

25 FIELD_NAME,CHAR_COUNT

26 FIELD_NAME,CHAR_COUNT

27 FIELD_NAME,CHAR_COUNT

28 """

29

30 def add_arguments(self, parser: CommandParser):

31 parser.add_argument("--csv-file", type=str, required=True)

32 parser.add_argument("--key-col-index", type=int, default=0)

33 parser.add_argument("--count-col-index", type=int, default=1)

34 parser.add_argument("--verbose", action="store_true")

35

36 def do(self, *args, **kwargs): # pylint: disable=too-many-locals,too-many-branches

37 verbose = kwargs["verbose"]

38

39 lines: List[List[str]] = []

40 with open(kwargs["csv_file"], "rt", encoding="utf-8") as fp:

41 reader = csv.reader(fp, dialect="excel")

42 for line in reader:

43 lines.append(line)

44 if verbose:

45 print(line)

46

47 key_ix = kwargs["key_col_index"]

48 count_ix = kwargs["count_col_index"]

49 heading = ""

50 data = {}

51 for line in lines:

52 key = variable_name_sanitizer(line[key_ix]).lower()

53 if not key:

54 heading = ""

55 continue

56 if not heading:

57 heading = key.upper()

58 data[heading] = []

59 continue

60 n = int(line[count_ix]) if line[count_ix] else 0

61 data[heading].append((key, n))

62

63 if verbose:

64 pprint(data)

65

66 for heading, fields in data.items():

67 print(f"{heading}: List[Tuple[str, str, str]] = [")

68 for k, n in fields:

69 print(f' ("{k}", "X({n})", "P"),')

70 print("]\n")