| 1 |
#!/usr/bin/env python |
| 2 |
######################################################################## |
| 3 |
# This is transmission-twitter, program that removes and notifies # |
| 4 |
# about completed torrents in transmission-daemon # |
| 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 |
|
| 19 |
|
| 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 |
| 30 |
This method reads the config file and returns the config information as a |
| 31 |
dictionary. If configfile is not present or invalid default configuration is returned. |
| 32 |
|
| 33 |
""" |
| 34 |
|
| 35 |
config_parser = ConfigParser.ConfigParser() |
| 36 |
configfile = os.getenv('HOME')+'/.config/transmission-daemon/plugins/transmission-scripts.conf' |
| 37 |
config_parser.read( |
| 38 |
['transmission-scripts.conf',configfile] |
| 39 |
) |
| 40 |
config = dict() |
| 41 |
|
| 42 |
try: |
| 43 |
config["AUTOSTOP_FINISHED_TORRENTS"] = config_parser.getboolean( |
| 44 |
'Transmission',"AUTOSTOP_FINISHED_TORRENTS") |
| 45 |
|
| 46 |
except (ValueError,ConfigParser.NoSectionError, |
| 47 |
ConfigParser.NoOptionError): |
| 48 |
config["AUTOSTOP_FINISHED_TORRENTS"] = True |
| 49 |
|
| 50 |
try: |
| 51 |
config["TwitterIntegration"] = config_parser.getboolean( |
| 52 |
'TwitterIntegration','ENABLED') |
| 53 |
except(ValueError,ConfigParser.NoSectionError): |
| 54 |
config["TwitterIntegration"] = True |
| 55 |
|
| 56 |
if config["TwitterIntegration"]: |
| 57 |
try: |
| 58 |
config["CONSUMER_KEY"] = config_parser.get( |
| 59 |
'TwitterIntegration', |
| 60 |
'CONSUMER_KEY') |
| 61 |
config["CONSUMER_SECRET"] = config_parser.get( |
| 62 |
'TwitterIntegration', |
| 63 |
'CONSUMER_SECRET' |
| 64 |
) |
| 65 |
except(ValueError,ConfigParser.NoSectionError, |
| 66 |
ConfigParser.NoOptionError): |
| 67 |
config["CONSUMER_KEY"] = "PbOGMLYLlsPtISZilEVeXw" |
| 68 |
config["CONSUMER_SECRET"] = "tNyHTMW0BfPW6Us1XjrLq1aU0ach42k8c8L7v6g2Ok" |
| 69 |
|
| 70 |
try: |
| 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): |
| 79 |
#User has to authenticate this app to get the below keys |
| 80 |
config["ACCESS_TOKEN"] = "" |
| 81 |
config["ACCESS_TOKEN_SECRET"] = "" |
| 82 |
|
| 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): |
| 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' |
| 101 |
config["SCREEN_NAME"] = "" |
| 102 |
|
| 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 |
""" |
| 111 |
|
| 112 |
config_parser = ConfigParser.ConfigParser() |
| 113 |
configfile = os.getenv('HOME')+'/.config/transmission-daemon/plugins/transmission-scripts.conf' |
| 114 |
config_parser.read(['transmission-scripts.conf',configfile]) |
| 115 |
|
| 116 |
try: |
| 117 |
config_parser.set(section,key,value) |
| 118 |
except(ConfigParser.NoSectionError,ConfigParser.NoOptionError): |
| 119 |
#seems like the config file is not there |
| 120 |
return False |
| 121 |
|
| 122 |
try: |
| 123 |
with open(configfile,'w') as file: |
| 124 |
config_parser.write(file) |
| 125 |
except IOError: |
| 126 |
return False |
| 127 |
|
| 128 |
return True |
| 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() |
| 137 |
self.assertEquals({"AUTOSTOP_FINISHED_TORRENTS": True, |
| 138 |
"TwitterIntegration": False},cf.get_config()) |
| 139 |
# self.assertRaises(ConfigParser.NoSectionError,get_config,'transmission-scripts.conf') |
| 140 |
|
| 141 |
def test_add_config(self): |
| 142 |
cf = ConfigFile() |
| 143 |
self.assertEquals(1,cf.write_config('TwitterIntegration', |
| 144 |
'ACCESS_TOKEN','aebcd')) |
| 145 |
|
| 146 |
if __name__ == '__main__': |
| 147 |
#unittest.main() |
| 148 |
cf = ConfigFile() |
| 149 |
config = cf.get_config() |
| 150 |
print config |