def read( self, f ): ''' считывает статью из файла. предполагается, что статьи разделны хотя бы одной пустой строкой в случае неудачи (например, EOF) возвращает None ''' self.title = u'' # пропустить пустые строки while True: _ = u( f.readline()) if _ is not u'\n': self.title = _.strip() break # print u'dslEntry.read: прочитал "%s"' % self.title self.entry = u'' # читать до пустой строки или EOF while True: line = u( f.readline()) if line.strip() is not u'': self.entry += line else: break self.entry = self.entry.strip() if len( self.entry ) == 0: return None return self
def read_headers( self ): headers = [] print 'читаю заголовки...' while True: self.last_read = u( self.infile.readline().strip()) # print 'read_headers: прочел "%s"' % utf( self.last_read ) if self.last_read.startswith( u'#' ): _ = re.match( # #INDEX_LANGUAGE "Russian" # # \w+ \s " .+ " ur'#(\w+)\s+([\'"]?)([^\n]+?)\2', self.last_read, re.UNICODE ) if _: # \1 -- имя, \3 -- значение _ = _.groups() headers.append({ 'title': _[0], 'value': _[2] }) print u'dslDictionary.read_headers: title: "%s", value: "%s"' % ( _[0], _[2] ) else: break else: # положить обратно где взял self.infile.seek( - len( self.last_read ), 1 ) break return headers
def set_headers( self, headers ): ''' set_headers([ {title: str, value: str}, ...]) ''' # альтернативный вариант default value для изменяемых типов headers = headers if headers else [] # переписать себе for h in headers: self.headers[ h[ 'title' ]] = u( h[ 'value' ])
@@ -24,22 +24,22 @@ class Generator(object): self.spec = get_by_path(dom, "spec")[0] def h(self, code): - self.decls.append(code.encode('utf-8')) + self.decls.append(code) def c(self, code): - self.impls.append(code.encode('utf-8')) + self.impls.append(code) def d(self, code): - self.docs.append(code.encode('utf-8')) + self.docs.append(code) def __call__(self): for f in self.h, self.c: self.do_header(f) self.do_body() - file_set_contents(self.implfile, ''.join(self.impls)) - file_set_contents(self.declfile, ''.join(self.decls)) - file_set_contents(self.docfile, ''.join(self.docs)) + file_set_contents(self.implfile, u('').join(self.impls).encode('utf-8')) + file_set_contents(self.declfile, u('').join(self.decls).encode('utf-8')) + file_set_contents(self.docfile, u('').join(self.docs).encode('utf-8')) # Header def do_header(self, f):
@@ -68,13 +68,13 @@ class GTypesGenerator(object): self.need_other_arrays = {} def h(self, code): - self.header.append(code.encode("utf-8")) + self.header.append(code) def c(self, code): - self.body.append(code.encode("utf-8")) + self.body.append(code) def d(self, code): - self.docs.append(code.encode('utf-8')) + self.docs.append(code) def do_mapping_header(self, mapping): members = mapping.getElementsByTagNameNS(NS_TP, 'member') @@ -292,9 +292,9 @@ class GTypesGenerator(object): self.c(' return t;\n') self.c('}\n\n') - file_set_contents(self.output + '.h', ''.join(self.header)) - file_set_contents(self.output + '-body.h', ''.join(self.body)) - file_set_contents(self.output + '-gtk-doc.h', ''.join(self.docs)) + file_set_contents(self.output + '.h', u('').join(self.header).encode('utf-8')) + file_set_contents(self.output + '-body.h', u('').join(self.body).encode('utf-8')) + file_set_contents(self.output + '-gtk-doc.h', u('').join(self.docs).encode('utf-8')) if __name__ == '__main__': argv = sys.argv[1:]
def postparse( self, t, s ): return u'%s%s' % ( u( t ), u( s ) )
import itertools as ;u=range;golf=lambda l:min(set(u(1,sum(l)+2))-set(sum(c)for r in u(1,len(l)+1)for c in t.combinations(l,r)))
def postparse(self, t, s): return u'%s%s' % (u(t), u(s))
get_descendant_text, get_by_path @@ -12,7 +12,7 @@ class Generator(object): self.prefix = prefix + '_' self.spec = get_by_path(dom, "spec")[0] - self.output_base = output_base + self.output_base = output_base self.__header = [] self.__docs = [] @@ -21,14 +21,14 @@ class Generator(object): self.do_body() self.do_footer() - file_set_contents(self.output_base + '.h', ''.join(self.__header)) - file_set_contents(self.output_base + '-gtk-doc.h', ''.join(self.__docs)) + file_set_contents(self.output_base + '.h', u('').join(self.__header).encode('utf-8')) + file_set_contents(self.output_base + '-gtk-doc.h', u('').join(self.__docs).encode('utf-8')) def write(self, code): - self.__header.append(code.encode('utf-8')) + self.__header.append(code) def d(self, code): - self.__docs.append(code.encode('utf-8')) + self.__docs.append(code) # Header def do_header(self):