-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merged devel branch, ready for release #2
- Loading branch information
Showing
23 changed files
with
2,156 additions
and
512 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,12 @@ | |
Copyright (c) 2014 Rafael Marmelo | ||
Copyright (c) 2015 Simone Marzona | ||
Copyright (c) 2016 Tim Sweeney | ||
TAS - Tim Sweeney - [email protected] | ||
2016/03/21 - TAS - Validate config file entries on read. | ||
""" | ||
|
||
# import modules | ||
|
@@ -32,7 +38,7 @@ class AppConfig(object): | |
""" | ||
|
||
def __init__(self, alternate_config_file): #pragma: no cover | ||
def __init__(self, alternate_config_file): | ||
"""Default config, they will be overwritten when a conf is loaded | ||
this will be used to write a default config file. | ||
If the command line specifies a config file we note it in | ||
|
@@ -48,7 +54,7 @@ def __init__(self, alternate_config_file): #pragma: no cover | |
self.io = IO() | ||
self.default_config_file = ".rig-remote/rig-remote.conf" | ||
self.config_file = None | ||
self.config = DEFAULT_CONFIG | ||
self.config = dict.copy(DEFAULT_CONFIG) | ||
|
||
if alternate_config_file: | ||
self.config_file = alternate_config_file | ||
|
@@ -57,21 +63,25 @@ def __init__(self, alternate_config_file): #pragma: no cover | |
self.config_file = os.path.join(os.path.expanduser('~'), | ||
self.default_config_file) | ||
|
||
def read_conf(self): # pragma: no cover | ||
def read_conf(self): | ||
"""Read the configuration file. | ||
If the default one doesn't exist we create one with sane values. | ||
and then we re-read it. | ||
and then we re-read it. It logs an error if a line of the file is not | ||
valid and moves on to the next one. | ||
:param: none | ||
:raises: none | ||
:returns: none | ||
""" | ||
|
||
if os.path.isfile(self.config_file): | ||
logger.info("Using config file:{}".format(self.config_file)) | ||
self.io.csv_load(self.config_file, "=") | ||
error = 0 | ||
for row in self.io.row_list: | ||
self.config[row[0].strip()] = row[1].strip() | ||
if len(row) == 2 and row[0].strip() in self.config.keys() : | ||
self.config[row[0].strip()] = row[1].strip() | ||
else: | ||
logger.warning("Error in config file line: " + str(row)) | ||
else: | ||
self.write_conf() | ||
self.read_conf() | ||
|
@@ -102,3 +112,4 @@ def write_conf(self): | |
row.append(self.config[key]) | ||
self.io.row_list.append(row) | ||
self.io.csv_save(self.config_file, "=") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,23 @@ | |
Copyright (c) 2014 Rafael Marmelo | ||
Copyright (c) 2015 Simone Marzona | ||
Copyright (c) 2016 Tim Sweeney | ||
TAS - Tim Sweeney - [email protected] | ||
2016/02/24 - TAS - Added log file class to handle logging scanning | ||
activity to a file. | ||
""" | ||
|
||
import csv | ||
import logging | ||
import os.path | ||
from modules.exceptions import InvalidPathError | ||
from modules.constants import BM | ||
from modules.constants import LOG_FILE_NAME | ||
import datetime | ||
import time | ||
|
||
# logging configuration | ||
logger = logging.getLogger(__name__) | ||
|
@@ -58,21 +69,20 @@ def csv_load(self, csv_file, delimiter): | |
""" | ||
|
||
self._path_check(csv_file) | ||
|
||
try: | ||
with open(csv_file, 'r') as data_file: | ||
reader = csv.reader(data_file, delimiter=delimiter) | ||
for line in reader: | ||
self.row_list.append(line) | ||
|
||
except csv.Error: | ||
logger.error("The file provided({})"\ | ||
" is not a file with values "\ | ||
"separated by {}.".format(csv_file, delimiter)) | ||
logger.exception("The file provided({})"\ | ||
" is not a file with values "\ | ||
"separated by {}.".format(csv_file, delimiter)) | ||
|
||
except (IOError, OSError): | ||
logger.error("Error while trying to read the file: "\ | ||
"{}".format(csv_file)) | ||
logger.exception("Error while trying to read the file: "\ | ||
"{}".format(csv_file)) | ||
|
||
def csv_save(self, csv_file, delimiter): | ||
"""Save current frequencies to disk. | ||
|
@@ -92,3 +102,88 @@ def csv_save(self, csv_file, delimiter): | |
"{}".format(csv_file)) | ||
|
||
|
||
class LogFile(object): | ||
"""Handles the a tasks of logging to a file. | ||
""" | ||
|
||
def __init__(self): | ||
"""Defines the log file name and | ||
sets the fhandler self.log_file to None. | ||
""" | ||
|
||
self.log_filename = LOG_FILE_NAME | ||
self.log_file = None | ||
|
||
def open(self, name = None): | ||
"""Opens a log file. | ||
:param name: log file name, defaults to None | ||
:type name: string | ||
""" | ||
if name != None : | ||
self.log_filename = name | ||
try: | ||
self.log_file = open(self.log_filename, 'a') | ||
except (IOError, OSError): | ||
logger.error("Error while trying to open log file: "\ | ||
"{}".format(self.log_filename)) | ||
|
||
def write(self, record_type, record, signal): | ||
"""Writes a message to the log file. | ||
:param record_type: type of the record to write | ||
:type record_type: string | ||
:param record: data to write | ||
:type record: tuple | ||
:param signal: signal level | ||
:type signal: list | ||
:raises IOError or OSError for any issue that happens while writing. | ||
""" | ||
|
||
if record_type not in ["B","F"]: | ||
logger.error("Record type not supported, must be 'B' or 'F'"\ | ||
"got {}".format(record_type)) | ||
raise TypeError | ||
|
||
if record_type == 'B' : | ||
lstr = 'B ' + str(datetime.datetime.today().strftime\ | ||
("%a %Y-%b-%d %H:%M:%S")) + ' ' + \ | ||
record[BM.freq] + ' ' + record[BM.mode] + \ | ||
' ' + str(signal) + "\n" | ||
else : | ||
lstr = 'F ' + str(datetime.datetime.today().strftime\ | ||
("%a %Y-%b-%d %H:%M:%S")) + ' ' + \ | ||
record[2] + ' ' + record[1] + \ | ||
' ' + str(signal) + "\n" | ||
try: | ||
self.log_file.write(lstr) | ||
except AttributeError: | ||
logger.exception("No log file provided, but log feature selected.") | ||
raise | ||
except (IOError, OSError): | ||
logger.exception("Error while trying to write log file: "\ | ||
"{}".format(self.log_filename)) | ||
except (TypeError, IndexError): | ||
logger.exception("At least one of the parameter isn't of the "\ | ||
"expected type:"\ | ||
"record_type {},"\ | ||
"record {},"\ | ||
"signal {}".format(type(record_type), | ||
type(record), | ||
type(signal))) | ||
raise | ||
|
||
def close(self): | ||
"""Closes the log file. | ||
:raises IOError OSError: if there are issues while closing the log file | ||
""" | ||
|
||
if self.log_file != None : | ||
try: | ||
self.log_file.close() | ||
except (IOError, OSError): | ||
logger.error("Error while trying to close log file: "\ | ||
"{}".format(self.log_filename)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,4 @@ class UnsupportedScanningConfigError(NonRetriableError): | |
|
||
class RetriableError (object): | ||
pass | ||
|
Oops, something went wrong.