44
55"""Visual Studio project reader/writer."""
66
7- import gyp .common
87import gyp .easy_xml as easy_xml
98
10- #------------------------------------------------------------------------------
9+ # ------------------------------------------------------------------------------
1110
1211
1312class Tool (object ):
14- """Visual Studio tool."""
13+ """Visual Studio tool."""
1514
16- def __init__ (self , name , attrs = None ):
17- """Initializes the tool.
15+ def __init__ (self , name , attrs = None ):
16+ """Initializes the tool.
1817
1918 Args:
2019 name: Tool name.
2120 attrs: Dict of tool attributes; may be None.
2221 """
23- self ._attrs = attrs or {}
24- self ._attrs [' Name' ] = name
22+ self ._attrs = attrs or {}
23+ self ._attrs [" Name" ] = name
2524
26- def _GetSpecification (self ):
27- """Creates an element for the tool.
25+ def _GetSpecification (self ):
26+ """Creates an element for the tool.
2827
2928 Returns:
3029 A new xml.dom.Element for the tool.
3130 """
32- return ['Tool' , self ._attrs ]
31+ return ["Tool" , self ._attrs ]
32+
3333
3434class Filter (object ):
35- """Visual Studio filter - that is, a virtual folder."""
35+ """Visual Studio filter - that is, a virtual folder."""
3636
37- def __init__ (self , name , contents = None ):
38- """Initializes the folder.
37+ def __init__ (self , name , contents = None ):
38+ """Initializes the folder.
3939
4040 Args:
4141 name: Filter (folder) name.
4242 contents: List of filenames and/or Filter objects contained.
4343 """
44- self .name = name
45- self .contents = list (contents or [])
44+ self .name = name
45+ self .contents = list (contents or [])
4646
4747
48- #------------------------------------------------------------------------------
48+ # ------------------------------------------------------------------------------
4949
5050
5151class Writer (object ):
52- """Visual Studio XML project writer."""
52+ """Visual Studio XML project writer."""
5353
54- def __init__ (self , project_path , version , name , guid = None , platforms = None ):
55- """Initializes the project.
54+ def __init__ (self , project_path , version , name , guid = None , platforms = None ):
55+ """Initializes the project.
5656
5757 Args:
5858 project_path: Path to the project file.
@@ -61,36 +61,36 @@ def __init__(self, project_path, version, name, guid=None, platforms=None):
6161 guid: GUID to use for project, if not None.
6262 platforms: Array of string, the supported platforms. If null, ['Win32']
6363 """
64- self .project_path = project_path
65- self .version = version
66- self .name = name
67- self .guid = guid
64+ self .project_path = project_path
65+ self .version = version
66+ self .name = name
67+ self .guid = guid
6868
69- # Default to Win32 for platforms.
70- if not platforms :
71- platforms = [' Win32' ]
69+ # Default to Win32 for platforms.
70+ if not platforms :
71+ platforms = [" Win32" ]
7272
73- # Initialize the specifications of the various sections.
74- self .platform_section = [' Platforms' ]
75- for platform in platforms :
76- self .platform_section .append ([' Platform' , {' Name' : platform }])
77- self .tool_files_section = [' ToolFiles' ]
78- self .configurations_section = [' Configurations' ]
79- self .files_section = [' Files' ]
73+ # Initialize the specifications of the various sections.
74+ self .platform_section = [" Platforms" ]
75+ for platform in platforms :
76+ self .platform_section .append ([" Platform" , {" Name" : platform }])
77+ self .tool_files_section = [" ToolFiles" ]
78+ self .configurations_section = [" Configurations" ]
79+ self .files_section = [" Files" ]
8080
81- # Keep a dict keyed on filename to speed up access.
82- self .files_dict = dict ()
81+ # Keep a dict keyed on filename to speed up access.
82+ self .files_dict = dict ()
8383
84- def AddToolFile (self , path ):
85- """Adds a tool file to the project.
84+ def AddToolFile (self , path ):
85+ """Adds a tool file to the project.
8686
8787 Args:
8888 path: Relative path from project to tool file.
8989 """
90- self .tool_files_section .append ([' ToolFile' , {' RelativePath' : path }])
90+ self .tool_files_section .append ([" ToolFile" , {" RelativePath" : path }])
9191
92- def _GetSpecForConfiguration (self , config_type , config_name , attrs , tools ):
93- """Returns the specification for a configuration.
92+ def _GetSpecForConfiguration (self , config_type , config_name , attrs , tools ):
93+ """Returns the specification for a configuration.
9494
9595 Args:
9696 config_type: Type of configuration node.
@@ -99,58 +99,57 @@ def _GetSpecForConfiguration(self, config_type, config_name, attrs, tools):
9999 tools: List of tools (strings or Tool objects); may be None.
100100 Returns:
101101 """
102- # Handle defaults
103- if not attrs :
104- attrs = {}
105- if not tools :
106- tools = []
107-
108- # Add configuration node and its attributes
109- node_attrs = attrs .copy ()
110- node_attrs ['Name' ] = config_name
111- specification = [config_type , node_attrs ]
112-
113- # Add tool nodes and their attributes
114- if tools :
115- for t in tools :
116- if isinstance (t , Tool ):
117- specification .append (t ._GetSpecification ())
118- else :
119- specification .append (Tool (t )._GetSpecification ())
120- return specification
121-
122-
123- def AddConfig (self , name , attrs = None , tools = None ):
124- """Adds a configuration to the project.
102+ # Handle defaults
103+ if not attrs :
104+ attrs = {}
105+ if not tools :
106+ tools = []
107+
108+ # Add configuration node and its attributes
109+ node_attrs = attrs .copy ()
110+ node_attrs ["Name" ] = config_name
111+ specification = [config_type , node_attrs ]
112+
113+ # Add tool nodes and their attributes
114+ if tools :
115+ for t in tools :
116+ if isinstance (t , Tool ):
117+ specification .append (t ._GetSpecification ())
118+ else :
119+ specification .append (Tool (t )._GetSpecification ())
120+ return specification
121+
122+ def AddConfig (self , name , attrs = None , tools = None ):
123+ """Adds a configuration to the project.
125124
126125 Args:
127126 name: Configuration name.
128127 attrs: Dict of configuration attributes; may be None.
129128 tools: List of tools (strings or Tool objects); may be None.
130129 """
131- spec = self ._GetSpecForConfiguration (' Configuration' , name , attrs , tools )
132- self .configurations_section .append (spec )
130+ spec = self ._GetSpecForConfiguration (" Configuration" , name , attrs , tools )
131+ self .configurations_section .append (spec )
133132
134- def _AddFilesToNode (self , parent , files ):
135- """Adds files and/or filters to the parent node.
133+ def _AddFilesToNode (self , parent , files ):
134+ """Adds files and/or filters to the parent node.
136135
137136 Args:
138137 parent: Destination node
139138 files: A list of Filter objects and/or relative paths to files.
140139
141140 Will call itself recursively, if the files list contains Filter objects.
142141 """
143- for f in files :
144- if isinstance (f , Filter ):
145- node = [' Filter' , {' Name' : f .name }]
146- self ._AddFilesToNode (node , f .contents )
147- else :
148- node = [' File' , {' RelativePath' : f }]
149- self .files_dict [f ] = node
150- parent .append (node )
151-
152- def AddFiles (self , files ):
153- """Adds files to the project.
142+ for f in files :
143+ if isinstance (f , Filter ):
144+ node = [" Filter" , {" Name" : f .name }]
145+ self ._AddFilesToNode (node , f .contents )
146+ else :
147+ node = [" File" , {" RelativePath" : f }]
148+ self .files_dict [f ] = node
149+ parent .append (node )
150+
151+ def AddFiles (self , files ):
152+ """Adds files to the project.
154153
155154 Args:
156155 files: A list of Filter objects and/or relative paths to files.
@@ -159,12 +158,12 @@ def AddFiles(self, files):
159158 later add files to a Filter object which was passed into a previous call
160159 to AddFiles(), it will not be reflected in this project.
161160 """
162- self ._AddFilesToNode (self .files_section , files )
163- # TODO(rspangler) This also doesn't handle adding files to an existing
164- # filter. That is, it doesn't merge the trees.
161+ self ._AddFilesToNode (self .files_section , files )
162+ # TODO(rspangler) This also doesn't handle adding files to an existing
163+ # filter. That is, it doesn't merge the trees.
165164
166- def AddFileConfig (self , path , config , attrs = None , tools = None ):
167- """Adds a configuration to a file.
165+ def AddFileConfig (self , path , config , attrs = None , tools = None ):
166+ """Adds a configuration to a file.
168167
169168 Args:
170169 path: Relative path to the file.
@@ -175,34 +174,33 @@ def AddFileConfig(self, path, config, attrs=None, tools=None):
175174 Raises:
176175 ValueError: Relative path does not match any file added via AddFiles().
177176 """
178- # Find the file node with the right relative path
179- parent = self .files_dict .get (path )
180- if not parent :
181- raise ValueError ('AddFileConfig: file "%s" not in project.' % path )
182-
183- # Add the config to the file node
184- spec = self ._GetSpecForConfiguration ('FileConfiguration' , config , attrs ,
185- tools )
186- parent .append (spec )
187-
188- def WriteIfChanged (self ):
189- """Writes the project file."""
190- # First create XML content definition
191- content = [
192- 'VisualStudioProject' ,
193- {'ProjectType' : 'Visual C++' ,
194- 'Version' : self .version .ProjectVersion (),
195- 'Name' : self .name ,
196- 'ProjectGUID' : self .guid ,
197- 'RootNamespace' : self .name ,
198- 'Keyword' : 'Win32Proj'
199- },
200- self .platform_section ,
201- self .tool_files_section ,
202- self .configurations_section ,
203- ['References' ], # empty section
204- self .files_section ,
205- ['Globals' ] # empty section
206- ]
207- easy_xml .WriteXmlIfChanged (content , self .project_path ,
208- encoding = "Windows-1252" )
177+ # Find the file node with the right relative path
178+ parent = self .files_dict .get (path )
179+ if not parent :
180+ raise ValueError ('AddFileConfig: file "%s" not in project.' % path )
181+
182+ # Add the config to the file node
183+ spec = self ._GetSpecForConfiguration ("FileConfiguration" , config , attrs , tools )
184+ parent .append (spec )
185+
186+ def WriteIfChanged (self ):
187+ """Writes the project file."""
188+ # First create XML content definition
189+ content = [
190+ "VisualStudioProject" ,
191+ {
192+ "ProjectType" : "Visual C++" ,
193+ "Version" : self .version .ProjectVersion (),
194+ "Name" : self .name ,
195+ "ProjectGUID" : self .guid ,
196+ "RootNamespace" : self .name ,
197+ "Keyword" : "Win32Proj" ,
198+ },
199+ self .platform_section ,
200+ self .tool_files_section ,
201+ self .configurations_section ,
202+ ["References" ], # empty section
203+ self .files_section ,
204+ ["Globals" ], # empty section
205+ ]
206+ easy_xml .WriteXmlIfChanged (content , self .project_path , encoding = "Windows-1252" )
0 commit comments