Transform a page / xref specification into a list of integers. Args ---- rlist: (str) the specification limit: maximum number, i.e. number of pages, number of objects what: a string to be used in error messages Returns ------- A list of integers repre
(rlist, limit, what="page")
| 108 | |
| 109 | |
| 110 | def get_list(rlist, limit, what="page"): |
| 111 | """Transform a page / xref specification into a list of integers. |
| 112 | |
| 113 | Args |
| 114 | ---- |
| 115 | rlist: (str) the specification |
| 116 | limit: maximum number, i.e. number of pages, number of objects |
| 117 | what: a string to be used in error messages |
| 118 | Returns |
| 119 | ------- |
| 120 | A list of integers representing the specification. |
| 121 | """ |
| 122 | N = str(limit - 1) |
| 123 | rlist = rlist.replace("N", N).replace(" ", "") |
| 124 | rlist_arr = rlist.split(",") |
| 125 | out_list = [] |
| 126 | for seq, item in enumerate(rlist_arr): |
| 127 | n = seq + 1 |
| 128 | if item.isdecimal(): # a single integer |
| 129 | i = int(item) |
| 130 | if 1 <= i < limit: |
| 131 | out_list.append(int(item)) |
| 132 | else: |
| 133 | sys.exit(f"bad {what} specification at item {n:d}") |
| 134 | continue |
| 135 | try: # this must be a range now, and all of the following must work: |
| 136 | i1, i2 = item.split("-") # will fail if not 2 items produced |
| 137 | i1 = int(i1) # will fail on non-integers |
| 138 | i2 = int(i2) |
| 139 | except Exception: |
| 140 | sys.exit(f"bad {what} range specification at item {n:d}") |
| 141 | |
| 142 | if not (1 <= i1 < limit and 1 <= i2 < limit): |
| 143 | sys.exit(f"bad {what} range specification at item {n:d}") |
| 144 | |
| 145 | if i1 == i2: # just in case: a range of equal numbers |
| 146 | out_list.append(i1) |
| 147 | continue |
| 148 | |
| 149 | if i1 < i2: # first less than second |
| 150 | out_list += list(range(i1, i2 + 1)) |
| 151 | else: # first larger than second |
| 152 | out_list += list(range(i1, i2 - 1, -1)) |
| 153 | |
| 154 | return out_list |
| 155 | |
| 156 | |
| 157 | def show(args): |