-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.cgi
executable file
·149 lines (111 loc) · 5.13 KB
/
components.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!./python
# Program: components.cgi
# Purpose: to provide the mappings from names and aliases to their
# corresponding web components.
# Usage: This script accepts two parameters...
# 'format' : 'tag' (default), 'url', or 'rcd'
# 'name' : optional name/alias for a shared web compoment
# If neither parameter is specified, you will get all the mappings from
# names/aliases to HTML tags. Each line has a name/alias, a carat (^),
# and then the tag.
# add the path to the standard MGI python libraries
import sys
import os
if "LIB_PY_MISC" not in os.environ:
sys.path.insert (0, '/usr/local/mgi/live/lib/python/')
else:
sys.path.insert (0, os.environ["LIB_PY_MISC"])
# Attempt to import the module that will ask Python to ignore any
# deprecation errors. If it fails, ignore it and go forward.
try:
import ignoreDeprecation
except:
pass
import Configuration
# configuration file for this product
config = Configuration.get_Configuration ("webshare.config")
import CGI
import webshare
###-------------------------------###
###--- public global variables ---###
###-------------------------------###
# standard exception raised in this CGI script
error = 'ComponentsCGI.error'
# possible values passed along when 'error' is raised...
UNKNOWN_FORMAT = "Unknown value for the 'format' paramter. " + \
"Must be 'rcd', 'url', or 'tag'."
UNKNOWN_NAME = "Unknown value for the 'name' parameter."
CONFIG_ERROR = "Error in configuration file: %s"
###-----------------------###
###--- private classes ---###
###-----------------------###
class ComponentsCGI (CGI.CGI):
# IS: this CGI script, charged with returning mappings from a web
# components name/alias to its tag, URL, or RcdFile entry.
# HAS: a set of parameters from the user, and a set of shared
# web components
# DOES: see IS
def main (self):
# Purpose: contains the main logic for this CGI script
# Returns: nothing
# Assumes: nothing
# Effects: writes to stdout when giving results
# Throws: 'error' if problems crop up.
# get input parameters
parms = self.get_parms()
name = None # default is to do all
if 'name' in parms:
name = parms['name']
format = 'tag' # default returns HTML tags
if 'format' in parms:
format = parms['format'].lower()
# check validity of format parameters
if format not in [ 'rcd', 'url', 'tag' ]:
raise error(UNKNOWN_FORMAT)
# get the data from the rcd file
rcdfile_path = config['RCDFILE_PATH']
components = webshare.SharedComponents (rcdfile_path)
# if user is requesting data for a single shared component...
if name:
# retrieve the data for the individual component
# requested, if possible
component = components.get(name)
if not component:
raise error(UNKNOWN_NAME)
# if requesting an HTML tag...
if format == 'tag':
print(component.getHtmlTag())
# if requesting just the URL to the component
elif format == 'url':
print(component.getUrl())
# if requesting full Rcd of info for this component
else: # format == 'rcd'
print(component.getRcd())
return
# otherwise, user is requesting data for all components...
# list of output lines
lines = []
# if user is requesting lines with 'name <HTML tag>' for each
if format == 'tag':
namesAliases = components.getNames() + \
components.getAliases()
for name in namesAliases:
lines.append ('%s^%s' % (name,
components.get(name).getHtmlTag() ) )
# if user is requesting lines with 'name URL' in each
elif format == 'url':
for name in components.getNames() + \
components.getAliases():
lines.append ('%s^%s' % (name,
components.get(name).getUrl() ) )
# if user is requesting the contents of the full Rcd file
else: # format == 'rcd'
lines.append (components.getRcdFile())
print('\n'.join(lines))
return
###--------------------###
###--- Main Program ---###
###--------------------###
if __name__ == '__main__':
myCgi = ComponentsCGI()
myCgi.go()