1
+ import json
2
+ import os
3
+
4
+ class Configuration ():
5
+ # default values
6
+ user_name = None
7
+ logo_file = None
8
+ countdown1 = 5 # seconds of preview before first snap
9
+ countdown2 = 3 # seconds of preview between snaps (Four pictures mode)
10
+ photoCaption = "" # Caption in the photo album
11
+ ARCHIVE = True # Do we archive photos locally
12
+ archive_dir = os .path .join (".." ,"Photos" ) # Where do we archive photos
13
+ albumID = None # use install_credentials.py to create 'album.id'
14
+ emailSubject = "Here's your photo!" # subject line of the email sent from the photobooth
15
+ emailMsg = "Greetings, here's your photo sent from the photobooth" # Brief body of the message sent from the photobooth
16
+ full_screen = True #Start application in full screen
17
+ enable_email = True #Enable the 'send email" feature
18
+ enable_upload = True #Enable the upload feature
19
+ enable_hardware_buttons = False #Enable hardware buttons
20
+
21
+ #init
22
+ def __init__ (self ,configuration_file_name ):
23
+ self .config_file = configuration_file_name
24
+ self .is_valid = False
25
+ self .__read_config_file ()
26
+
27
+ def __read_config_file (self ):
28
+ self .is_valid = True
29
+ try :
30
+ with open (self .config_file , 'r' ) as content_file :
31
+ file_content = content_file .read ()
32
+ config = json .loads (file_content )
33
+ except Exception as error :
34
+ print "Error while parsing %s config file : %s" % (self .config_file ,error )
35
+ self .is_valid = False
36
+ return False
37
+ if "gmail_user" in config .keys (): self .user_name = config ['gmail_user' ]
38
+ else :
39
+ #mandatory configuration!!
40
+ self .is_valid = False
41
+
42
+
43
+ # all other configuration keys are optional
44
+ if "countdown_before_snap" in config .keys (): self .countdown1 = config ["countdown_before_snap" ]
45
+ if "countdown_inter_snap" in config .keys (): self .countdown2 = config ["countdown_inter_snap" ]
46
+ if "snap_caption" in config .keys (): self .photoCaption = config ["snap_caption" ]
47
+ if "local_archive" in config .keys (): self .ARCHIVE = config ["local_archive" ]
48
+ if "local_archive_dir" in config .keys (): self .archive_dir = config ["local_archive_dir" ]
49
+ if "google_photo_album_id" in config .keys (): self .albumID = config ["google_photo_album_id" ]
50
+ if "email_subject" in config .keys (): self .emailSubject = config ["email_subject" ]
51
+ if "email_body" in config .keys (): self .emailMsg = config ["email_body" ]
52
+ if "logo_file" in config .keys (): self .logo_file = config ["logo_file" ]
53
+ if "full_screen" in config .keys (): self .full_screen = config ["full_screen" ]
54
+ if "enable_email" in config .keys (): self .enable_email = config ["enable_email" ]
55
+ if "enable_upload" in config .keys (): self .enable_upload = config ["enable_upload" ]
56
+ if "enable_hardware_buttons" in config .keys (): self .enable_hardware_buttons = config ["enable_hardware_buttons" ]
57
+
58
+
59
+ return self .is_valid
60
+
61
+
62
+ def write (self ):
63
+ myconfig = {
64
+ "gmail_user" : self .user_name ,
65
+ "countdown_before_snap" : self .countdown1 ,
66
+ "countdown_inter_snap" : self .countdown2 ,
67
+ "snap_caption" : self .photoCaption ,
68
+ "local_archive" : self .ARCHIVE ,
69
+ "local_archive_dir" : self .archive_dir ,
70
+ "google_photo_album_id" : self .albumID ,
71
+ "email_subject" : self .emailSubject ,
72
+ "email_body" :self .emailMsg ,
73
+ "logo_file" : self .logo_file ,
74
+ "full_screen" : self .full_screen ,
75
+ "enable_email" : self .enable_email ,
76
+ "enable_upload" : self .enable_upload ,
77
+ "enable_hardware_buttons" : self .enable_hardware_buttons
78
+ }
79
+ try :
80
+ with open (self .config_file ,'w' ) as config :
81
+ config .write (json .dumps (myconfig , indent = 4 , sort_keys = True ))
82
+ except Exception as error :
83
+ raise ValueError ("Problem writing %s configuration file: %s" % (self .config_file ,error ))
84
+
85
+
86
+ if __name__ == "__main__" :
87
+
88
+ config = Configuration ("myconf.conf" )
89
+ if not config .is_valid :
90
+ config .write ()
0 commit comments