Skip to content

Commit

Permalink
CSV -> JSON (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsmuc committed Nov 3, 2018
1 parent 71ca3a1 commit 4f21f0d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
10 changes: 10 additions & 0 deletions files/imap_accounts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{ "account":
{"server": "imap.example.net",
"user": "username",
"password": "examplepassword",
"junk_folder": "Junk",
"inbox_folder": "INBOX",
"ham_train_folder": "HAM",
"spam_train_folder": "SPAMTrain"
}
}
2 changes: 0 additions & 2 deletions files/imap_accounts.txt

This file was deleted.

23 changes: 13 additions & 10 deletions files/pushtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from imapclient import IMAPClient
import subprocess
import csv
import json
import sys
import logging
from logging.handlers import TimedRotatingFileHandler
Expand All @@ -29,16 +29,19 @@
# TODO: will raise an exception, if default configuration is installed
# read account information
try:
account = list(csv.reader(open('/root/accounts/imap_accounts.txt', 'r'), delimiter='\t'))
HOST = account[1][0]
USERNAME = account[1][1]
PASSWORD = account[1][2]
JUNK = account[1][3]
INPUT = account[1][4]
HAMTRAIN = account[1][5]
SPAMTRAIN = account[1][6]

with open("/root/accounts/imap_accounts.json", 'r') as f:
datastore = json.load(f)

HOST = datastore["account"]["server"]
USERNAME = datastore["account"]["user"]
PASSWORD = datastore["account"]["password"]
JUNK = datastore["account"]["junk_folder"]
INPUT = datastore["account"]["inbox_folder"]
HAMTRAIN = datastore["account"]["ham_train_folder"]
SPAMTRAIN = datastore["account"]["spam_train_folder"]
except IndexError:
print("ERROR: was not able to read imap_accounts.txt.")
print("ERROR: was not able to read imap_accounts.json.")
sys.exit(1)


Expand Down
15 changes: 8 additions & 7 deletions files/startup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from shutil import copyfile
import subprocess
import csv
import json
import sys


Expand Down Expand Up @@ -33,16 +33,17 @@ def start_service(servicename):

def check_imap_configuration():
""" check if the IMAP account has already been configured"""
account = list(csv.reader(open('/root/accounts/imap_accounts.txt', 'r'), delimiter='\t'))

try:
HOST = account[1][0]
with open("/root/accounts/imap_accounts.json", 'r') as f:
datastore = json.load(f)
HOST = datastore["account"]["server"]
except IndexError:
print ("ERROR: was not able to read imap_accounts.txt.")
print ("ERROR: was not able to read imap_accounts.json.")
sys.exit()

if HOST == "imap.example.net":
print("ERROR: no accounts in imap_accounts.txt configured - please configure and restart")
print("ERROR: no accounts in imap_accounts.json configured - please configure and restart")
sys.exit()


Expand Down Expand Up @@ -96,8 +97,8 @@ def start_imap_idle():
cleanup_file("/var/spamassassin/scan_lock")
cleanup_file("/root/.cache/isbg/lock")

print("\n\n *** copy imap_accounts.txt files")
copy_file_if_not_exists("/root/imap_accounts.txt", "/root/accounts/imap_accounts.txt")
print("\n\n *** copy imap_accounts.json file")
copy_file_if_not_exists("/root/imap_accounts.json", "/root/accounts/imap_accounts.json")

print("\n\n *** fixing permissions")
fix_permissions()
Expand Down

0 comments on commit 4f21f0d

Please sign in to comment.