Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/bioblend/util/__init__.py @ 0:d30785e31577 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:18:57 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d30785e31577 |
---|---|
1 import os | |
2 from collections import namedtuple | |
3 | |
4 | |
5 class Bunch(object): | |
6 """ | |
7 A convenience class to allow dict keys to be represented as object fields. | |
8 | |
9 The end result is that this allows a dict to be to be represented the same | |
10 as a database class, thus the two become interchangeable as a data source. | |
11 """ | |
12 def __init__(self, **kwargs): | |
13 self.__dict__.update(kwargs) | |
14 | |
15 def __repr__(self): | |
16 """ | |
17 Return the contents of the dict in a printable representation | |
18 """ | |
19 return str(self.__dict__) | |
20 | |
21 | |
22 def _file_stream_close(self): | |
23 """ | |
24 Close the open file descriptor associated with the FileStream | |
25 object. | |
26 """ | |
27 self[1].close() | |
28 | |
29 | |
30 FileStream = namedtuple("FileStream", ["name", "fd"]) | |
31 FileStream.close = _file_stream_close | |
32 | |
33 | |
34 def attach_file(path, name=None): | |
35 """ | |
36 Attach a path to a request payload object. | |
37 | |
38 :type path: str | |
39 :param path: Path to file to attach to payload. | |
40 | |
41 :type name: str | |
42 :param name: Name to give file, if different than actual pathname. | |
43 | |
44 :rtype: object | |
45 :return: Returns an object compatible with requests post operation and | |
46 capable of being closed with a ``close()`` method. | |
47 """ | |
48 if name is None: | |
49 name = os.path.basename(path) | |
50 attachment = FileStream(name, open(path, "rb")) | |
51 return attachment | |
52 | |
53 | |
54 __all__ = ( | |
55 'Bunch', | |
56 'attach_file', | |
57 ) |