From b113f49ce6aa4dec565b5c0ee30074a9ecdaf3f4 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Wed, 23 Mar 2011 01:59:19 -0400 Subject: [PATCH] python3 AND python2 packages. --- tablib/packages/markup.py | 44 +- tablib/packages/markup3.py | 484 ++ tablib/packages/xlwt/BIFFRecords.py | 4787 +++++++------- tablib/packages/xlwt/Bitmap.py | 520 +- tablib/packages/xlwt/Cell.py | 476 +- tablib/packages/xlwt/Column.py | 68 +- tablib/packages/xlwt/CompoundDoc.py | 1032 +-- tablib/packages/xlwt/ExcelFormula.py | 84 +- tablib/packages/xlwt/ExcelFormulaLexer.py | 254 +- tablib/packages/xlwt/ExcelFormulaParser.py | 1336 ++-- tablib/packages/xlwt/ExcelMagic.py | 1721 ++--- tablib/packages/xlwt/Formatting.py | 522 +- tablib/packages/xlwt/Row.py | 506 +- tablib/packages/xlwt/Style.py | 1185 ++-- tablib/packages/xlwt/UnicodeUtils.py | 162 +- tablib/packages/xlwt/Utils.py | 392 +- tablib/packages/xlwt/Workbook.py | 1271 ++-- tablib/packages/xlwt/Worksheet.py | 2593 ++++---- tablib/packages/xlwt/__init__.py | 25 +- tablib/packages/xlwt/antlr.py | 5743 +++++++++-------- tablib/packages/xlwt/doc/xlwt.html | 199 + tablib/packages/xlwt/examples/big-16Mb.py | 35 + tablib/packages/xlwt/examples/big-35Mb.py | 34 + tablib/packages/xlwt/examples/blanks.py | 36 + tablib/packages/xlwt/examples/col_width.py | 19 + tablib/packages/xlwt/examples/country.py | 10 + tablib/packages/xlwt/examples/dates.py | 37 + tablib/packages/xlwt/examples/format.py | 38 + .../packages/xlwt/examples/formula_names.py | 34 + tablib/packages/xlwt/examples/formulas.py | 47 + tablib/packages/xlwt/examples/hyperlinks.py | 28 + tablib/packages/xlwt/examples/image.py | 12 + tablib/packages/xlwt/examples/merged.py | 39 + tablib/packages/xlwt/examples/merged0.py | 30 + tablib/packages/xlwt/examples/merged1.py | 102 + tablib/packages/xlwt/examples/mini.py | 9 + tablib/packages/xlwt/examples/num_formats.py | 60 + tablib/packages/xlwt/examples/numbers.py | 25 + tablib/packages/xlwt/examples/outline.py | 113 + tablib/packages/xlwt/examples/panes.py | 58 + tablib/packages/xlwt/examples/parse-fmla.py | 12 + tablib/packages/xlwt/examples/protection.py | 122 + tablib/packages/xlwt/examples/python.bmp | Bin 0 -> 37446 bytes tablib/packages/xlwt/examples/row_styles.py | 17 + .../xlwt/examples/row_styles_empty.py | 18 + tablib/packages/xlwt/examples/simple.py | 24 + tablib/packages/xlwt/examples/sst.py | 52 + tablib/packages/xlwt/examples/unicode0.py | 12 + tablib/packages/xlwt/examples/unicode1.py | 28 + tablib/packages/xlwt/examples/unicode2.py | 19 + tablib/packages/xlwt/examples/wsprops.py | 155 + .../xlwt/examples/xlwt_easyxf_simple_demo.py | 46 + tablib/packages/xlwt/excel-formula.g | 374 ++ tablib/packages/yaml/__init__.py | 16 +- tablib/packages/yaml/composer.py | 18 +- tablib/packages/yaml/constructor.py | 198 +- tablib/packages/yaml/cyaml.py | 8 +- tablib/packages/yaml/dumper.py | 8 +- tablib/packages/yaml/emitter.py | 313 +- tablib/packages/yaml/error.py | 8 +- tablib/packages/yaml/loader.py | 12 +- tablib/packages/yaml/parser.py | 32 +- tablib/packages/yaml/reader.py | 89 +- tablib/packages/yaml/representer.py | 227 +- tablib/packages/yaml/resolver.py | 76 +- tablib/packages/yaml/scanner.py | 399 +- tablib/packages/yaml/serializer.py | 10 +- tablib/packages/yaml3/__init__.py | 288 + tablib/packages/yaml3/composer.py | 139 + tablib/packages/yaml3/constructor.py | 686 ++ tablib/packages/yaml3/cyaml.py | 85 + tablib/packages/yaml3/dumper.py | 62 + tablib/packages/yaml3/emitter.py | 1132 ++++ tablib/packages/yaml3/error.py | 75 + tablib/packages/yaml3/events.py | 86 + tablib/packages/yaml3/loader.py | 40 + tablib/packages/yaml3/nodes.py | 49 + tablib/packages/yaml3/parser.py | 584 ++ tablib/packages/yaml3/reader.py | 192 + tablib/packages/yaml3/representer.py | 374 ++ tablib/packages/yaml3/resolver.py | 224 + tablib/packages/yaml3/scanner.py | 1448 +++++ tablib/packages/yaml3/serializer.py | 111 + tablib/packages/yaml3/tokens.py | 104 + 84 files changed, 20179 insertions(+), 11963 deletions(-) create mode 100644 tablib/packages/markup3.py create mode 100644 tablib/packages/xlwt/doc/xlwt.html create mode 100644 tablib/packages/xlwt/examples/big-16Mb.py create mode 100644 tablib/packages/xlwt/examples/big-35Mb.py create mode 100644 tablib/packages/xlwt/examples/blanks.py create mode 100644 tablib/packages/xlwt/examples/col_width.py create mode 100644 tablib/packages/xlwt/examples/country.py create mode 100644 tablib/packages/xlwt/examples/dates.py create mode 100644 tablib/packages/xlwt/examples/format.py create mode 100644 tablib/packages/xlwt/examples/formula_names.py create mode 100644 tablib/packages/xlwt/examples/formulas.py create mode 100644 tablib/packages/xlwt/examples/hyperlinks.py create mode 100644 tablib/packages/xlwt/examples/image.py create mode 100644 tablib/packages/xlwt/examples/merged.py create mode 100644 tablib/packages/xlwt/examples/merged0.py create mode 100644 tablib/packages/xlwt/examples/merged1.py create mode 100644 tablib/packages/xlwt/examples/mini.py create mode 100644 tablib/packages/xlwt/examples/num_formats.py create mode 100644 tablib/packages/xlwt/examples/numbers.py create mode 100644 tablib/packages/xlwt/examples/outline.py create mode 100644 tablib/packages/xlwt/examples/panes.py create mode 100644 tablib/packages/xlwt/examples/parse-fmla.py create mode 100644 tablib/packages/xlwt/examples/protection.py create mode 100644 tablib/packages/xlwt/examples/python.bmp create mode 100644 tablib/packages/xlwt/examples/row_styles.py create mode 100644 tablib/packages/xlwt/examples/row_styles_empty.py create mode 100644 tablib/packages/xlwt/examples/simple.py create mode 100644 tablib/packages/xlwt/examples/sst.py create mode 100644 tablib/packages/xlwt/examples/unicode0.py create mode 100644 tablib/packages/xlwt/examples/unicode1.py create mode 100644 tablib/packages/xlwt/examples/unicode2.py create mode 100644 tablib/packages/xlwt/examples/wsprops.py create mode 100644 tablib/packages/xlwt/examples/xlwt_easyxf_simple_demo.py create mode 100644 tablib/packages/xlwt/excel-formula.g create mode 100644 tablib/packages/yaml3/__init__.py create mode 100644 tablib/packages/yaml3/composer.py create mode 100644 tablib/packages/yaml3/constructor.py create mode 100644 tablib/packages/yaml3/cyaml.py create mode 100644 tablib/packages/yaml3/dumper.py create mode 100644 tablib/packages/yaml3/emitter.py create mode 100644 tablib/packages/yaml3/error.py create mode 100644 tablib/packages/yaml3/events.py create mode 100644 tablib/packages/yaml3/loader.py create mode 100644 tablib/packages/yaml3/nodes.py create mode 100644 tablib/packages/yaml3/parser.py create mode 100644 tablib/packages/yaml3/reader.py create mode 100644 tablib/packages/yaml3/representer.py create mode 100644 tablib/packages/yaml3/resolver.py create mode 100644 tablib/packages/yaml3/scanner.py create mode 100644 tablib/packages/yaml3/serializer.py create mode 100644 tablib/packages/yaml3/tokens.py diff --git a/tablib/packages/markup.py b/tablib/packages/markup.py index 1973c00..234f116 100644 --- a/tablib/packages/markup.py +++ b/tablib/packages/markup.py @@ -68,7 +68,7 @@ class element: """Append the actual tags to content.""" out = "<%s" % tag - for key, value in kwargs.items( ): + for key, value in kwargs.iteritems( ): if value is not None: # when value is None that means stuff like <... checked> key = key.strip('_') # strip this so class_ will mean class, etc. if key == 'http_equiv': # special cases, maybe change _ to - overall? @@ -156,17 +156,17 @@ class page: if mode == 'strict_html' or mode == 'html': self.onetags = valid_onetags - self.onetags += list(map( str.lower, self.onetags )) + self.onetags += map( string.lower, self.onetags ) self.twotags = valid_twotags - self.twotags += list(map( str.lower, self.twotags )) + self.twotags += map( string.lower, self.twotags ) self.deptags = deprecated_onetags + deprecated_twotags - self.deptags += list(map( str.lower, self.deptags )) + self.deptags += map( string.lower, self.deptags ) self.mode = 'strict_html' elif mode == 'loose_html': self.onetags = valid_onetags + deprecated_onetags - self.onetags += list(map( str.lower, self.onetags )) + self.onetags += map( string.lower, self.onetags ) self.twotags = valid_twotags + deprecated_twotags - self.twotags += list(map( str.lower, self.twotags )) + self.twotags += map( string.lower, self.twotags ) self.mode = mode elif mode == 'xml': if onetags and twotags: @@ -183,7 +183,7 @@ class page: def __getattr__( self, attr ): if attr.startswith("__") and attr.endswith("__"): - raise AttributeError(attr) + raise AttributeError, attr return element( attr, case=self.case, parent=self ) def __str__( self ): @@ -307,7 +307,7 @@ class page: """This convenience function is only useful for html. It adds css stylesheet(s) to the document via the element.""" - if isinstance( filelist, str ): + if isinstance( filelist, basestring ): self.link( href=filelist, rel='stylesheet', type='text/css', media='all' ) else: for file in filelist: @@ -319,20 +319,20 @@ class page: a dictionary of the form { 'name':'content' }.""" if isinstance( mydict, dict ): - for name, content in mydict.items( ): + for name, content in mydict.iteritems( ): self.meta( name=name, content=content ) else: - raise TypeError("Metainfo should be called with a dictionary argument of name:content pairs.") + raise TypeError, "Metainfo should be called with a dictionary argument of name:content pairs." def scripts( self, mydict ): """Only useful in html, mydict is dictionary of src:type pairs will be rendered as """ if isinstance( mydict, dict ): - for src, type in mydict.items( ): + for src, type in mydict.iteritems( ): self.script( '', src=src, type='text/%s' % type ) else: - raise TypeError("Script should be given a dictionary of src:type pairs.") + raise TypeError, "Script should be given a dictionary of src:type pairs." class _oneliner: @@ -345,7 +345,7 @@ class _oneliner: def __getattr__( self, attr ): if attr.startswith("__") and attr.endswith("__"): - raise AttributeError(attr) + raise AttributeError, attr return element( attr, case=self.case, parent=None ) oneliner = _oneliner( case='lower' ) @@ -359,14 +359,14 @@ def _argsdicts( args, mydict ): elif len( args ) == 1: args = _totuple( args[0] ) else: - raise Exception("We should have never gotten here.") + raise Exception, "We should have never gotten here." - mykeys = list(mydict.keys( )) - myvalues = list(map( _totuple, list(mydict.values( )) )) + mykeys = mydict.keys( ) + myvalues = map( _totuple, mydict.values( ) ) - maxlength = max( list(map( len, [ args ] + myvalues )) ) + maxlength = max( map( len, [ args ] + myvalues ) ) - for i in range( maxlength ): + for i in xrange( maxlength ): thisdict = { } for key, value in zip( mykeys, myvalues ): try: @@ -383,7 +383,7 @@ def _argsdicts( args, mydict ): def _totuple( x ): """Utility stuff to convert string, int, float, None or anything to a usable tuple.""" - if isinstance( x, str ): + if isinstance( x, basestring ): out = x, elif isinstance( x, ( int, float ) ): out = str( x ), @@ -397,7 +397,7 @@ def _totuple( x ): def escape( text, newline=False ): """Escape special html characters.""" - if isinstance( text, str ): + if isinstance( text, basestring ): if '&' in text: text = text.replace( '&', '&' ) if '>' in text: @@ -419,7 +419,7 @@ _escape = escape def unescape( text ): """Inverse of escape.""" - if isinstance( text, str ): + if isinstance( text, basestring ): if '&' in text: text = text.replace( '&', '&' ) if '>' in text: @@ -481,4 +481,4 @@ class CustomizationError( MarkupError ): self.message = "If you customize the allowed elements, you must define both types 'onetags' and 'twotags'." if __name__ == '__main__': - print(__doc__) + print __doc__ diff --git a/tablib/packages/markup3.py b/tablib/packages/markup3.py new file mode 100644 index 0000000..1973c00 --- /dev/null +++ b/tablib/packages/markup3.py @@ -0,0 +1,484 @@ +# This code is in the public domain, it comes +# with absolutely no warranty and you can do +# absolutely whatever you want with it. + +__date__ = '17 May 2007' +__version__ = '1.7' +__doc__= """ +This is markup.py - a Python module that attempts to +make it easier to generate HTML/XML from a Python program +in an intuitive, lightweight, customizable and pythonic way. + +The code is in the public domain. + +Version: %s as of %s. + +Documentation and further info is at http://markup.sourceforge.net/ + +Please send bug reports, feature requests, enhancement +ideas or questions to nogradi at gmail dot com. + +Installation: drop markup.py somewhere into your Python path. +""" % ( __version__, __date__ ) + +import string + +class element: + """This class handles the addition of a new element.""" + + def __init__( self, tag, case='lower', parent=None ): + self.parent = parent + + if case == 'lower': + self.tag = tag.lower( ) + else: + self.tag = tag.upper( ) + + def __call__( self, *args, **kwargs ): + if len( args ) > 1: + raise ArgumentError( self.tag ) + + # if class_ was defined in parent it should be added to every element + if self.parent is not None and self.parent.class_ is not None: + if 'class_' not in kwargs: + kwargs['class_'] = self.parent.class_ + + if self.parent is None and len( args ) == 1: + x = [ self.render( self.tag, False, myarg, mydict ) for myarg, mydict in _argsdicts( args, kwargs ) ] + return '\n'.join( x ) + elif self.parent is None and len( args ) == 0: + x = [ self.render( self.tag, True, myarg, mydict ) for myarg, mydict in _argsdicts( args, kwargs ) ] + return '\n'.join( x ) + + if self.tag in self.parent.twotags: + for myarg, mydict in _argsdicts( args, kwargs ): + self.render( self.tag, False, myarg, mydict ) + elif self.tag in self.parent.onetags: + if len( args ) == 0: + for myarg, mydict in _argsdicts( args, kwargs ): + self.render( self.tag, True, myarg, mydict ) # here myarg is always None, because len( args ) = 0 + else: + raise ClosingError( self.tag ) + elif self.parent.mode == 'strict_html' and self.tag in self.parent.deptags: + raise DeprecationError( self.tag ) + else: + raise InvalidElementError( self.tag, self.parent.mode ) + + def render( self, tag, single, between, kwargs ): + """Append the actual tags to content.""" + + out = "<%s" % tag + for key, value in kwargs.items( ): + if value is not None: # when value is None that means stuff like <... checked> + key = key.strip('_') # strip this so class_ will mean class, etc. + if key == 'http_equiv': # special cases, maybe change _ to - overall? + key = 'http-equiv' + elif key == 'accept_charset': + key = 'accept-charset' + out = "%s %s=\"%s\"" % ( out, key, escape( value ) ) + else: + out = "%s %s" % ( out, key ) + if between is not None: + out = "%s>%s" % ( out, between, tag ) + else: + if single: + out = "%s />" % out + else: + out = "%s>" % out + if self.parent is not None: + self.parent.content.append( out ) + else: + return out + + def close( self ): + """Append a closing tag unless element has only opening tag.""" + + if self.tag in self.parent.twotags: + self.parent.content.append( "" % self.tag ) + elif self.tag in self.parent.onetags: + raise ClosingError( self.tag ) + elif self.parent.mode == 'strict_html' and self.tag in self.parent.deptags: + raise DeprecationError( self.tag ) + + def open( self, **kwargs ): + """Append an opening tag.""" + + if self.tag in self.parent.twotags or self.tag in self.parent.onetags: + self.render( self.tag, False, None, kwargs ) + elif self.mode == 'strict_html' and self.tag in self.parent.deptags: + raise DeprecationError( self.tag ) + +class page: + """This is our main class representing a document. Elements are added + as attributes of an instance of this class.""" + + def __init__( self, mode='strict_html', case='lower', onetags=None, twotags=None, separator='\n', class_=None ): + """Stuff that effects the whole document. + + mode -- 'strict_html' for HTML 4.01 (default) + 'html' alias for 'strict_html' + 'loose_html' to allow some deprecated elements + 'xml' to allow arbitrary elements + + case -- 'lower' element names will be printed in lower case (default) + 'upper' they will be printed in upper case + + onetags -- list or tuple of valid elements with opening tags only + twotags -- list or tuple of valid elements with both opening and closing tags + these two keyword arguments may be used to select + the set of valid elements in 'xml' mode + invalid elements will raise appropriate exceptions + + separator -- string to place between added elements, defaults to newline + + class_ -- a class that will be added to every element if defined""" + + valid_onetags = [ "AREA", "BASE", "BR", "COL", "FRAME", "HR", "IMG", "INPUT", "LINK", "META", "PARAM" ] + valid_twotags = [ "A", "ABBR", "ACRONYM", "ADDRESS", "B", "BDO", "BIG", "BLOCKQUOTE", "BODY", "BUTTON", + "CAPTION", "CITE", "CODE", "COLGROUP", "DD", "DEL", "DFN", "DIV", "DL", "DT", "EM", "FIELDSET", + "FORM", "FRAMESET", "H1", "H2", "H3", "H4", "H5", "H6", "HEAD", "HTML", "I", "IFRAME", "INS", + "KBD", "LABEL", "LEGEND", "LI", "MAP", "NOFRAMES", "NOSCRIPT", "OBJECT", "OL", "OPTGROUP", + "OPTION", "P", "PRE", "Q", "SAMP", "SCRIPT", "SELECT", "SMALL", "SPAN", "STRONG", "STYLE", + "SUB", "SUP", "TABLE", "TBODY", "TD", "TEXTAREA", "TFOOT", "TH", "THEAD", "TITLE", "TR", + "TT", "UL", "VAR" ] + deprecated_onetags = [ "BASEFONT", "ISINDEX" ] + deprecated_twotags = [ "APPLET", "CENTER", "DIR", "FONT", "MENU", "S", "STRIKE", "U" ] + + self.header = [ ] + self.content = [ ] + self.footer = [ ] + self.case = case + self.separator = separator + + # init( ) sets it to True so we know that has to be printed at the end + self._full = False + self.class_= class_ + + if mode == 'strict_html' or mode == 'html': + self.onetags = valid_onetags + self.onetags += list(map( str.lower, self.onetags )) + self.twotags = valid_twotags + self.twotags += list(map( str.lower, self.twotags )) + self.deptags = deprecated_onetags + deprecated_twotags + self.deptags += list(map( str.lower, self.deptags )) + self.mode = 'strict_html' + elif mode == 'loose_html': + self.onetags = valid_onetags + deprecated_onetags + self.onetags += list(map( str.lower, self.onetags )) + self.twotags = valid_twotags + deprecated_twotags + self.twotags += list(map( str.lower, self.twotags )) + self.mode = mode + elif mode == 'xml': + if onetags and twotags: + self.onetags = onetags + self.twotags = twotags + elif ( onetags and not twotags ) or ( twotags and not onetags ): + raise CustomizationError( ) + else: + self.onetags = russell( ) + self.twotags = russell( ) + self.mode = mode + else: + raise ModeError( mode ) + + def __getattr__( self, attr ): + if attr.startswith("__") and attr.endswith("__"): + raise AttributeError(attr) + return element( attr, case=self.case, parent=self ) + + def __str__( self ): + + if self._full and ( self.mode == 'strict_html' or self.mode == 'loose_html' ): + end = [ '', '' ] + else: + end = [ ] + + return self.separator.join( self.header + self.content + self.footer + end ) + + def __call__( self, escape=False ): + """Return the document as a string. + + escape -- False print normally + True replace < and > by < and > + the default escape sequences in most browsers""" + + if escape: + return _escape( self.__str__( ) ) + else: + return self.__str__( ) + + def add( self, text ): + """This is an alias to addcontent.""" + self.addcontent( text ) + + def addfooter( self, text ): + """Add some text to the bottom of the document""" + self.footer.append( text ) + + def addheader( self, text ): + """Add some text to the top of the document""" + self.header.append( text ) + + def addcontent( self, text ): + """Add some text to the main part of the document""" + self.content.append( text ) + + + def init( self, lang='en', css=None, metainfo=None, title=None, header=None, + footer=None, charset=None, encoding=None, doctype=None, bodyattrs=None, script=None ): + """This method is used for complete documents with appropriate + doctype, encoding, title, etc information. For an HTML/XML snippet + omit this method. + + lang -- language, usually a two character string, will appear + as in html mode (ignored in xml mode) + + css -- Cascading Style Sheet filename as a string or a list of + strings for multiple css files (ignored in xml mode) + + metainfo -- a dictionary in the form { 'name':'content' } to be inserted + into meta element(s) as + (ignored in xml mode) + + bodyattrs --a dictionary in the form { 'key':'value', ... } which will be added + as attributes of the element as + (ignored in xml mode) + + script -- dictionary containing src:type pairs, + + title -- the title of the document as a string to be inserted into + a title element as my title (ignored in xml mode) + + header -- some text to be inserted right after the element + (ignored in xml mode) + + footer -- some text to be inserted right before the element + (ignored in xml mode) + + charset -- a string defining the character set, will be inserted into a + + element (ignored in xml mode) + + encoding -- a string defining the encoding, will be put into to first line of + the document as in + xml mode (ignored in html mode) + + doctype -- the document type string, defaults to + + in html mode (ignored in xml mode)""" + + self._full = True + + if self.mode == 'strict_html' or self.mode == 'loose_html': + if doctype is None: + doctype = "" + self.header.append( doctype ) + self.html( lang=lang ) + self.head( ) + if charset is not None: + self.meta( http_equiv='Content-Type', content="text/html; charset=%s" % charset ) + if metainfo is not None: + self.metainfo( metainfo ) + if css is not None: + self.css( css ) + if title is not None: + self.title( title ) + if script is not None: + self.scripts( script ) + self.head.close() + if bodyattrs is not None: + self.body( **bodyattrs ) + else: + self.body( ) + if header is not None: + self.content.append( header ) + if footer is not None: + self.footer.append( footer ) + + elif self.mode == 'xml': + if doctype is None: + if encoding is not None: + doctype = "" % encoding + else: + doctype = "" + self.header.append( doctype ) + + def css( self, filelist ): + """This convenience function is only useful for html. + It adds css stylesheet(s) to the document via the element.""" + + if isinstance( filelist, str ): + self.link( href=filelist, rel='stylesheet', type='text/css', media='all' ) + else: + for file in filelist: + self.link( href=file, rel='stylesheet', type='text/css', media='all' ) + + def metainfo( self, mydict ): + """This convenience function is only useful for html. + It adds meta information via the element, the argument is + a dictionary of the form { 'name':'content' }.""" + + if isinstance( mydict, dict ): + for name, content in mydict.items( ): + self.meta( name=name, content=content ) + else: + raise TypeError("Metainfo should be called with a dictionary argument of name:content pairs.") + + def scripts( self, mydict ): + """Only useful in html, mydict is dictionary of src:type pairs will + be rendered as """ + + if isinstance( mydict, dict ): + for src, type in mydict.items( ): + self.script( '', src=src, type='text/%s' % type ) + else: + raise TypeError("Script should be given a dictionary of src:type pairs.") + + +class _oneliner: + """An instance of oneliner returns a string corresponding to one element. + This class can be used to write 'oneliners' that return a string + immediately so there is no need to instantiate the page class.""" + + def __init__( self, case='lower' ): + self.case = case + + def __getattr__( self, attr ): + if attr.startswith("__") and attr.endswith("__"): + raise AttributeError(attr) + return element( attr, case=self.case, parent=None ) + +oneliner = _oneliner( case='lower' ) +upper_oneliner = _oneliner( case='upper' ) + +def _argsdicts( args, mydict ): + """A utility generator that pads argument list and dictionary values, will only be called with len( args ) = 0, 1.""" + + if len( args ) == 0: + args = None, + elif len( args ) == 1: + args = _totuple( args[0] ) + else: + raise Exception("We should have never gotten here.") + + mykeys = list(mydict.keys( )) + myvalues = list(map( _totuple, list(mydict.values( )) )) + + maxlength = max( list(map( len, [ args ] + myvalues )) ) + + for i in range( maxlength ): + thisdict = { } + for key, value in zip( mykeys, myvalues ): + try: + thisdict[ key ] = value[i] + except IndexError: + thisdict[ key ] = value[-1] + try: + thisarg = args[i] + except IndexError: + thisarg = args[-1] + + yield thisarg, thisdict + +def _totuple( x ): + """Utility stuff to convert string, int, float, None or anything to a usable tuple.""" + + if isinstance( x, str ): + out = x, + elif isinstance( x, ( int, float ) ): + out = str( x ), + elif x is None: + out = None, + else: + out = tuple( x ) + + return out + +def escape( text, newline=False ): + """Escape special html characters.""" + + if isinstance( text, str ): + if '&' in text: + text = text.replace( '&', '&' ) + if '>' in text: + text = text.replace( '>', '>' ) + if '<' in text: + text = text.replace( '<', '<' ) + if '\"' in text: + text = text.replace( '\"', '"' ) + if '\'' in text: + text = text.replace( '\'', '"' ) + if newline: + if '\n' in text: + text = text.replace( '\n', '
' ) + + return text + +_escape = escape + +def unescape( text ): + """Inverse of escape.""" + + if isinstance( text, str ): + if '&' in text: + text = text.replace( '&', '&' ) + if '>' in text: + text = text.replace( '>', '>' ) + if '<' in text: + text = text.replace( '<', '<' ) + if '"' in text: + text = text.replace( '"', '\"' ) + + return text + +class dummy: + """A dummy class for attaching attributes.""" + pass + +doctype = dummy( ) +doctype.frameset = "" +doctype.strict = "" +doctype.loose = "" + +class russell: + """A dummy class that contains anything.""" + + def __contains__( self, item ): + return True + + +class MarkupError( Exception ): + """All our exceptions subclass this.""" + def __str__( self ): + return self.message + +class ClosingError( MarkupError ): + def __init__( self, tag ): + self.message = "The element '%s' does not accept non-keyword arguments (has no closing tag)." % tag + +class OpeningError( MarkupError ): + def __init__( self, tag ): + self.message = "The element '%s' can not be opened." % tag + +class ArgumentError( MarkupError ): + def __init__( self, tag ): + self.message = "The element '%s' was called with more than one non-keyword argument." % tag + +class InvalidElementError( MarkupError ): + def __init__( self, tag, mode ): + self.message = "The element '%s' is not valid for your mode '%s'." % ( tag, mode ) + +class DeprecationError( MarkupError ): + def __init__( self, tag ): + self.message = "The element '%s' is deprecated, instantiate markup.page with mode='loose_html' to allow it." % tag + +class ModeError( MarkupError ): + def __init__( self, mode ): + self.message = "Mode '%s' is invalid, possible values: strict_html, loose_html, xml." % mode + +class CustomizationError( MarkupError ): + def __init__( self ): + self.message = "If you customize the allowed elements, you must define both types 'onetags' and 'twotags'." + +if __name__ == '__main__': + print(__doc__) diff --git a/tablib/packages/xlwt/BIFFRecords.py b/tablib/packages/xlwt/BIFFRecords.py index e2120fd..4660c03 100644 --- a/tablib/packages/xlwt/BIFFRecords.py +++ b/tablib/packages/xlwt/BIFFRecords.py @@ -1,2394 +1,2393 @@ -# -*- coding: utf-8 -*- - -from struct import pack -from .UnicodeUtils import upack1, upack2 -import sys - -class SharedStringTable(object): - _SST_ID = 0x00FC - _CONTINUE_ID = 0x003C - - def __init__(self, encoding): - self.encoding = encoding - self._str_indexes = {} - self._tally = [] - self._add_calls = 0 - # Following 3 attrs are used for temporary storage in the - # get_biff_record() method and methods called by it. The pseudo- - # initialisation here is for documentation purposes only. - self._sst_record = None - self._continues = None - self._current_piece = None - - def add_str(self, s): - if self.encoding != 'ascii' and not isinstance(s, str): - s = str(s, self.encoding) - self._add_calls += 1 - if s not in self._str_indexes: - idx = len(self._str_indexes) - self._str_indexes[s] = idx - self._tally.append(1) - else: - idx = self._str_indexes[s] - self._tally[idx] += 1 - return idx - - def del_str(self, idx): - # This is called when we are replacing the contents of a string cell. - assert self._tally[idx] > 0 - self._tally[idx] -= 1 - self._add_calls -= 1 - - def str_index(self, s): - return self._str_indexes[s] - - def get_biff_record(self): - self._sst_record = b'' - self._continues = [None, None] - self._current_piece = pack(' 0x2020: # limit for BIFF7/8 - chunks = [] - pos = 0 - while pos < len(data): - chunk_pos = pos + 0x2020 - chunk = data[pos:chunk_pos] - chunks.append(chunk) - pos = chunk_pos - continues = pack('<2H', self._REC_ID, len(chunks[0])) + chunks[0] - for chunk in chunks[1:]: - continues += pack('<2H%ds'%len(chunk), 0x003C, len(chunk), chunk) - # 0x003C -- CONTINUE record id - return continues - else: - return self.get_rec_header() + data - - -class Biff8BOFRecord(BiffRecord): - """ - Offset Size Contents - 0 2 Version, contains 0600H for BIFF8 and BIFF8X - 2 2 Type of the following data: - 0005H = Workbook globals - 0006H = Visual Basic module - 0010H = Worksheet - 0020H = Chart - 0040H = Macro sheet - 0100H = Workspace file - 4 2 Build identifier - 6 2 Build year - 8 4 File history flags - 12 4 Lowest Excel version that can read all records in this file - """ - _REC_ID = 0x0809 - # stream types - BOOK_GLOBAL = 0x0005 - VB_MODULE = 0x0006 - WORKSHEET = 0x0010 - CHART = 0x0020 - MACROSHEET = 0x0040 - WORKSPACE = 0x0100 - - def __init__(self, rec_type): - version = 0x0600 - build = 0x0DBB - year = 0x07CC - file_hist_flags = 0x00 - ver_can_read = 0x06 - - self._rec_data = pack('<4H2I', version, rec_type, build, year, file_hist_flags, ver_can_read) - - -class InteraceHdrRecord(BiffRecord): - _REC_ID = 0x00E1 - - def __init__(self): - self._rec_data = pack('BB', 0xB0, 0x04) - - -class InteraceEndRecord(BiffRecord): - _REC_ID = 0x00E2 - - def __init__(self): - self._rec_data = b'' - - -class MMSRecord(BiffRecord): - _REC_ID = 0x00C1 - - def __init__(self): - self._rec_data = pack('> 15 - c = low_15 | high_15 - passwd_hash ^= c - passwd_hash ^= len(plaintext) - passwd_hash ^= 0xCE4B - return passwd_hash - - def __init__(self, passwd = b""): - self._rec_data = pack('