props.html


001| '''\
001| Arguments Processors.
001| 
001| Processes input arguments from the command line, or from a file, and
001| create a dictionary of the name/value pairs. The processor supports the use of
001| multiple arguments with the same name; the result is a list of values in the
001| order encountered in the input. The processor also supports a special native
001| argument with a filename value for importing additional argument files.
001| 
001| Basic argument format: name(value)
001| 
001| * Any number of spaces (including zero) are allowed between arguments.
001| * Any number of spaces (including zero) are allowed between 'name' and '('.
001| * The value consists of ALL text inside the parenthesis, INCLUDING any leading or trailing spaces.
001| 
001| Alternate formats for file use only:
001|     name: value
001|     name = value
001| 
001| * No space is allowed between 'name' and ':'.
001| * Any number of spaces (including zero) are allowed after the ':'.
001| * Any number of spaces (including zero) are allowed around the '='.
001| * In both cases, 'value' begins with the first non-space.
001| * In both cases, 'value' ends with the line (trailing spaces are removed).
001| * In both cases, a final backslash escapes 'value' termination.
001| * If 'value' extends beyond one line, leading spaces are kept on lines 2+.
001| * The final line in a multi-line 'value' has trailing spaces removed.
001| 
001| Developer@Sonnack.com
001| February 2014
001| '''

002| ####################################################################################################
003| from sys import argv
004| from os import path
005| from lexer import SourceText, SourceFile, Lexer, LexerLine
006| from properties import ApplicationProperties, ObjectProperties, QueryProperties
007| ####################################################################################################
008| BasePath = r'C:\CJS\prj\Python\ws\@sandbox'
009| DataPath = path.join(BasePath, 'dat')
010| 
011| File1 = 'props-1a.in'
012| File2 = 'props-1b.in'
013| File3 = 'props-2a.in'
014| File4 = 'props-2b.in'
015| 
016| ##------------------------------------------------------------------------------------------------##
017| Usage = '''
017| usage:
017|     args.main(command, inputs-list)
017| or:
017|     PYTHON args.py command [inputs]
017| 
017| command:
017|     main    - default operation
017|     test    - test operation
017| 
017|     tst-S1  - test Source
017|     tst-S2  - test Source (no newlines)
017|     tst-L1  - test Lexer
017|     tst-L2  - test Lexer (no newlines)
017|     tst-L3  - test Lexer
017|     tst-P1  - test Application Properties
017|     tst-P2  - test Object Properties
017|     tst-P3  - test Query Properties
017|     tst-all - all above tests
017| 
017|     list    - list inputs
017| '''

018| 
019| 
020| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
021| 
022| ##================================================================================================##
023| def test_datum_1 (*args):
024|     print('test/datum: %d' % len(args))
025|     ss = args[0] if 0 < len(args) else TestStrings
026|     for s in ss:
027|         dat = Datum(s)
028|         print(dat)
029|     return 'Done!'
030| ##------------------------------------------------------------------------------------------------##
031| TestStrings = [
032|     '', '0', '42', '3.1415', '11730',
033|     '52pickup', '-3', '+345.52', '-67.002', '4C',
034|     '27.50USD', '11:11', '12/31/2013',
035|     '+', '-'
036| ]
037| 
038| ##================================================================================================##
039| def test_src_nl (filename=File1):
040|     '''Test: SourceFile class.'''
041|     src = SourceFile(path.join(DataPath,filename))
042|     toks = []
043|     try:
044|         tok = src.get_char()
045|         while tok:
046|             toks.append('<nl>' if tok == SourceText.EOL else tok)
047|             tok = src.get_char()
048|     except IndexError:
049|         pass
050|     print()
051|     print(repr(src))
052|     print(src)
053|     print(toks)
054|     return 'Done!'
055| 
056| ##================================================================================================##
057| def test_src_nonl (filename=File3):
058|     '''Test: SourceFile class; ignore newlines.'''
059|     src = SourceFile(path.join(DataPath,filename))
060|     src.nl_lf = False
061|     src.nl_cr = False
062|     src.nl_ff = False
063|     toks = []
064|     try:
065|         tok = src.get_char()
066|         while tok:
067|             toks.append(tok)
068|             tok = src.get_char()
069|     except IndexError:
070|         pass
071|     print()
072|     print(repr(src))
073|     print(src)
074|     print(toks)
075|     return 'Done!'
076| 
077| ##================================================================================================##
078| def test_lex_spcl (filename=File1):
079|     '''Test: Lexer class with name=value properties.'''
080|     lex = LexerLine(SourceFile(path.join(DataPath,filename)))
081|     toks = []
082|     try:
083|         tok = lex.get_token()
084|         while tok:
085|             toks.append(tok)
086|             # Next token depends on if we hit a delimiter...
087|             if tok[1] not in [':', '=']:
088|                 tok = lex.get_token()
089|             else:
090|                 # Parameter value, so see if it's quoted...
091|                 ch = lex.skip_whitespace()
092|                 if ch in lex.sdq_chars:
093|                     # If it is, just get the string token...
094|                     tok = lex.get_token()
095|                 else:
096|                     # Otherwise, get the rest of the line...
097|                     tok = lex.get_rest_of_line()
098|     except IndexError:
099|         pass
100|     print()
101|     print(repr(lex))
102|     print(lex)
103|     for tok in toks: print(tok)
104|     return 'Done!'
105| 
106| ##================================================================================================##
107| def test_lex_nl (filename=File2):
108|     '''Test: Lexer class with name(value) properties.'''
109|     lex = Lexer(SourceFile(path.join(DataPath,filename)))
110|     toks = []
111|     try:
112|         tok = lex.get_token()
113|         while tok:
114|             toks.append(tok)
115|             tok = lex.get_token()
116|     except IndexError:
117|         pass
118|     print()
119|     print(repr(lex))
120|     print(lex)
121|     for tok in toks: print(tok)
122|     return 'Done!'
123| 
124| ##================================================================================================##
125| def test_lex_nonl (filename=File3):
126|     '''Test: Lexer class with name(value) properties.'''
127|     lex = Lexer(SourceFile(path.join(DataPath,filename)))
128|     lex.nl_flag = False
129|     toks = []
130|     try:
131|         tok = lex.get_token()
132|         while tok:
133|             toks.append(tok)
134|             tok = lex.get_token()
135|     except IndexError:
136|         pass
137|     print()
138|     print(repr(lex))
139|     print(lex)
140|     for tok in toks: print(tok)
141|     return 'Done!'
142| 
143| ##================================================================================================##
144| def test_app_props (filename=File1):
145|     '''Test: Application Properties class.'''
146|     props = ApplicationProperties(path.join(DataPath,filename))
147|     print()
148|     print(repr(props))
149|     print(props.src)
150|     print(props)
151|     return 'Done!'
152| 
153| ##================================================================================================##
154| def test_obj_props (filename=File2):
155|     '''Test: Object Properties class.'''
156|     props = ObjectProperties(path.join(DataPath,filename))
157|     print()
158|     print(repr(props))
159|     print(props.src)
160|     print(props)
161|     return 'Done!'
162| 
163| ##================================================================================================##
164| def test_qry_props (filename=File3):
165|     '''Test: Query Properties class.'''
166|     props = QueryProperties(path.join(DataPath,filename))
167|     print()
168|     print(repr(props))
169|     print(props.src)
170|     print(props)
171|     return 'Done!'
172| 
173| 
174| ##================================================================================================##
175| def dispatch (command, *args):
176|     print('command: %s' % command)
177|     print('args: %d' % len(args))
178| 
179|     if command == 'test':
180|         for ix,fn in enumerate(args):
181|             print('filename[%d]: %s' % (ix+1, fn))
182|             obj = QueryProperties(path.join(BasePath,fn))
183|             print(obj.src)
184|             print(obj)
185|         return 'Done!'
186| 
187|     if command == 'tst-all':
188|         test_src_nl()
189|         test_src_nonl()
190|         test_lex_spcl()
191|         test_lex_nl()
192|         test_lex_nonl()
193|         test_app_props()
194|         test_obj_props()
195|         test_qry_props()
196|         return 'Done!'
197| 
198|     if command == 'tst-D1': return test_datum_1()
199| 
200|     if command == 'tst-S1': return test_src_nl()
201|     if command == 'tst-S2': return test_src_nonl()
202| 
203|     if command == 'tst-L1': return test_lex_spcl()
204|     if command == 'tst-L2': return test_lex_nl()
205|     if command == 'tst-L3': return test_lex_nonl()
206| 
207|     if command == 'tst-P1': return test_app_props()
208|     if command == 'tst-P2': return test_obj_props()
209|     if command == 'tst-P3': return test_qry_props()
210| 
211|     if command == 'main':
212|         g = [test_lex_nl(fn) for fn in args]
213|         return 'Done!'
214| 
215|     if command == 'list':
216|         for ix,a in enumerate(args):
217|             print('arg[%d] = "%s"' % (1+ix, a))
218|         return 'Done!'
219| 
220|     if (command == '?') or (command == 'help'):
221|         print(Usage)
222|         return 'Done!'
223| 
224|     return 'Nothing to do.'
225| 
226| ####################################################################################################
227| if __name__ == '__main__':
228|     print('autorun: %s' % argv[0])
229|     cmd = argv[1]  if 1 < len(argv) else 'tst-L3'
230|     etc = argv[2:] if 2 < len(argv) else [File1, File2, File3, File4]
231|     ret = dispatch(cmd, *etc)
232|     print(ret)
233| ####################################################################################################
234| '''eof'''
235|