Added os.path.join() to a few filesystem related string operations.
[cmless] / bin / misc.py
CommitLineData
d955cfad
AT
1# (c) 2017 Aaron Taylor <ataylor at subgeniuskitty dot com>
2# See LICENSE file for copyright and license details.
3
4# Python imports
1debf034 5import os, sys, subprocess, configparser
d955cfad
AT
6
7# CMless imports
1debf034 8import debug, config, template
d955cfad 9
d955cfad 10def load_file(path):
78e2ec48 11 """Open the text file at 'path' and return the contents as a string."""
d955cfad
AT
12 try:
13 with open(path) as f:
14 contents = f.read()
15 except:
16 if debug.print_to_browser: print("Unable to open " + path + " for reading.")
17 sys.exit("Unable to open " + path + " for reading.")
18 return contents
19
d955cfad 20def process_markup(text):
78e2ec48
AT
21 """Execute external markup processor and return processed 'text'.
22
23 Markup processor is specified by config.markup_processor variable
24 and should accept markup on stdin while returning HTML on stdout.
25 """
d955cfad
AT
26 try:
27 p = subprocess.Popen(config.markup_processor,
28 stdout=subprocess.PIPE,stdin=subprocess.PIPE)
29 except:
30 if debug.print_to_browser: print("Unable to open markup processor: " +
31 config.markup_processor)
32 sys.exit("Unable to open markup processor: " + config.markup_processor)
33 text = p.communicate(text.encode('UTF-8'))[0]
34 return(text.decode('UTF-8'))
1debf034
AT
35
36def strip_page_metadata(content):
78e2ec48 37 """Strip metadata from the beginning of 'content' and return whatever remains."""
1debf034
AT
38 keyword = template.add_delimiter("END_PAGE_METADATA")
39 index = content.find(keyword)
40 if index != -1:
41 return content[index+len(keyword):]
42 else:
43 return content
44
78e2ec48
AT
45def lookup_current_metadata(key):
46 """Wrapper for lookup_file_metadata() supplying the current REQUEST_URI as 'file'."""
fafb94d2
AT
47 file = os.path.join(config.site_data_prefix + os.environ['REQUEST_URI'] +
48 config.markup_file_extension)
78e2ec48
AT
49 return lookup_file_metadata(key,file)
50
51def lookup_file_metadata(key,file):
52 """Read 'key' metadata in 'file' and return corresponding value.
53
54 Assumes key is contained in the [DEFAULT] category.
55 Returns empty string if key is not present.
56 """
57 page_file = load_file(file)
1debf034 58
78e2ec48 59 # Extract just the metadata block so we can pass it to ConfigParser.
1debf034
AT
60 keyword_begin = template.add_delimiter("BEGIN_PAGE_METADATA")
61 keyword_end = template.add_delimiter("END_PAGE_METADATA")
62 index = page_file.find(keyword_end)
63 if index != -1:
64 page_file = page_file[len(keyword_begin):index]
65 else:
78e2ec48 66 # ConfigParser will accept an empty string so use that as fallback.
1debf034
AT
67 page_file = ""
68
69 page_metadata = configparser.ConfigParser()
70 page_metadata.read_string(page_file)
71
72 if 'DEFAULT' in page_metadata and key in page_metadata['DEFAULT']:
73 return page_metadata['DEFAULT'][key]
74 else:
78e2ec48
AT
75 # An empty string is technically a valid value for a key but we will
76 # overload it as also indicating 'key' wasn't present since a key
77 # without a value is functionally equivalent to no key at all for our
78 # purposes.
1debf034 79 return ""