Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python
- """
- NoteReader
- """
- #CHANGELOG
- #
- # v0.07
- # * added getopt options parser
- #
- # v0.06
- # * added Trash section
- # * added Parser.save() function
- # * SORT flag in Parser
- # *
- import sys
- import getopt
- __VERSION__='0.07'
- class Parser:
- """Parser for files"""
- def __init__(self):
- """init parser"""
- self._sections=[]
- self.SORT=True
- def add_section(self,sectiondict):
- """add section"""
- self._sections.append(sectiondict)
- def remove_section(self,sectiondict):
- """remove section"""
- pass
- def get_sections(self):
- """get all sections, unsorted"""
- return self._sections
- def get_section_names(self):
- """get all section names"""
- result=[]
- for sec in self._sections:
- result.append(sec['name'])
- if self.SORT:result.sort()
- return result
- def get_section_tag(self,section):
- """get tag of section
- for #[/hello] its /hello and,
- for #[/hello/world] its /hello/world
- """
- tag=section['name'].lstrip('#[')
- tag=tag.rstrip(']')
- return tag
- def section_from_tag(self,tag):
- """convert tag to section name"""
- return '#['+tag+']'
- def get_section_by_tag(self,tag):
- """get section by given tag
- tag should be like this /hello"""
- section_name=self.section_from_tag(tag)
- if section_name in self.get_section_names():
- return self.get_section_by_name(section_name)
- def get_section_by_name(self,sectionname):
- """get section by name"""
- for sec in self._sections:
- if sec['name']==sectionname:
- return sec
- def save(self,filename):
- """save all sections to the file"""
- try:
- filew=open(filename,'w')
- except:pass
- else:
- sections=self.get_section_names()
- for sec in sections:
- nextsection=self.get_section_by_name(sec)
- filew.write(nextsection['name']+'\n')
- filew.write(nextsection['text'])
- filew.close()
- def Parse(pbuffer):
- """parse the buffer"""
- current_section=None
- trash_section={'name':'#[/trash]','text':''}
- parser=Parser()
- #for each line in buffer
- for nr,line in enumerate(pbuffer):
- line=line.rstrip()
- #found #[/text] --section
- if line.startswith('#[/') and line.endswith(']'):
- #store section
- if current_section!=None:
- parser.add_section(current_section)
- #create new section
- current_section={'name':line,'text':''}
- #add rest text to current section
- else:
- #if we have current_section
- if current_section!=None:
- #if this is not last line in buffer
- if nr!=len(pbuffer)-1:
- #not empty line
- if len(line)>0:
- current_section['text']+=line+'\n'
- #empty line
- else:
- current_section['text']+='\n'
- #if its last line in buffer
- else:
- current_section['text']+=line+'\n'
- #if there no current_section then add it to trash
- else:
- trash_section['text']+=line+'\n'
- #add last section
- if current_section!=None:
- parser.add_section(current_section)
- if trash_section['text']!='':
- parser.add_section(trash_section)
- return parser
- def parse_args(opts,args):
- """parse arguments passed to application"""
- for opt,arg in opts:
- if opt=='-s' and len(args)>0:
- buff=open(args[0]).readlines()
- parsed=Parse(buff)
- sections=parsed.get_section_names()
- section=parsed.get_section_by_tag(arg)
- if section:
- print section['text']
- elif opt=='-h' or '--help':
- print "Usage:"
- print "NoteReader [Flags] filename"
- print "Flags:"
- print "-s <section> --show section e.g. -s /section"
- print "-h --this help"
- if not opts and len(args)>0:
- buff=open(sys.argv[1]).readlines()
- parsed=Parse(buff)
- sections=parsed.get_section_names()
- for sec in sections:
- print sec
- ############################
- if __name__=='__main__':
- try:
- opts,args=getopt.getopt(sys.argv[1:],'s:h',[])
- if not opts and len(args)==0:
- print "Invalid option try:NoteReader -h"
- except getopt.GetoptError,Exception:
- print "Invalid option try:NoteReader -h"
- else:
- parse_args(opts,args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement