PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/dConfig.py

http://damnvid.googlecode.com/
Python | 207 lines | 206 code | 0 blank | 1 comment | 9 complexity | 491b0415a54f10a712ef1004577c80c7 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, GPL-3.0
  1. # -*- coding: utf-8 -*-
  2. from dCore import *
  3. from dConstants import *
  4. from dLog import *
  5. from dSpawn import *
  6. from dTubes import *
  7. from dModules import *
  8. import os
  9. import shutil
  10. import ConfigParser
  11. import base64
  12. class DamnPrefs: # Preference manager (backend, not GUI)
  13. def __init__(self):
  14. self.conf = {}
  15. f = DamnOpenFile(DV.conf_file, 'r')
  16. self.ini = ConfigParser.SafeConfigParser()
  17. self.ini.readfp(f)
  18. f.close()
  19. self.profiles = 0
  20. for i in self.ini.sections():
  21. if i[:16] == 'damnvid-profile-':
  22. self.profiles = self.profiles + 1
  23. def expandPath(self, value):
  24. value = DamnUnicode(value)
  25. value = REGEX_PATH_MULTI_SEPARATOR_CHECK.sub(u'/', value.replace(DV.sep, u'/').replace(u'?DAMNVID_MY_VIDEOS?', DV.my_videos_path.replace(DV.sep, u'/'))).replace(u'/', DV.sep)
  26. if value[-1:] != DV.sep:
  27. value += DV.sep
  28. return value
  29. def reducePath(self, value):
  30. value = DamnUnicode(value)
  31. value = REGEX_PATH_MULTI_SEPARATOR_CHECK.sub(u'/', value.replace(DV.sep, u'/').replace(DV.my_videos_path.replace(DV.sep, u'/'), u'?DAMNVID_MY_VIDEOS?')).replace(DV.sep, u'/')
  32. if value[-1:] != u'/':
  33. value += u'/'
  34. return value
  35. def gets(self, section, name):
  36. name = name.lower()
  37. shortsection = section
  38. if shortsection[:16] == 'damnvid-profile-':
  39. shortsection = 'damnvid-profile'
  40. if self.ini.has_section(section):
  41. if self.ini.has_option(section, name):
  42. value = DamnUnicode(self.ini.get(section, name))
  43. elif DV.defaultprefs.has_key(shortsection + ':' + name):
  44. value = DamnUnicode(DV.defaultprefs[shortsection + ':' + name])
  45. self.sets(section, name, value)
  46. else:
  47. value = u''
  48. if shortsection + ':' + name in DV.path_prefs:
  49. value = DamnUnicode(self.expandPath(value))
  50. return value
  51. if DV.defaultprefs.has_key(section + ':' + name):
  52. value = DamnUnicode(DV.defaultprefs[section + ':' + name])
  53. self.ini.add_section(section)
  54. self.sets(section, name, value)
  55. return DamnUnicode(self.gets(section, name))
  56. Damnlog('No such pref:', section + ':' + name)
  57. def sets(self, section, name, value):
  58. name = name.lower()
  59. value = DamnUnicode(value)
  60. if self.ini.has_section(section):
  61. if section + ':' + name in DV.path_prefs:
  62. value = self.reducePath(value)
  63. return self.ini.set(section, name, value.encode('utf8'))
  64. else:
  65. Damnlog('No such section:', section)
  66. def rems(self, section, name=None):
  67. try:
  68. if name is None:
  69. self.ini.remove_section(section)
  70. else:
  71. self.ini.remove_option(section, name)
  72. except:
  73. Damnlog('No such section/option:', section, '/', name)
  74. def lists(self, section):
  75. prefs = []
  76. if DV.preference_order.has_key(section):
  77. prefs.extend(DV.preference_order[section])
  78. if self.ini.has_section(section):
  79. for i in self.ini.options(section):
  80. if i not in prefs:
  81. prefs.append(i)
  82. if len(prefs):
  83. return prefs
  84. Damnlog('No such section:', section)
  85. def listsections(self):
  86. return self.ini.sections()
  87. def get(self, name):
  88. return self.gets('damnvid', name)
  89. def set(self, name, value):
  90. return self.sets('damnvid', name, value)
  91. def getp(self, profile, name):
  92. if int(profile) == -1:
  93. if name.lower() == 'name':
  94. return '(Do not encode)'
  95. if name.lower() == 'outdir':
  96. return self.get('defaultoutdir')
  97. return self.gets('damnvid-profile-' + str(profile), name)
  98. def setp(self, profile, name, value):
  99. return self.sets('damnvid-profile-' + str(profile), name, value)
  100. def listp(self, profile):
  101. return self.lists('damnvid-profile-' + str(profile))
  102. def getm(self, module, name):
  103. return self.gets('damnvid-module-' + module, name)
  104. def setm(self, module, name, value):
  105. return self.sets('damnvid-module-' + module, name, value)
  106. def addp(self):
  107. self.ini.add_section('damnvid-profile-' + str(self.profiles))
  108. for i in DV.defaultprefs.iterkeys():
  109. if i[0:16] == 'damnvid-profile:':
  110. self.setp(self.profiles, i[16:], DamnUnicode(DV.defaultprefs[i]))
  111. self.profiles += 1
  112. def remp(self, profile):
  113. if self.profiles > 1:
  114. for i in DV.preferences.iterkeys():
  115. section, option = (i[0:i.find(':')], i[i.find(':') + 1:])
  116. if DV.preferences[i]['kind'] == 'profile':
  117. if int(self.gets(section, option)) == int(profile):
  118. self.ini.set(section, option, '0') # Fall back to default profile
  119. elif int(self.gets(section, option)) > int(profile):
  120. self.ini.set(section, option, str(int(self.gets(section, option)) - 1))
  121. for i in DamnIterModules():
  122. for j in DV.modules[i]['preferences'].iterkeys():
  123. if DV.modules[i]['preferences'][j]['kind'] == 'profile':
  124. if int(self.getm(DV.modules[i]['name'], j)) == int(profile):
  125. self.setm(DV.modules[i]['name'], j, '0') # Fall back to default profile
  126. elif int(self.getm(DV.modules[i]['name'], j)) > int(profile):
  127. self.setm(DV.modules[i]['name'], j, str(int(self.getm(DV.modules[i]['name'], j)) - 1))
  128. for i in range(profile, self.profiles - 1):
  129. for j in self.ini.options('damnvid-profile-' + str(i)):
  130. self.ini.remove_option('damnvid-profile-' + str(i), j)
  131. for j in self.ini.options('damnvid-profile-' + str(i + 1)):
  132. self.ini.set('damnvid-profile-' + str(i), j, self.ini.get('damnvid-profile-' + str(i + 1), j))
  133. self.profiles -= 1
  134. self.ini.remove_section('damnvid-profile-' + str(self.profiles))
  135. return self.profiles
  136. return None
  137. def geta(self, section, name):
  138. try:
  139. array = eval(base64.b64decode(self.gets(section, name)))
  140. except:
  141. array = []
  142. unicodearray = []
  143. for i in array:
  144. unicodearray.append(DamnUnicode(i))
  145. return unicodearray
  146. def seta(self, section, name, value):
  147. return self.sets(section, name, base64.b64encode(DamnUnicode(value)))
  148. def save(self):
  149. f = DamnOpenFile(DV.conf_file, 'w')
  150. self.ini.write(f)
  151. f.close()
  152. DamnURLOpener()
  153. def DamnLoadConfig(forcemodules=False):
  154. Damnlog('Loading config.')
  155. DV.preferences = None
  156. DamnExecFile(DV.curdir + u'conf' + DV.sep + u'preferences.d', globs=globals())
  157. DV.path_prefs = []
  158. DV.defaultprefs = {
  159. }
  160. for i in DV.preferences.iterkeys():
  161. if DV.preferences[i].has_key('default'):
  162. DV.defaultprefs[i] = DV.preferences[i]['default']
  163. else:
  164. DV.defaultprefs[i] = None
  165. if DV.preferences[i]['kind'] == 'dir':
  166. DV.path_prefs.append(i)
  167. DV.prefs = None # Will be loaded later
  168. # Load modules
  169. Damnlog('Loading modules.')
  170. DV.modules_path = DV.conf_file_directory + 'modules' + DV.sep
  171. if not os.path.exists(DV.modules_path):
  172. os.makedirs(DV.modules_path)
  173. DV.modules = {}
  174. DV.modulesstorage = {}
  175. DV.generic_title_extract = re.compile('<title>\s*([^<>]+?)\s*</title>', re.IGNORECASE)
  176. DV.listicons.resetList({
  177. 'damnvid':DV.images_path + 'video.png',
  178. 'generic':DV.images_path + 'online.png'
  179. })
  180. if forcemodules or '--rebuild-modules' in DV.argv:
  181. Damnlog('forcemodules is on; resetting modules.')
  182. shutil.rmtree(DV.modules_path)
  183. os.makedirs(DV.modules_path)
  184. if '--rebuild-modules' in DV.argv: # DEBUG ONLY; rebuilds all modules
  185. Damnlog('Careful, rebuilding all modules!')
  186. DV.argv = [x for x in DV.argv if x != '--rebuild-modules']
  187. for i in os.listdir('.'):
  188. if i.lower().endswith(u'.module.' + DV.safeProduct):
  189. os.remove(i)
  190. for i in os.listdir(DV.curdir + 'modules/'):
  191. if i.lower().endswith(u'.module.' + DV.safeProduct):
  192. os.remove(DV.curdir + 'modules/' + i)
  193. for i in os.listdir(DV.curdir + 'modules'):
  194. if os.path.isdir(DV.curdir + 'modules/' + i) and i.find('svn') == -1:
  195. Damnlog('Building module ' + i)
  196. DamnSpawner(['python', 'build-any/module-package.py', DV.curdir + 'modules/' + i ], cwd=DV.curdir).wait()
  197. for i in os.listdir(DV.curdir):
  198. if i.lower().endswith(u'.module.' + DV.safeProduct):
  199. os.rename(DV.curdir + i, DV.curdir + 'modules/' + i)
  200. for i in os.listdir(DV.curdir + 'modules'):
  201. if i.lower().endswith(u'.module.' + DV.safeProduct):
  202. Damnlog('Installing', i)
  203. DamnInstallModule(DV.curdir + 'modules' + DV.sep + i)
  204. for i in os.listdir(DV.modules_path):
  205. if os.path.isdir(DV.modules_path + i):
  206. DamnLoadModule(DV.modules_path + i)
  207. # End load modules