MCPcopy
hub / github.com/skywind3000/ECDICT / load

Method load

dictutils.py:537–596  ·  view source on GitHub ↗
(self, filename)

Source from the content-addressed store, hash-verified

535 sys.stderr.flush()
536
537 def load (self, filename):
538 self._resembles = []
539 self._words = {}
540 file_content = stardict.tools.load_text(filename)
541 if file_content is None:
542 sys.stderr.write('cannot read: %s\n'%filename)
543 return False
544 key = None
545 content = []
546 self._filename = filename
547 self._lineno = 0
548 for line in file_content.split('\n'):
549 line = line.strip('\r\n\t ')
550 self._lineno += 1
551 if key is None:
552 if not line:
553 continue
554 if line[:1] != '%':
555 self.error('must starts with a percent sign')
556 return False
557 line = line[1:].lstrip('\r\n\t ')
558 key = [ n.strip('\r\n\t ') for n in line.split(',') ]
559 if not key:
560 self.error('empty heading words')
561 return False
562 for word in key:
563 if not word:
564 self.error('empty item')
565 return False
566 content = []
567 else:
568 if not line:
569 wt = {}
570 uuid = [ n for n in key ]
571 uuid.sort()
572 wt['words'] = tuple(key)
573 wt['content'] = content
574 wt['uuid'] = ', '.join(uuid)
575 self._resembles.append(wt)
576 key = None
577 content = []
578 elif line[:1] == '-':
579 line = line[1:].lstrip('\r\n\t')
580 pos = line.find(':')
581 if pos < 0:
582 self.error('expect colon')
583 word = line[:pos].strip('\r\n\t ')
584 text = line[pos+1:].strip('\r\n\t ')
585 text = text.replace('\\n', '\n')
586 content.append((word, text))
587 else:
588 content.append(line)
589 if key:
590 wt = {'words':tuple(key), 'content':content}
591 uuid = [ n for n in key ]
592 uuid.sort()
593 wt['uuid'] = uuid
594 self._resembles.append(wt)

Callers 2

test2Function · 0.45
test3Function · 0.45

Calls 3

errorMethod · 0.95
_init_refsMethod · 0.95
load_textMethod · 0.80

Tested by

no test coverage detected