d257d1e by Arun SAG at 2011-04-23 1
#!/usr/bin/env python
2
########################################################################
e0836ff by Arun SAG at 2011-04-23 3
# This is transmission-twitter, program that removes and notifies      #
4
# about completed torrents in transmission-daemon                      #
d257d1e by Arun SAG at 2011-04-23 5
#                                                                      #
6
# This program is free software: you can redistribute it and/or modify #
7
# it under the terms of the GNU General Public License as published by #
8
# the Free Software Foundation, either version 3 of the License, or    #
9
# (at your option) any later version.                                  #
10
#                                                                      #
11
# This program is distributed in the hope that it will be useful,      #
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of       #
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        #
14
# GNU General Public License for more details:                         #
15
# http://www.gnu.org/licenses/gpl-3.0.txt                              #
16
########################################################################
17
18
e0836ff by Arun SAG at 2011-04-23 19
b044318 by Arun SAG at 2010-11-06 20
import ConfigParser
21
import unittest
22
import os
23
24
class ConfigFile:
25
26
    def get_config(self):
27
        """ Returns the current configuration in a dictionary 
28
        
29
        The default config file is in .config/transmission-daemon/plugins/ directory
d257d1e by Arun SAG at 2011-04-23 30
        This method reads the config file and returns the config information as a 
b044318 by Arun SAG at 2010-11-06 31
        dictionary. If configfile is not present or invalid default configuration is returned.
32
33
        """
d257d1e by Arun SAG at 2011-04-23 34
        
b044318 by Arun SAG at 2010-11-06 35
        config_parser = ConfigParser.ConfigParser()
36
        configfile = os.getenv('HOME')+'/.config/transmission-daemon/plugins/transmission-scripts.conf'
c9c76c2 by Arun SAG at 2011-02-07 37
        config_parser.read(
882fecf by Arun SAG at 2011-02-07 38
            ['transmission-scripts.conf',configfile]
c9c76c2 by Arun SAG at 2011-02-07 39
            )
b044318 by Arun SAG at 2010-11-06 40
        config = dict()
d257d1e by Arun SAG at 2011-04-23 41
        
b044318 by Arun SAG at 2010-11-06 42
        try:
c9c76c2 by Arun SAG at 2011-02-07 43
            config["AUTOSTOP_FINISHED_TORRENTS"] = config_parser.getboolean(
44
                'Transmission',"AUTOSTOP_FINISHED_TORRENTS")
d257d1e by Arun SAG at 2011-04-23 45
        
b0cfb32 by Arun SAG at 2011-02-06 46
        except (ValueError,ConfigParser.NoSectionError,
47
                ConfigParser.NoOptionError):
48
            config["AUTOSTOP_FINISHED_TORRENTS"] = True
d257d1e by Arun SAG at 2011-04-23 49
        
b044318 by Arun SAG at 2010-11-06 50
        try:
c9c76c2 by Arun SAG at 2011-02-07 51
            config["TwitterIntegration"] = config_parser.getboolean(
52
                'TwitterIntegration','ENABLED')
b044318 by Arun SAG at 2010-11-06 53
        except(ValueError,ConfigParser.NoSectionError):
b0cfb32 by Arun SAG at 2011-02-06 54
            config["TwitterIntegration"] = True
d257d1e by Arun SAG at 2011-04-23 55
        
b0cfb32 by Arun SAG at 2011-02-06 56
        if config["TwitterIntegration"]:
b044318 by Arun SAG at 2010-11-06 57
            try:
c9c76c2 by Arun SAG at 2011-02-07 58
                config["CONSUMER_KEY"] = config_parser.get(
59
                    'TwitterIntegration',
60
                    'CONSUMER_KEY')
61
                config["CONSUMER_SECRET"] = config_parser.get(
62
                    'TwitterIntegration',
63
                    'CONSUMER_SECRET'
d257d1e by Arun SAG at 2011-04-23 64
                    )                
b0cfb32 by Arun SAG at 2011-02-06 65
            except(ValueError,ConfigParser.NoSectionError,
66
                   ConfigParser.NoOptionError):
67
                config["CONSUMER_KEY"] = "PbOGMLYLlsPtISZilEVeXw"
68
                config["CONSUMER_SECRET"] = "tNyHTMW0BfPW6Us1XjrLq1aU0ach42k8c8L7v6g2Ok"
d257d1e by Arun SAG at 2011-04-23 69
            
70
            try:    
c9c76c2 by Arun SAG at 2011-02-07 71
                config["ACCESS_TOKEN"] = config_parser.get(
72
                    'TwitterIntegration',
73
                    'ACCESS_TOKEN')
74
                config["ACCESS_TOKEN_SECRET"] = config_parser.get(
75
                    'TwitterIntegration',
76
                    'ACCESS_TOKEN_SECRET')
77
            except(ValueError,ConfigParser.NoSectionError,
78
                   ConfigParser.NoOptionError):
b044318 by Arun SAG at 2010-11-06 79
                #User has to authenticate this app to get the below keys
b0cfb32 by Arun SAG at 2011-02-06 80
                config["ACCESS_TOKEN"] = ""
81
                config["ACCESS_TOKEN_SECRET"] = ""
d257d1e by Arun SAG at 2011-04-23 82
c9c76c2 by Arun SAG at 2011-02-07 83
            try:
84
                config["REQUEST_TOKEN_URL"] = config_parser.get(
85
                    'TwitterIntegration',
86
                    'REQUEST_TOKEN_URL')
87
                config["ACCESS_TOKEN_URL"] = config_parser.get(
88
                    'TwitterIntegration',
89
                    'ACCESS_TOKEN_URL')
90
                config["AUTHORIZE_URL"] = config_parser.get(
91
                    'TwitterIntegration',
92
                    'AUTHORIZE_URL')
93
                config["SCREEN_NAME"] = config_parser.get(
94
                    'TwitterIntegration',
95
                    'SCREEN_NAME')
96
            except(ValueError,ConfigParser.NoSectionError,
97
                   ConfigParser.NoOptionError):
34591e5 by Arun SAG at 2011-02-06 98
                config["REQUEST_TOKEN_URL"] = 'https://api.twitter.com/oauth/request_token'
99
                config["ACCESS_TOKEN_URL"] = 'https://api.twitter.com/oauth/access_token'
100
                config["AUTHORIZE_URL"] = 'https://api.twitter.com/oauth/authorize'
c9c76c2 by Arun SAG at 2011-02-07 101
                config["SCREEN_NAME"] = ""
d257d1e by Arun SAG at 2011-04-23 102
b044318 by Arun SAG at 2010-11-06 103
        return config
104
    
105
    def write_config(self,section,key,value):
106
        """ writes key and value in specified section of configfile 
107
        
108
        It writes to the default config file /.config/transmission-daemon/plugins/transmission-scripts.conf
109
        Generally used to write the oauth tokens to the config file
110
        """
d257d1e by Arun SAG at 2011-04-23 111
        
b044318 by Arun SAG at 2010-11-06 112
        config_parser = ConfigParser.ConfigParser()
113
        configfile = os.getenv('HOME')+'/.config/transmission-daemon/plugins/transmission-scripts.conf'
882fecf by Arun SAG at 2011-02-07 114
        config_parser.read(['transmission-scripts.conf',configfile])
d257d1e by Arun SAG at 2011-04-23 115
        
b044318 by Arun SAG at 2010-11-06 116
        try:
117
            config_parser.set(section,key,value)
b0cfb32 by Arun SAG at 2011-02-06 118
        except(ConfigParser.NoSectionError,ConfigParser.NoOptionError):
b044318 by Arun SAG at 2010-11-06 119
            #seems like the config file is not there
b0cfb32 by Arun SAG at 2011-02-06 120
            return False
d257d1e by Arun SAG at 2011-04-23 121
        
b044318 by Arun SAG at 2010-11-06 122
        try:
123
            with open(configfile,'w') as file:
124
                config_parser.write(file)
125
        except IOError:
b0cfb32 by Arun SAG at 2011-02-06 126
            return False
b044318 by Arun SAG at 2010-11-06 127
b0cfb32 by Arun SAG at 2011-02-06 128
        return True
b044318 by Arun SAG at 2010-11-06 129
130
131
class Testing(unittest.TestCase):
132
    """ Bunch of unit test cases """
133
    
134
    def test_get_config(self):
135
        """ Tests the get_config method """
136
        cf = ConfigFile()
b0cfb32 by Arun SAG at 2011-02-06 137
        self.assertEquals({"AUTOSTOP_FINISHED_TORRENTS": True,
138
                           "TwitterIntegration": False},cf.get_config())
b044318 by Arun SAG at 2010-11-06 139
       # self.assertRaises(ConfigParser.NoSectionError,get_config,'transmission-scripts.conf')
140
        
141
    def test_add_config(self):
142
        cf = ConfigFile()
b0cfb32 by Arun SAG at 2011-02-06 143
        self.assertEquals(1,cf.write_config('TwitterIntegration',
144
                                            'ACCESS_TOKEN','aebcd'))
b044318 by Arun SAG at 2010-11-06 145
146
if __name__ == '__main__':
b0cfb32 by Arun SAG at 2011-02-06 147
    #unittest.main()
148
    cf = ConfigFile()
149
    config = cf.get_config()
c9c76c2 by Arun SAG at 2011-02-07 150
    print config