| 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. |
|---|
| 40 | Open Shakespeare is open-knowledge and open-source. See COPYING for details. | 44 | Open Shakespeare is open-knowledge and open-source. See COPYING for details. |
|---|
| 41 | | 45 | |
|---|
| 42 | For more information about the package run `info`. | 46 | For 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 | """ |
|---|
| 91 | Download and process all Project Gutenberg shakespeare texts""" | 95 | Download 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 | ''' |
|---|
| 109 | Download and process all Moby/Bosak shakespeare texts''' | 113 | Download 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 | |
|---|
| 155 | Please use `paster serve` to run a server now, e.g.:: | 159 | Please 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: |
|---|
| 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 | ''' |
|---|
| 250 | stats addtext {name} | 262 | stats addtext {name} |
|---|
| 251 | - Add db text named {name} to stats index. | 263 | - Add db text named {name} to stats index. |
|---|
| 252 | | 264 | |
|---|
| 253 | stats show {name} | 265 | stats show {name} |
|---|
| 254 | - Query stats index with {query}. | 266 | - Query stats index with {query}. |
|---|
| 255 | | 267 | |
|---|
| 256 | stats init | 268 | stats 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 | |
|---|
| 262 | def main(): | 274 | def main(): |
|---|
| 263 | import optparse | 275 | import optparse |
|---|
| 264 | usage = \ | 276 | usage = \ |
|---|
| 265 | '''%prog [options] <command> | 277 | '''%prog [options] <command> |
|---|
| 266 | | 278 | |
|---|
| 267 | Run about or help for details.''' | 279 | Run 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 | |
|---|