def volume(move = 2): """ Change volume level. Positive `move`: Volume Up Negative `move`: Volume Down """ if oa.sys.os == 'win': # Up by 2. if move > 0: # Volume up. key = chr(175) else: move =- move key = chr(174) while move > 0: wshell.SendKeys(key) move -= 2 elif oa.sys.os in ('linux','mac'): if move > 0: sys_exec('pamixer --increase %d' %move) else: sys_exec('pamixer --decrease %d' %(-move)) else: info('Unknown operating system.')
def mute(mute = True): """ Mute or unmute speakers. """ if oa.sys.os == 'win': wshell.SendKeys(chr(173)) elif oa.sys.os in ('linux', 'mac'): sys_exec('amixer set Master %smute' % (((not mute) and 'un') or '')) else: info('Unknown operating system.')
def calculate(): ret = expr2str() info(oa.sys.expr) info('expr=' + ret) try: say(eval(ret)) except: say('Error. Wrong expression. ' + ret) # Clear the expression. oa.sys.expr = []
def is_online(host = '8.8.8.8', port = 53, timeout = 1): """ If online Return True, if not return False. Host: 8.8.8.8 (google-public-dns-a.google.com) OpenPort: 53/tcp Service: domain (DNS/TCP) """ try: socket.setdefaulttimeout(timeout) socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port)) return True except Exception as ex: info(ex) return False
def read_file(fname, result_as_list=0): """ Read the contents of a file and return a string or a list of strings split by a new line symbol. """ try: info('- Reading file: ', fname) if not os.path.exists(fname): fname = find_file(fname) with open(fname, 'r') as f: if result_as_list: return f.readlines() else: return f.read() except: # FileNotFoundError: info("- Error loading file: {path}".format(path=fname)) # logger.warn("Error loading file: {path}".format(path = fname)) return ''
def read_news_feed(news_feed, category): rss = feedparser.parse(news_feed) info(rss['feed']['title']) say('- Reading %s news.' %category) headline_count = 1 # Amount of headlines to read. headline_amount = 5 for post in rss.entries: if(headline_count == headline_amount): break else: headline = post.title exclude = set(string.punctuation) headline = ''.join(ch for ch in headline if ch not in exclude) say(headline) headline_count += 1
def update_language(_): # Update the language model using the online `lmtool`. host = 'http://www.speech.cs.cmu.edu' url = host + '/cgi-bin/tools/lmtool/run' # Submit the corpus to the `lmtool`. response_text = "" with open(_.strings_file, 'r') as f: files = {'corpus': f} values = {'formtype': 'simple'} r = requests.post(url, files = files, data = values) response_text = r.text # Parse response to get urls of the files we need. path_re = r'.*<title>Index of (.*?)</title>.*' number_re = r'.*TAR([0-9]*?)\.tgz.*' path = None for line in response_text.split('\n'): # Error response. if "[_ERRO_]" in line: return 1 # If we find the directory, keep it and don't break. if re.search(path_re, line): path = host + re.sub(path_re, r'\1', line) # If we find a number, keep it and break. elif re.search(number_re, line): number = re.sub(number_re, r'\1', line) break if path is None: info('_.cache_dir',_.cache_dir) raise Exception('Not found: update_language: ' + response_text) lm_url = path + '/' + number + '.lm' dic_url = path + '/' + number + '.dic' if _.lang_file is not None: download_file(lm_url, _.lang_file) download_file(dic_url, _.dic_file)
def expr2str(): """ Convert a numerical expression into a string. """ ret = '' info(oa.sys.calc_opers.values()) for k, g in groupby( oa.sys.expr, lambda x: ((x in oa.sys.calc_opers.values()) and 1) or 2): l = list(g) if len(l) > 1: if k == 1: raise Exception('two opers') else: sr = '(' + l[0] for x in l[1:]: if isNum(x): sr += '+' + x else: # 'hundreds, thousands so on' sr += x ret += sr + ')' else: ret += l[0] return ret