Skip to content

Commit

Permalink
Python 3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Yen Chi Hsuan committed Apr 8, 2019
1 parent 4109ad3 commit 0b663dd
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 40 deletions.
28 changes: 18 additions & 10 deletions logcat-color
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
from __future__ import print_function, unicode_literals
"""
logcat-color
Expand Down Expand Up @@ -136,13 +137,19 @@ class LogcatColor(object):
if options.config and not os.path.isfile(options.config):
parser.error("Config file does not exist: %s" % options.config)

self.input = sys.stdin
try:
self.input = sys.stdin.buffer
except AttributeError:
self.input = sys.stdin
if options.input:
self.input = open(options.input, "r")
self.input = open(options.input, "rb")

self.output = sys.stdout
try:
self.output = sys.stdout.buffer
except AttributeError:
self.output = sys.stdout
if options.output:
self.output = open(options.output, "w")
self.output = open(options.output, "wb")

self.adb_device = options.adb_device
self.logcat_args = options.logcat_args or []
Expand Down Expand Up @@ -208,16 +215,17 @@ class LogcatColor(object):
adb_command.extend(self.get_logcat_args())
try:
self.input = Popen(adb_command, stdout=PIPE).stdout
except OSError, e:
except OSError as e:
if e.errno == errno.ENOENT:
print >>sys.stderr, \
print(
'Error, adb could not be found using: "%s"\n' \
'To fix this: \n' \
' 1) Add the directory containing adb to your PATH\n' \
' 2) Set the ADB environment variable\n' \
' 3) Set "adb" in ~/.logcat-color' % adb_command[0]
' 3) Set "adb" in ~/.logcat-color' % adb_command[0],
file=sys.stderr)
else:
print >>sys.stderr, 'Could not run ADB: %s' % str(e)
print('Could not run ADB: %s' % str(e), file=sys.stderr)
sys.exit(e.errno)

def init_reader(self):
Expand All @@ -242,7 +250,7 @@ class LogcatColor(object):
self.wait_for_device()
self.start_logcat()
self.init_reader()
except KeyboardInterrupt, e:
except KeyboardInterrupt as e:
pass

WAIT_FOR_DEVICE = Fore.WHITE + Back.BLACK + Style.DIM + \
Expand All @@ -258,7 +266,7 @@ class LogcatColor(object):
if self.adb_device:
device_str = "\"%s\" " % self.adb_device

print self.WAIT_FOR_DEVICE % device_str
print(self.WAIT_FOR_DEVICE % device_str)
check_call(command)

if __name__ == "__main__":
Expand Down
7 changes: 4 additions & 3 deletions logcatcolor/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
Columns for displaying logcat log data
"""
from __future__ import unicode_literals
import colorama
from colorama import Fore, Back, Style
import StringIO
from io import StringIO

colorama.init()

Expand Down Expand Up @@ -73,7 +74,7 @@ def __init__(self, layout):
tag_colors = layout.profile.tag_colors

self.tag_colors = tag_colors or {}
self.last_used = self.COLOR_MAP.values()[:]
self.last_used = list(self.COLOR_MAP.values())

# This will allocate a unique format for the given tag since we dont have
# very many colors, we always keep track of the LRU
Expand Down Expand Up @@ -134,7 +135,7 @@ def format(self, message):
if not self.width:
return message

messagebuf = StringIO.StringIO()
messagebuf = StringIO()
current = 0
while current < len(message):
next = min(current + self.width, len(message))
Expand Down
5 changes: 4 additions & 1 deletion logcatcolor/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
from logcatcolor.column import TagColumn
from logcatcolor.profile import Profile
import os
Expand Down Expand Up @@ -27,7 +28,9 @@ def __init__(self, options):
if os.path.exists(self.path) and os.path.isfile(self.path):
# config file is just a python script that globals are imported from
try:
execfile(self.path, self.config)
with open(self.path) as f:
code = compile(f.read(), os.path.basename(self.path), 'exec')
exec(code, self.config)
except:
self.report_config_error()
sys.exit(1)
Expand Down
5 changes: 3 additions & 2 deletions logcatcolor/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Support for reading various logcat logging formats into an easier to consume
data map.
"""
from __future__ import unicode_literals
import re

def format(cls):
Expand All @@ -32,7 +33,7 @@ def match(self, line):
if not match:
return False

for name, value in match.groupdict().iteritems():
for name, value in match.groupdict().items():
self.data[name] = value.strip()
return True

Expand Down Expand Up @@ -130,7 +131,7 @@ def detect_format(lines):
if Format.MARKER_REGEX.match(line):
continue

for name, regex in Format.REGEXES.iteritems():
for name, regex in Format.REGEXES.items():
if regex.match(line):
return name

Expand Down
3 changes: 2 additions & 1 deletion logcatcolor/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
Layouts for mapping logcat log data into a colorful terminal interface
"""
from __future__ import unicode_literals
from colorama import Fore, Back, Style
from logcatcolor.column import *
from logcatcolor.format import Format
import re
from cStringIO import StringIO
from io import StringIO

def layout(cls):
Layout.TYPES[cls.NAME] = cls
Expand Down
1 change: 1 addition & 0 deletions logcatcolor/profile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
import re

RegexType = type(re.compile(""))
Expand Down
15 changes: 10 additions & 5 deletions logcatcolor/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
Logcat I/O stream readers and helpers
"""
from __future__ import unicode_literals
import asyncore
import asynchat
from cStringIO import StringIO
from io import StringIO
import fcntl
import inspect
from logcatcolor.format import Format, detect_format
Expand All @@ -19,7 +20,7 @@

# Parts copied from asyncore.file_dispatcher
class FileLineReader(asynchat.async_chat):
LINE_TERMINATOR = "\n"
LINE_TERMINATOR = b"\n"

def __init__(self, fd):
asynchat.async_chat.__init__(self)
Expand All @@ -45,7 +46,7 @@ def set_file(self, fd):
fcntl.fcntl(fd, fcntl.F_SETFL, flags)

def collect_incoming_data(self, data):
self.log_buffer.write(data)
self.log_buffer.write(data.decode('utf-8'))

def found_terminator(self):
line = self.log_buffer.getvalue()
Expand All @@ -71,6 +72,10 @@ def __init__(self, file, config, profile=None, format=None, layout=None,
self.profile = profile
self.width = width
self.writer = writer or sys.stdout
try:
self.writer = self.writer.buffer
except AttributeError:
pass

self.format = None
if format is not None:
Expand Down Expand Up @@ -121,7 +126,7 @@ def layout_line(self, line):
if Format.MARKER_REGEX.match(line):
result = self.layout.layout_marker(line)
if result:
self.writer.write(result)
self.writer.write(result.encode('utf-8'))
return

try:
Expand All @@ -132,6 +137,6 @@ def layout_line(self, line):
if not result:
return

self.writer.write(result + "\n")
self.writer.write((result + "\n").encode('utf-8'))
finally:
self.format.data.clear()
8 changes: 5 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import codecs
import json
import sys
from setuptools import setup, find_packages

setup_data = json.load(open("setup.json", "r"))

# real classy distutils, name / version have to be ascii encoded :(
for ascii_key in ("name", "version"):
setup_data[ascii_key] = setup_data[ascii_key].encode("ascii")
if sys.version_info[0] == 2:
# real classy distutils, name / version have to be ascii encoded :(
for ascii_key in ("name", "version"):
setup_data[ascii_key] = setup_data[ascii_key].encode("ascii")

setup_data["packages"] = find_packages()
setup(**setup_data)
10 changes: 7 additions & 3 deletions test/common.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
from __future__ import unicode_literals
import json
import os
import sys

this_dir = os.path.abspath(os.path.dirname(__file__))
top_dir = os.path.dirname(this_dir)
logcat_color = os.path.join(top_dir, "logcat-color")
execfile(logcat_color)
with open(logcat_color) as f:
exec(compile(f.read(), "logcat-color", 'exec'))

filter_results = os.path.join(this_dir, ".filter_results")
mock_adb = os.path.join(this_dir, "mock-adb")

class MockObject(object):
def __init__(self, **kwargs):
for key, value in kwargs.iteritems():
for key, value in kwargs.items():
setattr(self, key, value)

class MockAdbLogcatColor(LogcatColor):
Expand All @@ -25,6 +28,7 @@ def __init__(self, log, results, args=None, max_wait_count=None):
def get_adb_args(self):
adb_args = LogcatColor.get_adb_args(self)
adb_args[0:1] = [mock_adb, "--log", self.log, "--results", self.results]
adb_args = [sys.executable] + adb_args
return adb_args

def wait_for_device(self):
Expand Down Expand Up @@ -56,6 +60,6 @@ def save_filter_results(name, data, result):
def read_filter_results():
results = {}
if os.path.exists(filter_results):
results = json.loads(open(filter_results, "r").read())
results = json.loads(open(filter_results, "rt").read())

return results
1 change: 1 addition & 0 deletions test/config_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
from common import *
from logcatcolor.column import *
from logcatcolor.config import *
Expand Down
1 change: 1 addition & 0 deletions test/format_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
from common import *
from logcatcolor.format import *
import unittest
Expand Down
24 changes: 14 additions & 10 deletions test/logcat_color_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import print_function, unicode_literals
import common
import json
import os
from StringIO import StringIO
from io import StringIO
from subprocess import Popen, PIPE
import sys
import tempfile
Expand Down Expand Up @@ -50,29 +51,32 @@ def start_logcat_color(self, *args, **kwargs):
piped_path = None
if "piped" in kwargs:
piped_path = kwargs["piped"]
piped = open(piped_path, "r").read()
piped = open(piped_path, "rb").read()
del kwargs["piped"]
elif "input" in kwargs:
piped = None
args[1:1] = ["--input", kwargs["input"]]
del kwargs["input"]

args = [sys.executable] + args

if self.DEBUG:
piped_debug = ""
if piped_path:
piped_debug = " < %s" % piped_path

print " ".join(args) + piped_debug
print(" ".join(args) + piped_debug)

self.proc = Popen(args, stdout=PIPE, stderr=PIPE, stdin=PIPE, **kwargs)
self.out, self.err = self.proc.communicate(piped)
self.out = self.out.decode('utf-8')

self.filter_results = common.read_filter_results()
if os.path.exists(common.filter_results):
os.unlink(common.filter_results)

if self.DEBUG and self.err:
print >>sys.stderr, self.err
print(self.err, file=sys.stderr)

@logcat_color_test(piped=BRIEF_LOG)
def test_piped_input(self):
Expand All @@ -85,7 +89,7 @@ def test_invalid_config(self):
@logcat_color_test("--plain", input=BRIEF_LOG)
def test_plain_logging(self):
self.assertEqual(self.proc.returncode, 0)
brief_data = open(BRIEF_LOG, "r").read()
brief_data = open(BRIEF_LOG, "rt").read()
self.assertEqual(self.out, brief_data)

@logcat_color_test("--plain", "brief_filter_fn",
Expand Down Expand Up @@ -132,8 +136,8 @@ def test_plain_logging_with_tag_filter(self):
@logcat_color_test("--plain", "--output", tmpout, input=BRIEF_LOG)
def test_file_output(self):
self.assertEqual(self.proc.returncode, 0)
brief_data = open(BRIEF_LOG, "r").read()
out_data = open(tmpout, "r").read()
brief_data = open(BRIEF_LOG, "rt").read()
out_data = open(tmpout, "rt").read()
self.assertEqual(out_data, brief_data)

def test_logcat_options_with_filters(self):
Expand Down Expand Up @@ -168,13 +172,13 @@ def test_stay_connected(self):
lc.loop()
self.assertEqual(lc.wait_count, 3)

results = json.loads(open(tmpout, "r").read())
results = json.loads(open(tmpout, "rt").read())
self.assertEqual(len(results), 6)

logcat_results = filter(lambda d: d["command"] == "logcat", results)
logcat_results = list(filter(lambda d: d["command"] == "logcat", results))
self.assertEqual(len(logcat_results), 3)

wait_results = filter(lambda d: d["command"] == "wait-for-device", results)
wait_results = list(filter(lambda d: d["command"] == "wait-for-device", results))
self.assertEquals(len(wait_results), 3)

for r in results:
Expand Down
5 changes: 3 additions & 2 deletions test/mock-adb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
from __future__ import unicode_literals
import argparse
import json
import os
Expand All @@ -8,7 +9,7 @@ import time
class MockAdb(object):
def __init__(self, args, command_args):
self.results_data = {}
for attr, value in args.__dict__.iteritems():
for attr, value in args.__dict__.items():
setattr(self, attr, value)
self.results_data[attr] = value

Expand All @@ -34,7 +35,7 @@ class MockAdb(object):
time.sleep(0.1)

def logcat(self):
print open(self.log, "r").read()
print(open(self.log, "r").read())

def main():
parser = argparse.ArgumentParser()
Expand Down
1 change: 1 addition & 0 deletions test/profile_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
from common import *
from logcatcolor.column import *
from logcatcolor.config import *
Expand Down
Loading

0 comments on commit 0b663dd

Please sign in to comment.