This repository has been archived by the owner on Apr 2, 2020. It is now read-only.
forked from n-i-x/pc-autobackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
125 lines (102 loc) · 4.27 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Copyright 2013 Jeff Rebeiro (jeff@rebeiro.net) All rights reserved
# Common functions for PC Autobackup
__author__ = 'jeff@rebeiro.net (Jeff Rebeiro)'
import ConfigParser
import logging
import os
import re
import socket
import uuid
CAMERA_CONFIG = {
'default': {'desc_file': os.path.join('dlna_web_root',
'samsungautobackupdesc.ini')},
'SAMSUNG EX2': {'desc_file': os.path.join('dlna_web_root',
'SAMSUNGAutoBackupDESC.ini')},
'SAMSUNG DV300': {'desc_file': os.path.join('DLNA_WEB_ROOT',
'SAMSUNGAutoBackupDESC.ini')},
'SAMSUNG WB150': {'desc_file': os.path.join('DLNA_WEB_ROOT',
'SAMSUNGAUTOBACKUPDESC.INI')},
'SAMSUNG WB350F': {'desc_file': os.path.join('DLNA_WEB_ROOT',
'SAMSUNGAutoBackupDESC.ini')},
'SAMSUNG Camera-MSCP': {'desc_file': os.path.join('dlna_web_root',
'SAMSUNGAutoBackupDESC.ini')},
'SAMSUNG NX1000': {'desc_file': os.path.join('dlna_web_root',
'SAMSUNGAutoBackupDESC.ini')},
'SAMSUNG NX500': {'desc_file': os.path.join('dlna_web_root',
'SAMSUNGAutoBackupDESC.ini')}}
BASEDIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_FILE = os.path.expanduser("~/.pc_autobackup.cfg")
CAMERA_INFO_FILE = [os.path.join('system', 'device.xml'),
os.path.join('SYSTEM', 'DEVICE.XML'),
os.path.join('SYSTEM', 'Device.xml'),
os.path.join('config', 'RVF', 'xml', 'DeviceDescription.xml'),
os.path.join('config', 'DMC', 'MSCP', 'xml', 'DeviceDescription.xml')]
CAMERA_MODEL = re.compile(r'<BaseModelName\s*value="(.*)"\s*/>')
DESC_SERVER_NAME = re.compile(r'friendlyName\s*=\s*(.*)')
DESC_UUID = re.compile(r'UDN\s*=\s*uuid:(.*)')
DESC_INI = '''MacAddr=%(mac_address)s\r
UDN=uuid:%(uuid)s\r
friendlyName=%(server_name)s\r
WOLSupport=1\r
ServerFlag=1\r
'''
LOG_DATE_FMT = '[%m/%d/%Y %I:%M %p]'
LOG_FMT = '%(asctime)s %(message)s'
LOG_DEFAULTS = {'level': logging.INFO,
'format': LOG_FMT,
'datefmt': LOG_DATE_FMT}
def EscapeHTML(html):
"""Escape characters in the given HTML.
Args:
html: A string containing HTML to be escaped
Returns:
A string containing escaped HTML
"""
html_codes = (('&', '&'),
('<', '<'),
('>', '>'),
('"', '"'),
("'", '''))
for old, new in html_codes:
html = html.replace(old, new)
return html
def GenerateUUID():
"""Generate a UUID.
Returns:
A string containing a valid UUID
"""
uuid_prefix = '4a682b0b-0361-dbae-6155'
uuid_suffix = str(uuid.uuid4()).split('-')[-1]
return '-'.join([uuid_prefix, uuid_suffix])
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)
if not config.has_section('AUTOBACKUP'):
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',
os.path.expanduser('~/PCAutoBackup'))
if not config.has_option('AUTOBACKUP', 'create_date_subdir'):
config.set('AUTOBACKUP', 'create_date_subdir', '1')
if not config.has_option('AUTOBACKUP', 'default_interface'):
try:
config.set('AUTOBACKUP', 'default_interface',
socket.gethostbyname(socket.gethostname()))
except socket.error:
logger.error('Unable to determine IP address. Please set manually!')
config.set('AUTOBACKUP', 'default_interface', '127.0.0.1')
if not config.has_option('AUTOBACKUP', 'server_name'):
config.set('AUTOBACKUP', 'server_name', '[PC]AutoBackup')
if not config.has_option('AUTOBACKUP', 'uuid'):
config.set('AUTOBACKUP', 'uuid', GenerateUUID())
with open(config_file, 'wb') as config_file:
config.write(config_file)
return config