Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright (c) 2012,2013 Big Switch Networks, Inc. |
| 3 | # |
| 4 | # Licensed under the Eclipse Public License, Version 1.0 (the |
| 5 | # "License"); you may not use this file except in compliance with the |
| 6 | # License. You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.eclipse.org/legal/epl-v10.html |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | # implied. See the License for the specific language governing |
| 14 | # permissions and limitations under the License. |
| 15 | # |
| 16 | |
| 17 | import os |
| 18 | |
| 19 | # |
| 20 | # doc, interface to manage 'markdown' text segments based on |
| 21 | # documentation tag's. |
| 22 | # |
| 23 | |
| 24 | doc = {} |
| 25 | |
| 26 | def register_doc(tag, path): |
| 27 | """ |
| 28 | Its not clear whether the registratio api ought to only name |
| 29 | the available tags, or whether the api ought to also associate |
| 30 | text, but it seems that it would be wiser to not keep all the |
| 31 | documentation in memory |
| 32 | """ |
| 33 | |
| 34 | doc[tag] = path |
| 35 | |
| 36 | def add_doc_tags(base, tag_prefix = None): |
| 37 | path = base |
| 38 | if tag_prefix == None: |
| 39 | tag_prefix = [] |
| 40 | elif type(tag_prefix) == str: |
| 41 | tag_prefix = [tag_prefix] |
| 42 | |
| 43 | if os.path.exists(path): |
| 44 | for elem in os.listdir(path): |
| 45 | this_tag_prefix = list(tag_prefix) |
| 46 | this_tag_prefix.append(elem) |
| 47 | full_path = os.path.join(path, elem) |
| 48 | if os.path.isdir(full_path): |
| 49 | add_doc_tags(full_path, this_tag_prefix) |
| 50 | elif os.path.isfile(full_path): |
| 51 | register_doc('|'.join(this_tag_prefix), full_path) |
| 52 | else: |
| 53 | print 'add_doc_tags: unknown file type for ', full_path |
| 54 | |
| 55 | |
| 56 | def get_text(context, tag): |
| 57 | path = doc.get(tag) |
| 58 | if path: |
| 59 | if os.path.exists(path): |
| 60 | with open(path, 'r') as f: |
| 61 | return f.read() |
| 62 | else: |
| 63 | if context.sdnsh.description: |
| 64 | print 'doc: tag %s: missing ' % tag |
| 65 | |
| 66 | return '' |