-
Notifications
You must be signed in to change notification settings - Fork 9
/
PythonTidy.py
4842 lines (3909 loc) · 144 KB
/
PythonTidy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
# PythonTidy.py
# 2006 Oct 27 . ccr
'''PythonTidy.py cleans up, regularizes, and reformats the text of
Python scripts.
===========================================
Copyright © 2006 Charles Curtis Rhode
===========================================
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA.
===========================================
Charles Curtis Rhode,
1518 N 3rd, Sheboygan, WI 53081
mailto:CRhode@LacusVeris.com?subject=PythonTidy
===========================================
This script reads Python code from standard input and writes a revised
version to standard output.
Alternatively, it may be invoked with file names as arguments:
o python PythonTidy.py input output
Suffice it to say that *input* defaults to \'-\', the standard input,
and *output* defaults to \'-\', the standard output.
It means to encapsulate the wisdom revealed in:
o Rossum, Guido van, and Barry Warsaw. "PEP 8: Style Guide for Python
Code." 23 Mar. 2006. Python.org. 28 Nov. 2006
<http://www.python.org/dev/peps/pep-0008/>.
Python scripts are usually so good looking that no beautification is
required. However, from time to time, it may be necessary to alter
the style to conform to changing standards. This script converts
programs in a consistent way. It abstracts the pretty presentation of
the symbolic code from the humdrum[1] process of writing it and
getting it to work.
This script assumes that the input Python code is well-formed and
works to begin with. It doesn\'t check. If all goes well, the output
Python code will work, too. Of course, you are advised to test it
fully to be sure.
This script should be run only by python.2.5 (and perhaps higher) on
scripts written for that version (and perhaps lower) because of its
limited knowledge of and expectations for the abstract syntax tree
node classes returned by the *compiler* module. It wouldn\'t hurt
much to try it from (and on) other versions, though, and it might
actually work.
Search this script for "Python Version Dependency."
Most of the Python 2.5 test suite passes through PythonTidy.py
unimpaired. I ran the Python regression tests for 2.5.2 which is the
version supported by Debian 5.0 "Lenny."
On my system these tests fail before tidying:
o test_imageop
o test_pyclbr
o test_sys
282 tests succeed after tidying with the default PythonTidy global
settings, but these tests fail:
*test_grammar* exposes bug 6978 in the *compiler* module. Tuples are
immutable and hashable and thus suitable as dict indices. Whereas a
singleton tuple literal (x,) is valid as an index, the *compiler*
module parses it as x when it appears.
*test_dis* compares "disassembled" Python byte code to what is
expected. While byte code for a tidied script should be functionally
equivalent to the untidied version, it will not necessarily be
identical.
*test_trace* compares the line numbers in a functional trace of a
running script with those expected. A statement in a tidied script
will generally have a line number slightly different from the same
statement in the untidied version.
*test_doctest* is an extensive suite of tests of the *doctest* module,
which itself is used to document test code within doc strings and at
need to compare instant results against those expected. One of the
tests in *test_doctest* appears to require line numbers consistent
with expectations, but tidied scripts generally violate such
conditions as explained above.
The more esoteric capabilities of PythonTidy.py had to be turned off
to avoid corrupting the test-suite code. In practice, you\'ll want to
run with PERSONAL = True (See, below.) to use all the functionality,
and of course you\'ll have the good taste to find and patch all the
glitches it introduces.
[1] "Humdrum: A low cart with three wheels, drawn by one horse." The
Collaborative International Dictionary of English v.0.48.
'''
from __future__ import division
DEBUG = False
PERSONAL = False
VERSION = '1.21' # 2010 Sep 03
# 2010 Sep 08 . v1.21 . ccr . For Nikolai Prokoschenko:
#
# o When double spacing is prescribed by PEP 8, do it before
# leading comments.
#
# o Per Pep 8, double space around top-level classes only.
#
# o Don't split index values from keys before colon.
#
# o Preserve spelling of words in long strings containing special
# characters.
#
# o Optionally, bring closing brackets, braces, and parens of split
# series back left to the margin of the enclosing statement. See
# JAVA_STYLE_LIST_DEDENT.
#
# 2010 Mar 10 . v1.20 . ccr . For Kuang-che Wu:
#
# o Optionally preserve unassigned constants so that code to be tidied
# may contain blocks of commented-out lines that have been no-op'ed
# with leading and trailing triple quotes. Python scripts may declare
# constants without assigning them to a variables, but PythonTidy
# considers this wasteful and normally elides them.
#
# o Generalize an earlier exception made for PythonDoc sentinels so
# that the COMMENT_PREFIX is not inserted before any comments that
# start with doubled number-signs.
#
# o Optionally omit parentheses around tuples, which are superfluous
# after all. Normal PythonTidy behavior will be still to include them
# as a sort of tuple display analogous to list displays, dict
# displays, and yet-to-come set displays.
#
# o Kuang-che Wu has provided code that removes superfluous parens in
# complex algebraic and logical expressions, which PythonTidy used to
# interpolate to make operator precedence explicit. From now on
# PythonTidy will rely upon default operator precedence and insert
# parens only to enforce order of evaluation that is not default.
# This should make tidied code more succinct, which usually results in
# improved legibility. This fixes a PythonTidy bug noticed by
# Kuang-che Wu having to do with order of evaluation of comparisons.
#
# o As a matter of style per PEP 308, parentheses are preferred around
# conditional expressions.
#
# o Give the bitwise invert operator the same precedence as unary plus
# and unary minus.
#
# I am making other changes to PythonTidy so that a few more of the
# examples from the Python *test* module will pass:
#
# o Index literal pool by type. (Use *repr*.)
#
# o Never append a trailing comma to starred or double-starred
# arguments.
# 2009 Jun 29 . v1.19 . ccr . For Daniel G. Siegel at
# http://home.cs.tum.edu, *python* 2.6 tokenizer returns newlines
# separate from comments, so, though it may be necessary to save
# newlines, it won't do for them to overlay comments.
# 2009 Feb 05 . v1.18 . ccr . For Massimo Di Pierro at
# http://mdp.cti.depaul.edu/, do not break up raw literals.
# 2008 Jan 30 . v1.17 . ccr . This fixes regression in newline support
# introduced at v1.11, which was first reported by Dr0id.
# 2008 Jan 06 . v1.16 . ccr . John Machin demonstrates that hex values
# are not in fact stored in the literal pool. They should always have
# been and should always be.
# Apparently, doubled number-signs in columns one and two are
# sacrosanct sentinels in Fredrik Lundh's PythonDoc documentation
# generator and must not therefore be disturbed.
# Fix a crash caused by indents' crossing the centerline.
# 2007 May 25 . v1.15 . ccr . Don't split lines in the middle of
# function-parameter assignment.
# Optionally wrap doc strings and comments to COL_LIMIT.
# 2007 May 01, 23, 24 . v1.14 . ccr . Gaëtan de Menten at
# http://openhex.org points out that a null statement is generated by
# a trailing semicolon. This has been fixed. He has been helpful by,
# among other things, insisting that I clean up the rendering of doc
# strings and comments.
# Forcing string-literal delimiters to quotes or apostrophes (if
# required) is now done before storing them to the literal pool.
# Wrap chunks of code whose successors cannot be wrapped.
# Don't elide leading tabs in comments and doc strings. Instead
# substitute DOC_TAB_REPLACEMENT so they can be edited out manually.
# Split long string literals at spaces when CAN_SPLIT_STRINGS is True.
# String literals with attributes are no longer parenthesized.
# For François Pinard, wrap before operators.
# Subscripted class attributes are no longer parenthesized.
# Differentiate MAX_SEPS for different situations.
# 2007 Mar 06 . v1.12 . ccr . The requests of Aaron Bingham: Specify
# boilerplate to be inserted after the module doc string. Optionally
# split wide string literals at the column limit. Force trailing
# newline.
# 2007 Jan 22 . v1.11 . ccr . This update implements a couple of
# well-taken user requests:
# Jens Diemer wants a module-level function, *tidy_up*, to accept file
# names or file-like objects.
# Wolfgang Grafen wants trailing spaces eliminated to avoid spurious
# differences with pre-tidied code.
# 2007 Jan 14 . v1.10 . ccr . There was a big problem with earlier
# versions: Canonical values were substituted for strings and numbers.
# For example, decimal integers were substituted for hexadecimal, and
# escaped strings for raw strings. Authors of Python scripts usually
# use peculiar notations for peculiar purposes, and doing away with
# them negatively impacts the readability of the code.
# This version preserves the original constants (parsed by *tokenize*)
# in a literal pool indexed by the value they evaluate to. The
# canonical values (output by *compiler*) are then translated back
# (when possible) to the original constants by looking them up in the
# literal pool.
# 2006 Dec 19 . v1.9 . ccr . If class name is a string, pass it to
# personal substitutions routine to distinguish module globals like
# gtk.VBox from class attributes like gtk.Dialog.vbox.
# 2006 Dec 17 . v1.8 . ccr . Trailing comma in function parameter list
# is not allowed in all cases. Catch substitutions that collide with
# built-ins.
# 2006 Dec 14 . v1.7 . ccr . Track line numbers on output.
# Write a "Name Substitutions Report" on stderr.
# 2006 Dec 13 . v1.6 . ccr . A *yield* may appear in parens when it is
# the subject of an assignment; otherwise, not.
# 2006 Dec 05 . v1.5 . ccr . Strings default to single quotes when
# DOUBLE_QUOTED_STRINGS = False. Pass the newline convention from
# input to output (transparently :-) ).
# 2006 Dec 01 . v1.4 . ccr . Tighten qualifications for in-line
# comments. Decode string nodes. Enclose doc strings in double
# quotes. Allow file-name arguments.
# 2006 Nov 30 . v1.3 . ccr . Safe check against names of *compiler* .
# abstract syntax tree nodes rather than their classes to step around
# one Python Version Dependency.
import sys
import os
import codecs
import StringIO
import re
import textwrap # 2007 May 25
if DEBUG:
import token
import doctest
import tokenize
import compiler
ZERO = 0
SPACE = ' '
NULL = ''
NA = -1
APOST = "'"
# Old code is parsed. New code is generated from the parsed version,
# using these literals:
COL_LIMIT = 72
INDENTATION = ' '
ASSIGNMENT = ' = '
FUNCTION_PARAM_ASSIGNMENT = '='
FUNCTION_PARAM_SEP = ', '
LIST_SEP = ', '
SUBSCRIPT_SEP = ', '
DICT_COLON = ': '
SLICE_COLON = ':'
COMMENT_PREFIX = '# ' # 2007 May 25
SHEBANG = '#!/usr/bin/python'
CODING = 'utf-8'
CODING_SPEC = '# -*- coding: %s -*-' % CODING
BOILERPLATE = NULL # 2007 Mar 06
BLANK_LINE = NULL
KEEP_BLANK_LINES = True
ADD_BLANK_LINES_AROUND_COMMENTS = True
ADD_BLANK_LINE_AFTER_DOC_STRING = True
MAX_SEPS_FUNC_DEF = 3 # 2007 May 24
MAX_SEPS_FUNC_REF = 5 # 2007 May 24
MAX_SEPS_SERIES = 5 # 2007 May 24
MAX_SEPS_DICT = 3 # 2007 May 24
MAX_LINES_BEFORE_SPLIT_LIT = 2
LEFT_MARGIN = NULL
NORMALIZE_DOC_STRINGS = False
LEFTJUST_DOC_STRINGS = False
WRAP_DOC_STRINGS = False # 2007 May 25
LEFTJUST_COMMENTS = False
WRAP_COMMENTS = False
DOUBLE_QUOTED_STRINGS = False # 2006 Dec 05
SINGLE_QUOTED_STRINGS = False # 2007 May 01
RECODE_STRINGS = False # 2006 Dec 01
OVERRIDE_NEWLINE = '' # 2006 Dec 05
CAN_SPLIT_STRINGS = False # 2007 Mar 06
DOC_TAB_REPLACEMENT = '....' # 2007 May 24
KEEP_UNASSIGNED_CONSTANTS = False # 2010 Mar 10
PARENTHESIZE_TUPLE_DISPLAY = True # 2010 Mar 10
JAVA_STYLE_LIST_DEDENT = False # 2010 Sep 08
# Repertoire of name-transformation functions:
def all_lower_case(str, **attribs):
return str.lower()
def all_upper_case(str, **attribs):
return str.upper()
def title_case(str, **attribs):
return str.title()
def strip_underscores(str, **attribs):
return str.replace('_', NULL)
def insert_underscores(str, **attribs):
return UNDERSCORE_PATTERN.sub('_\\1', str)
def is_magic(str):
return str in ['self', 'cls'] or str.startswith('__') and str.endswith('__')
def underscore_to_camel_case(str, **attribs):
if is_magic(str):
return str
else:
return strip_underscores(title_case(camel_case_to_underscore(str)))
def camel_case_to_underscore(str, **attribs):
if is_magic(str):
return str
else:
return all_lower_case(insert_underscores(str))
def unmangle(str, **attribs):
if str.startswith('__'):
str = str[2:]
return str
def munge(str, **attribs):
"""Create an unparsable name.
"""
return '<*%s*>' % str
def substitutions(str, **attribs):
result = SUBSTITUTE_FOR.get(str, str)
module = attribs.get('module') # 2006 Dec 19
if module is None:
pass
else:
result = SUBSTITUTE_FOR.get('%s.%s' % (module, str), result)
return result
def elide_c(str, **attribs):
return ELIDE_C_PATTERN.sub('\\1', str)
def elide_a(str, **attribs):
return ELIDE_A_PATTERN.sub('\\1', str)
def elide_f(str, **attribs):
return ELIDE_F_PATTERN.sub('\\1', str)
# Name-transformation scripts:
LOCAL_NAME_SCRIPT = []
GLOBAL_NAME_SCRIPT = []
CLASS_NAME_SCRIPT = []
FUNCTION_NAME_SCRIPT = []
# It is not wise to monkey with the
# spelling of function names (methods)
# where they are defined unless you are
# willing to change their spelling where
# they are referred to as class
# attributes, too.
FORMAL_PARAM_NAME_SCRIPT = []
# It is not wise to monkey with the
# spelling of formal parameters for fear
# of changing those of functions
# (methods) defined in other modules.
ATTR_NAME_SCRIPT = []
# It is not wise to monkey with the
# spelling of attributes (methods) for
# fear of changing those of classes
# defined in other modules.
# Author's preferences:
if PERSONAL:
LEFTJUST_DOC_STRINGS = True
LOCAL_NAME_SCRIPT.extend([unmangle, camel_case_to_underscore])
GLOBAL_NAME_SCRIPT.extend([unmangle, camel_case_to_underscore,
all_upper_case])
CLASS_NAME_SCRIPT.extend([elide_c, underscore_to_camel_case])
FUNCTION_NAME_SCRIPT.extend([camel_case_to_underscore])
FORMAL_PARAM_NAME_SCRIPT.extend([elide_a, camel_case_to_underscore])
ATTR_NAME_SCRIPT.extend([elide_f, camel_case_to_underscore,
substitutions])
# Other global constants:
UNDERSCORE_PATTERN = re.compile('(?<=[a-z])([A-Z])')
COMMENT_PATTERN = re.compile('([^#]*?)#\s?') # 2007 May 25
SHEBANG_PATTERN = re.compile('#!')
CODING_PATTERN = re.compile('coding[=:]\\s*([.\\w\\-_]+)')
NEW_LINE_PATTERN = re.compile(r'(?<!\\)(?:(?:\\n)|\n)')
PGRAPH_PATTERN = re.compile(r'\n{2,}') # 2007 May 25
UNIVERSAL_NEW_LINE_PATTERN = re.compile(r'((?:\r\n)|(?:\r)|(?:\n))')
QUOTE_PATTERN = re.compile('([rRuU]{,2})((?:"{3})|(?:\'{3})|(?:")|(?:\'))') # 2007 May 01
ELIDE_C_PATTERN = re.compile('^c([A-Z])')
ELIDE_A_PATTERN = re.compile('^a([A-Z])')
ELIDE_F_PATTERN = re.compile('^f([A-Z])')
DOC_WRAPPER = textwrap.TextWrapper(
width=COL_LIMIT,
expand_tabs=True,
replace_whitespace=True,
initial_indent=NULL,
subsequent_indent=NULL,
fix_sentence_endings=False,
break_long_words=True,
) # 2007 May 25
SUBSTITUTE_FOR = {
'abday_1':'ABDAY_1',
'abday_2':'ABDAY_2',
'abday_3':'ABDAY_3',
'abday_4':'ABDAY_4',
'abday_5':'ABDAY_5',
'abday_6':'ABDAY_6',
'abday_7':'ABDAY_7',
'abmon_1':'ABMON_1',
'abmon_10':'ABMON_10',
'abmon_11':'ABMON_11',
'abmon_12':'ABMON_12',
'abmon_2':'ABMON_2',
'abmon_3':'ABMON_3',
'abmon_4':'ABMON_4',
'abmon_5':'ABMON_5',
'abmon_6':'ABMON_6',
'abmon_7':'ABMON_7',
'abmon_8':'ABMON_8',
'abmon_9':'ABMON_9',
'accel_group': 'AccelGroup',
'action_default': 'ACTION_DEFAULT',
'action_copy': 'ACTION_COPY',
'align_left': 'ALIGN_LEFT',
'align_right': 'ALIGN_RIGHT',
'align_center': 'ALIGN_CENTER',
'alignment': 'Alignment',
'button_press': 'BUTTON_PRESS',
'button_press_mask': 'BUTTON_PRESS_MASK',
'buttons_cancel': 'BUTTONS_CANCEL',
'can_default': 'CAN_DEFAULT',
'can_focus': 'CAN_FOCUS',
'cell_renderer_pixbuf': 'CellRendererPixbuf',
'cell_renderer_text': 'CellRendererText',
'check_button': 'CheckButton',
'child_nodes': 'childNodes',
'color': 'Color',
'config_parser': 'ConfigParser',
'cursor': 'Cursor',
'day_1':'DAY_1',
'day_2':'DAY_2',
'day_3':'DAY_3',
'day_4':'DAY_4',
'day_5':'DAY_5',
'day_6':'DAY_6',
'day_7':'DAY_7',
'dest_default_all': 'DEST_DEFAULT_ALL',
'dialog_modal': 'DIALOG_MODAL',
'dict_reader': 'DictReader',
'dict_writer': 'DictWriter',
'dir_tab_forward': 'DIR_TAB_FORWARD',
'dotall': 'DOTALL',
'dotall': 'DOTALL',
'enter_notify_mask': 'ENTER_NOTIFY_MASK',
'error': 'Error',
'event_box': 'EventBox',
'expand': 'EXPAND',
'exposure_mask': 'EXPOSURE_MASK',
'file_selection': 'FileSelection',
'fill': 'FILL',
'ftp': 'FTP',
'get_attribute': 'getAttribute',
'gtk.button': 'Button',
'gtk.combo': 'Combo',
'gtk.dialog': 'Dialog',
'gtk.entry': 'Entry',
'pixmap': 'Pixmap',
'gtk.image': 'Image',
'gtk.label': 'Label',
'gtk.menu': 'Menu',
'gtk.pack_end': 'PACK_END',
'gtk.pack_start': 'PACK_START',
'gtk.vbox': 'VBox',
'gtk.window': 'Window',
'hand2': 'HAND2',
'hbox': 'HBox',
'icon_size_button': 'ICON_SIZE_BUTTON',
'icon_size_dialog': 'ICON_SIZE_DIALOG',
'icon_size_dnd': 'ICON_SIZE_DND',
'icon_size_large_toolbar': 'ICON_SIZE_LARGE_TOOLBAR',
'icon_size_menu': 'ICON_SIZE_MENU',
'icon_size_small_toolbar': 'ICON_SIZE_SMALL_TOOLBAR',
'image_menu_item': 'ImageMenuItem',
'item_factory': 'ItemFactory',
'justify_center': 'JUSTIFY_CENTER',
'justify_fill': 'JUSTIFY_FILL',
'justify_left': 'JUSTIFY_LEFT',
'justify_right': 'JUSTIFY_RIGHT',
'list_item': 'ListItem',
'list_store': 'ListStore',
'menu_bar': 'MenuBar',
'message_dialog': 'MessageDialog',
'message_info': 'MESSAGE_INFO',
'mon_1':'MON_1',
'mon_10':'MON_10',
'mon_11':'MON_11',
'mon_12':'MON_12',
'mon_2':'MON_2',
'mon_3':'MON_3',
'mon_4':'MON_4',
'mon_5':'MON_5',
'mon_6':'MON_6',
'mon_7':'MON_7',
'mon_8':'MON_8',
'mon_9':'MON_9',
'multiline': 'MULTILINE',
'node_type': 'nodeType',
'notebook': 'Notebook',
'o_creat': 'O_CREAT',
'o_excl': 'O_EXCL',
'o_ndelay': 'O_NDELAY',
'o_rdwr': 'O_RDWR',
'p_nowait':'P_NOWAIT',
'parsing_error': 'ParsingError',
'pointer_motion_mask': 'POINTER_MOTION_MASK',
'pointer_motion_hint_mask': 'POINTER_MOTION_HINT_MASK',
'policy_automatic': 'POLICY_AUTOMATIC',
'policy_never': 'POLICY_NEVER',
'radio_button': 'RadioButton',
'realized': 'REALIZED',
'relief_none': 'RELIEF_NONE',
'request':'Request',
'response_cancel': 'RESPONSE_CANCEL',
'response_delete_event': 'RESPONSE_DELETE_EVENT',
'response_no': 'RESPONSE_NO',
'response_none': 'RESPONSE_NONE',
'response_ok': 'RESPONSE_OK',
'response_yes': 'RESPONSE_YES',
'scrolled_window': 'ScrolledWindow',
'shadow_in': 'SHADOW_IN',
'sniffer': 'Sniffer',
'sort_ascending': 'SORT_ASCENDING',
'sort_descending': 'SORT_DESCENDING',
'state_normal': 'STATE_NORMAL',
'stock_add': 'STOCK_ADD',
'stock_apply': 'STOCK_APPLY',
'stock_bold': 'STOCK_BOLD',
'stock_cancel': 'STOCK_CANCEL',
'stock_close': 'STOCK_CLOSE',
'stock_convert': 'STOCK_CONVERT',
'stock_copy': 'STOCK_COPY',
'stock_cut': 'STOCK_CUT',
'stock_dialog_info': 'STOCK_DIALOG_INFO',
'stock_dialog_info': 'STOCK_DIALOG_INFO',
'stock_dialog_question': 'STOCK_DIALOG_QUESTION',
'stock_execute': 'STOCK_EXECUTE',
'stock_find': 'STOCK_FIND',
'stock_find_and_replace': 'STOCK_FIND_AND_REPLACE',
'stock_go_back': 'STOCK_GO_BACK',
'stock_go_forward': 'STOCK_GO_FORWARD',
'stock_help': 'STOCK_HELP',
'stock_index': 'STOCK_INDEX',
'stock_jump_to': 'STOCK_JUMP_TO',
'stock_new': 'STOCK_NEW',
'stock_no': 'STOCK_NO',
'stock_ok': 'STOCK_OK',
'stock_open': 'STOCK_OPEN',
'stock_paste': 'STOCK_PASTE',
'stock_preferences': 'STOCK_PREFERENCES',
'stock_print_preview': 'STOCK_PRINT_PREVIEW',
'stock_quit': 'STOCK_QUIT',
'stock_refresh': 'STOCK_REFRESH',
'stock_remove': 'STOCK_REMOVE',
'stock_save': 'STOCK_SAVE',
'stock_save_as': 'STOCK_SAVE_AS',
'stock_yes': 'STOCK_YES',
'string_io': 'StringIO',
'style_italic': 'STYLE_ITALIC',
'sunday': 'SUNDAY',
'tab': 'Tab',
'tab_array': 'TabArray',
'tab_left': 'TAB_LEFT',
'table': 'Table',
'target_same_app': 'TARGET_SAME_APP',
'target_same_widget': 'TARGET_SAME_WIDGET',
'text_iter': 'TextIter',
'text_node': 'TEXT_NODE',
'text_tag': 'TextTag',
'text_view': 'TextView',
'text_window_text': 'TEXT_WINDOW_TEXT',
'text_window_widget': 'TEXT_WINDOW_WIDGET',
'text_wrapper':'TextWrapper',
'tooltips': 'Tooltips',
'tree_view': 'TreeView',
'tree_view_column': 'TreeViewColumn',
'type_string': 'TYPE_STRING',
'underline_single': 'UNDERLINE_SINGLE',
'weight_bold': 'WEIGHT_BOLD',
'window_toplevel': 'WINDOW_TOPLEVEL',
'wrap_none': 'WRAP_NONE',
'wrap_word': 'WRAP_WORD',
}
def force_quote(encoded, double=True, quoted=True): # 2007 May 01
r"""Change the type of quotation marks (or not) on an already quoted string.
>>> force_quote("See the cat.", quoted=False)
'"See the cat."'
>>> force_quote("'See the cat.'")
'"See the cat."'
>>> force_quote("'See the cat.'", double=False)
"'See the cat.'"
>>> force_quote('"See the cat."')
'"See the cat."'
>>> force_quote('"See the cat."', double=False)
"'See the cat.'"
>>> force_quote('"\"That\'s that,\" said the cat."')
'"\\"That\'s that,\\" said the cat."'
>>> force_quote('"\"That\'s that,\" said the cat."', double=False)
'\'"That\\\'s that," said the cat.\''
>>> force_quote("'\"That\'s that,\" said the cat.'")
'"\\"That\'s that,\\" said the cat."'
>>> force_quote("ru'ick'")
'ru"ick"'
>>> force_quote("ru'ick'", double=False)
"ru'ick'"
>>> force_quote('ru"ick"')
'ru"ick"'
>>> force_quote('ru"ick"', double=False)
"ru'ick'"
>>> force_quote("'''ick'''", double=False)
"'''ick'''"
"""
if quoted: # 2007 May 23
match = QUOTE_PATTERN.match(encoded)
if match is None: # 2008 Jan 06
prefix = NULL
size = 1
else:
(prefix, quote_old) = match.group(1, 2)
encoded = QUOTE_PATTERN.sub(NULL, encoded, 1)
size = len(quote_old)
assert encoded[-size:] == quote_old
encoded = encoded[:-size]
else:
prefix = NULL
size = 1
double_backslash_delimited_substrings = encoded.split(r'\\')
for (ndx, substring) in enumerate(double_backslash_delimited_substrings):
substring = substring.replace(r'\"','"').replace(r"\'","'")
if double:
substring = substring.replace('"',r'\"')
else:
substring = substring.replace("'",r"\'")
double_backslash_delimited_substrings[ndx] = substring
encoded = r'\\'.join(double_backslash_delimited_substrings)
if double:
quote_new = '"' * size
else:
quote_new = "'" * size
result = NULL.join([prefix, quote_new, encoded, quote_new])
return result
def wrap_lines(lines, width=COL_LIMIT,
initial_indent=NULL, subsequent_indent=NULL): # 2007 May 25
"""Wrap lines of text, preserving blank lines.
Lines is a Python list of strings *without* new-line terminators.
Initial_indent is a string that will be prepended to the first
line of wrapped output.
Subsequent_indent is a string that will be prepended to all lines
of wrapped output except the first.
The result is a Python list of strings *without* new-Line terminators.
>>> print '\\n'.join(wrap_lines('''Now is the time
... for every good man
... to come to the aid of his party.
...
...
... Don't pass the buck
... but give your buck
... to the party of your choice.'''.splitlines(), width=40))
Now is the time for every good man to
come to the aid of his party.
<BLANKLINE>
Don't pass the buck but give your buck
to the party of your choice.
"""
DOC_WRAPPER.width = width
DOC_WRAPPER.initial_indent = initial_indent
DOC_WRAPPER.subsequent_indent = subsequent_indent
result = [line.strip() for line in lines]
result = '\n'.join(result)
pgraphs = PGRAPH_PATTERN.split(result)
result = []
while pgraphs:
pgraph = DOC_WRAPPER.fill(pgraphs.pop(ZERO))
result.extend(pgraph.splitlines())
if pgraphs:
result.append(NULL)
return result
def leftjust_lines(lines): # 2007 May 25
"""Left justify lines of text.
Lines is a Python list of strings *without* new-line terminators.
The result is a Python list of strings *without* new-Line terminators.
"""
result = [line.strip() for line in lines]
return result
class InputUnit(object):
"""File-buffered wrapper for sys.stdin.
"""
def __init__(self, file_in):
object.__init__(self)
self.is_file_like = hasattr(file_in, 'read') # 2007 Jan 22
if self.is_file_like:
buffer = file_in.read() # 2006 Dec 05
else:
unit = open(os.path.expanduser(file_in), 'rb')
buffer = unit.read() # 2006 Dec 05
unit.close()
self.lines = UNIVERSAL_NEW_LINE_PATTERN.split(buffer) # 2006 Dec 05
# self.lines will be a list of lines and terminators because
# UNIVERSAL_NEW_LINE_PATTERN captures the actual newline.
# If we have more than one line we'll follow the original newline
# usage unless OVERRIDE_NEWLINE is set:
if len(self.lines) > 2:
if not OVERRIDE_NEWLINE:
self.newline = self.lines[1] # ... the first delimiter.
else:
self.newline = OVERRIDE_NEWLINE
look_ahead = '\n'.join([self.lines[ZERO],self.lines[2]])
else:
self.newline = '\n'
look_ahead = NULL
match = CODING_PATTERN.search(look_ahead)
if match is None:
self.coding = 'ascii'
else:
self.coding = match.group(1)
self.rewind() # 2006 Dec 05
return
def rewind(self): # 2006 Dec 05
self.ndx = ZERO
self.end = len(self.lines) - 1
return self
def next(self): # 2006 Dec 05
if self.ndx > self.end:
raise StopIteration
elif self.ndx == self.end:
result = self.lines[self.ndx]
else:
result = self.lines[self.ndx] + '\n'
self.ndx += 2
return result
def __iter__(self): # 2006 Dec 05
return self
def readline(self): # 2006 Dec 05
try:
result = self.next()
except StopIteration:
result = NULL
return result
def readlines(self): # 2006 Dec 05
self.rewind()
return [line for line in self]
def __str__(self): # 2006 Dec 05
result = self.readlines()
while result[:-1] == NULL:
result.pop(-1)
last_line = result[-1]
if last_line[:-1] == '\n': # 2007 Mar 07
pass
else:
last_line += '\n'
result[-1] = last_line
return NULL.join(result)
def decode(self, str):
return str # It will not do to feed Unicode to *compiler.parse*.
class OutputUnit(object):
"""Line-buffered wrapper for sys.stdout.
"""
def __init__(self, file_out):
object.__init__(self)
self.is_file_like = hasattr(file_out, 'write') # 2007 Jan 22
if self.is_file_like:
self.unit = codecs.getwriter(CODING)(file_out)
else:
self.unit = codecs.open(os.path.expanduser(file_out), 'wb', CODING)
self.blank_line_count = 1
self.margin = LEFT_MARGIN
self.newline = INPUT.newline # 2006 Dec 05
self.lineno = ZERO # 2006 Dec 14
self.buffer = NULL
self.chunks = None # 2009 Oct 26
return
def close(self): # 2006 Dec 01
self.unit.write(self.buffer.rstrip(self.newline)) # 2007 Jan 22
if self.is_file_like:
pass
else:
self.unit.close()
return self
def line_init(self, indent=ZERO, lineno=ZERO):
self.blank_line_count = ZERO
self.col = ZERO
if DEBUG:
margin = '%5i %s' % (lineno, INDENTATION * indent)
else:
margin = self.margin + INDENTATION * indent
self.tab_stack = []
self.tab_set(len(margin) + len(INDENTATION))
self.chunks = []
self.line_more(margin)
return self
def line_more(
self,
chunk=NULL,
tab_set=False,
tab_clear=False,
can_split_str=False,
can_split_after=False,
can_break_after=False,
): # 2007 Mar 06
self.chunks.append([
chunk,
tab_set,
tab_clear,
can_split_str,
can_split_after,
can_break_after,
])
self.col += len(chunk)
return self
def line_term(self, pause=False): # 2007 May 25
def is_split_needed(cumulative_width):
pos = self.pos
return ((pos + cumulative_width) > COL_LIMIT) and (pos > ZERO) # 2007 May 01
def drop_word(chunk, can_split_after): # 2007 May 23
result = COL_LIMIT - self.pos
if can_split_after:
result -= 1
else:
result -= 2
ndx = result - 1
while (ndx >= 20) and ((result - ndx) <= 20):
if chunk[ndx] in [SPACE]:
result = ndx + 1
break
ndx -= 1
return result
self.pos = ZERO
can_split_before = False
can_break_before = False
cumulative_width = ZERO
chunk_lengths = []
self.chunks.reverse()
for (
chunk,
tab_set,
tab_clear,
can_split_str,
can_split_after,
can_break_after,
) in self.chunks: # 2007 May 01
if can_split_after or can_break_after:
cumulative_width = ZERO
cumulative_width += len(chunk)
chunk_lengths.insert(ZERO, [
chunk,
cumulative_width,
tab_set,
tab_clear,
can_split_str,
can_split_after,
can_break_after,
])
for (
chunk,
cumulative_width,
tab_set,
tab_clear,
can_split_str,