Skip to content
Snippets Groups Projects
diceware.py 1.11 KiB
Newer Older
  • Learn to ignore specific revisions
  • ulif's avatar
    ulif committed
    import os
    
    ulif's avatar
    ulif committed
    import re
    
    ulif's avatar
    ulif committed
    
    #: The directory in which wordlists are stored
    SRC_DIR = os.path.dirname(__file__)
    
    
    ulif's avatar
    ulif committed
    #: A regular expression matching ASCII chars
    RE_ASCII_CHARS = re.compile('^[a-zA-Z]{2}$')
    
    
    ulif's avatar
    ulif committed
    
    
    ulif's avatar
    ulif committed
    def get_wordlist(path):
        """Parse file at `path` and build a word list of it.
    
    ulif's avatar
    ulif committed
    
        A wordlist is expected to contain lines of format::
    
            <NUMS><TAB><WORD>\n
    
        for instance::
    
            136512\tTerm
    
    
    ulif's avatar
    ulif committed
        """
        result = []
        with open(path, 'r') as fd:
            for line in fd.readlines():
                if not '\t' in line:
                    continue
                term = line.split('\t')[1].strip()
    
    ulif's avatar
    ulif committed
                if term != '':  # do not accept empty strings
                    result.append(term)
    
    ulif's avatar
    ulif committed
        return result
    
    
    ulif's avatar
    ulif committed
    def get_wordlist_path(lang):
        """Get path to a wordlist file for language `lang`.
    
        The `lang` string is a 2-char country code. Invalid codes raise a
        ValueError.
        """
        basename = 'wordlist_%s.asc' % lang
        if not RE_ASCII_CHARS.match(lang):
            raise ValueError("Not a valid language code: %s" % lang)
        return os.path.abspath(os.path.join(SRC_DIR, basename.lower()))
    
    
    
    ulif's avatar
    ulif committed
    def main():
        pass