On Thu, Mar 5, 2015 at 3:49 PM, Vincent Guittot
<vincent.guit...@linaro.org> wrote:
> On 19 November 2014 at 14:14, pi-cheng.chen <pi-cheng.c...@linaro.org> wrote:
>> This scripts strips all comments in the input JSON file, modifies the
>> parameters according to commnad line arguments, and write the content to a 
>> new
>> JSON file.
>>
>> Signed-off-by: Pi-Cheng Chen <pi-cheng.c...@linaro.org>
>> ---
>>  doc/tune_json.py | 138 
>> +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>  1 file changed, 138 insertions(+)
>>  create mode 100755 doc/tune_json.py
>>
>> diff --git a/doc/tune_json.py b/doc/tune_json.py
>> new file mode 100755
>> index 0000000..36746ff
>> --- /dev/null
>> +++ b/doc/tune_json.py
>> @@ -0,0 +1,138 @@
>> +#!/usr/bin/env python
>> +
>> +import collections
>> +import argparse
>> +import shutil
>> +import os
>> +import sys
>> +import json
>> +import re
>> +
>> +
>> +def find_dict_by_key(doc, key):
>> +    if key in doc and type(doc[key]) is collections.OrderedDict:
>> +        return doc[key]
>> +
>> +    for k in doc:
>> +        if type(doc[k]) is collections.OrderedDict:
>> +            return find_dict_by_key(doc[k], key)
>> +
>> +
>> +def dict_find_and_replace_value(dic, key, val):
>> +    for k in dic:
>> +        if type(dic[k]) is collections.OrderedDict:
>> +            dict_find_and_replace_value(dic[k], key, val)
>> +        if k == key:
>> +            dic[k] = val
>> +
>> +
>> +def dict_of_loading(dic):
>> +    if not 'run' in dic:
>> +        return False, None
>> +
>> +    for k in dic:
>> +        if 'timer' in k and 'period' in dic[k]:
>> +            return True, k
>> +    else:
>> +        return False, None
>> +
>> +
>> +def calculate_and_update_loading(dic, loading):
>> +    of_loading, timer_id = dict_of_loading(dic)
>> +
>> +    if of_loading:
>> +        period = dic[timer_id]['period']
>> +        run = period * loading / 100
>> +        dic['run'] = run
>> +
>> +    for k in dic:
>> +        if type(dic[k]) is collections.OrderedDict:
>> +            calculate_and_update_loading(dic[k], loading)
>> +
>> +
>> +# strip comments in json file and load the file as a dict
>> +def load_json_file(filename):
>> +    try:
>> +        f = open(filename, 'r')
>> +    except:
>> +        print 'ERROR: Unable to open %s' %filename
>> +        sys.exit(2)
>> +
>> +    comment_re = re.compile(
>> +        '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
>> +        re.DOTALL | re.MULTILINE)
>> +
>> +    content = ''.join(f.readlines())
>> +
>> +    match = comment_re.search(content)
>> +    while match:
>> +        content = content[:match.start()] + content[match.end():]
>> +        match = comment_re.search(content)
>> +
>> +    return 
>> json.JSONDecoder(object_pairs_hook=collections.OrderedDict).decode(content)
>> +
>> +
>> +def dump_json_file(doc, filename):
>> +    try:
>> +        fo = open(filename, 'w+')
>> +    except:
>> +        print 'ERROR: Unable to open %s' %filename
>> +        sys.exit(2)
>> +
>> +    json.dump(doc, fo, indent=4, sort_keys=False)
>> +    fo.close()
>> +
>> +
>> +if __name__ == '__main__':
>> +    tmp = 'tmp.json'
>
> your tmp file should be a bit more unique than tmp.json. I can imagine
> that the user of tune_json.py already has a tmp.json in the dir which
> will be overwritten by the script.
> You could add something like the current time stamp in the temporary
> file name to reduce the probability to overwrite a user's file

Or actually use the python standard library functions to generate
unique temporary files:
https://docs.python.org/2/library/tempfile.html

If you care about referencing the file externally, NamedTemporaryFile
might be the right method to use.

_______________________________________________
linaro-dev mailing list
linaro-dev@lists.linaro.org
http://lists.linaro.org/mailman/listinfo/linaro-dev

Reply via email to