Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / scripts / tapconvert.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 import sys, getpass
5
6 from twisted.python import usage
7 from twisted.application import app
8 from twisted.persisted import sob
9
10
11 class ConvertOptions(usage.Options):
12     synopsis = "Usage: tapconvert [options]"
13     optParameters = [
14         ['in',      'i', None,     "The filename of the tap to read from"],
15         ['out',     'o', None,     "A filename to write the tap to"],
16         ['typein',  'f', 'guess',
17          "The  format to use; this can be 'guess', 'python', "
18          "'pickle', 'xml', or 'source'."],
19         ['typeout', 't', 'source',
20          "The output format to use; this can be 'pickle', 'xml', or 'source'."],
21         ]
22
23     optFlags = [
24         ['decrypt', 'd', "The specified tap/aos/xml file is encrypted."],
25         ['encrypt', 'e', "Encrypt file before writing"]
26         ]
27
28     compData = usage.Completions(
29         optActions={"typein": usage.CompleteList(["guess", "python", "pickle",
30                                                   "xml", "source"]),
31                     "typeout": usage.CompleteList(["pickle", "xml", "source"]),
32                     "in": usage.CompleteFiles(descr="tap file to read from"),
33                     "out": usage.CompleteFiles(descr="tap file to write to"),
34                     }
35         )
36
37     def postOptions(self):
38         if self['in'] is None:
39             raise usage.UsageError("%s\nYou must specify the input filename."
40                                    % self)
41         if self["typein"] == "guess":
42             try:
43                 self["typein"] = sob.guessType(self["in"])
44             except KeyError:
45                 raise usage.UsageError("Could not guess type for '%s'" %
46                                        self["typein"])
47
48 def run():
49     options = ConvertOptions()
50     try:
51         options.parseOptions(sys.argv[1:])
52     except usage.UsageError, e:
53         print e
54     else:
55         app.convertStyle(options["in"], options["typein"],
56                      options.opts['decrypt'] or getpass.getpass('Passphrase: '),
57                      options["out"], options['typeout'], options["encrypt"])