Skip to content

Commit

Permalink
Find default config file properly; do not use current directory
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Nagle committed Jun 22, 2016
1 parent b0d090f commit 0884ff1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
37 changes: 29 additions & 8 deletions messager/baudotrss.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
DEFAULTCONFIG = "configdefault.cfg" # default config values.
READTIMEOUT = 1.0 # read timeout - makes read abort (control-C) work on Linux.

import os
import inspect
import sys
assert(sys.version_info >= (2,7)) # Requires Python 2.7 or later.
import traceback
Expand All @@ -30,8 +32,24 @@
#
# Suppress deprecation warnings. We know feedparser and BeautifulSoup need updates.
#
warnings.filterwarnings(action='ignore', category=DeprecationWarning, module='BeautifulSoup')
warnings.filterwarnings(action='ignore', category=DeprecationWarning, module='feedparser')
####warnings.filterwarnings(action='ignore', category=DeprecationWarning, module='BeautifulSoup')
####warnings.filterwarnings(action='ignore', category=DeprecationWarning, module='feedparser')
#
# get_script_dir
#
def get_script_dir(follow_symlinks=True):
"""
Get directory from which script or program is executing
"""
if getattr(sys, 'frozen', False): # py2exe, PyInstaller, cx_Freeze
path = os.path.abspath(sys.executable)
else:
path = inspect.getabsfile(get_script_dir)
if follow_symlinks:
path = os.path.realpath(path)
return os.path.dirname(path)

####print(get_script_dir())

#
# opentty -- create and open the TTY device
Expand Down Expand Up @@ -80,7 +98,7 @@ def main() :
try:
# Process config files if present.
# There is a default config file, which can be overridden.
configfiles = [DEFAULTCONFIG] # default
configfiles = [get_script_dir() + "/" + DEFAULTCONFIG] # default config lives with program
feedurls = []
for arg in args : # file args
if arg.lower().endswith(".cfg") : # if config
Expand All @@ -91,14 +109,17 @@ def main() :
raise(ValueError(
'Command line "%s" should be a feed URL or a config file.'
% (arg,)))
config = configparser.ConfigParser() # read config file
logger.info('Configuration from "%s"' %
('", "'.join(configfiles))) # source of config
config.read(configfiles) # fetch configs
('", "'.join(configfiles))) # source of config
for fname in configfiles : # make sure config files are readable
if not os.access(fname, os.R_OK) : # configparser does not do this
raise(ValueError('Unable to read config file "%s".' % (fname,)))
config = configparser.ConfigParser() # read config file
config.read(configfiles) # fetch configs
# Get params
if options.verbose : # dump config
if options.verbose : # dump config
logger.debug("Configuration: ")
for section in config.sections() : # dump section
for section in config.sections() : # dump section
logger.debug(" [%s]" % (section,))
for (k,v) in config.items(section) :
logger.debug(' %s: "%s"' % (k,v))
Expand Down
31 changes: 31 additions & 0 deletions runbaudotrss.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
#
# Run Baudotrss
#
# Use dummy test file if no config file specified.
#
# Find directory in which this script resides.
# The code should be in ${CODEDIR}/messager
#
# Once running, keyboard commands are accepted.
# N print all news
# S Send SMS
# W print weather
# O off
# ESC Simulate BREAK; stop printing or wake up.
# CR Wait for new messages.
#
CODEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
TESTCFG=${CODEDIR}/configtest.cfg
#
echo "Program files should be in: " ${CODEDIR}
if [ "$1" != "" ]; then
PARAMS=$@
else
PARAMS=${TESTCFG}
fi
echo "Configuration file and options: " ${PARAMS}
#
echo python3 ${CODEDIR}/messager/baudotrss.py ${PARAMS} -v
python3 ${CODEDIR}/messager/baudotrss.py ${PARAMS} -v
sleep 5

0 comments on commit 0884ff1

Please sign in to comment.