more setuptools work
[Comic.py.git] / src / Comic / __init__.py
blob49538dbb8a01634726eca7d67945faa87a663b6e
1 #!/usr/bin/python
2 ###########################################################################
3 # Copyright (C) 2007 by Andrew Mahone
4 # <andrew.mahone@gmail.com>
6 # Copyright: See COPYING file that comes with this distribution
8 ###########################################################################
9 """support modules for scraping and viewing webcomics"""
10 import sys, os.path, yaml, re, sqlobject, socket
12 __all__ = [
13 'DB',
14 'Config',
15 'Fetch',
16 'Magic',
17 'NameEnc',
18 'Conf',
19 'InitDB',
20 'InitConf',
21 'Session',
24 if hasattr(yaml,'CSafeDumper'):
25 Dumper = yaml.CSafeDumper
26 else:
27 Dumper = yaml.SafeDumper
29 if hasattr(yaml,'CSafeLoader'):
30 Loader = yaml.CSafeLoader
31 else:
32 Loader = yaml.SafeLoader
34 class ComicLoader(Loader): pass
36 ComicLoader.add_constructor('!re', lambda x,y: re.compile(x.construct_scalar(y)))
38 if sys.platform[:3].lower() == 'win':
39 win = True
40 AppDir = os.path.join(os.getenv('APPDATA') or os.getcwd(), 'comic-get')
41 SysAppDir = os.path.join(os.getenv('PROGRAMFILES'), 'comic_get')
42 SysConfDir = SysAppDir
43 else:
44 win = False
45 AppDir = os.path.join(os.getenv('HOME') or os.getcwd(), '.comic_get')
46 SysAppDir = os.path.join('/usr/share', 'comic_get')
47 SysConfDir = os.path.join('/etc', 'comic_get')
49 Conf = None
51 def InitConf():
52 global Conf
53 if not Conf is None:
54 return
55 from Comic import Config
56 cdb = os.path.abspath(os.path.join(AppDir,'comics.db'))
57 if win:
58 cdb = cdb.replace('\\','/').replace(':','|',1)
59 cdb = 'sqlite:/' + cdb
60 else:
61 cdb = 'sqlite://' + cdb
62 cdb += "?timeout=15"
64 DefConfig = Config.Config(
65 comic_store=os.path.join(AppDir,'comics'),
66 session_store=os.path.join(AppDir,'sessions'),
67 comic_db=cdb,
68 dl_attempts=5,
69 dl_delay=1.0,
70 dl_delay_rand=0.2,
71 dl_delay_mul=1.8,
72 sock_timeout=5,
73 threads=5,
75 SysConfigFile = os.path.join(SysConfDir, 'config.yml')
76 UsrConfigFile = os.path.join(AppDir, 'config.yml')
77 if not os.path.isdir(AppDir):
78 os.makedirs(AppDir)
79 Conf = Config.Config()
80 Conf.update(DefConfig)
81 Conf.update(Config.Config(SysConfigFile))
82 Conf.update(UsrConfigFile)
83 Conf._save()
84 if not Conf:
85 Conf._parent._parent._save(UsrConfigFile)
86 if 'sock_timeout' in Conf:
87 socket.setdefaulttimeout(Conf.sock_timeout)
89 def InitDB():
90 from Comic import DB
91 DB.SetThreadConnection()
92 for obj in (
93 DB.Comic,
94 DB.Issue,
95 DB.Image,
96 DB.User,
97 DB.UserComic,
99 obj.createTable(ifNotExists=True)
101 InitConf()