Class: Dependabot::Python::FileParser::PyprojectDocument

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/python/file_parser/pyproject_document.rb

Defined Under Namespace

Classes: PoetrySource

Constant Summary collapse

PoetryRequirement =
T.type_alias { PyprojectValueParser::PoetryRequirement }
PoetryDependencyMap =
T.type_alias { PyprojectValueParser::PoetryDependencyMap }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ PyprojectDocument

Returns a new instance of PyprojectDocument.



102
103
104
# File 'lib/dependabot/python/file_parser/pyproject_document.rb', line 102

def initialize(data)
  @data = data
end

Class Method Details

.from_file(file) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/dependabot/python/file_parser/pyproject_document.rb', line 107

def self.from_file(file)
  content = T.must(file.content)
  parsed = T.cast(TomlRB.parse(content), Object)
  new(PyprojectValueParser.object_hash(parsed, "pyproject.toml"))
rescue TomlRB::ParseError, TomlRB::ValueOverwriteError
  raise Dependabot::DependencyFileNotParseable, file.path
end

Instance Method Details

#dynamic_fieldsObject



168
169
170
171
172
173
174
# File 'lib/dependabot/python/file_parser/pyproject_document.rb', line 168

def dynamic_fields
  project = section(@data, "project", "project")
  value = project&.[]("dynamic")
  return [] if value.nil?

  PyprojectValueParser.string_array(value, "project.dynamic")
end

#optional_dependency_group?(name) ⇒ Boolean

Returns:

  • (Boolean)


177
178
179
180
181
182
183
# File 'lib/dependabot/python/file_parser/pyproject_document.rb', line 177

def optional_dependency_group?(name)
  project = section(@data, "project", "project")
  value = project&.[]("optional-dependencies")
  return false if value.nil?

  PyprojectValueParser.object_hash(value, "project.optional-dependencies").key?(name)
end

#pep621?Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
# File 'lib/dependabot/python/file_parser/pyproject_document.rb', line 121

def pep621?
  project = section(@data, "project", "project")
  build_system = section(@data, "build-system", "build-system")

  !project&.[]("dependencies").nil? ||
    !project&.[]("optional-dependencies").nil? ||
    !build_system&.[]("requires").nil?
end

#pep735?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/dependabot/python/file_parser/pyproject_document.rb', line 131

def pep735?
  @data.key?("dependency-groups")
end

#poetry?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/dependabot/python/file_parser/pyproject_document.rb', line 116

def poetry?
  !poetry_root.nil?
end

#poetry_dependencies(type) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/dependabot/python/file_parser/pyproject_document.rb', line 136

def poetry_dependencies(type)
  root = poetry_root
  return {} unless root

  value = root[type]
  return {} if value.nil?

  PyprojectValueParser.poetry_dependency_map(value, "tool.poetry.#{type}")
end

#poetry_groupsObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/dependabot/python/file_parser/pyproject_document.rb', line 147

def poetry_groups
  root = poetry_root
  return {} unless root

  raw_groups = root["group"]
  return {} if raw_groups.nil?

  parsed_groups = PyprojectValueParser.object_hash(raw_groups, "tool.poetry.group")
  parsed_groups.each_with_object({}) do |(name, value), groups|
    group = PyprojectValueParser.object_hash(value, "tool.poetry.group.#{name}")
    dependencies = group["dependencies"]
    next if dependencies.nil?

    groups[name] = PyprojectValueParser.poetry_dependency_map(
      dependencies,
      "tool.poetry.group.#{name}.dependencies"
    )
  end
end

#poetry_source(name) ⇒ Object



186
187
188
# File 'lib/dependabot/python/file_parser/pyproject_document.rb', line 186

def poetry_source(name)
  poetry_sources.find { |source| source.name == name }
end

#workspace_globs(key) ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'lib/dependabot/python/file_parser/pyproject_document.rb', line 191

def workspace_globs(key)
  tool = section(@data, "tool", "tool")
  uv = tool && section(tool, "uv", "tool.uv")
  workspace = uv && section(uv, "workspace", "tool.uv.workspace")
  value = workspace&.[](key)
  return [] if value.nil?

  PyprojectValueParser.string_array(value, "tool.uv.workspace.#{key}")
end