~tj/pastebin/review_paste

« back to all changes in this revision

Viewing changes to pastebinit

  • Committer: Marius Gedminas
  • Date: 2009-12-30 13:35:24 UTC
  • mto: This revision was merged to the branch mainline in revision 71.
  • Revision ID: marius@gedmin.as-20091230133524-x6rrm3jkyj0u0czg
Improve config file parsing:

  - look for config files in three locations (/etc, source checkout, user's
    home directory)

  - skip nonexisting directories

  - report clear errors about config files, especially mentioning the filename

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
try:
21
21
    import urllib, os, sys, re, getopt, select, xml.dom.minidom, gettext
22
22
    from gettext import gettext as _
23
 
    from configobj import ConfigObj
 
23
    import configobj
24
24
 
25
25
    gettext.textdomain("pastebinit")
26
26
 
34
34
            return None
35
35
 
36
36
    def preloadPastebins():
37
 
        confdir = '/etc/pastebin.d/'
38
 
        confdirlist = os.listdir(confdir)
39
 
        pastebind = {}
40
 
        for fileitem in confdirlist:
41
 
            bininstance = ConfigObj(confdir + fileitem)
42
 
            basename = bininstance['pastebin']['basename']
43
 
            pastebind[basename] = bininstance
44
 
        return pastebind
 
37
        # Check several places for config files:
 
38
        #  - global config in /etc/pastebin.d
 
39
        #  - for source checkout, config in the checkout
 
40
        #  - user's overrides in ~/.pastebin.d
 
41
        # Files found later override files found earlier.
 
42
        for confdir in ['/etc/pastebin.d',
 
43
                        os.path.join(os.path.dirname(__file__), 'pastebin.d'),
 
44
                        os.path.expanduser('~/.pastebin.d')]:
 
45
            try:
 
46
                confdirlist = os.listdir(confdir)
 
47
            except OSError:
 
48
                continue
 
49
            pastebind = {}
 
50
            for fileitem in confdirlist:
 
51
                if fileitem.startswith('.'):
 
52
                    continue
 
53
                filename = os.path.join(confdir, fileitem)
 
54
                try:
 
55
                    bininstance = configobj.ConfigObj(filename)
 
56
                except configobj.ConfigObjError, e:
 
57
                    print >> sys.stderr, '%s: %s' % (filename, e)
 
58
                    continue
 
59
                try:
 
60
                    section = bininstance['pastebin']
 
61
                except KeyError:
 
62
                    print >> sys.stderr, _('%s: no section [pastebin]') % filename
 
63
                    continue
 
64
                try:
 
65
                    basename = section['basename']
 
66
                except KeyError:
 
67
                    print >> sys.stderr, _("%s: no 'basename' in [pastebin]") % filename
 
68
                    continue
 
69
                pastebind[basename] = bininstance
 
70
            return pastebind
45
71
 
46
72
    # pastey.net obfuscates parent ids for replies.  Rather than taking the
47
73
    # post ID given as the parent ID, we must handle this by going to that