Completed
Push — development ( 598f55...e44a14 )
by Thomas
01:10
created

doorpi.docs.service.parse_string()   A

Complexity

Conditions 3

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 5
rs 9.4285
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
4
import doorpi.metadata as metadata
5
import os
6
import urllib2
7
8
print("start to create daemon file now...")
9
10
11
def parse_string(raw_string):
12
    for meta_key in dir(metadata):
13
        if not meta_key.startswith('__'):
14
            raw_string = raw_string.replace('!!%s!!' % meta_key,  str(getattr(metadata, meta_key)))
15
    return raw_string
16
17
18
def main():
19
    url = 'https://raw.githubusercontent.com/motom001/DoorPi/master/'+metadata.daemon_name_template
20
    daemon_filename = os.path.join(metadata.daemon_folder, metadata.daemon_name)
21
    print("start down download and parse new daemon file:")
22
    print("URL:  "+url)
23
    print("FILE: "+daemon_filename)
24
    with open(daemon_filename, "w") as daemon_file:
25
        for line in urllib2.urlopen(url):
26
            daemon_file.write(parse_string(line))
27
        print("download successfully - change chmod to 0755 now")
28
        os.chmod(daemon_filename, 0755)
29
        print "finished"
30
31
if __name__ == '__main__':
32
    main()
33