Skip to content

Commit

Permalink
Fix PEP257 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fabaff committed Mar 7, 2016
1 parent 4f536ac commit 897b5c6
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 141 deletions.
1 change: 1 addition & 0 deletions homeassistant/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Init file for Home Assistant."""
48 changes: 24 additions & 24 deletions homeassistant/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Starts home assistant. """
"""Starts home assistant."""
from __future__ import print_function

import argparse
Expand All @@ -20,7 +20,7 @@


def validate_python():
""" Validate we're running the right Python version. """
"""Validate we're running the right Python version."""
major, minor = sys.version_info[:2]
req_major, req_minor = REQUIRED_PYTHON_VER

Expand All @@ -31,8 +31,7 @@ def validate_python():


def ensure_config_path(config_dir):
""" Validates configuration directory. """

"""Validate the configuration directory."""
lib_dir = os.path.join(config_dir, 'lib')

# Test if configuration directory exists
Expand Down Expand Up @@ -60,7 +59,7 @@ def ensure_config_path(config_dir):


def ensure_config_file(config_dir):
""" Ensure configuration file exists. """
"""Ensure configuration file exists."""
config_path = config_util.ensure_config_exists(config_dir)

if config_path is None:
Expand All @@ -71,7 +70,7 @@ def ensure_config_file(config_dir):


def get_arguments():
""" Get parsed passed in arguments. """
"""Get parsed passed in arguments."""
parser = argparse.ArgumentParser(
description="Home Assistant: Observe, Control, Automate.")
parser.add_argument('--version', action='version', version=__version__)
Expand Down Expand Up @@ -136,25 +135,25 @@ def get_arguments():


def daemonize():
""" Move current process to daemon process """
# create first fork
"""Move current process to daemon process."""
# Create first fork
pid = os.fork()
if pid > 0:
sys.exit(0)

# decouple fork
# Decouple fork
os.setsid()
os.umask(0)

# create second fork
# Create second fork
pid = os.fork()
if pid > 0:
sys.exit(0)


def check_pid(pid_file):
""" Check that HA is not already running """
# check pid file
"""Check that HA is not already running."""
# Check pid file
try:
pid = int(open(pid_file, 'r').readline())
except IOError:
Expand All @@ -171,7 +170,7 @@ def check_pid(pid_file):


def write_pid(pid_file):
""" Create PID File """
"""Create a PID File."""
pid = os.getpid()
try:
open(pid_file, 'w').write(str(pid))
Expand All @@ -181,7 +180,7 @@ def write_pid(pid_file):


def install_osx():
""" Setup to run via launchd on OS X """
"""Setup to run via launchd on OS X."""
with os.popen('which hass') as inp:
hass_path = inp.read().strip()

Expand Down Expand Up @@ -213,17 +212,18 @@ def install_osx():


def uninstall_osx():
""" Unload from launchd on OS X """
"""Unload from launchd on OS X."""
path = os.path.expanduser("~/Library/LaunchAgents/org.homeassistant.plist")
os.popen('launchctl unload ' + path)

print("Home Assistant has been uninstalled.")


def setup_and_run_hass(config_dir, args, top_process=False):
"""
Setup HASS and run. Block until stopped. Will assume it is running in a
subprocess unless top_process is set to true.
"""Setup HASS and run.
Block until stopped. Will assume it is running in a subprocess unless
top_process is set to true.
"""
if args.demo_mode:
config = {
Expand All @@ -243,7 +243,7 @@ def setup_and_run_hass(config_dir, args, top_process=False):

if args.open_ui:
def open_browser(event):
""" Open the webinterface in a browser. """
"""Open the webinterface in a browser."""
if hass.config.api is not None:
import webbrowser
webbrowser.open(hass.config.api.base_url)
Expand All @@ -259,12 +259,12 @@ def open_browser(event):


def run_hass_process(hass_proc):
""" Runs a child hass process. Returns True if it should be restarted. """
"""Run a child hass process. Returns True if it should be restarted."""
requested_stop = threading.Event()
hass_proc.daemon = True

def request_stop(*args):
""" request hass stop, *args is for signal handler callback """
"""Request hass stop, *args is for signal handler callback."""
requested_stop.set()
hass_proc.terminate()

Expand All @@ -289,15 +289,15 @@ def request_stop(*args):


def main():
""" Starts Home Assistant. """
"""Start Home Assistant."""
validate_python()

args = get_arguments()

config_dir = os.path.join(os.getcwd(), args.config)
ensure_config_path(config_dir)

# os x launchd functions
# OS X launchd functions
if args.install_osx:
install_osx()
return 0
Expand All @@ -311,7 +311,7 @@ def main():
install_osx()
return 0

# daemon functions
# Daemon functions
if args.pid_file:
check_pid(args.pid_file)
if args.daemon:
Expand Down
30 changes: 14 additions & 16 deletions homeassistant/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@


def setup_component(hass, domain, config=None):
""" Setup a component and all its dependencies. """

"""Setup a component and all its dependencies."""
if domain in hass.config.components:
return True

Expand All @@ -58,7 +57,7 @@ def setup_component(hass, domain, config=None):


def _handle_requirements(hass, component, name):
""" Installs requirements for component. """
"""Install the requirements for a component."""
if hass.config.skip_pip or not hasattr(component, 'REQUIREMENTS'):
return True

Expand Down Expand Up @@ -126,7 +125,7 @@ def _setup_component(hass, domain, config):


def prepare_setup_platform(hass, config, domain, platform_name):
""" Loads a platform and makes sure dependencies are setup. """
"""Load a platform and makes sure dependencies are setup."""
_ensure_loader_prepared(hass)

platform_path = PLATFORM_FORMAT.format(domain, platform_name)
Expand Down Expand Up @@ -158,16 +157,15 @@ def prepare_setup_platform(hass, config, domain, platform_name):


def mount_local_lib_path(config_dir):
""" Add local library to Python Path """
"""Add local library to Python Path."""
sys.path.insert(0, os.path.join(config_dir, 'lib'))


# pylint: disable=too-many-branches, too-many-statements, too-many-arguments
def from_config_dict(config, hass=None, config_dir=None, enable_log=True,
verbose=False, daemon=False, skip_pip=False,
log_rotate_days=None):
"""
Tries to configure Home Assistant from a config dict.
"""Try to configure Home Assistant from a config dict.
Dynamically loads required components and its dependencies.
"""
Expand Down Expand Up @@ -209,7 +207,7 @@ def from_config_dict(config, hass=None, config_dir=None, enable_log=True,

_LOGGER.info('Home Assistant core initialized')

# give event decorators access to HASS
# Give event decorators access to HASS
event_decorators.HASS = hass
service.HASS = hass

Expand All @@ -222,9 +220,9 @@ def from_config_dict(config, hass=None, config_dir=None, enable_log=True,

def from_config_file(config_path, hass=None, verbose=False, daemon=False,
skip_pip=True, log_rotate_days=None):
"""
Reads the configuration file and tries to start all the required
functionality. Will add functionality to 'hass' parameter if given,
"""Read the configuration file and try to start all the functionality.
Will add functionality to 'hass' parameter if given,
instantiates a new Home Assistant object if 'hass' is not given.
"""
if hass is None:
Expand All @@ -244,7 +242,7 @@ def from_config_file(config_path, hass=None, verbose=False, daemon=False,


def enable_logging(hass, verbose=False, daemon=False, log_rotate_days=None):
""" Setup the logging for home assistant. """
"""Setup the logging."""
if not daemon:
logging.basicConfig(level=logging.INFO)
fmt = ("%(log_color)s%(asctime)s %(levelname)s (%(threadName)s) "
Expand Down Expand Up @@ -297,7 +295,7 @@ def enable_logging(hass, verbose=False, daemon=False, log_rotate_days=None):


def process_ha_config_upgrade(hass):
""" Upgrade config if necessary. """
"""Upgrade config if necessary."""
version_path = hass.config.path('.HA_VERSION')

try:
Expand All @@ -322,11 +320,11 @@ def process_ha_config_upgrade(hass):


def process_ha_core_config(hass, config):
""" Processes the [homeassistant] section from the config. """
"""Process the [homeassistant] section from the config."""
hac = hass.config

def set_time_zone(time_zone_str):
""" Helper method to set time zone in HA. """
"""Helper method to set time zone."""
if time_zone_str is None:
return

Expand Down Expand Up @@ -397,6 +395,6 @@ def set_time_zone(time_zone_str):


def _ensure_loader_prepared(hass):
""" Ensure Home Assistant loader is prepared. """
"""Ensure Home Assistant loader is prepared."""
if not loader.PREPARED:
loader.prepare(hass)
27 changes: 13 additions & 14 deletions homeassistant/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
"""
homeassistant.config
~~~~~~~~~~~~~~~~~~~~
Module to help with parsing and generating configuration files.
"""
"""Module to help with parsing and generating configuration files."""
import logging
import os

Expand Down Expand Up @@ -43,16 +38,18 @@


def get_default_config_dir():
""" Put together the default configuration directory based on OS. """
"""Put together the default configuration directory based on OS."""
data_dir = os.getenv('APPDATA') if os.name == "nt" \
else os.path.expanduser('~')
return os.path.join(data_dir, CONFIG_DIR_NAME)


def ensure_config_exists(config_dir, detect_location=True):
""" Ensures a config file exists in given config dir.
Creating a default one if needed.
Returns path to the config file. """
"""Ensure a config file exists in given configuration directory.
Creating a default one if needed.
Return path to the config file.
"""
config_path = find_config_file(config_dir)

if config_path is None:
Expand All @@ -64,8 +61,10 @@ def ensure_config_exists(config_dir, detect_location=True):


def create_default_config(config_dir, detect_location=True):
""" Creates a default configuration file in given config dir.
Returns path to new config file if success, None if failed. """
"""Create a default configuration file in given configuration directory.
Return path to new config file if success, None if failed.
"""
config_path = os.path.join(config_dir, YAML_CONFIG_FILE)

info = {attr: default for attr, default, _, _ in DEFAULT_CONFIG}
Expand Down Expand Up @@ -108,14 +107,14 @@ def create_default_config(config_dir, detect_location=True):


def find_config_file(config_dir):
""" Looks in given directory for supported config files. """
"""Look in given directory for supported configuration files."""
config_path = os.path.join(config_dir, YAML_CONFIG_FILE)

return config_path if os.path.isfile(config_path) else None


def load_yaml_config_file(config_path):
""" Parse a YAML configuration file. """
"""Parse a YAML configuration file."""
conf_dict = load_yaml(config_path)

if not isinstance(conf_dict, dict):
Expand Down
Loading

0 comments on commit 897b5c6

Please sign in to comment.