Changeset 194

Show
Ignore:
Timestamp:
08/19/08 22:24:50 (3 months ago)
Author:
rgrp
Message:

[cli][s]: minor fixes to cli stats + search init methods to exclude folio texts because of weird spellings.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/shakespeare/cli.py

    Revision 189 Revision 194
    1#!/usr/bin/env python 1#!/usr/bin/env python 
    2 2 
    3import cmd 3import cmd 
    4import os 4import os 
    5import StringIO 5import StringIO 
    6 6 
    7class ShakespeareAdmin(cmd.Cmd): 7class ShakespeareAdmin(cmd.Cmd): 
    8    """ 8    """ 
    9    TODO: self.verbose option and associated self._print 9    TODO: self.verbose option and associated self._print 
    10    """ 10    """ 
    11 11 
    12    def __init__(self, verbose=False): 12    def __init__(self, verbose=False): 
    13        # cmd.Cmd is not a new style class 13        # cmd.Cmd is not a new style class 
    14        cmd.Cmd.__init__(self) 14        cmd.Cmd.__init__(self) 
    15        self.verbose = verbose 15        self.verbose = verbose 
      16 
      17    def _print(self, msg, force=False): 
      18        if self.verbose or force: 
      19            print msg 
    16 20 
    17    prompt = 'The Bard > ' 21    prompt = 'The Bard > ' 
    18 22 
    19    def run_interactive(self, line=None): 23    def run_interactive(self, line=None): 
    20        """Run an interactive session. 24        """Run an interactive session. 
    21        """ 25        """ 
    22        print 'Welcome to shakespeare-admin interactive mode\n' 26        print 'Welcome to shakespeare-admin interactive mode\n' 
    23        self.do_about() 27        self.do_about() 
    24        print 'Type:  "?" or "help" for help on commands.\n' 28        print 'Type:  "?" or "help" for help on commands.\n' 
    25        while 1: 29        while 1: 
    26            try: 30            try: 
    27                self.cmdloop() 31                self.cmdloop() 
    28                break 32                break 
    29            except KeyboardInterrupt: 33            except KeyboardInterrupt: 
    30                raise 34                raise 
    31 35 
    32    def do_help(self, line=None): 36    def do_help(self, line=None): 
    33        cmd.Cmd.do_help(self, line) 37        cmd.Cmd.do_help(self, line) 
    34 38 
    35    def do_about(self, line=None): 39    def do_about(self, line=None): 
    36        import shakespeare 40        import shakespeare 
    37        version = shakespeare.__version__ 41        version = shakespeare.__version__ 
    38        about = \ 42        about = \ 
    39'''Open Shakespeare version %s. Copyright the Open Knowledge Foundation. 43'''Open Shakespeare version %s. Copyright the Open Knowledge Foundation. 
    40Open Shakespeare is open-knowledge and open-source. See COPYING for details. 44Open Shakespeare is open-knowledge and open-source. See COPYING for details. 
    41 45 
    42For more information about the package run `info`. 46For more information about the package run `info`. 
    43''' % version 47''' % version 
    44        print about 48        print about 
    45 49 
    46    def do_quit(self, line=None): 50    def do_quit(self, line=None): 
    47        sys.exit() 51        sys.exit() 
    48 52 
    49    def do_EOF(self, *args): 53    def do_EOF(self, *args): 
    50        print '' 54        print '' 
    51        sys.exit() 55        sys.exit() 
    52 56 
    53    # ================= 57    # ================= 
    54    # Commands 58    # Commands 
    55 59 
    56    def do_db(self, line=None): 60    def do_db(self, line=None): 
    57        actions = [ 'create', 'clean', 'rebuild', 'init' ] 61        actions = [ 'create', 'clean', 'rebuild', 'init' ] 
    58        if line is None or line not in actions: 62        if line is None or line not in actions: 
    59            self.help_db() 63            self.help_db() 
    60            return 1 64            return 1 
    61        import shakespeare.model 65        import shakespeare.model 
    62        if line == 'init': 66        if line == 'init': 
    63            import pkg_resources 67            import pkg_resources 
    64            pkg = 'shksprdata' 68            pkg = 'shksprdata' 
    65            meta = pkg_resources.resource_stream(pkg, 'texts/metadata.txt') 69            meta = pkg_resources.resource_stream(pkg, 'texts/metadata.txt') 
    66            shakespeare.model.Material.load_from_metadata(meta) 70            shakespeare.model.Material.load_from_metadata(meta) 
    67        else: 71        else: 
    68            print 'To create db use paster: paster setup-app {config-file}' 72            print 'To create db use paster: paster setup-app {config-file}' 
    69 73 
    70    def help_db(self, line=None): 74    def help_db(self, line=None): 
    71        usage = \ 75        usage = \ 
    72'''db { create | init } 76'''db { create | init } 
    73''' 77''' 
    74        print usage 78        print usage 
    75     79     
    76    def do_gutenberg(self, line=None): 80    def do_gutenberg(self, line=None): 
    77        import shakespeare.gutenberg 81        import shakespeare.gutenberg 
    78        helper = shakespeare.gutenberg.Helper(verbose=True) 82        helper = shakespeare.gutenberg.Helper(verbose=True) 
    79        if not line: 83        if not line: 
    80            helper.execute() 84            helper.execute() 
    81        elif line == 'print_index': 85        elif line == 'print_index': 
    82            import pprint 86            import pprint 
    83            pprint.pprint(helper.get_index()) 87            pprint.pprint(helper.get_index()) 
    84        else: 88        else: 
    85            msg = 'Unknown argument %s' % line 89            msg = 'Unknown argument %s' % line 
    86            raise Exception(msg) 90            raise Exception(msg) 
    87 91 
    88    def help_gutenberg(self, line=None): 92    def help_gutenberg(self, line=None): 
    89        usage = \ 93        usage = \ 
    90""" 94""" 
    91Download and process all Project Gutenberg shakespeare texts""" 95Download and process all Project Gutenberg shakespeare texts""" 
    92        print usage  96        print usage  
    93 97 
    94    def do_moby(self, line=None): 98    def do_moby(self, line=None): 
    95        import shakespeare.moby 99        import shakespeare.moby 
    96        helper = shakespeare.moby.Helper(verbose=True) 100        helper = shakespeare.moby.Helper(verbose=True) 
    97        if not line: 101        if not line: 
    98            helper.execute() 102            helper.execute() 
    99        elif line == 'print_index': 103        elif line == 'print_index': 
    100            import pprint 104            import pprint 
    101            pprint.pprint(helper.get_index()) 105            pprint.pprint(helper.get_index()) 
    102        else: 106        else: 
    103            msg = 'Unknown argument %s' % line 107            msg = 'Unknown argument %s' % line 
    104            raise Exception(msg) 108            raise Exception(msg) 
    105 109 
    106    def help_moby(self, line=None): 110    def help_moby(self, line=None): 
    107        usage = \ 111        usage = \ 
    108''' 112''' 
    109Download and process all Moby/Bosak shakespeare texts''' 113Download and process all Moby/Bosak shakespeare texts''' 
    110        print usage  114        print usage  
    111 115 
    112    def _init_index(self): 116    def _init_index(self): 
    113        import shakespeare.index 117        import shakespeare.index 
    114        self._index = shakespeare.index.all 118        self._index = shakespeare.index.all 
    115 119 
    116    def _filter_index(self, line): 120    def _filter_index(self, line): 
    117        """Filter items in index return only those whose id (url) is in line 121        """Filter items in index return only those whose id (url) is in line 
    118        If line is empty or None return all items 122        If line is empty or None return all items 
    119        """ 123        """ 
    120        if line: 124        if line: 
    121            textsToAdd = [] 125            textsToAdd = [] 
    122            textNames = line.split() 126            textNames = line.split() 
    123            for item in self._index: 127            for item in self._index: 
    124                if item.name in textNames: 128                if item.name in textNames: 
    125                    textsToAdd.append(item) 129                    textsToAdd.append(item) 
    126            return textsToAdd 130            return textsToAdd 
    127        else: 131        else: 
    128            self._init_index() 132            self._init_index() 
    129            return self._index 133            return self._index 
    130     134     
    131    def do_index(self, line): 135    def do_index(self, line): 
    132        self._init_index() 136        self._init_index() 
    133        header = \ 137        header = \ 
    134'''          +-------------------+ 138'''          +-------------------+ 
    135          | Index of Material | 139          | Index of Material | 
    136          +-------------------+ 140          +-------------------+ 
    137 141 
    138''' 142''' 
    139        print header 143        print header 
    140        for row in self._index: 144        for row in self._index: 
    141            print row.name.ljust(35), row.title 145            print row.name.ljust(35), row.title 
    142 146 
    143    def help_index(self, line=None): 147    def help_index(self, line=None): 
    144        usage = \ 148        usage = \ 
    145'''Print index of Shakespeare texts to stdout''' 149'''Print index of Shakespeare texts to stdout''' 
    146        print usage 150        print usage 
    147 151 
    148    def do_runserver(self, line=None): 152    def do_runserver(self, line=None): 
    149        self.help_runserver() 153        self.help_runserver() 
    150 154 
    151    def help_runserver(self, line=None): 155    def help_runserver(self, line=None): 
    152        usage = \ 156        usage = \ 
    153'''This command has been DEPRECATED. 157'''This command has been DEPRECATED. 
    154 158 
    155Please use `paster serve` to run a server now, e.g.:: 159Please use `paster serve` to run a server now, e.g.:: 
    156 160 
    157    paster serve <my-config.ini> 161    paster serve <my-config.ini> 
    158''' 162''' 
    159        print usage 163        print usage 
    160     164     
    161    def do_info(self, line=None): 165    def do_info(self, line=None): 
    162        import shakespeare 166        import shakespeare 
    163        info = shakespeare.__doc__ 167        info = shakespeare.__doc__ 
    164        print 168        print 
    165        print '       ## Open Shakespeare ##' 169        print '       ## Open Shakespeare ##' 
    166        print info 170        print info 
    167     171     
    168    def help_info(self, line=None): 172    def help_info(self, line=None): 
    169        print 'Information about this package.' 173        print 'Information about this package.' 
    170 174 
    171    def _parse_line(self, line): 175    def _parse_line(self, line): 
    172        line = line.strip() 176        line = line.strip() 
    173        args = line.split() 177        args = line.split() 
    174        action = '' 178        action = '' 
    175        remainder = '' 179        remainder = '' 
    176        if len(args) > 0: 180        if len(args) > 0: 
    177            action = args[0] 181            action = args[0] 
    178        if len(args) > 1: 182        if len(args) > 1: 
    179            remainder = ' '.join(args[1:]) 183            remainder = ' '.join(args[1:]) 
    180        return (action, remainder) 184        return (action, remainder) 
    181 185 
    182    def do_search(self, line): 186    def do_search(self, line): 
    183        import shakespeare.search 187        import shakespeare.search 
    184        index = shakespeare.search.SearchIndex.default_index() 188        index = shakespeare.search.SearchIndex.default_index() 
    185 189 
    186        action, extra = self._parse_line(line) 190        action, extra = self._parse_line(line) 
    187        if action == 'addpath': 191        if action == 'addpath': 
    188            index.add_from_path(extra) 192            index.add_from_path(extra) 
    189        elif action == 'query': 193        elif action == 'query': 
    190            results = index.search(extra) 194            results = index.search(extra) 
    191            print index.print_matches(results) 195            print index.print_matches(results) 
    192        elif action == 'addtext': 196        elif action == 'addtext': 
    193            import shakespeare.model as model 197            import shakespeare.model as model 
    194            text = model.Material.byName(extra) 198            text = model.Material.byName(extra) 
    195            fileobj = text.get_text() 199            fileobj = text.get_text() 
    196            index.add_item(fileobj) 200            index.add_item(fileobj) 
    197        elif action == 'init': 201        elif action == 'init': 
    198            self._init_index() 202            self._init_index() 
    199            for text in self._index: 203            for text in self._index: 
      204                # exclude folios as many odd spellings 
      205                if text.name.endswith('_f'): 
      206                    continue 
      207                self._print('Adding: %s' % text.name) 
    200                fileobj = text.get_text() 208                fileobj = text.get_text() 
    201                index.add_item(fileobj) 209                index.add_item(fileobj) 
    202        else: 210        else: 
    203            print 'Unrecognized action: %s' % action 211            print 'Unrecognized action: %s' % action 
    204            self.help_search() 212            self.help_search() 
    205            return 1 213            return 1 
    206 214 
    207    def help_search(self, line=None): 215    def help_search(self, line=None): 
    208        info = \ 216        info = \ 
    209''' 217''' 
    210search addpath {path} 218search addpath {path} 
    211    - Add contents of {path} (file itself or all text files in directory if 219    - Add contents of {path} (file itself or all text files in directory if 
    212      directory) to the search index. 220      directory) to the search index. 
    213       221       
    214search addtext {name} 222search addtext {name} 
    215    - Add db text named {name} to search index. 223    - Add db text named {name} to search index. 
    216 224 
    217search query {query} 225search query {query} 
    218    - Query search index with {query}. 226    - Query search index with {query}. 
    219 227 
    220search init 228search init 
    221    - Add all texts in DB to index. 229    - Add all texts in DB to index. 
    222''' 230''' 
    223        print info 231        print info 
    224 232 
    225    def do_stats(self, line): 233    def do_stats(self, line): 
    226        action, extra = self._parse_line(line) 234        action, extra = self._parse_line(line) 
    227 235 
    228        import shakespeare.stats 236        import shakespeare.stats 
    229        stats = shakespeare.stats.Stats() 237        stats = shakespeare.stats.Stats() 
    230        if action == 'init': 238        if action == 'init': 
    231            self._init_index() 239            self._init_index() 
    232            for text in self._index: 240            for text in self._index: 
      241                # exclude folios as many odd spellings 
      242                if text.name.endswith('_f'): 
      243                    continue 
      244                self._print('Adding: %s' % text.name) 
    233                stats.statsify(text, text.get_text()) 245                stats.statsify(text, text.get_text()) 
    234        elif action == 'addtext': 246        elif action == 'addtext': 
    235            import shakespeare.model as model 247            import shakespeare.model as model 
    236            text = model.Material.byName(extra) 248            text = model.Material.byName(extra) 
    237            stats.statsify(text, text.get_text()) 249            stats.statsify(text, text.get_text()) 
    238        elif action == 'show': 250        elif action == 'show': 
    239            textstats = stats.text_stats(extra) 251            textstats = stats.text_stats(extra) 
    240            for s in textstats: 252            for s in textstats: 
    241                print s.word, s.freq 253                print s.word, s.freq 
    242        else: 254        else: 
    243            print 'Unrecognized action: %s' % action 255            print 'Unrecognized action: %s' % action 
    244            self.help_stats() 256            self.help_stats() 
    245            return 1 257            return 1 
    246 258 
    247    def help_stats(self, line=None): 259    def help_stats(self, line=None): 
    248        info = \ 260        info = \ 
    249''' 261''' 
    250stats addtext {name} 262stats addtext {name} 
    251    - Add db text named {name} to stats index. 263    - Add db text named {name} to stats index. 
    252 264 
    253stats show {name} 265stats show {name} 
    254    - Query stats index with {query}. 266    - Query stats index with {query}. 
    255 267 
    256stats init 268stats init 
    257    - Prepare statistics for all texts in DB. 269    - Prepare statistics for all texts in DB. 
    258''' 270''' 
    259        print info 271        print info 
    260 272 
    261 273 
    262def main(): 274def main(): 
    263    import optparse 275    import optparse 
    264    usage = \ 276    usage = \ 
    265'''%prog [options] <command> 277'''%prog [options] <command> 
    266 278 
    267Run about or help for details.''' 279Run about or help for details.''' 
    268    parser = optparse.OptionParser(usage) 280    parser = optparse.OptionParser(usage) 
    269    parser.add_option('-v', '--verbose', dest='verbose', help='Be verbose', 281    parser.add_option('-v', '--verbose', dest='verbose', help='Be verbose', 
    270            action='store_true', default=False)  282            action='store_true', default=False)  
    271    options, args = parser.parse_args() 283    options, args = parser.parse_args() 
    272     284     
    273    if len(args) == 0: 285    if len(args) == 0: 
    274        parser.print_help() 286        parser.print_help() 
    275        return 1 287        return 1 
    276    else: 288    else: 
    277        cmd = ShakespeareAdmin(verbose=options.verbose) 289        cmd = ShakespeareAdmin(verbose=options.verbose) 
    278        args = ' '.join(args) 290        args = ' '.join(args) 
    279        args = args.replace('-','_') 291        args = args.replace('-','_') 
    280        cmd.onecmd(args) 292        cmd.onecmd(args) 
    281 293 
  • trunk/shakespeare/templates/index.html

    Revision 181 Revision 194
    1<html xmlns:py="http://genshi.edgewall.org/" 1<html xmlns:py="http://genshi.edgewall.org/" 
    2  xmlns:xi="http://www.w3.org/2001/XInclude"> 2  xmlns:xi="http://www.w3.org/2001/XInclude"> 
    3   3   
    4  <py:def function="page_title">Home</py:def> 4  <py:def function="page_title">Home</py:def> 
    5 5 
    6  <div py:match="content"> 6  <div py:match="content"> 
    7    <p>Welcome to the Open Shakespeare web interface. This site provides access 7    <p>Welcome to the Open Shakespeare web interface. This site provides access 
    8    to many, but by no means all, of the facilities of the Open Shakespeare 8    to many, but by no means all, of the facilities of the Open Shakespeare 
    9    <strong>package</strong>. For example you can:</p> 9    <strong>package</strong>. For example you can:</p> 
    10 10 
    11    <ul> 11    <ul> 
    12      <li><a href="${h.url_for(controller='text', action=None)}">Read any of 12      <li><a href="${h.url_for(controller='text', action=None)}">Read any of 
    13        Shakespeare's plays or poems</a></li> 13        Shakespeare's plays or poems</a></li> 
    14      <li><a href="${h.url_for(controller='site', action='guide')}">Compare two 14      <li><a href="${h.url_for(controller='site', action='guide')}">Compare two 
    15        versions of the same text side-by-side</a></li> 15        versions of the same text side-by-side</a></li> 
    16      <li><a href="${h.url_for(controller='site', 16      <li><a href="${h.url_for(controller='search', 
    17        action='concordance')}">Browse the concordance</a></li> 17        action='index')}">Search any text</a></li> 
    18    </ul> 18    </ul> 
    19 19 
    20    <p>More information on using this web interface can be found in the <a 20    <p>More information on using this web interface can be found in the <a 
    21      href="${h.url_for(controller='site', action='guide')}">guide</a>.</p> 21      href="${h.url_for(controller='site', action='guide')}">guide</a>.</p> 
    22  </div> 22  </div> 
    23 23 
    24  <xi:include href="layout.html" /> 24  <xi:include href="layout.html" /> 
    25</html> 25</html>