Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/async_fetch_value.py |
| diff --git a/chrome/common/extensions/docs/server2/async_fetch_value.py b/chrome/common/extensions/docs/server2/async_fetch_value.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..63ee99958eac97ddef6c78fb1b9148e0559c5b9f |
| --- /dev/null |
| +++ b/chrome/common/extensions/docs/server2/async_fetch_value.py |
| @@ -0,0 +1,41 @@ |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import xml.dom.minidom as xml |
| + |
| +class AsyncFetchValue(object): |
|
not at google - send to devlin
2012/07/18 10:39:15
Hm, sorry, this is pretty tied into the FileSystem
cduvall
2012/07/18 21:26:10
Done.
|
| + def __init__(self, paths, fetcher): |
| + self._fetches = [] |
| + self._value = {} |
| + self._error = None |
| + for path in paths: |
| + rpc = fetcher.create_rpc() |
| + if path.endswith('/'): |
| + rpc.callback = self._MakeCallback(self._ListDir, rpc, path) |
| + else: |
| + rpc.callback = self._MakeCallback(lambda file_: file_, rpc, path) |
| + fetcher.make_fetch_call(rpc, path) |
| + self._fetches.append(rpc) |
| + |
| + def _ListDir(self, directory): |
| + dom = xml.parseString(directory) |
| + files = [elem.childNodes[0].data for elem in dom.getElementsByTagName('a')] |
| + if '..' in files: |
| + files.remove('..') |
| + return files |
| + |
| + def _MakeCallback(self, f, rpc, path): |
| + return lambda: self._Callback(rpc, path, f) |
| + |
| + def _Callback(self, rpc, key, f): |
| + result = rpc.get_result() |
| + self._value[key] = f(result.content) |
| + |
| + def Get(self): |
| + for fetch in self._fetches: |
| + fetch.wait() |
| + if self._error is not None: |
|
not at google - send to devlin
2012/07/18 10:39:15
what's wrong with !=
cduvall
2012/07/18 21:26:10
From PEP 8: "Comparisons to singletons like None s
not at google - send to devlin
2012/07/19 03:55:18
wtf python. "is not" is an operator? That could eq
cduvall
2012/07/19 17:18:28
haha {{/rant}}
|
| + raise self._error |
| + return self._value |
| + |