Peano
Loading...
Searching...
No Matches
Helper.py
Go to the documentation of this file.
1# This file is part of the Peano project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3import os
4
5from .Overwrite import Overwrite
6
7WARNING_BANNER = """// **********************************************************************************************
8// *** !!!WARNING!!! ***
9// *** WARNING: AUTO GENERATED FILE! DO NOT MODIFY BY HAND! YOUR CHANGES WILL BE OVERWRITTEN! ***
10// *** !!!WARNING!!! ***
11// *** Generated by Peano's Python API: www.peano-framework.org ***
12// **********************************************************************************************
13"""
14
15def write_file(overwrite,overwrite_is_default,full_qualified_filename):
16 """
17 overwrite is of type Overwrite and is what the user passes in
18 overwrite_is_default is a boolean
19 """
20 if overwrite==Overwrite.Always:
21 return True
22 elif overwrite==Overwrite.Never and os.path.exists(full_qualified_filename):
23 return False
24 elif overwrite==Overwrite.Never and not os.path.exists(full_qualified_filename):
25 return True
26 elif overwrite==Overwrite.Default:
27 if not overwrite_is_default and os.path.exists(full_qualified_filename):
28 return False
29 else:
30 return True
31 else:
32 print("Error: undefined overwrite strategy")
33 return True
34
35
36# Default for the longest class name of a generated action set. The class
37# name is also the file name (plus .h or .cpp), and file names are limited to
38# 255 characters on most file systems.
39MAX_CLASS_NAME_LENGTH = 200
40
41# Long class names handed out so far, mapped onto their shortened counterparts
42_shortened_class_names = {}
43
44
45def shortened_class_name(class_name, max_length=MAX_CLASS_NAME_LENGTH):
46 """!
47
48 Class name of a generated observer or action set which is short enough
49 to serve as file name
50
51 Names up to max_length characters are returned unchanged. Longer names
52 are cut off and get a consecutive number appended, e.g. ..._1, ..._2.
53 The cut-off names carry no meaning anymore, but every call with the same
54 long name and the same max_length yields the same short name, so the
55 generated files can refer to each other.
56
57 @param class_name: str
58 The full class name.
59
60 @param max_length: int
61 Longest admissible class name.
62
63 """
64 if len(class_name) <= max_length:
65 return class_name
66 key = (class_name, max_length)
67 if key not in _shortened_class_names:
68 suffix = "_" + str(len(_shortened_class_names) + 1)
69 _shortened_class_names[key] = class_name[: max_length - len(suffix)] + suffix
70 return _shortened_class_names[key]