|
4 | 4 | import time
|
5 | 5 | import sys
|
6 | 6 | from os.path import dirname, abspath, join as pathjoin
|
| 7 | +import argparse |
7 | 8 |
|
8 | 9 | root_dir = dirname(abspath(__file__))
|
9 | 10 | print("Running from {}".format(root_dir))
|
|
22 | 23 | strClearLogsCommand = 'truncate -s 0 {}/adhan.log 2>&1'.format(root_dir)
|
23 | 24 | strJobComment = 'rpiAdhanClockJob'
|
24 | 25 |
|
25 |
| -#Set latitude and longitude here |
26 |
| -#-------------------- |
27 |
| -lat = 42.288788 |
28 |
| -long = -71.551678 |
29 |
| - |
30 |
| -#Set calculation method, utcOffset and dst here |
31 |
| -#By default system timezone will be used |
32 |
| -#-------------------- |
33 |
| -PT.setMethod('ISNA') |
34 |
| -utcOffset = -(time.timezone/3600) |
35 |
| -isDst = time.localtime().tm_isdst |
36 |
| - |
37 |
| - |
38 | 26 | #HELPER FUNCTIONS
|
39 | 27 | #---------------------------------
|
40 | 28 | #---------------------------------
|
41 | 29 | #Function to add azaan time to cron
|
| 30 | +def parseArgs(): |
| 31 | + parser = argparse.ArgumentParser(description='Calculate prayer times and install cronjobs to play Adhan') |
| 32 | + parser.add_argument('--lat', type=float, dest='lat', |
| 33 | + help='Latitude of the location, for example 30.345621') |
| 34 | + parser.add_argument('--lng', type=float, dest='lng', |
| 35 | + help='Longitude of the location, for example 60.512126') |
| 36 | + parser.add_argument('--method', choices=['MWL', 'ISNA', 'Egypt', 'Makkah', 'Karachi', 'Tehran', 'Jafari'], |
| 37 | + dest='method', |
| 38 | + help='Method of calculation') |
| 39 | + return parser |
| 40 | + |
| 41 | +def mergeArgs(args): |
| 42 | + file_path = pathjoin(root_dir, '.settings') |
| 43 | + # load values |
| 44 | + lat = lng = method = None |
| 45 | + try: |
| 46 | + with open(file_path, 'rt') as f: |
| 47 | + lat, lng, method = f.readlines()[0].split(',') |
| 48 | + except: |
| 49 | + print('No .settings file found') |
| 50 | + # merge args |
| 51 | + if args.lat: |
| 52 | + lat = args.lat |
| 53 | + if args.lng: |
| 54 | + lng = args.lng |
| 55 | + if args.method: |
| 56 | + method = args.method |
| 57 | + # save values |
| 58 | + with open(file_path, 'wt') as f: |
| 59 | + f.write('{},{},{}'.format(lat or '', lng or '', method or '')) |
| 60 | + return lat or None, lng or None, method or None |
| 61 | + |
42 | 62 | def addAzaanTime (strPrayerName, strPrayerTime, objCronTab, strCommand):
|
43 | 63 | job = objCronTab.new(command=strCommand,comment=strPrayerName)
|
44 | 64 | timeArr = strPrayerTime.split(':')
|
@@ -70,6 +90,24 @@ def addClearLogsCronJob (objCronTab, strCommand):
|
70 | 90 | #---------------------------------
|
71 | 91 | #HELPER FUNCTIONS END
|
72 | 92 |
|
| 93 | +#Parse arguments |
| 94 | +parser = parseArgs() |
| 95 | +args = parser.parse_args() |
| 96 | +#Merge args with saved values if any |
| 97 | +lat, lng, method = mergeArgs(args) |
| 98 | +print(lat, lng, method) |
| 99 | +#Complain if any value is missing |
| 100 | +if not lat or not lng or not method: |
| 101 | + parser.print_usage() |
| 102 | + sys.exit(1) |
| 103 | + |
| 104 | +#Set calculation method, utcOffset and dst here |
| 105 | +#By default system timezone will be used |
| 106 | +#-------------------- |
| 107 | +PT.setMethod(method) |
| 108 | +utcOffset = -(time.timezone/3600) |
| 109 | +isDst = time.localtime().tm_isdst |
| 110 | + |
73 | 111 | # Remove existing jobs created by this script
|
74 | 112 | system_cron.remove_all(comment=strJobComment)
|
75 | 113 |
|
|
0 commit comments