Skip to content

Commit

Permalink
Merge pull request n-i-x#12 from birgerro/config_file
Browse files Browse the repository at this point in the history
Add --config_file=FILE option to pc_autobackup.py
  • Loading branch information
n-i-x committed Jan 26, 2016
2 parents 837ca4c + a1c9b71 commit a43dd72
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 29 deletions.
13 changes: 8 additions & 5 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,22 @@ def GenerateUUID():
return '-'.join([uuid_prefix, uuid_suffix])


def LoadOrCreateConfig():
def LoadOrCreateConfig(config_file=None):
"""Load an existing configuration or create one.
Returns:
ConfigParser.RawConfigParser
"""
if not config_file:
config_file = CONFIG_FILE

logger = logging.getLogger('pc_autobackup.common')

config = ConfigParser.RawConfigParser()
config.read(CONFIG_FILE)
config.read(config_file)

if not config.has_section('AUTOBACKUP'):
logger.info('Creating configuration file %s', CONFIG_FILE)
logger.info('Creating configuration file %s', config_file)
config.add_section('AUTOBACKUP')
if not config.has_option('AUTOBACKUP', 'backup_dir'):
config.set('AUTOBACKUP', 'backup_dir',
Expand All @@ -101,8 +104,8 @@ def LoadOrCreateConfig():
if not config.has_option('AUTOBACKUP', 'uuid'):
config.set('AUTOBACKUP', 'uuid', GenerateUUID())

with open(CONFIG_FILE, 'wb') as config_file:
config.write(config_file)
with open(config_file, 'wb') as file:
config.write(file)

return config

13 changes: 7 additions & 6 deletions mediaserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class Backup(object):

backup_objects = {}

def __init__(self):
def __init__(self, config_file=None):
self.logger = logging.getLogger('pc_autobackup.mediaserver.backup')
self.config = common.LoadOrCreateConfig()
self.config = common.LoadOrCreateConfig(config_file)

def _GenerateObjectID(self, obj_date, length=10):
"""Generate an ObjectID for a new backup item.
Expand Down Expand Up @@ -153,9 +153,10 @@ class MediaServer(Resource):
clients = {}
isLeaf = True

def __init__(self):
def __init__(self, config_file=None):
self.logger = logging.getLogger('pc_autobackup.mediaserver')
self.config = common.LoadOrCreateConfig()
self.config = common.LoadOrCreateConfig(config_file)
self.config_file = config_file

def render_GET(self, request):
if request.path != '/favicon.ico':
Expand Down Expand Up @@ -258,7 +259,7 @@ def GetContentDirectoryResponse(self, request):
request.setResponseCode(404)
return ''

backup = Backup()
backup = Backup(self.config_file)
obj_id = backup.CreateObject(obj_class, obj_date, obj_name, obj_size,
obj_subtype, obj_type)
obj_details = backup.GetObjectDetails(obj_id)
Expand Down Expand Up @@ -374,7 +375,7 @@ def ReceiveUpload(self, request):
response = ''

obj_id = request.args['didx'][0].split('=')[1]
backup = Backup()
backup = Backup(self.config_file)

data = request.content.read()
backup.WriteObject(obj_id, data)
Expand Down
43 changes: 27 additions & 16 deletions pc_autobackup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def GetCameraConfig(mountpoint):
sys.exit(1)


def GetSystemInfo():
def GetSystemInfo(config_file=None):
"""Log basic system information to the debug logger."""
logger = logging.getLogger('pc_autobackup')
logger.debug('Command-line: %s', ' '.join(sys.argv))
Expand All @@ -64,19 +64,22 @@ def GetSystemInfo():
logger.debug('System Information (node): %s', platform.node())
logger.debug('System Information (hostname): %s', socket.gethostname())

config = common.LoadOrCreateConfig()
config = common.LoadOrCreateConfig(config_file)
for section in config.sections():
for option in config.options(section):
logger.debug('Config (%s): %s = %s', section, option,
config.get(section, option))


def ImportCameraConfig(mountpoint):
def ImportCameraConfig(mountpoint,config_file=None):
"""Import the PC AutoBackup settings from a camera.
Args:
mountpoint: A string containing the path to the cameras SD card
"""
if not config_file:
config_file = common.CONFIG_FILE

logger = logging.getLogger('pc_autobackup')

camera_config = GetCameraConfig(mountpoint)
Expand All @@ -86,7 +89,7 @@ def ImportCameraConfig(mountpoint):
with open(desc_file, 'r') as f:
desc_data = f.read()
logger.info('Loading configuration from camera')
config = common.LoadOrCreateConfig()
config = common.LoadOrCreateConfig(config_file)

m = common.DESC_SERVER_NAME.search(desc_data)
if m:
Expand All @@ -104,10 +107,10 @@ def ImportCameraConfig(mountpoint):

config.set('AUTOBACKUP', 'server_name', friendly_name)
config.set('AUTOBACKUP', 'uuid', uuid)
with open(common.CONFIG_FILE, 'wb') as config_file:
with open(config_file, 'wb') as file:
logger.info('Saving server configuration')
try:
config.write(config_file)
config.write(file)
logger.info('Configuration saved successfully')
except IOError as e:
logger.error('Unable to save configuration: %s', str(e))
Expand All @@ -120,7 +123,7 @@ def ImportCameraConfig(mountpoint):
sys.exit(1)


def UpdateCameraConfig(mountpoint, create_desc_file=False):
def UpdateCameraConfig(mountpoint, create_desc_file=False, config_file=None):
"""Update the PC AutoBackup settings on a camera.
Args:
Expand All @@ -142,7 +145,7 @@ def UpdateCameraConfig(mountpoint, create_desc_file=False):

if os.path.isfile(desc_file):
with open(desc_file, 'wb') as f:
config = common.LoadOrCreateConfig()
config = common.LoadOrCreateConfig(config_file)
ini_params = {'mac_address': mac_address,
'server_name': config.get('AUTOBACKUP', 'server_name'),
'uuid': config.get('AUTOBACKUP', 'uuid')}
Expand All @@ -160,6 +163,8 @@ def main():
parser.add_option('-b', '--bind', dest='bind',
help='bind the server to a specific IP',
metavar='IP')
parser.add_option('--config_file', dest='config_file',
help='change config file location', metavar='FILE')
parser.add_option('--create_camera_config', dest='create_camera_config',
help='create new camera configuration file',
metavar='MOUNTPOINT')
Expand Down Expand Up @@ -214,7 +219,10 @@ def main():
console.setFormatter(formatter)
logger.addHandler(console)

config = common.LoadOrCreateConfig()
if not options.config_file:
options.config_file = common.CONFIG_FILE

config = common.LoadOrCreateConfig(options.config_file)
update_config = False

if options.bind:
Expand All @@ -231,19 +239,22 @@ def main():
update_config = True

if update_config:
with open(common.CONFIG_FILE, 'wb') as config_file:
with open(options.config_file, 'wb') as config_file:
config.write(config_file)

if options.create_camera_config:
UpdateCameraConfig(options.create_camera_config, create_desc_file=True)
UpdateCameraConfig(options.create_camera_config, create_desc_file=True,
config_file=options.config_file)
sys.exit(0)

if options.import_camera_config:
ImportCameraConfig(options.import_camera_config)
ImportCameraConfig(options.import_camera_config,
config_file=options.config_file)
sys.exit(0)

if options.update_camera_config:
UpdateCameraConfig(options.update_camera_config)
UpdateCameraConfig(options.update_camera_config,
config_file=options.config_file)
sys.exit(0)

if config.has_option('AUTOBACKUP', 'default_interface'):
Expand All @@ -256,12 +267,12 @@ def main():
logger.info('Server name: %s', config.get('AUTOBACKUP', 'server_name'))

if options.debug:
GetSystemInfo()
GetSystemInfo(config_file=options.config_file)

reactor.listenMulticast(1900, ssdp.SSDPServer(), listenMultiple=True)
reactor.listenMulticast(1900, ssdp.SSDPServer(options.config_file), listenMultiple=True)
logger.info('SSDPServer started')

resource = mediaserver.MediaServer()
resource = mediaserver.MediaServer(options.config_file)
factory = Site(resource)
reactor.listenTCP(52235, factory, interface=interface)
logger.info('MediaServer started')
Expand Down
4 changes: 2 additions & 2 deletions ssdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

class SSDPServer(DatagramProtocol):

def __init__(self):
def __init__(self, config_file=None):
self.logger = logging.getLogger('pc_autobackup.ssdp')
self.config = common.LoadOrCreateConfig()
self.config = common.LoadOrCreateConfig(config_file)

def startProtocol(self):
self.transport.setTTL(5)
Expand Down

0 comments on commit a43dd72

Please sign in to comment.