odfuserfield 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2006-2007 Søren Roug, European Environment Agency
  4. #
  5. # This is free software. You may redistribute it under the terms
  6. # of the Apache license and the GNU General Public License Version
  7. # 2 or at your option any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public
  15. # License along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. #
  18. # Contributor(s): Michael Howitz, gocept gmbh & co. kg
  19. import sys
  20. import getopt
  21. import odf.userfield
  22. if sys.version_info[0]==3: unicode=str
  23. listfields = False
  24. Listfields = False
  25. xfields = []
  26. Xfields = []
  27. setfields = {}
  28. outputfile = None
  29. inputfile = None
  30. def exitwithusage(exitcode=2):
  31. """ print out usage information """
  32. sys.stderr.write("Usage: %s [-lL] [-xX metafield] [-s metafield:value]... "
  33. "[-o output] [inputfile]\n" % sys.argv[0])
  34. sys.stderr.write("\tInputfile must be OpenDocument format\n")
  35. sys.exit(exitcode)
  36. try:
  37. opts, args = getopt.getopt(sys.argv[1:], "lLs:o:x:X:")
  38. except getopt.GetoptError:
  39. exitwithusage()
  40. if len(opts) == 0:
  41. exitwithusage()
  42. for o, a in opts:
  43. if o == '-s':
  44. if a.find(":") >= 0:
  45. k,v = a.split(":",1)
  46. else:
  47. k,v = (a, "")
  48. if len(k) == 0:
  49. exitwithusage()
  50. setfields[unicode(k)] = unicode(v)
  51. if o == '-l':
  52. listfields = True
  53. Listfields = False
  54. if o == '-L':
  55. Listfields = True
  56. listfields = False
  57. if o == "-x":
  58. xfields.append(unicode(a))
  59. if o == "-X":
  60. Xfields.append(unicode(a))
  61. if o == "-o":
  62. outputfile = unicode(a)
  63. if len(args) != 0:
  64. inputfile = unicode(args[0])
  65. user_fields = odf.userfield.UserFields(inputfile, outputfile)
  66. if xfields:
  67. for value in user_fields.list_values(xfields):
  68. print (value)
  69. if Listfields or Xfields:
  70. if Listfields:
  71. Xfields = None
  72. for field_name, value_type, value in user_fields.list_fields_and_values(
  73. Xfields):
  74. print ("%s#%s:%s" % (field_name, value_type, value))
  75. if listfields:
  76. for value in user_fields.list_fields():
  77. print (value)
  78. if setfields:
  79. user_fields.update(setfields)
  80. # Local Variables: ***
  81. # mode: python ***
  82. # End: ***