|
15 | 15 | written in Python. |
16 | 16 | """ |
17 | 17 |
|
18 | | -# XXX Perhaps there should be a slimmed version that doesn't contain |
19 | | -# all those backwards compatible and debugging classes and functions? |
20 | | - |
21 | 18 | # History |
22 | 19 | # ------- |
23 | 20 | # |
|
43 | 40 | import collections |
44 | 41 | from io import StringIO |
45 | 42 |
|
46 | | -__all__ = ["MiniFieldStorage", "FieldStorage", "FormContentDict", |
47 | | - "SvFormContentDict", "InterpFormContentDict", "FormContent", |
| 43 | +__all__ = ["MiniFieldStorage", "FieldStorage", |
48 | 44 | "parse", "parse_qs", "parse_qsl", "parse_multipart", |
49 | 45 | "parse_header", "print_exception", "print_environ", |
50 | 46 | "print_form", "print_directory", "print_arguments", |
@@ -777,124 +773,6 @@ def make_file(self): |
777 | 773 | return tempfile.TemporaryFile("w+", encoding="utf-8", newline="\n") |
778 | 774 |
|
779 | 775 |
|
780 | | - |
781 | | -# Backwards Compatibility Classes |
782 | | -# =============================== |
783 | | - |
784 | | -class FormContentDict(collections.Mapping): |
785 | | - """Form content as dictionary with a list of values per field. |
786 | | -
|
787 | | - form = FormContentDict() |
788 | | -
|
789 | | - form[key] -> [value, value, ...] |
790 | | - key in form -> Boolean |
791 | | - form.keys() -> [key, key, ...] |
792 | | - form.values() -> [[val, val, ...], [val, val, ...], ...] |
793 | | - form.items() -> [(key, [val, val, ...]), (key, [val, val, ...]), ...] |
794 | | - form.dict == {key: [val, val, ...], ...} |
795 | | -
|
796 | | - """ |
797 | | - def __init__(self, environ=os.environ, keep_blank_values=0, strict_parsing=0): |
798 | | - self.dict = self.data = parse(environ=environ, |
799 | | - keep_blank_values=keep_blank_values, |
800 | | - strict_parsing=strict_parsing) |
801 | | - self.query_string = environ['QUERY_STRING'] |
802 | | - |
803 | | - def __len__(self): |
804 | | - return len(self.dict) |
805 | | - |
806 | | - def __iter__(self): |
807 | | - return iter(self.dict) |
808 | | - |
809 | | - def __getitem__(self, key): |
810 | | - return self.dict[key] |
811 | | - |
812 | | - |
813 | | -class SvFormContentDict(FormContentDict): |
814 | | - """Form content as dictionary expecting a single value per field. |
815 | | -
|
816 | | - If you only expect a single value for each field, then form[key] |
817 | | - will return that single value. It will raise an IndexError if |
818 | | - that expectation is not true. If you expect a field to have |
819 | | - possible multiple values, than you can use form.getlist(key) to |
820 | | - get all of the values. values() and items() are a compromise: |
821 | | - they return single strings where there is a single value, and |
822 | | - lists of strings otherwise. |
823 | | -
|
824 | | - """ |
825 | | - def __getitem__(self, key): |
826 | | - if len(self.dict[key]) > 1: |
827 | | - raise IndexError('expecting a single value') |
828 | | - return self.dict[key][0] |
829 | | - def getlist(self, key): |
830 | | - return self.dict[key] |
831 | | - def values(self): |
832 | | - result = [] |
833 | | - for value in self.dict.values(): |
834 | | - if len(value) == 1: |
835 | | - result.append(value[0]) |
836 | | - else: result.append(value) |
837 | | - return result |
838 | | - def items(self): |
839 | | - result = [] |
840 | | - for key, value in self.dict.items(): |
841 | | - if len(value) == 1: |
842 | | - result.append((key, value[0])) |
843 | | - else: result.append((key, value)) |
844 | | - return result |
845 | | - |
846 | | - |
847 | | -class InterpFormContentDict(SvFormContentDict): |
848 | | - """This class is present for backwards compatibility only.""" |
849 | | - def __getitem__(self, key): |
850 | | - v = SvFormContentDict.__getitem__(self, key) |
851 | | - if v[0] in '0123456789+-.': |
852 | | - try: return int(v) |
853 | | - except ValueError: |
854 | | - try: return float(v) |
855 | | - except ValueError: pass |
856 | | - return v.strip() |
857 | | - def values(self): |
858 | | - result = [] |
859 | | - for key in self.keys(): |
860 | | - try: |
861 | | - result.append(self[key]) |
862 | | - except IndexError: |
863 | | - result.append(self.dict[key]) |
864 | | - return result |
865 | | - def items(self): |
866 | | - result = [] |
867 | | - for key in self.keys(): |
868 | | - try: |
869 | | - result.append((key, self[key])) |
870 | | - except IndexError: |
871 | | - result.append((key, self.dict[key])) |
872 | | - return result |
873 | | - |
874 | | - |
875 | | -class FormContent(FormContentDict): |
876 | | - """This class is present for backwards compatibility only.""" |
877 | | - def values(self, key): |
878 | | - if key in self.dict :return self.dict[key] |
879 | | - else: return None |
880 | | - def indexed_value(self, key, location): |
881 | | - if key in self.dict: |
882 | | - if len(self.dict[key]) > location: |
883 | | - return self.dict[key][location] |
884 | | - else: return None |
885 | | - else: return None |
886 | | - def value(self, key): |
887 | | - if key in self.dict: return self.dict[key][0] |
888 | | - else: return None |
889 | | - def length(self, key): |
890 | | - return len(self.dict[key]) |
891 | | - def stripped(self, key): |
892 | | - if key in self.dict: return self.dict[key][0].strip() |
893 | | - else: return None |
894 | | - def pars(self): |
895 | | - return self.dict |
896 | | - |
897 | | - |
898 | 776 | # Test/debug code |
899 | 777 | # =============== |
900 | 778 |
|
|
0 commit comments