PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/samples/sample2.py

https://bitbucket.org/apalala/pypeg
Python | 101 lines | 97 code | 0 blank | 4 comment | 0 complexity | 8fd7d33f77bde430654bd7feec91d968 MD5 | raw file
Possible License(s): GPL-2.0
  1. """
  2. Ini file sample (see end of file for the content of the ini file)
  3. To parse an ini file we use the grammar below. Comments in ini files are
  4. starting with a semicolon ";".
  5. >>> ini_file = parse(ini_file_text, IniFile, comment=(";", restline))
  6. Because IniFile and Section are Namespaces, we can access their content by
  7. name.
  8. >>> print("found: " + repr(ini_file["Number 1"]["that"]))
  9. found: ...'something else'
  10. pyPEG is measuring the position of each object in the input text with a
  11. tuple (line_number, offset).
  12. >>> ini_file["Number 1"]["that"].position_in_text
  13. (3, 26)
  14. >>> ini_file["Number 2"].position_in_text
  15. (6, 85)
  16. pyPEG can also do the reverse job, composing a text of an object tree.
  17. >>> ini_file["Number 1"]["that"] = Key("new one")
  18. >>> ini_file["Number 3"] = Section()
  19. >>> print(compose(ini_file))
  20. [Number 1]
  21. this=something
  22. that=new one
  23. [Number 2]
  24. once=anything
  25. twice=goes
  26. [Number 3]
  27. ...
  28. pyPEG contains an XML backend, too:
  29. >>> from pypeg2.xmlast import thing2xml
  30. >>> print(thing2xml(ini_file, pretty=True).decode())
  31. <IniFile>
  32. <Section name="Number 1">
  33. <Key name="this">something</Key>
  34. <Key name="that">new one</Key>
  35. </Section>
  36. <Section name="Number 2">
  37. <Key name="once">anything</Key>
  38. <Key name="twice">goes</Key>
  39. </Section>
  40. <Section name="Number 3"/>
  41. </IniFile>
  42. ...
  43. In this sample the tree contains named objects only. Then we can output object
  44. names as tag names. Spaces in names will be translated into underscores.
  45. >>> print(thing2xml(ini_file, pretty=True, object_names=True).decode())
  46. <IniFile>
  47. <Number_1>
  48. <this>something</this>
  49. <that>new one</that>
  50. </Number_1>
  51. <Number_2>
  52. <once>anything</once>
  53. <twice>goes</twice>
  54. </Number_2>
  55. <Number_3/>
  56. </IniFile>
  57. ...
  58. """
  59. from __future__ import unicode_literals, print_function
  60. from pypeg2 import *
  61. import re
  62. # ini file parser
  63. # symbols in ini files can include spaces
  64. Symbol.regex = re.compile(r"[\w\s]+")
  65. class Key(str):
  66. grammar = name(), "=", restline, endl
  67. class Section(Namespace):
  68. grammar = "[", name(), "]", endl, maybe_some(Key)
  69. class IniFile(Namespace):
  70. grammar = some(Section)
  71. if __name__ == "__main__":
  72. ini_file_text = """[Number 1]
  73. this=something
  74. that=something else
  75. ; now for something even more useless
  76. [Number 2]
  77. once=anything
  78. twice=goes
  79. """
  80. import doctest
  81. doctest.testmod(optionflags=(doctest.ELLIPSIS | doctest.REPORT_ONLY_FIRST_FAILURE))