Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit 0a33045

Browse files
committed
Use package specific logger instead of the root one
So lib users can control it without affecting everything else.
1 parent 81cf14a commit 0a33045

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

samsungctl/__main__.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
from . import Remote
1414

1515

16+
_LOGGER = logging.getLogger(__package__)
17+
18+
1619
def _read_config():
1720
config = collections.defaultdict(lambda: None, {
1821
"name": "samsungctl",
@@ -53,7 +56,7 @@ def _read_config():
5356
config_json = json.load(config_file)
5457
except ValueError as e:
5558
message = "Warning: Could not parse the configuration file.\n %s"
56-
logging.warning(message, e)
59+
_LOGGER.warning(message, e)
5760
return config
5861

5962
config.update(config_json)
@@ -103,7 +106,7 @@ def main():
103106
config.update({k: v for k, v in vars(args).items() if v is not None})
104107

105108
if not config["host"]:
106-
logging.error("Error: --host must be set")
109+
_LOGGER.error("Error: --host must be set")
107110
return
108111

109112
try:
@@ -112,21 +115,21 @@ def main():
112115
remote.control(key)
113116

114117
if args.interactive:
115-
logging.getLogger().setLevel(logging.ERROR)
118+
_LOGGER.setLevel(logging.ERROR)
116119
from . import interactive
117120
interactive.run(remote)
118121
elif len(args.key) == 0:
119-
logging.warning("Warning: No keys specified.")
122+
_LOGGER.warning("Warning: No keys specified.")
120123
except exceptions.ConnectionClosed:
121-
logging.error("Error: Connection closed!")
124+
_LOGGER.error("Error: Connection closed!")
122125
except exceptions.AccessDenied:
123-
logging.error("Error: Access denied!")
126+
_LOGGER.error("Error: Access denied!")
124127
except exceptions.UnknownMethod:
125-
logging.error("Error: Unknown method '{}'".format(config["method"]))
128+
_LOGGER.error("Error: Unknown method '{}'".format(config["method"]))
126129
except socket.timeout:
127-
logging.error("Error: Timed out!")
130+
_LOGGER.error("Error: Timed out!")
128131
except OSError as e:
129-
logging.error("Error: %s", e.strerror)
132+
_LOGGER.error("Error: %s", e.strerror)
130133

131134

132135
if __name__ == "__main__":

samsungctl/remote_legacy.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55

66
from . import exceptions
7+
_LOGGER = logging.getLogger(__package__)
78

89

910
class RemoteLegacy():
@@ -27,7 +28,7 @@ def __init__(self, config):
2728
+ self._serialize_string(config["name"])
2829
packet = b"\x00\x00\x00" + self._serialize_string(payload, True)
2930

30-
logging.info("Sending handshake.")
31+
_LOGGER.info("Sending handshake.")
3132
self.connection.send(packet)
3233
self._read_response(True)
3334

@@ -42,7 +43,7 @@ def close(self):
4243
if self.connection:
4344
self.connection.close()
4445
self.connection = None
45-
logging.debug("Connection closed.")
46+
_LOGGER.debug("Connection closed.")
4647

4748
def control(self, key):
4849
"""Send a control command."""
@@ -52,7 +53,7 @@ def control(self, key):
5253
payload = b"\x00\x00\x00" + self._serialize_string(key)
5354
packet = b"\x00\x00\x00" + self._serialize_string(payload, True)
5455

55-
logging.info("Sending control command: %s", key)
56+
_LOGGER.info("Sending control command: %s", key)
5657
self.connection.send(packet)
5758
self._read_response()
5859
time.sleep(self._key_interval)
@@ -66,7 +67,7 @@ def _read_response(self, first_time=False):
6667
tv_name = self.connection.recv(tv_name_len)
6768

6869
if first_time:
69-
logging.debug("Connected to '%s'.", tv_name.decode())
70+
_LOGGER.debug("Connected to '%s'.", tv_name.decode())
7071

7172
response_len = int.from_bytes(self.connection.recv(2),
7273
byteorder="little")
@@ -77,19 +78,19 @@ def _read_response(self, first_time=False):
7778
raise exceptions.ConnectionClosed()
7879

7980
if response == b"\x64\x00\x01\x00":
80-
logging.debug("Access granted.")
81+
_LOGGER.debug("Access granted.")
8182
return
8283
elif response == b"\x64\x00\x00\x00":
8384
raise exceptions.AccessDenied()
8485
elif response[0:1] == b"\x0a":
8586
if first_time:
86-
logging.warning("Waiting for authorization...")
87+
_LOGGER.warning("Waiting for authorization...")
8788
return self._read_response()
8889
elif response[0:1] == b"\x65":
89-
logging.warning("Authorization cancelled.")
90+
_LOGGER.warning("Authorization cancelled.")
9091
raise exceptions.AccessDenied()
9192
elif response == b"\x00\x00\x00\x00":
92-
logging.debug("Control accepted.")
93+
_LOGGER.debug("Control accepted.")
9394
return
9495

9596
raise exceptions.UnhandledResponse(response)

samsungctl/remote_websocket.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99

1010
URL_FORMAT = "ws://{}:{}/api/v2/channels/samsung.remote.control?name={}"
11+
_LOGGER = logging.getLogger(__package__)
1112

1213

1314
class RemoteWebsocket():
@@ -40,7 +41,7 @@ def close(self):
4041
if self.connection:
4142
self.connection.close()
4243
self.connection = None
43-
logging.debug("Connection closed.")
44+
_LOGGER.debug("Connection closed.")
4445

4546
def control(self, key):
4647
"""Send a control command."""
@@ -57,7 +58,7 @@ def control(self, key):
5758
}
5859
})
5960

60-
logging.info("Sending control command: %s", key)
61+
_LOGGER.info("Sending control command: %s", key)
6162
self.connection.send(payload)
6263
time.sleep(self._key_interval)
6364

@@ -71,7 +72,7 @@ def _read_response(self):
7172
self.close()
7273
raise exceptions.UnhandledResponse(response)
7374

74-
logging.debug("Access granted.")
75+
_LOGGER.debug("Access granted.")
7576

7677
@staticmethod
7778
def _serialize_string(string):

0 commit comments

Comments
 (0)