| Conditions | 22 |
| Total Lines | 120 |
| Code Lines | 91 |
| 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 exabgp.application.control.Control.loop() 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 | """ |
||
| 120 | def loop (self): |
||
| 121 | try: |
||
| 122 | self.r_pipe = os.open(self.recv, os.O_RDWR | os.O_NONBLOCK | os.O_EXCL) |
||
| 123 | except OSError: |
||
| 124 | self.terminate() |
||
| 125 | |||
| 126 | standard_in = sys.stdin.fileno() |
||
| 127 | standard_out = sys.stdout.fileno() |
||
| 128 | |||
| 129 | def monitor (function): |
||
| 130 | def wrapper (*args): |
||
| 131 | # print >> sys.stderr, "%s(%s)" % (function.func_name,','.join([str(_).replace('\n','\\n') for _ in args])) |
||
| 132 | r = function(*args) |
||
| 133 | # print >> sys.stderr, "%s -> %s" % (function.func_name,str(r)) |
||
| 134 | return r |
||
| 135 | return wrapper |
||
| 136 | |||
| 137 | @monitor |
||
| 138 | def std_reader (number): |
||
| 139 | try: |
||
| 140 | return os.read(standard_in,number) |
||
| 141 | except OSError as exc: |
||
| 142 | if exc.errno in error.block: |
||
| 143 | return '' |
||
| 144 | sys.exit(1) |
||
| 145 | |||
| 146 | @monitor |
||
| 147 | def std_writer (line): |
||
| 148 | try: |
||
| 149 | return os.write(standard_out,line) |
||
| 150 | except OSError as exc: |
||
| 151 | if exc.errno in error.block: |
||
| 152 | return 0 |
||
| 153 | sys.exit(1) |
||
| 154 | |||
| 155 | @monitor |
||
| 156 | def fifo_reader (number): |
||
| 157 | try: |
||
| 158 | return os.read(self.r_pipe,number) |
||
| 159 | except OSError as exc: |
||
| 160 | if exc.errno in error.block: |
||
| 161 | return '' |
||
| 162 | sys.exit(1) |
||
| 163 | |||
| 164 | @monitor |
||
| 165 | def fifo_writer (line): |
||
| 166 | pipe,nb = None,0 |
||
| 167 | try: |
||
| 168 | pipe = os.open(self.send, os.O_WRONLY | os.O_NONBLOCK | os.O_EXCL) |
||
| 169 | self.no_buffer(pipe) |
||
| 170 | except OSError: |
||
| 171 | time.sleep(0.05) |
||
| 172 | return 0 |
||
| 173 | if pipe is not None: |
||
| 174 | try: |
||
| 175 | nb = os.write(pipe,line) |
||
| 176 | except OSError: |
||
| 177 | pass |
||
| 178 | try: |
||
| 179 | os.close(pipe) |
||
| 180 | except OSError: |
||
| 181 | pass |
||
| 182 | return nb |
||
| 183 | |||
| 184 | read = { |
||
| 185 | standard_in: std_reader, |
||
| 186 | self.r_pipe: fifo_reader, |
||
| 187 | } |
||
| 188 | |||
| 189 | write = { |
||
| 190 | standard_in: fifo_writer, |
||
| 191 | self.r_pipe: std_writer, |
||
| 192 | } |
||
| 193 | |||
| 194 | backlog = { |
||
| 195 | standard_in: deque(), |
||
| 196 | self.r_pipe: deque(), |
||
| 197 | } |
||
| 198 | |||
| 199 | store = { |
||
| 200 | standard_in: b'', |
||
| 201 | self.r_pipe: b'', |
||
| 202 | } |
||
| 203 | |||
| 204 | def consume (source): |
||
| 205 | if not backlog[source] and b'\n' not in store[source]: |
||
|
|
|||
| 206 | store[source] += read[source](1024) |
||
| 207 | else: |
||
| 208 | backlog[source].append(read[source](1024)) |
||
| 209 | # assuming a route takes 80 chars, 100 Mb is over 1Millions routes |
||
| 210 | # something is really wrong if it was not consummed |
||
| 211 | if len(backlog) > 100*mb: |
||
| 212 | sys.stderr.write('using too much memory - exiting') |
||
| 213 | sys.exit(1) |
||
| 214 | |||
| 215 | reading = [standard_in, self.r_pipe] |
||
| 216 | |||
| 217 | while True: |
||
| 218 | ready = self.read_on(reading) |
||
| 219 | |||
| 220 | # command from user |
||
| 221 | if self.r_pipe in ready: |
||
| 222 | consume(self.r_pipe) |
||
| 223 | if standard_in in ready: |
||
| 224 | consume(standard_in) |
||
| 225 | |||
| 226 | for source in reading: |
||
| 227 | while b'\n' in store[source]: |
||
| 228 | line,_ = store[source].split(b'\n',1) |
||
| 229 | # sys.stderr.write(str(line).replace('\n','\\n') + '\n') |
||
| 230 | # sys.stderr.flush() |
||
| 231 | sent = write[source](line + b'\n') |
||
| 232 | # sys.stderr.write('sent %d\n' % sent) |
||
| 233 | # sys.stderr.flush() |
||
| 234 | if sent: |
||
| 235 | store[source] = store[source][sent:] |
||
| 236 | continue |
||
| 237 | break |
||
| 238 | if backlog[source]: |
||
| 239 | store[source] += backlog[source].popleft() |
||
| 240 | |||
| 272 |