configuration.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import os
  2. import gettext
  3. import sys
  4. import sqlite3
  5. # -*- coding: utf8 -*-
  6. class conf(object):
  7. def __init__(self):
  8. self.values = {
  9. "database" : "stov.sqlite",
  10. "downloaddir" : str(os.environ['HOME']) + "/stov",
  11. "maxvideos" : "15",
  12. "mailhost" : "localhost",
  13. "mailto" : "root",
  14. "mailfrom" : "stov@localhost",
  15. "smtpport" : "25"
  16. }
  17. self.dbpath = str(os.environ['HOME']) + "/.stov/" + self.values["database"]
  18. def WriteConfig(self):
  19. try:
  20. self.configfile = open(str(os.environ['HOME']) + "/.stov/stov.config", "w")
  21. for key in self.values.iterkeys():
  22. self.configfile.write(key.upper() + "=" + self.values[key] + "\n")
  23. self.configfile.close()
  24. except IOError, os.error:
  25. print >> sys.stderr, _("Configuration could not be written, please check that the configuration directory exists and is writable")
  26. def CreateDB(self):
  27. try:
  28. self.__database = sqlite3.connect(self.dbpath)
  29. self.__dbcursor = self.__database.cursor()
  30. self.__dbcursor.execute("""CREATE TABLE subscriptions (
  31. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  32. title TEXT,
  33. name TEXT,
  34. type TEXT,
  35. searchstring TEXT,
  36. directory TEXT
  37. );""")
  38. self.__dbcursor.execute("""CREATE TABLE videos (
  39. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  40. title TEXT,
  41. description TEXT,
  42. ytid TEXT,
  43. subscription_id INTEGER,
  44. downloaded int
  45. );""")
  46. self.__database.commit()
  47. self.__database.close()
  48. except sqlite3.OperationalError:
  49. print >> sys.stderr, _("The database could not be created, please check that the configuration directory exists and is writable")
  50. def Initialize(self):
  51. try:
  52. os.mkdir(str(os.environ['HOME']) + "/.stov", 0750)
  53. self.WriteConfig()
  54. except os.error:
  55. print >> sys.stderr, _("Configuration directory could not be created, please check that your home directory exists and is writable")
  56. self.CreateDB()
  57. def ReadConfig(self):
  58. try:
  59. self.configfile = open(str(os.environ['HOME']) + "/.stov/stov.config", "r")
  60. except IOError:
  61. print >> sys.stderr, _("Configuration could not be read, please check that the configuration file exists and is readable")
  62. for lines in self.configfile:
  63. self.tmpline = lines.strip()
  64. self.tmplist = self.tmpline.split("=")
  65. self.values[self.tmplist[0].lower()] = self.tmplist[1]
  66. self.configfile.close()
  67. self.dbpath = str(os.environ['HOME']) + "/.stov/" + self.values["database"]