<commit_before>#!/usr/bin/env python

import argparse
import collections
import json
import logging
import os
import sys

from . import __title__
from . import __version__
from . import interactive
from .remote import Remote

def read_config():
	config = collections.defaultdict(lambda: None, {
		"name": "samsungctl",
		"description": "PC",
		"id": "",
		"port": 55000,
	})

	config_directory = os.getenv("XDG_CONFIG_HOME") or os.path.join(os.getenv("HOME"), ".config")

	try:
		config_file = open(os.path.join(config_directory, "samsungctl.conf"))
	except FileNotFoundError:
		return config

	with config_file:
		try:
			config_json = json.load(config_file)
		except ValueError as e:
			logging.warning("Warning: Could not parse the configuration file.\n  %s", e)
			return config

		config.update(config_json)

	return config

def main():
	parser = argparse.ArgumentParser(prog=__title__,
	                                 description="Remote control Samsung televisions via TCP/IP connection.",
	                                 epilog="E.g. %(prog)s --host 192.168.0.10 --name myremote KEY_VOLDOWN")
	parser.add_argument("--version", action="version", version="%(prog)s {0}".format(__version__))
	parser.add_argument("-v", action="count", help="increase output verbosity")
	parser.add_argument("-q", action="store_true", help="suppress non-fatal output")
	parser.add_argument("key", nargs="*", help="keys to be sent (e.g. KEY_VOLDOWN)")
	parser.add_argument("--host", help="TV hostname or IP address")
	parser.add_argument("--name", help="remote control name")
	parser.add_argument("--description", help="remote control description")
	parser.add_argument("--id", help="remote control id")
	parser.add_argument("--port", type=int, help="TV port number (TCP)")
	parser.add_argument("--interactive", action="store_true", help="interactive control")

	args = parser.parse_args()

	if args.q:
		log_level = logging.ERROR
	elif not args.v:
		log_level = logging.WARNING
	elif args.v == 1:
		log_level = logging.INFO
	else:
		log_level = logging.DEBUG

	logging.basicConfig(format="%(message)s", level=log_level)

	if not args.key and not args.interactive:
		logging.error("Error: At least one key or --interactive must be set.")
		return

	config = read_config()

	host = args.host or config["host"]
	name = args.name or config["name"]
	description = args.description or config["description"]
	id = args.id or config["id"]
	port = args.port or config["port"]

	if not host:
		logging.error("Error: --host must be set")
		return

	try:
		remote = Remote(host, port, name, description, id)
	except Remote.AccessDenied:
		logging.error("Error: Access denied!")
		return

	with remote:
		if args.interactive:
			interactive.run(remote)
		else:
			for key in args.key:
				remote.control(key)

main()<commit_msg>Call main function only if running as __main__<commit_after>#!/usr/bin/env python

import argparse
import collections
import json
import logging
import os
import sys

from . import __title__
from . import __version__
from . import interactive
from .remote import Remote

def read_config():
	config = collections.defaultdict(lambda: None, {
		"name": "samsungctl",
		"description": "PC",
		"id": "",
		"port": 55000,
	})

	config_directory = os.getenv("XDG_CONFIG_HOME") or os.path.join(os.getenv("HOME"), ".config")

	try:
		config_file = open(os.path.join(config_directory, "samsungctl.conf"))
	except FileNotFoundError:
		return config

	with config_file:
		try:
			config_json = json.load(config_file)
		except ValueError as e:
			logging.warning("Warning: Could not parse the configuration file.\n  %s", e)
			return config

		config.update(config_json)

	return config

def main():
	parser = argparse.ArgumentParser(prog=__title__,
	                                 description="Remote control Samsung televisions via TCP/IP connection.",
	                                 epilog="E.g. %(prog)s --host 192.168.0.10 --name myremote KEY_VOLDOWN")
	parser.add_argument("--version", action="version", version="%(prog)s {0}".format(__version__))
	parser.add_argument("-v", action="count", help="increase output verbosity")
	parser.add_argument("-q", action="store_true", help="suppress non-fatal output")
	parser.add_argument("key", nargs="*", help="keys to be sent (e.g. KEY_VOLDOWN)")
	parser.add_argument("--host", help="TV hostname or IP address")
	parser.add_argument("--name", help="remote control name")
	parser.add_argument("--description", help="remote control description")
	parser.add_argument("--id", help="remote control id")
	parser.add_argument("--port", type=int, help="TV port number (TCP)")
	parser.add_argument("--interactive", action="store_true", help="interactive control")

	args = parser.parse_args()

	if args.q:
		log_level = logging.ERROR
	elif not args.v:
		log_level = logging.WARNING
	elif args.v == 1:
		log_level = logging.INFO
	else:
		log_level = logging.DEBUG

	logging.basicConfig(format="%(message)s", level=log_level)

	if not args.key and not args.interactive:
		logging.error("Error: At least one key or --interactive must be set.")
		return

	config = read_config()

	host = args.host or config["host"]
	name = args.name or config["name"]
	description = args.description or config["description"]
	id = args.id or config["id"]
	port = args.port or config["port"]

	if not host:
		logging.error("Error: --host must be set")
		return

	try:
		remote = Remote(host, port, name, description, id)
	except Remote.AccessDenied:
		logging.error("Error: Access denied!")
		return

	with remote:
		if args.interactive:
			interactive.run(remote)
		else:
			for key in args.key:
				remote.control(key)

if __name__ == "__main__":
	main()
{'fraction_non_alphanumeric': 0.13766966331746872, 'fraction_numerical': 0.005640754450907809, 'mean_word_length': 4.2102846648301195, 'pattern_counts': {'":': 9, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1269694', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1789, 'n_words': 483}