1 # Copyright 2018 Red Hat, Inc. and others. All Rights Reserved.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
21 logger = logging.getLogger("mdsal.request")
24 def debug_print(fname, text1, data):
25 logger.debug("%s: request: %s: processed %d lines", fname, text1, len(data))
26 # logger.debug("%s:\n%s", fname, json.dumps(data))
27 logger.debug("%s:\n%s", fname, json.dumps(data, indent=4, separators=(',', ': ')))
30 def get(url, user, pw):
32 resp = requests.get(url, auth=(user, pw))
33 except requests.exceptions.RequestException:
34 logger.exception("Failed to get url %s", url)
40 logger.exception("Failed to get url %s", url)
43 if logger.isEnabledFor(logging.DEBUG):
44 debug_print("get", url, data)
48 def read_file(filename):
49 if os.path.isfile(filename) is False:
52 with open(filename) as json_file:
53 data = json.load(json_file)
54 if logger.isEnabledFor(logging.DEBUG):
55 debug_print("read_file", filename, data)
59 def write_file(filename, data, pretty_print=False):
61 os.makedirs(os.path.dirname(filename))
62 except OSError as exception:
63 if exception.errno != errno.EEXIST:
66 with open(filename, 'w') as fp:
68 json.dump(data, fp, indent=4, separators=(',', ': '))
71 if logger.isEnabledFor(logging.DEBUG):
72 debug_print("write_file", filename, data)