diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-08-06 11:37:46 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-08-06 18:55:44 +0300 |
commit | 888db73b93aefe70d838d499f7f9cc43eee7372b (patch) | |
tree | dd8c08da03ac3ff4c1bb1d4121f5548010b8fa8f /yarns/lib.py | |
parent | edda45bd66a8d7c6bf2e5f3ec270237ac93b3d9d (diff) | |
download | ick2-888db73b93aefe70d838d499f7f9cc43eee7372b.tar.gz |
Start rewrite using Python 3, apifw, slog
apifw and slog are two libraries I've written for work. They make
writing RESTful HTTP JSON APIs easier.
Diffstat (limited to 'yarns/lib.py')
-rw-r--r-- | yarns/lib.py | 111 |
1 files changed, 0 insertions, 111 deletions
diff --git a/yarns/lib.py b/yarns/lib.py deleted file mode 100644 index fc4acfc..0000000 --- a/yarns/lib.py +++ /dev/null @@ -1,111 +0,0 @@ -import errno -import json -import os -import StringIO -import time - -import cliapp -import requests -import yaml -import yarnutils - - -datadir = os.environ['DATADIR'] -srcdir = os.environ['SRCDIR'] - -vars = yarnutils.Variables(datadir) - - -MAX_CAT_TIME = 5 # seconds -def cat(filename): - start = time.time() - while time.time() - start < MAX_CAT_TIME: - try: - with open(filename) as f: - data = f.read() - if len(data) == 0: - continue - return data - except (IOError, OSError) as e: - if e.errno == errno.ENOENT: - continue - raise - raise Exception("cat took more then %s seconds" % MAX_CAT_TIME) - - -def write(filename, content): - with open(filename, 'w') as f: - f.write(content) - - -def git(repo, *argv): - return cliapp.runcmd(['git'] + list(argv), cwd=repo) - - -def controller_url(port, path): - return 'http://localhost:{}{}'.format(port, path) - - -def request(method, url, body=None): - funcs = { - 'POST': requests.post, - 'PUT': requests.put, - 'GET': requests.get, - 'DELETE': requests.delete, - } - - headers = { - 'Content-Type': 'application/json', - } - - response = funcs[method]( - url, - headers=headers, - data=body, - ) - - return response.status_code, response.text - - -def parse_json(text): - return json.loads(text, object_pairs_hook=dictify) - - - -def dictify(pairs): - return { - stringify(key): stringify(value) - for key, value in pairs - } - - -def stringify(x): - if isinstance(x, unicode): - return str(x) - if isinstance(x, list): - return [stringify(y) for y in x] - if isinstance(x, dict): - return { - stringify(key): stringify(value) - for key, value in pairs - } - return x - - -def parse_yaml(text): - f = StringIO.StringIO(text) - return yaml.safe_load(stream=f) - - -def unescape(text): - def helper(text): - while text: - if text.startswith('\\n'): - skip = 2 - answer = '\n' - else: - skip = 1 - answer = text[0] - text = text[skip:] - yield answer - return ''.join(helper(text)) |