summaryrefslogtreecommitdiff
path: root/scripts/ini_editor.py
blob: aeeaefbd1f1e211638df3ec95924780eb74e8029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#!/usr/bin/python
#
# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
A script to modify dsp.ini config files.
A dsp.ini config file is represented by an Ini object.
An Ini object contains one or more Sections.
Each Section has a name, a list of Ports, and a list of NonPorts.
"""

import argparse
import logging
import os
import re
import StringIO
import sys
from collections import namedtuple

Parameter = namedtuple('Parameter', ['value', 'comment'])


class Port(object):
  """Class for port definition in ini file.

  Properties:
    io: "input" or "output".
    index: an integer for port index.
    definition: a string for the content after "=" in port definition line.
    parameter: a Parameter namedtuple which is parsed from definition.
  """
  @staticmethod
  def ParsePortLine(line):
    """Parses a port definition line in ini file and init a Port object.

    Args:
      line: A string possibly containing port definition line like
        "input_0=1;  something".

    Returns:
      A Port object if input is a valid port definition line. Returns
      None if input is not a valid port definition line.
    """
    result = re.match(r'(input|output)_(\d+)=(.*)', line)
    if result:
      parse_values = result.groups()
      io = parse_values[0]
      index = int(parse_values[1])
      definition = parse_values[2]
      return Port(io, index, definition)
    else:
      return None

  def __init__(self, io, index, definition):
    """Initializes a port.

    Initializes a port with io, index and definition. The definition will be
    further parsed to Parameter(value, comment) if the format matches
    "<some value> ; <some comment>".

    Args:
      io: "input" or "output".
      index: an integer for port index.
      definition: a string for the content after "=" in port definition line.
    """
    self.io = io
    self.index = index
    self.definition = definition
    result = re.match(r'(\S+)\s+; (.+)', definition)
    if result:
      self.parameter = Parameter._make(result.groups())
    else:
      self.parameter = None

  def FormatLine(self):
    """Returns a port definition line which is used in ini file."""
    line = '%s_%d=' % (self.io, self.index)
    if self.parameter:
      line +="{:<8}; {:}".format(self.parameter.value, self.parameter.comment)
    else:
      line += self.definition
    return line

  def _UpdateIndex(self, index):
    """Updates index of this port.

    Args:
      index: The new index.
    """
    self.index = index


class NonPort(object):
  """Class for non-port definition in ini file.

  Properties:
    name: A string representing the non-port name.
    definition: A string representing the non-port definition.
  """
  @staticmethod
  def ParseNonPortLine(line):
    """Parses a non-port definition line in ini file and init a NonPort object.

    Args:
      line: A string possibly containing non-port definition line like
        "library=builtin".

    Returns:
      A NonPort object if input is a valid non-port definition line. Returns
      None if input is not a valid non-port definition line.
    """
    result = re.match(r'(\w+)=(.*)', line)
    if result:
      parse_values = result.groups()
      name = parse_values[0]
      definition = parse_values[1]
      return NonPort(name, definition)
    else:
      return None

  def __init__(self, name, definition):
    """Initializes a NonPort <name>=<definition>.

    Args:
      name: A string representing the non-port name.
      definition: A string representing the non-port definition.
    """
    self.name = name
    self.definition = definition

  def FormatLine(self):
    """Formats a string representation of a NonPort.

    Returns:
      A string "<name>=<definition>".
    """
    line = '%s=%s' % (self.name, self.definition)
    return line


class SectionException(Exception):
  pass


class Section(object):
  """Class for section definition in ini file.

  Properties:
   name: Section name.
   non_ports: A list containing NonPorts of this section.
   ports: A list containing Ports of this section.
  """
  @staticmethod
  def ParseSectionName(line):
    """Parses a section name.

    Args:
      line: A string possibly containing a section name like [drc].

    Returns:
      Returns parsed section name without '[' and ']' if input matches
      the syntax [<section name>]. Returns None if not.
    """
    result = re.match(r'\[(\w+)\]', line)
    return result.groups()[0] if result else None

  @staticmethod
  def ParseLine(line):
    """Parses a line that belongs to a section.

    Returns:
      A Port or NonPort object if input line matches the format. Returns None
      if input line does not match the format of Port nor NonPort.
    """
    if not line:
      return
    parse_port = Port.ParsePortLine(line)
    if parse_port:
      return parse_port
    parse_non_port = NonPort.ParseNonPortLine(line)
    if parse_non_port:
      return parse_non_port

  def __init__(self, name):
    """Initializes a Section with given name."""
    self.name = name
    self.non_ports= []
    self.ports = []

  def AddLine(self, line):
    """Adds a line to this Section.

    Args:
      line: A line to be added to this section. If it matches port or non-port
      format, a Port or NonPort will be added to this section. Otherwise,
      this line is ignored.
    """
    to_add = Section.ParseLine(line)
    if not to_add:
      return
    if isinstance(to_add, Port):
      self.AppendPort(to_add)
      return
    if isinstance(to_add, NonPort):
      self.AppendNonPort(to_add)
      return

  def AppendNonPort(self, non_port):
    """Appends a NonPort to non_ports.

    Args:
      non_port: A NonPort object to be appended.
    """
    self.non_ports.append(non_port)

  def AppendPort(self, port):
    """Appends a Port to ports.

    Args:
      port: A Port object to be appended. The port should be appended
      in the order of index, so the index of port should equal to the current
      size of ports list.

    Raises:
      SectionException if the index of port is not the current size of ports
      list.
    """
    if not port.index == len(self.ports):
      raise SectionException(
          'The port with index %r can not be appended to the end of ports'
          ' of size' % (port.index, len(self.ports)))
    else:
      self.ports.append(port)

  def InsertLine(self, line):
    """Inserts a line to this section.

    Inserts a line containing port or non-port definition to this section.
    If input line matches Port or NonPort format, the corresponding insert
    method InsertNonPort or InsertPort will be called. If input line does not
    match the format, SectionException will be raised.

    Args:
      line: A line to be inserted. The line should

    Raises:
      SectionException if input line does not match the format of Port or
      NonPort.
    """
    to_insert = Section.ParseLine(line)
    if not to_insert:
      raise SectionException(
          'The line %s does not match Port or NonPort syntax' % line)
    if isinstance(to_insert, Port):
      self.InsertPort(to_insert)
      return
    if isinstance(to_insert, NonPort):
      self.InsertNonPort(to_insert)
      return

  def InsertNonPort(self, non_port):
    """Inserts a NonPort to non_ports list.

    Currently there is no ordering for non-port definition. This method just
    appends non_port to non_ports list.

    Args:
      non_port: A NonPort object.
    """
    self.non_ports.append(non_port)

  def InsertPort(self, port):
    """Inserts a Port to ports list.

    The index of port should not be greater than the current size of ports.
    After insertion, the index of each port in ports should be updated to the
    new index of that port in the ports list.
    E.g. Before insertion:
    self.ports=[Port("input", 0, "foo0"),
                Port("input", 1, "foo1"),
                Port("output", 2, "foo2")]
    Now we insert a Port with index 1 by invoking
    InsertPort(Port("output, 1, "bar")),
    Then,
    self.ports=[Port("input", 0, "foo0"),
                Port("output, 1, "bar"),
                Port("input", 2, "foo1"),
                Port("output", 3, "foo2")].
    Note that the indices of foo1 and foo2 had been shifted by one because a
    new port was inserted at index 1.

    Args:
      port: A Port object.

    Raises:
      SectionException: If the port to be inserted does not have a valid index.
    """
    if port.index > len(self.ports):
      raise SectionException('Inserting port index %d but'
          ' currently there are only %d ports' % (port.index,
              len(self.ports)))

    self.ports.insert(port.index, port)
    self._UpdatePorts()

  def _UpdatePorts(self):
    """Updates the index property of each Port in ports.

    Updates the index property of each Port in ports so the new index property
    is the index of that Port in ports list.
    """
    for index, port in enumerate(self.ports):
      port._UpdateIndex(index)

  def Print(self, output):
    """Prints the section definition to output.

    The format is:
    [section_name]
    non_port_name_0=non_port_definition_0
    non_port_name_1=non_port_definition_1
    ...
    port_name_0=port_definition_0
    port_name_1=port_definition_1
    ...

    Args:
      output: A StringIO.StringIO object.
    """
    output.write('[%s]\n' % self.name)
    for non_port in self.non_ports:
      output.write('%s\n' % non_port.FormatLine())
    for port in self.ports:
      output.write('%s\n' % port.FormatLine())


class Ini(object):
  """Class for an ini config file.

  Properties:
    sections: A dict containing mapping from section name to Section.
    section_names: A list of section names.
    file_path: The path of this ini config file.
  """
  def __init__(self, input_file):
    """Initializes an Ini object from input config file.

    Args:
      input_file: The path to an ini config file.
    """
    self.sections = {}
    self.section_names = []
    self.file_path = input_file
    self._ParseFromFile(input_file)

  def _ParseFromFile(self, input_file):
    """Parses sections in the input config file.

    Reads in the content of the input config file and parses each sections.
    The parsed sections are stored in sections dict.
    The names of each section is stored in section_names list.

    Args:
      input_file: The path to an ini config file.
    """
    content = open(input_file, 'r').read()
    content_lines = content.splitlines()
    self.sections = {}
    self.section_names = []
    current_name = None
    for line in content_lines:
      name = Section.ParseSectionName(line)
      if name:
        self.section_names.append(name)
        self.sections[name] = Section(name)
        current_name = name
      else:
        self.sections[current_name].AddLine(line)

  def Print(self, output_file=None):
    """Prints all sections of this Ini object.

    Args:
      output_file: The path to write output. If this is not None, writes the
        output to this path. Otherwise, just print the output to console.

    Returns:
      A StringIO.StringIO object containing output.
    """
    output = StringIO.StringIO()
    for index, name in enumerate(self.section_names):
      self.sections[name].Print(output)
      if index < len(self.section_names) - 1:
        output.write('\n')
    if output_file:
      with open(output_file, 'w') as f:
        f.write(output.getvalue())
        output.close()
    else:
      print output.getvalue()
    return output

  def HasSection(self, name):
    """Checks if this Ini object has a section with certain name.

    Args:
      name: The name of the section.
    """
    return name in self.sections

  def PrintSection(self, name):
    """Prints a section to console.

    Args:
      name: The name of the section.

    Returns:
      A StringIO.StringIO object containing output.
    """
    output = StringIO.StringIO()
    self.sections[name].Print(output)
    output.write('\n')
    print output.getvalue()
    return output

  def InsertLineToSection(self, name, line):
    """Inserts a line to a section.

    Args:
      name: The name of the section.
      line: A line to be inserted.
    """
    self.sections[name].InsertLine(line)


def prompt(question, binary_answer=True):
  """Displays the question to the user and wait for input.

  Args:
    question: The question to be displayed to user.
    binary_answer: True to expect an yes/no answer from user.
  Returns:
    True/False if binary_answer is True. Otherwise, returns a string
    containing user input to the question.
  """

  sys.stdout.write(question)
  answer = raw_input()
  if binary_answer:
    answer = answer.lower()
    if answer in ['y', 'yes']:
      return True
    elif answer in ['n', 'no']:
      return False
    else:
      return prompt(question)
  else:
    return answer


class IniEditorException(Exception):
  pass


class IniEditor(object):
  """The class for ini file editing command line interface.

  Properties:
    input_files: The files to be edited. Note that the same editing command
      can be applied on many config files.
    args: The result of ArgumentParser.parse_args method. It is an object
      containing args as attributes.
  """
  def __init__(self):
    self.input_files = []
    self.args = None

  def Main(self):
    """The main method of IniEditor.

    Parses the arguments and processes files according to the arguments.
    """
    self.ParseArgs()
    self.ProcessFiles()

  def ParseArgs(self):
    """Parses the arguments from command line.

    Parses the arguments from command line to determine input_files.
    Also, checks the arguments are valid.

    Raises:
      IniEditorException if arguments are not valid.
    """
    parser = argparse.ArgumentParser(
        description=('Edit or show the config files'))
    parser.add_argument('--input_file', '-i', default=None,
                        help='Use the specified file as input file. If this '
                             'is not given, the editor will try to find config '
                             'files using config_dirs and board.')
    parser.add_argument('--config_dirs', '-c',
                        default='~/trunk/src/third_party/adhd/cras-config',
                        help='Config directory. By default it is '
                             '~/trunk/src/third_party/adhd/cras-config.')
    parser.add_argument('--board', '-b', default=None, nargs='*',
                        help='The boards to apply the changes. Use "all" '
                             'to apply on all boards. '
                             'Use --board <board_1> <board_2> to specify more '
                             'than one boards')
    parser.add_argument('--section', '-s', default=None,
                        help='The section to be shown/edited in the ini file.')
    parser.add_argument('--insert', '-n', default=None,
                        help='The line to be inserted into the ini file. '
                             'Must be used with --section.')
    parser.add_argument('--output-suffix', '-o', default='.new',
                        help='The output file suffix. Set it to "None" if you '
                             'want to apply the changes in-place.')
    self.args = parser.parse_args()

    # If input file is given, just edit this file.
    if self.args.input_file:
      self.input_files.append(self.args.input_file)
    # Otherwise, try to find config files in board directories of config
    # directory.
    else:
      if self.args.config_dirs.startswith('~'):
        self.args.config_dirs = os.path.join(
            os.path.expanduser('~'),
            self.args.config_dirs.split('~/')[1])
      all_boards = os.walk(self.args.config_dirs).next()[1]
      # "board" argument must be a valid board name or "all".
      if (not self.args.board or
          (self.args.board != ['all'] and
           not set(self.args.board).issubset(set(all_boards)))):
        logging.error('Please select a board from %s or use "all".' % (
            ', '.join(all_boards)))
        raise IniEditorException('User must specify board if input_file '
                                 'is not given.')
      if self.args.board == ['all']:
        logging.info('Applying on all boards.')
        boards = all_boards
      else:
        boards = self.args.board

      self.input_files = []
      # Finds dsp.ini files in candidate boards directories.
      for board in boards:
        ini_file = os.path.join(self.args.config_dirs, board, 'dsp.ini')
        if os.path.exists(ini_file):
          self.input_files.append(ini_file)

    if self.args.insert and not self.args.section:
      raise IniEditorException('--insert must be used with --section')

  def ProcessFiles(self):
    """Processes the config files in input_files.

    Showes or edits every selected config file.
    """
    for input_file in self.input_files:
      logging.info('Looking at dsp.ini file at %s', input_file)
      ini = Ini(input_file)
      if self.args.insert:
        self.InsertCommand(ini)
      else:
        self.PrintCommand(ini)

  def PrintCommand(self, ini):
    """Prints this Ini object.

    Prints all sections or a section in input Ini object if
    args.section is specified and there is such section in this Ini object.

    Args:
      ini: An Ini object.
    """
    if self.args.section:
      if ini.HasSection(self.args.section):
        logging.info('Printing section %s.', self.args.section)
        ini.PrintSection(self.args.section)
      else:
        logging.info('There is no section %s in %s',
                     self.args.section, ini.file_path)
    else:
      logging.info('Printing ini content.')
      ini.Print()

  def InsertCommand(self, ini):
    """Processes insertion editing on Ini object.

    Inserts args.insert to section named args.section in input Ini object.
    If input Ini object does not have a section named args.section, this method
    does not do anything. If the editing is valid, prints the changed section
    to console. Writes the editied config file to the same path as input path
    plus a suffix speficied in args.output_suffix. If that suffix is "None",
    prompts and waits for user to confirm editing in-place.

    Args:
      ini: An Ini object.
    """
    if not ini.HasSection(self.args.section):
      logging.info('There is no section %s in %s',
                   self.args.section, ini.file_path)
      return

    ini.InsertLineToSection(self.args.section, self.args.insert)
    logging.info('Changed section:')
    ini.PrintSection(self.args.section)

    if self.args.output_suffix == 'None':
      answer = prompt(
          'Writing output file in-place at %s ? [y/n]' % ini.file_path)
      if not answer:
        sys.exit('Abort!')
      output_file = ini.file_path
    else:
      output_file = ini.file_path + self.args.output_suffix
      logging.info('Writing output file to : %s.', output_file)
    ini.Print(output_file)


if __name__ == '__main__':
  logging.basicConfig(
     format='%(asctime)s:%(levelname)s:%(filename)s:%(lineno)d:%(message)s',
     level=logging.DEBUG)
  IniEditor().Main()