comparison env/lib/python3.9/site-packages/bioblend/__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 logging
2 import os
3
4 from bioblend.config import (
5 BioBlendConfigLocations,
6 Config,
7 )
8
9 # Current version of the library
10 __version__ = '0.15.0'
11
12 # default chunk size (in bytes) for reading remote data
13 try:
14 import resource
15 CHUNK_SIZE = resource.getpagesize()
16 except Exception:
17 CHUNK_SIZE = 4096
18
19
20 config = Config()
21
22
23 def get_version():
24 """
25 Returns a string with the current version of the library (e.g., "0.2.0")
26 """
27 return __version__
28
29
30 def init_logging():
31 """
32 Initialize BioBlend's logging from a configuration file.
33 """
34 for config_file in BioBlendConfigLocations:
35 try:
36 logging.config.fileConfig(os.path.expanduser(config_file))
37 except Exception:
38 pass
39
40
41 class NullHandler(logging.Handler):
42 def emit(self, record):
43 pass
44
45
46 # By default, do not force any logging by the library. If you want to see the
47 # log messages in your scripts, add the following to the top of your script:
48 # import logging
49 # logging.basicConfig(filename="bioblend.log", level=logging.DEBUG)
50 default_format_string = "%(asctime)s %(name)s [%(levelname)s]: %(message)s"
51 log = logging.getLogger('bioblend')
52 log.addHandler(NullHandler())
53 init_logging()
54
55 # Convenience functions to set logging to a particular file or stream
56 # To enable either of these, simply add the following at the top of a
57 # bioblend module:
58 # import bioblend
59 # bioblend.set_stream_logger(__name__)
60
61
62 def set_file_logger(name, filepath, level=logging.INFO, format_string=None):
63 global log
64 if not format_string:
65 format_string = default_format_string
66 logger = logging.getLogger(name)
67 logger.setLevel(level)
68 fh = logging.FileHandler(filepath)
69 fh.setLevel(level)
70 formatter = logging.Formatter(format_string)
71 fh.setFormatter(formatter)
72 logger.addHandler(fh)
73 log = logger
74
75
76 def set_stream_logger(name, level=logging.DEBUG, format_string=None):
77 global log
78 if not format_string:
79 format_string = default_format_string
80 logger = logging.getLogger(name)
81 logger.setLevel(level)
82 fh = logging.StreamHandler()
83 fh.setLevel(level)
84 formatter = logging.Formatter(format_string)
85 fh.setFormatter(formatter)
86 logger.addHandler(fh)
87 log = logger
88
89
90 class ConnectionError(Exception):
91 """
92 An exception class that is raised when unexpected HTTP responses come back.
93
94 Should make it easier to debug when strange HTTP things happen such as a
95 proxy server getting in the way of the request etc.
96 @see: body attribute to see the content of the http response
97 """
98 def __init__(self, message, body=None, status_code=None):
99 super().__init__(message)
100 self.body = body
101 self.status_code = status_code
102
103 def __str__(self):
104 return "{}: {}".format(self.args[0], self.body)