def load_document(self, path): """ Check document exists and assign attribute values """ if os.path.isfile(path): document_repo_path = '%s/documents/' % Utility.get_repo_dir() self._file_name = os.path.basename(path) self._file_path = os.path.abspath(path) self._pretty_name = path.replace(document_repo_path, '') else: raise Exception('can not find document "%s"' % path)
def make_link_list(self, root_directory, nice_attribute_name, link_list_string, multiple_allowed=True): """ Returns list(array) of link list string and checks that each link exists as a file on disk """ # If value None, then we are done:) if link_list_string == None or link_list_string == "none": return None link_list_string = link_list_string.strip() if root_directory == "requirements": file_extension = ".req" elif root_directory == "documents": # Any extension is permissable file_extension = "" elif root_directory == "stakeholders": file_extension = ".sth" elif root_directory == "glossary": file_extension = ".def" else: Utility.report_error( 1, '%s: Field "%s" has unknown link list type "%s"' % (self._file_path, nice_attribute_name, root_directory), ) # If multiple links not allowed, but there are more than one (i.e. a comma) if multiple_allowed == False and link_list_string.find(",") > 0: Utility.report_error( 1, '%s: Field "%s" may only contain one link (content "%s")' % (self._file_path, nice_attribute_name, link_list_string), ) link_list_string = self.parse_link_list_string(link_list_string) link_list = [] if link_list_string: for link in link_list_string: full_file_path = os.path.join(Utility.get_repo_dir(), os.path.join(root_directory, link)) if os.path.isdir(full_file_path) == False: full_file_path += file_extension if os.path.isfile(full_file_path) == False and os.path.isdir(full_file_path) == False: Utility.report_error( 1, '%s: "%s" link "%s" is broken (%s)' % (self._file_path, nice_attribute_name, link, full_file_path), ) link_list.append(full_file_path) return link_list
def load_config_from_file(self, file_name): """ Read config file and pass key values on for assignment """ self._file_name = os.path.basename(file_name) self._file_path = os.path.abspath(file_name) self._relative_file_path = self._file_path.replace( os.path.join(Utility.get_repo_dir(), "requirements" + os.sep), "" ) if file_name.endswith(self._valid_file_extension) == False: Utility.report_error( 1, '%s: Invalid file extension, must be ".%s"' % (self._file_path, self._valid_file_extension) ) return self.set_name_and_id(file_name) try: conf_file = open(file_name, "r") try: lines = conf_file.readlines() i = 0 max_lines = len(lines) - 1 # Process file line by line and extract key value pairs, note # that values may span multiple lines if preceeded by '^[key]:$' and # indented by two spaces while True: key = "" value = "" # Break if no lines left if i > max_lines: break # Skip comments if lines[i].lstrip().startswith("#"): i += 1 continue # Skip lines that only contain whitespace elif lines[i].strip() == "": i += 1 continue # Multi line settings elif lines[i].strip().endswith(":"): key = lines[i] key = key.strip() key = key.rstrip(":") n = i + 1 multi_line_value = "" # Collect following lines that start with double space while True: if n == max_lines and lines[n].startswith(" "): value = multi_line_value + lines[n].lstrip(" ") i = max_lines + 1 # this was last line, so next loop will abort break elif n > max_lines: i = n break elif lines[n].startswith(" "): multi_line_value += lines[n].lstrip(" ") n += 1 # If no longer multi line set index one back and continue loop else: value = multi_line_value i = n break else: setting = lines[i].partition(":") if setting[0] != "" and setting[1] != "" and setting[2] != "": key = setting[0] value = setting[2] i += 1 else: # Something strange happend, reset counter 1 and exit with error error_line_number = i + 1 Utility.report_error( 1, '%s: Unable to parse line %s "%s"' % (self._file_path, error_line_number, lines[i]) ) # Tidy the key and values key = key.strip() key = key.lower() key = key.replace(" ", "_") value = value.strip() # print 'i: %s, Key: "%s", Value: "%s"' % (i, key, value) self.assign_attribute(key, value) finally: conf_file.close() except IOError: Utility.report_error(1, "%s: Unable to read file" % self._file_path) self.validate_settings()
# You should have received a copy of the GNU General Public License # along with Reqy. If not, see <http://www.gnu.org/licenses/>. import Utility from ProjectConfig import * import os import gettext import locale import sys def init_localization(): '''prepare l10n''' locale_str = locale.getdefaultlocale()[0] l10n_file = "%s/requirements/l10n/%s.mo" % (sys.path[0], locale_str) try: trans = gettext.GNUTranslations(open( l10n_file, "rb" ) ) except IOError: if locale_str != 'en_US': Utility.report_warning('Unable to find translation for %s, using default (en_US)' % (locale_str)) trans = gettext.NullTranslations() trans.install(unicode=1) init_localization() project = ProjectConfig() project.load_config_from_file(os.path.join(Utility.get_repo_dir(), 'project.conf'))