8000 Moved more functions to logger · helllma/python-for-android@2d76849 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d76849

Browse files
committed
Moved more functions to logger
1 parent 5fcc098 commit 2d76849

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

pythonforandroid/logger.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11

22
import logging
3-
from sys import stdout, stderr, platform
3+
import os
4+
import re
5+
import sh
6+
from sys import stdout, stderr
7+
from math import log10
48
from collections import defaultdict
59
from colorama import Style as Colo_Style, Fore as Colo_Fore
610

11+
712
class LevelDifferentiatingFormatter(logging.Formatter):
813
def format(self, record):
914
if record.levelno > 30:
@@ -74,6 +79,19 @@ def info_notify(s):
7479
Err_Style.RESET_ALL))
7580

7681

82+
def shorten_string(string, max_width):
83+
''' make limited length string in form:
84+
"the string is very lo...(and 15 more)"
85+
'''
86+
string_len = len(string)
87+
if string_len <= max_width:
88+
return string
89+
visible = max_width - 16 - int(log10(string_len))
90+
# expected suffix len "...(and XXXXX more)"
91+
return ''.join((string[:visible], '...(and ', str(string_len - visible),
92+
' more)'))
93+
94+
7795
def shprint(command, *args, **kwargs):
7896
'''Runs the command (which should be an sh.Command instance), while
7997
logging the output.'''
@@ -114,22 +132,22 @@ def shprint(command, *args, **kwargs):
114132
'\t', ' ').replace(
115133
'\b', ' ').rstrip()
116134
if msg:
117-
sys.stdout.write(u'{}\r{}{:<{width}}'.format(
135+
stdout.write(u'{}\r{}{:<{width}}'.format(
118136
Err_Style.RESET_ALL, msg_hdr,
119137
shorten_string(msg, msg_width), width=msg_width))
120-
sys.stdout.flush()
138+
stdout.flush()
121139
need_closing_newline = True
122140
else:
123141
logger.debug(''.join(['\t', line.rstrip()]))
124142
if need_closing_newline:
125-
sys.stdout.write('{}\r{:>{width}}\r'.format(
143+
stdout.write('{}\r{:>{width}}\r'.format(
126144
Err_Style.RESET_ALL, ' ', width=(columns - 1)))
127-
sys.stdout.flush()
145+
stdout.flush()
128146
except sh.ErrorReturnCode as err:
129147
if need_closing_newline:
130-
sys.stdout.write('{}\r{:>{width}}\r'.format(
148+
stdout.write('{}\r{:>{width}}\r'.format(
131149
Err_Style.RESET_ALL, ' ', width=(columns - 1)))
132-
sys.stdout.flush()
150+
stdout.flush()
133151
if tail_n or filter_in or filter_out:
134152
def printtail(out, name, forecolor, tail_n=0,
135153
re_filter_in=None, re_filter_out=None):
@@ -144,7 +162,8 @@ def printtail(out, name, forecolor, tail_n=0,
144162
else:
145163
info('{} (last {} lines of {}):\n{}\t{}{}'.format(
146164
name, tail_n, len(lines),
147-
forecolor, '\t\n'.join(lines[-tail_n:]), Out_Fore.RESET))
165+
forecolor, '\t\n'.join(lines[-tail_n:]),
166+
Out_Fore.RESET))
148167
printtail(err.stdout, 'STDOUT', Out_Fore.YELLOW, tail_n,
149168
re.compile(filter_in) if filter_in else None,
150169
re.compile(filter_out) if filter_out else None)
@@ -156,7 +175,8 @@ def printtail(out, name, forecolor, tail_n=0,
156175
Err_Fore.YELLOW, Err_Fore.RESET, "\n".join(
157176
"set {}={}".format(n, v) for n, v in env.items())))
158177
info("{}COMMAND:{}\ncd {} && {} {}\n".format(
159-
Err_Fore.YELLOW, Err_Fore.RESET, getcwd(), command, ' '.join(args)))
178+
Err_Fore.YELLOW, Err_Fore.RESET, os.getcwd(), command,
179+
' '.join(args)))
160180
warning("{}ERROR: {} failed!{}".format(
161181
Err_Fore.RED, command, Err_Fore.RESET))
162182
exit(1)

pythonforandroid/toolchain.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import shutil
2323
import re
2424
import imp
25-
import contextlib
2625
import logging
2726
import shlex
2827
from copy import deepcopy
@@ -85,19 +84,6 @@ def pretty_log_dists(dists, log_func=info):
8584
log_func('\t' + line)
8685

8786

88-
def shorten_string(string, max_width):
89-
''' make limited length string in form:
90-
"the string is very lo...(and 15 more)"
91-
'''
92-
string_len = len(string)
93-
if string_len <= max_width:
94-
return string
95-
visible = max_width - 16 - int(log10(string_len))
96-
# expected suffix len "...(and XXXXX more)"
97-
return ''.join((string[:visible], '...(and ', str(string_len - visible),
98-
' more)'))
99-
100-
10187

10288
# shprint(sh.ls, '-lah')
10389
# exit(1)

0 commit comments

Comments
 (0)
0