comparison env/lib/python3.9/site-packages/bioblend/util/__init__.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import os
2 from collections import namedtuple
3
4
5 class Bunch:
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 )