Conditions | 15 |
Total Lines | 81 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like make_offset() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | #!/usr/bin/env python3 |
||
35 | def make_offset(files, folder=None): |
||
36 | """ |
||
37 | Create magic offset file for use in autoloader creation. |
||
38 | Cap.exe MUST match separator version. |
||
39 | Version defined in :data:`bbarchivist.bbconstants.CAP.version`. |
||
40 | |||
41 | :param files: List of 1-6 signed files. |
||
42 | :type files: list(str) |
||
43 | |||
44 | :param folder: Working folder. Optional. Default is local. |
||
45 | :type folder: str |
||
46 | """ |
||
47 | if folder is None: |
||
48 | folder = os.getcwd() |
||
49 | capfile = utilities.grab_cap() |
||
50 | filelist = [file for file in files if file] |
||
51 | filecount = len(filelist) |
||
52 | fcount = b'0' + bytes(str(filecount), 'ascii') |
||
53 | # immutable things |
||
54 | scaff = b'at9dFE5LTEdOT0hHR0lTCxcKDR4MFFMtPiU6LT0zPjs6Ui88U05GTVFOSUdRTlFOT3BwcJzVxZec1cWXnNXFlw==' |
||
55 | separator = base64.b64decode(scaff) |
||
56 | password = binascii.unhexlify(b'0' * 160) |
||
57 | pad = b'\x00' # 1x, 2x or 8x |
||
58 | filepad = binascii.unhexlify(fcount) # 01-06 |
||
59 | trailers = binascii.unhexlify(b'00' * (7 - filecount)) # 00, 1-6x |
||
60 | capsize = os.path.getsize(capfile) |
||
61 | if not filecount: # we need at least one file |
||
62 | raise SystemExit |
||
63 | first = str(glob.glob(filelist[0])[0]) |
||
64 | firstsize = os.path.getsize(first) # required |
||
65 | if filecount >= 2: |
||
66 | second = str(glob.glob(filelist[1])[0]) |
||
67 | secondsize = os.path.getsize(second) |
||
68 | if filecount >= 3: |
||
69 | third = str(glob.glob(filelist[2])[0]) |
||
70 | thirdsize = os.path.getsize(third) |
||
71 | if filecount >= 4: |
||
72 | fourth = str(glob.glob(filelist[3])[0]) |
||
73 | fourthsize = os.path.getsize(fourth) |
||
74 | if filecount >= 5: |
||
75 | fifth = str(glob.glob(filelist[4])[0]) |
||
76 | fifthsize = os.path.getsize(fifth) |
||
77 | # start of first file; length of cap + length of offset |
||
78 | beginlength = len(separator) + len(password) + 64 |
||
79 | firstoffset = beginlength + capsize |
||
80 | firststart = ghetto_convert(firstoffset) |
||
81 | secondstart = thirdstart = fourthstart = fifthstart = sixthstart = pad * 8 |
||
82 | if filecount >= 2: |
||
83 | secondoffset = firstoffset + firstsize # start of second file |
||
84 | secondstart = ghetto_convert(secondoffset) |
||
85 | if filecount >= 3: |
||
86 | thirdoffset = secondoffset + secondsize # start of third file |
||
87 | thirdstart = ghetto_convert(thirdoffset) |
||
88 | if filecount >= 4: |
||
89 | fourthoffset = thirdoffset + thirdsize # start of fourth file |
||
90 | fourthstart = ghetto_convert(fourthoffset) |
||
91 | if filecount >= 5: |
||
92 | fifthoffset = fourthoffset + fourthsize # start of fifth file |
||
93 | fifthstart = ghetto_convert(fifthoffset) |
||
94 | if filecount == 6: |
||
95 | sixthoffset = fifthoffset + fifthsize # start of sixth file |
||
96 | sixthstart = ghetto_convert(sixthoffset) |
||
97 | makeuplen = 64 - 6 * len(pad * 8) - 2 * len(pad * 2) - 2 * \ |
||
98 | len(pad) - len(trailers) - len(filepad) |
||
99 | makeup = b'\x00' * makeuplen # pad to match offset begin |
||
100 | with open(os.path.join(folder, "offset.hex"), "wb") as file: |
||
101 | file.write(separator) |
||
102 | file.write(password) |
||
103 | file.write(filepad) |
||
104 | file.write(pad * 2) |
||
105 | file.write(pad) |
||
106 | file.write(firststart) |
||
107 | file.write(secondstart) |
||
108 | file.write(thirdstart) |
||
109 | file.write(fourthstart) |
||
110 | file.write(fifthstart) |
||
111 | file.write(sixthstart) |
||
112 | file.write(pad) |
||
113 | file.write(pad * 2) |
||
114 | file.write(trailers) |
||
115 | file.write(makeup) |
||
116 | |||
175 |