Skip to content

Commit

Permalink
add level information and fix error with dot because of failing the s…
Browse files Browse the repository at this point in the history
…cript
  • Loading branch information
vladislavzl committed Dec 7, 2019
1 parent 5f51970 commit fc58efc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
13 changes: 10 additions & 3 deletions cambrinary/Conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections import OrderedDict
from pathlib import Path

from .color_const import *
from color_const import *


def load(json_file):
Expand All @@ -12,13 +12,14 @@ def load(json_file):


class ColorScheme(object):
def __init__(self, pron_region, definition, exam_sen, trans_def, pronunciation, guidword):
def __init__(self, pron_region, definition, exam_sen, trans_def, pronunciation, guidword, level):
self.pron_region = pron_region
self.definition = definition
self.exam_sen = exam_sen
self.trans_def = trans_def
self.pronunciation = pronunciation
self.guidword = guidword
self.level = level

def to_dic(self):
json_dic = OrderedDict()
Expand All @@ -28,13 +29,16 @@ def to_dic(self):
json_dic['trans_def'] = self.trans_def
json_dic['pronunciation'] = self.pronunciation
json_dic['guidword'] = self.guidword
json_dic['level'] = self.level
return json_dic

@staticmethod
def to_obj(color_scheme_dict):
return ColorScheme(color_scheme_dict['pron_region'], color_scheme_dict['definition'],
color_scheme_dict['example_sentence'], color_scheme_dict['trans_definition'],
color_scheme_dict['pronunciation'], color_scheme_dict['guidword'])
color_scheme_dict['pronunciation'], color_scheme_dict['guidword'],
color_scheme_dict['level']
)


class Conf(object):
Expand Down Expand Up @@ -70,6 +74,9 @@ def color(s, color_scheme):
def pron_region(self, str):
return self.color(str, self.color_scheme.pron_region)

def level(self, str):
return self.color(str, self.color_scheme.level)

def pronunciation(self, str):
return self.color(str, self.color_scheme.pronunciation)

Expand Down
15 changes: 10 additions & 5 deletions cambrinary/cambrinary.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import aiohttp
from bs4 import BeautifulSoup

from .country_const import *
from .log import *
from .type import *
from country_const import *
from log import *
from type import *

translation = {GB: 'english',
CN: 'english-chinese-traditional',
Expand Down Expand Up @@ -169,6 +169,11 @@ def get_definition(p):
d = p.find('div', attrs={'class': 'def ddef_d db'})
return d.get_text() if d else None

def get_level(p):
d = p.find('span', attrs={'class': 'def-info ddef-info'})
l = d.find_all()[0]
return l.get_text() if l else None

def get_trans(body):
trans = body.find('span', attrs={'class': 'trans dtrans dtrans-se'}, recursive=False)
return trans.get_text().strip() if trans else None
Expand All @@ -192,10 +197,10 @@ def get_synonym(body, arg):
definition=get_definition(pad),
trans=(get_trans(def_body) if def_body else None),
examps=(get_examps(def_body) if def_body else None),
synonym=(get_synonym(def_body, args)) if def_body else None
synonym=(get_synonym(def_body, args) if def_body else None),
level=(get_level(pad))
)
res.append(pad_indent)

return res


Expand Down
8 changes: 7 additions & 1 deletion cambrinary/conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"second for foreground",
"third for background"
],
"level": [
"default",
"red",
"default"
],

"pron_region": [
"default",
"blue",
Expand Down Expand Up @@ -37,4 +43,4 @@
"default"
]
}
}
}
9 changes: 6 additions & 3 deletions cambrinary/type.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .Conf import *
from Conf import *


class Pron(object):
Expand Down Expand Up @@ -29,16 +29,19 @@ def to_str(self):


class PadIndent(object):
def __init__(self, definition=None, trans=None, examps=None, synonym=None):
def __init__(self, definition=None, trans=None, examps=None, synonym=None, level=None):
self.definition = definition
self.trans = trans
self.examps = examps
self.synonym = synonym
self.level = level

def to_str(self):
res = ''
if self.level:
res += colors.level('--' + self.level + '--' + '\n')
if self.definition:
res += colors.definition('* ' + self.definition) + '\n'
res += colors.definition('* ' + self.definition) + '\n\n'
if self.trans:
res += colors.trans_def(' {}\n'.format(self.trans))
if self.examps:
Expand Down

0 comments on commit fc58efc

Please sign in to comment.