Conditions | 11 |
Total Lines | 73 |
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 fetch_job() 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 | ''' |
||
85 | def fetch_job(config): |
||
86 | ''' |
||
87 | Fetch any available work from the OpenSubmit server and |
||
88 | return an according job object. |
||
89 | |||
90 | Returns None if no work is available. |
||
91 | |||
92 | Errors are reported by this function directly. |
||
93 | ''' |
||
94 | url = "%s/jobs/?Secret=%s&UUID=%s" % (config.get("Server", "url"), |
||
95 | config.get("Server", "secret"), |
||
96 | config.get("Server", "uuid")) |
||
97 | |||
98 | try: |
||
99 | # Fetch information from server |
||
100 | result = urlopen(url) |
||
101 | headers = result.info() |
||
102 | if not compatible_api_version(headers["APIVersion"]): |
||
103 | # No proper reporting possible, so only logging. |
||
104 | logger.error("Incompatible API version. Please update OpenSubmit.") |
||
105 | return None |
||
106 | |||
107 | if headers["Action"] == "get_config": |
||
108 | # The server does not know us, |
||
109 | # so it demands registration before hand. |
||
110 | logger.info("Machine unknown on server, sending registration ...") |
||
111 | send_hostinfo(config) |
||
112 | return None |
||
113 | |||
114 | # Create job object with information we got |
||
115 | from .job import Job |
||
116 | job = Job(config) |
||
117 | |||
118 | job.submitter_name = headers['SubmitterName'] |
||
119 | job.author_names = headers['AuthorNames'] |
||
120 | job.submitter_studyprogram = headers['SubmitterStudyProgram'] |
||
121 | job.course = headers['Course'] |
||
122 | job.assignment = headers['Assignment'] |
||
123 | job.action = headers["Action"] |
||
124 | job.file_id = headers["SubmissionFileId"] |
||
125 | job.sub_id = headers["SubmissionId"] |
||
126 | job.file_name = headers["SubmissionOriginalFilename"] |
||
127 | job.submitter_student_id = headers["SubmitterStudentId"] |
||
128 | if "Timeout" in headers: |
||
129 | job.timeout = int(headers["Timeout"]) |
||
130 | if "PostRunValidation" in headers: |
||
131 | job.validator_url = headers["PostRunValidation"] |
||
132 | job.working_dir = create_working_dir(config, job.sub_id) |
||
133 | |||
134 | # Store submission in working directory |
||
135 | submission_fname = job.working_dir + job.file_name |
||
136 | with open(submission_fname, 'wb') as target: |
||
137 | target.write(result.read()) |
||
138 | assert(os.path.exists(submission_fname)) |
||
139 | |||
140 | # Store validator package in working directory |
||
141 | validator_fname = job.working_dir + 'download.validator' |
||
142 | fetch(job.validator_url, validator_fname) |
||
143 | |||
144 | try: |
||
145 | prepare_working_directory(job, submission_fname, validator_fname) |
||
146 | except JobException as e: |
||
147 | job.send_fail_result(e.info_student, e.info_tutor) |
||
148 | return None |
||
149 | logger.debug("Got job: " + str(job)) |
||
150 | return job |
||
151 | except HTTPError as e: |
||
152 | if e.code == 404: |
||
153 | logger.debug("Nothing to do.") |
||
154 | return None |
||
155 | except URLError as e: |
||
156 | logger.error("Error while contacting {0}: {1}".format(url, str(e))) |
||
157 | return None |
||
158 | |||
195 |