| 
 | 1 | +# SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause  | 
 | 2 | +"""  | 
 | 3 | +Generic document construction classes.  | 
 | 4 | +
  | 
 | 5 | +These classes are templates for creating documents that are not bound  | 
 | 6 | +to a specific usage or data model.  | 
 | 7 | +
  | 
 | 8 | +Copyright (C) 2022 Intel Corporation <www.intel.com>  | 
 | 9 | +
  | 
 | 10 | +Author: Lee, Kah Jing <kah.jing.lee@intel.com>  | 
 | 11 | +"""  | 
 | 12 | + | 
 | 13 | +class document(object):  | 
 | 14 | + """  | 
 | 15 | + An abstract document class which does not dictate  | 
 | 16 | + how a document should be constructed or manipulated.  | 
 | 17 | +
  | 
 | 18 | + It's sole purpose is to describe the entire document  | 
 | 19 | + in smaller units  | 
 | 20 | + """  | 
 | 21 | + | 
 | 22 | + class entry(object):  | 
 | 23 | + """  | 
 | 24 | + An entry is the smallest unit  | 
 | 25 | + """  | 
 | 26 | + | 
 | 27 | + def __init__(self, parent):  | 
 | 28 | + """ entry initialization """  | 
 | 29 | + if parent != None:  | 
 | 30 | + parent.add(self)  | 
 | 31 | + | 
 | 32 | + class block(entry):  | 
 | 33 | + """  | 
 | 34 | + A block is the smallest collection unit  | 
 | 35 | + consists of entries and blocks.  | 
 | 36 | + """  | 
 | 37 | + | 
 | 38 | + def __init__(self, parent):  | 
 | 39 | + """ block initialization """  | 
 | 40 | + super(document.block, self).__init__(parent)  | 
 | 41 | + self.entries = []  | 
 | 42 | + | 
 | 43 | + def add(self, entry):  | 
 | 44 | + """ add entry to block """  | 
 | 45 | + self.entries.append(entry)  | 
 | 46 | + | 
 | 47 | + | 
 | 48 | + def __init__(self):  | 
 | 49 | + """ document initialization """  | 
 | 50 | + self.entries = []  | 
 | 51 | + | 
 | 52 | + def add(self, entry):  | 
 | 53 | + """ add entry to entry list """  | 
 | 54 | + self.entries.append(entry)  | 
 | 55 | + | 
 | 56 | + | 
 | 57 | +class text(document):  | 
 | 58 | + """  | 
 | 59 | + A simple text document implementation  | 
 | 60 | + """  | 
 | 61 | + | 
 | 62 | + class string(document.entry):  | 
 | 63 | + """  | 
 | 64 | + The smallest unit of a text file is a string  | 
 | 65 | + """  | 
 | 66 | + | 
 | 67 | + def __init__(self, parent, stringString=None):  | 
 | 68 | + """ string initialization """  | 
 | 69 | + super(text.string, self).__init__(parent)  | 
 | 70 | + self.stringString = stringString  | 
 | 71 | + | 
 | 72 | + def __str__(self):  | 
 | 73 | + """ convert None to empty string """  | 
 | 74 | + if (self.stringString != None):  | 
 | 75 | + return self.stringString  | 
 | 76 | + else:  | 
 | 77 | + return ""  | 
 | 78 | + | 
 | 79 | + | 
 | 80 | + class line(string):  | 
 | 81 | + """  | 
 | 82 | + A line is a string with EOL character  | 
 | 83 | + """  | 
 | 84 | + | 
 | 85 | + def __str__(self):  | 
 | 86 | + """ convert string with newline """  | 
 | 87 | + return super(text.line, self).__str__() + "\n"  | 
 | 88 | + | 
 | 89 | + class block(document.block):  | 
 | 90 | + """  | 
 | 91 | + A block of text which can be made up of  | 
 | 92 | + strings or lines  | 
 | 93 | + """  | 
 | 94 | + | 
 | 95 | + def __str__(self):  | 
 | 96 | + """ concatenate strings or lines """  | 
 | 97 | + blockString = ""  | 
 | 98 | + | 
 | 99 | + for entry in self.entries:  | 
 | 100 | + blockString += str(entry)  | 
 | 101 | + | 
 | 102 | + return blockString  | 
 | 103 | + | 
 | 104 | + | 
 | 105 | + def __str__(self):  | 
 | 106 | + """ concatenate strings or lines """  | 
 | 107 | + textString = ""  | 
 | 108 | + | 
 | 109 | + for entry in self.entries:  | 
 | 110 | + textString += str(entry)  | 
 | 111 | + | 
 | 112 | + return textString  | 
 | 113 | + | 
 | 114 | + | 
 | 115 | +class c_source(text):  | 
 | 116 | + """  | 
 | 117 | + A simple C header document implementation  | 
 | 118 | + """  | 
 | 119 | + | 
 | 120 | + class define(text.string):  | 
 | 121 | + """  | 
 | 122 | + C header define  | 
 | 123 | + """  | 
 | 124 | + | 
 | 125 | + def __init__(self, parent, id, token=None):  | 
 | 126 | + """ c header constructor initialization """  | 
 | 127 | + super(c_source.define, self).__init__(parent, id)  | 
 | 128 | + self.token = token  | 
 | 129 | + | 
 | 130 | + def __str__(self):  | 
 | 131 | + """ c header to strings """  | 
 | 132 | + defineString = "#define" + " " + super(c_source.define, self).__str__()  | 
 | 133 | + | 
 | 134 | + if self.token != None:  | 
 | 135 | + defineString += " " + self.token  | 
 | 136 | + | 
 | 137 | + defineString += "\n"  | 
 | 138 | + | 
 | 139 | + return defineString  | 
 | 140 | + | 
 | 141 | + class comment_string(text.string):  | 
 | 142 | + """  | 
 | 143 | + C header comment  | 
 | 144 | + """  | 
 | 145 | + | 
 | 146 | + def __str__(self):  | 
 | 147 | + """ c comment """  | 
 | 148 | + return "/*" + " " + super(c_source.comment_string, self).__str__() + " " + "*/"  | 
 | 149 | + | 
 | 150 | + class comment_line(comment_string):  | 
 | 151 | + """  | 
 | 152 | + C header comment with newline  | 
 | 153 | + """  | 
 | 154 | + | 
 | 155 | + def __str__(self):  | 
 | 156 | + """ c comment with newline """  | 
 | 157 | + return super(c_source.comment_line, self).__str__() + "\n"  | 
 | 158 | + | 
 | 159 | + class block(text.block):  | 
 | 160 | + """  | 
 | 161 | + A simple C block string implementation  | 
 | 162 | + """  | 
 | 163 | + | 
 | 164 | + def __init__(self, parent, prologue=None, epilogue=None):  | 
 | 165 | + """ ifdef block string implementation """  | 
 | 166 | + super(c_source.block, self).__init__(parent)  | 
 | 167 | + | 
 | 168 | + self.prologue = None  | 
 | 169 | + self.epilogue = None  | 
 | 170 | + | 
 | 171 | + if prologue != None:  | 
 | 172 | + self.prologue = prologue  | 
 | 173 | + | 
 | 174 | + if epilogue != None:  | 
 | 175 | + self.epilogue = epilogue  | 
 | 176 | + | 
 | 177 | + def __str__(self):  | 
 | 178 | + """ convert ifdef to string """  | 
 | 179 | + blockString = ""  | 
 | 180 | + | 
 | 181 | + if self.prologue != None:  | 
 | 182 | + blockString += str(self.prologue)  | 
 | 183 | + | 
 | 184 | + blockString += super(c_source.block, self).__str__()  | 
 | 185 | + | 
 | 186 | + if self.epilogue != None:  | 
 | 187 | + blockString += str(self.epilogue)  | 
 | 188 | + | 
 | 189 | + return blockString  | 
 | 190 | + | 
 | 191 | + class comment_block(block):  | 
 | 192 | + """  | 
 | 193 | + A simple C header block comment implementation  | 
 | 194 | + """  | 
 | 195 | + | 
 | 196 | + def __init__(self, parent, comments):  | 
 | 197 | + """ block comment initialization """  | 
 | 198 | + super(c_source.comment_block, self).__init__(parent, "/*\n", " */\n")  | 
 | 199 | + for comment in comments.split("\n"):  | 
 | 200 | + self.add(comment)  | 
 | 201 | + | 
 | 202 | + def add(self, entry):  | 
 | 203 | + """ add line to block comment """  | 
 | 204 | + super(c_source.block, self).add(" * " + entry + "\n")  | 
 | 205 | + | 
 | 206 | + class ifndef_block(block):  | 
 | 207 | + """  | 
 | 208 | + A simple C header ifndef implementation  | 
 | 209 | + """  | 
 | 210 | + | 
 | 211 | + def __init__(self, parent, id):  | 
 | 212 | + """ ifndef block initialization """  | 
 | 213 | + prologue = text.line(None, "#ifndef" + " " + id)  | 
 | 214 | + epilogue = text.block(None)  | 
 | 215 | + text.string(epilogue, "#endif")  | 
 | 216 | + text.string(epilogue, " ")  | 
 | 217 | + c_source.comment_line(epilogue, id)  | 
 | 218 | + super(c_source.ifndef_block, self).__init__(parent, prologue, epilogue)  | 
 | 219 | + | 
 | 220 | + | 
 | 221 | +class generated_c_source(c_source):  | 
 | 222 | + """  | 
 | 223 | + Caller to generate c format files using the helper classes  | 
 | 224 | + """  | 
 | 225 | + | 
 | 226 | + def __init__(self, filename):  | 
 | 227 | + """ Generate c header file with license, copyright, comment,  | 
 | 228 | + ifdef block  | 
 | 229 | + """  | 
 | 230 | + super(generated_c_source, self).__init__()  | 
 | 231 | + | 
 | 232 | + self.entries.append(c_source.comment_line(None, "SPDX-License-Identifier: BSD-3-Clause"))  | 
 | 233 | + self.entries.append(c_source.comment_block(None, "Copyright (C) 2022 Intel Corporation <www.intel.com>"))  | 
 | 234 | + self.entries.append(c_source.comment_block(None, "Altera SoCFPGA Clock and PLL configuration"))  | 
 | 235 | + self.entries.append(text.line(None))  | 
 | 236 | + | 
 | 237 | + self.body = c_source.ifndef_block(None, filename)  | 
 | 238 | + self.body.add(c_source.define(None, filename))  | 
 | 239 | + self.entries.append(self.body)  | 
 | 240 | + | 
 | 241 | + def add(self, entry):  | 
 | 242 | + """ add content to be written into c header file """  | 
 | 243 | + self.body.add(entry)  | 
0 commit comments