HCE Project Python language Distributed Tasks Manager Application, Distributed Crawler Application and client API bindings.  2.0.0-chaika
Hierarchical Cluster Engine Python language binding
join.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 """
4 HCE project, Python bindings, Distributed Tasks Manager application.
5 join.py - cli tool to join data files list delimited with space passed via stdin in
6 to one output serialized json stream to stdout.
7 
8 @package: dc
9 @file join.py
10 @author Alexander Vybornyh <alexander.hce.cluster@gmail.com>
11 @link: http://hierarchical-cluster-engine.com/
12 @copyright: Copyright &copy; 2013-2016 IOIX Ukraine
13 @license: http://hierarchical-cluster-engine.com/license/
14 @since: 0.1
15 """
16 
17 import ppath
18 from ppath import sys
19 
20 import os
21 import sys
22 import json
23 import app.Utils as Utils # pylint: disable=F0401
24 import app.Consts as APP_CONSTS
25 from app.Utils import varDump
26 
27 from cement.core import foundation
28 
29 
30 exitCode = APP_CONSTS.EXIT_SUCCESS
31 DEFAULT_DELIMITER_VALUE = ' '
32 
33 # #Parse command line argumets
34 #
35 # @param - None
36 # @return inputFiles - input files list and outputFile - output file name
38  # variables for result
39  inputFiles = outputFile = None
40 
41  # create the app
42  app = foundation.CementApp('join_tools')
43  try:
44  # setup the application
45  app.setup()
46  # add support command line arguments
47  app.args.add_argument('-f', '--files', action='store', metavar='input_file_names', help='input file names',
48  required=True)
49  app.args.add_argument('-d', '--delimiter', action='store', metavar='delimiter', help='delimiter of input file names string',
50  required=False)
51  app.args.add_argument('-o', '--output_file', action='store', metavar='output_file', help='output file name',
52  required=False)
53  # run the application
54  app.run()
55 
56  # get parameters
57  delimiter = DEFAULT_DELIMITER_VALUE
58  if app.pargs.delimiter is not None:
59  delimiter = app.pargs.delimiter
60 
61  if app.pargs.files is not None:
62  inputFiles = app.pargs.files.split(delimiter)
63 
64  if app.pargs.output_file is not None:
65  outputFile = app.pargs.output_file
66 
67  except Exception, err:
68  sys.stderr.write(str(err) + '\n' + Utils.getTracebackInfo() + '\n')
69  finally:
70  app.close()
71 
72  return inputFiles, outputFile
73 
74 
75 # # Read input file
76 #
77 # @param fileName- input file name
78 # @return loaded json from file
79 def readFile(fileName):
80  # variable for result
81  ret = None
82  try:
83  f = open(fileName, 'r')
84  ret = json.load(f)
85  f.close()
86 
87  except Exception, err:
88  sys.stderr.write(str(err) + '\n')
89 
90  return ret
91 
92 
93 # # Output result data
94 #
95 # @param outputFile - output file name
96 # @param result - result data
97 # @return - None
98 def outputResultData(outputFile, result):
99  if result is not None:
100  if outputFile is None:
101  sys.stdout.write(json.dumps(result))
102  else:
103  f = open(outputFile, 'w')
104  json.dump(result, f)
105  f.close()
106 
107 
108 # # Merge data
109 #
110 # @param inputFiles - input file names list
111 # @param result - result data
112 def mergeData(inputFiles):
113  # variable for result
114  ret = None
115  if isinstance(inputFiles, list):
116  objs = []
117  for fileName in inputFiles:
118  obj = readFile(fileName)
119  if obj is not None:
120  objs.append(obj)
121 
122  if len(objs) > 0:
123  ret = objs[0]
124 
125  for index, obj in enumerate(objs, 1):
126  ret['itemsList'][0]['itemObject'].extend(obj['itemsList'][0]['itemObject'])
127 
128  return ret
129 
130 
131 if __name__ == "__main__":
132  try:
133  inputFiles, outputFile = parseArguments()
134  result = mergeData(inputFiles)
135  outputResultData(outputFile, result)
136  except Exception as err:
137  sys.stderr.write(str(err) + '\n' + Utils.getTracebackInfo() + '\n')
138  exitCode = APP_CONSTS.EXIT_FAILURE
139  except:
140  exitCode = APP_CONSTS.EXIT_FAILURE
141 
142  sys.stdout.flush()
143  os._exit(exitCode)
def outputResultData(outputFile, result)
Definition: join.py:98
def parseArguments()
Definition: join.py:37
def readFile(fileName)
Definition: join.py:79
def mergeData(inputFiles)
Definition: join.py:112