/
util.py
executable file
·92 lines (71 loc) · 1.9 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#t!/usr/bin/env python2
# Copyright 2016 Andy Chu. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
"""
util.py - Common infrastructure.
"""
from __future__ import print_function
from mycpp import mylib
from typing import Any
class UserExit(Exception):
"""For explicit 'exit'."""
def __init__(self, status):
# type: (int) -> None
self.status = status
if mylib.PYTHON:
class HistoryError(Exception):
def __init__(self, msg, *args):
# type: (str, *Any) -> None
Exception.__init__(self)
self.msg = msg
self.args = args
def UserErrorString(self):
# type: () -> str
out = 'history: '
if self.args:
out += self.msg % self.args
else:
out += self.msg
return out
class _DebugFile(object):
def __init__(self):
# type: () -> None
pass
def log(self, msg, *args):
# type: (str, *Any) -> None
pass
def write(self, s):
# type: (str) -> None
pass
def isatty(self):
# type: () -> bool
return False
class NullDebugFile(_DebugFile):
def __init__(self):
# type: () -> None
"""Empty constructor for mycpp."""
_DebugFile.__init__(self)
class DebugFile(_DebugFile):
def __init__(self, f):
# type: (mylib.Writer) -> None
_DebugFile.__init__(self)
self.f = f
def log(self, msg, *args):
# type: (str, *Any) -> None
if mylib.PYTHON: # remove dynamic format
if args:
msg = msg % args
self.f.write(msg)
self.f.write('\n')
self.f.flush() # need to see it interactively
# These two methods are for node.PrettyPrint()
def write(self, s):
# type: (str) -> None
self.f.write(s)
def isatty(self):
# type: () -> bool
return self.f.isatty()